





'''simple docstring'''






from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


lowerCAmelCase									=   logging.get_logger(__name__)

# General docstring
lowerCAmelCase									=   """ResNetConfig"""

# Base docstring
lowerCAmelCase									=   """microsoft/resnet-50"""
lowerCAmelCase									=   [1, 20_48, 7, 7]

# Image classification docstring
lowerCAmelCase									=   """microsoft/resnet-50"""
lowerCAmelCase									=   """tiger cat"""

lowerCAmelCase									=   [
    """microsoft/resnet-50""",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]
class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_		,   a_		,   a_ = 3		,   a_ = 1		,   a_ = "relu"						):
			super().__init__()
			lowerCAmelCase  :     Optional[int]       						= nn.Convad(
			    a_		,   a_		,   kernel_size=a_		,   stride=a_		,   padding=kernel_size // 2		,   bias=a_						)
			lowerCAmelCase  :     Optional[int]       						= nn.BatchNormad(a_						)
			lowerCAmelCase  :     Optional[Any]       						= ACTaFN[activation] if activation is not None else nn.Identity()





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Union[str, Any]       						= self.convolution(a_						)
			lowerCAmelCase  :     Tuple       						= self.normalization(a_						)
			lowerCAmelCase  :     Union[str, Any]       						= self.activation(a_						)
			return hidden_state






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_						):
			super().__init__()
			lowerCAmelCase  :     int       						= ResNetConvLayer(
			    config.num_channels		,   config.embedding_size		,   kernel_size=7		,   stride=2		,   activation=config.hidden_act						)
			lowerCAmelCase  :     Optional[int]       						= nn.MaxPoolad(kernel_size=3		,   stride=2		,   padding=1						)
			lowerCAmelCase  :     Optional[int]       						= config.num_channels





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Union[str, Any]       						= pixel_values.shape[1]
			if num_channels != self.num_channels:
					raise ValueError(
					    "Make sure that the channel dimension of the pixel values match with the one set in the configuration."						)
			lowerCAmelCase  :     int       						= self.embedder(a_						)
			lowerCAmelCase  :     Tuple       						= self.pooler(a_						)
			return embedding






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_		,   a_		,   a_ = 2						):
			super().__init__()
			lowerCAmelCase  :     Optional[int]       						= nn.Convad(a_		,   a_		,   kernel_size=1		,   stride=a_		,   bias=a_						)
			lowerCAmelCase  :     Optional[Any]       						= nn.BatchNormad(a_						)





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Optional[int]       						= self.convolution(a_						)
			lowerCAmelCase  :     Tuple       						= self.normalization(a_						)
			return hidden_state






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_		,   a_		,   a_ = 1		,   a_ = "relu"						):
			super().__init__()
			lowerCAmelCase  :     Union[str, Any]       						= in_channels != out_channels or stride != 1
			lowerCAmelCase  :     str       						= (
			    ResNetShortCut(a_		,   a_		,   stride=a_						) if should_apply_shortcut else nn.Identity()
			)
			lowerCAmelCase  :     List[Any]       						= nn.Sequential(
			    ResNetConvLayer(a_		,   a_		,   stride=a_						)		,   ResNetConvLayer(a_		,   a_		,   activation=a_						)		,   )
			lowerCAmelCase  :     Optional[Any]       						= ACTaFN[activation]





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Optional[Any]       						= hidden_state
			lowerCAmelCase  :     Optional[int]       						= self.layer(a_						)
			lowerCAmelCase  :     List[Any]       						= self.shortcut(a_						)
			hidden_state += residual
			lowerCAmelCase  :     Dict       						= self.activation(a_						)
			return hidden_state






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_		,   a_		,   a_ = 1		,   a_ = "relu"		,   a_ = 4						):
			super().__init__()
			lowerCAmelCase  :     Any       						= in_channels != out_channels or stride != 1
			lowerCAmelCase  :     int       						= out_channels // reduction
			lowerCAmelCase  :     Dict       						= (
			    ResNetShortCut(a_		,   a_		,   stride=a_						) if should_apply_shortcut else nn.Identity()
			)
			lowerCAmelCase  :     List[str]       						= nn.Sequential(
			    ResNetConvLayer(a_		,   a_		,   kernel_size=1						)		,   ResNetConvLayer(a_		,   a_		,   stride=a_						)		,   ResNetConvLayer(a_		,   a_		,   kernel_size=1		,   activation=a_						)		,   )
			lowerCAmelCase  :     int       						= ACTaFN[activation]





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Dict       						= hidden_state
			lowerCAmelCase  :     List[Any]       						= self.layer(a_						)
			lowerCAmelCase  :     str       						= self.shortcut(a_						)
			hidden_state += residual
			lowerCAmelCase  :     Dict       						= self.activation(a_						)
			return hidden_state






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_		,   a_		,   a_		,   a_ = 2		,   a_ = 2		,   ):
			super().__init__()

			lowerCAmelCase  :     int       						= ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

			lowerCAmelCase  :     Optional[Any]       						= nn.Sequential(
			    # downsampling is done in the first layer with stride of 2
			    layer(a_		,   a_		,   stride=a_		,   activation=config.hidden_act						)		,   *[layer(a_		,   a_		,   activation=config.hidden_act						) for _ in range(depth - 1						)]		,   )





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     int       						= input
			for layer in self.layers:
					lowerCAmelCase  :     Optional[Any]       						= layer(a_						)
			return hidden_state






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_						):
			super().__init__()
			lowerCAmelCase  :     Dict       						= nn.ModuleList([]						)
			# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
			self.stages.append(
			    ResNetStage(
			        a_		,   config.embedding_size		,   config.hidden_sizes[0]		,   stride=2 if config.downsample_in_first_stage else 1		,   depth=config.depths[0]		,   )						)
			lowerCAmelCase  :     Optional[Any]       						= zip(config.hidden_sizes		,   config.hidden_sizes[1:]						)
			for (in_channels, out_channels), depth in zip(a_		,   config.depths[1:]						):
					self.stages.append(ResNetStage(a_		,   a_		,   a_		,   depth=a_						)						)





	def 	_lowerCamelCase     ( self		,   a_		,   a_ = False		,   a_ = True						):
			lowerCAmelCase  :     Tuple       						= () if output_hidden_states else None

			for stage_module in self.stages:
					if output_hidden_states:
							lowerCAmelCase  :     Any       						= hidden_states + (hidden_state,)

					lowerCAmelCase  :     Optional[Any]       						= stage_module(a_						)

			if output_hidden_states:
					lowerCAmelCase  :     Any       						= hidden_states + (hidden_state,)

			if not return_dict:
					return tuple(v for v in [hidden_state, hidden_states] if v is not None						)

			return BaseModelOutputWithNoAttention(
			    last_hidden_state=a_		,   hidden_states=a_		,   )






