



"""simple docstring"""
import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
from transformers.models.auto import get_values
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 torch import nn

  from transformers import (
      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
      VideoMAEForPreTraining,
      VideoMAEForVideoClassification,
      VideoMAEModel,
  )
  from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from transformers import VideoMAEImageProcessor
class 				_UpperCAmelCase				:





     '''simple docstring'''



     def __init__(   self			,      A			,      A=1_3			,      A=1_0			,      A=3			,      A=2			,      A=2			,      A=2			,      A=True			,      A=True			,      A=3_2			,      A=5			,      A=4			,      A=3_7			,      A="gelu"			,      A=0.1			,      A=0.1			,      A=1_0			,      A=0.02			,      A=0.9			,      A=None			,      )				->   List[Any]:
         _UpperCAmelCase				:    Dict											=       parent
         _UpperCAmelCase				:    Dict											=       batch_size
         _UpperCAmelCase				:    Tuple											=       image_size
         _UpperCAmelCase				:    List[str]											=       num_channels
         _UpperCAmelCase				:    Tuple											=       patch_size
         _UpperCAmelCase				:    Tuple											=       tubelet_size
         _UpperCAmelCase				:    Optional[Any]											=       num_frames
         _UpperCAmelCase				:    Optional[int]											=       is_training
         _UpperCAmelCase				:    int											=       use_labels
         _UpperCAmelCase				:    Any											=       hidden_size
         _UpperCAmelCase				:    Union[str, Any]											=       num_hidden_layers
         _UpperCAmelCase				:    List[Any]											=       num_attention_heads
         _UpperCAmelCase				:    List[str]											=       intermediate_size
         _UpperCAmelCase				:    Union[str, Any]											=       hidden_act
         _UpperCAmelCase				:    Tuple											=       hidden_dropout_prob
         _UpperCAmelCase				:    Union[str, Any]											=       attention_probs_dropout_prob
         _UpperCAmelCase				:    Union[str, Any]											=       type_sequence_label_size
         _UpperCAmelCase				:    Dict											=       initializer_range
         _UpperCAmelCase				:    Tuple											=       mask_ratio
         _UpperCAmelCase				:    str											=       scope

         # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
         _UpperCAmelCase				:    Dict											=       (image_size // patch_size) ** 2
         _UpperCAmelCase				:    List[str]											=       (num_frames // tubelet_size) * self.num_patches_per_frame

         # use this variable to define bool_masked_pos
         _UpperCAmelCase				:    Optional[Any]											=       int(mask_ratio * self.seq_length						)





     def 					__lowerCAmelCase      (   self						)				->   Union[str, Any]:
         _UpperCAmelCase				:    Optional[int]											=       floats_tensor(
             [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]						)

         _UpperCAmelCase				:    Tuple											=       None
         if self.use_labels:
             _UpperCAmelCase				:    Optional[int]											=       ids_tensor([self.batch_size]			,      self.type_sequence_label_size						)

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

         return config, pixel_values, labels





     def 					__lowerCAmelCase      (   self						)				->   Optional[Any]:
         return VideoMAEConfig(
             image_size=self.image_size			,      patch_size=self.patch_size			,      num_channels=self.num_channels			,      num_frames=self.num_frames			,      tubelet_size=self.tubelet_size			,      hidden_size=self.hidden_size			,      num_hidden_layers=self.num_hidden_layers			,      num_attention_heads=self.num_attention_heads			,      intermediate_size=self.intermediate_size			,      hidden_act=self.hidden_act			,      hidden_dropout_prob=self.hidden_dropout_prob			,      attention_probs_dropout_prob=self.attention_probs_dropout_prob			,      is_decoder=_lowerCamelCase			,      initializer_range=self.initializer_range			,      )





     def 					__lowerCAmelCase      (   self			,      A			,      A			,      A						)				->   Any:
         _UpperCAmelCase				:    List[Any]											=       VideoMAEModel(config=_lowerCamelCase						)
         model.to(_lowerCamelCase						)
         model.eval()
         _UpperCAmelCase				:    Tuple											=       model(_lowerCamelCase						)
         self.parent.assertEqual(result.last_hidden_state.shape			,      (self.batch_size, self.seq_length, self.hidden_size)						)





     def 					__lowerCAmelCase      (   self			,      A			,      A			,      A						)				->   Any:
         _UpperCAmelCase				:    Union[str, Any]											=       VideoMAEForPreTraining(_lowerCamelCase						)
         model.to(_lowerCamelCase						)
         model.eval()
         # important: each video needs to have the same number of masked patches
         # hence we define a single mask, which we then repeat for each example in the batch
         _UpperCAmelCase				:    str											=       torch.ones((self.num_masks,)						)
         _UpperCAmelCase				:    Optional[Any]											=       torch.cat([mask, torch.zeros(self.seq_length - mask.size(0						)						)]						)
         _UpperCAmelCase				:    Optional[Any]											=       mask.expand(self.batch_size			,      -1						).bool()

         _UpperCAmelCase				:    Optional[Any]											=       model(_lowerCamelCase			,      _lowerCamelCase						)
         # model only returns predictions for masked patches
         _UpperCAmelCase				:    int											=       mask.sum().item()
         _UpperCAmelCase				:    Dict											=       3 * self.tubelet_size * self.patch_size**2
         self.parent.assertEqual(result.logits.shape			,      (self.batch_size, num_masked_patches, decoder_num_labels)						)





     def 					__lowerCAmelCase      (   self						)				->   Dict:
         _UpperCAmelCase				:    Optional[int]											=       self.prepare_config_and_inputs()
         _UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase				:    Union[str, Any]											=       config_and_inputs
         _UpperCAmelCase				:    Any											=       {'''pixel_values''': pixel_values}
         return config, inputs_dict



@require_torch
class 				_UpperCAmelCase				(					__a						,__a						,unittest.TestCase ):





     '''simple docstring'''



     a__   							=(
         (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
     )
     a__   							=(
         {"""feature-extraction""": VideoMAEModel, """video-classification""": VideoMAEForVideoClassification}
         if is_torch_available()
         else {}
     )

     a__   							=False
     a__   							=False
     a__   							=False
     a__   							=False





     def 					__lowerCAmelCase      (   self						)				->   Any:
         _UpperCAmelCase				:    Optional[int]											=       VideoMAEModelTester(self						)
         _UpperCAmelCase				:    Any											=       ConfigTester(self			,      config_class=_lowerCamelCase			,      has_text_modality=_lowerCamelCase			,      hidden_size=3_7						)





     def 					__lowerCAmelCase      (   self			,      A			,      A			,      A=False						)				->   str:
         _UpperCAmelCase				:    Any											=       copy.deepcopy(_lowerCamelCase						)

         if model_class == VideoMAEForPreTraining:
             # important: each video needs to have the same number of masked patches
             # hence we define a single mask, which we then repeat for each example in the batch
             _UpperCAmelCase				:    Any											=       torch.ones((self.model_tester.num_masks,)						)
             _UpperCAmelCase				:    List[str]											=       torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0						)						)]						)
             _UpperCAmelCase				:    List[Any]											=       mask.expand(self.model_tester.batch_size			,      -1						).bool()
             _UpperCAmelCase				:    Any											=       bool_masked_pos.to(_lowerCamelCase						)

         if return_labels:
             if model_class in [
                 *get_values(_lowerCamelCase						),
             ]:
                 _UpperCAmelCase				:    int											=       torch.zeros(
                     self.model_tester.batch_size			,      dtype=torch.long			,      device=_lowerCamelCase						)

         return inputs_dict





     def 					__lowerCAmelCase      (   self						)				->   Any:
         self.config_tester.run_common_tests()





     @unittest.skip(reason='''VideoMAE does not use inputs_embeds'''						)
     def 					__lowerCAmelCase      (   self						)				->   List[Any]:
         pass





     def 					__lowerCAmelCase      (   self						)				->   List[Any]:
         _UpperCAmelCase				,							_UpperCAmelCase				:    Union[str, Any]											=       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             _UpperCAmelCase				:    List[str]											=       model_class(_lowerCamelCase						)
             self.assertIsInstance(model.get_input_embeddings()			,      (nn.Module)						)
             _UpperCAmelCase				:    Optional[int]											=       model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(_lowerCamelCase			,      nn.Linear						)						)





     def 					__lowerCAmelCase      (   self						)				->   List[Any]:
         _UpperCAmelCase				,							_UpperCAmelCase				:    Optional[Any]											=       self.model_tester.prepare_config_and_inputs_for_common()

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

             _UpperCAmelCase				:    Dict											=       ['''pixel_values''']
             self.assertListEqual(arg_names[:1]			,      _lowerCamelCase						)





     def 					__lowerCAmelCase      (   self						)				->   Any:
         _UpperCAmelCase				:    Dict											=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCamelCase						)





     def 					__lowerCAmelCase      (   self						)				->   List[Any]:
         _UpperCAmelCase				:    Any											=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*_lowerCamelCase						)





     @slow
     def 					__lowerCAmelCase      (   self						)				->   Dict:
         for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase				:    Optional[Any]											=       VideoMAEModel.from_pretrained(_lowerCamelCase						)
             self.assertIsNotNone(_lowerCamelCase						)





     def 					__lowerCAmelCase      (   self						)				->   Tuple:
         if not self.has_attentions:
             pass

         else:
             _UpperCAmelCase				,							_UpperCAmelCase				:    Any											=       self.model_tester.prepare_config_and_inputs_for_common()
             _UpperCAmelCase				:    List[str]											=       True

             for model_class in self.all_model_classes:
                 _UpperCAmelCase				:    Dict											=       self.model_tester.seq_length - self.model_tester.num_masks
                 _UpperCAmelCase				:    int											=       (
                     num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                 )

                 _UpperCAmelCase				:    Any											=       True
                 _UpperCAmelCase				:    Union[str, Any]											=       False
                 _UpperCAmelCase				:    Dict											=       True
                 _UpperCAmelCase				:    Optional[int]											=       model_class(_lowerCamelCase						)
                 model.to(_lowerCamelCase						)
                 model.eval()
                 with torch.no_grad():
                     _UpperCAmelCase				:    Dict											=       model(**self._prepare_for_class(_lowerCamelCase			,      _lowerCamelCase						)						)
                 _UpperCAmelCase				:    int											=       outputs.attentions
                 self.assertEqual(len(_lowerCamelCase						)			,      self.model_tester.num_hidden_layers						)

                 # check that output_attentions also work using config
                 del inputs_dict["output_attentions"]
                 _UpperCAmelCase				:    Union[str, Any]											=       True
                 _UpperCAmelCase				:    Union[str, Any]											=       model_class(_lowerCamelCase						)
                 model.to(_lowerCamelCase						)
                 model.eval()
                 with torch.no_grad():
                     _UpperCAmelCase				:    Union[str, Any]											=       model(**self._prepare_for_class(_lowerCamelCase			,      _lowerCamelCase						)						)
                 _UpperCAmelCase				:    Optional[Any]											=       outputs.attentions
                 self.assertEqual(len(_lowerCamelCase						)			,      self.model_tester.num_hidden_layers						)

                 self.assertListEqual(
                     list(attentions[0].shape[-3:]						)			,      [self.model_tester.num_attention_heads, seq_len, seq_len]			,      )
                 _UpperCAmelCase				:    str											=       len(_lowerCamelCase						)

                 # Check attention is always last and order is fine
                 _UpperCAmelCase				:    int											=       True
                 _UpperCAmelCase				:    str											=       True
                 _UpperCAmelCase				:    str											=       model_class(_lowerCamelCase						)
                 model.to(_lowerCamelCase						)
                 model.eval()
                 with torch.no_grad():
                     _UpperCAmelCase				:    Optional[Any]											=       model(**self._prepare_for_class(_lowerCamelCase			,      _lowerCamelCase						)						)

                 self.assertEqual(out_len + 1			,      len(_lowerCamelCase						)						)

                 _UpperCAmelCase				:    List[Any]											=       outputs.attentions

                 self.assertEqual(len(_lowerCamelCase						)			,      self.model_tester.num_hidden_layers						)
                 self.assertListEqual(
                     list(self_attentions[0].shape[-3:]						)			,      [self.model_tester.num_attention_heads, seq_len, seq_len]			,      )





     def 					__lowerCAmelCase      (   self						)				->   List[str]:
         def check_hidden_states_output(A			,      A			,      A						):
             _UpperCAmelCase				:    Dict											=       model_class(_lowerCamelCase						)
             model.to(_lowerCamelCase						)
             model.eval()

             with torch.no_grad():
                 _UpperCAmelCase				:    Union[str, Any]											=       model(**self._prepare_for_class(_lowerCamelCase			,      _lowerCamelCase						)						)

             _UpperCAmelCase				:    Dict											=       outputs.hidden_states
             _UpperCAmelCase				:    Union[str, Any]											=       self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(_lowerCamelCase						)			,      _lowerCamelCase						)

             _UpperCAmelCase				:    Any											=       self.model_tester.seq_length - self.model_tester.num_masks
             _UpperCAmelCase				:    Union[str, Any]											=       num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]						)			,      [seq_length, self.model_tester.hidden_size]			,      )

         _UpperCAmelCase				,							_UpperCAmelCase				:    Optional[Any]											=       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             _UpperCAmelCase				:    Any											=       True
             check_hidden_states_output(_lowerCamelCase			,      _lowerCamelCase			,      _lowerCamelCase						)

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

             check_hidden_states_output(_lowerCamelCase			,      _lowerCamelCase			,      _lowerCamelCase						)





     @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''						)
     def 					__lowerCAmelCase      (   self						)				->   List[Any]:
         pass





