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    ,       __UpperCamelCase    ,       __UpperCamelCase=13    ,       __UpperCamelCase=30    ,       __UpperCamelCase=2    ,       __UpperCamelCase=3    ,       __UpperCamelCase=True    ,       __UpperCamelCase=True    ,       __UpperCamelCase=32    ,       __UpperCamelCase=2    ,       __UpperCamelCase=4    ,       __UpperCamelCase=37    ,       __UpperCamelCase="gelu"    ,       __UpperCamelCase=0.1    ,       __UpperCamelCase=0.1    ,       __UpperCamelCase=10    ,       __UpperCamelCase=0.0_2    ,       __UpperCamelCase=3    ,       __UpperCamelCase=None    ,       __UpperCamelCase=2    ,       )     ->  List[str]:



									'''simple docstring'''







									snake_case__					:	List[Any]												=      parent
									snake_case__					:	int												=      batch_size
									snake_case__					:	Optional[int]												=      image_size
									snake_case__					:	Union[str, Any]												=      patch_size
									snake_case__					:	Optional[int]												=      num_channels
									snake_case__					:	Tuple												=      is_training
									snake_case__					:	Dict												=      use_labels
									snake_case__					:	List[str]												=      hidden_size
									snake_case__					:	Dict												=      num_hidden_layers
									snake_case__					:	List[Any]												=      num_attention_heads
									snake_case__					:	int												=      intermediate_size
									snake_case__					:	Optional[Any]												=      hidden_act
									snake_case__					:	Tuple												=      hidden_dropout_prob
									snake_case__					:	Optional[Any]												=      attention_probs_dropout_prob
									snake_case__					:	Tuple												=      type_sequence_label_size
									snake_case__					:	List[Any]												=      initializer_range
									snake_case__					:	Dict												=      scope
									snake_case__					:	Optional[Any]												=      encoder_stride

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




					def 			__a			(   self		)     ->  int:



									'''simple docstring'''







									snake_case__					:	Union[str, Any]												=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

									snake_case__					:	Any												=      self.get_config()

									return config, pixel_values, labels




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



									'''simple docstring'''







									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=SCREAMING_SNAKE_CASE__    ,       initializer_range=self.initializer_range    ,       encoder_stride=self.encoder_stride    ,       )




					def 			__a			(   self    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase		)     ->  List[Any]:



									'''simple docstring'''







									snake_case__					:	Optional[int]												=      TFDeiTModel(config=SCREAMING_SNAKE_CASE__		)
									snake_case__					:	int												=      model(SCREAMING_SNAKE_CASE__		)
									self.parent.assertEqual(result.last_hidden_state.shape    ,       (self.batch_size, self.seq_length, self.hidden_size)		)




					def 			__a			(   self    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase		)     ->  List[str]:



									'''simple docstring'''







									snake_case__					:	str												=      TFDeiTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__		)
									snake_case__					:	List[Any]												=      model(SCREAMING_SNAKE_CASE__		)
									self.parent.assertEqual(
									    result.reconstruction.shape    ,       (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

									# test greyscale images
									snake_case__					:	Optional[Any]												=      1
									snake_case__					:	str												=      TFDeiTForMaskedImageModeling(SCREAMING_SNAKE_CASE__		)

									snake_case__					:	Optional[int]												=      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									snake_case__					:	List[Any]												=      model(SCREAMING_SNAKE_CASE__		)
									self.parent.assertEqual(result.reconstruction.shape    ,       (self.batch_size, 1, self.image_size, self.image_size)		)




					def 			__a			(   self    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase		)     ->  Dict:



									'''simple docstring'''







									snake_case__					:	Dict												=      self.type_sequence_label_size
									snake_case__					:	Optional[int]												=      TFDeiTForImageClassification(SCREAMING_SNAKE_CASE__		)
									snake_case__					:	Optional[int]												=      model(SCREAMING_SNAKE_CASE__    ,       labels=SCREAMING_SNAKE_CASE__		)
									self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.type_sequence_label_size)		)

									# test greyscale images
									snake_case__					:	Any												=      1
									snake_case__					:	List[Any]												=      TFDeiTForImageClassification(SCREAMING_SNAKE_CASE__		)

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




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



									'''simple docstring'''







									snake_case__					:	List[str]												=      self.prepare_config_and_inputs()
									snake_case__					:	Tuple												=      config_and_inputs
									snake_case__					:	Union[str, Any]												=      {'pixel_values': pixel_values}
									return config, inputs_dict





