



'''simple docstring'''






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TFResNetForImageClassification, TFResNetModel
						from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor

class 				SCREAMING_SNAKE_CASE:







	"""simple docstring"""






	def __init__(						self			:     List[Any]		,			__snake_case			:     Any		,			__snake_case			:     List[str]=3		,			__snake_case			:     str=32		,			__snake_case			:     Tuple=3		,			__snake_case			:     Dict=10		,			__snake_case			:     List[Any]=[10, 20, 30, 40]		,			__snake_case			:     List[Any]=[1, 1, 2, 1]		,			__snake_case			:     Any=True		,			__snake_case			:     Dict=True		,			__snake_case			:     Any="relu"		,			__snake_case			:     Union[str, Any]=3		,			__snake_case			:     List[str]=None		,			)			->		List[str]:
								UpperCAmelCase :       int 							=		parent
								UpperCAmelCase :       List[Any] 							=		batch_size
								UpperCAmelCase :       List[str] 							=		image_size
								UpperCAmelCase :       Optional[int] 							=		num_channels
								UpperCAmelCase :       List[str] 							=		embeddings_size
								UpperCAmelCase :       List[str] 							=		hidden_sizes
								UpperCAmelCase :       int 							=		depths
								UpperCAmelCase :       Union[str, Any] 							=		is_training
								UpperCAmelCase :       Dict 							=		use_labels
								UpperCAmelCase :       List[str] 							=		hidden_act
								UpperCAmelCase :       Union[str, Any] 							=		num_labels
								UpperCAmelCase :       str 							=		scope
								UpperCAmelCase :       str 							=		len(__snake_case		)







	def      A	(						self			:     Union[str, Any]		)			->		List[Any]:
								UpperCAmelCase :       Optional[Any] 							=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

								UpperCAmelCase :       List[str] 							=		None
								if self.use_labels:
															UpperCAmelCase :       Union[str, Any] 							=		ids_tensor([self.batch_size]		,			self.num_labels		)

								UpperCAmelCase :       Optional[Any] 							=		self.get_config()

								return config, pixel_values, labels







	def      A	(						self			:     int		)			->		Tuple:
								return ResNetConfig(
								    num_channels=self.num_channels		,			embeddings_size=self.embeddings_size		,			hidden_sizes=self.hidden_sizes		,			depths=self.depths		,			hidden_act=self.hidden_act		,			num_labels=self.num_labels		,			image_size=self.image_size		,			)







	def      A	(						self			:     Optional[int]		,			__snake_case			:     Union[str, Any]		,			__snake_case			:     List[Any]		,			__snake_case			:     List[Any]		)			->		List[Any]:
								UpperCAmelCase :       int 							=		TFResNetModel(config=__snake_case		)
								UpperCAmelCase :       Tuple 							=		model(__snake_case		)
								# expected last hidden states: B, C, H // 32, W // 32
								self.parent.assertEqual(
								    result.last_hidden_state.shape		,			(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)		,			)







	def      A	(						self			:     List[str]		,			__snake_case			:     Optional[int]		,			__snake_case			:     Union[str, Any]		,			__snake_case			:     str		)			->		str:
								UpperCAmelCase :       List[Any] 							=		self.num_labels
								UpperCAmelCase :       Tuple 							=		TFResNetForImageClassification(__snake_case		)
								UpperCAmelCase :       Tuple 							=		model(__snake_case		,			labels=__snake_case		)
								self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_labels)		)







	def      A	(						self			:     str		)			->		Tuple:
								UpperCAmelCase :       Dict 							=		self.prepare_config_and_inputs()
								UpperCAmelCase							,		UpperCAmelCase							,		UpperCAmelCase :       Optional[Any] 							=		config_and_inputs
								UpperCAmelCase :       Any 							=		{'''pixel_values''': pixel_values}
								return config, inputs_dict

