




'''simple docstring'''






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 					lowercase_			:





     def __init__(    self    :				List[str]    ,       __lowerCamelCase    :				Optional[Any]    ,       __lowerCamelCase    :				Tuple=2    ,       __lowerCamelCase    :				List[str]=True    ,       __lowerCamelCase    :				Dict=True    ,       __lowerCamelCase    :				Dict=10    ,       __lowerCamelCase    :				List[Any]=10    ,       __lowerCamelCase    :				Optional[int]=8    ,       __lowerCamelCase    :				Tuple=[2, 4, 4]    ,       __lowerCamelCase    :				List[Any]=3    ,       __lowerCamelCase    :				Optional[int]=768    ,       __lowerCamelCase    :				Any=5    ,       __lowerCamelCase    :				str=4    ,       __lowerCamelCase    :				Optional[Any]=37    ,       __lowerCamelCase    :				int="gelu_fast"    ,       __lowerCamelCase    :				Tuple=0.0    ,       __lowerCamelCase    :				Union[str, Any]=0.0    ,       __lowerCamelCase    :				Tuple=0.0_2    ,       __lowerCamelCase    :				Optional[Any]=1E-06    ,       __lowerCamelCase    :				List[str]=True    ,       __lowerCamelCase    :				int=None    ,       ):
           snake_case__					:      Tuple  			=						parent
           snake_case__					:      List[Any]  			=						batch_size
           snake_case__					:      Dict  			=						is_training
           snake_case__					:      Optional[Any]  			=						use_labels
           snake_case__					:      Dict  			=						num_labels
           snake_case__					:      str  			=						image_size
           snake_case__					:      Optional[Any]  			=						num_frames
           snake_case__					:      str  			=						tubelet_size
           snake_case__					:      Optional[Any]  			=						num_channels
           snake_case__					:      List[str]  			=						hidden_size
           snake_case__					:      Optional[int]  			=						num_hidden_layers
           snake_case__					:      Dict  			=						num_attention_heads
           snake_case__					:      str  			=						intermediate_size
           snake_case__					:      int  			=						hidden_act
           snake_case__					:      Tuple  			=						hidden_dropout_prob
           snake_case__					:      str  			=						attention_probs_dropout_prob
           snake_case__					:      str  			=						initializer_range
           snake_case__					:      List[Any]  			=						layer_norm_eps
           snake_case__					:      Tuple  			=						qkv_bias
           snake_case__					:      Optional[int]  			=						scope

           snake_case__					:      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  _lowerCAmelCase			(    self    :				int ):
           snake_case__					:      Dict  			=						floats_tensor(
               [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] )

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

           snake_case__					:      List[Any]  			=						self.get_config()

           return config, pixel_values, labels





     def  _lowerCAmelCase			(    self    :				Tuple ):
           snake_case__					:      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    ,       )
           snake_case__					:      Any  			=						self.num_labels
           return config





     def  _lowerCAmelCase			(    self    :				str    ,       __lowerCamelCase    :				Dict    ,       __lowerCamelCase    :				Dict    ,       __lowerCamelCase    :				Optional[Any] ):
           snake_case__					:      Union[str, Any]  			=						VivitModel(config=__lowerCamelCase )
           model.to(__lowerCamelCase )
           model.eval()
           snake_case__					:      Optional[int]  			=						model(__lowerCamelCase )
           self.parent.assertEqual(result.last_hidden_state.shape    ,       (self.batch_size, self.seq_length, self.hidden_size) )





     def  _lowerCAmelCase			(    self    :				Dict    ,       __lowerCamelCase    :				Optional[Any]    ,       __lowerCamelCase    :				Optional[int]    ,       __lowerCamelCase    :				str ):
           snake_case__					:      int  			=						VivitForVideoClassification(__lowerCamelCase )
           model.to(__lowerCamelCase )
           model.eval()

           snake_case__					:      str  			=						model(__lowerCamelCase )

           # verify the logits shape
           snake_case__					:      List[Any]  			=						torch.Size((self.batch_size, self.num_labels) )
           self.parent.assertEqual(result.logits.shape    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				int ):
           snake_case__					:      Any  			=						self.prepare_config_and_inputs()
           snake_case__					:      str  			=						config_and_inputs
           snake_case__					:      str  			=						{"pixel_values": pixel_values}
           return config, inputs_dict