@require_tf
class  __snake_case  (			lowercase__    ,lowercase__    ,unittest.TestCase	):
					__lowerCamelCase       					=   (
					    (
					        TFDeiTModel,
					        TFDeiTForImageClassification,
					        TFDeiTForImageClassificationWithTeacher,
					        TFDeiTForMaskedImageModeling,
					    )
					    if is_tf_available()
					    else ()
					)
					__lowerCamelCase       					=   (
					    {
					        '''feature-extraction''': TFDeiTModel,
					        '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
					    }
					    if is_tf_available()
					    else {}
					)

					__lowerCamelCase       					=   False
					__lowerCamelCase       					=   False
					__lowerCamelCase       					=   False
					__lowerCamelCase       					=   False




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



									'''simple docstring'''







									snake_case__					:	str												=      TFDeiTModelTester(self		)
									snake_case__					:	List[str]												=      ConfigTester(self    ,       config_class=SCREAMING_SNAKE_CASE__    ,       has_text_modality=SCREAMING_SNAKE_CASE__    ,       hidden_size=37		)




					def 			__a			(   self		)     ->  Any:



									'''simple docstring'''







									self.config_tester.run_common_tests()




					@unittest.skip(reason='DeiT does not use inputs_embeds'		)
					def 			__a			(   self		)     ->  Any:



									'''simple docstring'''







									pass




					def 			__a			(   self		)     ->  int:



									'''simple docstring'''







									snake_case__					:	Optional[int]												=      self.model_tester.prepare_config_and_inputs_for_common()

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




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



									'''simple docstring'''







									snake_case__					:	List[Any]												=      self.model_tester.prepare_config_and_inputs_for_common()

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

													snake_case__					:	str												=      ['pixel_values']
													self.assertListEqual(arg_names[:1]    ,       SCREAMING_SNAKE_CASE__		)




					def 			__a			(   self		)     ->  Any:



									'''simple docstring'''







									snake_case__					:	List[Any]												=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__		)




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



									'''simple docstring'''







									snake_case__					:	str												=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE__		)




					def 			__a			(   self		)     ->  Tuple:



									'''simple docstring'''







									snake_case__					:	Optional[int]												=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__		)




					def 			__a			(   self    ,       __UpperCamelCase    ,       __UpperCamelCase    ,       __UpperCamelCase=False		)     ->  List[str]:



									'''simple docstring'''







									snake_case__					:	Tuple												=      super()._prepare_for_class(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    ,       return_labels=SCREAMING_SNAKE_CASE__		)

									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		)     ->  int:



									'''simple docstring'''







									for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													snake_case__					:	int												=      TFDeiTModel.from_pretrained(SCREAMING_SNAKE_CASE__		)
													self.assertIsNotNone(SCREAMING_SNAKE_CASE__		)




def  UpperCamelCase__						(				)   ->					str:
				snake_case__					:	Tuple												=      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:



									'''simple docstring'''







									return (
									    DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'		)
									    if is_vision_available()
									    else None
									)




					@slow
					def 			__a			(   self		)     ->  Any:



									'''simple docstring'''







									snake_case__					:	Dict												=      TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'		)

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

									# forward pass
									snake_case__					:	List[Any]												=      model(**SCREAMING_SNAKE_CASE__		)

									# verify the logits
									snake_case__					:	Tuple												=      tf.TensorShape((1, 1000)		)
									self.assertEqual(outputs.logits.shape    ,       SCREAMING_SNAKE_CASE__		)

									snake_case__					:	Tuple												=      tf.constant([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]		)

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






import flax.linen as nn
import jax
import jax.numpy as jnp
class  SCREAMING_SNAKE_CASE__  (				nn.Module			):
      snake_case__				:     int
      snake_case__				:     jnp.dtype        =						jnp.floataa
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			int:
             a_					:		Dict							=  nn.Conv(
                 self.out_channels     ,							kernel_size=(3, 3)     ,							strides=(1, 1)     ,							padding=((1, 1), (1, 1))     ,							dtype=self.dtype     ,							)

      def __call__(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     )		->			Optional[int]:
             a_							,		a_							,		a_							,		a_					:		Union[str, Any]							=  hidden_states.shape
             a_					:		List[str]							=  jax.image.resize(
                 SCREAMING_SNAKE_CASE__     ,							shape=(batch, height * 2, width * 2, channels)     ,							method='nearest'     ,							)
             a_					:		Any							=  self.conv(SCREAMING_SNAKE_CASE__     )
             return hidden_states
