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



     def __init__(					self				,      lowerCAmelCase__				,      lowerCAmelCase__=13				,      lowerCAmelCase__=30				,      lowerCAmelCase__=2				,      lowerCAmelCase__=3				,      lowerCAmelCase__=True				,      lowerCAmelCase__=True				,      lowerCAmelCase__=32				,      lowerCAmelCase__=5				,      lowerCAmelCase__=4				,      lowerCAmelCase__=37				,      lowerCAmelCase__="gelu"				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=10				,      lowerCAmelCase__=0.02				,      lowerCAmelCase__=3				,      lowerCAmelCase__=None				,      lowerCAmelCase__=2				,      ):
            _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 			a__  (					self       ):
            _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 			a__  (					self       ):
            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=lowerCAmelCase__				,      initializer_range=self.initializer_range				,      encoder_stride=self.encoder_stride				,      )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	DeiTModel(config=lowerCAmelCase__       )
            model.to(lowerCAmelCase__       )
            model.eval()
            _A=	model(lowerCAmelCase__       )
            self.parent.assertEqual(result.last_hidden_state.shape				,      (self.batch_size, self.seq_length, self.hidden_size)       )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	DeiTForMaskedImageModeling(config=lowerCAmelCase__       )
            model.to(lowerCAmelCase__       )
            model.eval()
            _A=	model(lowerCAmelCase__       )
            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(lowerCAmelCase__       )
            model.to(lowerCAmelCase__       )
            model.eval()

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



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	self.type_sequence_label_size
            _A=	DeiTForImageClassification(lowerCAmelCase__       )
            model.to(lowerCAmelCase__       )
            model.eval()
            _A=	model(lowerCAmelCase__				,      labels=lowerCAmelCase__       )
            self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.type_sequence_label_size)       )

            # test greyscale images
            _A=	1
            _A=	DeiTForImageClassification(lowerCAmelCase__       )
            model.to(lowerCAmelCase__       )
            model.eval()

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





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





