

"""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      (		a_):
			__a			:							Any					       =				"""new-model"""


if is_tf_available():






						class      _UpperCAmelCase      (		a_):
									__a			:							Tuple					       =				NewModelConfig






@require_tf
class      _UpperCAmelCase      (		unittest.TestCase):
			@slow
			def 				__snake_case			(		self  )     ->     List[Any]:






				'''simple docstring'''







				_UpperCAmelCase     :				str          =       '''bert-base-cased'''
				_UpperCAmelCase     :				Any          =       AutoConfig.from_pretrained(lowercase_  )
				self.assertIsNotNone(lowercase_  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )

				_UpperCAmelCase     :				Optional[Any]          =       TFAutoModel.from_pretrained(lowercase_  )
				self.assertIsNotNone(lowercase_  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			def 				__snake_case			(		self  )     ->     Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase     :				List[Any]          =       '''bert-base-cased'''
				_UpperCAmelCase     :				Any          =       AutoConfig.from_pretrained(lowercase_  )
				self.assertIsNotNone(lowercase_  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )

				_UpperCAmelCase     :				Union[str, Any]          =       TFAutoModelForPreTraining.from_pretrained(lowercase_  )
				self.assertIsNotNone(lowercase_  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			def 				__snake_case			(		self  )     ->     Any:






				'''simple docstring'''







				for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCAmelCase     :				Dict          =       AutoConfig.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )

					_UpperCAmelCase     :				Union[str, Any]          =       TFAutoModelForCausalLM.from_pretrained(lowercase_  )
					_UpperCAmelCase     :				List[str]          =       TFAutoModelForCausalLM.from_pretrained(lowercase_   ,		output_loading_info=lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			def 				__snake_case			(		self  )     ->     List[str]:






				'''simple docstring'''







				for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCAmelCase     :				Dict          =       AutoConfig.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )

					_UpperCAmelCase     :				int          =       TFAutoModelWithLMHead.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			def 				__snake_case			(		self  )     ->     str:






				'''simple docstring'''







				for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCAmelCase     :				List[str]          =       AutoConfig.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )

					_UpperCAmelCase     :				Union[str, Any]          =       TFAutoModelForMaskedLM.from_pretrained(lowercase_  )
					_UpperCAmelCase     :				Optional[int]          =       TFAutoModelForMaskedLM.from_pretrained(lowercase_   ,		output_loading_info=lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			def 				__snake_case			(		self  )     ->     int:






				'''simple docstring'''







				for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCAmelCase     :				List[str]          =       AutoConfig.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )

					_UpperCAmelCase     :				Any          =       TFAutoModelForSeqaSeqLM.from_pretrained(lowercase_  )
					_UpperCAmelCase     :				Optional[Any]          =       TFAutoModelForSeqaSeqLM.from_pretrained(lowercase_   ,		output_loading_info=lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			def 				__snake_case			(		self  )     ->     str:






				'''simple docstring'''







				for model_name in ["bert-base-uncased"]:
					_UpperCAmelCase     :				Optional[Any]          =       AutoConfig.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )

					_UpperCAmelCase     :				Dict          =       TFAutoModelForSequenceClassification.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			def 				__snake_case			(		self  )     ->     List[str]:






				'''simple docstring'''







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

					_UpperCAmelCase     :				Union[str, Any]          =       TFAutoModelForQuestionAnswering.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			@slow
			@require_tensorflow_probability
			def 				__snake_case			(		self  )     ->     Any:






				'''simple docstring'''







				for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
					_UpperCAmelCase     :				List[Any]          =       AutoConfig.from_pretrained(lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )

					_UpperCAmelCase     :				Any          =       TFAutoModelForTableQuestionAnswering.from_pretrained(lowercase_  )
					_UpperCAmelCase     :				Dict          =       TFAutoModelForTableQuestionAnswering.from_pretrained(
					    lowercase_   ,		output_loading_info=lowercase_  )
					self.assertIsNotNone(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			def 				__snake_case			(		self  )     ->     Tuple:






				'''simple docstring'''







				_UpperCAmelCase     :				str          =       TFAutoModelWithLMHead.from_pretrained(lowercase_  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )
				self.assertEqual(model.num_parameters()   ,		1_44_10  )
				self.assertEqual(model.num_parameters(only_trainable=lowercase_  )   ,		1_44_10  )
			def 				__snake_case			(		self  )     ->     Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase     :				List[Any]          =       TFAutoModelWithLMHead.from_pretrained(lowercase_  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )
				self.assertEqual(model.num_parameters()   ,		1_44_10  )
				self.assertEqual(model.num_parameters(only_trainable=lowercase_  )   ,		1_44_10  )
			def 				__snake_case			(		self  )     ->     Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase     :				List[Any]          =       TFAutoModel.from_pretrained("""sgugger/funnel-random-tiny"""  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )

				_UpperCAmelCase     :				Tuple          =       copy.deepcopy(model.config  )
				_UpperCAmelCase     :				Optional[Any]          =       ['''FunnelBaseModel''']
				_UpperCAmelCase     :				Dict          =       TFAutoModel.from_config(lowercase_  )
				self.assertIsInstance(lowercase_   ,		lowercase_  )

				with tempfile.TemporaryDirectory() as tmp_dir:
					model.save_pretrained(lowercase_  )
					_UpperCAmelCase     :				str          =       TFAutoModel.from_pretrained(lowercase_  )
					self.assertIsInstance(lowercase_   ,		lowercase_  )
			def 				__snake_case			(		self  )     ->     Dict:






				'''simple docstring'''







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

					_UpperCAmelCase     :				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
							_UpperCAmelCase     :				str          =       BertModelTester(self  ).get_config()
							_UpperCAmelCase     :				int          =       NewModelConfig(**tiny_config.to_dict()  )
							_UpperCAmelCase     :				Tuple          =       auto_class.from_config(lowercase_  )
							self.assertIsInstance(lowercase_   ,		lowercase_  )

							with tempfile.TemporaryDirectory() as tmp_dir:
								model.save_pretrained(lowercase_  )
								_UpperCAmelCase     :				Optional[Any]          =       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 				__snake_case			(		self  )     ->     Optional[int]:






				'''simple docstring'''







				with self.assertRaisesRegex(
				    lowercase_   ,		"""bert-base is not a local folder and is not a valid model identifier"""  ):
					_UpperCAmelCase     :				int          =       TFAutoModel.from_pretrained("""bert-base"""  )
			def 				__snake_case			(		self  )     ->     Dict:






				'''simple docstring'''







				with self.assertRaisesRegex(
				    lowercase_   ,		r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""  ):
					_UpperCAmelCase     :				Dict          =       TFAutoModel.from_pretrained(lowercase_   ,		revision="""aaaaaa"""  )
			def 				__snake_case			(		self  )     ->     Tuple:






				'''simple docstring'''







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






				'''simple docstring'''







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






				'''simple docstring'''







				_UpperCAmelCase     :				int          =       TFAutoModel.from_pretrained("""hf-internal-testing/tiny-random-bert"""  )
				with RequestCounter() as counter:
					_UpperCAmelCase     :				int          =       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     :				Union[str, Any]          =       TFAutoModel.from_pretrained("""ArthurZ/tiny-random-bert-sharded"""  )
				with RequestCounter() as counter:
					_UpperCAmelCase     :				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"""





import warnings

from ...utils import logging
from .image_processing_imagegpt import ImageGPTImageProcessor


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







class 							SCREAMING_SNAKE_CASE   (						a_			):






					"""simple docstring"""



					def __init__(			self     :							Any  ,*lowercase_     :							Dict  ,**lowercase_     :							List[str]						):
						warnings.warn(
						    '''The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
						    ''' Please use ImageGPTImageProcessor instead.'''  ,lowercase_  ,)
						super().__init__(*lowercase_  ,**lowercase_						)




