


'''simple docstring'''
from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
					from transformers import (
					    TFAutoModel,
					    TFAutoModelForCausalLM,
					    TFAutoModelForMaskedLM,
					    TFAutoModelForPreTraining,
					    TFAutoModelForQuestionAnswering,
					    TFAutoModelForSeqaSeqLM,
					    TFAutoModelForSequenceClassification,
					    TFAutoModelForTableQuestionAnswering,
					    TFAutoModelForTokenClassification,
					    TFAutoModelWithLMHead,
					    TFBertForMaskedLM,
					    TFBertForPreTraining,
					    TFBertForQuestionAnswering,
					    TFBertForSequenceClassification,
					    TFBertModel,
					    TFFunnelBaseModel,
					    TFFunnelModel,
					    TFGPTaLMHeadModel,
					    TFRobertaForMaskedLM,
					    TFTaForConditionalGeneration,
					    TFTapasForQuestionAnswering,
					)
					from transformers.models.auto.modeling_tf_auto import (
					    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
					    TF_MODEL_FOR_MASKED_LM_MAPPING,
					    TF_MODEL_FOR_PRETRAINING_MAPPING,
					    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
					    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
					    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
					    TF_MODEL_MAPPING,
					)
					from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
					from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
					from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
					from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST




class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE):


				'''simple docstring'''


				_snake_case						       =				'''new-model'''


if is_tf_available():




					class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE):


									'''simple docstring'''


									_snake_case						       =				NewModelConfig




