

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__  :



       '''simple docstring'''







       def __init__(							self ,   lowercase__ ,   lowercase__=1_3 ,   lowercase__=3_0 ,   lowercase__=2 ,   lowercase__=3 ,   lowercase__=True ,   lowercase__=True ,   lowercase__=3_2 ,   lowercase__=5 ,   lowercase__=4 ,   lowercase__=3_7 ,   lowercase__="gelu" ,   lowercase__=0.1 ,   lowercase__=0.1 ,   lowercase__=1_0 ,   lowercase__=0.02 ,   lowercase__=3 ,   lowercase__=None ,   lowercase__=2 ,   ):







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







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







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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__     ):







             '''simple docstring'''



             __A    =DeiTForMaskedImageModeling(config=lowercase__     )
             model.to(lowercase__     )
             model.eval()
             __A    =model(lowercase__     )
             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(lowercase__     )
             model.to(lowercase__     )
             model.eval()

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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__     ):







             '''simple docstring'''



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

             # test greyscale images
             __A    =1
             __A    =DeiTForImageClassification(lowercase__     )
             model.to(lowercase__     )
             model.eval()

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


       def     __UpperCamelCase     (							self     ):







             '''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        lowerCAmelCase__  (					__magic_name__				,							__magic_name__				,							unittest.TestCase			):



       '''simple docstring'''







       lowercase_						=    (
           (
               DeiTModel,
               DeiTForImageClassification,
               DeiTForImageClassificationWithTeacher,
               DeiTForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       lowercase_						=    (
           {
               """feature-extraction""": DeiTModel,
               """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
           }
           if is_torch_available()
           else {}
       )

       lowercase_						=    False
       lowercase_						=    False
       lowercase_						=    False

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =DeiTModelTester(self     )
             __A    =ConfigTester(self ,   config_class=lowercase__ ,   has_text_modality=lowercase__ ,   hidden_size=3_7     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             self.config_tester.run_common_tests()

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







             '''simple docstring'''



             pass

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    ,		__A    =self.model_tester.prepare_config_and_inputs_for_common()

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

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    ,		__A    =self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   __A    =model_class(lowercase__     )
                   __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] ,   lowercase__     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*lowercase__     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*lowercase__     )

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__=False     ):







             '''simple docstring'''



             __A    =super()._prepare_for_class(lowercase__ ,   lowercase__ ,   return_labels=lowercase__     )

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

             return inputs_dict

       def     __UpperCamelCase     (							self     ):







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

       def     __UpperCamelCase     (							self     ):







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

       def     __UpperCamelCase     (							self     ):







             '''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(lowercase__     ),
                           *get_values(lowercase__     ),
                       ]
                       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(lowercase__     )
                               model.to(lowercase__     )
                               model.train()

                               __A    =self._prepare_for_class(lowercase__ ,   lowercase__ ,   return_labels=lowercase__     )

                               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=lowercase__     ) as warning_list:
                                     __A    =model(**lowercase__     ).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     ):







             '''simple docstring'''



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





