


import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
							import jax

							from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel
class _lowerCamelCase	(						unittest.TestCase							):



		'''simple docstring'''







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

			# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
			__magic_name__	:   Optional[Any]					     =					(image_size // patch_size) ** 2
			__magic_name__	:   int					     =					num_patches + 1







		def        __lowerCAmelCase		( self		:						Optional[int]      )  ->			str:
			__magic_name__	:   List[str]					     =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

			__magic_name__	:   Tuple					     =					ViTConfig(
			    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  ,					)

			return config, pixel_values







		def        __lowerCAmelCase		( self		:						Union[str, Any]  ,					_A		:						Dict  ,					_A		:						Dict      )  ->			Optional[int]:
			__magic_name__	:   Optional[int]					     =					FlaxViTModel(config=_A      )
			__magic_name__	:   Any					     =					model(_A      )
			# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
			__magic_name__	:   Any					     =					(self.image_size, self.image_size)
			__magic_name__	:   List[str]					     =					(self.patch_size, self.patch_size)
			__magic_name__	:   Any					     =					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
			self.parent.assertEqual(result.last_hidden_state.shape  ,					(self.batch_size, num_patches + 1, self.hidden_size)      )







		def        __lowerCAmelCase		( self		:						Optional[Any]  ,					_A		:						List[str]  ,					_A		:						List[str]      )  ->			List[str]:
			__magic_name__	:   List[Any]					     =					self.type_sequence_label_size
			__magic_name__	:   Any					     =					FlaxViTForImageClassification(config=_A      )
			__magic_name__	:   Tuple					     =					model(_A      )
			self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.type_sequence_label_size)      )

			# test greyscale images
			__magic_name__	:   Union[str, Any]					     =					1
			__magic_name__	:   List[str]					     =					FlaxViTForImageClassification(_A      )

			__magic_name__	:   int					     =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
			__magic_name__	:   Optional[Any]					     =					model(_A      )







		def        __lowerCAmelCase		( self		:						Tuple      )  ->			Tuple:
			__magic_name__	:   Optional[Any]					     =					self.prepare_config_and_inputs()
			(
			    __magic_name__    
			)	:   List[str]					     =					config_and_inputs
			__magic_name__	:   List[str]					     =					{'pixel_values': pixel_values}
			return config, inputs_dict







@require_flax
class _lowerCamelCase	(						lowercase__		,					unittest.TestCase							):



		'''simple docstring'''







		A_     :      Any				   =    (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()







		def        __lowerCAmelCase		( self		:						str      )  ->			None:
			__magic_name__	:   Any					     =					FlaxViTModelTester(self      )
			__magic_name__	:   Tuple					     =					ConfigTester(self  ,					config_class=_A  ,					has_text_modality=_A  ,					hidden_size=37      )







		def        __lowerCAmelCase		( self		:						int      )  ->			Tuple:
			self.config_tester.run_common_tests()







		def        __lowerCAmelCase		( self		:						Optional[int]      )  ->			int:
			__magic_name__	:   int					     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_A      )







		def        __lowerCAmelCase		( self		:						Tuple      )  ->			Optional[Any]:
			__magic_name__	:   Any					     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*_A      )







		def        __lowerCAmelCase		( self		:						List[Any]      )  ->			Union[str, Any]:
			__magic_name__	:   Dict					     =					self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__magic_name__	:   Optional[int]					     =					model_class(_A      )
				__magic_name__	:   Dict					     =					inspect.signature(model.__call__      )
				# signature.parameters is an OrderedDict => so arg_names order is deterministic
				__magic_name__	:   Union[str, Any]					     =					[*signature.parameters.keys()]

				__magic_name__	:   Optional[int]					     =					['pixel_values']
				self.assertListEqual(arg_names[:1]  ,					_A      )







		def        __lowerCAmelCase		( self		:						Any      )  ->			Optional[Any]:
			__magic_name__	:   List[str]					     =					self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				with self.subTest(model_class.__name__      ):
					__magic_name__	:   Optional[int]					     =					self._prepare_for_class(_A  ,					_A      )
					__magic_name__	:   Dict					     =					model_class(_A      )

					@jax.jit
					def model_jitted(_A		:						List[Any]  ,					**_A		:						Any      ):
						return model(pixel_values=_A  ,					**_A      )

					with self.subTest('JIT Enabled'      ):
						__magic_name__	:   List[Any]					     =					model_jitted(**_A      ).to_tuple()

					with self.subTest('JIT Disabled'      ):
						with jax.disable_jit():
							__magic_name__	:   List[Any]					     =					model_jitted(**_A      ).to_tuple()

					self.assertEqual(len(_A      )  ,					len(_A      )      )
					for jitted_output, output in zip(_A  ,					_A      ):
						self.assertEqual(jitted_output.shape  ,					output.shape      )







		@slow
		def        __lowerCAmelCase		( self		:						int      )  ->			List[str]:
			for model_class_name in self.all_model_classes:
				__magic_name__	:   Optional[Any]					     =					model_class_name.from_pretrained('google/vit-base-patch16-224'      )
				__magic_name__	:   Any					     =					model(np.ones((1, 3, 224, 224)      )      )
				self.assertIsNotNone(_A      )



'''simple docstring'''

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


lowerCAmelCase				:Any        =      logging.get_logger(__name__)

lowerCAmelCase				:int        =      {
    '''unc-nlp/lxmert-base-uncased''': '''https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json''',
}
class _lowerCamelCase	(						lowercase__							):



		'''simple docstring'''







		A_     :      List[str]				   =    """lxmert"""
		A_     :      int				   =    {}







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