

import inspect
import unittest

from transformers import MobileViTConfig
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 MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
     from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import MobileViTImageProcessor





class  __UpperCAmelCase					(	lowerCamelCase__						):



   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase	:							Any						=			self.config_class(**self.inputs_dict						)
     self.parent.assertTrue(hasattr(__A,		'''hidden_sizes'''						)						)
     self.parent.assertTrue(hasattr(__A,		'''neck_hidden_sizes'''						)						)
     self.parent.assertTrue(hasattr(__A,		'''num_attention_heads'''						)						)






class  __UpperCAmelCase					:



   def __init__(							self							:					Any,		__A							:					Tuple,		__A							:					Optional[Any]=1_3,		__A							:					Any=3_2,		__A							:					int=2,		__A							:					Union[str, Any]=3,		__A							:					List[Any]=6_4_0,		__A							:					int=4,		__A							:					Optional[Any]="silu",		__A							:					Optional[Any]=3,		__A							:					List[str]=3_2,		__A							:					Dict=0.1,		__A							:					int=0.1,		__A							:					List[Any]=0.1,		__A							:					Optional[Any]=0.0_2,		__A							:					int=True,		__A							:					Optional[int]=True,		__A							:					List[str]=1_0,		__A							:					Optional[Any]=None,		):
     UpperCAmelCase	:							Optional[Any]						=			parent
     UpperCAmelCase	:							List[str]						=			batch_size
     UpperCAmelCase	:							Union[str, Any]						=			image_size
     UpperCAmelCase	:							int						=			patch_size
     UpperCAmelCase	:							Optional[int]						=			num_channels
     UpperCAmelCase	:							Optional[int]						=			last_hidden_size
     UpperCAmelCase	:							Union[str, Any]						=			num_attention_heads
     UpperCAmelCase	:							Optional[Any]						=			hidden_act
     UpperCAmelCase	:							Union[str, Any]						=			conv_kernel_size
     UpperCAmelCase	:							Tuple						=			output_stride
     UpperCAmelCase	:							Union[str, Any]						=			hidden_dropout_prob
     UpperCAmelCase	:							Dict						=			attention_probs_dropout_prob
     UpperCAmelCase	:							str						=			classifier_dropout_prob
     UpperCAmelCase	:							List[str]						=			use_labels
     UpperCAmelCase	:							Union[str, Any]						=			is_training
     UpperCAmelCase	:							List[Any]						=			num_labels
     UpperCAmelCase	:							Optional[Any]						=			initializer_range
     UpperCAmelCase	:							List[Any]						=			scope



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							str						=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

     UpperCAmelCase	:							List[Any]						=			None
     UpperCAmelCase	:							Tuple						=			None
     if self.use_labels:
       UpperCAmelCase	:							int						=			ids_tensor([self.batch_size],		self.num_labels						)
       UpperCAmelCase	:							int						=			ids_tensor([self.batch_size, self.image_size, self.image_size],		self.num_labels						)

     UpperCAmelCase	:							Dict						=			self.get_config()

     return config, pixel_values, labels, pixel_labels



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     return MobileViTConfig(
         image_size=self.image_size,		patch_size=self.patch_size,		num_channels=self.num_channels,		num_attention_heads=self.num_attention_heads,		hidden_act=self.hidden_act,		conv_kernel_size=self.conv_kernel_size,		output_stride=self.output_stride,		hidden_dropout_prob=self.hidden_dropout_prob,		attention_probs_dropout_prob=self.attention_probs_dropout_prob,		classifier_dropout_prob=self.classifier_dropout_prob,		initializer_range=self.initializer_range,		)



   def 	__magic_name__   (							self							:					str,		__A							:					Optional[Any],		__A							:					int,		__A							:					List[Any],		__A							:					List[str]						):
     UpperCAmelCase	:							List[Any]						=			MobileViTModel(config=__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							List[str]						=			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 	__magic_name__   (							self							:					Union[str, Any],		__A							:					List[str],		__A							:					Union[str, Any],		__A							:					Optional[int],		__A							:					Optional[int]						):
     UpperCAmelCase	:							Any						=			self.num_labels
     UpperCAmelCase	:							Optional[Any]						=			MobileViTForImageClassification(__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							Union[str, Any]						=			model(__A,		labels=__A						)
     self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)						)



   def 	__magic_name__   (							self							:					List[str],		__A							:					Optional[Any],		__A							:					Dict,		__A							:					Optional[int],		__A							:					List[Any]						):
     UpperCAmelCase	:							Tuple						=			self.num_labels
     UpperCAmelCase	:							Optional[int]						=			MobileViTForSemanticSegmentation(__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							Optional[Any]						=			model(__A						)
     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	:							List[Any]						=			model(__A,		labels=__A						)
     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 	__magic_name__   (							self							:					Optional[int]						):
     UpperCAmelCase	:							Optional[Any]						=			self.prepare_config_and_inputs()
     UpperCAmelCase			,						UpperCAmelCase			,						UpperCAmelCase			,						UpperCAmelCase	:							Dict						=			config_and_inputs
     UpperCAmelCase	:							List[Any]						=			{'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_torch
class  __UpperCAmelCase					(	lowerCamelCase__      ,  lowerCamelCase__      ,  unittest.TestCase						):
   UpperCamelCase       					=					(
       (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
       if is_torch_available()
       else ()
   )
   UpperCamelCase       					=					(
       {
           """feature-extraction""": MobileViTModel,
           """image-classification""": MobileViTForImageClassification,
           """image-segmentation""": MobileViTForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   UpperCamelCase       					=					False
   UpperCamelCase       					=					False
   UpperCamelCase       					=					False
   UpperCamelCase       					=					False



   def 	__magic_name__   (							self							:					Tuple						):
     UpperCAmelCase	:							Optional[int]						=			MobileViTModelTester(self						)
     UpperCAmelCase	:							Dict						=			MobileViTConfigTester(self,		config_class=__A,		has_text_modality=__A						)



   def 	__magic_name__   (							self							:					str						):
     self.config_tester.run_common_tests()



   @unittest.skip(reason='''MobileViT does not use inputs_embeds'''						)
   def 	__magic_name__   (							self							:					Optional[int]						):
     pass



   @unittest.skip(reason='''MobileViT does not support input and output embeddings'''						)
   def 	__magic_name__   (							self							:					Optional[Any]						):
     pass



   @unittest.skip(reason='''MobileViT does not output attentions'''						)
   def 	__magic_name__   (							self							:					Any						):
     pass



   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase			,						UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       UpperCAmelCase	:							Dict						=			model_class(__A						)
       UpperCAmelCase	:							Optional[Any]						=			inspect.signature(model.forward						)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       UpperCAmelCase	:							Tuple						=			[*signature.parameters.keys()]

       UpperCAmelCase	:							int						=			['''pixel_values''']
       self.assertListEqual(arg_names[:1],		__A						)



   @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''						)
   def 	__magic_name__   (							self							:					Tuple						):
     pass



   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__A						)



   def 	__magic_name__   (							self							:					Optional[int]						):
     def check_hidden_states_output(__A							:					Optional[Any],		__A							:					int,		__A							:					Dict						):
       UpperCAmelCase	:							str						=			model_class(__A						)
       model.to(__A						)
       model.eval()

       with torch.no_grad():
         UpperCAmelCase	:							List[Any]						=			model(**self._prepare_for_class(__A,		__A						)						)

       UpperCAmelCase	:							str						=			outputs.hidden_states

       UpperCAmelCase	:							List[str]						=			5
       self.assertEqual(len(__A						),		__A						)

       # MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
       # with the width and height being successively divided by 2.
       UpperCAmelCase	:							List[str]						=			2
       for i in range(len(__A						)						):
         self.assertListEqual(
             list(hidden_states[i].shape[-2:]						),		[self.model_tester.image_size // divisor, self.model_tester.image_size // divisor],		)
         divisor *= 2

       self.assertEqual(self.model_tester.output_stride,		divisor // 2						)

     UpperCAmelCase			,						UpperCAmelCase	:							int						=			self.model_tester.prepare_config_and_inputs_for_common()

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

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

       check_hidden_states_output(__A,		__A,		__A						)



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							Optional[int]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__A						)



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_semantic_segmentation(*__A						)





   @slow
   def 	__magic_name__   (							self							:					Dict						):
     for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							str						=			MobileViTModel.from_pretrained(__A						)
       self.assertIsNotNone(__A						)






def       a__					(		)  ->						int:
  UpperCAmelCase	:							int						=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
  return image






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



   @cached_property
   def 	__magic_name__   (							self							:					str						):
     return MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''						) if is_vision_available() else None



   @slow
   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase	:							Any						=			MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''						).to(__A						)

     UpperCAmelCase	:							Tuple						=			self.default_image_processor
     UpperCAmelCase	:							Dict						=			prepare_img()
     UpperCAmelCase	:							List[str]						=			image_processor(images=__A,		return_tensors='''pt'''						).to(__A						)

     # forward pass
     with torch.no_grad():
       UpperCAmelCase	:							int						=			model(**__A						)

     # verify the logits
     UpperCAmelCase	:							Optional[Any]						=			torch.Size((1, 1_0_0_0)						)
     self.assertEqual(outputs.logits.shape,		__A						)

     UpperCAmelCase	:							Optional[Any]						=			torch.tensor([-1.9_3_6_4, -1.2_3_2_7, -0.4_6_5_3]						).to(__A						)

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



   @slow
   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase	:							Dict						=			MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)
     UpperCAmelCase	:							int						=			model.to(__A						)

     UpperCAmelCase	:							Any						=			MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)

     UpperCAmelCase	:							int						=			prepare_img()
     UpperCAmelCase	:							int						=			image_processor(images=__A,		return_tensors='''pt'''						).to(__A						)

     # forward pass
     with torch.no_grad():
       UpperCAmelCase	:							str						=			model(**__A						)
     UpperCAmelCase	:							Union[str, Any]						=			outputs.logits

     # verify the logits
     UpperCAmelCase	:							Tuple						=			torch.Size((1, 2_1, 3_2, 3_2)						)
     self.assertEqual(logits.shape,		__A						)

     UpperCAmelCase	:							Optional[Any]						=			torch.tensor(
         [
             [[6.9_7_1_3, 6.9_7_8_6, 7.2_4_2_2], [7.2_8_9_3, 7.2_8_2_5, 7.4_4_4_6], [7.6_5_8_0, 7.8_7_9_7, 7.9_4_2_0]],
             [[-1_0.6_8_6_9, -1_0.3_2_5_0, -1_0.3_4_7_1], [-1_0.4_2_2_8, -9.9_8_6_8, -9.7_1_3_2], [-1_1.0_4_0_5, -1_1.0_2_2_1, -1_0.7_3_1_8]],
             [[-3.3_0_8_9, -2.8_5_3_9, -2.6_7_4_0], [-3.2_7_0_6, -2.5_6_2_1, -2.5_1_0_8], [-3.2_5_3_4, -2.6_6_1_5, -2.6_6_5_1]],
         ],		device=__A,		)

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





   @slow
   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase	:							Optional[int]						=			MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)
     UpperCAmelCase	:							Optional[int]						=			model.to(__A						)

     UpperCAmelCase	:							int						=			MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''						)

     UpperCAmelCase	:							List[str]						=			prepare_img()
     UpperCAmelCase	:							Union[str, Any]						=			image_processor(images=__A,		return_tensors='''pt'''						).to(__A						)

     # forward pass
     with torch.no_grad():
       UpperCAmelCase	:							Tuple						=			model(**__A						)

     UpperCAmelCase	:							str						=			outputs.logits.detach().cpu()

     UpperCAmelCase	:							Tuple						=			image_processor.post_process_semantic_segmentation(outputs=__A,		target_sizes=[(5_0, 6_0)]						)
     UpperCAmelCase	:							List[str]						=			torch.Size((5_0, 6_0)						)
     self.assertEqual(segmentation[0].shape,		__A						)

     UpperCAmelCase	:							Any						=			image_processor.post_process_semantic_segmentation(outputs=__A						)
     UpperCAmelCase	:							int						=			torch.Size((3_2, 3_2)						)
     self.assertEqual(segmentation[0].shape,		__A						)








