


'''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 							__UpperCamelCase    (		snake_case_ ):




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





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





class 							__UpperCamelCase    :




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

						'''simple docstring'''





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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

						__a      :	Union[str, Any]    =  None
						__a      :	int    =  None
						if self.use_labels:
											__a      :	Tuple    =  ids_tensor([self.batch_size]						,    self.num_labels							)
											__a      :	Tuple    =  ids_tensor([self.batch_size, self.image_size, self.image_size]						,    self.num_labels							)

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

						return config, pixel_values, labels, pixel_labels




	def 				__UpperCAmelCase							(       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 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Any    =  MobileNetVaModel(config=_A							)
						model.to(_A							)
						model.eval()
						__a      :	List[Any]    =  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 				__UpperCAmelCase							(       self						,    __a						,    __a						,    __a						,    __a							):

						'''simple docstring'''





						__a      :	Any    =  self.num_labels
						__a      :	str    =  MobileNetVaForImageClassification(_A							)
						model.to(_A							)
						model.eval()
						__a      :	Union[str, Any]    =  model(_A						,    labels=_A							)
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)							)





	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.prepare_config_and_inputs()
						__a      :	Dict    =  config_and_inputs
						__a      :	List[str]    =  {'pixel_values': pixel_values}
						return config, inputs_dict





@require_torch
class 							__UpperCamelCase    (		snake_case_		,				snake_case_		,				unittest.TestCase ):
	A_           =   (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
	A_           =   (
	    {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification}
	    if is_torch_available()
	    else {}
	)

	A_           =   False
	A_           =   False
	A_           =   False
	A_           =   False




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  MobileNetVaModelTester(self							)
						__a      :	Tuple    =  MobileNetVaConfigTester(self						,    config_class=_A						,    has_text_modality=_A							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




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

						'''simple docstring'''





						pass




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

						'''simple docstring'''





						pass




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

						'''simple docstring'''





						pass




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  self.model_tester.prepare_config_and_inputs_for_common()

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

											__a      :	int    =  ['pixel_values']
											self.assertListEqual(arg_names[:1]						,    _A							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_A							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						def check_hidden_states_output(__a						,    __a						,    __a							):
											__a      :	Dict    =  model_class(_A							)
											model.to(_A							)
											model.eval()

											with torch.no_grad():
																__a      :	Optional[Any]    =  model(**self._prepare_for_class(_A						,    _A							)							)

											__a      :	str    =  outputs.hidden_states

											__a      :	Dict    =  26
											self.assertEqual(len(_A							)						,    _A							)

						__a      :	Union[str, Any]    =  self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__a      :	Union[str, Any]    =  True
											check_hidden_states_output(_A						,    _A						,    _A							)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											__a      :	Optional[int]    =  True

											check_hidden_states_output(_A						,    _A						,    _A							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[int]    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*_A							)





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





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






def   lowerCamelCase							():
					__a      :	str    =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
					return image





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




	@cached_property
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





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





	@slow
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Tuple    =  MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v1_1.0_224'							).to(_A							)

						__a      :	int    =  self.default_image_processor
						__a      :	List[Any]    =  prepare_img()
						__a      :	List[Any]    =  image_processor(images=_A						,    return_tensors='pt'							).to(_A							)

						# forward pass
						with torch.no_grad():
											__a      :	int    =  model(**_A							)

						# verify the logits
						__a      :	Dict    =  torch.Size((1, 1001)							)
						self.assertEqual(outputs.logits.shape						,    _A							)

						__a      :	Tuple    =  torch.tensor([-4.1739, -1.1233, 3.1205]							).to(_A							)

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







'''simple docstring'''



from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


__lowercase     :						List[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 							__UpperCamelCase    (		lowerCAmelCase_ ):
	A_           =   "ernie_m"
	A_           =   {"dropout": "classifier_dropout", "num_classes": "num_labels"}




	def __init__(       self						,    __a = 25_0002						,    __a = 768						,    __a = 12						,    __a = 12						,    __a = 3072						,    __a = "gelu"						,    __a = 0.1						,    __a = 0.1						,    __a = 514						,    __a = 0.02						,    __a = 1						,    __a = 1E-0_5						,    __a=None						,    __a=False						,    __a=0.0						,    **__a						,    ):

						'''simple docstring'''





						super().__init__(pad_token_id=__a						,    **__a							)
						__a      :	int    =  vocab_size
						__a      :	Dict    =  hidden_size
						__a      :	str    =  num_hidden_layers
						__a      :	Dict    =  num_attention_heads
						__a      :	List[str]    =  intermediate_size
						__a      :	Union[str, Any]    =  hidden_act
						__a      :	List[Any]    =  hidden_dropout_prob
						__a      :	str    =  attention_probs_dropout_prob
						__a      :	Any    =  max_position_embeddings
						__a      :	int    =  initializer_range
						__a      :	Dict    =  layer_norm_eps
						__a      :	int    =  classifier_dropout
						__a      :	Dict    =  is_decoder
						__a      :	int    =  act_dropout



