





import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VivitConfig
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


if is_torch_available():
     import torch
     from torch import nn

     from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VivitForVideoClassification, VivitModel
     from transformers.models.vivit.modeling_vivit import VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from transformers import VivitImageProcessor



class 							_UpperCamelCase    :

       def __init__(						self:       Union[str, Any]    ,      _SCREAMING_SNAKE_CASE:       int    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]=2    ,      _SCREAMING_SNAKE_CASE:       str=True    ,      _SCREAMING_SNAKE_CASE:       Any=True    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=10    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=10    ,      _SCREAMING_SNAKE_CASE:       Dict=8    ,      _SCREAMING_SNAKE_CASE:       List[Any]=[2, 4, 4]    ,      _SCREAMING_SNAKE_CASE:       Dict=3    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=768    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=5    ,      _SCREAMING_SNAKE_CASE:       str=4    ,      _SCREAMING_SNAKE_CASE:       List[str]=37    ,      _SCREAMING_SNAKE_CASE:       Dict="gelu_fast"    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=0.0    ,      _SCREAMING_SNAKE_CASE:       Dict=0.0    ,      _SCREAMING_SNAKE_CASE:       int=0.02    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=1e-06    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=True    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]=None    ,      )				->       Union[str, Any]:




              """simple docstring"""


              UpperCamelCase_      				=     parent
              UpperCamelCase_      				=     batch_size
              UpperCamelCase_      				=     is_training
              UpperCamelCase_      				=     use_labels
              UpperCamelCase_      				=     num_labels
              UpperCamelCase_      				=     image_size
              UpperCamelCase_      				=     num_frames
              UpperCamelCase_      				=     tubelet_size
              UpperCamelCase_      				=     num_channels
              UpperCamelCase_      				=     hidden_size
              UpperCamelCase_      				=     num_hidden_layers
              UpperCamelCase_      				=     num_attention_heads
              UpperCamelCase_      				=     intermediate_size
              UpperCamelCase_      				=     hidden_act
              UpperCamelCase_      				=     hidden_dropout_prob
              UpperCamelCase_      				=     attention_probs_dropout_prob
              UpperCamelCase_      				=     initializer_range
              UpperCamelCase_      				=     layer_norm_eps
              UpperCamelCase_      				=     qkv_bias
              UpperCamelCase_      				=     scope

              UpperCamelCase_      				=     (
                  (self.image_size // self.tubelet_size[2])
                  * (self.image_size // self.tubelet_size[1])
                  * (self.num_frames // self.tubelet_size[0])
              ) + 1  # CLS token

       def lowercase       (						self:       Union[str, Any]						)				->       Optional[int]:




              """simple docstring"""


              UpperCamelCase_      				=     floats_tensor(
                  [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]						)

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

              UpperCamelCase_      				=     self.get_config()

              return config, pixel_values, labels

       def lowercase       (						self:       Optional[int]						)				->       str:




              """simple docstring"""


              UpperCamelCase_      				=     VivitConfig(
                  num_frames=self.num_frames    ,      image_size=self.image_size    ,      tubelet_size=self.tubelet_size    ,      num_channels=self.num_channels    ,      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    ,      initializer_range=self.initializer_range    ,      layer_norm_eps=self.layer_norm_eps    ,      qkv_bias=self.qkv_bias    ,      )
              UpperCamelCase_      				=     self.num_labels
              return config

       def lowercase       (						self:       Dict    ,      _SCREAMING_SNAKE_CASE:       Dict    ,      _SCREAMING_SNAKE_CASE:       Optional[int]    ,      _SCREAMING_SNAKE_CASE:       int						)				->       Dict:




              """simple docstring"""


              UpperCamelCase_      				=     VivitModel(config=_SCREAMING_SNAKE_CASE						)
              model.to(_SCREAMING_SNAKE_CASE						)
              model.eval()
              UpperCamelCase_      				=     model(_SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.last_hidden_state.shape    ,      (self.batch_size, self.seq_length, self.hidden_size)						)

       def lowercase       (						self:       List[str]    ,      _SCREAMING_SNAKE_CASE:       Tuple    ,      _SCREAMING_SNAKE_CASE:       Any    ,      _SCREAMING_SNAKE_CASE:       Optional[int]						)				->       Dict:




              """simple docstring"""


              UpperCamelCase_      				=     VivitForVideoClassification(_SCREAMING_SNAKE_CASE						)
              model.to(_SCREAMING_SNAKE_CASE						)
              model.eval()

              UpperCamelCase_      				=     model(_SCREAMING_SNAKE_CASE						)

              # verify the logits shape
              UpperCamelCase_      				=     torch.Size((self.batch_size, self.num_labels)						)
              self.parent.assertEqual(result.logits.shape    ,      _SCREAMING_SNAKE_CASE						)



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




              """simple docstring"""


              UpperCamelCase_      				=     self.prepare_config_and_inputs()
              UpperCamelCase_				,       UpperCamelCase_				,       UpperCamelCase_      				=     config_and_inputs
              UpperCamelCase_      				=     {"pixel_values": pixel_values}
              return config, inputs_dict



@require_torch
class 							_UpperCamelCase    (		lowerCAmelCase_     ,  unittest.TestCase   ):
       _UpperCamelCase :    List[str]   			=      (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

       _UpperCamelCase :    Union[str, Any]   			=      False
       _UpperCamelCase :    str   			=      False
       _UpperCamelCase :    Union[str, Any]   			=      False
       _UpperCamelCase :    int   			=      False

       def lowercase       (						self:       Union[str, Any]						)				->       Tuple:




              """simple docstring"""


              UpperCamelCase_      				=     VivitModelTester(self						)
              UpperCamelCase_      				=     ConfigTester(self    ,      config_class=_SCREAMING_SNAKE_CASE    ,      has_text_modality=_SCREAMING_SNAKE_CASE    ,      hidden_size=37						)

       def lowercase       (						self:       str    ,      _SCREAMING_SNAKE_CASE:       int    ,      _SCREAMING_SNAKE_CASE:       List[str]    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=False						)				->       Optional[int]:




              """simple docstring"""


              UpperCamelCase_      				=     copy.deepcopy(_SCREAMING_SNAKE_CASE						)

              if return_labels:
                     if model_class in get_values(_SCREAMING_SNAKE_CASE						):
                            UpperCamelCase_      				=     torch.zeros(
                                self.model_tester.batch_size    ,      dtype=torch.long    ,      device=_SCREAMING_SNAKE_CASE						)

              return inputs_dict

       def lowercase       (						self:       int						)				->       Union[str, Any]:




              """simple docstring"""


              self.config_tester.run_common_tests()

       @unittest.skip(reason="Vivit does not use inputs_embeds"						)
       def lowercase       (						self:       int						)				->       int:




              """simple docstring"""


              pass

       def lowercase       (						self:       Any						)				->       Optional[Any]:




              """simple docstring"""


              UpperCamelCase_				,       UpperCamelCase_      				=     self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     UpperCamelCase_      				=     model_class(_SCREAMING_SNAKE_CASE						)
                     self.assertIsInstance(model.get_input_embeddings()    ,      (nn.Module)						)
                     UpperCamelCase_      				=     model.get_output_embeddings()
                     self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE    ,      nn.Linear						)						)

       def lowercase       (						self:       Optional[int]						)				->       List[Any]:




              """simple docstring"""


              UpperCamelCase_				,       UpperCamelCase_      				=     self.model_tester.prepare_config_and_inputs_for_common()

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

                     UpperCamelCase_      				=     ["pixel_values", "head_mask"]
                     self.assertListEqual(arg_names[:2]    ,      _SCREAMING_SNAKE_CASE						)

       def lowercase       (						self:       Optional[int]						)				->       int:




              """simple docstring"""


              UpperCamelCase_      				=     self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)

       def lowercase       (						self:       Union[str, Any]						)				->       Optional[Any]:




              """simple docstring"""


              UpperCamelCase_      				=     self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_video_classification(*_SCREAMING_SNAKE_CASE						)

       @slow
       def lowercase       (						self:       Any						)				->       Union[str, Any]:




              """simple docstring"""


              for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase_      				=     VivitModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
                     self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)

       def lowercase       (						self:       Optional[Any]						)				->       Optional[int]:




              """simple docstring"""


              UpperCamelCase_				,       UpperCamelCase_      				=     self.model_tester.prepare_config_and_inputs_for_common()
              UpperCamelCase_      				=     True

              for model_class in self.all_model_classes:
                     UpperCamelCase_      				=     self.model_tester.seq_length

                     UpperCamelCase_      				=     True
                     UpperCamelCase_      				=     False
                     UpperCamelCase_      				=     True
                     UpperCamelCase_      				=     model_class(_SCREAMING_SNAKE_CASE						)
                     model.to(_SCREAMING_SNAKE_CASE						)
                     model.eval()
                     with torch.no_grad():
                            UpperCamelCase_      				=     model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,      _SCREAMING_SNAKE_CASE						)						)
                     UpperCamelCase_      				=     outputs.attentions
                     self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,      self.model_tester.num_hidden_layers						)

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

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

                     # Check attention is always last and order is fine
                     UpperCamelCase_      				=     True
                     UpperCamelCase_      				=     True
                     UpperCamelCase_      				=     model_class(_SCREAMING_SNAKE_CASE						)
                     model.to(_SCREAMING_SNAKE_CASE						)
                     model.eval()
                     with torch.no_grad():
                            UpperCamelCase_      				=     model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,      _SCREAMING_SNAKE_CASE						)						)

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

                     UpperCamelCase_      				=     outputs.attentions

                     self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,      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 lowercase       (						self:       Optional[int]						)				->       Optional[Any]:




              """simple docstring"""


              def check_hidden_states_output(_SCREAMING_SNAKE_CASE:       Dict    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]						):
                     UpperCamelCase_      				=     model_class(_SCREAMING_SNAKE_CASE						)
                     model.to(_SCREAMING_SNAKE_CASE						)
                     model.eval()

                     with torch.no_grad():
                            UpperCamelCase_      				=     model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,      _SCREAMING_SNAKE_CASE						)						)

                     UpperCamelCase_      				=     outputs.hidden_states
                     UpperCamelCase_      				=     self.model_tester.num_hidden_layers + 1
                     self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,      _SCREAMING_SNAKE_CASE						)

                     UpperCamelCase_      				=     self.model_tester.seq_length

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

              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(_SCREAMING_SNAKE_CASE    ,      _SCREAMING_SNAKE_CASE    ,      _SCREAMING_SNAKE_CASE						)

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

                     check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,      _SCREAMING_SNAKE_CASE    ,      _SCREAMING_SNAKE_CASE						)



       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
       def lowercase       (						self:       List[Any]						)				->       Union[str, Any]:




              """simple docstring"""


              pass




