



'''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       __magic_name__		(       __snake_case  ):







							def 				__lowercase     (				self   :			List[str]						):
									_a	: Tuple								=				self.config_class(**self.inputs_dict						)
									self.parent.assertTrue(hasattr(_UpperCAmelCase      ,'tf_padding'						)						)
									self.parent.assertTrue(hasattr(_UpperCAmelCase      ,'depth_multiplier'						)						)

class       __magic_name__		:







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







							def 				__lowercase     (				self   :			Tuple						):
									_a	: str								=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

									_a	: Dict								=				self.get_config()

									return config, pixel_values, labels, pixel_labels







							def 				__lowercase     (				self   :			Tuple						):
									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 				__lowercase     (				self   :			str      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			Dict						):
									_a	: Dict								=				MobileNetVaModel(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: int								=				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,
									    )      ,)







							def 				__lowercase     (				self   :			List[str]      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			Optional[int]						):
									_a	: Any								=				self.num_labels
									_a	: List[str]								=				MobileNetVaForImageClassification(_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: Union[str, Any]								=				model(_UpperCAmelCase      ,labels=_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)						)







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

@require_torch
class       __magic_name__		(       __snake_case     ,		__snake_case     ,		unittest.TestCase  ):
							lowerCAmelCase			:       Tuple  							=   (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
							lowerCAmelCase			:       Dict  							=   (
							    {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification}
							    if is_torch_available()
							    else {}
							)

							lowerCAmelCase			:       Any  							=   False
							lowerCAmelCase			:       int  							=   False
							lowerCAmelCase			:       List[str]  							=   False
							lowerCAmelCase			:       Optional[Any]  							=   False







							def 				__lowercase     (				self   :			Dict						):
									_a	: List[str]								=				MobileNetVaModelTester(self						)
									_a	: Union[str, Any]								=				MobileNetVaConfigTester(self      ,config_class=_UpperCAmelCase      ,has_text_modality=_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Optional[int]						):
									self.config_tester.run_common_tests()







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







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







							@unittest.skip(reason='MobileNetV1 does not output attentions'						)
							def 				__lowercase     (				self   :			List[str]						):
									pass







							def 				__lowercase     (				self   :			int						):
									_a	: Optional[Any]								=				self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											_a	: List[str]								=				model_class(_UpperCAmelCase						)
											_a	: List[str]								=				inspect.signature(model.forward						)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											_a	: Optional[Any]								=				[*signature.parameters.keys()]

											_a	: List[str]								=				['''pixel_values''']
											self.assertListEqual(arg_names[:1]      ,_UpperCAmelCase						)







							def 				__lowercase     (				self   :			List[Any]						):
									_a	: Optional[Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			str						):






									def check_hidden_states_output(_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			List[str]						):
											_a	: Optional[int]								=				model_class(_UpperCAmelCase						)
											model.to(_UpperCAmelCase						)
											model.eval()

											with torch.no_grad():
													_a	: str								=				model(**self._prepare_for_class(_UpperCAmelCase      ,_UpperCAmelCase						)						)

											_a	: int								=				outputs.hidden_states

											_a	: Optional[int]								=				26
											self.assertEqual(len(_UpperCAmelCase						)      ,_UpperCAmelCase						)

									_a	: Optional[int]								=				self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											_a	: List[Any]								=				True
											check_hidden_states_output(_UpperCAmelCase      ,_UpperCAmelCase      ,_UpperCAmelCase						)

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

											check_hidden_states_output(_UpperCAmelCase      ,_UpperCAmelCase      ,_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Tuple						):
									_a	: Optional[int]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase						)







							@slow
							def 				__lowercase     (				self   :			List[str]						):






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



def 	__lowerCamelCase	( )     -> List[str]:
		_a	: Dict								=				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
		return image

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







							@cached_property
							def 				__lowercase     (				self   :			Tuple						):
									return (
									    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v1_1.0_224'						) if is_vision_available() else None
									)







							@slow
							def 				__lowercase     (				self   :			Dict						):
									_a	: int								=				MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v1_1.0_224'						).to(_UpperCAmelCase						)

									_a	: Tuple								=				self.default_image_processor
									_a	: Optional[int]								=				prepare_img()
									_a	: Optional[Any]								=				image_processor(images=_UpperCAmelCase      ,return_tensors='pt'						).to(_UpperCAmelCase						)

									# forward pass
									with torch.no_grad():
											_a	: Tuple								=				model(**_UpperCAmelCase						)

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

									_a	: Dict								=				torch.tensor([-4.17_39, -1.12_33, 3.12_05]						).to(_UpperCAmelCase						)

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






'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase     :							Tuple       						=     {
    'configuration_luke': ['LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LukeConfig'],
    'tokenization_luke': ['LukeTokenizer'],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase     :							int       						=     [
          'LUKE_PRETRAINED_MODEL_ARCHIVE_LIST',
          'LukeForEntityClassification',
          'LukeForEntityPairClassification',
          'LukeForEntitySpanClassification',
          'LukeForMultipleChoice',
          'LukeForQuestionAnswering',
          'LukeForSequenceClassification',
          'LukeForTokenClassification',
          'LukeForMaskedLM',
          'LukeModel',
          'LukePreTrainedModel',
      ]


if TYPE_CHECKING:
      from .configuration_luke import LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP, LukeConfig
      from .tokenization_luke import LukeTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_luke import (
                LUKE_PRETRAINED_MODEL_ARCHIVE_LIST,
                LukeForEntityClassification,
                LukeForEntityPairClassification,
                LukeForEntitySpanClassification,
                LukeForMaskedLM,
                LukeForMultipleChoice,
                LukeForQuestionAnswering,
                LukeForSequenceClassification,
                LukeForTokenClassification,
                LukeModel,
                LukePreTrainedModel,
            )

else:
      import sys

      lowercase     :							Dict       						=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




