


from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import (
       TFDeiTForImageClassification,
       TFDeiTForImageClassificationWithTeacher,
       TFDeiTForMaskedImageModeling,
       TFDeiTModel,
   )
   from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import DeiTImageProcessor
class  SCREAMING_SNAKE_CASE__  :
      def __init__(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Tuple=1_3     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=3_0     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=2     ,							SCREAMING_SNAKE_CASE__				:       List[Any]=3     ,							SCREAMING_SNAKE_CASE__				:       Dict=True     ,							SCREAMING_SNAKE_CASE__				:       Any=True     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=3_2     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=2     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=4     ,							SCREAMING_SNAKE_CASE__				:       Any=3_7     ,							SCREAMING_SNAKE_CASE__				:       Tuple="gelu"     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=0.1     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=0.1     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=1_0     ,							SCREAMING_SNAKE_CASE__				:       str=0.02     ,							SCREAMING_SNAKE_CASE__				:       str=3     ,							SCREAMING_SNAKE_CASE__				:       Any=None     ,							SCREAMING_SNAKE_CASE__				:       Tuple=2     ,							)		->			List[str]:
             a_					:		List[Any]							=  parent
             a_					:		int							=  batch_size
             a_					:		Optional[int]							=  image_size
             a_					:		Union[str, Any]							=  patch_size
             a_					:		Optional[int]							=  num_channels
             a_					:		Tuple							=  is_training
             a_					:		Dict							=  use_labels
             a_					:		List[str]							=  hidden_size
             a_					:		Dict							=  num_hidden_layers
             a_					:		List[Any]							=  num_attention_heads
             a_					:		int							=  intermediate_size
             a_					:		Optional[Any]							=  hidden_act
             a_					:		Tuple							=  hidden_dropout_prob
             a_					:		Optional[Any]							=  attention_probs_dropout_prob
             a_					:		Tuple							=  type_sequence_label_size
             a_					:		List[Any]							=  initializer_range
             a_					:		Dict							=  scope
             a_					:		Optional[Any]							=  encoder_stride

             # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
             a_					:		Optional[int]							=  (image_size // patch_size) ** 2
             a_					:		Dict							=  num_patches + 2
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			int:
             a_					:		Union[str, Any]							=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

             a_					:		Optional[Any]							=  None
             if self.use_labels:
                    a_					:		List[str]							=  ids_tensor([self.batch_size]     ,							self.type_sequence_label_size     )

             a_					:		Any							=  self.get_config()

             return config, pixel_values, labels
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			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=SCREAMING_SNAKE_CASE__     ,							initializer_range=self.initializer_range     ,							encoder_stride=self.encoder_stride     ,							)
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			List[Any]:
             a_					:		Optional[int]							=  TFDeiTModel(config=SCREAMING_SNAKE_CASE__     )
             a_					:		int							=  model(SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Any     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Any     )		->			List[str]:
             a_					:		str							=  TFDeiTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__     )
             a_					:		List[Any]							=  model(SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(
                 result.reconstruction.shape     ,							(self.batch_size, self.num_channels, self.image_size, self.image_size)     )

             # test greyscale images
             a_					:		Optional[Any]							=  1
             a_					:		str							=  TFDeiTForMaskedImageModeling(SCREAMING_SNAKE_CASE__     )

             a_					:		Optional[int]							=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
             a_					:		List[Any]							=  model(SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.reconstruction.shape     ,							(self.batch_size, 1, self.image_size, self.image_size)     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Tuple     )		->			Dict:
             a_					:		Dict							=  self.type_sequence_label_size
             a_					:		Optional[int]							=  TFDeiTForImageClassification(SCREAMING_SNAKE_CASE__     )
             a_					:		Optional[int]							=  model(SCREAMING_SNAKE_CASE__     ,							labels=SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.type_sequence_label_size)     )

             # test greyscale images
             a_					:		Any							=  1
             a_					:		List[Any]							=  TFDeiTForImageClassification(SCREAMING_SNAKE_CASE__     )

             a_					:		List[str]							=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
             a_					:		Optional[int]							=  model(SCREAMING_SNAKE_CASE__     ,							labels=SCREAMING_SNAKE_CASE__     )
             self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.type_sequence_label_size)     )

      def        SCREAMING_SNAKE_CASE							(       self				:       List[str]     )		->			Union[str, Any]:
             a_					:		List[str]							=  self.prepare_config_and_inputs()
             a_							,		a_							,		a_					:		Tuple							=  config_and_inputs
             a_					:		Union[str, Any]							=  {'pixel_values': pixel_values}
             return config, inputs_dict
