






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


if is_vision_available():
						from PIL import Image

						from transformers import MobileNetVaImageProcessor






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



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






class 							__magic_name__     :







	"""simple docstring"""



	def __init__(							self  ,      _lowercase  ,      _lowercase=13  ,      _lowercase=3  ,      _lowercase=32  ,      _lowercase=0.25  ,      _lowercase=8  ,      _lowercase=8  ,      _lowercase=6  ,      _lowercase=32  ,      _lowercase=True  ,      _lowercase=True  ,      _lowercase=True  ,      _lowercase="relu6"  ,      _lowercase=1280  ,      _lowercase=0.1  ,      _lowercase=0.02  ,      _lowercase=True  ,      _lowercase=True  ,      _lowercase=10  ,      _lowercase=None  ,      )						->  int:
							lowercase_      :   Optional[Any]        =			parent
							lowercase_      :   Tuple        =			batch_size
							lowercase_      :   List[str]        =			num_channels
							lowercase_      :   Tuple        =			image_size
							lowercase_      :   List[Any]        =			depth_multiplier
							lowercase_      :   Optional[int]        =			depth_divisible_by
							lowercase_      :   str        =			min_depth
							lowercase_      :   Optional[int]        =			expand_ratio
							lowercase_      :   Optional[int]        =			tf_padding
							lowercase_      :   Tuple        =			output_stride
							lowercase_      :   Any        =			first_layer_is_expansion
							lowercase_      :   List[Any]        =			finegrained_output
							lowercase_      :   Any        =			hidden_act
							lowercase_      :   str        =			last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier       )
							lowercase_      :   Optional[int]        =			classifier_dropout_prob
							lowercase_      :   Dict        =			use_labels
							lowercase_      :   Tuple        =			is_training
							lowercase_      :   Any        =			num_labels
							lowercase_      :   List[str]        =			initializer_range
							lowercase_      :   int        =			scope
	def        lowerCamelCase__		(							self       )						->  Optional[int]:
							lowercase_      :   Union[str, Any]        =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

							lowercase_      :   str        =			None
							lowercase_      :   Optional[int]        =			None
							if self.use_labels:
													lowercase_      :   Any        =			ids_tensor([self.batch_size]  ,      self.num_labels       )
													lowercase_      :   str        =			ids_tensor([self.batch_size, self.image_size, self.image_size]  ,      self.num_labels       )

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

							return config, pixel_values, labels, pixel_labels
	def        lowerCamelCase__		(							self       )						->  int:
							return MobileNetVaConfig(
							    num_channels=self.num_channels  ,      image_size=self.image_size  ,      depth_multiplier=self.depth_multiplier  ,      depth_divisible_by=self.depth_divisible_by  ,      min_depth=self.min_depth  ,      expand_ratio=self.expand_ratio  ,      output_stride=self.output_stride  ,      first_layer_is_expansion=self.first_layer_is_expansion  ,      finegrained_output=self.finegrained_output  ,      hidden_act=self.hidden_act  ,      tf_padding=self.tf_padding  ,      classifier_dropout_prob=self.classifier_dropout_prob  ,      initializer_range=self.initializer_range  ,      )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase       )						->  str:
							lowercase_      :   Tuple        =			MobileNetVaModel(config=lowercase_       )
							model.to(lowercase_       )
							model.eval()
							lowercase_      :   Any        =			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,
							    )  ,      )
							self.parent.assertEqual(
							    result.pooler_output.shape  ,      (self.batch_size, self.last_hidden_size)  ,      )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase       )						->  int:
							lowercase_      :   Union[str, Any]        =			self.num_labels
							lowercase_      :   int        =			MobileNetVaForImageClassification(lowercase_       )
							model.to(lowercase_       )
							model.eval()
							lowercase_      :   str        =			model(lowercase_  ,      labels=lowercase_       )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.num_labels)       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase       )						->  Union[str, Any]:
							lowercase_      :   List[str]        =			self.num_labels
							lowercase_      :   Optional[int]        =			MobileNetVaForSemanticSegmentation(lowercase_       )
							model.to(lowercase_       )
							model.eval()
							lowercase_      :   Optional[int]        =			model(lowercase_       )
							self.parent.assertEqual(
							    result.logits.shape  ,      (
							        self.batch_size,
							        self.num_labels,
							        self.image_size // self.output_stride,
							        self.image_size // self.output_stride,
							    )  ,      )
							lowercase_      :   Tuple        =			model(lowercase_  ,      labels=lowercase_       )
							self.parent.assertEqual(
							    result.logits.shape  ,      (
							        self.batch_size,
							        self.num_labels,
							        self.image_size // self.output_stride,
							        self.image_size // self.output_stride,
							    )  ,      )







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






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







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      List[Any]        =  (
	    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE_					:      List[Any]        =  (
	    {
	        'feature-extraction': MobileNetVaModel,
	        'image-classification': MobileNetVaForImageClassification,
	        'image-segmentation': MobileNetVaForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	SCREAMING_SNAKE_CASE_					:      int        =  False
	SCREAMING_SNAKE_CASE_					:      List[Any]        =  False
	SCREAMING_SNAKE_CASE_					:      int        =  False
	SCREAMING_SNAKE_CASE_					:      Any        =  False
	def        lowerCamelCase__		(							self       )						->  List[str]:
							lowercase_      :   List[Any]        =			MobileNetVaModelTester(self       )
							lowercase_      :   Optional[int]        =			MobileNetVaConfigTester(self  ,      config_class=lowercase_  ,      has_text_modality=lowercase_       )
	def        lowerCamelCase__		(							self       )						->  List[str]:
							self.config_tester.run_common_tests()
	@unittest.skip(reason='MobileNetV2 does not use inputs_embeds'       )
	def        lowerCamelCase__		(							self       )						->  Dict:
							pass
	@unittest.skip(reason='MobileNetV2 does not support input and output embeddings'       )
	def        lowerCamelCase__		(							self       )						->  Optional[Any]:
							pass
	@unittest.skip(reason='MobileNetV2 does not output attentions'       )
	def        lowerCamelCase__		(							self       )						->  List[str]:
							pass
	def        lowerCamelCase__		(							self       )						->  int:
							lowercase_  ,      lowercase_      :   str        =			self.model_tester.prepare_config_and_inputs_for_common()

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

													lowercase_      :   Tuple        =			['pixel_values']
													self.assertListEqual(arg_names[:1]  ,      lowercase_       )
	def        lowerCamelCase__		(							self       )						->  Dict:
							lowercase_      :   Optional[Any]        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase_       )
	def        lowerCamelCase__		(							self       )						->  List[Any]:







							def check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase       ):
													lowercase_      :   List[str]        =			model_class(lowercase_       )
													model.to(lowercase_       )
													model.eval()

													with torch.no_grad():
																			lowercase_      :   int        =			model(**self._prepare_for_class(lowercase_  ,      lowercase_       )       )

													lowercase_      :   List[str]        =			outputs.hidden_states

													lowercase_      :   int        =			16
													self.assertEqual(len(lowercase_       )  ,      lowercase_       )

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

							for model_class in self.all_model_classes:
													lowercase_      :   Optional[Any]        =			True
													check_hidden_states_output(lowercase_  ,      lowercase_  ,      lowercase_       )

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

													check_hidden_states_output(lowercase_  ,      lowercase_  ,      lowercase_       )
	def        lowerCamelCase__		(							self       )						->  Optional[int]:
							lowercase_      :   int        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowercase_       )
	def        lowerCamelCase__		(							self       )						->  str:
							lowercase_      :   Any        =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_semantic_segmentation(*lowercase_       )







	@slow
	def        lowerCamelCase__		(							self       )						->  Dict:







							for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase_      :   List[Any]        =			MobileNetVaModel.from_pretrained(lowercase_       )
													self.assertIsNotNone(lowercase_       )