def        lowerCAmelCase_       (							)   ->						Optional[int]:
       UpperCamelCase_      				=     hf_hub_download(
           repo_id="hf-internal-testing/spaghetti-video"    ,						filename="eating_spaghetti_32_frames.npy"    ,						repo_type="dataset"     )
       UpperCamelCase_      				=     np.load(UpperCamelCase_     )
       return list(UpperCamelCase_     )







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

       @cached_property
       def lowercase       (						self:       Any						)				->       Union[str, Any]:




              """simple docstring"""


              return VivitImageProcessor() if is_vision_available() else None



       @slow
       def lowercase       (						self:       Dict						)				->       int:




              """simple docstring"""


              UpperCamelCase_      				=     VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"						).to(_SCREAMING_SNAKE_CASE						)

              UpperCamelCase_      				=     self.default_image_processor
              UpperCamelCase_      				=     prepare_video()
              UpperCamelCase_      				=     image_processor(_SCREAMING_SNAKE_CASE    ,      return_tensors="pt"						).to(_SCREAMING_SNAKE_CASE						)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase_      				=     model(**_SCREAMING_SNAKE_CASE						)

              # verify the logits
              UpperCamelCase_      				=     torch.Size((1, 400)						)
              self.assertEqual(outputs.logits.shape    ,      _SCREAMING_SNAKE_CASE						)

              # taken from original model
              UpperCamelCase_      				=     torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]						).to(_SCREAMING_SNAKE_CASE						)

              self.assertTrue(torch.allclose(outputs.logits[0, :5]    ,      _SCREAMING_SNAKE_CASE    ,      atol=1e-4						)						)








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


