'''simple docstring'''

import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import CvtForImageClassification, CvtModel
							from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor





class 							lowercase						(						__lowerCamelCase ):


			def __UpperCAmelCase       (   self				:	Union[str, Any])      ->					int:
									lowercase_     =      self.config_class(**self.inputs_dict)
									self.parent.assertTrue(hasattr(__lowerCAmelCase   ,		"embed_dim"))
									self.parent.assertTrue(hasattr(__lowerCAmelCase   ,		"num_heads"))





class 							lowercase						:


			def __init__(   self				:	Dict   ,		__lowerCAmelCase				:	List[Any]   ,		__lowerCAmelCase				:	Optional[Any]=13   ,		__lowerCAmelCase				:	Any=64   ,		__lowerCAmelCase				:	Tuple=3   ,		__lowerCAmelCase				:	Optional[Any]=[16, 48, 96]   ,		__lowerCAmelCase				:	Any=[1, 3, 6]   ,		__lowerCAmelCase				:	Union[str, Any]=[1, 2, 10]   ,		__lowerCAmelCase				:	Optional[int]=[7, 3, 3]   ,		__lowerCAmelCase				:	List[str]=[4, 2, 2]   ,		__lowerCAmelCase				:	Optional[int]=[2, 1, 1]   ,		__lowerCAmelCase				:	Optional[Any]=[2, 2, 2]   ,		__lowerCAmelCase				:	Tuple=[False, False, True]   ,		__lowerCAmelCase				:	Optional[int]=[0.0, 0.0, 0.0]   ,		__lowerCAmelCase				:	int=0.02   ,		__lowerCAmelCase				:	List[Any]=1e-12   ,		__lowerCAmelCase				:	Union[str, Any]=True   ,		__lowerCAmelCase				:	List[Any]=True   ,		__lowerCAmelCase				:	Optional[Any]=2   ,		)      ->					Optional[Any]:
									lowercase_     =      parent
									lowercase_     =      batch_size
									lowercase_     =      image_size
									lowercase_     =      patch_sizes
									lowercase_     =      patch_stride
									lowercase_     =      patch_padding
									lowercase_     =      is_training
									lowercase_     =      use_labels
									lowercase_     =      num_labels
									lowercase_     =      num_channels
									lowercase_     =      embed_dim
									lowercase_     =      num_heads
									lowercase_     =      stride_kv
									lowercase_     =      depth
									lowercase_     =      cls_token
									lowercase_     =      attention_drop_rate
									lowercase_     =      initializer_range
									lowercase_     =      layer_norm_eps


			def __UpperCAmelCase       (   self				:	Optional[Any])      ->					Any:
									lowercase_     =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

									lowercase_     =      None
									if self.use_labels:
															lowercase_     =      ids_tensor([self.batch_size]   ,		self.num_labels)

									lowercase_     =      self.get_config()
									return config, pixel_values, labels


			def __UpperCAmelCase       (   self				:	Dict)      ->					Dict:
									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 __UpperCAmelCase       (   self				:	List[Any]   ,		__lowerCAmelCase				:	List[str]   ,		__lowerCAmelCase				:	str   ,		__lowerCAmelCase				:	Any)      ->					Tuple:
									lowercase_     =      CvtModel(config=__lowerCAmelCase)
									model.to(__lowerCAmelCase)
									model.eval()
									lowercase_     =      model(__lowerCAmelCase)
									lowercase_     =      (self.image_size, self.image_size)
									lowercase_ ,							lowercase_     =      image_size[0], image_size[1]
									for i in range(len(self.depth)):
															lowercase_     =      floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1)
															lowercase_     =      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 __UpperCAmelCase       (   self				:	Union[str, Any]   ,		__lowerCAmelCase				:	List[Any]   ,		__lowerCAmelCase				:	Dict   ,		__lowerCAmelCase				:	List[str])      ->					List[str]:
									lowercase_     =      self.num_labels
									lowercase_     =      CvtForImageClassification(__lowerCAmelCase)
									model.to(__lowerCAmelCase)
									model.eval()
									lowercase_     =      model(__lowerCAmelCase   ,		labels=__lowerCAmelCase)
									self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.num_labels))


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





