






'''simple docstring'''





from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
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 (
						    TFDeiTForImageClassification,
						    TFDeiTForImageClassificationWithTeacher,
						    TFDeiTForMaskedImageModeling,
						    TFDeiTModel,
						)
						from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import DeiTImageProcessor


class  snake_case__      :




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

								# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
								UpperCAmelCase_      :       Optional[int]          =       (image_size // patch_size) ** 2
								UpperCAmelCase_      :       List[str]          =       num_patches + 2




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

								UpperCAmelCase_      :       Union[str, Any]          =       None
								if self.use_labels:
											UpperCAmelCase_      :       Optional[Any]          =       ids_tensor([self.batch_size]				, self.type_sequence_label_size      )

								UpperCAmelCase_      :       int          =       self.get_config()

								return config, pixel_values, labels




					def 		A      (  self  :		Union[str, Any]      )    ->							Any:
								return DeiTConfig(
								    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				, encoder_stride=self.encoder_stride				, )




					def 		A      (  self  :		Union[str, Any]				, _A  :		Any				, _A  :		List[str]				, _A  :		Union[str, Any]      )    ->							List[Any]:
								UpperCAmelCase_      :       Tuple          =       TFDeiTModel(config=_A      )
								UpperCAmelCase_      :       Any          =       model(_A      )
								self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.hidden_size)      )




					def 		A      (  self  :		Optional[int]				, _A  :		Dict				, _A  :		Dict				, _A  :		Optional[Any]      )    ->							int:
								UpperCAmelCase_      :       Union[str, Any]          =       TFDeiTForMaskedImageModeling(config=_A      )
								UpperCAmelCase_      :       Tuple          =       model(_A      )
								self.parent.assertEqual(
								    result.reconstruction.shape				, (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

								# test greyscale images
								UpperCAmelCase_      :       Optional[int]          =       1
								UpperCAmelCase_      :       int          =       TFDeiTForMaskedImageModeling(_A      )

								UpperCAmelCase_      :       List[str]          =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
								UpperCAmelCase_      :       Tuple          =       model(_A      )
								self.parent.assertEqual(result.reconstruction.shape				, (self.batch_size, 1, self.image_size, self.image_size)      )




					def 		A      (  self  :		str				, _A  :		Optional[int]				, _A  :		List[Any]				, _A  :		Any      )    ->							int:
								UpperCAmelCase_      :       Optional[int]          =       self.type_sequence_label_size
								UpperCAmelCase_      :       str          =       TFDeiTForImageClassification(_A      )
								UpperCAmelCase_      :       Optional[Any]          =       model(_A				, labels=_A      )
								self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.type_sequence_label_size)      )

								# test greyscale images
								UpperCAmelCase_      :       List[Any]          =       1
								UpperCAmelCase_      :       List[Any]          =       TFDeiTForImageClassification(_A      )

								UpperCAmelCase_      :       int          =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
								UpperCAmelCase_      :       List[str]          =       model(_A				, labels=_A      )
								self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.type_sequence_label_size)      )





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






@require_tf
class  snake_case__      (					UpperCamelCase     ,				UpperCamelCase     ,				unittest.TestCase):
					a_        =					(
					    (
					        TFDeiTModel,
					        TFDeiTForImageClassification,
					        TFDeiTForImageClassificationWithTeacher,
					        TFDeiTForMaskedImageModeling,
					    )
					    if is_tf_available()
					    else ()
					)
					a_        =					(
					    {
					        "feature-extraction": TFDeiTModel,
					        "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
					    }
					    if is_tf_available()
					    else {}
					)

					a_        =					False
					a_        =					False
					a_        =					False
					a_        =					False




					def 		A      (  self  :		Optional[Any]      )    ->							Any:
								UpperCAmelCase_      :       List[str]          =       TFDeiTModelTester(self      )
								UpperCAmelCase_      :       Dict          =       ConfigTester(self				, config_class=_A				, has_text_modality=_A				, hidden_size=37      )




					def 		A      (  self  :		str      )    ->							Dict:
								self.config_tester.run_common_tests()




					@unittest.skip(reason='''DeiT does not use inputs_embeds'''      )
					def 		A      (  self  :		List[str]      )    ->							List[Any]:
								pass




					def 		A      (  self  :		Dict      )    ->							int:
								UpperCAmelCase_    ,      UpperCAmelCase_      :       Union[str, Any]          =       self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											UpperCAmelCase_      :       Union[str, Any]          =       model_class(_A      )
											self.assertIsInstance(model.get_input_embeddings()				, (tf.keras.layers.Layer)      )
											UpperCAmelCase_      :       List[Any]          =       model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(_A				, tf.keras.layers.Dense      )      )




					def 		A      (  self  :		Union[str, Any]      )    ->							Dict:
								UpperCAmelCase_    ,      UpperCAmelCase_      :       Tuple          =       self.model_tester.prepare_config_and_inputs_for_common()

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

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




					def 		A      (  self  :		Union[str, Any]      )    ->							Optional[Any]:
								UpperCAmelCase_      :       Union[str, Any]          =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_A      )




					def 		A      (  self  :		Any      )    ->							int:
								UpperCAmelCase_      :       Optional[int]          =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*_A      )




					def 		A      (  self  :		Union[str, Any]      )    ->							Union[str, Any]:
								UpperCAmelCase_      :       Any          =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_A      )




					def 		A      (  self  :		Optional[Any]				, _A  :		Tuple				, _A  :		str				, _A  :		Any=False      )    ->							int:
								UpperCAmelCase_      :       Optional[int]          =       super()._prepare_for_class(_A				, _A				, return_labels=_A      )

								if return_labels:
											if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call      ).parameters:
														del inputs_dict["labels"]

								return inputs_dict





					@slow
					def 		A      (  self  :		List[Any]      )    ->							Any:
								for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCAmelCase_      :       Optional[int]          =       TFDeiTModel.from_pretrained(_A      )
											self.assertIsNotNone(_A      )






