





"""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 		SCREAMING_SNAKE_CASE_							(    snake_case__   ):
				"""simple docstring"""
				__snake_case   :       List[str]	 =		"""new-model"""


if is_tf_available():
							class 		SCREAMING_SNAKE_CASE_							(    snake_case__   ):
											"""simple docstring"""
											__snake_case   :       Optional[int]	 =		NewModelConfig
@require_tf
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				@slow
				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      Tuple         ='''bert-base-cased'''
											__lowerCamelCase      :      str         =AutoConfig.from_pretrained(__lowercase			)
											self.assertIsNotNone(__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)

											__lowerCamelCase      :      List[str]         =TFAutoModel.from_pretrained(__lowercase			)
											self.assertIsNotNone(__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)




				@slow
				def 					__lowercase  (     self  :Union[str, Any]			):
											__lowerCamelCase      :      Optional[int]         ='''bert-base-cased'''
											__lowerCamelCase      :      str         =AutoConfig.from_pretrained(__lowercase			)
											self.assertIsNotNone(__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)

											__lowerCamelCase      :      List[Any]         =TFAutoModelForPreTraining.from_pretrained(__lowercase			)
											self.assertIsNotNone(__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)




				@slow
				def 					__lowercase  (     self  :Dict			):
											for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase      :      Tuple         =AutoConfig.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)

																		__lowerCamelCase      :      List[Any]         =TFAutoModelForCausalLM.from_pretrained(__lowercase			)
																		__lowerCamelCase    ,						__lowerCamelCase      :      Optional[Any]         =TFAutoModelForCausalLM.from_pretrained(__lowercase						,    output_loading_info=__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)




				@slow
				def 					__lowercase  (     self  :Any			):
											for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase      :      List[Any]         =AutoConfig.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)

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




				@slow
				def 					__lowercase  (     self  :Tuple			):
											for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase      :      List[str]         =AutoConfig.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)

																		__lowerCamelCase      :      Dict         =TFAutoModelForMaskedLM.from_pretrained(__lowercase			)
																		__lowerCamelCase    ,						__lowerCamelCase      :      Optional[Any]         =TFAutoModelForMaskedLM.from_pretrained(__lowercase						,    output_loading_info=__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)




				@slow
				def 					__lowercase  (     self  :List[str]			):
											for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase      :      Dict         =AutoConfig.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)

																		__lowerCamelCase      :      Tuple         =TFAutoModelForSeqaSeqLM.from_pretrained(__lowercase			)
																		__lowerCamelCase    ,						__lowerCamelCase      :      int         =TFAutoModelForSeqaSeqLM.from_pretrained(__lowercase						,    output_loading_info=__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)




				@slow
				def 					__lowercase  (     self  :Optional[Any]			):
											# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											for model_name in ["bert-base-uncased"]:
																		__lowerCamelCase      :      Optional[int]         =AutoConfig.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)

																		__lowerCamelCase      :      Optional[Any]         =TFAutoModelForSequenceClassification.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)




				@slow
				def 					__lowercase  (     self  :int			):
											# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											for model_name in ["bert-base-uncased"]:
																		__lowerCamelCase      :      Tuple         =AutoConfig.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)

																		__lowerCamelCase      :      Union[str, Any]         =TFAutoModelForQuestionAnswering.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)




				@slow
				@require_tensorflow_probability
				def 					__lowercase  (     self  :Dict			):
											for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
																		__lowerCamelCase      :      Any         =AutoConfig.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)

																		__lowerCamelCase      :      Tuple         =TFAutoModelForTableQuestionAnswering.from_pretrained(__lowercase			)
																		__lowerCamelCase    ,						__lowerCamelCase      :      Optional[int]         =TFAutoModelForTableQuestionAnswering.from_pretrained(
																		    __lowercase						,    output_loading_info=__lowercase			)
																		self.assertIsNotNone(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      Optional[Any]         =TFAutoModelWithLMHead.from_pretrained(__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)
											self.assertEqual(model.num_parameters()						,    1_4410			)
											self.assertEqual(model.num_parameters(only_trainable=__lowercase			)						,    1_4410			)




				def 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      str         =TFAutoModelWithLMHead.from_pretrained(__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)
											self.assertEqual(model.num_parameters()						,    1_4410			)
											self.assertEqual(model.num_parameters(only_trainable=__lowercase			)						,    1_4410			)




				def 					__lowercase  (     self  :Tuple			):
											# For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
											__lowerCamelCase      :      Optional[int]         =TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''			)
											self.assertIsInstance(__lowercase						,    __lowercase			)

											__lowerCamelCase      :      int         =copy.deepcopy(model.config			)
											__lowerCamelCase      :      Union[str, Any]         =['''FunnelBaseModel''']
											__lowerCamelCase      :      Tuple         =TFAutoModel.from_config(__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)

											with tempfile.TemporaryDirectory() as tmp_dir:
																		model.save_pretrained(__lowercase			)
																		__lowerCamelCase      :      Optional[Any]         =TFAutoModel.from_pretrained(__lowercase			)
																		self.assertIsInstance(__lowercase						,    __lowercase			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											try:
																		AutoConfig.register('''new-model'''						,    __lowercase			)

																		__lowerCamelCase      :      Optional[Any]         =[
																		    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(__lowercase			):
																																							auto_class.register(__lowercase						,    __lowercase			)
																																auto_class.register(__lowercase						,    __lowercase			)
																																# Trying to register something existing in the Transformers library will raise an error
																																with self.assertRaises(__lowercase			):
																																							auto_class.register(__lowercase						,    __lowercase			)

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

																																with tempfile.TemporaryDirectory() as tmp_dir:
																																							model.save_pretrained(__lowercase			)
																																							__lowerCamelCase      :      int         =auto_class.from_pretrained(__lowercase			)
																																							self.assertIsInstance(__lowercase						,    __lowercase			)

											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 					__lowercase  (     self  :str			):
											with self.assertRaisesRegex(
											    __lowercase						,    '''bert-base is not a local folder and is not a valid model identifier'''			):
																		__lowerCamelCase      :      int         =TFAutoModel.from_pretrained('''bert-base'''			)




				def 					__lowercase  (     self  :List[str]			):
											with self.assertRaisesRegex(
											    __lowercase						,    r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''			):
																		__lowerCamelCase      :      List[str]         =TFAutoModel.from_pretrained(__lowercase						,    revision='''aaaaaa'''			)




				def 					__lowercase  (     self  :Any			):
											with self.assertRaisesRegex(
											    __lowercase						,    '''hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'''						,    ):
																		__lowerCamelCase      :      List[str]         =TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											with self.assertRaisesRegex(__lowercase						,    '''Use `from_pt=True` to load this model'''			):
																		__lowerCamelCase      :      Any         =TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''			)




				def 					__lowercase  (     self  :Tuple			):
											# Make sure we have cached the model.
											__lowerCamelCase      :      Optional[Any]         =TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''			)
											with RequestCounter() as counter:
																		__lowerCamelCase      :      Dict         =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
											__lowerCamelCase      :      Tuple         =TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''			)
											with RequestCounter() as counter:
																		__lowerCamelCase      :      str         =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"""





