



from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
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 TFViTForImageClassification, TFViTModel


if is_vision_available():
				from PIL import Image

				from transformers import ViTImageProcessor






class     __a			:



	def __init__(    self				:     Optional[int]					,						snake_case_				:     int					,						snake_case_				:     Optional[Any]=13					,						snake_case_				:     str=30					,						snake_case_				:     Optional[Any]=2					,						snake_case_				:     Union[str, Any]=3					,						snake_case_				:     str=True					,						snake_case_				:     Optional[Any]=True					,						snake_case_				:     Union[str, Any]=32					,						snake_case_				:     Optional[Any]=2					,						snake_case_				:     List[str]=4					,						snake_case_				:     Any=37					,						snake_case_				:     List[str]="gelu"					,						snake_case_				:     List[Any]=0.1					,						snake_case_				:     Optional[Any]=0.1					,						snake_case_				:     Any=10					,						snake_case_				:     Any=0.0_2					,						snake_case_				:     Optional[int]=3					,						snake_case_				:     int=None					,						)->				List[Any]:
						__lowerCAmelCase						 =parent
						__lowerCAmelCase						 =batch_size
						__lowerCAmelCase						 =image_size
						__lowerCAmelCase						 =patch_size
						__lowerCAmelCase						 =num_channels
						__lowerCAmelCase						 =is_training
						__lowerCAmelCase						 =use_labels
						__lowerCAmelCase						 =hidden_size
						__lowerCAmelCase						 =num_hidden_layers
						__lowerCAmelCase						 =num_attention_heads
						__lowerCAmelCase						 =intermediate_size
						__lowerCAmelCase						 =hidden_act
						__lowerCAmelCase						 =hidden_dropout_prob
						__lowerCAmelCase						 =attention_probs_dropout_prob
						__lowerCAmelCase						 =type_sequence_label_size
						__lowerCAmelCase						 =initializer_range
						__lowerCAmelCase						 =scope

						# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
						__lowerCAmelCase						 =(image_size // patch_size) ** 2
						__lowerCAmelCase						 =num_patches + 1



	def 		UpperCamelCase				(    self				:     Optional[Any])->				str:
						__lowerCAmelCase						 =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

						__lowerCAmelCase						 =None
						if self.use_labels:
											__lowerCAmelCase						 =ids_tensor([self.batch_size]					,						self.type_sequence_label_size)

						__lowerCAmelCase						 =self.get_config()

						return config, pixel_values, labels



	def 		UpperCamelCase				(    self				:     int)->				Optional[int]:
						return ViTConfig(
						    image_size=self.image_size					,						patch_size=self.patch_size					,						num_channels=self.num_channels					,						hidden_size=self.hidden_size					,						num_hidden_layers=self.num_hidden_layers					,						num_attention_heads=self.num_attention_heads					,						intermediate_size=self.intermediate_size					,						hidden_act=self.hidden_act					,						hidden_dropout_prob=self.hidden_dropout_prob					,						attention_probs_dropout_prob=self.attention_probs_dropout_prob					,						is_decoder=snake_case_					,						initializer_range=self.initializer_range					,						)



	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Tuple					,						snake_case_				:     Tuple)->				Optional[int]:
						__lowerCAmelCase						 =TFViTModel(config=snake_case_)
						__lowerCAmelCase						 =model(snake_case_					,						training=snake_case_)
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size))

						# Test with an image with different size than the one specified in config.
						__lowerCAmelCase						 =self.image_size // 2
						__lowerCAmelCase						 =pixel_values[:, :, :image_size, :image_size]
						__lowerCAmelCase						 =model(snake_case_					,						interpolate_pos_encoding=snake_case_					,						training=snake_case_)
						__lowerCAmelCase						 =(image_size // self.patch_size) ** 2 + 1
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, seq_length, self.hidden_size))



	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     Optional[int]					,						snake_case_				:     List[str]					,						snake_case_				:     List[str])->				int:
						__lowerCAmelCase						 =self.type_sequence_label_size
						__lowerCAmelCase						 =TFViTForImageClassification(snake_case_)
						__lowerCAmelCase						 =model(snake_case_					,						labels=snake_case_					,						training=snake_case_)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.type_sequence_label_size))

						# Test with an image with different size than the one specified in config.
						__lowerCAmelCase						 =self.image_size // 2
						__lowerCAmelCase						 =pixel_values[:, :, :image_size, :image_size]
						__lowerCAmelCase						 =model(snake_case_					,						interpolate_pos_encoding=snake_case_					,						training=snake_case_)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.type_sequence_label_size))

						# test greyscale images
						__lowerCAmelCase						 =1
						__lowerCAmelCase						 =TFViTForImageClassification(snake_case_)
						__lowerCAmelCase						 =floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
						__lowerCAmelCase						 =model(snake_case_)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.type_sequence_label_size))



	def 		UpperCamelCase				(    self				:     List[Any])->				List[Any]:
						__lowerCAmelCase						 =self.prepare_config_and_inputs()
						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase						 =config_and_inputs
						__lowerCAmelCase						 ={"""pixel_values""": pixel_values}
						return config, inputs_dict






