import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_tensorflow_text_available():
  from transformers.models.bert import TFBertTokenizer


UpperCAmelCase_			    =							['''bert-base-uncased''', '''bert-base-cased''']
UpperCAmelCase_			    =							'''hf-internal-testing/tiny-bert-tf-only'''

if is_tf_available():




  class  __magic_name__  (					tf.keras.Model    ):





     """simple docstring"""




     def __init__(	self					:  str		,  _lowercase					:  str							):



      """simple docstring"""



      super().__init__()
      _UpperCamelCase:     Dict							= tokenizer
      _UpperCamelCase:     Union[str, Any]							= AutoConfig.from_pretrained(_lowercase							)
      _UpperCamelCase:     Optional[int]							= TFAutoModel.from_config(_lowercase							)



     def   lowerCAmelCase						(	self					:  Dict		,  _lowercase					:  Tuple							):



      """simple docstring"""



      _UpperCamelCase:     Any							= self.tokenizer(_lowercase							)
      _UpperCamelCase:     Optional[int]							= self.bert(**_lowercase							)
      return out["pooler_output"]




@require_tf
@require_tensorflow_text
class  __magic_name__  (					unittest.TestCase    ):





   """simple docstring"""




   def   lowerCAmelCase						(	self					:  List[str]							):



    """simple docstring"""



    super().setUp()

    _UpperCamelCase:     List[str]							= [
        BertTokenizer.from_pretrained(_lowercase							) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
    ]  # repeat for when fast_bert_tokenizer=false
    _UpperCamelCase:     Any							= [TFBertTokenizer.from_pretrained(_lowercase							) for checkpoint in TOKENIZER_CHECKPOINTS] + [
        TFBertTokenizer.from_pretrained(_lowercase		,  use_fast_bert_tokenizer=_lowercase							)
        for checkpoint in TOKENIZER_CHECKPOINTS
    ]
    assert len(self.tokenizers							) == len(self.tf_tokenizers							)

    _UpperCamelCase:     Union[str, Any]							= [
        "This is a straightforward English test sentence.",
        "This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.",
        "Now we're going to add some Chinese: 一 二 三 一二三",
        "And some much more rare Chinese: 齉 堃 齉堃",
        "Je vais aussi écrire en français pour tester les accents",
        "Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ",
    ]
    _UpperCamelCase:     str							= list(zip(self.test_sentences		,  self.test_sentences[::-1]							)							)



   def   lowerCAmelCase						(	self					:  Tuple							):



    """simple docstring"""



    for tokenizer, tf_tokenizer in zip(self.tokenizers		,  self.tf_tokenizers							):
     for test_inputs in (self.test_sentences, self.paired_sentences):
      _UpperCamelCase:     Any							= tokenizer(_lowercase		,  return_tensors='''tf'''		,  padding='''longest'''							)
      _UpperCamelCase:     List[Any]							= tf_tokenizer(_lowercase							)

      for key in python_outputs.keys():
       self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape							)							)
       self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]		,  tf.intaa							) == tf_outputs[key]							)							)



   @slow
   def   lowerCAmelCase						(	self					:  Any							):



    """simple docstring"""



    for tf_tokenizer in self.tf_tokenizers:
     _UpperCamelCase:     List[Any]							= tf_tokenizer(self.paired_sentences							)
     _UpperCamelCase:     int							= tf_tokenizer(
         text=[sentence[0] for sentence in self.paired_sentences]		,  text_pair=[sentence[1] for sentence in self.paired_sentences]		,  )
     for key in merged_outputs.keys():
      self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]		,  tf.intaa							) == separated_outputs[key]							)							)



   @slow
   def   lowerCAmelCase						(	self					:  List[str]							):



    """simple docstring"""



    for tf_tokenizer in self.tf_tokenizers:
     _UpperCamelCase:     Any							= tf.function(_lowercase							)
     for test_inputs in (self.test_sentences, self.paired_sentences):
      _UpperCamelCase:     Any							= tf.constant(_lowercase							)
      _UpperCamelCase:     Union[str, Any]							= compiled_tokenizer(_lowercase							)
      _UpperCamelCase:     str							= tf_tokenizer(_lowercase							)

      for key in eager_outputs.keys():
       self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]							)							)



   @slow
   def   lowerCAmelCase						(	self					:  Tuple							):



    """simple docstring"""



    for tf_tokenizer in self.tf_tokenizers:
     _UpperCamelCase:     Tuple							= ModelToSave(tokenizer=_lowercase							)
     _UpperCamelCase:     Optional[Any]							= tf.convert_to_tensor(self.test_sentences							)
     _UpperCamelCase:     List[Any]							= model(_lowercase							)  # Build model with some sample inputs
     with TemporaryDirectory() as tempdir:
      _UpperCamelCase:     str							= Path(_lowercase							) / "saved.model"
      model.save(_lowercase							)
      _UpperCamelCase:     Any							= tf.keras.models.load_model(_lowercase							)
     _UpperCamelCase:     Any							= loaded_model(_lowercase							)
     # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
     self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output							)							)		,  1E-5							)
def       lowerCAmelCase_     (		lowercase:							int = 10**9		)    ->				int:







 '''simple docstring'''
 _UpperCamelCase:     List[Any]							= 1
 _UpperCamelCase:     Dict							= 2

 _UpperCamelCase:     Tuple							= 0
 _UpperCamelCase:     int							= 0
 _UpperCamelCase:     Dict							= 0
 while perimeter <= max_perimeter:
  perimeters_sum += perimeter

  prev_value += 2 * value
  value += prev_value

  _UpperCamelCase:     str							= 2 * value + 2 if i % 2 == 0 else 2 * value - 2
  i += 1

 return perimeters_sum


if __name__ == "__main__":
  print(f"""{solution() = }""")