def 	_UpperCAmelCase    (  )			->			List[str]:





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






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







	"""simple docstring"""



	@cached_property
	def        lowerCamelCase__		(							self       )						->  Optional[int]:
							return (
							    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'       ) if is_vision_available() else None
							)
	@slow
	def        lowerCamelCase__		(							self       )						->  Dict:
							lowercase_      :   List[str]        =			MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'       ).to(lowercase_       )

							lowercase_      :   Dict        =			self.default_image_processor
							lowercase_      :   Optional[int]        =			prepare_img()
							lowercase_      :   Any        =			image_processor(images=lowercase_  ,      return_tensors='pt'       ).to(lowercase_       )

							# forward pass
							with torch.no_grad():
													lowercase_      :   Any        =			model(**lowercase_       )

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

							lowercase_      :   str        =			torch.tensor([0.24_45, -1.19_93, 0.19_05]       ).to(lowercase_       )

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







	@slow
	def        lowerCamelCase__		(							self       )						->  List[Any]:
							lowercase_      :   int        =			MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )
							lowercase_      :   Any        =			model.to(lowercase_       )

							lowercase_      :   int        =			MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'       )

							lowercase_      :   str        =			prepare_img()
							lowercase_      :   Dict        =			image_processor(images=lowercase_  ,      return_tensors='pt'       ).to(lowercase_       )

							# forward pass
							with torch.no_grad():
													lowercase_      :   Optional[int]        =			model(**lowercase_       )
							lowercase_      :   Optional[Any]        =			outputs.logits

							# verify the logits
							lowercase_      :   List[Any]        =			torch.Size((1, 21, 65, 65)       )
							self.assertEqual(logits.shape  ,      lowercase_       )

							lowercase_      :   Optional[int]        =			torch.tensor(
							    [
							        [[17.57_90, 17.75_81, 18.33_55], [18.32_57, 18.42_30, 18.89_73], [18.61_69, 18.86_50, 19.21_87]],
							        [[-2.15_95, -2.09_77, -2.37_41], [-2.42_26, -2.30_28, -2.68_35], [-2.78_19, -2.59_91, -2.77_06]],
							        [[4.20_58, 4.83_17, 4.76_38], [4.41_36, 5.03_61, 4.93_83], [4.50_28, 4.96_44, 4.87_34]],
							    ]  ,      device=lowercase_  ,      )

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








