

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    (					lowercase						):
	SCREAMING_SNAKE_CASE__    				=     'new-model'


if is_tf_available():

				class 						__UpperCamelCase    (					lowercase						):
					SCREAMING_SNAKE_CASE__    				=     NewModelConfig

@require_tf
class 						__UpperCamelCase    (					unittest.TestCase						):




	@slow
	def       __A					(		self	:						Tuple							):


		'''simple docstring'''







		UpperCAmelCase_       				=			"bert-base-cased"
		UpperCAmelCase_       				=			AutoConfig.from_pretrained(lowerCAmelCase							)
		self.assertIsNotNone(lowerCAmelCase							)
		self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)

		UpperCAmelCase_       				=			TFAutoModel.from_pretrained(lowerCAmelCase							)
		self.assertIsNotNone(lowerCAmelCase							)
		self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	def       __A					(		self	:						List[str]							):


		'''simple docstring'''







		UpperCAmelCase_       				=			"bert-base-cased"
		UpperCAmelCase_       				=			AutoConfig.from_pretrained(lowerCAmelCase							)
		self.assertIsNotNone(lowerCAmelCase							)
		self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)

		UpperCAmelCase_       				=			TFAutoModelForPreTraining.from_pretrained(lowerCAmelCase							)
		self.assertIsNotNone(lowerCAmelCase							)
		self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	def       __A					(		self	:						List[Any]							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase							)
			UpperCAmelCase_				,   UpperCAmelCase_       				=			TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase		,	output_loading_info=lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)
			self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	def       __A					(		self	:						Optional[Any]							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)
			self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	def       __A					(		self	:						Tuple							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase							)
			UpperCAmelCase_				,   UpperCAmelCase_       				=			TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase		,	output_loading_info=lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)
			self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	def       __A					(		self	:						int							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase							)
			UpperCAmelCase_				,   UpperCAmelCase_       				=			TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase		,	output_loading_info=lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)
			self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	def       __A					(		self	:						int							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			TFAutoModelForSequenceClassification.from_pretrained(lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)
			self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	def       __A					(		self	:						Tuple							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			TFAutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)
			self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	@slow
	@require_tensorflow_probability
	def       __A					(		self	:						Union[str, Any]							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			TFAutoModelForTableQuestionAnswering.from_pretrained(lowerCAmelCase							)
			UpperCAmelCase_				,   UpperCAmelCase_       				=			TFAutoModelForTableQuestionAnswering.from_pretrained(
			    lowerCAmelCase		,	output_loading_info=lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)
			self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)




	def       __A					(		self	:						int							):


		'''simple docstring'''







		UpperCAmelCase_       				=			TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase							)
		self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)
		self.assertEqual(model.num_parameters()		,	14_410							)
		self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase							)		,	14_410							)




	def       __A					(		self	:						List[str]							):


		'''simple docstring'''







		UpperCAmelCase_       				=			TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase							)
		self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)
		self.assertEqual(model.num_parameters()		,	14_410							)
		self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase							)		,	14_410							)




	def       __A					(		self	:						Optional[Any]							):


		'''simple docstring'''







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

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

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




	def       __A					(		self	:						List[str]							):


		'''simple docstring'''







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

			UpperCAmelCase_       				=			[
			    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
					UpperCAmelCase_       				=			BertModelTester(self							).get_config()
					UpperCAmelCase_       				=			NewModelConfig(**tiny_config.to_dict()							)
					UpperCAmelCase_       				=			auto_class.from_config(lowerCAmelCase							)
					self.assertIsInstance(lowerCAmelCase		,	lowerCAmelCase							)

					with tempfile.TemporaryDirectory() as tmp_dir:
						model.save_pretrained(lowerCAmelCase							)
						UpperCAmelCase_       				=			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	:						Dict							):


		'''simple docstring'''







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




	def       __A					(		self	:						Any							):


		'''simple docstring'''







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




	def       __A					(		self	:						int							):


		'''simple docstring'''







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




	def       __A					(		self	:						Dict							):


		'''simple docstring'''







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




	def       __A					(		self	:						str							):


		'''simple docstring'''







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


from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a:    List[str] 							=			logging.get_logger(__name__)

_a:    Any 							=			{
    """sayakpaul/vit-msn-base""": """https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json""",
    # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn
}

class 						__UpperCamelCase    (					lowercase						):
	SCREAMING_SNAKE_CASE__    				=     'vit_msn'




	def __init__(		self	:						Union[str, Any]		,	lowerCAmelCase	:						Optional[int]=768		,	lowerCAmelCase	:						Dict=12		,	lowerCAmelCase	:						str=12		,	lowerCAmelCase	:						List[str]=3_072		,	lowerCAmelCase	:						Tuple="gelu"		,	lowerCAmelCase	:						int=0.0		,	lowerCAmelCase	:						Optional[int]=0.0		,	lowerCAmelCase	:						Union[str, Any]=0.02		,	lowerCAmelCase	:						str=1e-06		,	lowerCAmelCase	:						Dict=224		,	lowerCAmelCase	:						str=16		,	lowerCAmelCase	:						List[Any]=3		,	lowerCAmelCase	:						Union[str, Any]=True		,	**lowerCAmelCase	:						List[str]		,	):


		'''simple docstring'''







		super().__init__(**lowerCAmelCase							)

		UpperCAmelCase_       				=			hidden_size
		UpperCAmelCase_       				=			num_hidden_layers
		UpperCAmelCase_       				=			num_attention_heads
		UpperCAmelCase_       				=			intermediate_size
		UpperCAmelCase_       				=			hidden_act
		UpperCAmelCase_       				=			hidden_dropout_prob
		UpperCAmelCase_       				=			attention_probs_dropout_prob
		UpperCAmelCase_       				=			initializer_range
		UpperCAmelCase_       				=			layer_norm_eps
		UpperCAmelCase_       				=			image_size
		UpperCAmelCase_       				=			patch_size
		UpperCAmelCase_       				=			num_channels
		UpperCAmelCase_       				=			qkv_bias