


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




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=13   ,    _lowerCamelCase=30   ,    _lowerCamelCase=2   ,    _lowerCamelCase=3   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=32   ,    _lowerCamelCase=2   ,    _lowerCamelCase=4   ,    _lowerCamelCase=37   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=10   ,    _lowerCamelCase=0.02   ,    _lowerCamelCase=3   ,    _lowerCamelCase=None   ,    _lowerCamelCase=2   ,    ):
       a		:List[Any]									=			parent
       a		:str									=			batch_size
       a		:List[str]									=			image_size
       a		:Union[str, Any]									=			patch_size
       a		:Any									=			num_channels
       a		:Optional[int]									=			is_training
       a		:List[Any]									=			use_labels
       a		:Optional[int]									=			hidden_size
       a		:Dict									=			num_hidden_layers
       a		:Union[str, Any]									=			num_attention_heads
       a		:List[Any]									=			intermediate_size
       a		:Tuple									=			hidden_act
       a		:Optional[Any]									=			hidden_dropout_prob
       a		:Dict									=			attention_probs_dropout_prob
       a		:List[Any]									=			type_sequence_label_size
       a		:int									=			initializer_range
       a		:List[Any]									=			scope
       a		:List[str]									=			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		:List[str]									=			num_patches + 2




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:int									=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

       a		:int									=			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       ):
       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 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:Tuple									=			TFDeiTModel(config=_lowerCamelCase       )
       a		:Dict									=			model(_lowerCamelCase       )
       self.parent.assertEqual(result.last_hidden_state.shape   ,    (self.batch_size, self.seq_length, self.hidden_size)       )




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

       a		:str									=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
       a		:str									=			model(_lowerCamelCase       )
       self.parent.assertEqual(result.reconstruction.shape   ,    (self.batch_size, 1, self.image_size, self.image_size)       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:Any									=			self.type_sequence_label_size
       a		:Optional[Any]									=			TFDeiTForImageClassification(_lowerCamelCase       )
       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		:Optional[Any]									=			1
       a		:Any									=			TFDeiTForImageClassification(_lowerCamelCase       )

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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:int									=			self.prepare_config_and_inputs()
       a				,							a				,							a		:Union[str, Any]									=			config_and_inputs
       a		:Union[str, Any]									=			{'''pixel_values''': pixel_values}
       return config, inputs_dict







@require_tf
class 						_snake_case    ( _snake_case		,       _snake_case		,       unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			(
     (
         TFDeiTModel,
         TFDeiTForImageClassification,
         TFDeiTForImageClassificationWithTeacher,
         TFDeiTForMaskedImageModeling,
     )
     if is_tf_available()
     else ()
 )
 SCREAMING_SNAKE_CASE__ =			(
     {
         'feature-extraction': TFDeiTModel,
         'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
     }
     if is_tf_available()
     else {}
 )

 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:str									=			TFDeiTModelTester(self       )
       a		:Dict									=			ConfigTester(self   ,    config_class=_lowerCamelCase   ,    has_text_modality=_lowerCamelCase   ,    hidden_size=37       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       self.config_tester.run_common_tests()




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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:Union[str, Any]									=			self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             a		:Optional[int]									=			model_class(_lowerCamelCase       )
             self.assertIsInstance(model.get_input_embeddings()   ,    (tf.keras.layers.Layer)       )
             a		:Dict									=			model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(_lowerCamelCase   ,    tf.keras.layers.Dense       )       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:List[str]									=			self.model_tester.prepare_config_and_inputs_for_common()

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

             a		:Dict									=			['''pixel_values''']
             self.assertListEqual(arg_names[:1]   ,    _lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[Any]									=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Union[str, Any]									=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Tuple									=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase=False       ):
       a		:int									=			super()._prepare_for_class(_lowerCamelCase   ,    _lowerCamelCase   ,    return_labels=_lowerCamelCase       )

       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       ):
       for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a		:Union[str, Any]									=			TFDeiTModel.from_pretrained(_lowerCamelCase       )
             self.assertIsNotNone(_lowerCamelCase       )







def 							__lowerCamelCase		(					):




      """simple docstring"""





      a		:Tuple									=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
      return image







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




 @cached_property
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       return (
           DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224'''       )
           if is_vision_available()
           else None
       )




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[Any]									=			TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''       )

       a		:str									=			self.default_image_processor
       a		:List[str]									=			prepare_img()
       a		:List[str]									=			image_processor(images=_lowerCamelCase   ,    return_tensors='''tf'''       )

       # forward pass
       a		:Union[str, Any]									=			model(**_lowerCamelCase       )

       # verify the logits
       a		:str									=			tf.TensorShape((1, 1000)       )
       self.assertEqual(outputs.logits.shape   ,    _lowerCamelCase       )

       a		:int									=			tf.constant([-1.0266, 0.1912, -1.2861]       )

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









from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case							:       List[Any]										=	logging.get_logger(__name__)

snake_case							:       Dict										=	{
    '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''',
    '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-vqa-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json'''
    ),
    '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''',
    '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-vcr-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json'''
    ),
    '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''',
    '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-nlvr2-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json'''
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}







class 						_snake_case    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			'visual_bert'




 def __init__(						self   ,    _lowerCamelCase=3_0522   ,    _lowerCamelCase=768   ,    _lowerCamelCase=512   ,    _lowerCamelCase=12   ,    _lowerCamelCase=12   ,    _lowerCamelCase=3072   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=512   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.02   ,    _lowerCamelCase=1e-12   ,    _lowerCamelCase=False   ,    _lowerCamelCase=True   ,    _lowerCamelCase=1   ,    _lowerCamelCase=0   ,    _lowerCamelCase=2   ,    **_lowerCamelCase   ,    ):
       super().__init__(pad_token_id=_lowerCamelCase   ,    bos_token_id=_lowerCamelCase   ,    eos_token_id=_lowerCamelCase   ,    **_lowerCamelCase       )

       a		:Any									=			vocab_size
       a		:str									=			max_position_embeddings
       a		:str									=			hidden_size
       a		:List[Any]									=			visual_embedding_dim
       a		:str									=			num_hidden_layers
       a		:Optional[int]									=			num_attention_heads
       a		:int									=			intermediate_size
       a		:int									=			hidden_act
       a		:Union[str, Any]									=			hidden_dropout_prob
       a		:List[Any]									=			attention_probs_dropout_prob
       a		:int									=			initializer_range
       a		:List[Any]									=			type_vocab_size
       a		:str									=			layer_norm_eps
       a		:Optional[int]									=			bypass_transformer
       a		:str									=			special_visual_initialize