'''simple docstring'''

import json
import logging
import os
import socket

import git
import numpy as np
import torch


logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d -  %(message)s",
    datefmt="%m/%d/%Y %H:%M:%S",
    level=logging.INFO,
)
A:      Tuple  				=				logging.getLogger(__name__)






def 	_UpperCAmelCase    (  a		:	str							)			->			List[Any]:





						"""simple docstring"""
						lowercase_      :   List[str]        =			git.Repo(search_parent_directories=a							)
						lowercase_      :   Union[str, Any]        =			{
						    'repo_id': str(a							),
						    'repo_sha': str(repo.head.object.hexsha							),
						    'repo_branch': str(repo.active_branch							),
						}

						with open(os.path.join(a						,			'git_log.json'							)						,			'w'							) as f:
												json.dump(a						,			a						,			indent=4							)






def 	_UpperCAmelCase    (  a		:	str							)			->			Union[str, Any]:





						"""simple docstring"""
						if params.n_gpu <= 0:
												lowercase_      :   int        =			0
												lowercase_      :   Union[str, Any]        =			-1
												lowercase_      :   List[str]        =			True
												lowercase_      :   Optional[Any]        =			False
												return

						assert torch.cuda.is_available()

						logger.info('Initializing GPUs'							)
						if params.n_gpu > 1:
												assert params.local_rank != -1

												lowercase_      :   Dict        =			int(os.environ['WORLD_SIZE']							)
												lowercase_      :   Union[str, Any]        =			int(os.environ['N_GPU_NODE']							)
												lowercase_      :   Optional[int]        =			int(os.environ['RANK']							)

												# number of nodes / node ID
												lowercase_      :   int        =			params.world_size // params.n_gpu_per_node
												lowercase_      :   str        =			params.global_rank // params.n_gpu_per_node
												lowercase_      :   Dict        =			True

												assert params.n_nodes == int(os.environ['N_NODES']							)
												assert params.node_id == int(os.environ['NODE_RANK']							)

						# local job (single GPU)
						else:
												assert params.local_rank == -1

												lowercase_      :   str        =			1
												lowercase_      :   Dict        =			0
												lowercase_      :   Tuple        =			0
												lowercase_      :   List[Any]        =			0
												lowercase_      :   int        =			1
												lowercase_      :   Tuple        =			1
												lowercase_      :   str        =			False

						# sanity checks
						assert params.n_nodes >= 1
						assert 0 <= params.node_id < params.n_nodes
						assert 0 <= params.local_rank <= params.global_rank < params.world_size
						assert params.world_size == params.n_nodes * params.n_gpu_per_node

						# define whether this is the master process / if we are in multi-node distributed mode
						lowercase_      :   List[str]        =			params.node_id == 0 and params.local_rank == 0
						lowercase_      :   Optional[Any]        =			params.n_nodes > 1

						# summary
						lowercase_      :   int        =			f"--- Global rank: {params.global_rank} - "
						logger.info(PREFIX + 'Number of nodes: %i' % params.n_nodes							)
						logger.info(PREFIX + 'Node ID        : %i' % params.node_id							)
						logger.info(PREFIX + 'Local rank     : %i' % params.local_rank							)
						logger.info(PREFIX + 'World size     : %i' % params.world_size							)
						logger.info(PREFIX + 'GPUs per node  : %i' % params.n_gpu_per_node							)
						logger.info(PREFIX + 'Master         : %s' % str(params.is_master							)							)
						logger.info(PREFIX + 'Multi-node     : %s' % str(params.multi_node							)							)
						logger.info(PREFIX + 'Multi-GPU      : %s' % str(params.multi_gpu							)							)
						logger.info(PREFIX + 'Hostname       : %s' % socket.gethostname()							)

						# set GPU device
						torch.cuda.set_device(params.local_rank							)

						# initialize multi-GPU
						if params.multi_gpu:
												logger.info('Initializing PyTorch distributed'							)
												torch.distributed.init_process_group(
												    init_method='env://'						,			backend='nccl'						,			)






def 	_UpperCAmelCase    (  a		:	Dict							)			->			Optional[int]:





						"""simple docstring"""
						np.random.seed(args.seed							)
						torch.manual_seed(args.seed							)
						if args.n_gpu > 0:
												torch.cuda.manual_seed_all(args.seed							)
