



"""simple docstring"""




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



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

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



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

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

           _UpperCAmelCase      :     Optional[int]        =   self.get_config()

           return config, pixel_values, labels



      def 						snake_case_ (					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=A					,     initializer_range=self.initializer_range					,     encoder_stride=self.encoder_stride					,     )



      def 						snake_case_ (					self					:     Any					,     A					:     Union[str, Any]					,     A					:     Union[str, Any]					,     A					:     List[str]						):
           _UpperCAmelCase      :     Dict        =   TFDeiTModel(config=A						)
           _UpperCAmelCase      :     Union[str, Any]        =   model(A						)
           self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)



      def 						snake_case_ (					self					:     Union[str, Any]					,     A					:     int					,     A					:     Dict					,     A					:     Union[str, Any]						):
           _UpperCAmelCase      :     Dict        =   TFDeiTForMaskedImageModeling(config=A						)
           _UpperCAmelCase      :     Tuple        =   model(A						)
           self.parent.assertEqual(
               result.reconstruction.shape					,     (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

           # test greyscale images
           _UpperCAmelCase      :     List[str]        =   1
           _UpperCAmelCase      :     str        =   TFDeiTForMaskedImageModeling(A						)

           _UpperCAmelCase      :     Union[str, Any]        =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
           _UpperCAmelCase      :     List[str]        =   model(A						)
           self.parent.assertEqual(result.reconstruction.shape					,     (self.batch_size, 1, self.image_size, self.image_size)						)



      def 						snake_case_ (					self					:     int					,     A					:     List[str]					,     A					:     Optional[int]					,     A					:     Dict						):
           _UpperCAmelCase      :     List[Any]        =   self.type_sequence_label_size
           _UpperCAmelCase      :     Tuple        =   TFDeiTForImageClassification(A						)
           _UpperCAmelCase      :     Optional[int]        =   model(A					,     labels=A						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.type_sequence_label_size)						)

           # test greyscale images
           _UpperCAmelCase      :     Union[str, Any]        =   1
           _UpperCAmelCase      :     Dict        =   TFDeiTForImageClassification(A						)

           _UpperCAmelCase      :     List[str]        =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
           _UpperCAmelCase      :     List[Any]        =   model(A					,     labels=A						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.type_sequence_label_size)						)



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







@require_tf
class      UpperCAmelCase_  (    _UpperCamelCase	,      _UpperCamelCase	,      unittest.TestCase						):
      __SCREAMING_SNAKE_CASE						:       List[Any]								=			(
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      __SCREAMING_SNAKE_CASE						:       Tuple								=			(
          {
              'feature-extraction': TFDeiTModel,
              'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      __SCREAMING_SNAKE_CASE						:       Union[str, Any]								=			False
      __SCREAMING_SNAKE_CASE						:       Tuple								=			False
      __SCREAMING_SNAKE_CASE						:       List[Any]								=			False
      __SCREAMING_SNAKE_CASE						:       Dict								=			False



      def 						snake_case_ (					self					:     List[Any]						):
           _UpperCAmelCase      :     str        =   TFDeiTModelTester(self						)
           _UpperCAmelCase      :     Optional[Any]        =   ConfigTester(self					,     config_class=A					,     has_text_modality=A					,     hidden_size=3_7						)



      def 						snake_case_ (					self					:     Union[str, Any]						):
           self.config_tester.run_common_tests()



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



      def 						snake_case_ (					self					:     str						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     str        =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                _UpperCAmelCase      :     Dict        =   model_class(A						)
                self.assertIsInstance(model.get_input_embeddings()					,     (tf.keras.layers.Layer)						)
                _UpperCAmelCase      :     Tuple        =   model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(A					,     tf.keras.layers.Dense						)						)



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     int        =   self.model_tester.prepare_config_and_inputs_for_common()

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

                _UpperCAmelCase      :     List[Any]        =   ["pixel_values"]
                self.assertListEqual(arg_names[:1]					,     A						)



      def 						snake_case_ (					self					:     Union[str, Any]						):
           _UpperCAmelCase      :     Union[str, Any]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A						)



      def 						snake_case_ (					self					:     Union[str, Any]						):
           _UpperCAmelCase      :     Dict        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*A						)



      def 						snake_case_ (					self					:     str						):
           _UpperCAmelCase      :     Tuple        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*A						)



      def 						snake_case_ (					self					:     List[str]					,     A					:     Any					,     A					:     Union[str, Any]					,     A					:     Optional[Any]=False						):
           _UpperCAmelCase      :     Union[str, Any]        =   super()._prepare_for_class(A					,     A					,     return_labels=A						)

           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 						snake_case_ (					self					:     Optional[Any]						):
           for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _UpperCAmelCase      :     str        =   TFDeiTModel.from_pretrained(A						)
                self.assertIsNotNone(A						)







