




"""simple docstring"""

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        lowercase      (    __UpperCAmelCase):







				def 					a_  (							self   :					Optional[int]       ):



					"""simple docstring"""

					A_  :				Optional[int]	   =		self.config_class(**self.inputs_dict       )
					self.parent.assertTrue(hasattr(_lowerCamelCase      ,     '''tf_padding'''       )       )
					self.parent.assertTrue(hasattr(_lowerCamelCase      ,     '''depth_multiplier'''       )       )

class        lowercase      :







				def __init__(							self   :					str      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					str=13      ,     _lowerCamelCase   :					List[Any]=3      ,     _lowerCamelCase   :					Optional[int]=32      ,     _lowerCamelCase   :					Tuple=0.25      ,     _lowerCamelCase   :					Union[str, Any]=8      ,     _lowerCamelCase   :					List[str]=8      ,     _lowerCamelCase   :					Tuple=6      ,     _lowerCamelCase   :					str=32      ,     _lowerCamelCase   :					Tuple=True      ,     _lowerCamelCase   :					Tuple=True      ,     _lowerCamelCase   :					Optional[int]=True      ,     _lowerCamelCase   :					Dict="relu6"      ,     _lowerCamelCase   :					Dict=12_80      ,     _lowerCamelCase   :					Union[str, Any]=0.1      ,     _lowerCamelCase   :					List[str]=0.02      ,     _lowerCamelCase   :					Union[str, Any]=True      ,     _lowerCamelCase   :					Optional[int]=True      ,     _lowerCamelCase   :					str=10      ,     _lowerCamelCase   :					Dict=None      ,     ):



					"""simple docstring"""

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







				def 					a_  (							self   :					List[Any]       ):



					"""simple docstring"""

					A_  :				Optional[Any]	   =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

					A_  :				Dict	   =		None
					A_  :				Tuple	   =		None
					if self.use_labels:
						A_  :				str	   =		ids_tensor([self.batch_size]      ,     self.num_labels       )
						A_  :				Dict	   =		ids_tensor([self.batch_size, self.image_size, self.image_size]      ,     self.num_labels       )

					A_  :				Any	   =		self.get_config()

					return config, pixel_values, labels, pixel_labels







				def 					a_  (							self   :					int       ):



					"""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 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					str      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					Optional[int]      ,     _lowerCamelCase   :					List[Any]       ):



					"""simple docstring"""

					A_  :				Dict	   =		MobileNetVaModel(config=_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()
					A_  :				Dict	   =		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 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					Optional[Any]      ,     _lowerCamelCase   :					int      ,     _lowerCamelCase   :					str       ):



					"""simple docstring"""

					A_  :				Dict	   =		self.num_labels
					A_  :				Optional[int]	   =		MobileNetVaForImageClassification(_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()
					A_  :				Optional[Any]	   =		model(_lowerCamelCase      ,     labels=_lowerCamelCase       )
					self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.num_labels)       )







				def 					a_  (							self   :					Union[str, Any]      ,     _lowerCamelCase   :					int      ,     _lowerCamelCase   :					str      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					Union[str, Any]       ):



					"""simple docstring"""

					A_  :				Union[str, Any]	   =		self.num_labels
					A_  :				Union[str, Any]	   =		MobileNetVaForSemanticSegmentation(_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()
					A_  :				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,
					    )      ,     )
					A_  :				Optional[int]	   =		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 					a_  (							self   :					List[str]       ):



					"""simple docstring"""

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

@require_torch
class        lowercase      (    __UpperCAmelCase     ,	__UpperCAmelCase     ,	unittest.TestCase):
				__lowerCAmelCase    :    str         =   (
				    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				__lowerCAmelCase    :    List[str]         =   (
				    {
				        """feature-extraction""": MobileNetVaModel,
				        """image-classification""": MobileNetVaForImageClassification,
				        """image-segmentation""": MobileNetVaForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				__lowerCAmelCase    :    Tuple         =   False
				__lowerCAmelCase    :    List[Any]         =   False
				__lowerCAmelCase    :    Tuple         =   False
				__lowerCAmelCase    :    Optional[int]         =   False







				def 					a_  (							self   :					Optional[Any]       ):



					"""simple docstring"""

					A_  :				Any	   =		MobileNetVaModelTester(self       )
					A_  :				Dict	   =		MobileNetVaConfigTester(self      ,     config_class=_lowerCamelCase      ,     has_text_modality=_lowerCamelCase       )







				def 					a_  (							self   :					List[str]       ):



					"""simple docstring"""

					self.config_tester.run_common_tests()







				@unittest.skip(reason='''MobileNetV2 does not use inputs_embeds'''       )
				def 					a_  (							self   :					List[str]       ):



					"""simple docstring"""

					pass







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



					"""simple docstring"""

					pass







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



					"""simple docstring"""

					pass







				def 					a_  (							self   :					List[Any]       ):



					"""simple docstring"""

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

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

						A_  :				Union[str, Any]	   =		['''pixel_values''']
						self.assertListEqual(arg_names[:1]      ,     _lowerCamelCase       )







				def 					a_  (							self   :					Any       ):



					"""simple docstring"""

					A_  :				List[Any]	   =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_lowerCamelCase       )







				def 					a_  (							self   :					int       ):



					"""simple docstring"""

					def check_hidden_states_output(_lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					Optional[int]      ,     _lowerCamelCase   :					Optional[int]       ):
						A_  :				List[Any]	   =		model_class(_lowerCamelCase       )
						model.to(_lowerCamelCase       )
						model.eval()

						with torch.no_grad():
							A_  :				List[Any]	   =		model(**self._prepare_for_class(_lowerCamelCase      ,     _lowerCamelCase       )       )

						A_  :				Any	   =		outputs.hidden_states

						A_  :				Tuple	   =		16
						self.assertEqual(len(_lowerCamelCase       )      ,     _lowerCamelCase       )

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

					for model_class in self.all_model_classes:
						A_  :				Optional[Any]	   =		True
						check_hidden_states_output(_lowerCamelCase      ,     _lowerCamelCase      ,     _lowerCamelCase       )

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

						check_hidden_states_output(_lowerCamelCase      ,     _lowerCamelCase      ,     _lowerCamelCase       )







				def 					a_  (							self   :					Union[str, Any]       ):



					"""simple docstring"""

					A_  :				Dict	   =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase       )







				def 					a_  (							self   :					List[str]       ):



					"""simple docstring"""

					A_  :				Optional[Any]	   =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCamelCase       )







				@slow
				def 					a_  (							self   :					Dict       ):



					"""simple docstring"""

					for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						A_  :				str	   =		MobileNetVaModel.from_pretrained(_lowerCamelCase       )
						self.assertIsNotNone(_lowerCamelCase       )

def 		lowercase_      (		):




	"""simple docstring"""






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

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







				@cached_property
				def 					a_  (							self   :					str       ):



					"""simple docstring"""

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







				@slow
				def 					a_  (							self   :					Any       ):



					"""simple docstring"""

					A_  :				List[Any]	   =		MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''       ).to(_lowerCamelCase       )

					A_  :				Dict	   =		self.default_image_processor
					A_  :				List[str]	   =		prepare_img()
					A_  :				Dict	   =		image_processor(images=_lowerCamelCase      ,     return_tensors='''pt'''       ).to(_lowerCamelCase       )

					# forward pass
					with torch.no_grad():
						A_  :				List[str]	   =		model(**_lowerCamelCase       )

					# verify the logits
					A_  :				List[Any]	   =		torch.Size((1, 10_01)       )
					self.assertEqual(outputs.logits.shape      ,     _lowerCamelCase       )

					A_  :				int	   =		torch.tensor([0.2445, -1.1993, 0.1905]       ).to(_lowerCamelCase       )

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







				@slow
				def 					a_  (							self   :					Tuple       ):



					"""simple docstring"""

					A_  :				Dict	   =		MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''       )
					A_  :				Optional[int]	   =		model.to(_lowerCamelCase       )

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

					A_  :				Optional[int]	   =		prepare_img()
					A_  :				Dict	   =		image_processor(images=_lowerCamelCase      ,     return_tensors='''pt'''       ).to(_lowerCamelCase       )

					# forward pass
					with torch.no_grad():
						A_  :				Union[str, Any]	   =		model(**_lowerCamelCase       )
					A_  :				Optional[Any]	   =		outputs.logits

					# verify the logits
					A_  :				List[str]	   =		torch.Size((1, 21, 65, 65)       )
					self.assertEqual(logits.shape      ,     _lowerCamelCase       )

					A_  :				Optional[int]	   =		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       )       )











"""simple docstring"""

from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


_lowerCamelCase  :			List[str]        =       logging.get_logger(__name__)

_lowerCamelCase  :			str        =       {
    'google/umt5-small': 'https://huggingface.co/google/umt5-small/resolve/main/config.json',
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}

class        lowercase      (    __UpperCAmelCase):
				__lowerCAmelCase    :    int         =   """umt5"""
				__lowerCAmelCase    :    List[str]         =   ["""past_key_values"""]







				def __init__(							self   :					Any      ,     _lowerCamelCase   :					Union[str, Any]=25_01_12      ,     _lowerCamelCase   :					Any=5_12      ,     _lowerCamelCase   :					Optional[int]=64      ,     _lowerCamelCase   :					str=10_24      ,     _lowerCamelCase   :					List[str]=8      ,     _lowerCamelCase   :					Union[str, Any]=None      ,     _lowerCamelCase   :					Any=6      ,     _lowerCamelCase   :					List[Any]=32      ,     _lowerCamelCase   :					Optional[Any]=1_28      ,     _lowerCamelCase   :					int=0.1      ,     _lowerCamelCase   :					Union[str, Any]=1E-6      ,     _lowerCamelCase   :					Tuple=1.0      ,     _lowerCamelCase   :					Optional[int]="gated-gelu"      ,     _lowerCamelCase   :					List[str]=True      ,     _lowerCamelCase   :					str=True      ,     _lowerCamelCase   :					Tuple="T5Tokenizer"      ,     _lowerCamelCase   :					Optional[int]=True      ,     _lowerCamelCase   :					Optional[int]=0      ,     _lowerCamelCase   :					Tuple=1      ,     _lowerCamelCase   :					Tuple=0      ,     **_lowerCamelCase   :					List[str]      ,     ):



					"""simple docstring"""

					super().__init__(
					    is_encoder_decoder=_lowerCamelCase      ,     tokenizer_class=_lowerCamelCase      ,     tie_word_embeddings=_lowerCamelCase      ,     pad_token_id=_lowerCamelCase      ,     eos_token_id=_lowerCamelCase      ,     decoder_start_token_id=_lowerCamelCase      ,     **_lowerCamelCase      ,     )
					A_  :				str	   =		vocab_size
					A_  :				List[Any]	   =		d_model
					A_  :				Optional[int]	   =		d_kv
					A_  :				int	   =		d_ff
					A_  :				Union[str, Any]	   =		num_layers
					A_  :				Optional[Any]	   =		(
					    num_decoder_layers if num_decoder_layers is not None else self.num_layers
					)  # default = symmetry
					A_  :				Dict	   =		num_heads
					A_  :				Optional[int]	   =		relative_attention_num_buckets
					A_  :				Union[str, Any]	   =		relative_attention_max_distance
					A_  :				Any	   =		dropout_rate
					A_  :				Optional[int]	   =		layer_norm_epsilon
					A_  :				Tuple	   =		initializer_factor
					A_  :				Optional[int]	   =		feed_forward_proj
					A_  :				Dict	   =		use_cache

					A_  :				Any	   =		self.feed_forward_proj.split('''-'''       )
					A_  :				Tuple	   =		act_info[-1]
					A_  :				Any	   =		act_info[0] == '''gated'''

					if len(_lowerCamelCase       ) > 1 and act_info[0] != "gated" or len(_lowerCamelCase       ) > 2:
						raise ValueError(
						    F"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
						    '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '''
						    '''\'gated-gelu\' or \'relu\''''       )

					if feed_forward_proj == "gated-gelu":
						A_  :				Dict	   =		'''gelu_new'''







				@property
				def 					a_  (							self   :					Tuple       ):



					"""simple docstring"""

					return self.d_model







				@property
				def 					a_  (							self   :					Optional[int]       ):



					"""simple docstring"""

					return self.num_heads







				@property
				def 					a_  (							self   :					int       ):



					"""simple docstring"""

					return self.num_layers

class        lowercase      (    __UpperCAmelCase):







				@property
				# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
				def 					a_  (							self   :					Any       ):



					"""simple docstring"""

					A_  :				str	   =		{
					    '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''},
					    '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''},
					}
					if self.use_past:
						A_  :				Optional[Any]	   =		'''past_encoder_sequence + sequence'''
						A_  :				Optional[Any]	   =		{0: '''batch'''}
						A_  :				List[Any]	   =		{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
					else:
						A_  :				Optional[int]	   =		{0: '''batch''', 1: '''decoder_sequence'''}
						A_  :				List[str]	   =		{0: '''batch''', 1: '''decoder_sequence'''}

					if self.use_past:
						self.fill_with_past_key_values_(_lowerCamelCase      ,     direction='''inputs'''       )

					return common_inputs







				@property
				# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
				def 					a_  (							self   :					Union[str, Any]       ):



					"""simple docstring"""

					return 13







				@property
				def 					a_  (							self   :					Any       ):



					"""simple docstring"""

					return 5E-4





