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





   '''simple docstring'''


   def __init__(					self     :							Any			,			lowerCAmelCase_     :							Dict			,			lowerCAmelCase_     :							Union[str, Any]=1_3			,			lowerCAmelCase_     :							int=3_0			,			lowerCAmelCase_     :							str=2			,			lowerCAmelCase_     :							Optional[int]=3			,			lowerCAmelCase_     :							str=True			,			lowerCAmelCase_     :							Dict=True			,			lowerCAmelCase_     :							Tuple=3_2			,			lowerCAmelCase_     :							Optional[Any]=5			,			lowerCAmelCase_     :							str=4			,			lowerCAmelCase_     :							List[Any]=3_7			,			lowerCAmelCase_     :							int="gelu"			,			lowerCAmelCase_     :							int=0.1			,			lowerCAmelCase_     :							Any=0.1			,			lowerCAmelCase_     :							List[Any]=1_0			,			lowerCAmelCase_     :							Dict=0.02			,			lowerCAmelCase_     :							Optional[int]=3			,			lowerCAmelCase_     :							List[str]=None			,			lowerCAmelCase_     :							int=2			,			):

      """simple docstring"""
      _A:  int					=       parent
      _A:  List[Any]					=       batch_size
      _A:  List[Any]					=       image_size
      _A:  Tuple					=       patch_size
      _A:  Union[str, Any]					=       num_channels
      _A:  Optional[int]					=       is_training
      _A:  Tuple					=       use_labels
      _A:  Tuple					=       hidden_size
      _A:  int					=       num_hidden_layers
      _A:  List[str]					=       num_attention_heads
      _A:  Dict					=       intermediate_size
      _A:  Any					=       hidden_act
      _A:  Tuple					=       hidden_dropout_prob
      _A:  Optional[int]					=       attention_probs_dropout_prob
      _A:  str					=       type_sequence_label_size
      _A:  List[Any]					=       initializer_range
      _A:  List[Any]					=       scope
      _A:  Any					=       encoder_stride

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


   def  __magic_name__				(					self     :							Dict     ):

      """simple docstring"""
      _A:  List[str]					=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

      _A:  Optional[int]					=       None
      if self.use_labels:
         _A:  Optional[int]					=       ids_tensor([self.batch_size]			,			self.type_sequence_label_size     )

      _A:  Union[str, Any]					=       self.get_config()

      return config, pixel_values, labels


   def  __magic_name__				(					self     :							Dict     ):

      """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=lowerCAmelCase_			,			initializer_range=self.initializer_range			,			encoder_stride=self.encoder_stride			,			)


   def  __magic_name__				(					self     :							Optional[int]			,			lowerCAmelCase_     :							List[Any]			,			lowerCAmelCase_     :							int			,			lowerCAmelCase_     :							Optional[int]     ):

      """simple docstring"""
      _A:  int					=       DeiTModel(config=lowerCAmelCase_     )
      model.to(lowerCAmelCase_     )
      model.eval()
      _A:  Optional[Any]					=       model(lowerCAmelCase_     )
      self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size)     )


   def  __magic_name__				(					self     :							str			,			lowerCAmelCase_     :							Union[str, Any]			,			lowerCAmelCase_     :							Union[str, Any]			,			lowerCAmelCase_     :							List[Any]     ):

      """simple docstring"""
      _A:  int					=       DeiTForMaskedImageModeling(config=lowerCAmelCase_     )
      model.to(lowerCAmelCase_     )
      model.eval()
      _A:  List[Any]					=       model(lowerCAmelCase_     )
      self.parent.assertEqual(
          result.reconstruction.shape			,			(self.batch_size, self.num_channels, self.image_size, self.image_size)     )

      # test greyscale images
      _A:  Union[str, Any]					=       1
      _A:  str					=       DeiTForMaskedImageModeling(lowerCAmelCase_     )
      model.to(lowerCAmelCase_     )
      model.eval()

      _A:  Tuple					=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
      _A:  Any					=       model(lowerCAmelCase_     )
      self.parent.assertEqual(result.reconstruction.shape			,			(self.batch_size, 1, self.image_size, self.image_size)     )


   def  __magic_name__				(					self     :							str			,			lowerCAmelCase_     :							int			,			lowerCAmelCase_     :							Tuple			,			lowerCAmelCase_     :							Any     ):

      """simple docstring"""
      _A:  Dict					=       self.type_sequence_label_size
      _A:  int					=       DeiTForImageClassification(lowerCAmelCase_     )
      model.to(lowerCAmelCase_     )
      model.eval()
      _A:  Optional[int]					=       model(lowerCAmelCase_			,			labels=lowerCAmelCase_     )
      self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.type_sequence_label_size)     )

      # test greyscale images
      _A:  Any					=       1
      _A:  Optional[int]					=       DeiTForImageClassification(lowerCAmelCase_     )
      model.to(lowerCAmelCase_     )
      model.eval()

      _A:  str					=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
      _A:  Optional[Any]					=       model(lowerCAmelCase_			,			labels=lowerCAmelCase_     )
      self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.type_sequence_label_size)     )


   def  __magic_name__				(					self     :							List[str]     ):

      """simple docstring"""
      _A:  List[str]					=       self.prepare_config_and_inputs()
      (
          (
          _A
      )			,  (
          _A
      )			,  (
          _A
      )			,  
      ):  List[Any]					=       config_and_inputs
      _A:  Dict					=       {'''pixel_values''': pixel_values}
      return config, inputs_dict






@require_torch
class UpperCAmelCase   (						SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase   ):





   '''simple docstring'''
   __UpperCamelCase		:    Optional[int] 				=    (
       (
           DeiTModel,
           DeiTForImageClassification,
           DeiTForImageClassificationWithTeacher,
           DeiTForMaskedImageModeling,
       )
       if is_torch_available()
       else ()
   )
   __UpperCamelCase		:    Optional[Any] 				=    (
       {
           '''feature-extraction''': DeiTModel,
           '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
       }
       if is_torch_available()
       else {}
   )

   __UpperCamelCase		:    Optional[int] 				=    False
   __UpperCamelCase		:    Dict 				=    False
   __UpperCamelCase		:    Union[str, Any] 				=    False


   def  __magic_name__				(					self     :							List[Any]     ):

      """simple docstring"""
      _A:  List[str]					=       DeiTModelTester(self     )
      _A:  List[str]					=       ConfigTester(self			,			config_class=lowerCAmelCase_			,			has_text_modality=lowerCAmelCase_			,			hidden_size=3_7     )


   def  __magic_name__				(					self     :							Union[str, Any]     ):

      """simple docstring"""
      self.config_tester.run_common_tests()


   @unittest.skip(reason='''DeiT does not use inputs_embeds'''     )
   def  __magic_name__				(					self     :							List[Any]     ):

      """simple docstring"""
      pass


   def  __magic_name__				(					self     :							List[Any]     ):

      """simple docstring"""
      _A			,  _A:  Optional[Any]					=       self.model_tester.prepare_config_and_inputs_for_common()

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


   def  __magic_name__				(					self     :							str     ):

      """simple docstring"""
      _A			,  _A:  Dict					=       self.model_tester.prepare_config_and_inputs_for_common()

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

         _A:  Any					=       ['''pixel_values''']
         self.assertListEqual(arg_names[:1]			,			lowerCAmelCase_     )


   def  __magic_name__				(					self     :							Dict     ):

      """simple docstring"""
      _A:  Tuple					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCAmelCase_     )


   def  __magic_name__				(					self     :							str     ):

      """simple docstring"""
      _A:  str					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase_     )


   def  __magic_name__				(					self     :							Union[str, Any]     ):

      """simple docstring"""
      _A:  Optional[int]					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_     )


   def  __magic_name__				(					self     :							Optional[int]			,			lowerCAmelCase_     :							Dict			,			lowerCAmelCase_     :							Optional[int]			,			lowerCAmelCase_     :							Optional[Any]=False     ):

      """simple docstring"""
      _A:  Optional[Any]					=       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  __magic_name__				(					self     :							Any     ):

      """simple docstring"""
      if not self.model_tester.is_training:
         return

      _A			,  _A:  str					=       self.model_tester.prepare_config_and_inputs_for_common()
      _A:  Dict					=       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:  int					=       model_class(lowerCAmelCase_     )
         model.to(lowerCAmelCase_     )
         model.train()
         _A:  Union[str, Any]					=       self._prepare_for_class(lowerCAmelCase_			,			lowerCAmelCase_			,			return_labels=lowerCAmelCase_     )
         _A:  List[Any]					=       model(**lowerCAmelCase_     ).loss
         loss.backward()


   def  __magic_name__				(					self     :							List[Any]     ):

      """simple docstring"""
      _A			,  _A:  Tuple					=       self.model_tester.prepare_config_and_inputs_for_common()
      if not self.model_tester.is_training:
         return

      _A:  List[str]					=       False
      _A:  str					=       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:  Tuple					=       model_class(lowerCAmelCase_     )
         model.gradient_checkpointing_enable()
         model.to(lowerCAmelCase_     )
         model.train()
         _A:  Union[str, Any]					=       self._prepare_for_class(lowerCAmelCase_			,			lowerCAmelCase_			,			return_labels=lowerCAmelCase_     )
         _A:  Tuple					=       model(**lowerCAmelCase_     ).loss
         loss.backward()


   def  __magic_name__				(					self     :							List[Any]     ):

      """simple docstring"""
      _A			,  _A:  str					=       self.model_tester.prepare_config_and_inputs_for_common()

      _A:  Optional[int]					=       [
          {'''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:  Tuple					=       problem_type['''title''']
               _A:  Optional[Any]					=       problem_type['''num_labels''']

               _A:  List[Any]					=       model_class(lowerCAmelCase_     )
               model.to(lowerCAmelCase_     )
               model.train()

               _A:  Any					=       self._prepare_for_class(lowerCAmelCase_			,			lowerCAmelCase_			,			return_labels=lowerCAmelCase_     )

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

               _A:  Optional[int]					=       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:  List[Any]					=       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  __magic_name__				(					self     :							Tuple     ):

      """simple docstring"""
      for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _A:  Any					=       DeiTModel.from_pretrained(lowerCAmelCase_     )
         self.assertIsNotNone(lowerCAmelCase_     )






def   lowerCamelCase__							(					)      ->   List[Any]:
   _A:  str					=       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
   return image






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





   '''simple docstring'''


   @cached_property
   def  __magic_name__				(					self     :							Optional[int]     ):

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


   @slow
   def  __magic_name__				(					self     :							Optional[Any]     ):

      """simple docstring"""
      _A:  int					=       DeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''     ).to(
          lowerCAmelCase_     )

      _A:  str					=       self.default_image_processor
      _A:  Optional[Any]					=       prepare_img()
      _A:  Optional[int]					=       image_processor(images=lowerCAmelCase_			,			return_tensors='''pt'''     ).to(lowerCAmelCase_     )

      # forward pass
      with torch.no_grad():
         _A:  Optional[int]					=       model(**lowerCAmelCase_     )

      # verify the logits
      _A:  Any					=       torch.Size((1, 1_0_0_0)     )
      self.assertEqual(outputs.logits.shape			,			lowerCAmelCase_     )

      _A:  Dict					=       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  __magic_name__				(					self     :							Optional[int]     ):

      """simple docstring"""
      _A:  List[Any]					=       DeiTModel.from_pretrained(
          '''facebook/deit-base-distilled-patch16-224'''			,			torch_dtype=torch.floataa			,			device_map='''auto'''     )
      _A:  Union[str, Any]					=       self.default_image_processor

      _A:  int					=       prepare_img()
      _A:  List[str]					=       image_processor(images=lowerCAmelCase_			,			return_tensors='''pt'''     )
      _A:  Any					=       inputs.pixel_values.to(lowerCAmelCase_     )

      # forward pass to make sure inference works in fp16
      with torch.no_grad():
         _A:  Tuple					=       model(lowerCAmelCase_     )





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_convbert import ConvBertTokenizer


UpperCAmelCase__		:     Optional[Any]     			=      logging.get_logger(__name__)

UpperCAmelCase__		:     Dict     			=      {'vocab_file': 'vocab.txt'}

UpperCAmelCase__		:     List[Any]     			=      {
    'vocab_file': {
        'YituTech/conv-bert-base': 'https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt',
        'YituTech/conv-bert-medium-small': (
            'https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt'
        ),
        'YituTech/conv-bert-small': 'https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt',
    }
}

UpperCAmelCase__		:     Union[str, Any]     			=      {
    'YituTech/conv-bert-base': 512,
    'YituTech/conv-bert-medium-small': 512,
    'YituTech/conv-bert-small': 512,
}


UpperCAmelCase__		:     Dict     			=      {
    'YituTech/conv-bert-base': {'do_lower_case': True},
    'YituTech/conv-bert-medium-small': {'do_lower_case': True},
    'YituTech/conv-bert-small': {'do_lower_case': True},
}






class UpperCAmelCase   (						SCREAMING_SNAKE_CASE__   ):





   '''simple docstring'''
   __UpperCamelCase		:    Any 				=    VOCAB_FILES_NAMES
   __UpperCamelCase		:    Tuple 				=    PRETRAINED_VOCAB_FILES_MAP
   __UpperCamelCase		:    str 				=    PRETRAINED_INIT_CONFIGURATION
   __UpperCamelCase		:    int 				=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __UpperCamelCase		:    int 				=    ConvBertTokenizer


   def __init__(					self     :							Tuple			,			lowerCAmelCase_     :							int=None			,			lowerCAmelCase_     :							List[Any]=None			,			lowerCAmelCase_     :							Optional[Any]=True			,			lowerCAmelCase_     :							List[Any]="[UNK]"			,			lowerCAmelCase_     :							Optional[Any]="[SEP]"			,			lowerCAmelCase_     :							Optional[Any]="[PAD]"			,			lowerCAmelCase_     :							List[Any]="[CLS]"			,			lowerCAmelCase_     :							Optional[int]="[MASK]"			,			lowerCAmelCase_     :							Any=True			,			lowerCAmelCase_     :							List[str]=None			,			**lowerCAmelCase_     :							List[Any]			,			):

      """simple docstring"""
      super().__init__(
          lowerCAmelCase_			,			tokenizer_file=lowerCAmelCase_			,			do_lower_case=lowerCAmelCase_			,			unk_token=lowerCAmelCase_			,			sep_token=lowerCAmelCase_			,			pad_token=lowerCAmelCase_			,			cls_token=lowerCAmelCase_			,			mask_token=lowerCAmelCase_			,			tokenize_chinese_chars=lowerCAmelCase_			,			strip_accents=lowerCAmelCase_			,			**lowerCAmelCase_			,			)

      _A:  List[Any]					=       json.loads(self.backend_tokenizer.normalizer.__getstate__()     )
      if (
          normalizer_state.get('''lowercase'''			,			lowerCAmelCase_     ) != do_lower_case
          or normalizer_state.get('''strip_accents'''			,			lowerCAmelCase_     ) != strip_accents
          or normalizer_state.get('''handle_chinese_chars'''			,			lowerCAmelCase_     ) != tokenize_chinese_chars
      ):
         _A:  List[str]					=       getattr(lowerCAmelCase_			,			normalizer_state.pop('''type'''     )     )
         _A:  List[Any]					=       do_lower_case
         _A:  Optional[Any]					=       strip_accents
         _A:  Union[str, Any]					=       tokenize_chinese_chars
         _A:  Optional[int]					=       normalizer_class(**lowerCAmelCase_     )

      _A:  Optional[Any]					=       do_lower_case


   def  __magic_name__				(					self     :							str			,			lowerCAmelCase_     :							Tuple			,			lowerCAmelCase_     :							Union[str, Any]=None     ):

      """simple docstring"""
      _A:  Dict					=       [self.cls_token_id] + token_ids_a + [self.sep_token_id]

      if token_ids_a:
         output += token_ids_a + [self.sep_token_id]

      return output


   def  __magic_name__				(					self     :							Union[str, Any]			,			lowerCAmelCase_     :							List[int]			,			lowerCAmelCase_     :							Optional[List[int]] = None     ):

      """simple docstring"""
      _A:  Any					=       [self.sep_token_id]
      _A:  int					=       [self.cls_token_id]
      if token_ids_a is None:
         return len(cls + token_ids_a + sep     ) * [0]
      return len(cls + token_ids_a + sep     ) * [0] + len(token_ids_a + sep     ) * [1]


   def  __magic_name__				(					self     :							int			,			lowerCAmelCase_     :							str			,			lowerCAmelCase_     :							Optional[str] = None     ):

      """simple docstring"""
      _A:  str					=       self._tokenizer.model.save(lowerCAmelCase_			,			name=lowerCAmelCase_     )
      return tuple(lowerCAmelCase_     )



