


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			:  Any     =["""bert-base-uncased""", """bert-base-cased"""]
_UpperCAmelCase			:  Dict     ="""hf-internal-testing/tiny-bert-tf-only"""

if is_tf_available():







						class 				snake_case__(							tf.keras.Model							):







												'''simple docstring'''




												def __init__(				self   ,						__lowercase							)					->    Any:
																super().__init__()
																lowerCAmelCase_ :      Any    				=				tokenizer
																lowerCAmelCase_ :      Union[str, Any]    				=				AutoConfig.from_pretrained(__lowercase							)
																lowerCAmelCase_ :      Any    				=				TFAutoModel.from_config(__lowercase							)




												def 						lowercase_ (				self   ,						__lowercase							)					->    Tuple:
																lowerCAmelCase_ :      Tuple    				=				self.tokenizer(__lowercase							)
																lowerCAmelCase_ :      Optional[Any]    				=				self.bert(**__lowercase							)
																return out["pooler_output"]







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







						'''simple docstring'''




						def 						lowercase_ (				self							)					->    int:
										super().setUp()

										lowerCAmelCase_ :      Any    				=				[
										    BertTokenizer.from_pretrained(__lowercase							) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
										]  # repeat for when fast_bert_tokenizer=false
										lowerCAmelCase_ :      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							)

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




						def 						lowercase_ (				self							)					->    int:
										for tokenizer, tf_tokenizer in zip(self.tokenizers   ,						self.tf_tokenizers							):
														for test_inputs in (self.test_sentences, self.paired_sentences):
																		lowerCAmelCase_ :      Union[str, Any]    				=				tokenizer(__lowercase   ,						return_tensors='''tf'''   ,						padding='''longest'''							)
																		lowerCAmelCase_ :      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 						lowercase_ (				self							)					->    Union[str, Any]:
										for tf_tokenizer in self.tf_tokenizers:
														lowerCAmelCase_ :      Tuple    				=				tf_tokenizer(self.paired_sentences							)
														lowerCAmelCase_ :      Optional[Any]    				=				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 						lowercase_ (				self							)					->    Union[str, Any]:
										for tf_tokenizer in self.tf_tokenizers:
														lowerCAmelCase_ :      int    				=				tf.function(__lowercase							)
														for test_inputs in (self.test_sentences, self.paired_sentences):
																		lowerCAmelCase_ :      Optional[int]    				=				tf.constant(__lowercase							)
																		lowerCAmelCase_ :      Any    				=				compiled_tokenizer(__lowercase							)
																		lowerCAmelCase_ :      Tuple    				=				tf_tokenizer(__lowercase							)

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




						@slow
						def 						lowercase_ (				self							)					->    Optional[int]:
										for tf_tokenizer in self.tf_tokenizers:
														lowerCAmelCase_ :      Any    				=				ModelToSave(tokenizer=__lowercase							)
														lowerCAmelCase_ :      Dict    				=				tf.convert_to_tensor(self.test_sentences							)
														lowerCAmelCase_ :      Dict    				=				model(__lowercase							)  # Build model with some sample inputs
														with TemporaryDirectory() as tempdir:
																		lowerCAmelCase_ :      str    				=				Path(__lowercase							) / '''saved.model'''
																		model.save(__lowercase							)
																		lowerCAmelCase_ :      Tuple    				=				tf.keras.models.load_model(__lowercase							)
														lowerCAmelCase_ :      Dict    				=				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							)



from manim import *







