


"""simple docstring"""







from manim import *






class 				__A    (	_SCREAMING_SNAKE_CASE      ):






 """simple docstring"""




 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     a					=Rectangle(height=0.5			,   width=0.5						)
     a					=Rectangle(height=0.46			,   width=0.46						).set_stroke(width=0						)

     a					=[mem.copy() for i in range(6						)]
     a					=[mem.copy() for i in range(6						)]
     a					=VGroup(*__A						).arrange(__A			,   buff=0						)
     a					=VGroup(*__A						).arrange(__A			,   buff=0						)
     a					=VGroup(__A			,   __A						).arrange(__A			,   buff=0						)
     a					=Text('''CPU'''			,   font_size=24						)
     a					=Group(__A			,   __A						).arrange(__A			,   buff=0.5			,   aligned_edge=__A						)
     cpu.move_to([-2.5, -0.5, 0]						)
     self.add(__A						)

     a					=[mem.copy() for i in range(4						)]
     a					=VGroup(*__A						).arrange(__A			,   buff=0						)
     a					=Text('''GPU'''			,   font_size=24						)
     a					=Group(__A			,   __A						).arrange(__A			,   buff=0.5			,   aligned_edge=__A						)
     gpu.move_to([-1, -1, 0]						)
     self.add(__A						)

     a					=[mem.copy() for i in range(6						)]
     a					=VGroup(*__A						).arrange(__A			,   buff=0						)

     a					=Text('''Model'''			,   font_size=24						)
     a					=Group(__A			,   __A						).arrange(__A			,   buff=0.5			,   aligned_edge=__A						)
     model.move_to([3, -1.0, 0]						)
     self.add(__A						)

     a					=[]
     for i, rect in enumerate(__A						):
         rect.set_stroke(__A						)
         # target = fill.copy().set_fill(YELLOW, opacity=0.7)
         # target.move_to(rect)
         # self.add(target)

         a					=Rectangle(height=0.46 / 4			,   width=0.46 / 3						).set_stroke(width=0.0						).set_fill(__A			,   opacity=0.7						)

         if i == 0:
             cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT						)			,   buff=0.02			,   direction=__A						)
             cpu_target.set_x(cpu_target.get_x() + 0.1						)
         elif i == 3:
             cpu_target.next_to(cpu_targs[0]			,   direction=__A			,   buff=0.0						)
         else:
             cpu_target.next_to(cpu_targs[i - 1]			,   direction=__A			,   buff=0.0						)
         self.add(__A						)
         cpu_targs.append(__A						)

     a					=[mem.copy() for i in range(6						)]
     a					=VGroup(*__A						).arrange(__A			,   buff=0						)

     a					=Text('''Loaded Checkpoint'''			,   font_size=24						)
     a					=Group(__A			,   __A						).arrange(__A			,   aligned_edge=__A			,   buff=0.4						)
     checkpoint.move_to([3, 0.5, 0]						)

     a					=Square(side_length=2.2						)
     key.move_to([-5, 2, 0]						)

     a					=MarkupText(
         f'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''			,   font_size=18			,   )

     key_text.move_to([-5, 2.4, 0]						)

     self.add(__A			,   __A						)

     a					=MarkupText(
         f'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''			,   font_size=18			,   )

     blue_text.next_to(__A			,   DOWN * 2.4			,   aligned_edge=key_text.get_left()						)

     a					=MarkupText(
         f'''Next, a <i><span fgcolor="{BLUE}">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor="{BLUE}">single shard</span>.'''			,   font_size=24			,   )
     step_a.move_to([2, 2, 0]						)
     self.play(Write(__A						)			,   Write(__A						)						)

     self.play(Write(__A			,   run_time=1						)			,   Create(__A			,   run_time=1						)						)

     a					=[]
     a					=[]
     for i, rect in enumerate(__A						):
         a					=fill.copy().set_fill(__A			,   opacity=0.7						)
         target.move_to(__A						)
         first_animations.append(GrowFromCenter(__A			,   run_time=1						)						)

         a					=target.copy()
         cpu_target.generate_target()
         if i < 5:
             cpu_target.target.move_to(cpu_left_col_base[i + 1]						)
         else:
             cpu_target.target.move_to(cpu_right_col_base[i - 5]						)
         second_animations.append(MoveToTarget(__A			,   run_time=1.5						)						)

     self.play(*__A						)
     self.play(*__A						)
     self.wait()



"""simple docstring"""







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


lowerCamelCase_       :       str      						=  ["""bert-base-uncased""", """bert-base-cased"""]
lowerCamelCase_       :       List[str]      						=  """hf-internal-testing/tiny-bert-tf-only"""

if is_tf_available():






       class 				__A    (	tf.keras.Model      ):






        """simple docstring"""




        def __init__(		self			,   __A						) ->					Dict:
            super().__init__()
            a					=tokenizer
            a					=AutoConfig.from_pretrained(__A						)
            a					=TFAutoModel.from_config(__A						)





        def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					int:
            a					=self.tokenizer(__A						)
            a					=self.bert(**__A						)
            return out["pooler_output"]






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






 """simple docstring"""




 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     super().setUp()

     a					=[
         BertTokenizer.from_pretrained(__A						) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
     ]  # repeat for when fast_bert_tokenizer=false
     a					=[TFBertTokenizer.from_pretrained(__A						) for checkpoint in TOKENIZER_CHECKPOINTS] + [
         TFBertTokenizer.from_pretrained(__A			,   use_fast_bert_tokenizer=__A						)
         for checkpoint in TOKENIZER_CHECKPOINTS
     ]
     assert len(self.tokenizers						) == len(self.tf_tokenizers						)

     a					=[
         '''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ċ, ꝼ''',
     ]
     a					=list(zip(self.test_sentences			,   self.test_sentences[::-1]						)						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[int]:
     for tokenizer, tf_tokenizer in zip(self.tokenizers			,   self.tf_tokenizers						):
         for test_inputs in (self.test_sentences, self.paired_sentences):
             a					=tokenizer(__A			,   return_tensors='''tf'''			,   padding='''longest'''						)
             a					=tf_tokenizer(__A						)

             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 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     for tf_tokenizer in self.tf_tokenizers:
         a					=tf_tokenizer(self.paired_sentences						)
         a					=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 				SCREAMING_SNAKE_CASE							(		self						) ->					List[str]:
     for tf_tokenizer in self.tf_tokenizers:
         a					=tf.function(__A						)
         for test_inputs in (self.test_sentences, self.paired_sentences):
             a					=tf.constant(__A						)
             a					=compiled_tokenizer(__A						)
             a					=tf_tokenizer(__A						)

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





 @slow
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     for tf_tokenizer in self.tf_tokenizers:
         a					=ModelToSave(tokenizer=__A						)
         a					=tf.convert_to_tensor(self.test_sentences						)
         a					=model(__A						)  # Build model with some sample inputs
         with TemporaryDirectory() as tempdir:
             a					=Path(__A						) / '''saved.model'''
             model.save(__A						)
             a					=tf.keras.models.load_model(__A						)
         a					=loaded_model(__A						)
         # 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						)