class  SCREAMING_SNAKE_CASE__  (				nn.Module			):
      snake_case__				:     int
      snake_case__				:     jnp.dtype        =						jnp.floataa
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			Optional[int]:
             a_					:		Optional[int]							=  nn.Conv(
                 self.out_channels     ,							kernel_size=(3, 3)     ,							strides=(2, 2)     ,							padding=((1, 1), (1, 1))     ,							dtype=self.dtype     ,							)

      def __call__(       self				:       Any     ,							SCREAMING_SNAKE_CASE__				:       int     )		->			Optional[int]:
             # pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
             # hidden_states = jnp.pad(hidden_states, pad_width=pad)
             a_					:		str							=  self.conv(SCREAMING_SNAKE_CASE__     )
             return hidden_states
class  SCREAMING_SNAKE_CASE__  (				nn.Module			):
      snake_case__				:     int
      snake_case__				:     int        =						None
      snake_case__				:     float        =						0.0
      snake_case__				:     bool        =						None
      snake_case__				:     jnp.dtype        =						jnp.floataa
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Dict:
             a_					:		List[str]							=  self.in_channels if self.out_channels is None else self.out_channels

             a_					:		Optional[int]							=  nn.GroupNorm(num_groups=3_2     ,							epsilon=1E-5     )
             a_					:		Any							=  nn.Conv(
                 SCREAMING_SNAKE_CASE__     ,							kernel_size=(3, 3)     ,							strides=(1, 1)     ,							padding=((1, 1), (1, 1))     ,							dtype=self.dtype     ,							)

             a_					:		Optional[int]							=  nn.Dense(SCREAMING_SNAKE_CASE__     ,							dtype=self.dtype     )

             a_					:		Union[str, Any]							=  nn.GroupNorm(num_groups=3_2     ,							epsilon=1E-5     )
             a_					:		int							=  nn.Dropout(self.dropout_prob     )
             a_					:		Optional[Any]							=  nn.Conv(
                 SCREAMING_SNAKE_CASE__     ,							kernel_size=(3, 3)     ,							strides=(1, 1)     ,							padding=((1, 1), (1, 1))     ,							dtype=self.dtype     ,							)

             a_					:		List[str]							=  self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

             a_					:		List[Any]							=  None
             if use_nin_shortcut:
                    a_					:		Union[str, Any]							=  nn.Conv(
                        SCREAMING_SNAKE_CASE__     ,							kernel_size=(1, 1)     ,							strides=(1, 1)     ,							padding='VALID'     ,							dtype=self.dtype     ,							)

      def __call__(       self				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       int     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=True     )		->			int:
             a_					:		List[Any]							=  hidden_states
             a_					:		Any							=  self.norma(SCREAMING_SNAKE_CASE__     )
             a_					:		Any							=  nn.swish(SCREAMING_SNAKE_CASE__     )
             a_					:		Union[str, Any]							=  self.conva(SCREAMING_SNAKE_CASE__     )

             a_					:		int							=  self.time_emb_proj(nn.swish(SCREAMING_SNAKE_CASE__     )     )
             a_					:		List[str]							=  jnp.expand_dims(jnp.expand_dims(SCREAMING_SNAKE_CASE__     ,							1     )     ,							1     )
             a_					:		Optional[int]							=  hidden_states + temb

             a_					:		List[str]							=  self.norma(SCREAMING_SNAKE_CASE__     )
             a_					:		Tuple							=  nn.swish(SCREAMING_SNAKE_CASE__     )
             a_					:		Optional[int]							=  self.dropout(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  self.conva(SCREAMING_SNAKE_CASE__     )

             if self.conv_shortcut is not None:
                    a_					:		List[str]							=  self.conv_shortcut(SCREAMING_SNAKE_CASE__     )

             return hidden_states + residual