class 		lowerCamelCase	(   _A							):
	snake_case_   = ResNetConfig
	snake_case_   = '''resnet'''
	snake_case_   = '''pixel_values'''
	snake_case_   = True



	def 	_lowerCamelCase     ( self		,   a_						):


			if isinstance(a_		,   nn.Convad						):
					nn.init.kaiming_normal_(module.weight		,   mode="fan_out"		,   nonlinearity="relu"						)
			elif isinstance(a_		,   (nn.BatchNormad, nn.GroupNorm)						):
					nn.init.constant_(module.weight		,   1						)
					nn.init.constant_(module.bias		,   0						)





	def 	_lowerCamelCase     ( self		,   a_		,   a_=False						):


			if isinstance(a_		,   a_						):
					lowerCAmelCase  :     Optional[int]       						= value






lowerCAmelCase									=   R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

lowerCAmelCase									=   R"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""
@add_start_docstrings(
    "The bare ResNet model outputting raw features without any specific head on top."   , _A   , )
class 		lowerCamelCase	(   _A							):



	def __init__( self		,   a_						):
			super().__init__(a_						)
			lowerCAmelCase  :     Optional[Any]       						= config
			lowerCAmelCase  :     List[str]       						= ResNetEmbeddings(a_						)
			lowerCAmelCase  :     Union[str, Any]       						= ResNetEncoder(a_						)
			lowerCAmelCase  :     Dict       						= nn.AdaptiveAvgPoolad((1, 1)						)
			# Initialize weights and apply final processing
			self.post_init()





	@add_start_docstrings_to_model_forward(a_						)
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC		,   output_type=a_		,   config_class=_CONFIG_FOR_DOC		,   modality="vision"		,   expected_output=_EXPECTED_OUTPUT_SHAPE		,   )
	def 	_lowerCamelCase     ( self		,   a_		,   a_ = None		,   a_ = None						):
			lowerCAmelCase  :     Optional[Any]       						= (
			    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
			)
			lowerCAmelCase  :     Optional[Any]       						= return_dict if return_dict is not None else self.config.use_return_dict

			lowerCAmelCase  :     Union[str, Any]       						= self.embedder(a_						)

			lowerCAmelCase  :     Dict       						= self.encoder(
			    a_		,   output_hidden_states=a_		,   return_dict=a_						)

			lowerCAmelCase  :     Dict       						= encoder_outputs[0]

			lowerCAmelCase  :     Optional[Any]       						= self.pooler(a_						)

			if not return_dict:
					return (last_hidden_state, pooled_output) + encoder_outputs[1:]

			return BaseModelOutputWithPoolingAndNoAttention(
			    last_hidden_state=a_		,   pooler_output=a_		,   hidden_states=encoder_outputs.hidden_states		,   )






