



"""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       _a  ( SCREAMING_SNAKE_CASE__):



			def 				__lowercase					(			self							:				int		)							->					List[str]:
									snake_case   :			Union[str, Any]						    = self.config_class(**self.inputs_dict		)
									self.parent.assertTrue(hasattr(_lowercase   ,	"tf_padding"		)		)
									self.parent.assertTrue(hasattr(_lowercase   ,	"depth_multiplier"		)		)



class       _a  :



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



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

									snake_case   :			int						    = None
									snake_case   :			List[str]						    = None
									if self.use_labels:
															snake_case   :			List[str]						    = ids_tensor([self.batch_size]   ,	self.num_labels		)
															snake_case   :			List[str]						    = ids_tensor([self.batch_size, self.image_size, self.image_size]   ,	self.num_labels		)

									snake_case   :			Tuple						    = self.get_config()

									return config, pixel_values, labels, pixel_labels



			def 				__lowercase					(			self							:				Union[str, Any]		)							->					Optional[int]:
									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 				__lowercase					(			self							:				Optional[Any]   ,	_lowercase							:				Any   ,	_lowercase							:				List[str]   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Dict		)							->					int:
									snake_case   :			str						    = MobileNetVaModel(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			Optional[int]						    = model(_lowercase		)
									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 				__lowercase					(			self							:				List[str]   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Tuple		)							->					Tuple:
									snake_case   :			Union[str, Any]						    = self.num_labels
									snake_case   :			Optional[Any]						    = MobileNetVaForImageClassification(_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			Dict						    = model(_lowercase   ,	labels=_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.num_labels)		)



			def 				__lowercase					(			self							:				List[Any]		)							->					Any:
									snake_case   :			Optional[int]						    = self.prepare_config_and_inputs()
									snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			List[Any]						    = config_and_inputs
									snake_case   :			Tuple						    = {"pixel_values": pixel_values}
									return config, inputs_dict



@require_torch
class       _a  ( SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	unittest.TestCase):
			__magic_name__     					=     (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
			__magic_name__     					=     (
			    {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
			    if is_torch_available()
			    else {}
			)

			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False



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



			def 				__lowercase					(			self							:				str		)							->					Union[str, Any]:
									self.config_tester.run_common_tests()



			@unittest.skip(reason="MobileNetV1 does not use inputs_embeds"		)
			def 				__lowercase					(			self							:				List[str]		)							->					Dict:
									pass



			@unittest.skip(reason="MobileNetV1 does not support input and output embeddings"		)
			def 				__lowercase					(			self							:				Any		)							->					Dict:
									pass



			@unittest.skip(reason="MobileNetV1 does not output attentions"		)
			def 				__lowercase					(			self							:				List[Any]		)							->					Tuple:
									pass



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

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

															snake_case   :			List[Any]						    = ["pixel_values"]
															self.assertListEqual(arg_names[:1]   ,	_lowercase		)



			def 				__lowercase					(			self							:				str		)							->					int:
									snake_case   :			Optional[int]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase		)



			def 				__lowercase					(			self							:				List[Any]		)							->					List[Any]:
									def check_hidden_states_output(_lowercase							:				str   ,	_lowercase							:				Tuple   ,	_lowercase							:				str		):
															snake_case   :			Union[str, Any]						    = model_class(_lowercase		)
															model.to(_lowercase		)
															model.eval()

															with torch.no_grad():
																					snake_case   :			Any						    = model(**self._prepare_for_class(_lowercase   ,	_lowercase		)		)

															snake_case   :			Tuple						    = outputs.hidden_states

															snake_case   :			str						    = 26
															self.assertEqual(len(_lowercase		)   ,	_lowercase		)

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

									for model_class in self.all_model_classes:
															snake_case   :			str						    = True
															check_hidden_states_output(_lowercase   ,	_lowercase   ,	_lowercase		)

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

															check_hidden_states_output(_lowercase   ,	_lowercase   ,	_lowercase		)



			def 				__lowercase					(			self							:				Optional[int]		)							->					str:
									snake_case   :			Dict						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_lowercase		)



			@slow
			def 				__lowercase					(			self							:				Union[str, Any]		)							->					int:
									for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case   :			Optional[int]						    = MobileNetVaModel.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)



def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						snake_case   :			List[str]						    = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
						return image

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



			@cached_property
			def 				__lowercase					(			self							:				Optional[int]		)							->					Optional[int]:
									return (
									    MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224"		) if is_vision_available() else None
									)



			@slow
			def 				__lowercase					(			self							:				Dict		)							->					Tuple:
									snake_case   :			List[str]						    = MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224"		).to(_lowercase		)

									snake_case   :			int						    = self.default_image_processor
									snake_case   :			str						    = prepare_img()
									snake_case   :			Dict						    = image_processor(images=_lowercase   ,	return_tensors="pt"		).to(_lowercase		)

									# forward pass
									with torch.no_grad():
															snake_case   :			Union[str, Any]						    = model(**_lowercase		)

									# verify the logits
									snake_case   :			List[str]						    = torch.Size((1, 1001)		)
									self.assertEqual(outputs.logits.shape   ,	_lowercase		)

									snake_case   :			List[str]						    = torch.tensor([-4.1739, -1.1233, 3.1205]		).to(_lowercase		)

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








"""simple docstring"""







def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							int     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






						return int((input_a, input_a).count(0 ) != 0 )

def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						assert nand_gate(0     ,		0 ) == 1
						assert nand_gate(0     ,		1 ) == 1
						assert nand_gate(1     ,		0 ) == 1
						assert nand_gate(1     ,		1 ) == 0


if __name__ == "__main__":
			print(nand_gate(0, 0))
			print(nand_gate(0, 1))
			print(nand_gate(1, 0))
			print(nand_gate(1, 1))



