





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






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     self.config_class(**self.inputs_dict					)
														self.parent.assertTrue(hasattr(__lowerCAmelCase					,	"""tf_padding"""					)					)
														self.parent.assertTrue(hasattr(__lowerCAmelCase					,	"""depth_multiplier"""					)					)






class     __SCREAMING_SNAKE_CASE   :






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






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

														UpperCamelCase__			       =     self.get_config()

														return config, pixel_values, labels, pixel_labels






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






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






@require_torch
class     __SCREAMING_SNAKE_CASE   (       _a				,						_a				,						unittest.TestCase    ):
							snake_case       : Optional[Any]         =    (
							    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)
							snake_case       : Dict         =    (
							    {
							        """feature-extraction""": MobileNetVaModel,
							        """image-classification""": MobileNetVaForImageClassification,
							        """image-segmentation""": MobileNetVaForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							snake_case       : List[Any]         =    False
							snake_case       : Optional[int]         =    False
							snake_case       : Optional[int]         =    False
							snake_case       : int         =    False






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     MobileNetVaModelTester(self					)
														UpperCamelCase__			       =     MobileNetVaConfigTester(self					,	config_class=__lowerCAmelCase					,	has_text_modality=__lowerCAmelCase					)






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






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






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






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






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__       ,       UpperCamelCase__			       =     self.model_tester.prepare_config_and_inputs_for_common()

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

																					UpperCamelCase__			       =     ["""pixel_values"""]
																					self.assertListEqual(arg_names[:1]					,	__lowerCAmelCase					)






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__lowerCAmelCase					)






							def 			_lowerCamelCase						(  self					):
														def check_hidden_states_output(__lowerCAmelCase					,	__lowerCAmelCase					,	__lowerCAmelCase					):
																					UpperCamelCase__			       =     model_class(__lowerCAmelCase					)
																					model.to(__lowerCAmelCase					)
																					model.eval()

																					with torch.no_grad():
																												UpperCamelCase__			       =     model(**self._prepare_for_class(__lowerCAmelCase					,	__lowerCAmelCase					)					)

																					UpperCamelCase__			       =     outputs.hidden_states

																					UpperCamelCase__			       =     16
																					self.assertEqual(len(__lowerCAmelCase					)					,	__lowerCAmelCase					)

														UpperCamelCase__       ,       UpperCamelCase__			       =     self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					UpperCamelCase__			       =     True
																					check_hidden_states_output(__lowerCAmelCase					,	__lowerCAmelCase					,	__lowerCAmelCase					)

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

																					check_hidden_states_output(__lowerCAmelCase					,	__lowerCAmelCase					,	__lowerCAmelCase					)






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase					)






							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCAmelCase					)







							@slow
							def 			_lowerCamelCase						(  self					):
														for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					UpperCamelCase__			       =     MobileNetVaModel.from_pretrained(__lowerCAmelCase					)
																					self.assertIsNotNone(__lowerCAmelCase					)






def       _UpperCamelCase       ():






							"""simple docstring"""





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




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






							@cached_property
							def 			_lowerCamelCase						(  self					):
														return (
														    MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""					) if is_vision_available() else None
														)






							@slow
							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""					).to(__lowerCAmelCase					)

														UpperCamelCase__			       =     self.default_image_processor
														UpperCamelCase__			       =     prepare_img()
														UpperCamelCase__			       =     image_processor(images=__lowerCAmelCase					,	return_tensors="""pt"""					).to(__lowerCAmelCase					)

														# forward pass
														with torch.no_grad():
																					UpperCamelCase__			       =     model(**__lowerCAmelCase					)

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

														UpperCamelCase__			       =     torch.tensor([0.2445, -1.1993, 0.1905]					).to(__lowerCAmelCase					)

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







							@slow
							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""					)
														UpperCamelCase__			       =     model.to(__lowerCAmelCase					)

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

														UpperCamelCase__			       =     prepare_img()
														UpperCamelCase__			       =     image_processor(images=__lowerCAmelCase					,	return_tensors="""pt"""					).to(__lowerCAmelCase					)

														# forward pass
														with torch.no_grad():
																					UpperCamelCase__			       =     model(**__lowerCAmelCase					)
														UpperCamelCase__			       =     outputs.logits

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

														UpperCamelCase__			       =     torch.tensor(
														    [
														        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
														        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
														        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
														    ]					,	device=__lowerCAmelCase					,	)

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











