
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 					lowercase__(							UpperCAmelCase				):



	"""simple docstring"""




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

class 					lowercase__:



	"""simple docstring"""




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

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

					lowercase_	    =   self.get_config()

					return config, pixel_values, labels, pixel_labels
	def 		_lowercase    (			self    :     int			)			->	Tuple:
					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 		_lowercase    (			self    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     List[str]			)			->	str:
					lowercase_	    =   MobileNetVaModel(config=SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			)
					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 		_lowercase    (			self    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     Any			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]			,				SCREAMING_SNAKE_CASE_    :     Dict			)			->	List[str]:
					lowercase_	    =   self.num_labels
					lowercase_	    =   MobileNetVaForImageClassification(SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_			)
					self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_labels)			)
	def 		_lowercase    (			self    :     Any			,				SCREAMING_SNAKE_CASE_    :     Any			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     Tuple			)			->	Dict:
					lowercase_	    =   self.num_labels
					lowercase_	    =   MobileNetVaForSemanticSegmentation(SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			)
					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_	    =   model(SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_			)
					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 		_lowercase    (			self    :     Optional[Any]			)			->	Dict:
					lowercase_	    =   self.prepare_config_and_inputs()
					lowercase_    ,      lowercase_    ,      lowercase_    ,      lowercase_	    =   config_and_inputs
					lowercase_	    =   {'''pixel_values''': pixel_values}
					return config, inputs_dict

@require_torch
class 					lowercase__(							UpperCAmelCase					,		UpperCAmelCase					,		unittest.TestCase				):



	"""simple docstring"""




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

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

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

									lowercase_	    =   ['''pixel_values''']
									self.assertListEqual(arg_names[:1]			,				SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     int			)			->	List[Any]:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     List[str]			)			->	List[Any]:
					def check_hidden_states_output(SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     List[str]			,				SCREAMING_SNAKE_CASE_    :     Tuple			):
									lowercase_	    =   model_class(SCREAMING_SNAKE_CASE_			)
									model.to(SCREAMING_SNAKE_CASE_			)
									model.eval()

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

									lowercase_	    =   outputs.hidden_states

									lowercase_	    =   1_6
									self.assertEqual(len(SCREAMING_SNAKE_CASE_			)			,				SCREAMING_SNAKE_CASE_			)

					lowercase_    ,      lowercase_	    =   self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									lowercase_	    =   True
									check_hidden_states_output(SCREAMING_SNAKE_CASE_			,				SCREAMING_SNAKE_CASE_			,				SCREAMING_SNAKE_CASE_			)

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

									check_hidden_states_output(SCREAMING_SNAKE_CASE_			,				SCREAMING_SNAKE_CASE_			,				SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     str			)			->	List[Any]:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     Tuple			)			->	Optional[Any]:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE_			)




	@slow
	def 		_lowercase    (			self    :     Tuple			)			->	Optional[int]:
					for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_	    =   MobileNetVaModel.from_pretrained(SCREAMING_SNAKE_CASE_			)
									self.assertIsNotNone(SCREAMING_SNAKE_CASE_			)




def 							a		(				):


				'''simple docstring'''
				lowercase_	    =   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
				return image


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



	"""simple docstring"""




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

					lowercase_	    =   self.default_image_processor
					lowercase_	    =   prepare_img()
					lowercase_	    =   image_processor(images=SCREAMING_SNAKE_CASE_			,				return_tensors='''pt'''			).to(SCREAMING_SNAKE_CASE_			)

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

					# verify the logits
					lowercase_	    =   torch.Size((1, 1_0_0_1)			)
					self.assertEqual(outputs.logits.shape			,				SCREAMING_SNAKE_CASE_			)

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

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




	@slow
	def 		_lowercase    (			self    :     Optional[int]			)			->	Union[str, Any]:
					lowercase_	    =   MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''			)
					lowercase_	    =   model.to(SCREAMING_SNAKE_CASE_			)

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

					lowercase_	    =   prepare_img()
					lowercase_	    =   image_processor(images=SCREAMING_SNAKE_CASE_			,				return_tensors='''pt'''			).to(SCREAMING_SNAKE_CASE_			)

					# forward pass
					with torch.no_grad():
									lowercase_	    =   model(**SCREAMING_SNAKE_CASE_			)
					lowercase_	    =   outputs.logits

					# verify the logits
					lowercase_	    =   torch.Size((1, 2_1, 6_5, 6_5)			)
					self.assertEqual(logits.shape			,				SCREAMING_SNAKE_CASE_			)

					lowercase_	    =   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=SCREAMING_SNAKE_CASE_			,				)

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









from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_		 =					logging.get_logger(__name__)

UpperCAmelCase_		 =					{"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""}


class 	lowerCamelCase__	(       _A):



 """simple docstring"""

 a__					: int =  "openai-gpt"
 a__					: Dict =  {
     "max_position_embeddings": "n_positions",
     "hidden_size": "n_embd",
     "num_attention_heads": "n_head",
     "num_hidden_layers": "n_layer",
 }

 def __init__(    self					:					Union[str, Any]			,						__lowerCAmelCase					:					int=4_04_78			,						__lowerCAmelCase					:					Tuple=5_12			,						__lowerCAmelCase					:					str=7_68			,						__lowerCAmelCase					:					List[Any]=12			,						__lowerCAmelCase					:					Any=12			,						__lowerCAmelCase					:					Dict="gelu"			,						__lowerCAmelCase					:					Union[str, Any]=0.1			,						__lowerCAmelCase					:					Optional[int]=0.1			,						__lowerCAmelCase					:					Any=0.1			,						__lowerCAmelCase					:					List[str]=1E-5			,						__lowerCAmelCase					:					Optional[int]=0.02			,						__lowerCAmelCase					:					Optional[Any]="cls_index"			,						__lowerCAmelCase					:					str=True			,						__lowerCAmelCase					:					int=None			,						__lowerCAmelCase					:					Optional[int]=True			,						__lowerCAmelCase					:					Optional[Any]=0.1			,						**__lowerCAmelCase					:					Tuple			,						)   ->	Optional[Any]:
    _A      =       vocab_size
    _A      =       n_positions
    _A      =       n_embd
    _A      =       n_layer
    _A      =       n_head
    _A      =       afn
    _A      =       resid_pdrop
    _A      =       embd_pdrop
    _A      =       attn_pdrop
    _A      =       layer_norm_epsilon
    _A      =       initializer_range
    _A      =       summary_type
    _A      =       summary_use_proj
    _A      =       summary_activation
    _A      =       summary_first_dropout
    _A      =       summary_proj_to_labels
    super().__init__(**__lowerCAmelCase      )



