






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  _lowerCAmelCase      (		lowercase_   ):



				"""simple docstring"""



				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







											snake_case__							=							self.config_class(**self.inputs_dict)
											self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""tf_padding"""))
											self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""depth_multiplier"""))


class  _lowerCAmelCase      :



				"""simple docstring"""



				def __init__(   self     :     List[Any]      ,		UpperCamelCase__     :     Optional[Any]      ,		UpperCamelCase__     :     List[str]=1_3      ,		UpperCamelCase__     :     Optional[int]=3      ,		UpperCamelCase__     :     Optional[Any]=3_2      ,		UpperCamelCase__     :     List[str]=0.25      ,		UpperCamelCase__     :     Optional[int]=8      ,		UpperCamelCase__     :     Union[str, Any]=8      ,		UpperCamelCase__     :     List[Any]=6      ,		UpperCamelCase__     :     Tuple=3_2      ,		UpperCamelCase__     :     Any=True      ,		UpperCamelCase__     :     Any=True      ,		UpperCamelCase__     :     List[str]=True      ,		UpperCamelCase__     :     str="relu6"      ,		UpperCamelCase__     :     Any=1_2_8_0      ,		UpperCamelCase__     :     Dict=0.1      ,		UpperCamelCase__     :     Union[str, Any]=0.02      ,		UpperCamelCase__     :     Optional[Any]=True      ,		UpperCamelCase__     :     List[Any]=True      ,		UpperCamelCase__     :     Optional[int]=1_0      ,		UpperCamelCase__     :     Union[str, Any]=None      ,		):






											'''simple docstring'''







											snake_case__							=							parent
											snake_case__							=							batch_size
											snake_case__							=							num_channels
											snake_case__							=							image_size
											snake_case__							=							depth_multiplier
											snake_case__							=							depth_divisible_by
											snake_case__							=							min_depth
											snake_case__							=							expand_ratio
											snake_case__							=							tf_padding
											snake_case__							=							output_stride
											snake_case__							=							first_layer_is_expansion
											snake_case__							=							finegrained_output
											snake_case__							=							hidden_act
											snake_case__							=							last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier)
											snake_case__							=							classifier_dropout_prob
											snake_case__							=							use_labels
											snake_case__							=							is_training
											snake_case__							=							num_labels
											snake_case__							=							initializer_range
											snake_case__							=							scope



				def        __magic_name__				(   self     :     Any):






											'''simple docstring'''







											snake_case__							=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

											snake_case__							=							self.get_config()

											return config, pixel_values, labels, pixel_labels



				def        __magic_name__				(   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        __magic_name__				(   self     :     Optional[int]      ,		UpperCamelCase__     :     Optional[int]      ,		UpperCamelCase__     :     Any      ,		UpperCamelCase__     :     str      ,		UpperCamelCase__     :     Any):






											'''simple docstring'''







											snake_case__							=							MobileNetVaModel(config=_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											snake_case__							=							model(_SCREAMING_SNAKE_CASE)
											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        __magic_name__				(   self     :     Union[str, Any]      ,		UpperCamelCase__     :     Any      ,		UpperCamelCase__     :     Any      ,		UpperCamelCase__     :     int      ,		UpperCamelCase__     :     Optional[int]):






											'''simple docstring'''







											snake_case__							=							self.num_labels
											snake_case__							=							MobileNetVaForImageClassification(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											snake_case__							=							model(_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_labels))



				def        __magic_name__				(   self     :     Union[str, Any]      ,		UpperCamelCase__     :     List[str]      ,		UpperCamelCase__     :     int      ,		UpperCamelCase__     :     Optional[int]      ,		UpperCamelCase__     :     List[Any]):






											'''simple docstring'''







											snake_case__							=							self.num_labels
											snake_case__							=							MobileNetVaForSemanticSegmentation(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											snake_case__							=							model(_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(
											    result.logits.shape      ,		(
											        self.batch_size,
											        self.num_labels,
											        self.image_size // self.output_stride,
											        self.image_size // self.output_stride,
											    )      ,		)
											snake_case__							=							model(_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE)
											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        __magic_name__				(   self     :     str):






											'''simple docstring'''







											snake_case__							=							self.prepare_config_and_inputs()
											snake_case__	,			snake_case__	,			snake_case__	,			snake_case__							=							config_and_inputs
											snake_case__							=							{"""pixel_values""": pixel_values}
											return config, inputs_dict


@require_torch
class  _lowerCAmelCase      (		lowercase_  ,   lowercase_  ,   unittest.TestCase   ):



				"""simple docstring"""



				_lowercase					: List[str]	     =	(
				    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				_lowercase					: Optional[int]	     =	(
				    {
				        'feature-extraction': MobileNetVaModel,
				        'image-classification': MobileNetVaForImageClassification,
				        'image-segmentation': MobileNetVaForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				_lowercase					: Optional[Any]	     =	False
				_lowercase					: str	     =	False
				_lowercase					: Optional[int]	     =	False
				_lowercase					: List[Any]	     =	False



				def        __magic_name__				(   self     :     Optional[int]):






											'''simple docstring'''







											snake_case__							=							MobileNetVaModelTester(self)
											snake_case__							=							MobileNetVaConfigTester(self      ,		config_class=_SCREAMING_SNAKE_CASE      ,		has_text_modality=_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     str):






											'''simple docstring'''







											self.config_tester.run_common_tests()



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






											'''simple docstring'''







											pass



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






											'''simple docstring'''







											pass



				@unittest.skip(reason="""MobileNetV2 does not output attentions""")
				def        __magic_name__				(   self     :     str):






											'''simple docstring'''







											pass



				def        __magic_name__				(   self     :     Optional[Any]):






											'''simple docstring'''







											snake_case__	,			snake_case__							=							self.model_tester.prepare_config_and_inputs_for_common()

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

																		snake_case__							=							["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]      ,		_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







											snake_case__							=							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     List[str]):






											'''simple docstring'''







											def check_hidden_states_output(UpperCamelCase__     :     List[Any]      ,		UpperCamelCase__     :     Optional[int]      ,		UpperCamelCase__     :     Dict):
																		snake_case__							=							model_class(_SCREAMING_SNAKE_CASE)
																		model.to(_SCREAMING_SNAKE_CASE)
																		model.eval()

																		with torch.no_grad():
																									snake_case__							=							model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE))

																		snake_case__							=							outputs.hidden_states

																		snake_case__							=							1_6
																		self.assertEqual(len(_SCREAMING_SNAKE_CASE)      ,		_SCREAMING_SNAKE_CASE)

											snake_case__	,			snake_case__							=							self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		snake_case__							=							True
																		check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE)

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

																		check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     Optional[Any]):






											'''simple docstring'''







											snake_case__							=							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE)



				def        __magic_name__				(   self     :     List[str]):






											'''simple docstring'''







											snake_case__							=							self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE)



				@slow
				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







											for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		snake_case__							=							MobileNetVaModel.from_pretrained(_SCREAMING_SNAKE_CASE)
																		self.assertIsNotNone(_SCREAMING_SNAKE_CASE)


def _UpperCAmelCase (						):
							snake_case__							=							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
							return image



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



				"""simple docstring"""



				@cached_property
				def        __magic_name__				(   self     :     List[Any]):






											'''simple docstring'''







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



				@slow
				def        __magic_name__				(   self     :     List[str]):






											'''simple docstring'''







											snake_case__							=							MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224""").to(_SCREAMING_SNAKE_CASE)

											snake_case__							=							self.default_image_processor
											snake_case__							=							prepare_img()
											snake_case__							=							image_processor(images=_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt""").to(_SCREAMING_SNAKE_CASE)

											# forward pass
											with torch.no_grad():
																		snake_case__							=							model(**_SCREAMING_SNAKE_CASE)

											# verify the logits
											snake_case__							=							torch.Size((1, 1_0_0_1))
											self.assertEqual(outputs.logits.shape      ,		_SCREAMING_SNAKE_CASE)

											snake_case__							=							torch.tensor([0.24_45, -1.19_93, 0.19_05]).to(_SCREAMING_SNAKE_CASE)

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



				@slow
				def        __magic_name__				(   self     :     Union[str, Any]):






											'''simple docstring'''







											snake_case__							=							MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513""")
											snake_case__							=							model.to(_SCREAMING_SNAKE_CASE)

											snake_case__							=							MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513""")

											snake_case__							=							prepare_img()
											snake_case__							=							image_processor(images=_SCREAMING_SNAKE_CASE      ,		return_tensors="""pt""").to(_SCREAMING_SNAKE_CASE)

											# forward pass
											with torch.no_grad():
																		snake_case__							=							model(**_SCREAMING_SNAKE_CASE)
											snake_case__							=							outputs.logits

											# verify the logits
											snake_case__							=							torch.Size((1, 2_1, 6_5, 6_5))
											self.assertEqual(logits.shape      ,		_SCREAMING_SNAKE_CASE)

											snake_case__							=							torch.tensor(
											    [
											        [[17.57_90, 17.75_81, 18.33_55], [18.32_57, 18.42_30, 18.89_73], [18.61_69, 18.86_50, 19.21_87]],
											        [[-2.15_95, -2.09_77, -2.37_41], [-2.42_26, -2.30_28, -2.68_35], [-2.78_19, -2.59_91, -2.77_06]],
											        [[4.20_58, 4.83_17, 4.76_38], [4.41_36, 5.03_61, 4.93_83], [4.50_28, 4.96_44, 4.87_34]],
											    ]      ,		device=_SCREAMING_SNAKE_CASE      ,		)

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




def 			lowerCAmelCase__					(     a__:						int     ,    a__:						int			)    ->						int:


							'''simple docstring'''





							return x if y == 0 else greatest_common_divisor(a__     ,    x % y			)

def 			lowerCAmelCase__					(     a__:						int     ,    a__:						int			)    ->						int:


							'''simple docstring'''





							return (x * y) // greatest_common_divisor(a__     ,    a__			)

def 			lowerCAmelCase__					(     a__:						int = 2_0			)    ->						int:


							'''simple docstring'''





							_UpperCAmelCase											=     1
							for i in range(1     ,    n + 1			):
														_UpperCAmelCase											=     lcm(a__     ,    a__			)
							return g


if __name__ == "__main__":
		print(f'''{solution() = }''')
