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     lowerCAmelCase_     :







							def __init__(   self      :     Union[str, Any] ,			_A      :     List[Any] ,			_A      :     Union[str, Any]=13 ,			_A      :     int=30 ,			_A      :     Union[str, Any]=2 ,			_A      :     Tuple=3 ,			_A      :     str=True ,			_A      :     Optional[int]=True ,			_A      :     Optional[int]=32 ,			_A      :     Dict=2 ,			_A      :     Optional[Any]=4 ,			_A      :     Optional[int]=37 ,			_A      :     Tuple="gelu" ,			_A      :     Tuple=0.1 ,			_A      :     Optional[int]=0.1 ,			_A      :     Any=10 ,			_A      :     Any=0.02 ,			_A      :     Union[str, Any]=3 ,			_A      :     str=None ,			):
												_UpperCamelCase			     =     parent
												_UpperCamelCase			     =     batch_size
												_UpperCamelCase			     =     image_size
												_UpperCamelCase			     =     patch_size
												_UpperCamelCase			     =     num_channels
												_UpperCamelCase			     =     is_training
												_UpperCamelCase			     =     use_labels
												_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			     =     type_sequence_label_size
												_UpperCamelCase			     =     initializer_range
												_UpperCamelCase			     =     scope

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







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

												_UpperCamelCase			     =     self.get_config()

												return config, pixel_values, labels







							def  UpperCamelCase_						(   self      :     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=_A ,			initializer_range=self.initializer_range ,			)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     Tuple ,			_A      :     int ,			_A      :     Dict		):
												_UpperCamelCase			     =     TFViTModel(config=_A		)
												_UpperCamelCase			     =     model(_A ,			training=_A		)
												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.
												_UpperCamelCase			     =     self.image_size // 2
												_UpperCamelCase			     =     pixel_values[:, :, :image_size, :image_size]
												_UpperCamelCase			     =     model(_A ,			interpolate_pos_encoding=_A ,			training=_A		)
												_UpperCamelCase			     =     (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      :     int ,			_A      :     Any ,			_A      :     Union[str, Any] ,			_A      :     List[str]		):
												_UpperCamelCase			     =     self.type_sequence_label_size
												_UpperCamelCase			     =     TFViTForImageClassification(_A		)
												_UpperCamelCase			     =     model(_A ,			labels=_A ,			training=_A		)
												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.
												_UpperCamelCase			     =     self.image_size // 2
												_UpperCamelCase			     =     pixel_values[:, :, :image_size, :image_size]
												_UpperCamelCase			     =     model(_A ,			interpolate_pos_encoding=_A ,			training=_A		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)		)

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







							def  UpperCamelCase_						(   self      :     int		):
												_UpperCamelCase			     =     self.prepare_config_and_inputs()
												_UpperCamelCase     ,    _UpperCamelCase     ,    _UpperCamelCase			     =     config_and_inputs
												_UpperCamelCase			     =     {'''pixel_values''': pixel_values}
												return config, inputs_dict




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

							UpperCAmelCase				   =     False
							UpperCAmelCase				   =     False
							UpperCAmelCase				   =     False







							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     TFViTModelTester(self		)
												_UpperCamelCase			     =     ConfigTester(self ,			config_class=_A ,			has_text_modality=_A ,			hidden_size=37		)







							def  UpperCamelCase_						(   self      :     Any		):
												self.config_tester.run_common_tests()







							@unittest.skip(reason='''ViT does not use inputs_embeds'''		)
							def  UpperCamelCase_						(   self      :     List[Any]		):
												pass







							@unittest.skip(reason='''ViT does not use inputs_embeds'''		)
							def  UpperCamelCase_						(   self      :     List[str]		):
												pass







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase     ,    _UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs_for_common()

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







							def  UpperCamelCase_						(   self      :     Union[str, Any]		):
												_UpperCamelCase     ,    _UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs_for_common()

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

																	_UpperCamelCase			     =     ['''pixel_values''']
																	self.assertListEqual(arg_names[:1] ,			_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*_A		)







							@slow
							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     TFViTModel.from_pretrained('''google/vit-base-patch16-224'''		)
												self.assertIsNotNone(_A		)




