


"""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      __A     (snake_case__):





							'''simple docstring'''

							__lowercase:		Optional[Any]							      =    """new-model"""


if is_tf_available():





				class      __A     (snake_case__):





											'''simple docstring'''

											__lowercase:		Union[str, Any]							      =    NewModelConfig





@require_tf
class      __A     (unittest.TestCase):





							'''simple docstring'''

							@slow
							def 		lowerCAmelCase       (				self							:      List[str]       )					->Union[str, Any]:



														"""simple docstring"""



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

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



							@slow
							def 		lowerCAmelCase       (				self							:      int       )					->int:



														"""simple docstring"""



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

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



							@slow
							def 		lowerCAmelCase       (				self							:      Tuple       )					->Union[str, Any]:



														"""simple docstring"""



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

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



							@slow
							def 		lowerCAmelCase       (				self							:      int       )					->str:



														"""simple docstring"""



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

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



							@slow
							def 		lowerCAmelCase       (				self							:      Optional[Any]       )					->str:



														"""simple docstring"""



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

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



							@slow
							def 		lowerCAmelCase       (				self							:      Optional[Any]       )					->List[str]:



														"""simple docstring"""



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

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



							@slow
							def 		lowerCAmelCase       (				self							:      List[Any]       )					->int:



														"""simple docstring"""



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

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



							@slow
							def 		lowerCAmelCase       (				self							:      Optional[Any]       )					->Dict:



														"""simple docstring"""



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

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



							@slow
							@require_tensorflow_probability
							def 		lowerCAmelCase       (				self							:      Dict       )					->Optional[int]:



														"""simple docstring"""



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

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



							def 		lowerCAmelCase       (				self							:      Union[str, Any]       )					->Tuple:



														"""simple docstring"""



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



							def 		lowerCAmelCase       (				self							:      Optional[Any]       )					->Tuple:



														"""simple docstring"""



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



							def 		lowerCAmelCase       (				self							:      Any       )					->Union[str, Any]:



														"""simple docstring"""



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

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

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



							def 		lowerCAmelCase       (				self							:      List[Any]       )					->List[Any]:



														"""simple docstring"""



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

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

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

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

														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 		lowerCAmelCase       (				self							:      Tuple       )					->List[str]:



														"""simple docstring"""



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



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->str:



														"""simple docstring"""



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



							def 		lowerCAmelCase       (				self							:      Union[str, Any]       )					->List[str]:



														"""simple docstring"""



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



							def 		lowerCAmelCase       (				self							:      int       )					->Union[str, Any]:



														"""simple docstring"""



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



							def 		lowerCAmelCase       (				self							:      Tuple       )					->Dict:



														"""simple docstring"""



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

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
				import torch

if is_vision_available():
				from PIL import Image

				from transformers import ImageGPTImageProcessor





class      __A     (unittest.TestCase):





							'''simple docstring'''

							def __init__(				self							:      int		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      Tuple=7		,       UpperCAmelCase_							:      Dict=3		,       UpperCAmelCase_							:      Tuple=18		,       UpperCAmelCase_							:      Optional[Any]=30		,       UpperCAmelCase_							:      str=400		,       UpperCAmelCase_							:      int=True		,       UpperCAmelCase_							:      Optional[int]=None		,       UpperCAmelCase_							:      Optional[Any]=True		,       )					->Optional[Any]:



														"""simple docstring"""



														snake_case_           =		size if size is not None else {"""height""": 18, """width""": 18}
														snake_case_           =		parent
														snake_case_           =		batch_size
														snake_case_           =		num_channels
														snake_case_           =		image_size
														snake_case_           =		min_resolution
														snake_case_           =		max_resolution
														snake_case_           =		do_resize
														snake_case_           =		size
														snake_case_           =		do_normalize



							def 		lowerCAmelCase       (				self							:      List[str]       )					->Optional[Any]:



														"""simple docstring"""



														return {
														    # here we create 2 clusters for the sake of simplicity
														    "clusters": np.asarray(
														        [
														            [0.8_866_443_634_033_203, 0.6_618_829_369_544_983, 0.3_891_746_401_786_804],
														            [-0.6_042_559_146_881_104, -0.02_295_008_860_528_469, 0.5_423_797_369_003_296],
														        ]       ),
														    "do_resize": self.do_resize,
														    "size": self.size,
														    "do_normalize": self.do_normalize,
														}





@require_torch
@require_vision
class      __A     (snake_case__						,		unittest.TestCase):





							'''simple docstring'''

							__lowercase:		List[Any]							      =    ImageGPTImageProcessor if is_vision_available() else None



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->Optional[int]:



														"""simple docstring"""



														snake_case_           =		ImageGPTImageProcessingTester(self       )



							@property
							def 		lowerCAmelCase       (				self							:      Tuple       )					->List[Any]:



														"""simple docstring"""



														return self.image_processor_tester.prepare_image_processor_dict()



							def 		lowerCAmelCase       (				self							:      Optional[Any]       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """clusters"""       )       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """do_resize"""       )       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """size"""       )       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """do_normalize"""       )       )



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->Optional[Any]:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class.from_dict(self.image_processor_dict       )
														self.assertEqual(image_processor.size		,       {"""height""": 18, """width""": 18}       )

														snake_case_           =		self.image_processing_class.from_dict(self.image_processor_dict		,       size=42       )
														self.assertEqual(image_processor.size		,       {"""height""": 42, """width""": 42}       )



							def 		lowerCAmelCase       (				self							:      Any       )					->List[Any]:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )
														snake_case_           =		json.loads(image_processor.to_json_string()       )
														for key, value in self.image_processor_dict.items():
																					if key == "clusters":
																												self.assertTrue(np.array_equal(UpperCAmelCase_		,       obj[key]       )       )
																					else:
																												self.assertEqual(obj[key]		,       UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->Dict:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )

														with tempfile.TemporaryDirectory() as tmpdirname:
																					snake_case_           =		os.path.join(UpperCAmelCase_		,       """image_processor.json"""       )
																					image_processor_first.to_json_file(UpperCAmelCase_       )
																					snake_case_           =		self.image_processing_class.from_json_file(UpperCAmelCase_       ).to_dict()

														snake_case_           =		image_processor_first.to_dict()
														for key, value in image_processor_first.items():
																					if key == "clusters":
																												self.assertTrue(np.array_equal(UpperCAmelCase_		,       image_processor_second[key]       )       )
																					else:
																												self.assertEqual(image_processor_first[key]		,       UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Optional[Any]       )					->List[Any]:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )

														with tempfile.TemporaryDirectory() as tmpdirname:
																					image_processor_first.save_pretrained(UpperCAmelCase_       )
																					snake_case_           =		self.image_processing_class.from_pretrained(UpperCAmelCase_       ).to_dict()

														snake_case_           =		image_processor_first.to_dict()
														for key, value in image_processor_first.items():
																					if key == "clusters":
																												self.assertTrue(np.array_equal(UpperCAmelCase_		,       image_processor_second[key]       )       )
																					else:
																												self.assertEqual(image_processor_first[key]		,       UpperCAmelCase_       )



							@unittest.skip("""ImageGPT requires clusters at initialization"""       )
							def 		lowerCAmelCase       (				self							:      List[Any]       )					->Tuple:



														"""simple docstring"""



														pass







def 						_a				(      )    ->				str:
							snake_case_           =		load_dataset("""hf-internal-testing/fixtures_image_utils"""				, split="""test"""				)

							snake_case_           =		Image.open(dataset[4]["""file"""]				)
							snake_case_           =		Image.open(dataset[5]["""file"""]				)

							snake_case_           =		[imagea, imagea]

							return images





@require_vision
@require_torch
class      __A     (unittest.TestCase):





							'''simple docstring'''

							@slow
							def 		lowerCAmelCase       (				self							:      Tuple       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		ImageGPTImageProcessor.from_pretrained("""openai/imagegpt-small"""       )

														snake_case_           =		prepare_images()

														# test non-batched
														snake_case_           =		image_processing(images[0]		,       return_tensors="""pt"""       )

														self.assertIsInstance(encoding.input_ids		,       torch.LongTensor       )
														self.assertEqual(encoding.input_ids.shape		,       (1, 1_024)       )

														snake_case_           =		[306, 191, 191]
														self.assertEqual(encoding.input_ids[0, :3].tolist()		,       UpperCAmelCase_       )

														# test batched
														snake_case_           =		image_processing(UpperCAmelCase_		,       return_tensors="""pt"""       )

														self.assertIsInstance(encoding.input_ids		,       torch.LongTensor       )
														self.assertEqual(encoding.input_ids.shape		,       (2, 1_024)       )

														snake_case_           =		[303, 13, 13]
														self.assertEqual(encoding.input_ids[1, -3:].tolist()		,       UpperCAmelCase_       )



