




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




    """simple docstring"""

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

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


    def   a (			self	:	Any  ) ->			Dict:
         lowerCAmelCase__            =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

         lowerCAmelCase__            =					self.get_config()

         return config, pixel_values, labels


    def   a (			self	:	Optional[int]  ) ->			List[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=SCREAMING_SNAKE_CASE__  ,      initializer_range=self.initializer_range  ,      encoder_stride=self.encoder_stride  ,      )


    def   a (			self	:	Optional[Any]  ,      SCREAMING_SNAKE_CASE__	:	int  ,      SCREAMING_SNAKE_CASE__	:	Tuple  ,      SCREAMING_SNAKE_CASE__	:	str  ) ->			Any:
         lowerCAmelCase__            =					TFDeiTModel(config=SCREAMING_SNAKE_CASE__  )
         lowerCAmelCase__            =					model(SCREAMING_SNAKE_CASE__  )
         self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)  )


    def   a (			self	:	str  ,      SCREAMING_SNAKE_CASE__	:	Optional[Any]  ,      SCREAMING_SNAKE_CASE__	:	Optional[Any]  ,      SCREAMING_SNAKE_CASE__	:	Any  ) ->			int:
         lowerCAmelCase__            =					TFDeiTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__  )
         lowerCAmelCase__            =					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
         lowerCAmelCase__            =					1
         lowerCAmelCase__            =					TFDeiTForMaskedImageModeling(SCREAMING_SNAKE_CASE__  )

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


    def   a (			self	:	List[Any]  ,      SCREAMING_SNAKE_CASE__	:	str  ,      SCREAMING_SNAKE_CASE__	:	Union[str, Any]  ,      SCREAMING_SNAKE_CASE__	:	int  ) ->			Union[str, Any]:
         lowerCAmelCase__            =					self.type_sequence_label_size
         lowerCAmelCase__            =					TFDeiTForImageClassification(SCREAMING_SNAKE_CASE__  )
         lowerCAmelCase__            =					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
         lowerCAmelCase__            =					1
         lowerCAmelCase__            =					TFDeiTForImageClassification(SCREAMING_SNAKE_CASE__  )

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


    def   a (			self	:	Optional[Any]  ) ->			Any:
         lowerCAmelCase__            =					self.prepare_config_and_inputs()
         lowerCAmelCase__      ,						lowerCAmelCase__      ,						lowerCAmelCase__            =					config_and_inputs
         lowerCAmelCase__            =					{"pixel_values": pixel_values}
         return config, inputs_dict






@require_tf
class 					__lowerCamelCase			(       UpperCamelCase__      ,							UpperCamelCase__      ,							unittest.TestCase       ):




    """simple docstring"""

    snake_case__   = (
        (
            TFDeiTModel,
            TFDeiTForImageClassification,
            TFDeiTForImageClassificationWithTeacher,
            TFDeiTForMaskedImageModeling,
        )
        if is_tf_available()
        else ()
    )
    snake_case__   = (
        {
            "feature-extraction": TFDeiTModel,
            "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
        }
        if is_tf_available()
        else {}
    )

    snake_case__   = False
    snake_case__   = False
    snake_case__   = False
    snake_case__   = False


    def   a (			self	:	Tuple  ) ->			Optional[int]:
         lowerCAmelCase__            =					TFDeiTModelTester(self  )
         lowerCAmelCase__            =					ConfigTester(self  ,      config_class=SCREAMING_SNAKE_CASE__  ,      has_text_modality=SCREAMING_SNAKE_CASE__  ,      hidden_size=37  )


    def   a (			self	:	Dict  ) ->			int:
         self.config_tester.run_common_tests()


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


    def   a (			self	:	Optional[int]  ) ->			Dict:
         lowerCAmelCase__      ,						lowerCAmelCase__            =					self.model_tester.prepare_config_and_inputs_for_common()

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


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

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

              lowerCAmelCase__            =					["pixel_values"]
              self.assertListEqual(arg_names[:1]  ,      SCREAMING_SNAKE_CASE__  )


    def   a (			self	:	Dict  ) ->			Optional[Any]:
         lowerCAmelCase__            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__  )


    def   a (			self	:	Any  ) ->			List[Any]:
         lowerCAmelCase__            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE__  )


    def   a (			self	:	str  ) ->			int:
         lowerCAmelCase__            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__  )


    def   a (			self	:	str  ,      SCREAMING_SNAKE_CASE__	:	Tuple  ,      SCREAMING_SNAKE_CASE__	:	List[Any]  ,      SCREAMING_SNAKE_CASE__	:	int=False  ) ->			Optional[int]:
         lowerCAmelCase__            =					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   a (			self	:	List[Any]  ) ->			Optional[Any]:
         for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowerCAmelCase__            =					TFDeiTModel.from_pretrained(SCREAMING_SNAKE_CASE__  )
              self.assertIsNotNone(SCREAMING_SNAKE_CASE__  )






def 				_A  (		):
     """simple docstring"""

     lowerCAmelCase__            =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
     return image






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




    """simple docstring"""

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


    @slow
    def   a (			self	:	Optional[Any]  ) ->			List[str]:
         lowerCAmelCase__            =					TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"  )

         lowerCAmelCase__            =					self.default_image_processor
         lowerCAmelCase__            =					prepare_img()
         lowerCAmelCase__            =					image_processor(images=SCREAMING_SNAKE_CASE__  ,      return_tensors="tf"  )

         # forward pass
         lowerCAmelCase__            =					model(**SCREAMING_SNAKE_CASE__  )

         # verify the logits
         lowerCAmelCase__            =					tf.TensorShape((1, 1_000)  )
         self.assertEqual(outputs.logits.shape  ,      SCREAMING_SNAKE_CASE__  )

         lowerCAmelCase__            =					tf.constant([-1.0_266, 0.1_912, -1.2_861]  )

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




from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)