@require_tf
class 				SCREAMING_SNAKE_CASE(							A__							,		A__							,		unittest.TestCase			):







	"""simple docstring"""






	lowerCamelCase__			 =       (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
	lowerCamelCase__			 =       (
	    {"""feature-extraction""": TFResNetModel, """image-classification""": TFResNetForImageClassification}
	    if is_tf_available()
	    else {}
	)

	lowerCamelCase__			 =       False
	lowerCamelCase__			 =       False
	lowerCamelCase__			 =       False
	lowerCamelCase__			 =       False
	lowerCamelCase__			 =       False







	def      A	(						self			:     Any		)			->		List[Any]:
								UpperCAmelCase :       List[Any] 							=		TFResNetModelTester(self		)
								UpperCAmelCase :       str 							=		ConfigTester(self		,			config_class=__snake_case		,			has_text_modality=__snake_case		)







	def      A	(						self			:     List[str]		)			->		int:
								self.create_and_test_config_common_properties()
								self.config_tester.create_and_test_config_to_json_string()
								self.config_tester.create_and_test_config_to_json_file()
								self.config_tester.create_and_test_config_from_and_save_pretrained()
								self.config_tester.create_and_test_config_with_num_labels()
								self.config_tester.check_config_can_be_init_without_params()
								self.config_tester.check_config_arguments_init()







	def      A	(						self			:     Dict		)			->		Dict:
								return







	@unittest.skip(reason='''ResNet does not use inputs_embeds'''		)
	def      A	(						self			:     str		)			->		Dict:
								pass







	@unittest.skip(reason='''ResNet does not support input and output embeddings'''		)
	def      A	(						self			:     Dict		)			->		Any:
								pass







	def      A	(						self			:     Optional[int]		)			->		str:
								UpperCAmelCase							,		UpperCAmelCase :       List[Any] 							=		self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
															UpperCAmelCase :       Union[str, Any] 							=		model_class(__snake_case		)
															UpperCAmelCase :       List[Any] 							=		inspect.signature(model.call		)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															UpperCAmelCase :       List[Any] 							=		[*signature.parameters.keys()]

															UpperCAmelCase :       Any 							=		['''pixel_values''']
															self.assertListEqual(arg_names[:1]		,			__snake_case		)







	def      A	(						self			:     Any		)			->		List[Any]:
								UpperCAmelCase :       Optional[Any] 							=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__snake_case		)







	def      A	(						self			:     Dict		)			->		str:
								def check_hidden_states_output(__snake_case			:     int		,			__snake_case			:     List[Any]		,			__snake_case			:     Optional[int]		):
															UpperCAmelCase :       List[Any] 							=		model_class(__snake_case		)
															UpperCAmelCase :       List[str] 							=		model(**self._prepare_for_class(__snake_case		,			__snake_case		)		)

															UpperCAmelCase :       Optional[Any] 							=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															UpperCAmelCase :       List[str] 							=		self.model_tester.num_stages
															self.assertEqual(len(__snake_case		)		,			expected_num_stages + 1		)

															# ResNet's feature maps are of shape (batch_size, num_channels, height, width)
															self.assertListEqual(
															    list(hidden_states[0].shape[-2:]		)		,			[self.model_tester.image_size // 4, self.model_tester.image_size // 4]		,			)

								UpperCAmelCase							,		UpperCAmelCase :       Union[str, Any] 							=		self.model_tester.prepare_config_and_inputs_for_common()
								UpperCAmelCase :       List[str] 							=		['''basic''', '''bottleneck''']
								for model_class in self.all_model_classes:
															for layer_type in layers_type:
																						UpperCAmelCase :       int 							=		layer_type
																						UpperCAmelCase :       Tuple 							=		True
																						check_hidden_states_output(__snake_case		,			__snake_case		,			__snake_case		)

																						# check that output_hidden_states also work using config
																						del inputs_dict["output_hidden_states"]
																						UpperCAmelCase :       Optional[Any] 							=		True

																						check_hidden_states_output(__snake_case		,			__snake_case		,			__snake_case		)







	def      A	(						self			:     Tuple		)			->		int:
								UpperCAmelCase :       List[str] 							=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*__snake_case		)







	@slow
	def      A	(						self			:     str		)			->		Optional[Any]:
								for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCAmelCase :       Optional[int] 							=		TFResNetModel.from_pretrained(__snake_case		)
															self.assertIsNotNone(__snake_case		)


def  snake_case_     (					)      ->   List[str]:
							UpperCAmelCase :       Union[str, Any] 							=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
							return image




@require_tf
@require_vision
class 				SCREAMING_SNAKE_CASE(							unittest.TestCase			):







	"""simple docstring"""






	@cached_property
	def      A	(						self			:     str		)			->		int:
								return (
								    AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)
								    if is_vision_available()
								    else None
								)







	@slow
	def      A	(						self			:     Dict		)			->		Optional[int]:
								UpperCAmelCase :       Optional[int] 							=		TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

								UpperCAmelCase :       List[str] 							=		self.default_image_processor
								UpperCAmelCase :       Any 							=		prepare_img()
								UpperCAmelCase :       Tuple 							=		image_processor(images=__snake_case		,			return_tensors='''tf'''		)

								# forward pass
								UpperCAmelCase :       Dict 							=		model(**__snake_case		)

								# verify the logits
								UpperCAmelCase :       Dict 							=		tf.TensorShape((1, 1000)		)
								self.assertEqual(outputs.logits.shape		,			__snake_case		)

								UpperCAmelCase :       Any 							=		tf.constant([-11.10_69, -9.78_77, -8.37_77]		)

								self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()		,			__snake_case		,			atol=1E-4		)		)