@require_tf
class        _UpperCamelCase	(    unittest.TestCase):


				'''simple docstring'''


				@slow
				def 	a__		(     self							)    ->  List[Any]:
					lowercase						:		Optional[Any]   					=		"bert-base-cased"
					lowercase						:		Optional[int]   					=		AutoConfig.from_pretrained(a_							)
					self.assertIsNotNone(a_							)
					self.assertIsInstance(a_							,      a_							)

					lowercase						:		Tuple   					=		TFAutoModel.from_pretrained(a_							)
					self.assertIsNotNone(a_							)
					self.assertIsInstance(a_							,      a_							)

				@slow
				def 	a__		(     self							)    ->  Optional[Any]:
					lowercase						:		Union[str, Any]   					=		"bert-base-cased"
					lowercase						:		Any   					=		AutoConfig.from_pretrained(a_							)
					self.assertIsNotNone(a_							)
					self.assertIsInstance(a_							,      a_							)

					lowercase						:		Union[str, Any]   					=		TFAutoModelForPreTraining.from_pretrained(a_							)
					self.assertIsNotNone(a_							)
					self.assertIsInstance(a_							,      a_							)

				@slow
				def 	a__		(     self							)    ->  List[str]:
					for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase						:		Optional[int]   					=		AutoConfig.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

						lowercase						:		List[str]   					=		TFAutoModelForCausalLM.from_pretrained(a_							)
						lowercase					,		lowercase						:		Dict   					=		TFAutoModelForCausalLM.from_pretrained(a_							,      output_loading_info=a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

				@slow
				def 	a__		(     self							)    ->  Optional[Any]:
					for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase						:		Optional[int]   					=		AutoConfig.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

						lowercase						:		Union[str, Any]   					=		TFAutoModelWithLMHead.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

				@slow
				def 	a__		(     self							)    ->  List[str]:
					for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase						:		Optional[Any]   					=		AutoConfig.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

						lowercase						:		List[str]   					=		TFAutoModelForMaskedLM.from_pretrained(a_							)
						lowercase					,		lowercase						:		Any   					=		TFAutoModelForMaskedLM.from_pretrained(a_							,      output_loading_info=a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

				@slow
				def 	a__		(     self							)    ->  List[str]:
					for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase						:		Dict   					=		AutoConfig.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

						lowercase						:		List[Any]   					=		TFAutoModelForSeqaSeqLM.from_pretrained(a_							)
						lowercase					,		lowercase						:		List[Any]   					=		TFAutoModelForSeqaSeqLM.from_pretrained(a_							,      output_loading_info=a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

				@slow
				def 	a__		(     self							)    ->  Union[str, Any]:
					# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					for model_name in ["bert-base-uncased"]:
						lowercase						:		Union[str, Any]   					=		AutoConfig.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

						lowercase						:		Tuple   					=		TFAutoModelForSequenceClassification.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

				@slow
				def 	a__		(     self							)    ->  List[Any]:
					# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					for model_name in ["bert-base-uncased"]:
						lowercase						:		List[Any]   					=		AutoConfig.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

						lowercase						:		Dict   					=		TFAutoModelForQuestionAnswering.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

				@slow
				@require_tensorflow_probability
				def 	a__		(     self							)    ->  Optional[int]:
					for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
						lowercase						:		str   					=		AutoConfig.from_pretrained(a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

						lowercase						:		List[str]   					=		TFAutoModelForTableQuestionAnswering.from_pretrained(a_							)
						lowercase					,		lowercase						:		Any   					=		TFAutoModelForTableQuestionAnswering.from_pretrained(
						    a_							,      output_loading_info=a_							)
						self.assertIsNotNone(a_							)
						self.assertIsInstance(a_							,      a_							)

				def 	a__		(     self							)    ->  Union[str, Any]:
					lowercase						:		str   					=		TFAutoModelWithLMHead.from_pretrained(a_							)
					self.assertIsInstance(a_							,      a_							)
					self.assertEqual(model.num_parameters()							,      1_4_4_1_0							)
					self.assertEqual(model.num_parameters(only_trainable=a_							)							,      1_4_4_1_0							)

				def 	a__		(     self							)    ->  List[Any]:
					lowercase						:		List[Any]   					=		TFAutoModelWithLMHead.from_pretrained(a_							)
					self.assertIsInstance(a_							,      a_							)
					self.assertEqual(model.num_parameters()							,      1_4_4_1_0							)
					self.assertEqual(model.num_parameters(only_trainable=a_							)							,      1_4_4_1_0							)

				def 	a__		(     self							)    ->  Dict:
					# For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
					lowercase						:		Optional[int]   					=		TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"							)
					self.assertIsInstance(a_							,      a_							)

					lowercase						:		Optional[int]   					=		copy.deepcopy(model.config							)
					lowercase						:		Optional[Any]   					=		["FunnelBaseModel"]
					lowercase						:		Tuple   					=		TFAutoModel.from_config(a_							)
					self.assertIsInstance(a_							,      a_							)

					with tempfile.TemporaryDirectory() as tmp_dir:
						model.save_pretrained(a_							)
						lowercase						:		Optional[int]   					=		TFAutoModel.from_pretrained(a_							)
						self.assertIsInstance(a_							,      a_							)

				def 	a__		(     self							)    ->  List[Any]:
					try:
						AutoConfig.register("new-model"							,      a_							)

						lowercase						:		Tuple   					=		[
						    TFAutoModel,
						    TFAutoModelForCausalLM,
						    TFAutoModelForMaskedLM,
						    TFAutoModelForPreTraining,
						    TFAutoModelForQuestionAnswering,
						    TFAutoModelForSequenceClassification,
						    TFAutoModelForTokenClassification,
						]

						for auto_class in auto_classes:
							with self.subTest(auto_class.__name__							):
								# Wrong config class will raise an error
								with self.assertRaises(a_							):
									auto_class.register(a_							,      a_							)
								auto_class.register(a_							,      a_							)
								# Trying to register something existing in the Transformers library will raise an error
								with self.assertRaises(a_							):
									auto_class.register(a_							,      a_							)

								# Now that the config is registered, it can be used as any other config with the auto-API
								lowercase						:		Tuple   					=		BertModelTester(self							).get_config()
								lowercase						:		Union[str, Any]   					=		NewModelConfig(**tiny_config.to_dict()							)
								lowercase						:		Optional[int]   					=		auto_class.from_config(a_							)
								self.assertIsInstance(a_							,      a_							)

								with tempfile.TemporaryDirectory() as tmp_dir:
									model.save_pretrained(a_							)
									lowercase						:		Tuple   					=		auto_class.from_pretrained(a_							)
									self.assertIsInstance(a_							,      a_							)

					finally:
						if "new-model" in CONFIG_MAPPING._extra_content:
							del CONFIG_MAPPING._extra_content["new-model"]
						for mapping in (
						    TF_MODEL_MAPPING,
						    TF_MODEL_FOR_PRETRAINING_MAPPING,
						    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
						    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
						    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
						    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
						    TF_MODEL_FOR_MASKED_LM_MAPPING,
						):
							if NewModelConfig in mapping._extra_content:
								del mapping._extra_content[NewModelConfig]

				def 	a__		(     self							)    ->  Optional[Any]:
					with self.assertRaisesRegex(
					    a_							,      "bert-base is not a local folder and is not a valid model identifier"							):
						lowercase						:		Tuple   					=		TFAutoModel.from_pretrained("bert-base"							)

				def 	a__		(     self							)    ->  Dict:
					with self.assertRaisesRegex(
					    a_							,      R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"							):
						lowercase						:		Dict   					=		TFAutoModel.from_pretrained(a_							,      revision="aaaaaa"							)

				def 	a__		(     self							)    ->  Any:
					with self.assertRaisesRegex(
					    a_							,      "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"							,      ):
						lowercase						:		Tuple   					=		TFAutoModel.from_pretrained("hf-internal-testing/config-no-model"							)

				def 	a__		(     self							)    ->  Tuple:
					with self.assertRaisesRegex(a_							,      "Use `from_pt=True` to load this model"							):
						lowercase						:		Optional[Any]   					=		TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"							)


				def 	a__		(     self							)    ->  Dict:
					# Make sure we have cached the model.
					lowercase						:		List[Any]   					=		TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"							)
					with RequestCounter() as counter:
						lowercase						:		Optional[Any]   					=		TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"							)
						self.assertEqual(counter.get_request_count							,      0							)
						self.assertEqual(counter.head_request_count							,      1							)
						self.assertEqual(counter.other_request_count							,      0							)

					# With a sharded checkpoint
					lowercase						:		Optional[int]   					=		TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"							)
					with RequestCounter() as counter:
						lowercase						:		Optional[int]   					=		TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"							)
						self.assertEqual(counter.get_request_count							,      0							)
						self.assertEqual(counter.head_request_count							,      1							)
						self.assertEqual(counter.other_request_count							,      0							)






'''simple docstring'''
from manim import *




class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE):


				'''simple docstring'''


				def 	a__		(     self							)    ->  List[str]:
					lowercase						:		List[Any]   					=		Rectangle(height=0.5							,      width=0.5							)
					lowercase						:		str   					=		Rectangle(height=0.25							,      width=0.25							)
					lowercase						:		Union[str, Any]   					=		Rectangle(height=0.46							,      width=0.46							).set_stroke(width=0							)

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

					lowercase						:		List[Any]   					=		[mem.copy() for i in range(4							)]
					lowercase						:		Union[str, Any]   					=		VGroup(*a_							).arrange(a_							,      buff=0							)
					lowercase						:		Dict   					=		Text("GPU"							,      font_size=2_4							)
					lowercase						:		Tuple   					=		Group(a_							,      a_							).arrange(a_							,      buff=0.5							,      aligned_edge=a_							)
					gpu.move_to([-1, -1, 0]							)
					self.add(a_							)

					lowercase						:		Tuple   					=		[mem.copy() for i in range(6							)]
					lowercase						:		Optional[int]   					=		VGroup(*a_							).arrange(a_							,      buff=0							)

					lowercase						:		Any   					=		Text("Model"							,      font_size=2_4							)
					lowercase						:		str   					=		Group(a_							,      a_							).arrange(a_							,      buff=0.5							,      aligned_edge=a_							)
					model.move_to([3, -1.0, 0]							)
					self.add(a_							)

					lowercase						:		Dict   					=		[]
					lowercase						:		Tuple   					=		[]
					lowercase						:		List[Any]   					=		[]

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

						lowercase						:		Tuple   					=		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(model_cpu_arr[0]							,      direction=a_							,      buff=0.0							)
						else:
							cpu_target.next_to(model_cpu_arr[i - 1]							,      direction=a_							,      buff=0.0							)
						self.add(a_							)
						model_cpu_arr.append(a_							)

					self.add(*a_							,      *a_							,      *a_							)

					lowercase						:		Any   					=		[mem.copy() for i in range(6							)]
					lowercase						:		Dict   					=		VGroup(*a_							).arrange(a_							,      buff=0							)

					lowercase						:		List[str]   					=		Text("Loaded Checkpoint"							,      font_size=2_4							)
					lowercase						:		Optional[Any]   					=		Group(a_							,      a_							).arrange(a_							,      buff=0.5							,      aligned_edge=a_							)
					checkpoint.move_to([3, 0.5, 0]							)

					self.add(a_							)

					lowercase						:		Any   					=		[]
					lowercase						:		int   					=		[]

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

						lowercase						:		Optional[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(a_							)
					self.add(*a_							,      *a_							)

					lowercase						:		Optional[Any]   					=		Square(side_length=2.2							)
					key.move_to([-5, 2, 0]							)

					lowercase						:		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(a_							,      a_							)

					lowercase						:		Any   					=		MarkupText(
					    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''							,      font_size=1_8							,      )

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

					lowercase						:		List[Any]   					=		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]							)

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

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

					self.play(FadeOut(a_							)							)

					lowercase						:		List[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(a_							,      run_time=3							)							)

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

					self.wait()


