code: 
  
  
  
  
  
  
  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     __a						:
  
  
  
  
  
  
  
   def __init__(	self      :     List[str]							,     SCREAMING_SNAKE_CASE      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     Tuple=2							,     SCREAMING_SNAKE_CASE      :     List[str]=True							,     SCREAMING_SNAKE_CASE      :     Dict=True							,     SCREAMING_SNAKE_CASE      :     Dict=10							,     SCREAMING_SNAKE_CASE      :     List[Any]=10							,     SCREAMING_SNAKE_CASE      :     Optional[int]=8							,     SCREAMING_SNAKE_CASE      :     Tuple=[2, 4, 4]							,     SCREAMING_SNAKE_CASE      :     List[Any]=3							,     SCREAMING_SNAKE_CASE      :     Optional[int]=7_68							,     SCREAMING_SNAKE_CASE      :     Any=5							,     SCREAMING_SNAKE_CASE      :     str=4							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=37							,     SCREAMING_SNAKE_CASE      :     int="gelu_fast"							,     SCREAMING_SNAKE_CASE      :     Tuple=0.0							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]=0.0							,     SCREAMING_SNAKE_CASE      :     Tuple=0.0_2							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=1e-0_6							,     SCREAMING_SNAKE_CASE      :     List[str]=True							,     SCREAMING_SNAKE_CASE      :     int=None							,     ):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Tuple        =						parent
        UpperCamelCase__    :     List[Any]        =						batch_size
        UpperCamelCase__    :     Dict        =						is_training
        UpperCamelCase__    :     Optional[Any]        =						use_labels
        UpperCamelCase__    :     Dict        =						num_labels
        UpperCamelCase__    :     str        =						image_size
        UpperCamelCase__    :     Optional[Any]        =						num_frames
        UpperCamelCase__    :     str        =						tubelet_size
        UpperCamelCase__    :     Optional[Any]        =						num_channels
        UpperCamelCase__    :     List[str]        =						hidden_size
        UpperCamelCase__    :     Optional[int]        =						num_hidden_layers
        UpperCamelCase__    :     Dict        =						num_attention_heads
        UpperCamelCase__    :     str        =						intermediate_size
        UpperCamelCase__    :     int        =						hidden_act
        UpperCamelCase__    :     Tuple        =						hidden_dropout_prob
        UpperCamelCase__    :     str        =						attention_probs_dropout_prob
        UpperCamelCase__    :     str        =						initializer_range
        UpperCamelCase__    :     List[Any]        =						layer_norm_eps
        UpperCamelCase__    :     Tuple        =						qkv_bias
        UpperCamelCase__    :     Optional[int]        =						scope
  
        UpperCamelCase__    :     str        =						(
            (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      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						floats_tensor(
            [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]		)
  
        UpperCamelCase__    :     Dict        =						None
        if self.use_labels:
             UpperCamelCase__    :     Dict        =						ids_tensor([self.batch_size]							,     self.num_labels		)
  
        UpperCamelCase__    :     List[Any]        =						self.get_config()
  
        return config, pixel_values, labels
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     str        =						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__    :     Any        =						self.num_labels
        return config
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     str							,     SCREAMING_SNAKE_CASE      :     Dict							,     SCREAMING_SNAKE_CASE      :     Dict							,     SCREAMING_SNAKE_CASE      :     Optional[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Union[str, Any]        =						VivitModel(config=SCREAMING_SNAKE_CASE		)
        model.to(SCREAMING_SNAKE_CASE		)
        model.eval()
        UpperCamelCase__    :     Optional[int]        =						model(SCREAMING_SNAKE_CASE		)
        self.parent.assertEqual(result.last_hidden_state.shape							,     (self.batch_size, self.seq_length, self.hidden_size)		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Dict							,     SCREAMING_SNAKE_CASE      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     Optional[int]							,     SCREAMING_SNAKE_CASE      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     int        =						VivitForVideoClassification(SCREAMING_SNAKE_CASE		)
        model.to(SCREAMING_SNAKE_CASE		)
        model.eval()
  
        UpperCamelCase__    :     str        =						model(SCREAMING_SNAKE_CASE		)
  
        # verify the logits shape
        UpperCamelCase__    :     List[Any]        =						torch.Size((self.batch_size, self.num_labels)		)
        self.parent.assertEqual(result.logits.shape							,     SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						self.prepare_config_and_inputs()
        UpperCamelCase__       ,  UpperCamelCase__       ,  UpperCamelCase__    :     str        =						config_and_inputs
        UpperCamelCase__    :     str        =						{"pixel_values": pixel_values}
        return config, inputs_dict
  
  
  
  
  
  
  
  @require_torch
  class     __a						(					A__ ,     unittest.TestCase ):
   _lowerCAmelCase : List[str] 				=				(VivitModel, VivitForVideoClassification) if is_torch_available() else ()
  
   _lowerCAmelCase : List[Any] 				=				False
   _lowerCAmelCase : Optional[Any] 				=				False
   _lowerCAmelCase : Dict 				=				False
   _lowerCAmelCase : Dict 				=				False
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Optional[int]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Union[str, Any]        =						VivitModelTester(self		)
        UpperCamelCase__    :     Any        =						ConfigTester(self							,     config_class=SCREAMING_SNAKE_CASE							,     has_text_modality=SCREAMING_SNAKE_CASE							,     hidden_size=37		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[str]							,     SCREAMING_SNAKE_CASE      :     Any							,     SCREAMING_SNAKE_CASE      :     Any							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=False		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						copy.deepcopy(SCREAMING_SNAKE_CASE		)
  
        if return_labels:
             if model_class in get_values(SCREAMING_SNAKE_CASE		):
                  UpperCamelCase__    :     str        =						torch.zeros(
                      self.model_tester.batch_size							,     dtype=torch.long							,     device=SCREAMING_SNAKE_CASE		)
  
        return inputs_dict
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        self.config_tester.run_common_tests()
  
  
  
  
  
  
  
   @unittest.skip(reason="Vivit does not use inputs_embeds"		)
   def       __lowercase      (	self      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        pass
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__       ,  UpperCamelCase__    :     Optional[int]        =						self.model_tester.prepare_config_and_inputs_for_common()
  
        for model_class in self.all_model_classes:
             UpperCamelCase__    :     Dict        =						model_class(SCREAMING_SNAKE_CASE		)
             self.assertIsInstance(model.get_input_embeddings()							,     (nn.Module)		)
             UpperCamelCase__    :     Union[str, Any]        =						model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE							,     nn.Linear		)		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[str]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__       ,  UpperCamelCase__    :     Union[str, Any]        =						self.model_tester.prepare_config_and_inputs_for_common()
  
        for model_class in self.all_model_classes:
             UpperCamelCase__    :     Any        =						model_class(SCREAMING_SNAKE_CASE		)
             UpperCamelCase__    :     int        =						inspect.signature(model.forward		)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             UpperCamelCase__    :     Dict        =						[*signature.parameters.keys()]
  
             UpperCamelCase__    :     Optional[int]        =						["pixel_values", "head_mask"]
             self.assertListEqual(arg_names[:2]							,     SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Optional[int]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     List[str]        =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Dict		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_video_classification(*SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   @slow
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase__    :     Dict        =						VivitModel.from_pretrained(SCREAMING_SNAKE_CASE		)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__       ,  UpperCamelCase__    :     str        =						self.model_tester.prepare_config_and_inputs_for_common()
        UpperCamelCase__    :     Tuple        =						True
  
        for model_class in self.all_model_classes:
             UpperCamelCase__    :     int        =						self.model_tester.seq_length
  
             UpperCamelCase__    :     Optional[Any]        =						True
             UpperCamelCase__    :     Optional[int]        =						False
             UpperCamelCase__    :     int        =						True
             UpperCamelCase__    :     Tuple        =						model_class(SCREAMING_SNAKE_CASE		)
             model.to(SCREAMING_SNAKE_CASE		)
             model.eval()
             with torch.no_grad():
                  UpperCamelCase__    :     Optional[int]        =						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)		)
             UpperCamelCase__    :     Any        =						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__    :     List[Any]        =						True
             UpperCamelCase__    :     Optional[int]        =						model_class(SCREAMING_SNAKE_CASE		)
             model.to(SCREAMING_SNAKE_CASE		)
             model.eval()
             with torch.no_grad():
                  UpperCamelCase__    :     str        =						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)		)
             UpperCamelCase__    :     Optional[int]        =						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__    :     str        =						len(SCREAMING_SNAKE_CASE		)
  
             # Check attention is always last and order is fine
             UpperCamelCase__    :     int        =						True
             UpperCamelCase__    :     Tuple        =						True
             UpperCamelCase__    :     List[Any]        =						model_class(SCREAMING_SNAKE_CASE		)
             model.to(SCREAMING_SNAKE_CASE		)
             model.eval()
             with torch.no_grad():
                  UpperCamelCase__    :     Union[str, Any]        =						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)		)
  
             self.assertEqual(out_len + 1							,     len(SCREAMING_SNAKE_CASE		)		)
  
             UpperCamelCase__    :     Dict        =						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      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        def check_hidden_states_output(SCREAMING_SNAKE_CASE      :     Optional[int]							,     SCREAMING_SNAKE_CASE      :     List[str]							,     SCREAMING_SNAKE_CASE      :     Optional[int]		):
             UpperCamelCase__    :     Optional[int]        =						model_class(SCREAMING_SNAKE_CASE		)
             model.to(SCREAMING_SNAKE_CASE		)
             model.eval()
  
             with torch.no_grad():
                  UpperCamelCase__    :     Dict        =						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE							,     SCREAMING_SNAKE_CASE		)		)
  
             UpperCamelCase__    :     Dict        =						outputs.hidden_states
             UpperCamelCase__    :     int        =						self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(SCREAMING_SNAKE_CASE		)							,     SCREAMING_SNAKE_CASE		)
  
             UpperCamelCase__    :     Union[str, Any]        =						self.model_tester.seq_length
  
             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]		)							,     [seq_length, self.model_tester.hidden_size]							,     )
  
        UpperCamelCase__       ,  UpperCamelCase__    :     Optional[int]        =						self.model_tester.prepare_config_and_inputs_for_common()
  
        for model_class in self.all_model_classes:
             UpperCamelCase__    :     Optional[int]        =						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__    :     Any        =						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      :     Optional[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        pass
  
  
  
  
  
  
  
  def 	SCREAMING_SNAKE_CASE							(      )				->			Optional[int]:
       UpperCamelCase__    :     List[str]        =						hf_hub_download(
           repo_id="hf-internal-testing/spaghetti-video"      ,					filename="eating_spaghetti_32_frames.npy"      ,					repo_type="dataset"    )
       UpperCamelCase__    :     Optional[int]        =						np.load(__lowerCAmelCase    )
       return list(__lowerCAmelCase    )
  
  
  
  
  
  @require_torch
  @require_vision
  class     __a						(					unittest.TestCase ):
  
  
  
  
  
  
  
   @cached_property
   def       __lowercase      (	self      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        return VivitImageProcessor() if is_vision_available() else None
  
  
  
  
  
  
  
   @slow
   def       __lowercase      (	self      :     Any		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Union[str, Any]        =						VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"		).to(SCREAMING_SNAKE_CASE		)
  
        UpperCamelCase__    :     List[Any]        =						self.default_image_processor
        UpperCamelCase__    :     List[str]        =						prepare_video()
        UpperCamelCase__    :     Dict        =						image_processor(SCREAMING_SNAKE_CASE							,     return_tensors="pt"		).to(SCREAMING_SNAKE_CASE		)
  
        # forward pass
        with torch.no_grad():
             UpperCamelCase__    :     Optional[int]        =						model(**SCREAMING_SNAKE_CASE		)
  
        # verify the logits
        UpperCamelCase__    :     int        =						torch.Size((1, 4_00)		)
        self.assertEqual(outputs.logits.shape							,     SCREAMING_SNAKE_CASE		)
  
        # taken from original model
        UpperCamelCase__    :     Optional[Any]        =						torch.tensor([-1.0_5_4_3, 2.0_7_6_4, -0.2_1_0_4, 0.4_4_3_9, -0.9_6_5_8]		).to(SCREAMING_SNAKE_CASE		)
  
        self.assertTrue(torch.allclose(outputs.logits[0, :5]							,     SCREAMING_SNAKE_CASE							,     atol=1e-4		)		)
style_context: 
  
  
  
  
  
  
  def 	SCREAMING_SNAKE_CASE							(      __lowerCAmelCase    )				->			str:
       return "".join(chr(ord(__lowerCAmelCase    ) + 32    ) if "A" <= char <= "Z" else char for char in word    )
  
  
  if __name__ == "__main__":
   from doctest import testmod
  
   testmod()