



"""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        __SCREAMING_SNAKE_CASE	(      A_   ):


					'''simple docstring'''





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





class        __SCREAMING_SNAKE_CASE	:


					'''simple docstring'''





					def __init__(       self							:  List[str],  lowerCamelCase							:  Union[str, Any],  lowerCamelCase							:  Any=13,  lowerCamelCase							:  int=3,  lowerCamelCase							:  List[str]=32,  lowerCamelCase							:  Any=0.25,  lowerCamelCase							:  List[Any]=8,  lowerCamelCase							:  List[str]=True,  lowerCamelCase							:  Any=1024,  lowerCamelCase							:  List[Any]=32,  lowerCamelCase							:  Optional[int]="relu6",  lowerCamelCase							:  List[str]=0.1,  lowerCamelCase							:  Any=0.02,  lowerCamelCase							:  Union[str, Any]=True,  lowerCamelCase							:  Dict=True,  lowerCamelCase							:  Tuple=10,  lowerCamelCase							:  Tuple=None,  )->					Dict:
								lowerCamelCase__		:		Optional[int]			=parent
								lowerCamelCase__		:		List[str]			=batch_size
								lowerCamelCase__		:		Any			=num_channels
								lowerCamelCase__		:		List[Any]			=image_size
								lowerCamelCase__		:		List[str]			=depth_multiplier
								lowerCamelCase__		:		Optional[int]			=min_depth
								lowerCamelCase__		:		Optional[Any]			=tf_padding
								lowerCamelCase__		:		Tuple			=int(last_hidden_size * depth_multiplier     )
								lowerCamelCase__		:		List[Any]			=output_stride
								lowerCamelCase__		:		Union[str, Any]			=hidden_act
								lowerCamelCase__		:		Optional[Any]			=classifier_dropout_prob
								lowerCamelCase__		:		Dict			=use_labels
								lowerCamelCase__		:		Union[str, Any]			=is_training
								lowerCamelCase__		:		Optional[int]			=num_labels
								lowerCamelCase__		:		Optional[Any]			=initializer_range
								lowerCamelCase__		:		str			=scope



					def 		snake_case       (       self							:  List[str]     )->					List[str]:
								lowerCamelCase__		:		Optional[int]			=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

								lowerCamelCase__		:		Union[str, Any]			=self.get_config()

								return config, pixel_values, labels, pixel_labels



					def 		snake_case       (       self							:  List[str]     )->					Optional[Any]:
								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 		snake_case       (       self							:  int,  lowerCamelCase							:  Optional[Any],  lowerCamelCase							:  Optional[int],  lowerCamelCase							:  Optional[Any],  lowerCamelCase							:  Union[str, Any]     )->					List[str]:
								lowerCamelCase__		:		Optional[int]			=MobileNetVaModel(config=snake_case__     )
								model.to(snake_case__     )
								model.eval()
								lowerCamelCase__		:		int			=model(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,
								    ),  )



					def 		snake_case       (       self							:  List[Any],  lowerCamelCase							:  Any,  lowerCamelCase							:  Any,  lowerCamelCase							:  Dict,  lowerCamelCase							:  Optional[int]     )->					Any:
								lowerCamelCase__		:		int			=self.num_labels
								lowerCamelCase__		:		Tuple			=MobileNetVaForImageClassification(snake_case__     )
								model.to(snake_case__     )
								model.eval()
								lowerCamelCase__		:		Union[str, Any]			=model(snake_case__,  labels=snake_case__     )
								self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.num_labels)     )




					def 		snake_case       (       self							:  str     )->					List[str]:
								lowerCamelCase__		:		str			=self.prepare_config_and_inputs()
								lowerCamelCase__		:		List[Any]			=config_and_inputs
								lowerCamelCase__		:		Tuple			={"pixel_values": pixel_values}
								return config, inputs_dict





@require_torch
class        __SCREAMING_SNAKE_CASE	(      A_		,   A_		,   unittest.TestCase   ):


					'''simple docstring'''





					_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 		snake_case       (       self							:  List[Any]     )->					Any:
								lowerCamelCase__		:		List[Any]			=MobileNetVaModelTester(self     )
								lowerCamelCase__		:		int			=MobileNetVaConfigTester(self,  config_class=snake_case__,  has_text_modality=snake_case__     )



					def 		snake_case       (       self							:  List[str]     )->					Tuple:
								self.config_tester.run_common_tests()



					@unittest.skip(reason='''MobileNetV1 does not use inputs_embeds'''     )
					def 		snake_case       (       self							:  int     )->					Dict:
								pass



					@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings'''     )
					def 		snake_case       (       self							:  List[str]     )->					Tuple:
								pass



					@unittest.skip(reason='''MobileNetV1 does not output attentions'''     )
					def 		snake_case       (       self							:  Any     )->					Dict:
								pass



					def 		snake_case       (       self							:  Dict     )->					Any:
								lowerCamelCase__		:		Any			=self.model_tester.prepare_config_and_inputs_for_common()

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

											lowerCamelCase__		:		Optional[int]			=["pixel_values"]
											self.assertListEqual(arg_names[:1],  snake_case__     )



					def 		snake_case       (       self							:  Optional[Any]     )->					List[Any]:
								lowerCamelCase__		:		Tuple			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case__     )



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


								def check_hidden_states_output(lowerCamelCase							:  str,  lowerCamelCase							:  Dict,  lowerCamelCase							:  int     ):
											lowerCamelCase__		:		Optional[Any]			=model_class(snake_case__     )
											model.to(snake_case__     )
											model.eval()

											with torch.no_grad():
														lowerCamelCase__		:		Any			=model(**self._prepare_for_class(snake_case__,  snake_case__     )     )

											lowerCamelCase__		:		Optional[int]			=outputs.hidden_states

											lowerCamelCase__		:		int			=26
											self.assertEqual(len(snake_case__     ),  snake_case__     )

								lowerCamelCase__		:		Any			=self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											lowerCamelCase__		:		List[str]			=True
											check_hidden_states_output(snake_case__,  snake_case__,  snake_case__     )

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											lowerCamelCase__		:		Union[str, Any]			=True

											check_hidden_states_output(snake_case__,  snake_case__,  snake_case__     )



					def 		snake_case       (       self							:  List[str]     )->					Any:
								lowerCamelCase__		:		List[Any]			=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*snake_case__     )




					@slow
					def 		snake_case       (       self							:  Any     )->					Union[str, Any]:


								for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCamelCase__		:		str			=MobileNetVaModel.from_pretrained(snake_case__     )
											self.assertIsNotNone(snake_case__     )






def      snake_case__   (		):
			"""simple docstring"""
			lowerCamelCase__		:		Union[str, Any]			=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
			return image

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


					'''simple docstring'''





					@cached_property
					def 		snake_case       (       self							:  Optional[int]     )->					Tuple:
								return (
								    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224'''     ) if is_vision_available() else None
								)




					@slow
					def 		snake_case       (       self							:  Union[str, Any]     )->					Union[str, Any]:
								lowerCamelCase__		:		List[str]			=MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''     ).to(snake_case__     )

								lowerCamelCase__		:		Union[str, Any]			=self.default_image_processor
								lowerCamelCase__		:		Dict			=prepare_img()
								lowerCamelCase__		:		Tuple			=image_processor(images=snake_case__,  return_tensors='''pt'''     ).to(snake_case__     )

								# forward pass
								with torch.no_grad():
											lowerCamelCase__		:		List[Any]			=model(**snake_case__     )

								# verify the logits
								lowerCamelCase__		:		Optional[int]			=torch.Size((1, 1001)     )
								self.assertEqual(outputs.logits.shape,  snake_case__     )

								lowerCamelCase__		:		List[Any]			=torch.tensor([-4.1_739, -1.1_233, 3.1_205]     ).to(snake_case__     )

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






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__lowerCamelCase							=       {"""configuration_xglm""": ["""XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XGLMConfig"""]}

try:
   if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       ["""XGLMTokenizer"""]

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       ["""XGLMTokenizerFast"""]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       [
       """XGLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """XGLMForCausalLM""",
       """XGLMModel""",
       """XGLMPreTrainedModel""",
   ]


try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       [
       """FlaxXGLMForCausalLM""",
       """FlaxXGLMModel""",
       """FlaxXGLMPreTrainedModel""",
   ]


try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       [
       """TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """TFXGLMForCausalLM""",
       """TFXGLMModel""",
       """TFXGLMPreTrainedModel""",
   ]


if TYPE_CHECKING:
   from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

   try:
      if not is_sentencepiece_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_xglm import XGLMTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_xglm_fast import XGLMTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_xglm import (
          TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFXGLMForCausalLM,
          TFXGLMModel,
          TFXGLMPreTrainedModel,
      )


else:
   import sys

   __lowerCamelCase							=       _LazyModule(__name__, globals()["""__file__"""], _import_structure)





