



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

						'''simple docstring'''




						def 	_A     (   self		):







													'''simple docstring'''

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







class        UpperCAmelCase_  :

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A=13				,							_A=3				,							_A=32				,							_A=0.2_5				,							_A=8				,							_A=True				,							_A=1_024				,							_A=32				,							_A="relu6"				,							_A=0.1				,							_A=0.0_2				,							_A=True				,							_A=True				,							_A=10				,							_A=None				,							):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= parent
													__SCREAMING_SNAKE_CASE									= batch_size
													__SCREAMING_SNAKE_CASE									= num_channels
													__SCREAMING_SNAKE_CASE									= image_size
													__SCREAMING_SNAKE_CASE									= depth_multiplier
													__SCREAMING_SNAKE_CASE									= min_depth
													__SCREAMING_SNAKE_CASE									= tf_padding
													__SCREAMING_SNAKE_CASE									= int(last_hidden_size * depth_multiplier		)
													__SCREAMING_SNAKE_CASE									= output_stride
													__SCREAMING_SNAKE_CASE									= hidden_act
													__SCREAMING_SNAKE_CASE									= classifier_dropout_prob
													__SCREAMING_SNAKE_CASE									= use_labels
													__SCREAMING_SNAKE_CASE									= is_training
													__SCREAMING_SNAKE_CASE									= num_labels
													__SCREAMING_SNAKE_CASE									= initializer_range
													__SCREAMING_SNAKE_CASE									= scope






						def 	_A     (   self		):







													'''simple docstring'''

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

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

													__SCREAMING_SNAKE_CASE									= self.get_config()

													return config, pixel_values, labels, pixel_labels






						def 	_A     (   self		):







													'''simple docstring'''

													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 	_A     (   self				,							_A				,							_A				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MobileNetVaModel(config=_a		)
													model.to(_a		)
													model.eval()
													__SCREAMING_SNAKE_CASE									= model(_a		)
													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 	_A     (   self				,							_A				,							_A				,							_A				,							_A		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''simple docstring'''

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







@require_torch
class        UpperCAmelCase_  (       _a   ,   _a   ,   unittest.TestCase	):

						'''simple docstring'''




						UpperCamelCase__      :			Dict      =  (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
						UpperCamelCase__      :			List[str]      =  (
						    {'''feature-extraction''': MobileNetVaModel, '''image-classification''': MobileNetVaForImageClassification}
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase__      :			Optional[int]      =  False
						UpperCamelCase__      :			Optional[int]      =  False
						UpperCamelCase__      :			Any      =  False
						UpperCamelCase__      :			Optional[Any]      =  False






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MobileNetVaModelTester(self		)
													__SCREAMING_SNAKE_CASE									= MobileNetVaConfigTester(self				,							config_class=_a				,							has_text_modality=_a		)






						def 	_A     (   self		):







													'''simple docstring'''

													self.config_tester.run_common_tests()






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







													'''simple docstring'''

													pass






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







													'''simple docstring'''

													pass






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







													'''simple docstring'''

													pass






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()

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

																				__SCREAMING_SNAKE_CASE									= ["pixel_values"]
																				self.assertListEqual(arg_names[:1]				,							_a		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_a		)






						def 	_A     (   self		):







													'''simple docstring'''

													def check_hidden_states_output(_A				,							_A				,							_A		):
																				__SCREAMING_SNAKE_CASE									= model_class(_a		)
																				model.to(_a		)
																				model.eval()

																				with torch.no_grad():
																											__SCREAMING_SNAKE_CASE									= model(**self._prepare_for_class(_a				,							_a		)		)

																				__SCREAMING_SNAKE_CASE									= outputs.hidden_states

																				__SCREAMING_SNAKE_CASE									= 26
																				self.assertEqual(len(_a		)				,							_a		)

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()

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

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

																				check_hidden_states_output(_a				,							_a				,							_a		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*_a		)






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

													for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				__SCREAMING_SNAKE_CASE									= MobileNetVaModel.from_pretrained(_a		)
																				self.assertIsNotNone(_a		)







def      __lowercase       (			)    ->			Union[str, Any]:
							__SCREAMING_SNAKE_CASE									= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
							return image

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

						'''simple docstring'''




						@cached_property
						def 	_A     (   self		):







													'''simple docstring'''

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






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v1_1.0_224'		).to(_a		)

													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(images=_a				,							return_tensors='pt'		).to(_a		)

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

													# verify the logits
													__SCREAMING_SNAKE_CASE									= torch.Size((1, 1_001)		)
													self.assertEqual(outputs.logits.shape				,							_a		)

													__SCREAMING_SNAKE_CASE									= torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5]		).to(_a		)

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











import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows






def      __lowercase       (			a__       )    ->			List[Any]:  # picklable for multiprocessing
							return i + 1






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def      __lowercase       (			)    ->			Any:
							with parallel_backend('spark'       ):
														assert ParallelBackendConfig.backend_name == "spark"

							__SCREAMING_SNAKE_CASE									= [1, 2, 3]
							with pytest.raises(a__       ):
														with parallel_backend('unsupported backend'       ):
																					map_nested(a__							,       a__							,       num_proc=2       )

							with pytest.raises(a__       ):
														with parallel_backend('unsupported backend'       ):
																					map_nested(a__							,       a__							,       num_proc=-1       )






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize('num_proc'							,       [2, -1]       )
def      __lowercase       (			a__       )    ->			Union[str, Any]:
							__SCREAMING_SNAKE_CASE									= [1, 2]
							__SCREAMING_SNAKE_CASE									= {'a': 1, 'b': 2}
							__SCREAMING_SNAKE_CASE									= {'a': [1, 2], 'b': [3, 4]}
							__SCREAMING_SNAKE_CASE									= {'a': {'1': 1}, 'b': 2}
							__SCREAMING_SNAKE_CASE									= {'a': 1, 'b': 2, 'c': 3, 'd': 4}
							__SCREAMING_SNAKE_CASE									= [2, 3]
							__SCREAMING_SNAKE_CASE									= {'a': 2, 'b': 3}
							__SCREAMING_SNAKE_CASE									= {'a': [2, 3], 'b': [4, 5]}
							__SCREAMING_SNAKE_CASE									= {'a': {'1': 2}, 'b': 3}
							__SCREAMING_SNAKE_CASE									= {'a': 2, 'b': 3, 'c': 4, 'd': 5}

							with parallel_backend('spark'       ):
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa






