


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



		"""simple docstring"""

		def 					__A   ( self	)  -> Tuple:







					'''simple docstring'''







					lowerCamelCase    		=  self.config_class(**self.inputs_dict	)
					self.parent.assertTrue(hasattr(A		,      """tf_padding"""	)	)
					self.parent.assertTrue(hasattr(A		,      """depth_multiplier"""	)	)







class        __lowercase       :



		"""simple docstring"""

		def __init__( self		,      A		,      A=13		,      A=3		,      A=32		,      A=0.25		,      A=8		,      A=True		,      A=10_24		,      A=32		,      A="relu6"		,      A=0.1		,      A=0.02		,      A=True		,      A=True		,      A=10		,      A=None		,      )  -> Tuple:







					'''simple docstring'''







					lowerCamelCase    		=  parent
					lowerCamelCase    		=  batch_size
					lowerCamelCase    		=  num_channels
					lowerCamelCase    		=  image_size
					lowerCamelCase    		=  depth_multiplier
					lowerCamelCase    		=  min_depth
					lowerCamelCase    		=  tf_padding
					lowerCamelCase    		=  int(last_hidden_size * depth_multiplier	)
					lowerCamelCase    		=  output_stride
					lowerCamelCase    		=  hidden_act
					lowerCamelCase    		=  classifier_dropout_prob
					lowerCamelCase    		=  use_labels
					lowerCamelCase    		=  is_training
					lowerCamelCase    		=  num_labels
					lowerCamelCase    		=  initializer_range
					lowerCamelCase    		=  scope







		def 					__A   ( self	)  -> Tuple:







					'''simple docstring'''







					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 					__A   ( self	)  -> Any:







					'''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 					__A   ( self		,      A		,      A		,      A		,      A	)  -> Optional[Any]:







					'''simple docstring'''







					lowerCamelCase    		=  MobileNetVaModel(config=A	)
					model.to(A	)
					model.eval()
					lowerCamelCase    		=  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 					__A   ( self		,      A		,      A		,      A		,      A	)  -> str:







					'''simple docstring'''







					lowerCamelCase    		=  self.num_labels
					lowerCamelCase    		=  MobileNetVaForImageClassification(A	)
					model.to(A	)
					model.eval()
					lowerCamelCase    		=  model(A		,      labels=A	)
					self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)	)







		def 					__A   ( self	)  -> Union[str, Any]:







					'''simple docstring'''







					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        __lowercase       (							a_			,							a_			,							unittest.TestCase  ):



		"""simple docstring"""

		UpperCamelCase     :					Union[str, Any]			       =		(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
		UpperCamelCase     :					Dict			       =		(
		    {"feature-extraction": MobileNetVaModel, "image-classification": MobileNetVaForImageClassification}
		    if is_torch_available()
		    else {}
		)

		UpperCamelCase     :					str			       =		False
		UpperCamelCase     :					str			       =		False
		UpperCamelCase     :					Union[str, Any]			       =		False
		UpperCamelCase     :					int			       =		False







		def 					__A   ( self	)  -> int:







					'''simple docstring'''







					lowerCamelCase    		=  MobileNetVaModelTester(self	)
					lowerCamelCase    		=  MobileNetVaConfigTester(self		,      config_class=A		,      has_text_modality=A	)







		def 					__A   ( self	)  -> Optional[int]:







					'''simple docstring'''







					self.config_tester.run_common_tests()







		@unittest.skip(reason="""MobileNetV1 does not use inputs_embeds"""	)
		def 					__A   ( self	)  -> str:







					'''simple docstring'''







					pass







		@unittest.skip(reason="""MobileNetV1 does not support input and output embeddings"""	)
		def 					__A   ( self	)  -> Dict:







					'''simple docstring'''







					pass







		@unittest.skip(reason="""MobileNetV1 does not output attentions"""	)
		def 					__A   ( self	)  -> List[Any]:







					'''simple docstring'''







					pass







		def 					__A   ( self	)  -> int:







					'''simple docstring'''







					lowerCamelCase		,		lowerCamelCase    		=  self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								lowerCamelCase    		=  model_class(A	)
								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]		,      A	)







		def 					__A   ( self	)  -> Union[str, Any]:







					'''simple docstring'''







					lowerCamelCase    		=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A	)







		def 					__A   ( self	)  -> Tuple:







					'''simple docstring'''







					def check_hidden_states_output(A		,      A		,      A	):
								lowerCamelCase    		=  model_class(A	)
								model.to(A	)
								model.eval()

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

								lowerCamelCase    		=  outputs.hidden_states

								lowerCamelCase    		=  26
								self.assertEqual(len(A	)		,      A	)

					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(A		,      A		,      A	)

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

								check_hidden_states_output(A		,      A		,      A	)







		def 					__A   ( self	)  -> List[Any]:







					'''simple docstring'''







					lowerCamelCase    		=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*A	)







		@slow
		def 					__A   ( self	)  -> str:







					'''simple docstring'''







					for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								lowerCamelCase    		=  MobileNetVaModel.from_pretrained(A	)
								self.assertIsNotNone(A	)






def 	__lowerCamelCase						(							):





			'''simple docstring'''




			lowerCamelCase    		=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
			return image







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



		"""simple docstring"""

		@cached_property
		def 					__A   ( self	)  -> Dict:







					'''simple docstring'''







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







		@slow
		def 					__A   ( self	)  -> List[Any]:







					'''simple docstring'''







					lowerCamelCase    		=  MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v1_1.0_224"""	).to(A	)

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

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

					# verify the logits
					lowerCamelCase    		=  torch.Size((1, 10_01)	)
					self.assertEqual(outputs.logits.shape		,      A	)

					lowerCamelCase    		=  torch.tensor([-4.1739, -1.1233, 3.1205]	).to(A	)

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





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


UpperCAmelCase   :    Union[str, Any]     =		logging.get_logger(__name__)

UpperCAmelCase   :    List[Any]     =		{
    "SCUT-DLVCLab/lilt-roberta-en-base": (
        "https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json"
    ),
}







class        __lowercase       (							a_  ):



		"""simple docstring"""

		UpperCamelCase     :					List[str]			       =		"lilt"







		def __init__( self		,      A=3_05_22		,      A=7_68		,      A=12		,      A=12		,      A=30_72		,      A="gelu"		,      A=0.1		,      A=0.1		,      A=5_12		,      A=2		,      A=0.02		,      A=1e-1_2		,      A=0		,      A="absolute"		,      A=None		,      A=4		,      A=10_24		,      **A		,      )  -> List[Any]:







					'''simple docstring'''







					super().__init__(pad_token_id=A		,      **A	)

					lowerCamelCase    		=  vocab_size
					lowerCamelCase    		=  hidden_size
					lowerCamelCase    		=  num_hidden_layers
					lowerCamelCase    		=  num_attention_heads
					lowerCamelCase    		=  hidden_act
					lowerCamelCase    		=  intermediate_size
					lowerCamelCase    		=  hidden_dropout_prob
					lowerCamelCase    		=  attention_probs_dropout_prob
					lowerCamelCase    		=  max_position_embeddings
					lowerCamelCase    		=  type_vocab_size
					lowerCamelCase    		=  initializer_range
					lowerCamelCase    		=  layer_norm_eps
					lowerCamelCase    		=  position_embedding_type
					lowerCamelCase    		=  classifier_dropout
					lowerCamelCase    		=  channel_shrink_ratio
					lowerCamelCase    		=  max_ad_position_embeddings