@require_torch
class 					lowercase_			(	A__	,					unittest.TestCase       ):
     A_       							= (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

     A_       							= False
     A_       							= False
     A_       							= False
     A_       							= False





     def  _lowerCAmelCase			(    self    :				Optional[int] ):
           snake_case__					:      Union[str, Any]  			=						VivitModelTester(self )
           snake_case__					:      Any  			=						ConfigTester(self    ,       config_class=__lowerCamelCase    ,       has_text_modality=__lowerCamelCase    ,       hidden_size=37 )





     def  _lowerCAmelCase			(    self    :				List[str]    ,       __lowerCamelCase    :				Any    ,       __lowerCamelCase    :				Any    ,       __lowerCamelCase    :				Optional[Any]=False ):
           snake_case__					:      Any  			=						copy.deepcopy(__lowerCamelCase )

           if return_labels:
                 if model_class in get_values(__lowerCamelCase ):
                       snake_case__					:      str  			=						torch.zeros(
                           self.model_tester.batch_size    ,       dtype=torch.long    ,       device=__lowerCamelCase )

           return inputs_dict





     def  _lowerCAmelCase			(    self    :				Tuple ):
           self.config_tester.run_common_tests()





     @unittest.skip(reason='Vivit does not use inputs_embeds' )
     def  _lowerCAmelCase			(    self    :				List[Any] ):
           pass





     def  _lowerCAmelCase			(    self    :				List[Any] ):
           snake_case__					:      Optional[int]  			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 snake_case__					:      Dict  			=						model_class(__lowerCamelCase )
                 self.assertIsInstance(model.get_input_embeddings()    ,       (nn.Module) )
                 snake_case__					:      Union[str, Any]  			=						model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(__lowerCamelCase    ,       nn.Linear ) )





     def  _lowerCAmelCase			(    self    :				List[str] ):
           snake_case__					:      Union[str, Any]  			=						self.model_tester.prepare_config_and_inputs_for_common()

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

                 snake_case__					:      Optional[int]  			=						["pixel_values", "head_mask"]
                 self.assertListEqual(arg_names[:2]    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				Optional[int] ):
           snake_case__					:      List[str]  			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				Dict ):
           snake_case__					:      Dict  			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_video_classification(*__lowerCamelCase )





     @slow
     def  _lowerCAmelCase			(    self    :				int ):






           for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case__					:      Dict  			=						VivitModel.from_pretrained(__lowerCamelCase )
                 self.assertIsNotNone(__lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				List[Any] ):
           snake_case__					:      str  			=						self.model_tester.prepare_config_and_inputs_for_common()
           snake_case__					:      Tuple  			=						True

           for model_class in self.all_model_classes:
                 snake_case__					:      int  			=						self.model_tester.seq_length

                 snake_case__					:      Optional[Any]  			=						True
                 snake_case__					:      Optional[int]  			=						False
                 snake_case__					:      int  			=						True
                 snake_case__					:      Tuple  			=						model_class(__lowerCamelCase )
                 model.to(__lowerCamelCase )
                 model.eval()
                 with torch.no_grad():
                       snake_case__					:      Optional[int]  			=						model(**self._prepare_for_class(__lowerCamelCase    ,       __lowerCamelCase ) )
                 snake_case__					:      Any  			=						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"]
                 snake_case__					:      List[Any]  			=						True
                 snake_case__					:      Optional[int]  			=						model_class(__lowerCamelCase )
                 model.to(__lowerCamelCase )
                 model.eval()
                 with torch.no_grad():
                       snake_case__					:      str  			=						model(**self._prepare_for_class(__lowerCamelCase    ,       __lowerCamelCase ) )
                 snake_case__					:      Optional[int]  			=						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]    ,       )
                 snake_case__					:      str  			=						len(__lowerCamelCase )

                 # Check attention is always last and order is fine
                 snake_case__					:      int  			=						True
                 snake_case__					:      Tuple  			=						True
                 snake_case__					:      List[Any]  			=						model_class(__lowerCamelCase )
                 model.to(__lowerCamelCase )
                 model.eval()
                 with torch.no_grad():
                       snake_case__					:      Union[str, Any]  			=						model(**self._prepare_for_class(__lowerCamelCase    ,       __lowerCamelCase ) )

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

                 snake_case__					:      Dict  			=						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[Any] ):






           def check_hidden_states_output(__lowerCamelCase    :				Optional[int]    ,       __lowerCamelCase    :				List[str]    ,       __lowerCamelCase    :				Optional[int] ):
                 snake_case__					:      Optional[int]  			=						model_class(__lowerCamelCase )
                 model.to(__lowerCamelCase )
                 model.eval()

                 with torch.no_grad():
                       snake_case__					:      Dict  			=						model(**self._prepare_for_class(__lowerCamelCase    ,       __lowerCamelCase ) )

                 snake_case__					:      Dict  			=						outputs.hidden_states
                 snake_case__					:      int  			=						self.model_tester.num_hidden_layers + 1
                 self.assertEqual(len(__lowerCamelCase )    ,       __lowerCamelCase )

                 snake_case__					:      Union[str, Any]  			=						self.model_tester.seq_length

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

           snake_case__					:      Optional[int]  			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 snake_case__					:      Optional[int]  			=						True
                 check_hidden_states_output(__lowerCamelCase    ,       __lowerCamelCase    ,       __lowerCamelCase )

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 snake_case__					:      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    :				Optional[Any] ):
           pass