@add_start_docstrings(
    "\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "   , _A   , )
class 		lowerCamelCase	(   _A							):



	def __init__( self		,   a_						):
			super().__init__(a_						)
			lowerCAmelCase  :     Optional[Any]       						= config.num_labels
			lowerCAmelCase  :     List[Any]       						= ResNetModel(a_						)
			# classification head
			lowerCAmelCase  :     Tuple       						= nn.Sequential(
			    nn.Flatten()		,   nn.Linear(config.hidden_sizes[-1]		,   config.num_labels						) if config.num_labels > 0 else nn.Identity()		,   )
			# initialize weights and apply final processing
			self.post_init()





	@add_start_docstrings_to_model_forward(a_						)
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT		,   output_type=a_		,   config_class=_CONFIG_FOR_DOC		,   expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,   )
	def 	_lowerCamelCase     ( self		,   a_ = None		,   a_ = None		,   a_ = None		,   a_ = None		,   ):
			lowerCAmelCase  :     Dict       						= return_dict if return_dict is not None else self.config.use_return_dict

			lowerCAmelCase  :     List[str]       						= self.resnet(a_		,   output_hidden_states=a_		,   return_dict=a_						)

			lowerCAmelCase  :     Optional[int]       						= outputs.pooler_output if return_dict else outputs[1]

			lowerCAmelCase  :     Optional[Any]       						= self.classifier(a_						)

			lowerCAmelCase  :     Dict       						= None

			if labels is not None:
					if self.config.problem_type is None:
							if self.num_labels == 1:
									lowerCAmelCase  :     str       						= "regression"
							elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
									lowerCAmelCase  :     int       						= "single_label_classification"
							else:
									lowerCAmelCase  :     Optional[Any]       						= "multi_label_classification"
					if self.config.problem_type == "regression":
							lowerCAmelCase  :     Union[str, Any]       						= MSELoss()
							if self.num_labels == 1:
									lowerCAmelCase  :     List[Any]       						= loss_fct(logits.squeeze()		,   labels.squeeze()						)
							else:
									lowerCAmelCase  :     Optional[int]       						= loss_fct(a_		,   a_						)
					elif self.config.problem_type == "single_label_classification":
							lowerCAmelCase  :     int       						= CrossEntropyLoss()
							lowerCAmelCase  :     List[Any]       						= loss_fct(logits.view(-1		,   self.num_labels						)		,   labels.view(-1						)						)
					elif self.config.problem_type == "multi_label_classification":
							lowerCAmelCase  :     Dict       						= BCEWithLogitsLoss()
							lowerCAmelCase  :     Tuple       						= loss_fct(a_		,   a_						)

			if not return_dict:
					lowerCAmelCase  :     Union[str, Any]       						= (logits,) + outputs[2:]
					return (loss,) + output if loss is not None else output

			return ImageClassifierOutputWithNoAttention(loss=a_		,   logits=a_		,   hidden_states=outputs.hidden_states						)






