






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     (  _A ):
	__snake_case						:Dict =							"new-model"


if is_tf_available():


						class 							__UpperCamelCase     (  _A ):
							__snake_case						:Optional[Any] =							NewModelConfig


@require_tf
class 							__UpperCamelCase     (  unittest.TestCase ):







	@slow
	def 			_a (	self   :				Optional[int]      )		->			List[str]:
		"""simple docstring"""







		__lowercase   							=	"""bert-base-cased"""
		__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
		self.assertIsNotNone(__lowerCAmelCase      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

		__lowercase   							=	TFAutoModel.from_pretrained(__lowerCAmelCase      )
		self.assertIsNotNone(__lowerCAmelCase      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	def 			_a (	self   :				Union[str, Any]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	"""bert-base-cased"""
		__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
		self.assertIsNotNone(__lowerCAmelCase      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

		__lowercase   							=	TFAutoModelForPreTraining.from_pretrained(__lowerCAmelCase      )
		self.assertIsNotNone(__lowerCAmelCase      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	def 			_a (	self   :				List[Any]      )		->			Tuple:
		"""simple docstring"""







		for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

			__lowercase   							=	TFAutoModelForCausalLM.from_pretrained(__lowerCAmelCase      )
			__lowercase							,    __lowercase   							=	TFAutoModelForCausalLM.from_pretrained(__lowerCAmelCase							,					output_loading_info=__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	def 			_a (	self   :				str      )		->			Optional[Any]:
		"""simple docstring"""







		for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

			__lowercase   							=	TFAutoModelWithLMHead.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	def 			_a (	self   :				List[str]      )		->			Union[str, Any]:
		"""simple docstring"""







		for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

			__lowercase   							=	TFAutoModelForMaskedLM.from_pretrained(__lowerCAmelCase      )
			__lowercase							,    __lowercase   							=	TFAutoModelForMaskedLM.from_pretrained(__lowerCAmelCase							,					output_loading_info=__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	def 			_a (	self   :				str      )		->			Dict:
		"""simple docstring"""







		for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

			__lowercase   							=	TFAutoModelForSeqaSeqLM.from_pretrained(__lowerCAmelCase      )
			__lowercase							,    __lowercase   							=	TFAutoModelForSeqaSeqLM.from_pretrained(__lowerCAmelCase							,					output_loading_info=__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	def 			_a (	self   :				int      )		->			Dict:
		"""simple docstring"""







		for model_name in ["bert-base-uncased"]:
			__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

			__lowercase   							=	TFAutoModelForSequenceClassification.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	def 			_a (	self   :				Any      )		->			Any:
		"""simple docstring"""







		for model_name in ["bert-base-uncased"]:
			__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

			__lowercase   							=	TFAutoModelForQuestionAnswering.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	@slow
	@require_tensorflow_probability
	def 			_a (	self   :				List[Any]      )		->			str:
		"""simple docstring"""







		for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
			__lowercase   							=	AutoConfig.from_pretrained(__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

			__lowercase   							=	TFAutoModelForTableQuestionAnswering.from_pretrained(__lowerCAmelCase      )
			__lowercase							,    __lowercase   							=	TFAutoModelForTableQuestionAnswering.from_pretrained(
			    __lowerCAmelCase							,					output_loading_info=__lowerCAmelCase      )
			self.assertIsNotNone(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	def 			_a (	self   :				str      )		->			List[str]:
		"""simple docstring"""







		__lowercase   							=	TFAutoModelWithLMHead.from_pretrained(__lowerCAmelCase      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )
		self.assertEqual(model.num_parameters()							,					1_4410      )
		self.assertEqual(model.num_parameters(only_trainable=__lowerCAmelCase      )							,					1_4410      )







	def 			_a (	self   :				List[str]      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	TFAutoModelWithLMHead.from_pretrained(__lowerCAmelCase      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )
		self.assertEqual(model.num_parameters()							,					1_4410      )
		self.assertEqual(model.num_parameters(only_trainable=__lowerCAmelCase      )							,					1_4410      )







	def 			_a (	self   :				Optional[int]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	TFAutoModel.from_pretrained("""sgugger/funnel-random-tiny"""      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

		__lowercase   							=	copy.deepcopy(model.config      )
		__lowercase   							=	["""FunnelBaseModel"""]
		__lowercase   							=	TFAutoModel.from_config(__lowerCAmelCase      )
		self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

		with tempfile.TemporaryDirectory() as tmp_dir:
			model.save_pretrained(__lowerCAmelCase      )
			__lowercase   							=	TFAutoModel.from_pretrained(__lowerCAmelCase      )
			self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )







	def 			_a (	self   :				List[str]      )		->			Optional[int]:
		"""simple docstring"""







		try:
			AutoConfig.register("""new-model"""							,					__lowerCAmelCase      )

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

					# Now that the config is registered, it can be used as any other config with the auto-API
					__lowercase   							=	BertModelTester(self      ).get_config()
					__lowercase   							=	NewModelConfig(**tiny_config.to_dict()      )
					__lowercase   							=	auto_class.from_config(__lowerCAmelCase      )
					self.assertIsInstance(__lowerCAmelCase							,					__lowerCAmelCase      )

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

		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[int]      )		->			Optional[int]:
		"""simple docstring"""







		with self.assertRaisesRegex(
		    __lowerCAmelCase							,					"""bert-base is not a local folder and is not a valid model identifier"""      ):
			__lowercase   							=	TFAutoModel.from_pretrained("""bert-base"""      )







	def 			_a (	self   :				int      )		->			str:
		"""simple docstring"""







		with self.assertRaisesRegex(
		    __lowerCAmelCase							,					r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""      ):
			__lowercase   							=	TFAutoModel.from_pretrained(__lowerCAmelCase							,					revision="""aaaaaa"""      )







	def 			_a (	self   :				Any      )		->			List[str]:
		"""simple docstring"""







		with self.assertRaisesRegex(
		    __lowerCAmelCase							,					"""hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"""							,					):
			__lowercase   							=	TFAutoModel.from_pretrained("""hf-internal-testing/config-no-model"""      )







	def 			_a (	self   :				List[str]      )		->			Any:
		"""simple docstring"""







		with self.assertRaisesRegex(__lowerCAmelCase							,					"""Use `from_pt=True` to load this model"""      ):
			__lowercase   							=	TFAutoModel.from_pretrained("""hf-internal-testing/tiny-bert-pt-only"""      )







	def 			_a (	self   :				List[Any]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	TFAutoModel.from_pretrained("""hf-internal-testing/tiny-random-bert"""      )
		with RequestCounter() as counter:
			__lowercase   							=	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   							=	TFAutoModel.from_pretrained("""ArthurZ/tiny-random-bert-sharded"""      )
		with RequestCounter() as counter:
			__lowercase   							=	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      )






def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:bytes				)  ->     str:
   return "".join([hex(_snake_case				)[2:].zfill(2				).upper() for byte in list(_snake_case				)]				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:str				)  ->     bytes:
   # Check data validity, following RFC3548
   # https://www.ietf.org/rfc/rfc3548.txt
   if (len(_snake_case				) % 2) != 0:
      raise ValueError(
          '''Base16 encoded data is invalid:
Data does not have an even number of hex digits.'''				)
   # Check the character set - the standard base16 alphabet
   # is uppercase according to RFC3548 section 6
   if not set(_snake_case				) <= set('''0123456789ABCDEF'''				):
      raise ValueError(
          '''Base16 encoded data is invalid:
Data is not uppercase hex or it contains invalid characters.'''				)
   # For every two hexadecimal digits (= a byte), turn it into an integer.
   # Then, string the result together into bytes, and return it.
   return bytes(int(data[i] + data[i + 1]	,   16				) for i in range(0	,   len(_snake_case				)	,   2				)				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()