def 		UpperCamelCase__			(		)						->      Optional[int]:
      snake_case__					:      List[str]  			=						hf_hub_download(
          repo_id='hf-internal-testing/spaghetti-video'							,			filename='eating_spaghetti_32_frames.npy'							,			repo_type='dataset'		)
      snake_case__					:      Optional[int]  			=						np.load(__lowerCAmelCase		)
      return list(__lowerCAmelCase		)






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





     @cached_property
     def  _lowerCAmelCase			(    self    :				str ):
           return VivitImageProcessor() if is_vision_available() else None





     @slow
     def  _lowerCAmelCase			(    self    :				Any ):
           snake_case__					:      Union[str, Any]  			=						VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400' ).to(__lowerCamelCase )

           snake_case__					:      List[Any]  			=						self.default_image_processor
           snake_case__					:      List[str]  			=						prepare_video()
           snake_case__					:      Dict  			=						image_processor(__lowerCamelCase    ,       return_tensors='pt' ).to(__lowerCamelCase )

           # forward pass
           with torch.no_grad():
                 snake_case__					:      Optional[int]  			=						model(**__lowerCamelCase )

           # verify the logits
           snake_case__					:      int  			=						torch.Size((1, 400) )
           self.assertEqual(outputs.logits.shape    ,       __lowerCamelCase )

           # taken from original model
           snake_case__					:      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(__lowerCamelCase )

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







'''simple docstring'''






def 		UpperCamelCase__			(		__SCREAMING_SNAKE_CASE = 1000		)						->      int:
      snake_case__					:      List[str]  			=						0
      snake_case__					:      int  			=						(n - 1) // 3
      total += ((terms) * (6 + (terms - 1) * 3)) // 2  # total of an A.P.
      snake_case__					:      Optional[Any]  			=						(n - 1) // 5
      total += ((terms) * (10 + (terms - 1) * 5)) // 2
      snake_case__					:      Tuple  			=						(n - 1) // 15
      total -= ((terms) * (30 + (terms - 1) * 15)) // 2
      return total


if __name__ == "__main__":
      print(F'{solution() = }')