@add_start_docstrings(
    "\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    "   , _A   , )
class 		lowerCamelCase	(   _A   , _A							):



	def __init__( self		,   a_						):
			super().__init__(a_						)
			super()._init_backbone(a_						)

			lowerCAmelCase  :     Optional[Any]       						= [config.embedding_size] + config.hidden_sizes
			lowerCAmelCase  :     Any       						= ResNetEmbeddings(a_						)
			lowerCAmelCase  :     Tuple       						= ResNetEncoder(a_						)

			# initialize weights and apply final processing
			self.post_init()





	@add_start_docstrings_to_model_forward(a_						)
	@replace_return_docstrings(output_type=a_		,   config_class=_CONFIG_FOR_DOC						)
	def 	_lowerCamelCase     ( self		,   a_		,   a_ = None		,   a_ = None						):
			lowerCAmelCase  :     str       						= return_dict if return_dict is not None else self.config.use_return_dict
			lowerCAmelCase  :     Union[str, Any]       						= (
			    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
			)

			lowerCAmelCase  :     int       						= self.embedder(a_						)

			lowerCAmelCase  :     str       						= self.encoder(a_		,   output_hidden_states=a_		,   return_dict=a_						)

			lowerCAmelCase  :     List[str]       						= outputs.hidden_states

			lowerCAmelCase  :     Optional[Any]       						= ()
			for idx, stage in enumerate(self.stage_names						):
					if stage in self.out_features:
							feature_maps += (hidden_states[idx],)

			if not return_dict:
					lowerCAmelCase  :     Optional[int]       						= (feature_maps,)
					if output_hidden_states:
							output += (outputs.hidden_states,)
					return output

			return BackboneOutput(
			    feature_maps=a_		,   hidden_states=outputs.hidden_states if output_hidden_states else None		,   attentions=a_		,   )









'''simple docstring'''







import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import (
        MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
        MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
        MODEL_MAPPING,
        DeiTForImageClassification,
        DeiTForImageClassificationWithTeacher,
        DeiTForMaskedImageModeling,
        DeiTModel,
    )
    from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import DeiTImageProcessor