@require_tf
class     __a			(  SCREAMING_SNAKE_CASE						,     SCREAMING_SNAKE_CASE						,     unittest.TestCase					):
	SCREAMING_SNAKE_CASE  	=							(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
	SCREAMING_SNAKE_CASE  	=							(
	    {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
	    if is_tf_available()
	    else {}
	)

	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False



	def 		UpperCamelCase				(    self				:     Tuple)->				Any:
						__lowerCAmelCase						 =TFViTModelTester(self)
						__lowerCAmelCase						 =ConfigTester(self					,						config_class=snake_case_					,						has_text_modality=snake_case_					,						hidden_size=37)



	def 		UpperCamelCase				(    self				:     Dict)->				List[Any]:
						self.config_tester.run_common_tests()



	@unittest.skip(reason="""ViT does not use inputs_embeds""")
	def 		UpperCamelCase				(    self				:     str)->				Union[str, Any]:
						pass



	@unittest.skip(reason="""ViT does not use inputs_embeds""")
	def 		UpperCamelCase				(    self				:     Any)->				Tuple:
						pass



	def 		UpperCamelCase				(    self				:     Any)->				Union[str, Any]:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__lowerCAmelCase						 =model_class(snake_case_)
											self.assertIsInstance(model.get_input_embeddings()					,						(tf.keras.layers.Layer))
											__lowerCAmelCase						 =model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(snake_case_					,						tf.keras.layers.Layer))



	def 		UpperCamelCase				(    self				:     str)->				List[str]:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__lowerCAmelCase						 =model_class(snake_case_)
											__lowerCAmelCase						 =inspect.signature(model.call)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											__lowerCAmelCase						 =[*signature.parameters.keys()]

											__lowerCAmelCase						 =["""pixel_values"""]
											self.assertListEqual(arg_names[:1]					,						snake_case_)



	def 		UpperCamelCase				(    self				:     Dict)->				Optional[int]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*snake_case_)



	def 		UpperCamelCase				(    self				:     Dict)->				List[str]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*snake_case_)



	@slow
	def 		UpperCamelCase				(    self				:     int)->				Union[str, Any]:
						__lowerCAmelCase						 =TFViTModel.from_pretrained("""google/vit-base-patch16-224""")
						self.assertIsNotNone(snake_case_)






def        __lowerCAmelCase		(	)	->    Tuple:
					__lowerCAmelCase						 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
					return image






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



	@cached_property
	def 		UpperCamelCase				(    self				:     str)->				int:
						return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224""") if is_vision_available() else None



	@slow
	def 		UpperCamelCase				(    self				:     List[Any])->				Union[str, Any]:
						__lowerCAmelCase						 =TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""")

						__lowerCAmelCase						 =self.default_image_processor
						__lowerCAmelCase						 =prepare_img()
						__lowerCAmelCase						 =image_processor(images=snake_case_					,						return_tensors="""tf""")

						# forward pass
						__lowerCAmelCase						 =model(**snake_case_)

						# verify the logits
						__lowerCAmelCase						 =tf.TensorShape((1, 10_00))
						self.assertEqual(outputs.logits.shape					,						snake_case_)

						__lowerCAmelCase						 =tf.constant([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6])

						tf.debugging.assert_near(outputs.logits[0, :3]					,						snake_case_					,						atol=1e-4)









from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase_													=      logging.get_logger(__name__)

lowercase_													=      {
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}






class     __a			(  SCREAMING_SNAKE_CASE					):
	SCREAMING_SNAKE_CASE  	=							"distilbert"
	SCREAMING_SNAKE_CASE  	=							{
	    "hidden_size": "dim",
	    "num_attention_heads": "n_heads",
	    "num_hidden_layers": "n_layers",
	}



	def __init__(    self				:     Optional[Any]					,						snake_case_				:     Tuple=3_05_22					,						snake_case_				:     Any=5_12					,						snake_case_				:     Dict=False					,						snake_case_				:     Tuple=6					,						snake_case_				:     Union[str, Any]=12					,						snake_case_				:     List[str]=7_68					,						snake_case_				:     Optional[Any]=4 * 7_68					,						snake_case_				:     Optional[Any]=0.1					,						snake_case_				:     Dict=0.1					,						snake_case_				:     Union[str, Any]="gelu"					,						snake_case_				:     Any=0.0_2					,						snake_case_				:     Optional[Any]=0.1					,						snake_case_				:     List[Any]=0.2					,						snake_case_				:     Optional[Any]=0					,						**snake_case_				:     List[Any]					,						)->				Tuple:
						__lowerCAmelCase						 =vocab_size
						__lowerCAmelCase						 =max_position_embeddings
						__lowerCAmelCase						 =sinusoidal_pos_embds
						__lowerCAmelCase						 =n_layers
						__lowerCAmelCase						 =n_heads
						__lowerCAmelCase						 =dim
						__lowerCAmelCase						 =hidden_dim
						__lowerCAmelCase						 =dropout
						__lowerCAmelCase						 =attention_dropout
						__lowerCAmelCase						 =activation
						__lowerCAmelCase						 =initializer_range
						__lowerCAmelCase						 =qa_dropout
						__lowerCAmelCase						 =seq_classif_dropout
						super().__init__(**snake_case_					,						pad_token_id=snake_case_)






class     __a			(  SCREAMING_SNAKE_CASE					):



	@property
	def 		UpperCamelCase				(    self				:     Union[str, Any])->				Mapping[str, Mapping[int, str]]:
						if self.task == "multiple-choice":
											__lowerCAmelCase						 ={0: """batch""", 1: """choice""", 2: """sequence"""}
						else:
											__lowerCAmelCase						 ={0: """batch""", 1: """sequence"""}
						return OrderedDict(
						    [
						        ("""input_ids""", dynamic_axis),
						        ("""attention_mask""", dynamic_axis),
						    ])




