





"""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, 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  __A       (  a      ):


				def _snake_case    (						self							):
								lowerCamelCase			=self.config_class(**self.inputs_dict							)
								self.parent.assertTrue(hasattr(UpperCAmelCase_				,	"""tf_padding"""							)							)
								self.parent.assertTrue(hasattr(UpperCAmelCase_				,	"""depth_multiplier"""							)							)




class  __A       :


				def __init__(						self				,	UpperCAmelCase_				,	UpperCAmelCase_=13				,	UpperCAmelCase_=3				,	UpperCAmelCase_=32				,	UpperCAmelCase_=0.2_5				,	UpperCAmelCase_=8				,	UpperCAmelCase_=8				,	UpperCAmelCase_=6				,	UpperCAmelCase_=32				,	UpperCAmelCase_=True				,	UpperCAmelCase_=True				,	UpperCAmelCase_=True				,	UpperCAmelCase_="relu6"				,	UpperCAmelCase_=1280				,	UpperCAmelCase_=0.1				,	UpperCAmelCase_=0.0_2				,	UpperCAmelCase_=True				,	UpperCAmelCase_=True				,	UpperCAmelCase_=10				,	UpperCAmelCase_=None				,	):
								lowerCamelCase			=parent
								lowerCamelCase			=batch_size
								lowerCamelCase			=num_channels
								lowerCamelCase			=image_size
								lowerCamelCase			=depth_multiplier
								lowerCamelCase			=depth_divisible_by
								lowerCamelCase			=min_depth
								lowerCamelCase			=expand_ratio
								lowerCamelCase			=tf_padding
								lowerCamelCase			=output_stride
								lowerCamelCase			=first_layer_is_expansion
								lowerCamelCase			=finegrained_output
								lowerCamelCase			=hidden_act
								lowerCamelCase			=last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier							)
								lowerCamelCase			=classifier_dropout_prob
								lowerCamelCase			=use_labels
								lowerCamelCase			=is_training
								lowerCamelCase			=num_labels
								lowerCamelCase			=initializer_range
								lowerCamelCase			=scope


				def _snake_case    (						self							):
								lowerCamelCase			=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

								lowerCamelCase			=self.get_config()

								return config, pixel_values, labels, pixel_labels


				def _snake_case    (						self							):
								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 _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_							):
								lowerCamelCase			=MobileNetVaModel(config=UpperCAmelCase_							)
								model.to(UpperCAmelCase_							)
								model.eval()
								lowerCamelCase			=model(UpperCAmelCase_							)
								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 _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_							):
								lowerCamelCase			=self.num_labels
								lowerCamelCase			=MobileNetVaForImageClassification(UpperCAmelCase_							)
								model.to(UpperCAmelCase_							)
								model.eval()
								lowerCamelCase			=model(UpperCAmelCase_				,	labels=UpperCAmelCase_							)
								self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.num_labels)							)


				def _snake_case    (						self				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_							):
								lowerCamelCase			=self.num_labels
								lowerCamelCase			=MobileNetVaForSemanticSegmentation(UpperCAmelCase_							)
								model.to(UpperCAmelCase_							)
								model.eval()
								lowerCamelCase			=model(UpperCAmelCase_							)
								self.parent.assertEqual(
								    result.logits.shape				,	(
								        self.batch_size,
								        self.num_labels,
								        self.image_size // self.output_stride,
								        self.image_size // self.output_stride,
								    )				,	)
								lowerCamelCase			=model(UpperCAmelCase_				,	labels=UpperCAmelCase_							)
								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 _snake_case    (						self							):
								lowerCamelCase			=self.prepare_config_and_inputs()
								lowerCamelCase	,					lowerCamelCase	,					lowerCamelCase	,					lowerCamelCase			=config_and_inputs
								lowerCamelCase			={"""pixel_values""": pixel_values}
								return config, inputs_dict




@require_torch
class  __A       (  a      ,   a      ,   unittest.TestCase      ):
				__A								=			(
				    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				__A								=			(
				    {
				        """feature-extraction""": MobileNetVaModel,
				        """image-classification""": MobileNetVaForImageClassification,
				        """image-segmentation""": MobileNetVaForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				__A								=			False
				__A								=			False
				__A								=			False
				__A								=			False


				def _snake_case    (						self							):
								lowerCamelCase			=MobileNetVaModelTester(self							)
								lowerCamelCase			=MobileNetVaConfigTester(self				,	config_class=UpperCAmelCase_				,	has_text_modality=UpperCAmelCase_							)


				def _snake_case    (						self							):
								self.config_tester.run_common_tests()


				@unittest.skip(reason="""MobileNetV2 does not use inputs_embeds"""							)
				def _snake_case    (						self							):
								pass


				@unittest.skip(reason="""MobileNetV2 does not support input and output embeddings"""							)
				def _snake_case    (						self							):
								pass


				@unittest.skip(reason="""MobileNetV2 does not output attentions"""							)
				def _snake_case    (						self							):
								pass


				def _snake_case    (						self							):
								lowerCamelCase	,					lowerCamelCase			=self.model_tester.prepare_config_and_inputs_for_common()

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

												lowerCamelCase			=["""pixel_values"""]
												self.assertListEqual(arg_names[:1]				,	UpperCAmelCase_							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase_							)


				def _snake_case    (						self							):
								def check_hidden_states_output(UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_							):
												lowerCamelCase			=model_class(UpperCAmelCase_							)
												model.to(UpperCAmelCase_							)
												model.eval()

												with torch.no_grad():
																lowerCamelCase			=model(**self._prepare_for_class(UpperCAmelCase_				,	UpperCAmelCase_							)							)

												lowerCamelCase			=outputs.hidden_states

												lowerCamelCase			=16
												self.assertEqual(len(UpperCAmelCase_							)				,	UpperCAmelCase_							)

								lowerCamelCase	,					lowerCamelCase			=self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												lowerCamelCase			=True
												check_hidden_states_output(UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_							)

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

												check_hidden_states_output(UpperCAmelCase_				,	UpperCAmelCase_				,	UpperCAmelCase_							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_							)


				def _snake_case    (						self							):
								lowerCamelCase			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*UpperCAmelCase_							)


				@slow
				def _snake_case    (						self							):
								for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCamelCase			=MobileNetVaModel.from_pretrained(UpperCAmelCase_							)
												self.assertIsNotNone(UpperCAmelCase_							)





def        _lowercase   (  )  ->					Optional[int]:
				lowerCamelCase			=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							):
								return (
								    MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""							) if is_vision_available() else None
								)


				@slow
				def _snake_case    (						self							):
								lowerCamelCase			=MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""							).to(UpperCAmelCase_							)

								lowerCamelCase			=self.default_image_processor
								lowerCamelCase			=prepare_img()
								lowerCamelCase			=image_processor(images=UpperCAmelCase_				,	return_tensors="""pt"""							).to(UpperCAmelCase_							)

								# forward pass
								with torch.no_grad():
												lowerCamelCase			=model(**UpperCAmelCase_							)

								# verify the logits
								lowerCamelCase			=torch.Size((1, 1001)							)
								self.assertEqual(outputs.logits.shape				,	UpperCAmelCase_							)

								lowerCamelCase			=torch.tensor([0.2_4_4_5, -1.1_9_9_3, 0.1_9_0_5]							).to(UpperCAmelCase_							)

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


				@slow
				def _snake_case    (						self							):
								lowerCamelCase			=MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""							)
								lowerCamelCase			=model.to(UpperCAmelCase_							)

								lowerCamelCase			=MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""							)

								lowerCamelCase			=prepare_img()
								lowerCamelCase			=image_processor(images=UpperCAmelCase_				,	return_tensors="""pt"""							).to(UpperCAmelCase_							)

								# forward pass
								with torch.no_grad():
												lowerCamelCase			=model(**UpperCAmelCase_							)
								lowerCamelCase			=outputs.logits

								# verify the logits
								lowerCamelCase			=torch.Size((1, 21, 65, 65)							)
								self.assertEqual(logits.shape				,	UpperCAmelCase_							)

								lowerCamelCase			=torch.tensor(
								    [
								        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
								        [[-2.1_5_9_5, -2.0_9_7_7, -2.3_7_4_1], [-2.4_2_2_6, -2.3_0_2_8, -2.6_8_3_5], [-2.7_8_1_9, -2.5_9_9_1, -2.7_7_0_6]],
								        [[4.2_0_5_8, 4.8_3_1_7, 4.7_6_3_8], [4.4_1_3_6, 5.0_3_6_1, 4.9_3_8_3], [4.5_0_2_8, 4.9_6_4_4, 4.8_7_3_4]],
								    ]				,	device=UpperCAmelCase_				,	)

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












from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


UpperCAmelCase__  :     Optional[Any]   			={
    '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''',
    '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''',
}

class  __A       (  a      ):
				__A								=			"""ernie_m"""
				__A								=			{"dropout": "classifier_dropout", "num_classes": "num_labels"}


				def __init__(						self				,	UpperCAmelCase_ = 250002				,	UpperCAmelCase_ = 768				,	UpperCAmelCase_ = 12				,	UpperCAmelCase_ = 12				,	UpperCAmelCase_ = 3072				,	UpperCAmelCase_ = "gelu"				,	UpperCAmelCase_ = 0.1				,	UpperCAmelCase_ = 0.1				,	UpperCAmelCase_ = 514				,	UpperCAmelCase_ = 0.0_2				,	UpperCAmelCase_ = 1				,	UpperCAmelCase_ = 1E-05				,	UpperCAmelCase_=None				,	UpperCAmelCase_=False				,	UpperCAmelCase_=0.0				,	**UpperCAmelCase_				,	):
								super().__init__(pad_token_id=UpperCAmelCase_				,	**UpperCAmelCase_							)
								lowerCamelCase			=vocab_size
								lowerCamelCase			=hidden_size
								lowerCamelCase			=num_hidden_layers
								lowerCamelCase			=num_attention_heads
								lowerCamelCase			=intermediate_size
								lowerCamelCase			=hidden_act
								lowerCamelCase			=hidden_dropout_prob
								lowerCamelCase			=attention_probs_dropout_prob
								lowerCamelCase			=max_position_embeddings
								lowerCamelCase			=initializer_range
								lowerCamelCase			=layer_norm_eps
								lowerCamelCase			=classifier_dropout
								lowerCamelCase			=is_decoder
								lowerCamelCase			=act_dropout