def 					lowerCamelCase_		():
    _UpperCAmelCase				:    int											=       hf_hub_download(
        repo_id='''hf-internal-testing/spaghetti-video'''	,			filename='''eating_spaghetti.npy'''	,			repo_type='''dataset'''				)
    _UpperCAmelCase				:    Dict											=       np.load(UpperCamelCase__				)
    return list(UpperCamelCase__				)


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





     '''simple docstring'''



     @cached_property
     def 					__lowerCAmelCase      (   self						)				->   str:
         return (
             VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]			,      image_std=[0.5, 0.5, 0.5]						)
             if is_vision_available()
             else None
         )





     @slow
     def 					__lowerCAmelCase      (   self						)				->   List[str]:
         _UpperCAmelCase				:    Tuple											=       VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''						).to(
             _lowerCamelCase						)

         _UpperCAmelCase				:    Any											=       self.default_image_processor
         _UpperCAmelCase				:    List[Any]											=       prepare_video()
         _UpperCAmelCase				:    int											=       image_processor(_lowerCamelCase			,      return_tensors='''pt'''						).to(_lowerCamelCase						)

         # forward pass
         with torch.no_grad():
             _UpperCAmelCase				:    Union[str, Any]											=       model(**_lowerCamelCase						)

         # verify the logits
         _UpperCAmelCase				:    List[str]											=       torch.Size((1, 4_0_0)						)
         self.assertEqual(outputs.logits.shape			,      _lowerCamelCase						)

         _UpperCAmelCase				:    Optional[Any]											=       torch.tensor([0.3_669, -0.0_688, -0.2_421]						).to(_lowerCamelCase						)

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





     @slow
     def 					__lowerCAmelCase      (   self						)				->   Tuple:
         _UpperCAmelCase				:    str											=       VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''						).to(_lowerCamelCase						)

         _UpperCAmelCase				:    Optional[Any]											=       self.default_image_processor
         _UpperCAmelCase				:    Tuple											=       prepare_video()
         _UpperCAmelCase				:    Any											=       image_processor(_lowerCamelCase			,      return_tensors='''pt'''						).to(_lowerCamelCase						)

         # add boolean mask, indicating which patches to mask
         _UpperCAmelCase				:    List[str]											=       hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos'''			,      filename='''bool_masked_pos.pt'''						)
         _UpperCAmelCase				:    Tuple											=       torch.load(_lowerCamelCase						)

         # forward pass
         with torch.no_grad():
             _UpperCAmelCase				:    int											=       model(**_lowerCamelCase						)

         # verify the logits
         _UpperCAmelCase				:    int											=       torch.Size([1, 1_4_0_8, 1_5_3_6]						)
         _UpperCAmelCase				:    List[Any]											=       torch.tensor(
             [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]]			,      device=_lowerCamelCase						)
         self.assertEqual(outputs.logits.shape			,      _lowerCamelCase						)
         self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]			,      _lowerCamelCase			,      atol=1E-4						)						)

         # verify the loss (`config.norm_pix_loss` = `True`)
         _UpperCAmelCase				:    Any											=       torch.tensor([0.5_142]			,      device=_lowerCamelCase						)
         self.assertTrue(torch.allclose(outputs.loss			,      _lowerCamelCase			,      atol=1E-4						)						)

         # verify the loss (`config.norm_pix_loss` = `False`)
         _UpperCAmelCase				:    Dict											=       VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''			,      norm_pix_loss=_lowerCamelCase						).to(
             _lowerCamelCase						)

         with torch.no_grad():
             _UpperCAmelCase				:    Union[str, Any]											=       model(**_lowerCamelCase						)

         _UpperCAmelCase				:    str											=       torch.tensor(torch.tensor([0.6_469]						)			,      device=_lowerCamelCase						)
         self.assertTrue(torch.allclose(outputs.loss			,      _lowerCamelCase			,      atol=1E-4						)						)




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



							'''simple docstring'''







							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[str]:




										'''simple docstring'''
										__lowercase       =       self.config_class(**self.inputs_dict       )
										self.parent.assertTrue(hasattr(_lowerCamelCase  ,    "hidden_sizes"       )       )
										self.parent.assertTrue(hasattr(_lowerCamelCase  ,    "neck_hidden_sizes"       )       )
										self.parent.assertTrue(hasattr(_lowerCamelCase  ,    "num_attention_heads"       )       )





class 					__a     :



							'''simple docstring'''







							def __init__(     self  ,    _lowerCamelCase  ,    _lowerCamelCase=13  ,    _lowerCamelCase=32  ,    _lowerCamelCase=2  ,    _lowerCamelCase=3  ,    _lowerCamelCase=640  ,    _lowerCamelCase=4  ,    _lowerCamelCase="silu"  ,    _lowerCamelCase=3  ,    _lowerCamelCase=32  ,    _lowerCamelCase=0.1  ,    _lowerCamelCase=0.1  ,    _lowerCamelCase=0.1  ,    _lowerCamelCase=0.02  ,    _lowerCamelCase=True  ,    _lowerCamelCase=True  ,    _lowerCamelCase=10  ,    _lowerCamelCase=None  ,    )			->							Tuple:




										'''simple docstring'''
										__lowercase       =       parent
										__lowercase       =       batch_size
										__lowercase       =       image_size
										__lowercase       =       patch_size
										__lowercase       =       num_channels
										__lowercase       =       last_hidden_size
										__lowercase       =       num_attention_heads
										__lowercase       =       hidden_act
										__lowercase       =       conv_kernel_size
										__lowercase       =       output_stride
										__lowercase       =       hidden_dropout_prob
										__lowercase       =       attention_probs_dropout_prob
										__lowercase       =       classifier_dropout_prob
										__lowercase       =       use_labels
										__lowercase       =       is_training
										__lowercase       =       num_labels
										__lowercase       =       initializer_range
										__lowercase       =       scope
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Tuple:




										'''simple docstring'''
										__lowercase       =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

										__lowercase       =       self.get_config()

										return config, pixel_values, labels, pixel_labels
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Tuple:




										'''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   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							str:




										'''simple docstring'''
										__lowercase       =       MobileViTModel(config=_lowerCamelCase       )
										model.to(_lowerCamelCase       )
										model.eval()
										__lowercase       =       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,
										    )  ,    )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							int:




										'''simple docstring'''
										__lowercase       =       self.num_labels
										__lowercase       =       MobileViTForImageClassification(_lowerCamelCase       )
										model.to(_lowerCamelCase       )
										model.eval()
										__lowercase       =       model(_lowerCamelCase  ,    labels=_lowerCamelCase       )
										self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_labels)       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							Optional[int]:




										'''simple docstring'''
										__lowercase       =       self.num_labels
										__lowercase       =       MobileViTForSemanticSegmentation(_lowerCamelCase       )
										model.to(_lowerCamelCase       )
										model.eval()
										__lowercase       =       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,
										    )  ,    )
										__lowercase       =       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   SCREAMING_SNAKE_CASE						(     self       )			->							List[str]:




										'''simple docstring'''
										__lowercase       =       self.prepare_config_and_inputs()
										__lowercase   ,					__lowercase   ,					__lowercase   ,					__lowercase       =       config_and_inputs
										__lowercase       =       {"pixel_values": pixel_values}
										return config, inputs_dict





