



"""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, MobileNetVaModel
		from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import MobileNetVaImageProcessor

class  __SCREAMING_SNAKE_CASE			(   lowerCAmelCase_     ):


					'''simple docstring'''





					def 	snake_case ( self							:						Union[str, Any]  )->      List[str]:
								lowerCamelCase__	:	Dict				=self.config_class(**self.inputs_dict  )
								self.parent.assertTrue(hasattr(lowerCamelCase,						'''tf_padding'''  )  )
								self.parent.assertTrue(hasattr(lowerCamelCase,						'''depth_multiplier'''  )  )





class  __SCREAMING_SNAKE_CASE			:


					'''simple docstring'''





					def __init__( self							:						Optional[Any],						lowerCamelCase							:						Optional[int],						lowerCamelCase							:						List[str]=13,						lowerCamelCase							:						str=3,						lowerCamelCase							:						List[str]=32,						lowerCamelCase							:						Any=0.25,						lowerCamelCase							:						Optional[int]=8,						lowerCamelCase							:						str=True,						lowerCamelCase							:						Tuple=1024,						lowerCamelCase							:						Optional[int]=32,						lowerCamelCase							:						Union[str, Any]="relu6",						lowerCamelCase							:						Any=0.1,						lowerCamelCase							:						Tuple=0.02,						lowerCamelCase							:						Union[str, Any]=True,						lowerCamelCase							:						Union[str, Any]=True,						lowerCamelCase							:						Optional[int]=10,						lowerCamelCase							:						int=None,						)->      Any:
								lowerCamelCase__	:	Any				=parent
								lowerCamelCase__	:	Optional[Any]				=batch_size
								lowerCamelCase__	:	Union[str, Any]				=num_channels
								lowerCamelCase__	:	str				=image_size
								lowerCamelCase__	:	Union[str, Any]				=depth_multiplier
								lowerCamelCase__	:	List[str]				=min_depth
								lowerCamelCase__	:	int				=tf_padding
								lowerCamelCase__	:	Optional[Any]				=int(last_hidden_size * depth_multiplier  )
								lowerCamelCase__	:	List[str]				=output_stride
								lowerCamelCase__	:	Optional[Any]				=hidden_act
								lowerCamelCase__	:	Tuple				=classifier_dropout_prob
								lowerCamelCase__	:	Any				=use_labels
								lowerCamelCase__	:	int				=is_training
								lowerCamelCase__	:	Union[str, Any]				=num_labels
								lowerCamelCase__	:	int				=initializer_range
								lowerCamelCase__	:	Optional[int]				=scope



					def 	snake_case ( self							:						List[str]  )->      Union[str, Any]:
								lowerCamelCase__	:	str				=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

								lowerCamelCase__	:	Dict				=self.get_config()

								return config, pixel_values, labels, pixel_labels



					def 	snake_case ( self							:						int  )->      List[Any]:
								return MobileNetVaConfig(
								    num_channels=self.num_channels,						image_size=self.image_size,						depth_multiplier=self.depth_multiplier,						min_depth=self.min_depth,						tf_padding=self.tf_padding,						hidden_act=self.hidden_act,						classifier_dropout_prob=self.classifier_dropout_prob,						initializer_range=self.initializer_range,						)



					def 	snake_case ( self							:						Tuple,						lowerCamelCase							:						Optional[int],						lowerCamelCase							:						Tuple,						lowerCamelCase							:						List[Any],						lowerCamelCase							:						Optional[Any]  )->      Tuple:
								lowerCamelCase__	:	str				=MobileNetVaModel(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowerCamelCase__	:	Optional[int]				=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,
								    ),						)



					def 	snake_case ( self							:						Tuple,						lowerCamelCase							:						Optional[Any],						lowerCamelCase							:						Tuple,						lowerCamelCase							:						Union[str, Any],						lowerCamelCase							:						int  )->      int:
								lowerCamelCase__	:	Optional[Any]				=self.num_labels
								lowerCamelCase__	:	Any				=MobileNetVaForImageClassification(lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowerCamelCase__	:	Optional[Any]				=model(lowerCamelCase,						labels=lowerCamelCase  )
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_labels)  )




					def 	snake_case ( self							:						Union[str, Any]  )->      Optional[int]:
								lowerCamelCase__	:	str				=self.prepare_config_and_inputs()
								lowerCamelCase__			,     lowerCamelCase__			,     lowerCamelCase__			,     lowerCamelCase__	:	Tuple				=config_and_inputs
								lowerCamelCase__	:	Any				={'''pixel_values''': pixel_values}
								return config, inputs_dict





@require_torch
class  __SCREAMING_SNAKE_CASE			(   lowerCAmelCase_	,     lowerCAmelCase_	,     unittest.TestCase     ):


					'''simple docstring'''





					_a         =							(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
					_a         =							(
					    {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification}
					    if is_torch_available()
					    else {}
					)

					_a         =							False
					_a         =							False
					_a         =							False
					_a         =							False



					def 	snake_case ( self							:						Any  )->      Union[str, Any]:
								lowerCamelCase__	:	str				=MobileNetVaModelTester(self  )
								lowerCamelCase__	:	int				=MobileNetVaConfigTester(self,						config_class=lowerCamelCase,						has_text_modality=lowerCamelCase  )



					def 	snake_case ( self							:						Optional[int]  )->      Optional[int]:
								self.config_tester.run_common_tests()



					@unittest.skip(reason='''MobileNetV1 does not use inputs_embeds'''  )
					def 	snake_case ( self							:						Dict  )->      Union[str, Any]:
								pass



					@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings'''  )
					def 	snake_case ( self							:						Dict  )->      str:
								pass



					@unittest.skip(reason='''MobileNetV1 does not output attentions'''  )
					def 	snake_case ( self							:						List[str]  )->      Tuple:
								pass



					def 	snake_case ( self							:						Dict  )->      Optional[Any]:
								lowerCamelCase__			,     lowerCamelCase__	:	int				=self.model_tester.prepare_config_and_inputs_for_common()

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

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



					def 	snake_case ( self							:						Optional[Any]  )->      List[str]:
								lowerCamelCase__	:	Any				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase  )



					def 	snake_case ( self							:						int  )->      List[Any]:
								def check_hidden_states_output(lowerCamelCase							:						Dict,						lowerCamelCase							:						Union[str, Any],						lowerCamelCase							:						List[str]  ):
											lowerCamelCase__	:	int				=model_class(lowerCamelCase  )
											model.to(lowerCamelCase  )
											model.eval()

											with torch.no_grad():
														lowerCamelCase__	:	Optional[Any]				=model(**self._prepare_for_class(lowerCamelCase,						lowerCamelCase  )  )

											lowerCamelCase__	:	List[Any]				=outputs.hidden_states

											lowerCamelCase__	:	Any				=26
											self.assertEqual(len(lowerCamelCase  ),						lowerCamelCase  )

								lowerCamelCase__			,     lowerCamelCase__	:	List[Any]				=self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											lowerCamelCase__	:	int				=True
											check_hidden_states_output(lowerCamelCase,						lowerCamelCase,						lowerCamelCase  )

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

											check_hidden_states_output(lowerCamelCase,						lowerCamelCase,						lowerCamelCase  )



					def 	snake_case ( self							:						Any  )->      List[Any]:
								lowerCamelCase__	:	List[str]				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowerCamelCase  )




					@slow
					def 	snake_case ( self							:						Optional[Any]  )->      List[Any]:
								for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCamelCase__	:	Optional[int]				=MobileNetVaModel.from_pretrained(lowerCamelCase  )
											self.assertIsNotNone(lowerCamelCase  )






def   snake_case__     (	):
			"""simple docstring"""
			lowerCamelCase__	:	Optional[Any]				=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
			return image

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


					'''simple docstring'''





					@cached_property
					def 	snake_case ( self							:						List[Any]  )->      List[Any]:
								return (
								    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224'''  ) if is_vision_available() else None
								)




					@slow
					def 	snake_case ( self							:						Optional[int]  )->      Any:
								lowerCamelCase__	:	Dict				=MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''  ).to(lowerCamelCase  )

								lowerCamelCase__	:	Any				=self.default_image_processor
								lowerCamelCase__	:	Tuple				=prepare_img()
								lowerCamelCase__	:	List[str]				=image_processor(images=lowerCamelCase,						return_tensors='''pt'''  ).to(lowerCamelCase  )

								# forward pass
								with torch.no_grad():
											lowerCamelCase__	:	str				=model(**lowerCamelCase  )

								# verify the logits
								lowerCamelCase__	:	str				=torch.Size((1, 1001)  )
								self.assertEqual(outputs.logits.shape,						lowerCamelCase  )

								lowerCamelCase__	:	Dict				=torch.tensor([-4.1_739, -1.1_233, 3.1_205]  ).to(lowerCamelCase  )

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









"""simple docstring"""






def   snake_case__     (	__lowerCamelCase       :					list[int]			):
			"""simple docstring"""
			if not numbers:
						return 0

			if not isinstance(__lowerCamelCase							,	(list, tuple)			) or not all(
			    isinstance(__lowerCamelCase							,	__lowerCamelCase			) for number in numbers			):
						raise ValueError('''numbers must be an iterable of integers'''			)

			lowerCamelCase__	:	Any				=numbers[0]

			for i in range(1							,	len(__lowerCamelCase			)			):
						# update the maximum and minimum subarray products
						lowerCamelCase__	:	Dict				=numbers[i]
						if number < 0:
									lowerCamelCase__			,     lowerCamelCase__	:	List[Any]				=min_till_now, max_till_now
						lowerCamelCase__	:	Optional[int]				=max(__lowerCamelCase							,	max_till_now * number			)
						lowerCamelCase__	:	Dict				=min(__lowerCamelCase							,	min_till_now * number			)

						# update the maximum product found till now
						lowerCamelCase__	:	Tuple				=max(__lowerCamelCase							,	__lowerCamelCase			)

			return max_prod