import warnings

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


UpperCamelCase__     =	logging.get_logger(__name__)

UpperCamelCase__     =	{
    "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json",
    "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json",
}

class     __SCREAMING_SNAKE_CASE   (       _a    ):
							snake_case       : Any         =    """xlnet"""
							snake_case       : Optional[Any]         =    ["""mems"""]
							snake_case       : Any         =    {
							    """n_token""": """vocab_size""",  # Backward compatibility
							    """hidden_size""": """d_model""",
							    """num_attention_heads""": """n_head""",
							    """num_hidden_layers""": """n_layer""",
							}






							def __init__(  self					,	__lowerCAmelCase=32000					,	__lowerCAmelCase=1024					,	__lowerCAmelCase=24					,	__lowerCAmelCase=16					,	__lowerCAmelCase=4096					,	__lowerCAmelCase="gelu"					,	__lowerCAmelCase=True					,	__lowerCAmelCase="bi"					,	__lowerCAmelCase=0.02					,	__lowerCAmelCase=1E-12					,	__lowerCAmelCase=0.1					,	__lowerCAmelCase=512					,	__lowerCAmelCase=None					,	__lowerCAmelCase=True					,	__lowerCAmelCase=False					,	__lowerCAmelCase=False					,	__lowerCAmelCase=-1					,	__lowerCAmelCase=False					,	__lowerCAmelCase="last"					,	__lowerCAmelCase=True					,	__lowerCAmelCase="tanh"					,	__lowerCAmelCase=0.1					,	__lowerCAmelCase=5					,	__lowerCAmelCase=5					,	__lowerCAmelCase=5					,	__lowerCAmelCase=1					,	__lowerCAmelCase=2					,	**__lowerCAmelCase					,	):
														UpperCamelCase__			       =     vocab_size
														UpperCamelCase__			       =     d_model
														UpperCamelCase__			       =     n_layer
														UpperCamelCase__			       =     n_head
														if d_model % n_head != 0:
																					raise ValueError(f"""'d_model % n_head' ({d_model % n_head}) should be equal to 0"""					)
														if "d_head" in kwargs:
																					if kwargs["d_head"] != d_model // n_head:
																												raise ValueError(
																												    f"""`d_head` ({kwargs["d_head"]}) should be equal to `d_model // n_head` ({d_model // n_head})"""					)
														UpperCamelCase__			       =     d_model // n_head
														UpperCamelCase__			       =     ff_activation
														UpperCamelCase__			       =     d_inner
														UpperCamelCase__			       =     untie_r
														UpperCamelCase__			       =     attn_type

														UpperCamelCase__			       =     initializer_range
														UpperCamelCase__			       =     layer_norm_eps

														UpperCamelCase__			       =     dropout
														UpperCamelCase__			       =     mem_len
														UpperCamelCase__			       =     reuse_len
														UpperCamelCase__			       =     bi_data
														UpperCamelCase__			       =     clamp_len
														UpperCamelCase__			       =     same_length

														UpperCamelCase__			       =     summary_type
														UpperCamelCase__			       =     summary_use_proj
														UpperCamelCase__			       =     summary_activation
														UpperCamelCase__			       =     summary_last_dropout
														UpperCamelCase__			       =     start_n_top
														UpperCamelCase__			       =     end_n_top

														UpperCamelCase__			       =     bos_token_id
														UpperCamelCase__			       =     pad_token_id
														UpperCamelCase__			       =     eos_token_id

														if "use_cache" in kwargs:
																					warnings.warn(
																					    """The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`"""
																					    """ instead."""					,	__lowerCAmelCase					,	)
																					UpperCamelCase__			       =     kwargs["""use_cache"""]

														UpperCamelCase__			       =     use_mems_eval
														UpperCamelCase__			       =     use_mems_train
														super().__init__(pad_token_id=__lowerCAmelCase					,	bos_token_id=__lowerCAmelCase					,	eos_token_id=__lowerCAmelCase					,	**__lowerCAmelCase					)






							@property
							def 			_lowerCamelCase						(  self					):
														logger.info(f"""The model {self.model_type} is one of the few models that has no sequence length limit."""					)
														return -1







							@max_position_embeddings.setter
							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														# Message copied from Transformer-XL documentation
														raise NotImplementedError(
														    f"""The model {self.model_type} is one of the few models that has no sequence length limit."""					)