_UpperCAmelCase				       = logging.get_logger(__name__)

_UpperCAmelCase				       = {
    'MCG-NJU/videomae-base': 'https://huggingface.co/MCG-NJU/videomae-base/resolve/main/config.json',
}



class 							_UpperCamelCase    (		lowerCAmelCase_   ):
       _UpperCamelCase :    List[Any]   			=      '''videomae'''

       def __init__(						self:       Optional[Any]    ,      _SCREAMING_SNAKE_CASE:       List[str]=224    ,      _SCREAMING_SNAKE_CASE:       Dict=16    ,      _SCREAMING_SNAKE_CASE:       List[Any]=3    ,      _SCREAMING_SNAKE_CASE:       Tuple=16    ,      _SCREAMING_SNAKE_CASE:       Tuple=2    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=768    ,      _SCREAMING_SNAKE_CASE:       str=12    ,      _SCREAMING_SNAKE_CASE:       List[Any]=12    ,      _SCREAMING_SNAKE_CASE:       Dict=3072    ,      _SCREAMING_SNAKE_CASE:       Tuple="gelu"    ,      _SCREAMING_SNAKE_CASE:       List[Any]=0.0    ,      _SCREAMING_SNAKE_CASE:       List[Any]=0.0    ,      _SCREAMING_SNAKE_CASE:       Tuple=0.02    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=1e-12    ,      _SCREAMING_SNAKE_CASE:       Dict=True    ,      _SCREAMING_SNAKE_CASE:       Dict=True    ,      _SCREAMING_SNAKE_CASE:       List[str]=6    ,      _SCREAMING_SNAKE_CASE:       Dict=384    ,      _SCREAMING_SNAKE_CASE:       Any=4    ,      _SCREAMING_SNAKE_CASE:       Dict=1536    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=True    ,      **_SCREAMING_SNAKE_CASE:       str    ,      )				->       Dict:




              """simple docstring"""


              super().__init__(**_SCREAMING_SNAKE_CASE						)

              UpperCamelCase_      				=     image_size
              UpperCamelCase_      				=     patch_size
              UpperCamelCase_      				=     num_channels
              UpperCamelCase_      				=     num_frames
              UpperCamelCase_      				=     tubelet_size

              UpperCamelCase_      				=     hidden_size
              UpperCamelCase_      				=     num_hidden_layers
              UpperCamelCase_      				=     num_attention_heads
              UpperCamelCase_      				=     intermediate_size
              UpperCamelCase_      				=     hidden_act
              UpperCamelCase_      				=     hidden_dropout_prob
              UpperCamelCase_      				=     attention_probs_dropout_prob
              UpperCamelCase_      				=     initializer_range
              UpperCamelCase_      				=     layer_norm_eps
              UpperCamelCase_      				=     qkv_bias
              UpperCamelCase_      				=     use_mean_pooling

              UpperCamelCase_      				=     decoder_num_attention_heads
              UpperCamelCase_      				=     decoder_hidden_size
              UpperCamelCase_      				=     decoder_num_hidden_layers
              UpperCamelCase_      				=     decoder_intermediate_size
              UpperCamelCase_      				=     norm_pix_loss