def 						__snake_case						(				)  ->       Union[str, Any]:





     '''simple docstring'''

     _UpperCAmelCase      :     Union[str, Any]        =   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
     return image






@require_tf
@require_vision
class      UpperCAmelCase_  (    unittest.TestCase						):



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



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

           _UpperCAmelCase      :     str        =   self.default_image_processor
           _UpperCAmelCase      :     str        =   prepare_img()
           _UpperCAmelCase      :     int        =   image_processor(images=A					,     return_tensors="tf"						)

           # forward pass
           _UpperCAmelCase      :     str        =   model(**A						)

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

           _UpperCAmelCase      :     List[str]        =   tf.constant([-1.0_266, 0.1_912, -1.2_861]						)

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





"""simple docstring"""




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_electra import ElectraTokenizer


_lowerCAmelCase    : List[str]							=							{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

_lowerCAmelCase    : int							=							{
    "vocab_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt"
        ),
        "google/electra-base-generator": "https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt",
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-generator": (
            "https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json"
        ),
    },
}

_lowerCAmelCase    : List[Any]							=							{
    "google/electra-small-generator": 5_12,
    "google/electra-base-generator": 5_12,
    "google/electra-large-generator": 5_12,
    "google/electra-small-discriminator": 5_12,
    "google/electra-base-discriminator": 5_12,
    "google/electra-large-discriminator": 5_12,
}

_lowerCAmelCase    : Optional[Any]							=							{
    "google/electra-small-generator": {"do_lower_case": True},
    "google/electra-base-generator": {"do_lower_case": True},
    "google/electra-large-generator": {"do_lower_case": True},
    "google/electra-small-discriminator": {"do_lower_case": True},
    "google/electra-base-discriminator": {"do_lower_case": True},
    "google/electra-large-discriminator": {"do_lower_case": True},
}






class      UpperCAmelCase_  (    _UpperCamelCase						):
      __SCREAMING_SNAKE_CASE						:       Optional[Any]								=			VOCAB_FILES_NAMES
      __SCREAMING_SNAKE_CASE						:       Any								=			PRETRAINED_VOCAB_FILES_MAP
      __SCREAMING_SNAKE_CASE						:       List[str]								=			PRETRAINED_INIT_CONFIGURATION
      __SCREAMING_SNAKE_CASE						:       List[str]								=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __SCREAMING_SNAKE_CASE						:       Tuple								=			ElectraTokenizer



      def __init__(					self					:     Dict					,     A					:     Dict=None					,     A					:     Optional[int]=None					,     A					:     Dict=True					,     A					:     Optional[Any]="[UNK]"					,     A					:     Any="[SEP]"					,     A					:     str="[PAD]"					,     A					:     Tuple="[CLS]"					,     A					:     Optional[Any]="[MASK]"					,     A					:     Any=True					,     A					:     Tuple=None					,     **A					:     Any					,     ):
           super().__init__(
               A					,     tokenizer_file=A					,     do_lower_case=A					,     unk_token=A					,     sep_token=A					,     pad_token=A					,     cls_token=A					,     mask_token=A					,     tokenize_chinese_chars=A					,     strip_accents=A					,     **A					,     )

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

           _UpperCAmelCase      :     int        =   do_lower_case



      def 						snake_case_ (					self					:     Tuple					,     A					:     str					,     A					:     int=None						):
           _UpperCAmelCase      :     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 						snake_case_ (					self					:     Any					,     A					:     List[int]					,     A					:     Optional[List[int]] = None						):
           _UpperCAmelCase      :     Any        =   [self.sep_token_id]
           _UpperCAmelCase      :     Dict        =   [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 						snake_case_ (					self					:     Any					,     A					:     str					,     A					:     Optional[str] = None						):
           _UpperCAmelCase      :     List[Any]        =   self._tokenizer.model.save(A					,     name=A						)
           return tuple(A						)