def __UpperCAmelCase   (					)    ->   Dict:
			UpperCAmelCase_      :       Any          =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
			return image







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




					@cached_property
					def 		A      (  self  :		int      )    ->							Any:
								return (
								    DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224'''      )
								    if is_vision_available()
								    else None
								)





					@slow
					def 		A      (  self  :		Any      )    ->							Optional[int]:
								UpperCAmelCase_      :       Union[str, Any]          =       TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''      )

								UpperCAmelCase_      :       Dict          =       self.default_image_processor
								UpperCAmelCase_      :       List[str]          =       prepare_img()
								UpperCAmelCase_      :       List[Any]          =       image_processor(images=_A				, return_tensors='''tf'''      )

								# forward pass
								UpperCAmelCase_      :       List[Any]          =       model(**_A      )

								# verify the logits
								UpperCAmelCase_      :       Tuple          =       tf.TensorShape((1, 10_00)      )
								self.assertEqual(outputs.logits.shape				, _A      )

								UpperCAmelCase_      :       int          =       tf.constant([-1.0_266, 0.1_912, -1.2_861]      )

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










'''simple docstring'''





import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCamelCase						:						Optional[Any]  =					logging.get_logger(__name__)

_UpperCamelCase						:						Dict  =					{
    'RUCAIBox/mvp': 'https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json',
}


class  snake_case__      (					UpperCamelCase):
					a_        =					"mvp"
					a_        =					["past_key_values"]
					a_        =					{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}




					def __init__(  self  :		Any				, _A  :		List[str]=5_02_67				, _A  :		Dict=10_24				, _A  :		List[str]=12				, _A  :		Dict=40_96				, _A  :		Optional[int]=16				, _A  :		Optional[Any]=12				, _A  :		int=40_96				, _A  :		Dict=16				, _A  :		Union[str, Any]=0.0				, _A  :		Optional[Any]=0.0				, _A  :		int="gelu"				, _A  :		Union[str, Any]=10_24				, _A  :		int=0.1				, _A  :		Optional[Any]=0.0				, _A  :		Optional[Any]=0.0				, _A  :		Optional[Any]=0.02				, _A  :		Optional[Any]=0.0				, _A  :		str=False				, _A  :		List[Any]=True				, _A  :		Optional[int]=1				, _A  :		Tuple=0				, _A  :		Dict=2				, _A  :		Dict=True				, _A  :		int=2				, _A  :		Tuple=2				, _A  :		List[str]=False				, _A  :		Any=1_00				, _A  :		Union[str, Any]=8_00				, **_A  :		List[str]				, )    ->							int:
								UpperCAmelCase_      :       Union[str, Any]          =       vocab_size
								UpperCAmelCase_      :       int          =       max_position_embeddings
								UpperCAmelCase_      :       Dict          =       d_model
								UpperCAmelCase_      :       Dict          =       encoder_ffn_dim
								UpperCAmelCase_      :       Optional[Any]          =       encoder_layers
								UpperCAmelCase_      :       Tuple          =       encoder_attention_heads
								UpperCAmelCase_      :       List[Any]          =       decoder_ffn_dim
								UpperCAmelCase_      :       List[Any]          =       decoder_layers
								UpperCAmelCase_      :       Dict          =       decoder_attention_heads
								UpperCAmelCase_      :       str          =       dropout
								UpperCAmelCase_      :       Optional[Any]          =       attention_dropout
								UpperCAmelCase_      :       Optional[Any]          =       activation_dropout
								UpperCAmelCase_      :       Union[str, Any]          =       activation_function
								UpperCAmelCase_      :       Union[str, Any]          =       init_std
								UpperCAmelCase_      :       Optional[int]          =       encoder_layerdrop
								UpperCAmelCase_      :       Optional[int]          =       decoder_layerdrop
								UpperCAmelCase_      :       Dict          =       classifier_dropout
								UpperCAmelCase_      :       List[Any]          =       use_cache
								UpperCAmelCase_      :       Dict          =       encoder_layers
								UpperCAmelCase_      :       Tuple          =       scale_embedding  # scale factor will be sqrt(d_model) if True
								UpperCAmelCase_      :       int          =       use_prompt
								UpperCAmelCase_      :       Tuple          =       prompt_length
								UpperCAmelCase_      :       str          =       prompt_mid_dim

								super().__init__(
								    pad_token_id=_A				, bos_token_id=_A				, eos_token_id=_A				, is_encoder_decoder=_A				, decoder_start_token_id=_A				, forced_eos_token_id=_A				, **_A				, )

								if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated'''				, _A      ):
											UpperCAmelCase_      :       Optional[Any]          =       self.bos_token_id
											warnings.warn(
											    F"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. "
											    '''The config can simply be saved and uploaded again to be fixed.'''      )


