

'''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     a			:






						"""simple docstring"""




						def __init__(							self					:      Any			,			snake_case_					:      Optional[int]			,			snake_case_					:      int=1_3			,			snake_case_					:      Any=3_0			,			snake_case_					:      Dict=2			,			snake_case_					:      Union[str, Any]=3			,			snake_case_					:      Optional[int]=True			,			snake_case_					:      Dict=True			,			snake_case_					:      List[str]=3_2			,			snake_case_					:      str=2			,			snake_case_					:      Tuple=4			,			snake_case_					:      int=3_7			,			snake_case_					:      Union[str, Any]="gelu"			,			snake_case_					:      Optional[int]=0.1			,			snake_case_					:      int=0.1			,			snake_case_					:      Tuple=1_0			,			snake_case_					:      Optional[Any]=0.0_2			,			snake_case_					:      Optional[Any]=3			,			snake_case_					:      Union[str, Any]=None			,			snake_case_					:      List[Any]=2			,			):



								'''simple docstring'''



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

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







						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



								snake_case__						:       Optional[int]       						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

								snake_case__						:       str       						=  self.get_config()

								return config, pixel_values, labels







						def       __magic_name__							(							self					:      Optional[int]  ):



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







						def       __magic_name__							(							self					:      Optional[Any]			,			snake_case_					:      Any			,			snake_case_					:      Optional[Any]			,			snake_case_					:      Optional[int]  ):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      str			,			snake_case_					:      List[str]			,			snake_case_					:      Any			,			snake_case_					:      Tuple  ):



								'''simple docstring'''



								snake_case__						:       str       						=  TFDeiTForMaskedImageModeling(config=snake_case_  )
								snake_case__						:       str       						=  model(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__						:       int       						=  1
								snake_case__						:       Tuple       						=  TFDeiTForMaskedImageModeling(snake_case_  )

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







						def       __magic_name__							(							self					:      List[Any]			,			snake_case_					:      Any			,			snake_case_					:      List[Any]			,			snake_case_					:      Tuple  ):



								'''simple docstring'''



								snake_case__						:       Tuple       						=  self.type_sequence_label_size
								snake_case__						:       Dict       						=  TFDeiTForImageClassification(snake_case_  )
								snake_case__						:       int       						=  model(snake_case_			,			labels=snake_case_  )
								self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.type_sequence_label_size)  )

								# test greyscale images
								snake_case__						:       int       						=  1
								snake_case__						:       Any       						=  TFDeiTForImageClassification(snake_case_  )

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







						def       __magic_name__							(							self					:      Optional[int]  ):



								'''simple docstring'''



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





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






						"""simple docstring"""




						__UpperCAmelCase			     =    (
						    (
						        TFDeiTModel,
						        TFDeiTForImageClassification,
						        TFDeiTForImageClassificationWithTeacher,
						        TFDeiTForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						__UpperCAmelCase			     =    (
						    {
						        """feature-extraction""": TFDeiTModel,
						        """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
						    }
						    if is_tf_available()
						    else {}
						)

						__UpperCAmelCase			     =    False
						__UpperCAmelCase			     =    False
						__UpperCAmelCase			     =    False
						__UpperCAmelCase			     =    False







						def       __magic_name__							(							self					:      Any  ):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  TFDeiTModelTester(self  )
								snake_case__						:       Optional[int]       						=  ConfigTester(self			,			config_class=snake_case_			,			has_text_modality=snake_case_			,			hidden_size=3_7  )







						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



								self.config_tester.run_common_tests()







						@unittest.skip(reason='''DeiT does not use inputs_embeds'''  )
						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



								pass







						def       __magic_name__							(							self					:      Tuple  ):



								'''simple docstring'''



								snake_case__						,       snake_case__						:       str       						=  self.model_tester.prepare_config_and_inputs_for_common()

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







						def       __magic_name__							(							self					:      Union[str, Any]  ):



								'''simple docstring'''



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

								for model_class in self.all_model_classes:
										snake_case__						:       Tuple       						=  model_class(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__						:       Dict       						=  ['''pixel_values''']
										self.assertListEqual(arg_names[:1]			,			snake_case_  )







						def       __magic_name__							(							self					:      Any  ):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case_  )







						def       __magic_name__							(							self					:      List[Any]  ):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_  )







						def       __magic_name__							(							self					:      str  ):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      List[Any]			,			snake_case_					:      Optional[int]			,			snake_case_					:      Optional[Any]			,			snake_case_					:      str=False  ):



								'''simple docstring'''



								snake_case__						:       List[str]       						=  super()._prepare_for_class(snake_case_			,			snake_case_			,			return_labels=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       __magic_name__							(							self					:      Optional[int]  ):



								'''simple docstring'''



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





def 	_a       (				):

		"""simple docstring"""

		snake_case__						:       Tuple       						=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
		return image





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






						"""simple docstring"""




						@cached_property
						def       __magic_name__							(							self					:      Optional[Any]  ):



								'''simple docstring'''



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







						@slow
						def       __magic_name__							(							self					:      int  ):



								'''simple docstring'''



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

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

								# forward pass
								snake_case__						:       Optional[int]       						=  model(**snake_case_  )

								# verify the logits
								snake_case__						:       List[Any]       						=  tf.TensorShape((1, 1_0_0_0)  )
								self.assertEqual(outputs.logits.shape			,			snake_case_  )

								snake_case__						:       Optional[int]       						=  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]			,			snake_case_			,			atol=1e-4  )  )






'''simple docstring'''


from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
					import timm


if is_torch_available():
					from torch import Tensor




class     a			(				SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    """pixel_values"""
						__UpperCAmelCase			     =    False
						__UpperCAmelCase			     =    TimmBackboneConfig







						def __init__(							self					:      str			,			snake_case_					:      str			,			**snake_case_					:      Dict  ):



								'''simple docstring'''



								requires_backends(self			,			'''timm'''  )
								super().__init__(snake_case_  )
								snake_case__						:       List[str]       						=  config

								if config.backbone is None:
										raise ValueError('''backbone is not set in the config. Please set it to a timm model name.'''  )

								if config.backbone not in timm.list_models():
										raise ValueError(F"""backbone {config.backbone} is not supported by timm."""  )

								if hasattr(snake_case_			,			'''out_features'''  ) and config.out_features is not None:
										raise ValueError('''out_features is not supported by TimmBackbone. Please use out_indices instead.'''  )

								snake_case__						:       Dict       						=  getattr(snake_case_			,			'''use_pretrained_backbone'''			,			snake_case_  )
								if pretrained is None:
										raise ValueError('''use_pretrained_backbone is not set in the config. Please set it to True or False.'''  )

								# We just take the final layer by default. This matches the default for the transformers models.
								snake_case__						:       int       						=  config.out_indices if getattr(snake_case_			,			'''out_indices'''			,			snake_case_  ) is not None else (-1,)

								snake_case__						:       Any       						=  timm.create_model(
								    config.backbone			,			pretrained=snake_case_			,			features_only=config.features_only			,			in_chans=config.num_channels			,			out_indices=snake_case_			,			**snake_case_			,			)
								# These are used to control the output of the model when called. If output_hidden_states is True, then
								# return_layers is modified to include all layers.
								snake_case__						:       str       						=  self._backbone.return_layers
								snake_case__						:       List[Any]       						=  {layer['''module''']: str(snake_case_  ) for i, layer in enumerate(self._backbone.feature_info.info  )}
								super()._init_backbone(snake_case_  )







						@classmethod
						def       __magic_name__							(							cls					:      Union[str, Any]			,			snake_case_					:      Union[str, Any]			,			*snake_case_					:      Optional[Any]			,			**snake_case_					:      Optional[Any]  ):



								'''simple docstring'''



								requires_backends(cls			,			['''vision''', '''timm''']  )
								from ...models.timm_backbone import TimmBackboneConfig

								snake_case__						:       Dict       						=  kwargs.pop('''config'''			,			TimmBackboneConfig()  )

								snake_case__						:       Optional[Any]       						=  kwargs.pop('''use_timm_backbone'''			,			snake_case_  )
								if not use_timm:
										raise ValueError('''use_timm_backbone must be True for timm backbones'''  )

								snake_case__						:       List[Any]       						=  kwargs.pop('''num_channels'''			,			config.num_channels  )
								snake_case__						:       Optional[Any]       						=  kwargs.pop('''features_only'''			,			config.features_only  )
								snake_case__						:       Tuple       						=  kwargs.pop('''use_pretrained_backbone'''			,			config.use_pretrained_backbone  )
								snake_case__						:       Union[str, Any]       						=  kwargs.pop('''out_indices'''			,			config.out_indices  )
								snake_case__						:       int       						=  TimmBackboneConfig(
								    backbone=snake_case_			,			num_channels=snake_case_			,			features_only=snake_case_			,			use_pretrained_backbone=snake_case_			,			out_indices=snake_case_			,			)
								return super()._from_config(snake_case_			,			**snake_case_  )







						def       __magic_name__							(							self					:      List[str]			,			snake_case_					:      Union[str, Any]  ):



								'''simple docstring'''



								pass







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      Union[str, Any]			,			snake_case_					:      Tuple=None			,			snake_case_					:      Tuple=None			,			snake_case_					:      Optional[Any]=None			,			**snake_case_					:      List[str]  ):



								'''simple docstring'''



								snake_case__						:       List[Any]       						=  return_dict if return_dict is not None else self.config.use_return_dict
								snake_case__						:       Optional[int]       						=  (
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								snake_case__						:       int       						=  output_attentions if output_attentions is not None else self.config.output_attentions

								if output_attentions:
										raise ValueError('''Cannot output attentions for timm backbones at the moment'''  )

								if output_hidden_states:
										# We modify the return layers to include all the stages of the backbone
										snake_case__						:       Any       						=  self._all_layers
										snake_case__						:       str       						=  self._backbone(snake_case_			,			**snake_case_  )
										snake_case__						:       Tuple       						=  self._return_layers
										snake_case__						:       str       						=  tuple(hidden_states[i] for i in self.out_indices  )
								else:
										snake_case__						:       Dict       						=  self._backbone(snake_case_			,			**snake_case_  )
										snake_case__						:       Any       						=  None

								snake_case__						:       Union[str, Any]       						=  tuple(snake_case_  )
								snake_case__						:       Optional[int]       						=  tuple(snake_case_  ) if hidden_states is not None else None

								if not return_dict:
										snake_case__						:       List[str]       						=  (feature_maps,)
										if output_hidden_states:
												snake_case__						:       Optional[int]       						=  output + (hidden_states,)
										return output

								return BackboneOutput(feature_maps=snake_case_			,			hidden_states=snake_case_			,			attentions=snake_case_  )