@require_tf
class  SCREAMING_SNAKE_CASE__  (				lowercase__						,    lowercase__						,    unittest.TestCase			):
      snake_case__				:     Tuple        =						(
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      snake_case__				:     str        =						(
          {
              '''feature-extraction''': TFDeiTModel,
              '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      snake_case__				:     List[Any]        =						False
      snake_case__				:     int        =						False
      snake_case__				:     Dict        =						False
      snake_case__				:     List[str]        =						False
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Union[str, Any]:
             a_					:		str							=  TFDeiTModelTester(self     )
             a_					:		List[str]							=  ConfigTester(self     ,							config_class=SCREAMING_SNAKE_CASE__     ,							has_text_modality=SCREAMING_SNAKE_CASE__     ,							hidden_size=3_7     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			Any:
             self.config_tester.run_common_tests()
      @unittest.skip(reason='DeiT does not use inputs_embeds'     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			Any:
             pass
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			int:
             a_							,		a_					:		Optional[int]							=  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    a_					:		List[str]							=  model_class(SCREAMING_SNAKE_CASE__     )
                    self.assertIsInstance(model.get_input_embeddings()     ,							(tf.keras.layers.Layer)     )
                    a_					:		Optional[Any]							=  model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE__     ,							tf.keras.layers.Dense     )     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     )		->			List[Any]:
             a_							,		a_					:		List[Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    a_					:		Optional[int]							=  model_class(SCREAMING_SNAKE_CASE__     )
                    a_					:		Union[str, Any]							=  inspect.signature(model.call     )
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    a_					:		Any							=  [*signature.parameters.keys()]

                    a_					:		str							=  ['pixel_values']
                    self.assertListEqual(arg_names[:1]     ,							SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Any:
             a_					:		List[Any]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     )		->			List[Any]:
             a_					:		str							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Tuple:
             a_					:		Optional[int]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       int=False     )		->			List[str]:
             a_					:		Tuple							=  super()._prepare_for_class(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     ,							return_labels=SCREAMING_SNAKE_CASE__     )

             if return_labels:
                    if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call     ).parameters:
                           del inputs_dict["labels"]

             return inputs_dict

      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       int     )		->			int:
             for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_					:		int							=  TFDeiTModel.from_pretrained(SCREAMING_SNAKE_CASE__     )
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )







def        SCREAMING_SNAKE_CASE_						(			)	->		str:

       """simple docstring"""

       a_					:		Tuple							=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
       return image
@require_tf
@require_vision
class  SCREAMING_SNAKE_CASE__  (				unittest.TestCase			):
      @cached_property
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			int:
             return (
                 DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'     )
                 if is_vision_available()
                 else None
             )

      @slow
      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     )		->			Any:
             a_					:		Dict							=  TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'     )

             a_					:		List[Any]							=  self.default_image_processor
             a_					:		str							=  prepare_img()
             a_					:		Tuple							=  image_processor(images=SCREAMING_SNAKE_CASE__     ,							return_tensors='tf'     )

             # forward pass
             a_					:		List[Any]							=  model(**SCREAMING_SNAKE_CASE__     )

             # verify the logits
             a_					:		Tuple							=  tf.TensorShape((1, 1_0_0_0)     )
             self.assertEqual(outputs.logits.shape     ,							SCREAMING_SNAKE_CASE__     )

             a_					:		Tuple							=  tf.constant([-1.0266, 0.1912, -1.2861]     )

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










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								=						logging.get_logger(__name__)

__UpperCAmelCase								=						{'vocab_file': 'vocab.txt'}

__UpperCAmelCase								=						{
    '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								=						{
    'YituTech/conv-bert-base': 512,
    'YituTech/conv-bert-medium-small': 512,
    'YituTech/conv-bert-small': 512,
}


__UpperCAmelCase								=						{
    '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   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		_snake_case    :    Optional[int]       						=  VOCAB_FILES_NAMES
		_snake_case    :    int       						=  PRETRAINED_VOCAB_FILES_MAP
		_snake_case    :    Dict       						=  PRETRAINED_INIT_CONFIGURATION
		_snake_case    :    List[str]       						=  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		_snake_case    :    Any       						=  ConvBertTokenizer



		def __init__(							self				,		_UpperCamelCase=None				,		_UpperCamelCase=None				,		_UpperCamelCase=True				,		_UpperCamelCase="[UNK]"				,		_UpperCamelCase="[SEP]"				,		_UpperCamelCase="[PAD]"				,		_UpperCamelCase="[CLS]"				,		_UpperCamelCase="[MASK]"				,		_UpperCamelCase=True				,		_UpperCamelCase=None				,		**_UpperCamelCase				,		)	->							Dict:
							super().__init__(
							    _UpperCamelCase				,		tokenizer_file=_UpperCamelCase				,		do_lower_case=_UpperCamelCase				,		unk_token=_UpperCamelCase				,		sep_token=_UpperCamelCase				,		pad_token=_UpperCamelCase				,		cls_token=_UpperCamelCase				,		mask_token=_UpperCamelCase				,		tokenize_chinese_chars=_UpperCamelCase				,		strip_accents=_UpperCamelCase				,		**_UpperCamelCase				,		)

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

							UpperCAmelCase_    :      Any            =						do_lower_case



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=None   )	->							List[str]:
							UpperCAmelCase_    :      int            =						[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 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = None   )	->							List[int]:
							UpperCAmelCase_    :      Union[str, Any]            =						[self.sep_token_id]
							UpperCAmelCase_    :      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 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = None   )	->							Tuple[str]:
							UpperCAmelCase_    :      Any            =						self._tokenizer.model.save(_UpperCamelCase				,		name=_UpperCamelCase   )
							return tuple(_UpperCamelCase   )