@require_torch
class      lowerCAmelCase						(					_a			,		_a			,		unittest.TestCase			):
     _SCREAMING_SNAKE_CASE     :     int		=(
         (
             DeiTModel,
             DeiTForImageClassification,
             DeiTForImageClassificationWithTeacher,
             DeiTForMaskedImageModeling,
         )
         if is_torch_available()
         else ()
     )
     _SCREAMING_SNAKE_CASE     :     Dict		=(
         {
             """feature-extraction""": DeiTModel,
             """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
         }
         if is_torch_available()
         else {}
     )

     _SCREAMING_SNAKE_CASE     :     str		=False
     _SCREAMING_SNAKE_CASE     :     Union[str, Any]		=False
     _SCREAMING_SNAKE_CASE     :     Optional[Any]		=False



     def 			a__  (					self       ):
            _A=	DeiTModelTester(self       )
            _A=	ConfigTester(self				,      config_class=lowerCAmelCase__				,      has_text_modality=lowerCAmelCase__				,      hidden_size=37       )



     def 			a__  (					self       ):
            self.config_tester.run_common_tests()



     @unittest.skip(reason='DeiT does not use inputs_embeds'       )
     def 			a__  (					self       ):
            pass



     def 			a__  (					self       ):
            _A,			_A=	self.model_tester.prepare_config_and_inputs_for_common()

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



     def 			a__  (					self       ):
            _A,			_A=	self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _A=	model_class(lowerCAmelCase__       )
                   _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]				,      lowerCAmelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCAmelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__       )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__=False       ):
            _A=	super()._prepare_for_class(lowerCAmelCase__				,      lowerCAmelCase__				,      return_labels=lowerCAmelCase__       )

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

            return inputs_dict



     def 			a__  (					self       ):
            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(lowerCAmelCase__       )
                       or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
                   ):
                          continue
                   _A=	model_class(lowerCAmelCase__       )
                   model.to(lowerCAmelCase__       )
                   model.train()
                   _A=	self._prepare_for_class(lowerCAmelCase__				,      lowerCAmelCase__				,      return_labels=lowerCAmelCase__       )
                   _A=	model(**lowerCAmelCase__       ).loss
                   loss.backward()



     def 			a__  (					self       ):
            _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(lowerCAmelCase__       ) or not model_class.supports_gradient_checkpointing:
                          continue
                   # DeiTForImageClassificationWithTeacher supports inference-only
                   if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
                          continue
                   _A=	model_class(lowerCAmelCase__       )
                   model.gradient_checkpointing_enable()
                   model.to(lowerCAmelCase__       )
                   model.train()
                   _A=	self._prepare_for_class(lowerCAmelCase__				,      lowerCAmelCase__				,      return_labels=lowerCAmelCase__       )
                   _A=	model(**lowerCAmelCase__       ).loss
                   loss.backward()



     def 			a__  (					self       ):
            _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(lowerCAmelCase__       ),
                           *get_values(lowerCAmelCase__       ),
                       ]
                       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(lowerCAmelCase__       )
                                 model.to(lowerCAmelCase__       )
                                 model.train()

                                 _A=	self._prepare_for_class(lowerCAmelCase__				,      lowerCAmelCase__				,      return_labels=lowerCAmelCase__       )

                                 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=lowerCAmelCase__       ) as warning_list:
                                        _A=	model(**lowerCAmelCase__       ).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 			a__  (					self       ):
            for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A=	DeiTModel.from_pretrained(lowerCAmelCase__       )
                   self.assertIsNotNone(lowerCAmelCase__       )


def 							UpperCamelCase				(    )		->						Optional[int]:




       '''simple docstring'''






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





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



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



     @slow
     def 			a__  (					self       ):
            _A=	DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'       ).to(
                lowerCAmelCase__       )

            _A=	self.default_image_processor
            _A=	prepare_img()
            _A=	image_processor(images=lowerCAmelCase__				,      return_tensors='pt'       ).to(lowerCAmelCase__       )

            # forward pass
            with torch.no_grad():
                   _A=	model(**lowerCAmelCase__       )

            # verify the logits
            _A=	torch.Size((1, 1000)       )
            self.assertEqual(outputs.logits.shape				,      lowerCAmelCase__       )

            _A=	torch.tensor([-1.0266, 0.1912, -1.2861]       ).to(lowerCAmelCase__       )

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





     @slow
     @require_accelerate
     @require_torch_gpu
     def 			a__  (					self       ):
            _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=lowerCAmelCase__				,      return_tensors='pt'       )
            _A=	inputs.pixel_values.to(lowerCAmelCase__       )

            # forward pass to make sure inference works in fp16
            with torch.no_grad():
                   _A=	model(lowerCAmelCase__       )
import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

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


UpperCAmelCase_								=							logging.get_logger(__name__)

UpperCAmelCase_								=							{
    '''nvidia/segformer-b0-finetuned-ade-512-512''': (
        '''https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'''
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}





class      lowerCAmelCase						(					_a			):
     _SCREAMING_SNAKE_CASE     :     Tuple		="""segformer"""



     def __init__(					self				,      lowerCAmelCase__=3				,      lowerCAmelCase__=4				,      lowerCAmelCase__=[2, 2, 2, 2]				,      lowerCAmelCase__=[8, 4, 2, 1]				,      lowerCAmelCase__=[32, 64, 160, 256]				,      lowerCAmelCase__=[7, 3, 3, 3]				,      lowerCAmelCase__=[4, 2, 2, 2]				,      lowerCAmelCase__=[1, 2, 5, 8]				,      lowerCAmelCase__=[4, 4, 4, 4]				,      lowerCAmelCase__="gelu"				,      lowerCAmelCase__=0.0				,      lowerCAmelCase__=0.0				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.02				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=1E-6				,      lowerCAmelCase__=256				,      lowerCAmelCase__=255				,      **lowerCAmelCase__				,      ):
            super().__init__(**lowerCAmelCase__       )

            if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
                   warnings.warn(
                       'Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'
                       ' removed, as the behaviour will default to that of reshape_last_stage = True.'				,      lowerCAmelCase__				,      )

            _A=	num_channels
            _A=	num_encoder_blocks
            _A=	depths
            _A=	sr_ratios
            _A=	hidden_sizes
            _A=	patch_sizes
            _A=	strides
            _A=	mlp_ratios
            _A=	num_attention_heads
            _A=	hidden_act
            _A=	hidden_dropout_prob
            _A=	attention_probs_dropout_prob
            _A=	classifier_dropout_prob
            _A=	initializer_range
            _A=	drop_path_rate
            _A=	layer_norm_eps
            _A=	decoder_hidden_size
            _A=	kwargs.get('reshape_last_stage'				,      lowerCAmelCase__       )
            _A=	semantic_loss_ignore_index





class      lowerCAmelCase						(					_a			):
     _SCREAMING_SNAKE_CASE     :     List[str]		=version.parse("""1.11"""			)



     @property
     def 			a__  (					self       ):
            return OrderedDict(
                [
                    ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
                ]       )



     @property
     def 			a__  (					self       ):
            return 1E-4





     @property
     def 			a__  (					self       ):
            return 12