class 	_snake_case:






 def __init__(self       :  List[Any]    ,		a       :  str    ,		a       :  Any=13    ,		a       :  Optional[Any]=30    ,		a       :  Union[str, Any]=2    ,		a       :  List[str]=3    ,		a       :  List[str]=True    ,		a       :  List[Any]=True    ,		a       :  Tuple=32    ,		a       :  Optional[Any]=5    ,		a       :  str=4    ,		a       :  List[str]=37    ,		a       :  List[str]="gelu"    ,		a       :  int=0.1    ,		a       :  int=0.1    ,		a       :  str=10    ,		a       :  Tuple=0.02    ,		a       :  Union[str, Any]=3    ,		a       :  List[str]=None    ,		a       :  Any=2    ,		)       ->   Optional[int]:






    """simple docstring"""


    A__       =     parent
    A__       =     batch_size
    A__       =     image_size
    A__       =     patch_size
    A__       =     num_channels
    A__       =     is_training
    A__       =     use_labels
    A__       =     hidden_size
    A__       =     num_hidden_layers
    A__       =     num_attention_heads
    A__       =     intermediate_size
    A__       =     hidden_act
    A__       =     hidden_dropout_prob
    A__       =     attention_probs_dropout_prob
    A__       =     type_sequence_label_size
    A__       =     initializer_range
    A__       =     scope
    A__       =     encoder_stride

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






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   List[str]:






    """simple docstring"""


    A__       =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

    A__       =     self.get_config()

    return config, pixel_values, labels






 def 						_UpperCamelCase (self       :  Union[str, Any]    )       ->   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=a    ,		initializer_range=self.initializer_range    ,		encoder_stride=self.encoder_stride    ,		)






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  Tuple    ,		a       :  Dict    ,		a       :  Optional[Any]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     DeiTModel(config=a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    )
    self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)    )






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  Any    ,		a       :  Optional[Any]    ,		a       :  Optional[int]    )       ->   Dict:






    """simple docstring"""


    A__       =     DeiTForMaskedImageModeling(config=a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    )
    self.parent.assertEqual(
        result.reconstruction.shape    ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

    # test greyscale images
    A__       =     1
    A__       =     DeiTForMaskedImageModeling(a    )
    model.to(a    )
    model.eval()

    A__       =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
    A__       =     model(a    )
    self.parent.assertEqual(result.reconstruction.shape    ,		(self.batch_size, 1, self.image_size, self.image_size)    )






 def 						_UpperCamelCase (self       :  Optional[Any]    ,		a       :  Optional[Any]    ,		a       :  Union[str, Any]    ,		a       :  Union[str, Any]    )       ->   str:






    """simple docstring"""


    A__       =     self.type_sequence_label_size
    A__       =     DeiTForImageClassification(a    )
    model.to(a    )
    model.eval()
    A__       =     model(a    ,		labels=a    )
    self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)    )

    # test greyscale images
    A__       =     1
    A__       =     DeiTForImageClassification(a    )
    model.to(a    )
    model.eval()

    A__       =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
    A__       =     model(a    ,		labels=a    )
    self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)    )






 def 						_UpperCamelCase (self       :  List[Any]    )       ->   Optional[int]:






    """simple docstring"""


    A__       =     self.prepare_config_and_inputs()
    (
        (
        A__
    )  ,			(
        A__
    )  ,			(
        A__
    )  ,			
    )       =     config_and_inputs
    A__       =     {'pixel_values': pixel_values}
    return config, inputs_dict