@require_torch
class 							lowercase						(						__lowerCamelCase ,				__lowerCamelCase ,				unittest.TestCase ):
			lowerCamelCase_    					=(CvtModel, CvtForImageClassification) if is_torch_available() else ()
			lowerCamelCase_    					=(
			    {'feature-extraction': CvtModel, 'image-classification': CvtForImageClassification}
			    if is_torch_available()
			    else {}
			)

			lowerCamelCase_    					=False
			lowerCamelCase_    					=False
			lowerCamelCase_    					=False
			lowerCamelCase_    					=False
			lowerCamelCase_    					=False


			def __UpperCAmelCase       (   self				:	Any)      ->					Optional[int]:
									lowercase_     =      CvtModelTester(self)
									lowercase_     =      ConfigTester(self   ,		config_class=__lowerCAmelCase   ,		has_text_modality=__lowerCAmelCase   ,		hidden_size=37)


			def __UpperCAmelCase       (   self				:	int)      ->					Any:
									self.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()


			def __UpperCAmelCase       (   self				:	Union[str, Any])      ->					Any:
									return


			@unittest.skip(reason="Cvt does not output attentions")
			def __UpperCAmelCase       (   self				:	int)      ->					Tuple:
									pass


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


			@unittest.skip(reason="Cvt does not support input and output embeddings")
			def __UpperCAmelCase       (   self				:	Optional[Any])      ->					List[str]:
									pass


			def __UpperCAmelCase       (   self				:	Optional[Any])      ->					Union[str, Any]:
									lowercase_ ,							lowercase_     =      self.model_tester.prepare_config_and_inputs_for_common()

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

															lowercase_     =      ["pixel_values"]
															self.assertListEqual(arg_names[:1]   ,		__lowerCAmelCase)


			def __UpperCAmelCase       (   self				:	Any)      ->					int:
									lowercase_     =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCAmelCase)


			def __UpperCAmelCase       (   self				:	Optional[int])      ->					Union[str, Any]:
									def check_hidden_states_output(__lowerCAmelCase				:	List[str]   ,		__lowerCAmelCase				:	List[str]   ,		__lowerCAmelCase				:	List[Any]):
															lowercase_     =      model_class(__lowerCAmelCase)
															model.to(__lowerCAmelCase)
															model.eval()

															with torch.no_grad():
																					lowercase_     =      model(**self._prepare_for_class(__lowerCAmelCase   ,		__lowerCAmelCase))

															lowercase_     =      outputs.hidden_states

															lowercase_     =      len(self.model_tester.depth)
															self.assertEqual(len(__lowerCAmelCase)   ,		__lowerCAmelCase)

															# 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,
															    ]   ,		)

									lowercase_ ,							lowercase_     =      self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															lowercase_     =      True
															check_hidden_states_output(__lowerCAmelCase   ,		__lowerCAmelCase   ,		__lowerCAmelCase)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															lowercase_     =      True

															check_hidden_states_output(__lowerCAmelCase   ,		__lowerCAmelCase   ,		__lowerCAmelCase)


			def __UpperCAmelCase       (   self				:	Union[str, Any])      ->					Any:
									lowercase_     =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase)


			@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests.")
			def __UpperCAmelCase       (   self				:	Any)      ->					Dict:
									pass


			@slow
			def __UpperCAmelCase       (   self				:	Any)      ->					Any:
									for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowercase_     =      CvtModel.from_pretrained(__lowerCAmelCase)
															self.assertIsNotNone(__lowerCAmelCase)




def __a (	)	->							List[Any]:
						'''simple docstring'''


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