def 					_snake_case	(  ):
					_UpperCamelCase			     =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
					return image







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







							@cached_property
							def  UpperCamelCase_						(   self      :     List[Any]		):
												return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''		) if is_vision_available() else None







							@slow
							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''		)

												_UpperCamelCase			     =     self.default_image_processor
												_UpperCamelCase			     =     prepare_img()
												_UpperCamelCase			     =     image_processor(images=_A ,			return_tensors='''tf'''		)

												# forward pass
												_UpperCamelCase			     =     model(**_A		)

												# verify the logits
												_UpperCamelCase			     =     tf.TensorShape((1, 1000)		)
												self.assertEqual(outputs.logits.shape ,			_A		)

												_UpperCamelCase			     =     tf.constant([-0.2744, 0.8215, -0.0836]		)

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






from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

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


if TYPE_CHECKING:
     from ... import FeatureExtractionMixin, TensorType

__a							     =					logging.get_logger(__name__)

__a							     =					{
    'openai/imagegpt-small': '',
    'openai/imagegpt-medium': '',
    'openai/imagegpt-large': '',
}

class     __a(	_a  ):






    """simple docstring"""





    lowerCAmelCase         = '''imagegpt'''
    lowerCAmelCase         = ['''past_key_values''']
    lowerCAmelCase         = {
        '''hidden_size''': '''n_embd''',
        '''max_position_embeddings''': '''n_positions''',
        '''num_attention_heads''': '''n_head''',
        '''num_hidden_layers''': '''n_layer''',
    }





    def __init__( self  ,_SCREAMING_SNAKE_CASE=512 + 1  ,_SCREAMING_SNAKE_CASE=32 * 32  ,_SCREAMING_SNAKE_CASE=512  ,_SCREAMING_SNAKE_CASE=24  ,_SCREAMING_SNAKE_CASE=8  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE="quick_gelu"  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=1e-5  ,_SCREAMING_SNAKE_CASE=0.02  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=False  ,_SCREAMING_SNAKE_CASE=False  ,_SCREAMING_SNAKE_CASE=False  ,**_SCREAMING_SNAKE_CASE  ,)					->						Optional[int]:
         UpperCAmelCase_     :				Optional[int]  =     vocab_size
         UpperCAmelCase_     :				Union[str, Any]  =     n_positions
         UpperCAmelCase_     :				Union[str, Any]  =     n_embd
         UpperCAmelCase_     :				Any  =     n_layer
         UpperCAmelCase_     :				Optional[Any]  =     n_head
         UpperCAmelCase_     :				Union[str, Any]  =     n_inner
         UpperCAmelCase_     :				List[Any]  =     activation_function
         UpperCAmelCase_     :				List[str]  =     resid_pdrop
         UpperCAmelCase_     :				str  =     embd_pdrop
         UpperCAmelCase_     :				Optional[Any]  =     attn_pdrop
         UpperCAmelCase_     :				Dict  =     layer_norm_epsilon
         UpperCAmelCase_     :				Union[str, Any]  =     initializer_range
         UpperCAmelCase_     :				Dict  =     scale_attn_weights
         UpperCAmelCase_     :				Any  =     use_cache
         UpperCAmelCase_     :				List[str]  =     scale_attn_by_inverse_layer_idx
         UpperCAmelCase_     :				Tuple  =     reorder_and_upcast_attn
         UpperCAmelCase_     :				int  =     tie_word_embeddings

         super().__init__(tie_word_embeddings=_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )


class     __a(	_a  ):






    """simple docstring"""





    @property
    def 				a__ ( self    )					->						Mapping[str, Mapping[int, str]]:
         return OrderedDict(
             [
                 ('''input_ids''', {0: '''batch''', 1: '''sequence'''}),
             ]    )





    def 				a__ ( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE = 1  ,_SCREAMING_SNAKE_CASE = -1  ,_SCREAMING_SNAKE_CASE = False  ,_SCREAMING_SNAKE_CASE = None  ,_SCREAMING_SNAKE_CASE = 3  ,_SCREAMING_SNAKE_CASE = 32  ,_SCREAMING_SNAKE_CASE = 32  ,)					->						Mapping[str, Any]:
         UpperCAmelCase_     :				Any  =     self._generate_dummy_images(_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				Optional[int]  =     dict(preprocessor(images=_SCREAMING_SNAKE_CASE  ,return_tensors=_SCREAMING_SNAKE_CASE    )    )

         return inputs