





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

    '''simple docstring'''


    def __init__(		self ,		A ,		A=13 ,		A=30 ,		A=2 ,		A=3 ,		A=True ,		A=True ,		A=32 ,		A=5 ,		A=4 ,		A=37 ,		A="gelu" ,		A=0.1 ,		A=0.1 ,		A=10 ,		A=0.02 ,		A=3 ,		A=None ,		A=2 ,		)		->   Optional[int]:
        _SCREAMING_SNAKE_CASE									=			parent
        _SCREAMING_SNAKE_CASE									=			batch_size
        _SCREAMING_SNAKE_CASE									=			image_size
        _SCREAMING_SNAKE_CASE									=			patch_size
        _SCREAMING_SNAKE_CASE									=			num_channels
        _SCREAMING_SNAKE_CASE									=			is_training
        _SCREAMING_SNAKE_CASE									=			use_labels
        _SCREAMING_SNAKE_CASE									=			hidden_size
        _SCREAMING_SNAKE_CASE									=			num_hidden_layers
        _SCREAMING_SNAKE_CASE									=			num_attention_heads
        _SCREAMING_SNAKE_CASE									=			intermediate_size
        _SCREAMING_SNAKE_CASE									=			hidden_act
        _SCREAMING_SNAKE_CASE									=			hidden_dropout_prob
        _SCREAMING_SNAKE_CASE									=			attention_probs_dropout_prob
        _SCREAMING_SNAKE_CASE									=			type_sequence_label_size
        _SCREAMING_SNAKE_CASE									=			initializer_range
        _SCREAMING_SNAKE_CASE									=			scope
        _SCREAMING_SNAKE_CASE									=			encoder_stride

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


    def 			snake_case_(		self       )		->   Tuple:
        _SCREAMING_SNAKE_CASE									=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

        _SCREAMING_SNAKE_CASE									=			self.get_config()

        return config, pixel_values, labels


    def 			snake_case_(		self       )		->   str:
        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 			snake_case_(		self ,		A ,		A ,		A       )		->   Dict:
        _SCREAMING_SNAKE_CASE									=			DeiTModel(config=A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A       )
        self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.seq_length, self.hidden_size)       )


    def 			snake_case_(		self ,		A ,		A ,		A       )		->   Tuple:
        _SCREAMING_SNAKE_CASE									=			DeiTForMaskedImageModeling(config=A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A       )
        self.parent.assertEqual(
            result.reconstruction.shape ,		(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

        # test greyscale images
        _SCREAMING_SNAKE_CASE									=			1
        _SCREAMING_SNAKE_CASE									=			DeiTForMaskedImageModeling(A       )
        model.to(A       )
        model.eval()

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


    def 			snake_case_(		self ,		A ,		A ,		A       )		->   Any:
        _SCREAMING_SNAKE_CASE									=			self.type_sequence_label_size
        _SCREAMING_SNAKE_CASE									=			DeiTForImageClassification(A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A ,		labels=A       )
        self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.type_sequence_label_size)       )

        # test greyscale images
        _SCREAMING_SNAKE_CASE									=			1
        _SCREAMING_SNAKE_CASE									=			DeiTForImageClassification(A       )
        model.to(A       )
        model.eval()

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





    def 			snake_case_(		self       )		->   Optional[int]:
        _SCREAMING_SNAKE_CASE									=			self.prepare_config_and_inputs()
        (
            (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       
        )									=			config_and_inputs
        _SCREAMING_SNAKE_CASE									=			{"""pixel_values""": pixel_values}
        return config, inputs_dict




@require_torch
class 					a_     (				snake_case_      ,    snake_case_      ,    unittest.TestCase       ):

    '''simple docstring'''
    UpperCamelCase     = (
        (
            DeiTModel,
            DeiTForImageClassification,
            DeiTForImageClassificationWithTeacher,
            DeiTForMaskedImageModeling,
        )
        if is_torch_available()
        else ()
    )
    UpperCamelCase     = (
        {
            '''feature-extraction''': DeiTModel,
            '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
        }
        if is_torch_available()
        else {}
    )

    UpperCamelCase     = False
    UpperCamelCase     = False
    UpperCamelCase     = False


    def 			snake_case_(		self       )		->   int:
        _SCREAMING_SNAKE_CASE									=			DeiTModelTester(self       )
        _SCREAMING_SNAKE_CASE									=			ConfigTester(self ,		config_class=A ,		has_text_modality=A ,		hidden_size=37       )


    def 			snake_case_(		self       )		->   str:
        self.config_tester.run_common_tests()


    @unittest.skip(reason="""DeiT does not use inputs_embeds"""       )
    def 			snake_case_(		self       )		->   List[Any]:
        pass


    def 			snake_case_(		self       )		->   Union[str, Any]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

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


    def 			snake_case_(		self       )		->   List[str]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

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

            _SCREAMING_SNAKE_CASE									=			["""pixel_values"""]
            self.assertListEqual(arg_names[:1] ,		A       )


    def 			snake_case_(		self       )		->   int:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A       )


    def 			snake_case_(		self       )		->   int:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*A       )


    def 			snake_case_(		self       )		->   List[Any]:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*A       )


    def 			snake_case_(		self ,		A ,		A ,		A=False       )		->   Dict:
        _SCREAMING_SNAKE_CASE									=			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 			snake_case_(		self       )		->   Any:
        if not self.model_tester.is_training:
            return

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


    def 			snake_case_(		self       )		->   str:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
            return

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


    def 			snake_case_(		self       )		->   List[Any]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

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

                    _SCREAMING_SNAKE_CASE									=			model_class(A       )
                    model.to(A       )
                    model.train()

                    _SCREAMING_SNAKE_CASE									=			self._prepare_for_class(A ,		A ,		return_labels=A       )

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

                    _SCREAMING_SNAKE_CASE									=			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:
                        _SCREAMING_SNAKE_CASE									=			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 			snake_case_(		self       )		->   Optional[int]:
        for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE									=			DeiTModel.from_pretrained(A       )
            self.assertIsNotNone(A       )