@require_torch
@require_vision
class 							lowercase						(						unittest.TestCase ):


			@cached_property
			def __UpperCAmelCase       (   self				:	List[str])      ->					Optional[int]:
									return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])


			@slow
			def __UpperCAmelCase       (   self				:	List[Any])      ->					int:
									lowercase_     =      CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(__lowerCAmelCase)

									lowercase_     =      self.default_image_processor
									lowercase_     =      prepare_img()
									lowercase_     =      image_processor(images=__lowerCAmelCase   ,		return_tensors="pt").to(__lowerCAmelCase)

									# forward pass
									with torch.no_grad():
															lowercase_     =      model(**__lowerCAmelCase)

									# verify the logits
									lowercase_     =      torch.Size((1, 1000))
									self.assertEqual(outputs.logits.shape   ,		__lowerCAmelCase)

									lowercase_     =      torch.tensor([0.9285, 0.9015, -0.3150]).to(__lowerCAmelCase)

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







'''simple docstring'''

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


lowerCAmelCase_ : Dict						  =      logging.get_logger(__name__)

lowerCAmelCase_ : List[Any]						  =      {
    "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json",
}





class 							lowercase						(						__lowerCamelCase ):
   lowerCamelCase_    					='lxmert'
   lowerCamelCase_    					={}


   def __init__(   self				:	str   ,		__lowerCAmelCase				:	str=3_0522   ,		__lowerCAmelCase				:	Dict=768   ,		__lowerCAmelCase				:	List[str]=12   ,		__lowerCAmelCase				:	List[str]=9500   ,		__lowerCAmelCase				:	Optional[int]=1600   ,		__lowerCAmelCase				:	Tuple=400   ,		__lowerCAmelCase				:	str=3072   ,		__lowerCAmelCase				:	str="gelu"   ,		__lowerCAmelCase				:	Optional[Any]=0.1   ,		__lowerCAmelCase				:	List[Any]=0.1   ,		__lowerCAmelCase				:	Tuple=512   ,		__lowerCAmelCase				:	Tuple=2   ,		__lowerCAmelCase				:	Dict=0.02   ,		__lowerCAmelCase				:	int=1e-12   ,		__lowerCAmelCase				:	Union[str, Any]=9   ,		__lowerCAmelCase				:	List[str]=5   ,		__lowerCAmelCase				:	Any=5   ,		__lowerCAmelCase				:	Dict=2048   ,		__lowerCAmelCase				:	Any=4   ,		__lowerCAmelCase				:	Optional[int]=6.67   ,		__lowerCAmelCase				:	Optional[Any]=True   ,		__lowerCAmelCase				:	Tuple=True   ,		__lowerCAmelCase				:	Optional[int]=True   ,		__lowerCAmelCase				:	List[Any]=True   ,		__lowerCAmelCase				:	Optional[Any]=True   ,		__lowerCAmelCase				:	int=True   ,		__lowerCAmelCase				:	List[str]=True   ,		**__lowerCAmelCase				:	Dict   ,		)      ->					Dict:
         lowercase_     =      vocab_size
         lowercase_     =      hidden_size
         lowercase_     =      num_attention_heads
         lowercase_     =      hidden_act
         lowercase_     =      intermediate_size
         lowercase_     =      hidden_dropout_prob
         lowercase_     =      attention_probs_dropout_prob
         lowercase_     =      max_position_embeddings
         lowercase_     =      type_vocab_size
         lowercase_     =      initializer_range
         lowercase_     =      layer_norm_eps
         lowercase_     =      num_qa_labels
         lowercase_     =      num_object_labels
         lowercase_     =      num_attr_labels
         lowercase_     =      l_layers
         lowercase_     =      x_layers
         lowercase_     =      r_layers
         lowercase_     =      visual_feat_dim
         lowercase_     =      visual_pos_dim
         lowercase_     =      visual_loss_normalizer
         lowercase_     =      task_matched
         lowercase_     =      task_mask_lm
         lowercase_     =      task_obj_predict
         lowercase_     =      task_qa
         lowercase_     =      visual_obj_loss
         lowercase_     =      visual_attr_loss
         lowercase_     =      visual_feat_loss
         lowercase_     =      {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers}
         super().__init__(**__lowerCAmelCase)