@require_torch
class 					__a     ( __a					,   __a					,   unittest.TestCase       ):



							'''simple docstring'''







							_lowerCamelCase					:			str          =   (
							    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)
							_lowerCamelCase					:			Optional[int]          =   (
							    {
							        """feature-extraction""": MobileViTModel,
							        """image-classification""": MobileViTForImageClassification,
							        """image-segmentation""": MobileViTForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							_lowerCamelCase					:			Tuple          =   False
							_lowerCamelCase					:			Optional[Any]          =   False
							_lowerCamelCase					:			Any          =   False
							_lowerCamelCase					:			List[Any]          =   False
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Tuple:




										'''simple docstring'''
										__lowercase       =       MobileViTModelTester(self       )
										__lowercase       =       MobileViTConfigTester(self  ,    config_class=_lowerCamelCase  ,    has_text_modality=_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Dict:




										'''simple docstring'''
										self.config_tester.run_common_tests()
							@unittest.skip(reason="MobileViT does not use inputs_embeds"       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							str:




										'''simple docstring'''
										pass
							@unittest.skip(reason="MobileViT does not support input and output embeddings"       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Dict:




										'''simple docstring'''
										pass
							@unittest.skip(reason="MobileViT does not output attentions"       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Dict:




										'''simple docstring'''
										pass
							def   SCREAMING_SNAKE_CASE						(     self       )			->							int:




										'''simple docstring'''
										__lowercase   ,					__lowercase       =       self.model_tester.prepare_config_and_inputs_for_common()

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

													__lowercase       =       ["pixel_values"]
													self.assertListEqual(arg_names[:1]  ,    _lowerCamelCase       )
							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Dict:




										'''simple docstring'''
										pass
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Tuple:




										'''simple docstring'''
										__lowercase       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[Any]:




										'''simple docstring'''
										def check_hidden_states_output(_lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       ):
													__lowercase       =       model_class(_lowerCamelCase       )
													model.to(_lowerCamelCase       )
													model.eval()

													with torch.no_grad():
																__lowercase       =       model(**self._prepare_for_class(_lowerCamelCase  ,    _lowerCamelCase       )       )

													__lowercase       =       outputs.hidden_states

													__lowercase       =       5
													self.assertEqual(len(_lowerCamelCase       )  ,    _lowerCamelCase       )

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

										__lowercase   ,					__lowercase       =       self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													__lowercase       =       True
													check_hidden_states_output(_lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )

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

													check_hidden_states_output(_lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[str]:




										'''simple docstring'''
										__lowercase       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[Any]:




										'''simple docstring'''
										__lowercase       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCamelCase       )





							@slow
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Tuple:




										'''simple docstring'''
										for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowercase       =       MobileViTModel.from_pretrained(_lowerCamelCase       )
													self.assertIsNotNone(_lowerCamelCase       )





def      lowerCAmelCase__			(		)						->List[Any]:
			__lowercase       =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
			return image







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



							'''simple docstring'''







							@cached_property
							def   SCREAMING_SNAKE_CASE						(     self       )			->							str:




										'''simple docstring'''
										return MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"       ) if is_vision_available() else None
							@slow
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[int]:




										'''simple docstring'''
										__lowercase       =       MobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"       ).to(_lowerCamelCase       )

										__lowercase       =       self.default_image_processor
										__lowercase       =       prepare_img()
										__lowercase       =       image_processor(images=_lowerCamelCase  ,    return_tensors="pt"       ).to(_lowerCamelCase       )

										# forward pass
										with torch.no_grad():
													__lowercase       =       model(**_lowerCamelCase       )

										# verify the logits
										__lowercase       =       torch.Size((1, 1_000)       )
										self.assertEqual(outputs.logits.shape  ,    _lowerCamelCase       )

										__lowercase       =       torch.tensor([-1.9364, -1.2327, -0.4653]       ).to(_lowerCamelCase       )

										self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,    _lowerCamelCase  ,    atol=1e-4       )       )
							@slow
							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[str]:




										'''simple docstring'''
										__lowercase       =       MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )
										__lowercase       =       model.to(_lowerCamelCase       )

										__lowercase       =       MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )

										__lowercase       =       prepare_img()
										__lowercase       =       image_processor(images=_lowerCamelCase  ,    return_tensors="pt"       ).to(_lowerCamelCase       )

										# forward pass
										with torch.no_grad():
													__lowercase       =       model(**_lowerCamelCase       )
										__lowercase       =       outputs.logits

										# verify the logits
										__lowercase       =       torch.Size((1, 21, 32, 32)       )
										self.assertEqual(logits.shape  ,    _lowerCamelCase       )

										__lowercase       =       torch.tensor(
										    [
										        [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
										        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
										        [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
										    ]  ,    device=_lowerCamelCase  ,    )

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





							@slow
							def   SCREAMING_SNAKE_CASE						(     self       )			->							List[str]:




										'''simple docstring'''
										__lowercase       =       MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )
										__lowercase       =       model.to(_lowerCamelCase       )

										__lowercase       =       MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )

										__lowercase       =       prepare_img()
										__lowercase       =       image_processor(images=_lowerCamelCase  ,    return_tensors="pt"       ).to(_lowerCamelCase       )

										# forward pass
										with torch.no_grad():
													__lowercase       =       model(**_lowerCamelCase       )

										__lowercase       =       outputs.logits.detach().cpu()

										__lowercase       =       image_processor.post_process_semantic_segmentation(outputs=_lowerCamelCase  ,    target_sizes=[(50, 60)]       )
										__lowercase       =       torch.Size((50, 60)       )
										self.assertEqual(segmentation[0].shape  ,    _lowerCamelCase       )

										__lowercase       =       image_processor.post_process_semantic_segmentation(outputs=_lowerCamelCase       )
										__lowercase       =       torch.Size((32, 32)       )
										self.assertEqual(segmentation[0].shape  ,    _lowerCamelCase       )