import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      Any         =Vector([1, 2, 3]			)
											self.assertEqual(x.component(0			)						,    1			)
											self.assertEqual(x.component(2			)						,    3			)
											__lowerCamelCase      :      Any         =Vector()




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Tuple         =Vector([0, 0, 0, 0, 0, 1]			)
											self.assertEqual(str(__lowercase			)						,    '''(0,0,0,0,0,1)'''			)




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      int         =Vector([1, 2, 3, 4]			)
											self.assertEqual(len(__lowercase			)						,    4			)




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Optional[Any]         =Vector([1, 2]			)
											__lowerCamelCase      :      Dict         =Vector([1, 2, 3, 4, 5]			)
											__lowerCamelCase      :      List[Any]         =Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]			)
											__lowerCamelCase      :      int         =Vector([1, -1, 1, -1, 2, -3, 4, -5]			)
											self.assertAlmostEqual(x.euclidean_length()						,    2.236						,    3			)
											self.assertAlmostEqual(y.euclidean_length()						,    7.416						,    3			)
											self.assertEqual(z.euclidean_length()						,    0			)
											self.assertAlmostEqual(w.euclidean_length()						,    7.616						,    3			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      Tuple         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      Any         =Vector([1, 1, 1]			)
											self.assertEqual((x + y).component(0			)						,    2			)
											self.assertEqual((x + y).component(1			)						,    3			)
											self.assertEqual((x + y).component(2			)						,    4			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Union[str, Any]         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      int         =Vector([1, 1, 1]			)
											self.assertEqual((x - y).component(0			)						,    0			)
											self.assertEqual((x - y).component(1			)						,    1			)
											self.assertEqual((x - y).component(2			)						,    2			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      List[Any]         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      List[Any]         =Vector([2, -1, 4]			)  # for test of dot product
											__lowerCamelCase      :      Any         =Vector([1, -2, -1]			)
											self.assertEqual(str(x * 3.0			)						,    '''(3.0,6.0,9.0)'''			)
											self.assertEqual((a * b)						,    0			)




				def 					__lowercase  (     self  :List[Any]			):
											self.assertEqual(str(zero_vector(10			)			).count('''0'''			)						,    10			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											self.assertEqual(str(unit_basis_vector(3						,    1			)			)						,    '''(0,1,0)'''			)




				def 					__lowercase  (     self  :List[Any]			):
											__lowerCamelCase      :      Any         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      Optional[int]         =Vector([1, 0, 1]			)
											self.assertEqual(str(axpy(2						,    __lowercase						,    __lowercase			)			)						,    '''(3,4,7)'''			)




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      List[Any]         =Vector([1, 0, 0, 0, 0, 0]			)
											__lowerCamelCase      :      Optional[int]         =x.copy()
											self.assertEqual(str(__lowercase			)						,    str(__lowercase			)			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      str         =Vector([1, 0, 0]			)
											x.change_component(0						,    0			)
											x.change_component(1						,    1			)
											self.assertEqual(str(__lowercase			)						,    '''(0,1,0)'''			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Any         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											self.assertEqual('''|1,2,3|\n|2,4,5|\n|6,7,8|\n'''						,    str(__lowercase			)			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Tuple         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      List[Any]         =[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
											for x in range(a.height()			):
																		for y in range(a.width()			):
																									self.assertEqual(minors[x][y]						,    a.minor(__lowercase						,    __lowercase			)			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      Optional[Any]         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      Tuple         =[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
											for x in range(a.height()			):
																		for y in range(a.width()			):
																									self.assertEqual(cofactors[x][y]						,    a.cofactor(__lowercase						,    __lowercase			)			)




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      Tuple         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											self.assertEqual(-5						,    a.determinant()			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Union[str, Any]         =Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]						,    3						,    3			)
											__lowerCamelCase      :      Tuple         =Vector([1, 2, 3]			)
											self.assertEqual('''(14,32,50)'''						,    str(a * x			)			)
											self.assertEqual('''|2,4,6|\n|8,10,12|\n|14,16,18|\n'''						,    str(a * 2			)			)




				def 					__lowercase  (     self  :Optional[Any]			):
											__lowerCamelCase      :      Optional[int]         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											a.change_component(0						,    2						,    5			)
											self.assertEqual('''|1,2,5|\n|2,4,5|\n|6,7,8|\n'''						,    str(__lowercase			)			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      str         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											self.assertEqual(7						,    a.component(2						,    1			)						,    0.01			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      List[str]         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      List[str]         =Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]						,    3						,    3			)
											self.assertEqual('''|2,4,10|\n|4,8,10|\n|12,14,18|\n'''						,    str(a + b			)			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											__lowerCamelCase      :      int         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      Optional[int]         =Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]						,    3						,    3			)
											self.assertEqual('''|0,0,-4|\n|0,0,0|\n|0,0,-2|\n'''						,    str(a - b			)			)




				def 					__lowercase  (     self  :Any			):
											self.assertEqual(
											    '''|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n'''						,    str(square_zero_matrix(5			)			)						,    )


if __name__ == "__main__":
							unittest.main()




