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


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

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


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

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

      _UpperCAmelCase    			=   self.get_config()

      return config, pixel_values, labels


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      return DeiTConfig(
          image_size=self.image_size ,					patch_size=self.patch_size ,					num_channels=self.num_channels ,					hidden_size=self.hidden_size ,					num_hidden_layers=self.num_hidden_layers ,					num_attention_heads=self.num_attention_heads ,					intermediate_size=self.intermediate_size ,					hidden_act=self.hidden_act ,					hidden_dropout_prob=self.hidden_dropout_prob ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob ,					is_decoder=__lowerCAmelCase ,					initializer_range=self.initializer_range ,					encoder_stride=self.encoder_stride ,					)


    def   lowerCAmelCase_	(       self						:	Any ,					__lowerCAmelCase						:	Dict ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	Optional[Any]       ):
      _UpperCAmelCase    			=   DeiTModel(config=__lowerCAmelCase       )
      model.to(__lowerCAmelCase       )
      model.eval()
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)       )


    def   lowerCAmelCase_	(       self						:	Dict ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	int       ):
      _UpperCAmelCase    			=   DeiTForMaskedImageModeling(config=__lowerCAmelCase       )
      model.to(__lowerCAmelCase       )
      model.eval()
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(
          result.reconstruction.shape ,					(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

      # test greyscale images
      _UpperCAmelCase    			=   1
      _UpperCAmelCase    			=   DeiTForMaskedImageModeling(__lowerCAmelCase       )
      model.to(__lowerCAmelCase       )
      model.eval()

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


    def   lowerCAmelCase_	(       self						:	List[Any] ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	Tuple       ):
      _UpperCAmelCase    			=   self.type_sequence_label_size
      _UpperCAmelCase    			=   DeiTForImageClassification(__lowerCAmelCase       )
      model.to(__lowerCAmelCase       )
      model.eval()
      _UpperCAmelCase    			=   model(__lowerCAmelCase ,					labels=__lowerCAmelCase       )
      self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.type_sequence_label_size)       )

      # test greyscale images
      _UpperCAmelCase    			=   1
      _UpperCAmelCase    			=   DeiTForImageClassification(__lowerCAmelCase       )
      model.to(__lowerCAmelCase       )
      model.eval()

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


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   self.prepare_config_and_inputs()
      (
          (
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					
      )    			=   config_and_inputs
      _UpperCAmelCase    			=   {"""pixel_values""": pixel_values}
      return config, inputs_dict



@require_torch
class 							a					(		lowerCAmelCase_						,				lowerCAmelCase_						,				unittest.TestCase ):
    _snake_case  :	Dict  				=				(
        (
            DeiTModel,
            DeiTForImageClassification,
            DeiTForImageClassificationWithTeacher,
            DeiTForMaskedImageModeling,
        )
        if is_torch_available()
        else ()
    )
    _snake_case  :	List[Any]  				=				(
        {
            'feature-extraction': DeiTModel,
            'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
        }
        if is_torch_available()
        else {}
    )

    _snake_case  :	Dict  				=				False
    _snake_case  :	Optional[Any]  				=				False
    _snake_case  :	Optional[Any]  				=				False


    def   lowerCAmelCase_	(       self						:	Tuple       ):
      _UpperCAmelCase    			=   DeiTModelTester(self       )
      _UpperCAmelCase    			=   ConfigTester(self ,					config_class=__lowerCAmelCase ,					has_text_modality=__lowerCAmelCase ,					hidden_size=37       )


    def   lowerCAmelCase_	(       self						:	List[str]       ):
      self.config_tester.run_common_tests()


    @unittest.skip(reason="""DeiT does not use inputs_embeds"""       )
    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      pass


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()

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


    def   lowerCAmelCase_	(       self						:	Dict       ):
      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()

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

        _UpperCAmelCase    			=   ["""pixel_values"""]
        self.assertListEqual(arg_names[:1] ,					__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	str       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Dict ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	Any=False       ):
      _UpperCAmelCase    			=   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   lowerCAmelCase_	(       self						:	Dict       ):
      if not self.model_tester.is_training:
        return

      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()
      _UpperCAmelCase    			=   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
        _UpperCAmelCase    			=   model_class(__lowerCAmelCase       )
        model.to(__lowerCAmelCase       )
        model.train()
        _UpperCAmelCase    			=   self._prepare_for_class(__lowerCAmelCase ,					__lowerCAmelCase ,					return_labels=__lowerCAmelCase       )
        _UpperCAmelCase    			=   model(**__lowerCAmelCase       ).loss
        loss.backward()


    def   lowerCAmelCase_	(       self						:	int       ):
      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()
      if not self.model_tester.is_training:
        return

      _UpperCAmelCase    			=   False
      _UpperCAmelCase    			=   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
        _UpperCAmelCase    			=   model_class(__lowerCAmelCase       )
        model.gradient_checkpointing_enable()
        model.to(__lowerCAmelCase       )
        model.train()
        _UpperCAmelCase    			=   self._prepare_for_class(__lowerCAmelCase ,					__lowerCAmelCase ,					return_labels=__lowerCAmelCase       )
        _UpperCAmelCase    			=   model(**__lowerCAmelCase       ).loss
        loss.backward()


    def   lowerCAmelCase_	(       self						:	Optional[Any]       ):
      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()

      _UpperCAmelCase    			=   [
          {"""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"]}'''       ):
            _UpperCAmelCase    			=   problem_type["""title"""]
            _UpperCAmelCase    			=   problem_type["""num_labels"""]

            _UpperCAmelCase    			=   model_class(__lowerCAmelCase       )
            model.to(__lowerCAmelCase       )
            model.train()

            _UpperCAmelCase    			=   self._prepare_for_class(__lowerCAmelCase ,					__lowerCAmelCase ,					return_labels=__lowerCAmelCase       )

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

            _UpperCAmelCase    			=   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:
              _UpperCAmelCase    			=   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   lowerCAmelCase_	(       self						:	int       ):
      for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase    			=   DeiTModel.from_pretrained(__lowerCAmelCase       )
        self.assertIsNotNone(__lowerCAmelCase       )






def      __UpperCAmelCase       (      ):







  """simple docstring"""
  _UpperCAmelCase    			=   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
  return image


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


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


    @slow
    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""       ).to(
          __lowerCAmelCase       )

      _UpperCAmelCase    			=   self.default_image_processor
      _UpperCAmelCase    			=   prepare_img()
      _UpperCAmelCase    			=   image_processor(images=__lowerCAmelCase ,					return_tensors="""pt"""       ).to(__lowerCAmelCase       )

      # forward pass
      with torch.no_grad():
        _UpperCAmelCase    			=   model(**__lowerCAmelCase       )

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

      _UpperCAmelCase    			=   torch.tensor([-1.0_266, 0.1_912, -1.2_861]       ).to(__lowerCAmelCase       )

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


    @slow
    @require_accelerate
    @require_torch_gpu
    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   DeiTModel.from_pretrained(
          """facebook/deit-base-distilled-patch16-224""" ,					torch_dtype=torch.floataa ,					device_map="""auto"""       )
      _UpperCAmelCase    			=   self.default_image_processor

      _UpperCAmelCase    			=   prepare_img()
      _UpperCAmelCase    			=   image_processor(images=__lowerCAmelCase ,					return_tensors="""pt"""       )
      _UpperCAmelCase    			=   inputs.pixel_values.to(__lowerCAmelCase       )

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

"""simple docstring"""


import pytest

from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs






@pytest.mark.parametrize(
    """kwargs, expected""" ,[
        ({"""num_shards""": 0, """max_num_jobs""": 1}, []),
        ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10		)]),
        ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(lowercase ,i + 1		) for i in range(10		)]),
        ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1		)]),
        ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 ,4		), range(4 ,7		), range(7 ,10		)]),
        ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 ,1		), range(1 ,2		), range(2 ,3		)]),
    ] ,)
def      __UpperCAmelCase       (      lowercase ,lowercase		):







  """simple docstring"""
  _UpperCAmelCase    			=   _distribute_shards(**lowercase		)
  assert out == expected






@pytest.mark.parametrize(
    """gen_kwargs, max_num_jobs, expected""" ,[
        ({"""foo""": 0}, 10, [{"""foo""": 0}]),
        ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]),
        ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]),
        ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]),
        ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]),
    ] ,)
def      __UpperCAmelCase       (      lowercase ,lowercase ,lowercase		):







  """simple docstring"""
  _UpperCAmelCase    			=   _split_gen_kwargs(lowercase ,lowercase		)
  assert out == expected






@pytest.mark.parametrize(
    """gen_kwargs, expected""" ,[
        ({"""foo""": 0}, 1),
        ({"""shards""": [0]}, 1),
        ({"""shards""": [0, 1, 2, 3]}, 4),
        ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4),
        ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4),
        ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError),
    ] ,)
def      __UpperCAmelCase       (      lowercase ,lowercase		):







  """simple docstring"""
  if expected is RuntimeError:
    with pytest.raises(lowercase		):
      _number_of_shards_in_gen_kwargs(lowercase		)
  else:
    _UpperCAmelCase    			=   _number_of_shards_in_gen_kwargs(lowercase		)
    assert out == expected
