

from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
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 TFCvtForImageClassification, TFCvtModel
  from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor







class        a__       (   UpperCAmelCase       ):







  """simple docstring"""




  def  _lowercase      (			self    : Union[str, Any]		)					->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   self.config_class(**self.inputs_dict		)
        self.parent.assertTrue(hasattr(UpperCAmelCase__				,				"""embed_dim"""		)		)
        self.parent.assertTrue(hasattr(UpperCAmelCase__				,				"""num_heads"""		)		)







class        a__       :







  """simple docstring"""




  def __init__(			self    : int				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Any=1_3				,				UpperCAmelCase__    : Optional[Any]=6_4				,				UpperCAmelCase__    : str=3				,				UpperCAmelCase__    : Tuple=[1_6, 4_8, 9_6]				,				UpperCAmelCase__    : int=[1, 3, 6]				,				UpperCAmelCase__    : int=[1, 2, 1_0]				,				UpperCAmelCase__    : Any=[7, 3, 3]				,				UpperCAmelCase__    : Optional[Any]=[4, 2, 2]				,				UpperCAmelCase__    : str=[2, 1, 1]				,				UpperCAmelCase__    : List[str]=[2, 2, 2]				,				UpperCAmelCase__    : Tuple=[False, False, True]				,				UpperCAmelCase__    : List[str]=[0.0, 0.0, 0.0]				,				UpperCAmelCase__    : Tuple=0.02				,				UpperCAmelCase__    : int=1e-12				,				UpperCAmelCase__    : Dict=True				,				UpperCAmelCase__    : Union[str, Any]=True				,				UpperCAmelCase__    : Optional[Any]=2				,				)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      str       =   parent
        SCREAMING_SNAKE_CASE :      List[Any]       =   batch_size
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   image_size
        SCREAMING_SNAKE_CASE :      List[str]       =   patch_sizes
        SCREAMING_SNAKE_CASE :      int       =   patch_stride
        SCREAMING_SNAKE_CASE :      int       =   patch_padding
        SCREAMING_SNAKE_CASE :      List[Any]       =   is_training
        SCREAMING_SNAKE_CASE :      List[Any]       =   use_labels
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   num_labels
        SCREAMING_SNAKE_CASE :      Dict       =   num_channels
        SCREAMING_SNAKE_CASE :      Tuple       =   embed_dim
        SCREAMING_SNAKE_CASE :      List[Any]       =   num_heads
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   stride_kv
        SCREAMING_SNAKE_CASE :      List[Any]       =   depth
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   cls_token
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   attention_drop_rate
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   initializer_range
        SCREAMING_SNAKE_CASE :      str       =   layer_norm_eps


  def  _lowercase      (			self    : List[str]		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

        SCREAMING_SNAKE_CASE :      Optional[int]       =   None
        if self.use_labels:
              # create a random int32 tensor of given shape
              SCREAMING_SNAKE_CASE :      List[Any]       =   ids_tensor([self.batch_size]				,				self.num_labels		)

        SCREAMING_SNAKE_CASE :      Dict       =   self.get_config()
        return config, pixel_values, labels


  def  _lowercase      (			self    : str		)					->int:
        """simple docstring"""







        return CvtConfig(
            image_size=self.image_size				,				num_labels=self.num_labels				,				num_channels=self.num_channels				,				embed_dim=self.embed_dim				,				num_heads=self.num_heads				,				patch_sizes=self.patch_sizes				,				patch_padding=self.patch_padding				,				patch_stride=self.patch_stride				,				stride_kv=self.stride_kv				,				depth=self.depth				,				cls_token=self.cls_token				,				attention_drop_rate=self.attention_drop_rate				,				initializer_range=self.initializer_range				,				)


  def  _lowercase      (			self    : str				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : int		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   TFCvtModel(config=UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      Optional[int]       =   model(UpperCAmelCase__				,				training=UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      List[Any]       =   (self.image_size, self.image_size)
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[Any]       =   image_size[0], image_size[1]
        for i in range(len(self.depth		)		):
              SCREAMING_SNAKE_CASE :      Tuple       =   floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
              SCREAMING_SNAKE_CASE :      Any       =   floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
        self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.embed_dim[-1], height, width)		)


  def  _lowercase      (			self    : List[Any]				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : Union[str, Any]		)					->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   self.num_labels
        SCREAMING_SNAKE_CASE :      str       =   TFCvtForImageClassification(UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      str       =   model(UpperCAmelCase__				,				labels=UpperCAmelCase__				,				training=UpperCAmelCase__		)
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)		)




  def  _lowercase      (			self    : Optional[Any]		)					->List[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   self.prepare_config_and_inputs()
        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[str]       =   config_and_inputs
        SCREAMING_SNAKE_CASE :      str       =   {"""pixel_values""": pixel_values}
        return config, inputs_dict







@require_tf
class        a__       (   UpperCAmelCase	,			UpperCAmelCase	,			unittest.TestCase       ):







  """simple docstring"""




  UpperCAmelCase__      : Any			       =(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
  UpperCAmelCase__      : Any			       =(
      {"""feature-extraction""": TFCvtModel, """image-classification""": TFCvtForImageClassification}
      if is_tf_available()
      else {}
  )
  UpperCAmelCase__      : Tuple			       =False
  UpperCAmelCase__      : str			       =False
  UpperCAmelCase__      : Optional[Any]			       =False
  UpperCAmelCase__      : Dict			       =False
  UpperCAmelCase__      : Dict			       =False


  def  _lowercase      (			self    : Dict		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Optional[int]       =   TFCvtModelTester(self		)
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   TFCvtConfigTester(self				,				config_class=UpperCAmelCase__				,				has_text_modality=UpperCAmelCase__				,				hidden_size=3_7		)


  def  _lowercase      (			self    : int		)					->List[Any]:
        """simple docstring"""







        self.config_tester.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()


  @unittest.skip(reason="""Cvt does not output attentions"""		)
  def  _lowercase      (			self    : str		)					->Optional[int]:
        """simple docstring"""







        pass


  @unittest.skip(reason="""Cvt does not use inputs_embeds"""		)
  def  _lowercase      (			self    : Any		)					->str:
        """simple docstring"""







        pass


  @unittest.skip(reason="""Cvt does not support input and output embeddings"""		)
  def  _lowercase      (			self    : Any		)					->str:
        """simple docstring"""







        pass


  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""		)		) == 0				,				reason="""TF does not support backprop for grouped convolutions on CPU."""				,				)
  def  _lowercase      (			self    : Union[str, Any]		)					->int:
        """simple docstring"""







        super().test_dataset_conversion()


  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""		)		) == 0				,				reason="""TF does not support backprop for grouped convolutions on CPU."""				,				)
  @slow
  def  _lowercase      (			self    : Tuple		)					->Optional[int]:
        """simple docstring"""







        super().test_keras_fit()


  @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8"""		)
  def  _lowercase      (			self    : List[Any]		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Tuple       =   tf.keras.mixed_precision.Policy("""mixed_float16"""		)
        tf.keras.mixed_precision.set_global_policy(UpperCAmelCase__		)
        super().test_keras_fit()
        tf.keras.mixed_precision.set_global_policy("""float32"""		)


  def  _lowercase      (			self    : Dict		)					->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[str]       =   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE :      Tuple       =   model_class(UpperCAmelCase__		)
              SCREAMING_SNAKE_CASE :      Optional[int]       =   inspect.signature(model.call		)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              SCREAMING_SNAKE_CASE :      Dict       =   [*signature.parameters.keys()]

              SCREAMING_SNAKE_CASE :      Union[str, Any]       =   ["""pixel_values"""]
              self.assertListEqual(arg_names[:1]				,				UpperCAmelCase__		)


  def  _lowercase      (			self    : Optional[Any]		)					->Union[str, Any]:
        """simple docstring"""







        def check_hidden_states_output(UpperCAmelCase__    : Union[str, Any]				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : int		):
              SCREAMING_SNAKE_CASE :      Optional[int]       =   model_class(UpperCAmelCase__		)

              SCREAMING_SNAKE_CASE :      Tuple       =   model(**self._prepare_for_class(UpperCAmelCase__				,				UpperCAmelCase__		)		)
              SCREAMING_SNAKE_CASE :      Optional[Any]       =   outputs.hidden_states

              SCREAMING_SNAKE_CASE :      Optional[Any]       =   len(self.model_tester.depth		)
              self.assertEqual(len(UpperCAmelCase__		)				,				UpperCAmelCase__		)

              # verify the first hidden states (first block)
              self.assertListEqual(
                  list(hidden_states[0].shape[-3:]		)				,				[
                      self.model_tester.embed_dim[0],
                      self.model_tester.image_size // 4,
                      self.model_tester.image_size // 4,
                  ]				,				)

        SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      str       =   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE :      Tuple       =   True
              check_hidden_states_output(UpperCAmelCase__				,				UpperCAmelCase__				,				UpperCAmelCase__		)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              SCREAMING_SNAKE_CASE :      Any       =   True

              check_hidden_states_output(UpperCAmelCase__				,				UpperCAmelCase__				,				UpperCAmelCase__		)


  def  _lowercase      (			self    : Optional[int]		)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Any       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCAmelCase__		)


  def  _lowercase      (			self    : List[Any]		)					->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Any       =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__		)




  @slow
  def  _lowercase      (			self    : List[str]		)					->Optional[int]:
        """simple docstring"""







        for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE :      Optional[int]       =   TFCvtModel.from_pretrained(UpperCAmelCase__		)
              self.assertIsNotNone(UpperCAmelCase__		)






def 					__lowercase      (     )     ->						Optional[int]:
      SCREAMING_SNAKE_CASE :      Any       =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
      return image







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







  """simple docstring"""




  @cached_property
  def  _lowercase      (			self    : List[str]		)					->Dict:
        """simple docstring"""







        return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)




  @slow
  def  _lowercase      (			self    : Union[str, Any]		)					->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

        SCREAMING_SNAKE_CASE :      List[Any]       =   self.default_image_processor
        SCREAMING_SNAKE_CASE :      Tuple       =   prepare_img()
        SCREAMING_SNAKE_CASE :      Tuple       =   image_processor(images=UpperCAmelCase__				,				return_tensors="""tf"""		)

        # forward pass
        SCREAMING_SNAKE_CASE :      int       =   model(**UpperCAmelCase__		)

        # verify the logits
        SCREAMING_SNAKE_CASE :      List[Any]       =   tf.TensorShape((1, 1_0_0_0)		)
        self.assertEqual(outputs.logits.shape				,				UpperCAmelCase__		)

        SCREAMING_SNAKE_CASE :      Dict       =   tf.constant([0.92_85, 0.90_15, -0.31_50]		)
        self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()				,				UpperCAmelCase__				,				atol=1e-4		)		)







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


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

UpperCAmelCase__ :     int				    =    {
    """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json""",
}







class        a__       (   UpperCAmelCase       ):







  """simple docstring"""




  UpperCAmelCase__      : List[str]			       ="""lxmert"""
  UpperCAmelCase__      : str			       ={}


  def __init__(			self    : int				,				UpperCAmelCase__    : Dict=3_0_5_2_2				,				UpperCAmelCase__    : int=7_6_8				,				UpperCAmelCase__    : Union[str, Any]=1_2				,				UpperCAmelCase__    : int=9_5_0_0				,				UpperCAmelCase__    : Union[str, Any]=1_6_0_0				,				UpperCAmelCase__    : Optional[Any]=4_0_0				,				UpperCAmelCase__    : List[str]=3_0_7_2				,				UpperCAmelCase__    : List[Any]="gelu"				,				UpperCAmelCase__    : int=0.1				,				UpperCAmelCase__    : Optional[Any]=0.1				,				UpperCAmelCase__    : Union[str, Any]=5_1_2				,				UpperCAmelCase__    : int=2				,				UpperCAmelCase__    : List[str]=0.02				,				UpperCAmelCase__    : str=1e-12				,				UpperCAmelCase__    : int=9				,				UpperCAmelCase__    : Optional[int]=5				,				UpperCAmelCase__    : int=5				,				UpperCAmelCase__    : List[str]=2_0_4_8				,				UpperCAmelCase__    : Tuple=4				,				UpperCAmelCase__    : List[str]=6.67				,				UpperCAmelCase__    : Optional[int]=True				,				UpperCAmelCase__    : Dict=True				,				UpperCAmelCase__    : Optional[Any]=True				,				UpperCAmelCase__    : Optional[int]=True				,				UpperCAmelCase__    : List[Any]=True				,				UpperCAmelCase__    : Dict=True				,				UpperCAmelCase__    : Optional[Any]=True				,				**UpperCAmelCase__    : Tuple				,				)					->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   vocab_size
        SCREAMING_SNAKE_CASE :      List[Any]       =   hidden_size
        SCREAMING_SNAKE_CASE :      Any       =   num_attention_heads
        SCREAMING_SNAKE_CASE :      str       =   hidden_act
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   intermediate_size
        SCREAMING_SNAKE_CASE :      List[str]       =   hidden_dropout_prob
        SCREAMING_SNAKE_CASE :      str       =   attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE :      Dict       =   max_position_embeddings
        SCREAMING_SNAKE_CASE :      Any       =   type_vocab_size
        SCREAMING_SNAKE_CASE :      Optional[int]       =   initializer_range
        SCREAMING_SNAKE_CASE :      Optional[int]       =   layer_norm_eps
        SCREAMING_SNAKE_CASE :      Dict       =   num_qa_labels
        SCREAMING_SNAKE_CASE :      str       =   num_object_labels
        SCREAMING_SNAKE_CASE :      str       =   num_attr_labels
        SCREAMING_SNAKE_CASE :      List[str]       =   l_layers
        SCREAMING_SNAKE_CASE :      Optional[int]       =   x_layers
        SCREAMING_SNAKE_CASE :      List[str]       =   r_layers
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   visual_feat_dim
        SCREAMING_SNAKE_CASE :      Dict       =   visual_pos_dim
        SCREAMING_SNAKE_CASE :      List[Any]       =   visual_loss_normalizer
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   task_matched
        SCREAMING_SNAKE_CASE :      int       =   task_mask_lm
        SCREAMING_SNAKE_CASE :      int       =   task_obj_predict
        SCREAMING_SNAKE_CASE :      Optional[int]       =   task_qa
        SCREAMING_SNAKE_CASE :      Any       =   visual_obj_loss
        SCREAMING_SNAKE_CASE :      str       =   visual_attr_loss
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   visual_feat_loss
        SCREAMING_SNAKE_CASE :      List[Any]       =   {"""vision""": r_layers, """cross_encoder""": x_layers, """language""": l_layers}
        super().__init__(**UpperCAmelCase__		)