class 				snake_case__(							UpperCAmelCase__							):







						'''simple docstring'''




						def 						lowercase_ (				self							)					->    Tuple:
										lowerCAmelCase_ :      Dict    				=				Rectangle(height=0.5   ,						width=0.5							)
										lowerCAmelCase_ :      Tuple    				=				Rectangle(height=0.25   ,						width=0.25							)
										lowerCAmelCase_ :      Tuple    				=				Rectangle(height=0.46   ,						width=0.46							).set_stroke(width=0							)

										lowerCAmelCase_ :      Optional[int]    				=				[mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      int    				=				[mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      Optional[int]    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      List[str]    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      int    				=				VGroup(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      Tuple    				=				Text('''CPU'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      Union[str, Any]    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										cpu.move_to([-2.5, -0.5, 0]							)
										self.add(__lowercase							)

										lowerCAmelCase_ :      List[str]    				=				[mem.copy() for i in range(4							)]
										lowerCAmelCase_ :      Any    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      List[Any]    				=				Text('''GPU'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      int    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										gpu.move_to([-1, -1, 0]							)
										self.add(__lowercase							)

										lowerCAmelCase_ :      str    				=				[mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      Dict    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)

										lowerCAmelCase_ :      Dict    				=				Text('''Model'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      str    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										model.move_to([3, -1.0, 0]							)
										self.add(__lowercase							)

										lowerCAmelCase_ :      int    				=				[]
										lowerCAmelCase_ :      int    				=				[]
										lowerCAmelCase_ :      Dict    				=				[]

										for i, rect in enumerate(__lowercase							):
														rect.set_stroke(__lowercase							)

														lowerCAmelCase_ :      Any    				=				Rectangle(height=0.46 / 4   ,						width=0.46 / 3							).set_stroke(width=0.0							).set_fill(__lowercase   ,						opacity=0.7							)

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

										self.add(*__lowercase   ,						*__lowercase   ,						*__lowercase							)

										lowerCAmelCase_ :      Union[str, Any]    				=				[mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      List[str]    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)

										lowerCAmelCase_ :      Union[str, Any]    				=				Text('''Loaded Checkpoint'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      int    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										checkpoint.move_to([3, 0.5, 0]							)

										self.add(__lowercase							)

										lowerCAmelCase_ :      Optional[Any]    				=				[]
										lowerCAmelCase_ :      Dict    				=				[]

										for i, rect in enumerate(__lowercase							):
														lowerCAmelCase_ :      str    				=				fill.copy().set_fill(__lowercase   ,						opacity=0.7							)
														target.move_to(__lowercase							)
														ckpt_arr.append(__lowercase							)

														lowerCAmelCase_ :      Union[str, Any]    				=				target.copy()
														if i < 5:
																		cpu_target.move_to(cpu_left_col_base[i + 1]							)
														else:
																		cpu_target.move_to(cpu_right_col_base[i - 5]							)
														ckpt_cpu_arr.append(__lowercase							)
										self.add(*__lowercase   ,						*__lowercase							)

										lowerCAmelCase_ :      Union[str, Any]    				=				Square(side_length=2.2							)
										key.move_to([-5, 2, 0]							)

										lowerCAmelCase_ :      str    				=				MarkupText(
										    f"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""   ,						font_size=1_8   ,						)

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

										self.add(__lowercase   ,						__lowercase							)

										lowerCAmelCase_ :      str    				=				MarkupText(
										    f"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""   ,						font_size=1_8   ,						)

										blue_text.next_to(__lowercase   ,						DOWN * 2.4   ,						aligned_edge=key_text.get_left()							)
										self.add(__lowercase							)

										lowerCAmelCase_ :      str    				=				MarkupText(
										    f"""Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device."""   ,						font_size=2_4   ,						)
										step_a.move_to([2, 2, 0]							)

										lowerCAmelCase_ :      List[Any]    				=				[meta_mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      Any    				=				[meta_mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      Any    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      Union[str, Any]    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      int    				=				VGroup(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      List[str]    				=				Text('''Disk'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      Optional[int]    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										disk.move_to([-4.0, -1.25, 0]							)
										self.play(Write(__lowercase   ,						run_time=3							)   ,						Write(__lowercase   ,						run_time=1							)   ,						Create(__lowercase   ,						run_time=1							)							)

										lowerCAmelCase_ :      int    				=				[]
										for i, rect in enumerate(__lowercase							):
														lowerCAmelCase_ :      int    				=				rect.copy()
														target.generate_target()
														target.target.move_to(disk_left_col_base[i]							).scale(0.5							)
														animations.append(MoveToTarget(__lowercase   ,						run_time=1.5							)							)
										self.play(*__lowercase							)

										self.play(FadeOut(__lowercase							)							)

										lowerCAmelCase_ :      Union[str, Any]    				=				MarkupText(f"""Then, the checkpoint is removed from memory\nthrough garbage collection."""   ,						font_size=2_4							)
										step_a.move_to([2, 2, 0]							)

										self.play(Write(__lowercase   ,						run_time=3							)							)

										self.play(
										    FadeOut(__lowercase   ,						__lowercase   ,						*__lowercase   ,						*__lowercase							)   ,						)

										self.wait()