





"""simple docstring"""





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 					_lowerCAmelCase   (     a       ):
       """simple docstring"""




       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							self.config_class(**self.inputs_dict      )
         self.parent.assertTrue(hasattr(__UpperCAmelCase    , 'hidden_sizes'      )      )
         self.parent.assertTrue(hasattr(__UpperCAmelCase    , 'neck_hidden_sizes'      )      )
         self.parent.assertTrue(hasattr(__UpperCAmelCase    , 'num_attention_heads'      )      )






class 					_lowerCAmelCase   :
       """simple docstring"""




       def __init__(    self    , __UpperCAmelCase    , __UpperCAmelCase=1_3    , __UpperCAmelCase=3_2    , __UpperCAmelCase=2    , __UpperCAmelCase=3    , __UpperCAmelCase=6_4_0    , __UpperCAmelCase=4    , __UpperCAmelCase="silu"    , __UpperCAmelCase=3    , __UpperCAmelCase=3_2    , __UpperCAmelCase=0.1    , __UpperCAmelCase=0.1    , __UpperCAmelCase=0.1    , __UpperCAmelCase=0.02    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=1_0    , __UpperCAmelCase=None    , ):



         '''simple docstring'''

         lowerCAmelCase__			:Any											=							parent
         lowerCAmelCase__			:Optional[int]											=							batch_size
         lowerCAmelCase__			:int											=							image_size
         lowerCAmelCase__			:List[Any]											=							patch_size
         lowerCAmelCase__			:Optional[int]											=							num_channels
         lowerCAmelCase__			:Optional[int]											=							last_hidden_size
         lowerCAmelCase__			:Optional[Any]											=							num_attention_heads
         lowerCAmelCase__			:Any											=							hidden_act
         lowerCAmelCase__			:Dict											=							conv_kernel_size
         lowerCAmelCase__			:Optional[Any]											=							output_stride
         lowerCAmelCase__			:List[Any]											=							hidden_dropout_prob
         lowerCAmelCase__			:Union[str, Any]											=							attention_probs_dropout_prob
         lowerCAmelCase__			:Optional[int]											=							classifier_dropout_prob
         lowerCAmelCase__			:int											=							use_labels
         lowerCAmelCase__			:int											=							is_training
         lowerCAmelCase__			:int											=							num_labels
         lowerCAmelCase__			:Any											=							initializer_range
         lowerCAmelCase__			:int											=							scope






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Any											=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

         lowerCAmelCase__			:Optional[int]											=							self.get_config()

         return config, pixel_values, labels, pixel_labels






       def      snake_case						(    self      ):



         '''simple docstring'''

         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      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							MobileViTModel(config=__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:Union[str, Any]											=							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      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Tuple											=							self.num_labels
         lowerCAmelCase__			:List[str]											=							MobileViTForImageClassification(__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:Optional[int]											=							model(__UpperCAmelCase    , labels=__UpperCAmelCase      )
         self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.num_labels)      )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[int]											=							self.num_labels
         lowerCAmelCase__			:Union[str, Any]											=							MobileViTForSemanticSegmentation(__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:str											=							model(__UpperCAmelCase      )
         self.parent.assertEqual(
             result.logits.shape    , (
                 self.batch_size,
                 self.num_labels,
                 self.image_size // self.output_stride,
                 self.image_size // self.output_stride,
             )    , )
         lowerCAmelCase__			:List[Any]											=							model(__UpperCAmelCase    , labels=__UpperCAmelCase      )
         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      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[int]											=							self.prepare_config_and_inputs()
         lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__			:Optional[Any]											=							config_and_inputs
         lowerCAmelCase__			:Any											=							{'pixel_values': pixel_values}
         return config, inputs_dict






@require_torch
class 					_lowerCAmelCase   (     a  ,   a  ,   unittest.TestCase       ):
       """simple docstring"""




       __magic_name__     :Dict		  =					(
           (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       __magic_name__     :Optional[Any]		  =					(
           {
               """feature-extraction""": MobileViTModel,
               """image-classification""": MobileViTForImageClassification,
               """image-segmentation""": MobileViTForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

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






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:int											=							MobileViTModelTester(self      )
         lowerCAmelCase__			:List[str]											=							MobileViTConfigTester(self    , config_class=__UpperCAmelCase    , has_text_modality=__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         self.config_tester.run_common_tests()






       @unittest.skip(reason='MobileViT does not use inputs_embeds'      )
       def      snake_case						(    self      ):



         '''simple docstring'''

         pass






       @unittest.skip(reason='MobileViT does not support input and output embeddings'      )
       def      snake_case						(    self      ):



         '''simple docstring'''

         pass






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



         '''simple docstring'''

         pass






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__			:List[Any]											=							self.model_tester.prepare_config_and_inputs_for_common()

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

           lowerCAmelCase__			:Union[str, Any]											=							['pixel_values']
           self.assertListEqual(arg_names[:1]    , __UpperCAmelCase      )






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



         '''simple docstring'''

         pass






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[Any]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         def check_hidden_states_output(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):
           lowerCAmelCase__			:List[Any]											=							model_class(__UpperCAmelCase      )
           model.to(__UpperCAmelCase      )
           model.eval()

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

           lowerCAmelCase__			:str											=							outputs.hidden_states

           lowerCAmelCase__			:Any											=							5
           self.assertEqual(len(__UpperCAmelCase      )    , __UpperCAmelCase      )

           # MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
           # with the width and height being successively divided by 2.
           lowerCAmelCase__			:List[Any]											=							2
           for i in range(len(__UpperCAmelCase      )      ):
             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      )

         lowerCAmelCase__							,				lowerCAmelCase__			:List[str]											=							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(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      )

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

           check_hidden_states_output(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[int]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Tuple											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCAmelCase      )






       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowerCAmelCase__			:Optional[Any]											=							MobileViTModel.from_pretrained(__UpperCAmelCase      )
           self.assertIsNotNone(__UpperCAmelCase      )






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





  """simple docstring"""


  lowerCAmelCase__			:List[str]											=							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
  return image






@require_torch
@require_vision
class 					_lowerCAmelCase   (     unittest.TestCase       ):
       """simple docstring"""




       @cached_property
       def      snake_case						(    self      ):



         '''simple docstring'''

         return MobileViTImageProcessor.from_pretrained('apple/mobilevit-xx-small'      ) if is_vision_available() else None






       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:str											=							MobileViTForImageClassification.from_pretrained('apple/mobilevit-xx-small'      ).to(__UpperCAmelCase      )

         lowerCAmelCase__			:str											=							self.default_image_processor
         lowerCAmelCase__			:Optional[Any]											=							prepare_img()
         lowerCAmelCase__			:Dict											=							image_processor(images=__UpperCAmelCase    , return_tensors='pt'      ).to(__UpperCAmelCase      )

         # forward pass
         with torch.no_grad():
           lowerCAmelCase__			:str											=							model(**__UpperCAmelCase      )

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

         lowerCAmelCase__			:Optional[Any]											=							torch.tensor([-1.93_64, -1.23_27, -0.46_53]      ).to(__UpperCAmelCase      )

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






       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:str											=							MobileViTForSemanticSegmentation.from_pretrained('apple/deeplabv3-mobilevit-xx-small'      )
         lowerCAmelCase__			:Optional[int]											=							model.to(__UpperCAmelCase      )

         lowerCAmelCase__			:Optional[int]											=							MobileViTImageProcessor.from_pretrained('apple/deeplabv3-mobilevit-xx-small'      )

         lowerCAmelCase__			:Any											=							prepare_img()
         lowerCAmelCase__			:Any											=							image_processor(images=__UpperCAmelCase    , return_tensors='pt'      ).to(__UpperCAmelCase      )

         # forward pass
         with torch.no_grad():
           lowerCAmelCase__			:List[str]											=							model(**__UpperCAmelCase      )
         lowerCAmelCase__			:Dict											=							outputs.logits

         # verify the logits
         lowerCAmelCase__			:List[Any]											=							torch.Size((1, 2_1, 3_2, 3_2)      )
         self.assertEqual(logits.shape    , __UpperCAmelCase      )

         lowerCAmelCase__			:Union[str, Any]											=							torch.tensor(
             [
                 [[6.97_13, 6.97_86, 7.24_22], [7.28_93, 7.28_25, 7.44_46], [7.65_80, 7.87_97, 7.94_20]],
                 [[-10.68_69, -10.32_50, -10.34_71], [-10.42_28, -9.98_68, -9.71_32], [-11.04_05, -11.02_21, -10.73_18]],
                 [[-3.30_89, -2.85_39, -2.67_40], [-3.27_06, -2.56_21, -2.51_08], [-3.25_34, -2.66_15, -2.66_51]],
             ]    , device=__UpperCAmelCase    , )

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






       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							MobileViTForSemanticSegmentation.from_pretrained('apple/deeplabv3-mobilevit-xx-small'      )
         lowerCAmelCase__			:List[Any]											=							model.to(__UpperCAmelCase      )

         lowerCAmelCase__			:str											=							MobileViTImageProcessor.from_pretrained('apple/deeplabv3-mobilevit-xx-small'      )

         lowerCAmelCase__			:str											=							prepare_img()
         lowerCAmelCase__			:int											=							image_processor(images=__UpperCAmelCase    , return_tensors='pt'      ).to(__UpperCAmelCase      )

         # forward pass
         with torch.no_grad():
           lowerCAmelCase__			:int											=							model(**__UpperCAmelCase      )

         lowerCAmelCase__			:Union[str, Any]											=							outputs.logits.detach().cpu()

         lowerCAmelCase__			:Tuple											=							image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase    , target_sizes=[(5_0, 6_0)]      )
         lowerCAmelCase__			:Optional[int]											=							torch.Size((5_0, 6_0)      )
         self.assertEqual(segmentation[0].shape    , __UpperCAmelCase      )

         lowerCAmelCase__			:Optional[Any]											=							image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase      )
         lowerCAmelCase__			:str											=							torch.Size((3_2, 3_2)      )
         self.assertEqual(segmentation[0].shape    , __UpperCAmelCase      )







"""simple docstring"""





from typing import TYPE_CHECKING

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


__A											=		{"""configuration_reformer""": ["""REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ReformerConfig"""]}

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

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

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __A											=		[
      """REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """ReformerAttention""",
      """ReformerForMaskedLM""",
      """ReformerForQuestionAnswering""",
      """ReformerForSequenceClassification""",
      """ReformerLayer""",
      """ReformerModel""",
      """ReformerModelWithLMHead""",
      """ReformerPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_reformer import ReformerTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_reformer_fast import ReformerTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_reformer import (
        REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
        ReformerAttention,
        ReformerForMaskedLM,
        ReformerForQuestionAnswering,
        ReformerForSequenceClassification,
        ReformerLayer,
        ReformerModel,
        ReformerModelWithLMHead,
        ReformerPreTrainedModel,
    )

else:
  import sys

  __A											=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
