





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			(      __lowercase			):






				def 			snake_case_    (  self					):

										"""simple docstring"""




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





class 				a			:






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=13			, _lowerCAmelCase=3			, _lowerCAmelCase=32			, _lowerCAmelCase=0.25			, _lowerCAmelCase=8			, _lowerCAmelCase=True			, _lowerCAmelCase=1024			, _lowerCAmelCase=32			, _lowerCAmelCase="relu6"			, _lowerCAmelCase=0.1			, _lowerCAmelCase=0.02			, _lowerCAmelCase=True			, _lowerCAmelCase=True			, _lowerCAmelCase=10			, _lowerCAmelCase=None			, ):

										"""simple docstring"""




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






				def 			snake_case_    (  self					):

										"""simple docstring"""




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

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   None
										__SCREAMING_SNAKE_CASE:				List[Any]           =   None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE:				List[str]           =   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 			snake_case_    (  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 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




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

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   self.num_labels
										__SCREAMING_SNAKE_CASE:				Dict           =   MobileNetVaForImageClassification(_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Tuple           =   model(_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_labels)					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   self.prepare_config_and_inputs()
										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				str           =   config_and_inputs
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   {'''pixel_values''': pixel_values}
										return config, inputs_dict





@require_torch
class 				a			(      __lowercase   ,__lowercase   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   List[str]		      =     (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE__     :   Tuple		      =     (
				    {'''feature-extraction''': MobileNetVaModel, '''image-classification''': MobileNetVaForImageClassification}
				    if is_torch_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE__     :   Optional[Any]		      =     False
				SCREAMING_SNAKE_CASE__     :   Any		      =     False
				SCREAMING_SNAKE_CASE__     :   int		      =     False
				SCREAMING_SNAKE_CASE__     :   List[Any]		      =     False






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   MobileNetVaModelTester(self					)
										__SCREAMING_SNAKE_CASE:				List[Any]           =   MobileNetVaConfigTester(self			, config_class=_lowerCAmelCase			, has_text_modality=_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										self.config_tester.run_common_tests()






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

										"""simple docstring"""




										pass






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

										"""simple docstring"""




										pass






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

										"""simple docstring"""




										pass






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				Any           =   self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__SCREAMING_SNAKE_CASE:				List[str]           =   model_class(_lowerCAmelCase					)
																__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   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:				List[str]           =   ['''pixel_values''']
																self.assertListEqual(arg_names[:1]			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										def check_hidden_states_output(_lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):
																__SCREAMING_SNAKE_CASE:				str           =   model_class(_lowerCAmelCase					)
																model.to(_lowerCAmelCase					)
																model.eval()

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

																__SCREAMING_SNAKE_CASE:				Tuple           =   outputs.hidden_states

																__SCREAMING_SNAKE_CASE:				str           =   26
																self.assertEqual(len(_lowerCAmelCase					)			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				int           =   self.model_tester.prepare_config_and_inputs_for_common()

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

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

																check_hidden_states_output(_lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase					)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




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






def 		lowerCAmelCase  (	)      ->    int:

						"""simple docstring"""





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





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






				@cached_property
				def 			snake_case_    (  self					):

										"""simple docstring"""




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






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''					).to(_lowerCAmelCase					)

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

										# forward pass
										with torch.no_grad():
																__SCREAMING_SNAKE_CASE:				List[str]           =   model(**_lowerCAmelCase					)

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

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   torch.tensor([-4.1739, -1.1233, 3.1205]					).to(_lowerCAmelCase					)

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










import baseaa
import io
import json
import os
from copy import deepcopy

from ..optimizer import AcceleratedOptimizer
from ..scheduler import AcceleratedScheduler





class 				a			:






				def __init__(  self			, _lowerCAmelCase					):

										"""simple docstring"""




										if isinstance(_lowerCAmelCase			, _lowerCAmelCase					):
																# Don't modify user's data should they want to reuse it (e.g. in tests), because once we
																# modified it, it will not be accepted here again, since `auto` values would have been overridden
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   deepcopy(_lowerCAmelCase					)
										elif os.path.exists(_lowerCAmelCase					):
																with io.open(_lowerCAmelCase			, '''r'''			, encoding='''utf-8'''					) as f:
																						__SCREAMING_SNAKE_CASE:				int           =   json.load(_lowerCAmelCase					)
										else:
																try:
																						__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   baseaa.urlsafe_baadecode(_lowerCAmelCase					).decode('''utf-8'''					)
																						__SCREAMING_SNAKE_CASE:				List[Any]           =   json.loads(_lowerCAmelCase					)
																except (UnicodeDecodeError, AttributeError, ValueError):
																						raise ValueError(
																						    f"""Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}"""					)

										__SCREAMING_SNAKE_CASE:				str           =   config

										self.set_stage_and_offload()






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   self.get_value('''zero_optimization.stage'''			, -1					)

										# offload
										__SCREAMING_SNAKE_CASE:				Dict           =   False
										if self.is_zeroa() or self.is_zeroa():
																__SCREAMING_SNAKE_CASE:				str           =   set(['''cpu''', '''nvme''']					)
																__SCREAMING_SNAKE_CASE:				Tuple           =   set(
																    [
																        self.get_value('''zero_optimization.offload_optimizer.device'''					),
																        self.get_value('''zero_optimization.offload_param.device'''					),
																    ]					)
																if len(offload_devices & offload_devices_valid					) > 0:
																						__SCREAMING_SNAKE_CASE:				Dict           =   True






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   self.config

										# find the config node of interest if it exists
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   ds_key_long.split('''.'''					)
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   nodes.pop()
										for node in nodes:
																__SCREAMING_SNAKE_CASE:				int           =   config.get(_lowerCAmelCase					)
																if config is None:
																						return None, ds_key

										return config, ds_key






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=None					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				List[Any]           =   self.find_config_node(_lowerCAmelCase					)
										if config is None:
																return default
										return config.get(_lowerCAmelCase			, _lowerCAmelCase					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase=False					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   self.config

										# find the config node of interest if it exists
										__SCREAMING_SNAKE_CASE:				str           =   ds_key_long.split('''.'''					)
										for node in nodes:
																__SCREAMING_SNAKE_CASE:				Tuple           =   config
																__SCREAMING_SNAKE_CASE:				List[str]           =   config.get(_lowerCAmelCase					)
																if config is None:
																						if must_exist:
																												raise ValueError(f"""Can't find {ds_key_long} entry in the config: {self.config}"""					)
																						else:
																												return

        # if found remove it
										if parent_config is not None:
																parent_config.pop(_lowerCAmelCase					)






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.get_value(_lowerCAmelCase					)
										return False if value is None else bool(_lowerCAmelCase					)






				def 			snake_case_    (  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   self.get_value(_lowerCAmelCase					)
										return False if value is None else not bool(_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return self._stage == 2






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return self._stage == 3






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return self._offload





class 				a			:






				def __init__(  self			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   engine






				def 			snake_case_    (  self			, _lowerCAmelCase			, **_lowerCAmelCase					):

										"""simple docstring"""




										self.engine.backward(_lowerCAmelCase			, **_lowerCAmelCase					)

										# Deepspeed's `engine.step` performs the following operations:
										# - gradient accumulation check
										# - gradient clipping
										# - optimizer step
										# - zero grad
										# - checking overflow
										# - lr_scheduler step (only if engine.lr_scheduler is not None)
										self.engine.step()
										# and this plugin overrides the above calls with no-ops when Accelerate runs under
										# Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple
										# training loop that works transparently under many training regimes.





class 				a			(      __lowercase			):






				def __init__(  self			, _lowerCAmelCase					):

										"""simple docstring"""




										super().__init__(_lowerCAmelCase			, device_placement=_lowerCAmelCase			, scaler=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				str           =   hasattr(self.optimizer			, '''overflow'''					)






				def 			snake_case_    (  self			, _lowerCAmelCase=None					):

										"""simple docstring"""




										pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed






				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										if self.__has_overflow__:
																return self.optimizer.overflow
										return False





class 				a			(      __lowercase			):






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										super().__init__(_lowerCAmelCase			, _lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed





class 				a			:






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=0.001			, _lowerCAmelCase=0			, **_lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   params
										__SCREAMING_SNAKE_CASE:				int           =   lr
										__SCREAMING_SNAKE_CASE:				List[str]           =   weight_decay
										__SCREAMING_SNAKE_CASE:				List[Any]           =   kwargs





class 				a			:






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=None			, _lowerCAmelCase=0			, **_lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   optimizer
										__SCREAMING_SNAKE_CASE:				int           =   total_num_steps
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   warmup_num_steps
										__SCREAMING_SNAKE_CASE:				str           =   kwargs