@require_torch
class 	_snake_case(			UpperCAmelCase       , UpperCAmelCase       , unittest.TestCase      ):
 __snake_case:							Optional[Any]						=					(
     (
         DeiTModel,
         DeiTForImageClassification,
         DeiTForImageClassificationWithTeacher,
         DeiTForMaskedImageModeling,
     )
     if is_torch_available()
     else ()
 )
 __snake_case:							int						=					(
     {
         '''feature-extraction''': DeiTModel,
         '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
     }
     if is_torch_available()
     else {}
 )

 __snake_case:							Any						=					False
 __snake_case:							Any						=					False
 __snake_case:							Any						=					False






 def 						_UpperCamelCase (self       :  Any    )       ->   int:






    """simple docstring"""


    A__       =     DeiTModelTester(self    )
    A__       =     ConfigTester(self    ,		config_class=a    ,		has_text_modality=a    ,		hidden_size=37    )






 def 						_UpperCamelCase (self       :  Dict    )       ->   Tuple:






    """simple docstring"""


    self.config_tester.run_common_tests()






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






    """simple docstring"""


    pass






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   Tuple:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       A__       =     model_class(a    )
       self.assertIsInstance(model.get_input_embeddings()    ,		(nn.Module)    )
       A__       =     model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(a    ,		nn.Linear    )    )






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






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

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

       A__       =     ['pixel_values']
       self.assertListEqual(arg_names[:1]    ,		a    )






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






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*a    )






 def 						_UpperCamelCase (self       :  Dict    )       ->   Any:






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_image_modeling(*a    )






 def 						_UpperCamelCase (self       :  List[Any]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*a    )






 def 						_UpperCamelCase (self       :  Optional[int]    ,		a       :  int    ,		a       :  Union[str, Any]    ,		a       :  List[Any]=False    )       ->   Optional[int]:






    """simple docstring"""


    A__       =     super()._prepare_for_class(a    ,		a    ,		return_labels=a    )

    if return_labels:
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
          del inputs_dict["labels"]

    return inputs_dict






 def 						_UpperCamelCase (self       :  Any    )       ->   Tuple:






    """simple docstring"""


    if not self.model_tester.is_training:
       return

    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()
    A__       =     True

    for model_class in self.all_model_classes:
       # DeiTForImageClassificationWithTeacher supports inference-only
       if (
           model_class in get_values(a    )
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
          continue
       A__       =     model_class(a    )
       model.to(a    )
       model.train()
       A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )
       A__       =     model(**a    ).loss
       loss.backward()






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   int:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()
    if not self.model_tester.is_training:
       return

    A__       =     False
    A__       =     True

    for model_class in self.all_model_classes:
       if model_class in get_values(a    ) or not model_class.supports_gradient_checkpointing:
          continue
       # DeiTForImageClassificationWithTeacher supports inference-only
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
          continue
       A__       =     model_class(a    )
       model.gradient_checkpointing_enable()
       model.to(a    )
       model.train()
       A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )
       A__       =     model(**a    ).loss
       loss.backward()






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   List[str]:






    """simple docstring"""


    A__  ,			A__       =     self.model_tester.prepare_config_and_inputs_for_common()

    A__       =     [
        {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
        {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
        {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
    ]

    for model_class in self.all_model_classes:
       if (
           model_class
           not in [
               *get_values(a    ),
               *get_values(a    ),
           ]
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
          continue

       for problem_type in problem_types:
          with self.subTest(msg=f"""Testing {model_class} with {problem_type["title"]}"""    ):
             A__       =     problem_type['title']
             A__       =     problem_type['num_labels']

             A__       =     model_class(a    )
             model.to(a    )
             model.train()

             A__       =     self._prepare_for_class(a    ,		a    ,		return_labels=a    )

             if problem_type["num_labels"] > 1:
                A__       =     inputs['labels'].unsqueeze(1    ).repeat(1    ,		problem_type['num_labels']    )

             A__       =     inputs['labels'].to(problem_type['dtype']    )

             # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
             # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
             # they have the same size." which is a symptom something in wrong for the regression problem.
             # See https://github.com/huggingface/transformers/issues/11780
             with warnings.catch_warnings(record=a    ) as warning_list:
                A__       =     model(**a    ).loss
             for w in warning_list:
                if "Using a target size that is different to the input size" in str(w.message    ):
                   raise ValueError(
                       f"""Something is going wrong in the regression problem: intercepted {w.message}"""    )

             loss.backward()






 @slow
 def 						_UpperCamelCase (self       :  Union[str, Any]    )       ->   Tuple:






    """simple docstring"""


    for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       A__       =     DeiTModel.from_pretrained(a    )
       self.assertIsNotNone(a    )







def 						_A      ( ):






   '''simple docstring'''







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







@require_torch
@require_vision
class 	_snake_case(			unittest.TestCase      ):






 @cached_property
 def 						_UpperCamelCase (self       :  Tuple    )       ->   Union[str, Any]:






    """simple docstring"""


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






 @slow
 def 						_UpperCamelCase (self       :  List[str]    )       ->   Optional[Any]:






    """simple docstring"""


    A__       =     DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'    ).to(
        a    )

    A__       =     self.default_image_processor
    A__       =     prepare_img()
    A__       =     image_processor(images=a    ,		return_tensors='pt'    ).to(a    )

    # forward pass
    with torch.no_grad():
       A__       =     model(**a    )

    # verify the logits
    A__       =     torch.Size((1, 10_00)    )
    self.assertEqual(outputs.logits.shape    ,		a    )

    A__       =     torch.tensor([-1.0266, 0.1912, -1.2861]    ).to(a    )

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






 @slow
 @require_accelerate
 @require_torch_gpu
 def 						_UpperCamelCase (self       :  Tuple    )       ->   str:






    """simple docstring"""


    A__       =     DeiTModel.from_pretrained(
        'facebook/deit-base-distilled-patch16-224'    ,		torch_dtype=torch.floataa    ,		device_map='auto'    )
    A__       =     self.default_image_processor

    A__       =     prepare_img()
    A__       =     image_processor(images=a    ,		return_tensors='pt'    )
    A__       =     inputs.pixel_values.to(a    )

    # forward pass to make sure inference works in fp16
    with torch.no_grad():
       A__       =     model(a    )