from typing import TYPE_CHECKING

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


_lowerCamelCase    :       str        =							{"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _lowerCamelCase    :       List[Any]        =							["XLNetTokenizer"]

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _lowerCamelCase    :       Optional[Any]        =							["XLNetTokenizerFast"]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _lowerCamelCase    :       Dict        =							[
         "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST",
         "XLNetForMultipleChoice",
         "XLNetForQuestionAnswering",
         "XLNetForQuestionAnsweringSimple",
         "XLNetForSequenceClassification",
         "XLNetForTokenClassification",
         "XLNetLMHeadModel",
         "XLNetModel",
         "XLNetPreTrainedModel",
         "load_tf_weights_in_xlnet",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _lowerCamelCase    :       Union[str, Any]        =							[
         "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST",
         "TFXLNetForMultipleChoice",
         "TFXLNetForQuestionAnsweringSimple",
         "TFXLNetForSequenceClassification",
         "TFXLNetForTokenClassification",
         "TFXLNetLMHeadModel",
         "TFXLNetMainLayer",
         "TFXLNetModel",
         "TFXLNetPreTrainedModel",
     ]


if TYPE_CHECKING:
     from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_xlnet import XLNetTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_xlnet_fast import XLNetTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_xlnet import (
              XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
              XLNetForMultipleChoice,
              XLNetForQuestionAnswering,
              XLNetForQuestionAnsweringSimple,
              XLNetForSequenceClassification,
              XLNetForTokenClassification,
              XLNetLMHeadModel,
              XLNetModel,
              XLNetPreTrainedModel,
              load_tf_weights_in_xlnet,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_xlnet import (
              TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFXLNetForMultipleChoice,
              TFXLNetForQuestionAnsweringSimple,
              TFXLNetForSequenceClassification,
              TFXLNetForTokenClassification,
              TFXLNetLMHeadModel,
              TFXLNetMainLayer,
              TFXLNetModel,
              TFXLNetPreTrainedModel,
          )

else:
     import sys

     _lowerCamelCase    :       Optional[Any]        =							_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