'''simple docstring'''




import unittest

from transformers import AlbertTokenizer, AlbertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


a   :			Any					  =    get_tests_dir("""fixtures/spiece.model""")

@require_sentencepiece
@require_tokenizers
class 			UpperCamelCase_ (							__magic_name__      ,     unittest.TestCase			):
	lowercase          = AlbertTokenizer
	lowercase          = AlbertTokenizerFast
	lowercase          = True
	lowercase          = True
	lowercase          = True





	def  _lowercase(  self    )			->			str:
								super().setUp()

								# We have a SentencePiece fixture for testing
								UpperCAmelCase     :		Optional[int]							     =		AlbertTokenizer(A    )
								tokenizer.save_pretrained(self.tmpdirname    )





	def  _lowercase(  self		,		A    )			->			int:
								UpperCAmelCase     :		Optional[int]							     =		"""this is a test"""
								UpperCAmelCase     :		Dict							     =		"""this is a test"""
								return input_text, output_text





	def  _lowercase(  self    )			->			int:
								UpperCAmelCase     :		Tuple							     =		"""<pad>"""
								UpperCAmelCase     :		Optional[int]							     =		0

								self.assertEqual(self.get_tokenizer()._convert_token_to_id(A    )		,		A    )
								self.assertEqual(self.get_tokenizer()._convert_id_to_token(A    )		,		A    )





	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		str							     =		list(self.get_tokenizer().get_vocab().keys()    )

								self.assertEqual(vocab_keys[0]		,		"""<pad>"""    )
								self.assertEqual(vocab_keys[1]		,		"""<unk>"""    )
								self.assertEqual(vocab_keys[-1]		,		"""▁eloquent"""    )
								self.assertEqual(len(A    )		,		30000    )





	def  _lowercase(  self    )			->			Tuple:
								self.assertEqual(self.get_tokenizer().vocab_size		,		30000    )





	def  _lowercase(  self    )			->			Union[str, Any]:
								if not self.test_rust_tokenizer:
															return

								UpperCAmelCase     :		int							     =		self.get_tokenizer()
								UpperCAmelCase     :		List[str]							     =		self.get_rust_tokenizer()

								UpperCAmelCase     :		Optional[Any]							     =		"""I was born in 92000, and this is falsé."""

								UpperCAmelCase     :		str							     =		tokenizer.tokenize(A    )
								UpperCAmelCase     :		Optional[int]							     =		rust_tokenizer.tokenize(A    )
								self.assertListEqual(A		,		A    )

								UpperCAmelCase     :		Any							     =		tokenizer.encode(A		,		add_special_tokens=A    )
								UpperCAmelCase     :		Optional[int]							     =		rust_tokenizer.encode(A		,		add_special_tokens=A    )
								self.assertListEqual(A		,		A    )

								UpperCAmelCase     :		Optional[Any]							     =		self.get_rust_tokenizer()
								UpperCAmelCase     :		Union[str, Any]							     =		tokenizer.encode(A    )
								UpperCAmelCase     :		Optional[int]							     =		rust_tokenizer.encode(A    )
								self.assertListEqual(A		,		A    )





	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		List[Any]							     =		AlbertTokenizer(A		,		keep_accents=A    )

								UpperCAmelCase     :		Optional[int]							     =		tokenizer.tokenize("""This is a test"""    )
								self.assertListEqual(A		,		["""▁this""", """▁is""", """▁a""", """▁test"""]    )

								self.assertListEqual(tokenizer.convert_tokens_to_ids(A    )		,		[48, 25, 21, 1289]    )

								UpperCAmelCase     :		Optional[Any]							     =		tokenizer.tokenize("""I was born in 92000, and this is falsé."""    )
								self.assertListEqual(
								    A		,		["""▁i""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """é""", """."""]    )
								UpperCAmelCase     :		str							     =		tokenizer.convert_tokens_to_ids(A    )
								self.assertListEqual(A		,		[31, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9]    )

								UpperCAmelCase     :		List[str]							     =		tokenizer.convert_ids_to_tokens(A    )
								self.assertListEqual(
								    A		,		["""▁i""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """."""]		,		)





	def  _lowercase(  self    )			->			Optional[Any]:
								UpperCAmelCase     :		str							     =		AlbertTokenizer(A    )

								UpperCAmelCase     :		Optional[int]							     =		tokenizer.encode("""sequence builders"""    )
								UpperCAmelCase     :		Any							     =		tokenizer.encode("""multi-sequence build"""    )

								UpperCAmelCase     :		Optional[Any]							     =		tokenizer.build_inputs_with_special_tokens(A    )
								UpperCAmelCase     :		List[str]							     =		tokenizer.build_inputs_with_special_tokens(A		,		A    )

								assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
								assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
								    tokenizer.sep_token_id
								]






	@slow
	def  _lowercase(  self    )			->			Dict:
								# fmt: off
								UpperCAmelCase     :		Tuple							     =		{"""attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """input_ids""": [[2, 21970, 13, 5, 6092, 167, 28, 7103, 2153, 673, 8, 7028, 12051, 18, 17, 7103, 2153, 673, 8, 3515, 18684, 8, 4461, 6, 1927, 297, 8, 12060, 2607, 18, 13, 5, 4461, 15, 10538, 38, 8, 135, 15, 822, 58, 15, 993, 10363, 15, 1460, 8005, 4461, 15, 993, 255, 2328, 9, 9, 9, 6, 26, 1112, 816, 3260, 13, 5, 103, 2377, 6, 17, 1112, 816, 2782, 13, 5, 103, 10641, 6, 29, 84, 2512, 2430, 782, 18684, 2761, 19, 808, 2430, 2556, 17, 855, 1480, 9477, 4091, 128, 11712, 15, 7103, 2153, 673, 17, 24883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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
								# fmt: on

								self.tokenizer_integration_test_util(
								    expected_encoding=A		,		model_name="""albert-base-v2"""		,		revision="""6b6560eaf5ff2e250b00c50f380c5389a9c2d82e"""		,		)