def      A__					(			)					->str:
      __A    =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
      return image





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



       '''simple docstring'''







       @cached_property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

       @slow
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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

             __A    =self.default_image_processor
             __A    =prepare_img()
             __A    =image_processor(images=lowercase__ ,   return_tensors='''pt'''     ).to(lowercase__     )

             # forward pass
             with torch.no_grad():
                   __A    =model(**lowercase__     )

             # verify the logits
             __A    =torch.Size((1, 1_0_0_0)     )
             self.assertEqual(outputs.logits.shape ,   lowercase__     )

             __A    =torch.tensor([-1.0266, 0.1912, -1.2861]     ).to(lowercase__     )

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


       @slow
       @require_accelerate
       @require_torch_gpu
       def     __UpperCamelCase     (							self     ):







             '''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=lowercase__ ,   return_tensors='''pt'''     )
             __A    =inputs.pixel_values.to(lowercase__     )

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





import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class        lowerCAmelCase__  (					__magic_name__			):



       '''simple docstring'''







       lowercase_						=    ["""image_processor""", """tokenizer"""]
       lowercase_						=    """LayoutLMv2ImageProcessor"""
       lowercase_						=    ("""LayoutXLMTokenizer""", """LayoutXLMTokenizerFast""")

       def __init__(							self ,   lowercase__=None ,   lowercase__=None ,   **lowercase__     ):







             '''simple docstring'''



             if "feature_extractor" in kwargs:
                   warnings.warn(
                       '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
                       ''' instead.''' ,   lowercase__ ,   )
                   __A    =kwargs.pop('''feature_extractor'''     )

             __A    =image_processor if image_processor is not None else feature_extractor
             if image_processor is None:
                   raise ValueError('''You need to specify an `image_processor`.'''     )
             if tokenizer is None:
                   raise ValueError('''You need to specify a `tokenizer`.'''     )

             super().__init__(lowercase__ ,   lowercase__     )

       def __call__(							self ,   lowercase__ ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = True ,   lowercase__ = False ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = 0 ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = False ,   lowercase__ = False ,   lowercase__ = False ,   lowercase__ = False ,   lowercase__ = True ,   lowercase__ = None ,   **lowercase__ ,   ):







             '''simple docstring'''



             if self.image_processor.apply_ocr and (boxes is not None):
                   raise ValueError(
                       '''You cannot provide bounding boxes '''
                       '''if you initialized the image processor with apply_ocr set to True.'''     )

             if self.image_processor.apply_ocr and (word_labels is not None):
                   raise ValueError(
                       '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'''     )

             if return_overflowing_tokens is True and return_offsets_mapping is False:
                   raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.'''     )

             # first, apply the image processor
             __A    =self.image_processor(images=lowercase__ ,   return_tensors=lowercase__     )

             # second, apply the tokenizer
             if text is not None and self.image_processor.apply_ocr and text_pair is None:
                   if isinstance(lowercase__ ,   lowercase__     ):
                         __A    =[text]  # add batch dimension (as the image processor always adds a batch dimension)
                   __A    =features['''words''']

             __A    =self.tokenizer(
                 text=text if text is not None else features['''words'''] ,   text_pair=text_pair if text_pair is not None else None ,   boxes=boxes if boxes is not None else features['''boxes'''] ,   word_labels=lowercase__ ,   add_special_tokens=lowercase__ ,   padding=lowercase__ ,   truncation=lowercase__ ,   max_length=lowercase__ ,   stride=lowercase__ ,   pad_to_multiple_of=lowercase__ ,   return_token_type_ids=lowercase__ ,   return_attention_mask=lowercase__ ,   return_overflowing_tokens=lowercase__ ,   return_special_tokens_mask=lowercase__ ,   return_offsets_mapping=lowercase__ ,   return_length=lowercase__ ,   verbose=lowercase__ ,   return_tensors=lowercase__ ,   **lowercase__ ,   )

             # add pixel values
             __A    =features.pop('''pixel_values'''     )
             if return_overflowing_tokens is True:
                   __A    =self.get_overflowing_images(lowercase__ ,   encoded_inputs['''overflow_to_sample_mapping''']     )
             __A    =images

             return encoded_inputs

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__     ):







             '''simple docstring'''



             __A    =[]
             for sample_idx in overflow_to_sample_mapping:
                   images_with_overflow.append(images[sample_idx]     )

             if len(lowercase__     ) != len(lowercase__     ):
                   raise ValueError(
                       '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
                       f''' {len(lowercase__     )} and {len(lowercase__     )}'''     )

             return images_with_overflow

       def     __UpperCamelCase     (							self ,   *lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



             return self.tokenizer.batch_decode(*lowercase__ ,   **lowercase__     )

       def     __UpperCamelCase     (							self ,   *lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



             return self.tokenizer.decode(*lowercase__ ,   **lowercase__     )

       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             return ["input_ids", "bbox", "attention_mask", "image"]

       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             warnings.warn(
                 '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' ,   lowercase__ ,   )
             return self.image_processor_class


       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             warnings.warn(
                 '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' ,   lowercase__ ,   )
             return self.image_processor


