





import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
							from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import MobileNetVaImageProcessor





class 				a			(      _SCREAMING_SNAKE_CASE			):






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.config_class(**self.inputs_dict					)
										self.parent.assertTrue(hasattr(_lowerCAmelCase			, '''tf_padding'''					)					)
										self.parent.assertTrue(hasattr(_lowerCAmelCase			, '''depth_multiplier'''					)					)





class 				a			:






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=13			, _lowerCAmelCase=3			, _lowerCAmelCase=32			, _lowerCAmelCase=0.25			, _lowerCAmelCase=8			, _lowerCAmelCase=8			, _lowerCAmelCase=6			, _lowerCAmelCase=32			, _lowerCAmelCase=True			, _lowerCAmelCase=True			, _lowerCAmelCase=True			, _lowerCAmelCase="relu6"			, _lowerCAmelCase=1280			, _lowerCAmelCase=0.1			, _lowerCAmelCase=0.02			, _lowerCAmelCase=True			, _lowerCAmelCase=True			, _lowerCAmelCase=10			, _lowerCAmelCase=None			, ):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   parent
										__SCREAMING_SNAKE_CASE:				str           =   batch_size
										__SCREAMING_SNAKE_CASE:				Tuple           =   num_channels
										__SCREAMING_SNAKE_CASE:				List[str]           =   image_size
										__SCREAMING_SNAKE_CASE:				List[str]           =   depth_multiplier
										__SCREAMING_SNAKE_CASE:				str           =   depth_divisible_by
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   min_depth
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   expand_ratio
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   tf_padding
										__SCREAMING_SNAKE_CASE:				Tuple           =   output_stride
										__SCREAMING_SNAKE_CASE:				Tuple           =   first_layer_is_expansion
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   finegrained_output
										__SCREAMING_SNAKE_CASE:				List[str]           =   hidden_act
										__SCREAMING_SNAKE_CASE:				List[Any]           =   last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier					)
										__SCREAMING_SNAKE_CASE:				str           =   classifier_dropout_prob
										__SCREAMING_SNAKE_CASE:				List[str]           =   use_labels
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   is_training
										__SCREAMING_SNAKE_CASE:				List[Any]           =   num_labels
										__SCREAMING_SNAKE_CASE:				List[Any]           =   initializer_range
										__SCREAMING_SNAKE_CASE:				Tuple           =   scope






				def 			snake_case_    (  self					):

										"""simple docstring"""




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

										__SCREAMING_SNAKE_CASE:				Dict           =   None
										__SCREAMING_SNAKE_CASE:				str           =   None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   ids_tensor([self.batch_size]			, self.num_labels					)
																__SCREAMING_SNAKE_CASE:				int           =   ids_tensor([self.batch_size, self.image_size, self.image_size]			, self.num_labels					)

										__SCREAMING_SNAKE_CASE:				List[str]           =   self.get_config()

										return config, pixel_values, labels, pixel_labels






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return MobileNetVaConfig(
										    num_channels=self.num_channels			, image_size=self.image_size			, depth_multiplier=self.depth_multiplier			, depth_divisible_by=self.depth_divisible_by			, min_depth=self.min_depth			, expand_ratio=self.expand_ratio			, output_stride=self.output_stride			, first_layer_is_expansion=self.first_layer_is_expansion			, finegrained_output=self.finegrained_output			, hidden_act=self.hidden_act			, tf_padding=self.tf_padding			, classifier_dropout_prob=self.classifier_dropout_prob			, initializer_range=self.initializer_range			, )






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   MobileNetVaModel(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Tuple           =   model(_lowerCAmelCase					)
										self.parent.assertEqual(
										    result.last_hidden_state.shape			, (
										        self.batch_size,
										        self.last_hidden_size,
										        self.image_size // self.output_stride,
										        self.image_size // self.output_stride,
										    )			, )
										self.parent.assertEqual(
										    result.pooler_output.shape			, (self.batch_size, self.last_hidden_size)			, )






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.num_labels
										__SCREAMING_SNAKE_CASE:				List[str]           =   MobileNetVaForImageClassification(_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   model(_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_labels)					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   self.num_labels
										__SCREAMING_SNAKE_CASE:				List[str]           =   MobileNetVaForSemanticSegmentation(_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   model(_lowerCAmelCase					)
										self.parent.assertEqual(
										    result.logits.shape			, (
										        self.batch_size,
										        self.num_labels,
										        self.image_size // self.output_stride,
										        self.image_size // self.output_stride,
										    )			, )
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   model(_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(
										    result.logits.shape			, (
										        self.batch_size,
										        self.num_labels,
										        self.image_size // self.output_stride,
										        self.image_size // self.output_stride,
										    )			, )






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.prepare_config_and_inputs()
										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Dict           =   config_and_inputs
										__SCREAMING_SNAKE_CASE:				Tuple           =   {'''pixel_values''': pixel_values}
										return config, inputs_dict





@require_torch
class 				a			(      _SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   Optional[int]		      =     (
				    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE__     :   Optional[Any]		      =     (
				    {
				        '''feature-extraction''': MobileNetVaModel,
				        '''image-classification''': MobileNetVaForImageClassification,
				        '''image-segmentation''': MobileNetVaForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE__     :   str		      =     False
				SCREAMING_SNAKE_CASE__     :   int		      =     False
				SCREAMING_SNAKE_CASE__     :   Optional[int]		      =     False
				SCREAMING_SNAKE_CASE__     :   Optional[Any]		      =     False






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   MobileNetVaModelTester(self					)
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   MobileNetVaConfigTester(self			, config_class=_lowerCAmelCase			, has_text_modality=_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										self.config_tester.run_common_tests()






				@unittest.skip(reason='''MobileNetV2 does not use inputs_embeds'''					)
				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass






				@unittest.skip(reason='''MobileNetV2 does not support input and output embeddings'''					)
				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass






				@unittest.skip(reason='''MobileNetV2 does not output attentions'''					)
				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				int           =   self.model_tester.prepare_config_and_inputs_for_common()

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

																__SCREAMING_SNAKE_CASE:				Tuple           =   ['''pixel_values''']
																self.assertListEqual(arg_names[:1]			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										def check_hidden_states_output(_lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):
																__SCREAMING_SNAKE_CASE:				Optional[Any]           =   model_class(_lowerCAmelCase					)
																model.to(_lowerCAmelCase					)
																model.eval()

																with torch.no_grad():
																						__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   model(**self._prepare_for_class(_lowerCAmelCase			, _lowerCAmelCase					)					)

																__SCREAMING_SNAKE_CASE:				str           =   outputs.hidden_states

																__SCREAMING_SNAKE_CASE:				str           =   16
																self.assertEqual(len(_lowerCAmelCase					)			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				int           =   self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__SCREAMING_SNAKE_CASE:				str           =   True
																check_hidden_states_output(_lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					)

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																__SCREAMING_SNAKE_CASE:				List[str]           =   True

																check_hidden_states_output(_lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[str]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCAmelCase					)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE:				Tuple           =   MobileNetVaModel.from_pretrained(_lowerCAmelCase					)
																self.assertIsNotNone(_lowerCAmelCase					)






def 		lowerCAmelCase  (	)      ->    Dict:

						"""simple docstring"""





						__SCREAMING_SNAKE_CASE:				Optional[Any]           =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
						return image





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






				@cached_property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										return (
										    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224'''					) if is_vision_available() else None
										)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''					).to(_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.default_image_processor
										__SCREAMING_SNAKE_CASE:				str           =   prepare_img()
										__SCREAMING_SNAKE_CASE:				List[Any]           =   image_processor(images=_lowerCAmelCase			, return_tensors='''pt'''					).to(_lowerCAmelCase					)

										# forward pass
										with torch.no_grad():
																__SCREAMING_SNAKE_CASE:				str           =   model(**_lowerCAmelCase					)

										# verify the logits
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   torch.Size((1, 1001)					)
										self.assertEqual(outputs.logits.shape			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Any           =   torch.tensor([0.2445, -1.1993, 0.1905]					).to(_lowerCAmelCase					)

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






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''					)
										__SCREAMING_SNAKE_CASE:				Dict           =   model.to(_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				int           =   MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''					)

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   prepare_img()
										__SCREAMING_SNAKE_CASE:				List[str]           =   image_processor(images=_lowerCAmelCase			, return_tensors='''pt'''					).to(_lowerCAmelCase					)

										# forward pass
										with torch.no_grad():
																__SCREAMING_SNAKE_CASE:				List[Any]           =   model(**_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Tuple           =   outputs.logits

										# verify the logits
										__SCREAMING_SNAKE_CASE:				str           =   torch.Size((1, 21, 65, 65)					)
										self.assertEqual(logits.shape			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				List[Any]           =   torch.tensor(
										    [
										        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
										        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
										        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
										    ]			, device=_lowerCAmelCase			, )

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









import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       "M-CLIP"



 def __init__(				self    :						Tuple							,							_lowerCAmelCase    :						List[str]=1_024							,							_lowerCAmelCase    :						str=768							,							**_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     transformerDimSize
       SCREAMING_SNAKE_CASE_      		=     imageDimSize
       super().__init__(**_lowerCAmelCase		)





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       MCLIPConfig



 def __init__(				self    :						Dict							,							_lowerCAmelCase    :						Union[str, Any]							,							*_lowerCAmelCase    :						str							,							**_lowerCAmelCase    :						str		):
       super().__init__(_lowerCAmelCase							,							*_lowerCAmelCase							,							**_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     XLMRobertaModel(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.nn.Linear(
           in_features=config.transformerDimensions							,							out_features=config.numDims		)




 def  lowerCAmelCase_    (				self    :						List[Any]							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.transformer(input_ids=_lowerCAmelCase							,							attention_mask=_lowerCAmelCase		)[0]
       SCREAMING_SNAKE_CASE_      		=     (embs * attention_mask.unsqueeze(2		)).sum(dim=1		) / attention_mask.sum(dim=1		)[:, None]
       return self.LinearTransformation(_lowerCAmelCase		), embs