



'''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 	lowerCamelCase_							(					UpperCamelCase__			):







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

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			self.config_class(**self.inputs_dict				)
													self.parent.assertTrue(hasattr(__A				,    '''tf_padding'''				)				)
													self.parent.assertTrue(hasattr(__A				,    '''depth_multiplier'''				)				)


class 	lowerCamelCase_							:







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

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			parent
													SCREAMING_SNAKE_CASE					:      Dict     					=			batch_size
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			num_channels
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			image_size
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			depth_multiplier
													SCREAMING_SNAKE_CASE					:      Dict     					=			min_depth
													SCREAMING_SNAKE_CASE					:      Any     					=			tf_padding
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			int(last_hidden_size * depth_multiplier				)
													SCREAMING_SNAKE_CASE					:      Dict     					=			output_stride
													SCREAMING_SNAKE_CASE					:      Any     					=			hidden_act
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			classifier_dropout_prob
													SCREAMING_SNAKE_CASE					:      int     					=			use_labels
													SCREAMING_SNAKE_CASE					:      Any     					=			is_training
													SCREAMING_SNAKE_CASE					:      List[str]     					=			num_labels
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			initializer_range
													SCREAMING_SNAKE_CASE					:      int     					=			scope







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

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

													SCREAMING_SNAKE_CASE					:      List[str]     					=			self.get_config()

													return config, pixel_values, labels, pixel_labels







						def 					__lowercase    (  self   :      Dict				):

													"""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 					__lowercase    (  self   :      Optional[int]				,    lowerCAmelCase__   :      Any				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Dict				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			MobileNetVaModel(config=__A				)
													model.to(__A				)
													model.eval()
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			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 					__lowercase    (  self   :      List[str]				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      str				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			self.num_labels
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			MobileNetVaForImageClassification(__A				)
													model.to(__A				)
													model.eval()
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			model(__A				,    labels=__A				)
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)				)







						def 					__lowercase    (  self   :      int				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			self.prepare_config_and_inputs()
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      List[str]     					=			config_and_inputs
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			{'''pixel_values''': pixel_values}
													return config, inputs_dict


@require_torch
class 	lowerCamelCase_							(					UpperCamelCase__    ,				UpperCamelCase__    ,				unittest.TestCase			):
						_lowerCAmelCase     :   Dict	     =				(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
						_lowerCAmelCase     :   List[str]	     =				(
						    {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification}
						    if is_torch_available()
						    else {}
						)

						_lowerCAmelCase     :   Dict	     =				False
						_lowerCAmelCase     :   Any	     =				False
						_lowerCAmelCase     :   Any	     =				False
						_lowerCAmelCase     :   Dict	     =				False







						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[str]     					=			MobileNetVaModelTester(self				)
													SCREAMING_SNAKE_CASE					:      Dict     					=			MobileNetVaConfigTester(self				,    config_class=__A				,    has_text_modality=__A				)







						def 					__lowercase    (  self   :      List[str]				):

													"""simple docstring"""
													self.config_tester.run_common_tests()







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

													"""simple docstring"""
													pass







						@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings'''				)
						def 					__lowercase    (  self   :      Optional[int]				):

													"""simple docstring"""
													pass







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

													"""simple docstring"""
													pass







						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.model_tester.prepare_config_and_inputs_for_common()

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

																				SCREAMING_SNAKE_CASE					:      Any     					=			['''pixel_values''']
																				self.assertListEqual(arg_names[:1]				,    __A				)







						def 					__lowercase    (  self   :      int				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__A				)







						def 					__lowercase    (  self   :      Optional[Any]				):

													"""simple docstring"""



													def check_hidden_states_output(lowerCAmelCase__   :      Optional[Any]				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      List[str]				):
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			model_class(__A				)
																				model.to(__A				)
																				model.eval()

																				with torch.no_grad():
																											SCREAMING_SNAKE_CASE					:      Optional[int]     					=			model(**self._prepare_for_class(__A				,    __A				)				)

																				SCREAMING_SNAKE_CASE					:      Tuple     					=			outputs.hidden_states

																				SCREAMING_SNAKE_CASE					:      Tuple     					=			26
																				self.assertEqual(len(__A				)				,    __A				)

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

													for model_class in self.all_model_classes:
																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			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					:      Optional[Any]     					=			True

																				check_hidden_states_output(__A				,    __A				,    __A				)







						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*__A				)







						@slow
						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""



													for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				SCREAMING_SNAKE_CASE					:      Any     					=			MobileNetVaModel.from_pretrained(__A				)
																				self.assertIsNotNone(__A				)


def 					UpperCAmelCase   (						):

							'''simple docstring'''





							SCREAMING_SNAKE_CASE					:      List[Any]     					=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
							return image






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







						@cached_property
						def 					__lowercase    (  self   :      Any				):

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







						@slow
						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''				).to(__A				)

													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self.default_image_processor
													SCREAMING_SNAKE_CASE					:      str     					=			prepare_img()
													SCREAMING_SNAKE_CASE					:      int     					=			image_processor(images=__A				,    return_tensors='''pt'''				).to(__A				)

													# forward pass
													with torch.no_grad():
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			model(**__A				)

													# verify the logits
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			torch.Size((1, 10_01)				)
													self.assertEqual(outputs.logits.shape				,    __A				)

													SCREAMING_SNAKE_CASE					:      str     					=			torch.tensor([-4.1739, -1.1233, 3.1205]				).to(__A				)

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









'''simple docstring'''



import os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
lowerCAmelCase_     :       Optional[Any]								=						'.'


if __name__ == "__main__":
							lowerCAmelCase_     :       Optional[Any]								=						os.path.join(REPO_PATH, 'utils/documentation_tests.txt')
							lowerCAmelCase_     :       Dict								=						[]
							lowerCAmelCase_     :       Any								=						[]
							with open(doctest_file_path) as fp:
														for line in fp:
																					lowerCAmelCase_     :       List[str]								=						line.strip()
																					lowerCAmelCase_     :       Optional[Any]								=						os.path.join(REPO_PATH, line)
																					if not (os.path.isfile(path) or os.path.isdir(path)):
																												non_existent_paths.append(line)
																					all_paths.append(path)
							if len(non_existent_paths) > 0:
														lowerCAmelCase_     :       Dict								=						'\n'.join(non_existent_paths)
														raise ValueError(f'`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}')
							if all_paths != sorted(all_paths):
														raise ValueError('Files in `utils/documentation_tests.txt` are not in alphabetical order.')