def lowerCamelCase						(     )						->int:
    _SCREAMING_SNAKE_CASE									=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
    return image






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

    '''simple docstring'''


    @cached_property
    def 			snake_case_(		self       )		->   List[str]:
        return (
            DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""       )
            if is_vision_available()
            else None
        )


    @slow
    def 			snake_case_(		self       )		->   Any:
        _SCREAMING_SNAKE_CASE									=			DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""       ).to(
            A       )

        _SCREAMING_SNAKE_CASE									=			self.default_image_processor
        _SCREAMING_SNAKE_CASE									=			prepare_img()
        _SCREAMING_SNAKE_CASE									=			image_processor(images=A ,		return_tensors="""pt"""       ).to(A       )

        # forward pass
        with torch.no_grad():
            _SCREAMING_SNAKE_CASE									=			model(**A       )

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

        _SCREAMING_SNAKE_CASE									=			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 			snake_case_(		self       )		->   Union[str, Any]:
        _SCREAMING_SNAKE_CASE									=			DeiTModel.from_pretrained(
            """facebook/deit-base-distilled-patch16-224""" ,		torch_dtype=torch.floataa ,		device_map="""auto"""       )
        _SCREAMING_SNAKE_CASE									=			self.default_image_processor

        _SCREAMING_SNAKE_CASE									=			prepare_img()
        _SCREAMING_SNAKE_CASE									=			image_processor(images=A ,		return_tensors="""pt"""       )
        _SCREAMING_SNAKE_CASE									=			inputs.pixel_values.to(A       )

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










'''simple docstring'''

import importlib.metadata
import operator
import re
import sys
from typing import Optional

from packaging import version


lowercase_					= {
    """<""": operator.lt,
    """<=""": operator.le,
    """==""": operator.eq,
    """!=""": operator.ne,
    """>=""": operator.ge,
    """>""": operator.gt,
}





def lowerCamelCase						(     __lowerCamelCase :    Tuple					,       __lowerCamelCase :    Optional[Any]					,       __lowerCamelCase :    Dict					,       __lowerCamelCase :    List[Any]					,       __lowerCamelCase :    Optional[int]					,       __lowerCamelCase :    Optional[Any]    )						->Tuple:
    if got_ver is None or want_ver is None:
        raise ValueError(
            F'Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider'
            F' reinstalling {pkg}.'    )
    if not ops[op](version.parse(__lowerCamelCase    )					,       version.parse(__lowerCamelCase    )    ):
        raise ImportError(
            F'{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}'    )





def lowerCamelCase						(     __lowerCamelCase :    str					,       __lowerCamelCase :    Optional[str] = None    )						->None:
    _SCREAMING_SNAKE_CASE									=			F'\n{hint}' if hint is not None else """"""

    # non-versioned check
    if re.match(R"""^[\w_\-\d]+$"""					,       __lowerCamelCase    ):
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			requirement, None, None
    else:
        _SCREAMING_SNAKE_CASE									=			re.findall(R"""^([^!=<>\s]+)([\s!=<>]{1,2}.+)"""					,       __lowerCamelCase    )
        if not match:
            raise ValueError(
                """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but"""
                F' got {requirement}'    )
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			match[0]
        _SCREAMING_SNAKE_CASE									=			want_full.split(""","""    )  # there could be multiple requirements
        _SCREAMING_SNAKE_CASE									=			{}
        for w in want_range:
            _SCREAMING_SNAKE_CASE									=			re.findall(R"""^([\s!=<>]{1,2})(.+)"""					,       __lowerCamelCase    )
            if not match:
                raise ValueError(
                    """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,"""
                    F' but got {requirement}'    )
            _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			match[0]
            _SCREAMING_SNAKE_CASE									=			want_ver
            if op not in ops:
                raise ValueError(F'{requirement}: need one of {list(ops.keys()    )}, but got {op}'    )

    # special case
    if pkg == "python":
        _SCREAMING_SNAKE_CASE									=			""".""".join([str(__lowerCamelCase    ) for x in sys.version_info[:3]]    )
        for op, want_ver in wanted.items():
            _compare_versions(__lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase    )
        return

    # check if any version is installed
    try:
        _SCREAMING_SNAKE_CASE									=			importlib.metadata.version(__lowerCamelCase    )
    except importlib.metadata.PackageNotFoundError:
        raise importlib.metadata.PackageNotFoundError(
            F'The \'{requirement}\' distribution was not found and is required by this application. {hint}'    )

    # check that the right version is installed if version number or a range was provided
    if want_ver is not None:
        for op, want_ver in wanted.items():
            _compare_versions(__lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase    )






def lowerCamelCase						(     __lowerCamelCase :    Union[str, Any]    )						->str:
    _SCREAMING_SNAKE_CASE									=			"""Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main"""
    return require_version(__lowerCamelCase					,       __lowerCamelCase    )



