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 				__SCREAMING_SNAKE_CASE   :
  
  
        def __init__(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__=2							,						SCREAMING_SNAKE_CASE__=True							,						SCREAMING_SNAKE_CASE__=True							,						SCREAMING_SNAKE_CASE__=10							,						SCREAMING_SNAKE_CASE__=10							,						SCREAMING_SNAKE_CASE__=8							,						SCREAMING_SNAKE_CASE__=[2, 4, 4]							,						SCREAMING_SNAKE_CASE__=3							,						SCREAMING_SNAKE_CASE__=768							,						SCREAMING_SNAKE_CASE__=5							,						SCREAMING_SNAKE_CASE__=4							,						SCREAMING_SNAKE_CASE__=37							,						SCREAMING_SNAKE_CASE__="gelu_fast"							,						SCREAMING_SNAKE_CASE__=0.0							,						SCREAMING_SNAKE_CASE__=0.0							,						SCREAMING_SNAKE_CASE__=0.02							,						SCREAMING_SNAKE_CASE__=1E-06							,						SCREAMING_SNAKE_CASE__=True							,						SCREAMING_SNAKE_CASE__=None							,						):
              lowercase			:   Any					   =		parent
              lowercase			:   str					   =		batch_size
              lowercase			:   Optional[Any]					   =		is_training
              lowercase			:   Optional[Any]					   =		use_labels
              lowercase			:   Any					   =		num_labels
              lowercase			:   Dict					   =		image_size
              lowercase			:   List[Any]					   =		num_frames
              lowercase			:   List[str]					   =		tubelet_size
              lowercase			:   List[str]					   =		num_channels
              lowercase			:   List[Any]					   =		hidden_size
              lowercase			:   Dict					   =		num_hidden_layers
              lowercase			:   List[str]					   =		num_attention_heads
              lowercase			:   Tuple					   =		intermediate_size
              lowercase			:   int					   =		hidden_act
              lowercase			:   List[Any]					   =		hidden_dropout_prob
              lowercase			:   Union[str, Any]					   =		attention_probs_dropout_prob
              lowercase			:   Optional[Any]					   =		initializer_range
              lowercase			:   List[Any]					   =		layer_norm_eps
              lowercase			:   Optional[int]					   =		qkv_bias
              lowercase			:   str					   =		scope
  
              lowercase			:   Union[str, Any]					   =		(
                  (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		):
              lowercase			:   Union[str, Any]					   =		floats_tensor(
                  [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]		)
  
              lowercase			:   str					   =		None
              if self.use_labels:
                    lowercase			:   Union[str, Any]					   =		ids_tensor([self.batch_size]							,						self.num_labels		)
  
              lowercase			:   int					   =		self.get_config()
  
              return config, pixel_values, labels
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   int					   =		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							,						)
              lowercase			:   str					   =		self.num_labels
              return config
  
  
        def        __lowerCamelCase			(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__		):
              lowercase			:   Dict					   =		VivitModel(config=UpperCamelCase__		)
              model.to(UpperCamelCase__		)
              model.eval()
              lowercase			:   List[Any]					   =		model(UpperCamelCase__		)
              self.parent.assertEqual(result.last_hidden_state.shape							,						(self.batch_size, self.seq_length, self.hidden_size)		)
  
  
        def        __lowerCamelCase			(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__		):
              lowercase			:   List[Any]					   =		VivitForVideoClassification(UpperCamelCase__		)
              model.to(UpperCamelCase__		)
              model.eval()
  
              lowercase			:   List[str]					   =		model(UpperCamelCase__		)
  
              # verify the logits shape
              lowercase			:   List[str]					   =		torch.Size((self.batch_size, self.num_labels)		)
              self.parent.assertEqual(result.logits.shape							,						UpperCamelCase__		)
  
  
  
  
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   Union[str, Any]					   =		self.prepare_config_and_inputs()
              lowercase			:   int					   =		config_and_inputs
              lowercase			:   Dict					   =		{'''pixel_values''': pixel_values}
              return config, inputs_dict
  
  
  
  
  
  
  
  @require_torch
  class 				__SCREAMING_SNAKE_CASE   (			__SCREAMING_SNAKE_CASE  ,						unittest.TestCase   ):
        A				:							Any    							=     (VivitModel, VivitForVideoClassification) if is_torch_available() else ()
  
        A				:							int    							=     False
        A				:							Dict    							=     False
        A				:							Optional[Any]    							=     False
        A				:							Optional[int]    							=     False
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   Tuple					   =		VivitModelTester(self		)
              lowercase			:   List[Any]					   =		ConfigTester(self							,						config_class=UpperCamelCase__							,						has_text_modality=UpperCamelCase__							,						hidden_size=37		)
  
  
        def        __lowerCamelCase			(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__=False		):
              lowercase			:   Tuple					   =		copy.deepcopy(UpperCamelCase__		)
  
              if return_labels:
                    if model_class in get_values(UpperCamelCase__		):
                          lowercase			:   Optional[Any]					   =		torch.zeros(
                              self.model_tester.batch_size							,						dtype=torch.long							,						device=UpperCamelCase__		)
  
              return inputs_dict
  
  
        def        __lowerCamelCase			(							self		):
              self.config_tester.run_common_tests()
  
  
        @unittest.skip(reason='''Vivit does not use inputs_embeds'''		)
        def        __lowerCamelCase			(							self		):
              pass
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   Dict					   =		self.model_tester.prepare_config_and_inputs_for_common()
  
              for model_class in self.all_model_classes:
                    lowercase			:   Any					   =		model_class(UpperCamelCase__		)
                    self.assertIsInstance(model.get_input_embeddings()							,						(nn.Module)		)
                    lowercase			:   Union[str, Any]					   =		model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(UpperCamelCase__							,						nn.Linear		)		)
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   Dict					   =		self.model_tester.prepare_config_and_inputs_for_common()
  
              for model_class in self.all_model_classes:
                    lowercase			:   str					   =		model_class(UpperCamelCase__		)
                    lowercase			:   Any					   =		inspect.signature(model.forward		)
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    lowercase			:   List[Any]					   =		[*signature.parameters.keys()]
  
                    lowercase			:   Tuple					   =		['''pixel_values''', '''head_mask''']
                    self.assertListEqual(arg_names[:2]							,						UpperCamelCase__		)
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   Union[str, Any]					   =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCamelCase__		)
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   Optional[int]					   =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_video_classification(*UpperCamelCase__		)
  
  
        @slow
        def        __lowerCamelCase			(							self		):
  
  
  
  
  
  
  
              for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase			:   Optional[Any]					   =		VivitModel.from_pretrained(UpperCamelCase__		)
                    self.assertIsNotNone(UpperCamelCase__		)
  
  
        def        __lowerCamelCase			(							self		):
              lowercase			:   int					   =		self.model_tester.prepare_config_and_inputs_for_common()
              lowercase			:   Dict					   =		True
  
              for model_class in self.all_model_classes:
                    lowercase			:   Dict					   =		self.model_tester.seq_length
  
                    lowercase			:   Tuple					   =		True
                    lowercase			:   List[Any]					   =		False
                    lowercase			:   Dict					   =		True
                    lowercase			:   Dict					   =		model_class(UpperCamelCase__		)
                    model.to(UpperCamelCase__		)
                    model.eval()
                    with torch.no_grad():
                          lowercase			:   str					   =		model(**self._prepare_for_class(UpperCamelCase__							,						UpperCamelCase__		)		)
                    lowercase			:   int					   =		outputs.attentions
                    self.assertEqual(len(UpperCamelCase__		)							,						self.model_tester.num_hidden_layers		)
  
                    # check that output_attentions also work using config
                    del inputs_dict["output_attentions"]
                    lowercase			:   Union[str, Any]					   =		True
                    lowercase			:   str					   =		model_class(UpperCamelCase__		)
                    model.to(UpperCamelCase__		)
                    model.eval()
                    with torch.no_grad():
                          lowercase			:   int					   =		model(**self._prepare_for_class(UpperCamelCase__							,						UpperCamelCase__		)		)
                    lowercase			:   Any					   =		outputs.attentions
                    self.assertEqual(len(UpperCamelCase__		)							,						self.model_tester.num_hidden_layers		)
  
                    self.assertListEqual(
                        list(attentions[0].shape[-3:]		)							,						[self.model_tester.num_attention_heads, seq_len, seq_len]							,						)
                    lowercase			:   Optional[Any]					   =		len(UpperCamelCase__		)
  
                    # Check attention is always last and order is fine
                    lowercase			:   Optional[Any]					   =		True
                    lowercase			:   Tuple					   =		True
                    lowercase			:   List[str]					   =		model_class(UpperCamelCase__		)
                    model.to(UpperCamelCase__		)
                    model.eval()
                    with torch.no_grad():
                          lowercase			:   Optional[Any]					   =		model(**self._prepare_for_class(UpperCamelCase__							,						UpperCamelCase__		)		)
  
                    self.assertEqual(out_len + 1							,						len(UpperCamelCase__		)		)
  
                    lowercase			:   Optional[int]					   =		outputs.attentions
  
                    self.assertEqual(len(UpperCamelCase__		)							,						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		):
  
  
  
  
  
  
  
              def check_hidden_states_output(SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__		):
                    lowercase			:   int					   =		model_class(UpperCamelCase__		)
                    model.to(UpperCamelCase__		)
                    model.eval()
  
                    with torch.no_grad():
                          lowercase			:   List[str]					   =		model(**self._prepare_for_class(UpperCamelCase__							,						UpperCamelCase__		)		)
  
                    lowercase			:   Optional[int]					   =		outputs.hidden_states
                    lowercase			:   Any					   =		self.model_tester.num_hidden_layers + 1
                    self.assertEqual(len(UpperCamelCase__		)							,						UpperCamelCase__		)
  
                    lowercase			:   Union[str, Any]					   =		self.model_tester.seq_length
  
                    self.assertListEqual(
                        list(hidden_states[0].shape[-2:]		)							,						[seq_length, self.model_tester.hidden_size]							,						)
  
              lowercase			:   Optional[int]					   =		self.model_tester.prepare_config_and_inputs_for_common()
  
              for model_class in self.all_model_classes:
                    lowercase			:   str					   =		True
                    check_hidden_states_output(UpperCamelCase__							,						UpperCamelCase__							,						UpperCamelCase__		)
  
                    # check that output_hidden_states also work using config
                    del inputs_dict["output_hidden_states"]
                    lowercase			:   Optional[int]					   =		True
  
                    check_hidden_states_output(UpperCamelCase__							,						UpperCamelCase__							,						UpperCamelCase__		)
  
  
  
  
  
  
        @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''		)
        def        __lowerCamelCase			(							self		):
              pass
  
  
  
  
  
  def 			__lowercase	(						)    ->Optional[Any]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase			:   Union[str, Any]					   =		hf_hub_download(
            repo_id='''hf-internal-testing/spaghetti-video''',    filename='''eating_spaghetti_32_frames.npy''',    repo_type='''dataset'''     )
        lowercase			:   Any					   =		np.load(SCREAMING_SNAKE_CASE__     )
        return list(SCREAMING_SNAKE_CASE__     )
  
  
  
  
  
  
  
  @require_torch
  @require_vision
  class 				__SCREAMING_SNAKE_CASE   (			unittest.TestCase   ):
  
  
        @cached_property
        def        __lowerCamelCase			(							self		):
              return VivitImageProcessor() if is_vision_available() else None
  
  
  
  
  
  
        @slow
        def        __lowerCamelCase			(							self		):
              lowercase			:   List[Any]					   =		VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''		).to(UpperCamelCase__		)
  
              lowercase			:   Union[str, Any]					   =		self.default_image_processor
              lowercase			:   str					   =		prepare_video()
              lowercase			:   List[str]					   =		image_processor(UpperCamelCase__							,						return_tensors='''pt'''		).to(UpperCamelCase__		)
  
              # forward pass
              with torch.no_grad():
                    lowercase			:   str					   =		model(**UpperCamelCase__		)
  
              # verify the logits
              lowercase			:   Optional[int]					   =		torch.Size((1, 400)		)
              self.assertEqual(outputs.logits.shape							,						UpperCamelCase__		)
  
              # taken from original model
              lowercase			:   str					   =		torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]		).to(UpperCamelCase__		)
  
              self.assertTrue(torch.allclose(outputs.logits[0, :5]							,						UpperCamelCase__							,						atol=1E-4		)		)
  
  
style_context: 
  
  
  import argparse
  import logging
  
  import numpy as np
  import torch
  
  from transformers import AutoModelForCausalLM, AutoTokenizer
  
  
  logging.basicConfig(
      format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''',
      datefmt='''%m/%d/%Y %H:%M:%S''',
      level=logging.INFO,
  )
  __a   				=      logging.getLogger(__name__)
  
  
  
  
  def 			__lowercase	(						_UpperCamelCase     )    ->Any:
  
  
  
  
  
  
  
        """simple docstring"""
  
        np.random.seed(args.seed     )
        torch.manual_seed(args.seed     )
        if args.n_gpu > 0:
              torch.cuda.manual_seed_all(args.seed     )
  
  
  
  
  def 			__lowercase	(						)    ->List[str]:
  
  
  
  
  
  
  
        """simple docstring"""
  
        lowercase			:   List[str]					   =		argparse.ArgumentParser()
        parser.add_argument(
            '''--model_name_or_path''',    default=_UpperCamelCase,    type=_UpperCamelCase,    required=_UpperCamelCase,    )
        parser.add_argument('''--prompt''',    type=_UpperCamelCase,    default=''''''     )
        parser.add_argument('''--length''',    type=_UpperCamelCase,    default=20     )
        parser.add_argument('''--stop_token''',    type=_UpperCamelCase,    default=_UpperCamelCase,    help='''Token at which text generation is stopped'''     )
        parser.add_argument(
            '''--temperature''',    type=_UpperCamelCase,    default=1.0,    help='''temperature of 1.0 has no effect, lower tend toward greedy sampling''',    )
        parser.add_argument(
            '''--repetition_penalty''',    type=_UpperCamelCase,    default=1.0,    help='''primarily useful for CTRL model; in that case, use 1.2'''     )
        parser.add_argument('''--k''',    type=_UpperCamelCase,    default=0     )
        parser.add_argument('''--penalty_alpha''',    type=_UpperCamelCase,    default=0.0     )
        parser.add_argument('''--p''',    type=_UpperCamelCase,    default=0.9     )
  
        parser.add_argument('''--prefix''',    type=_UpperCamelCase,    default='''''',    help='''Text added prior to input.'''     )
        parser.add_argument('''--padding_text''',    type=_UpperCamelCase,    default='''''',    help='''Deprecated, the use of `--prefix` is preferred.'''     )
        parser.add_argument('''--xlm_language''',    type=_UpperCamelCase,    default='''''',    help='''Optional language when used with the XLM model.'''     )
  
        parser.add_argument('''--seed''',    type=_UpperCamelCase,    default=42,    help='''random seed for initialization'''     )
        parser.add_argument('''--no_cuda''',    action='''store_true''',    help='''Avoid using CUDA when available'''     )
        parser.add_argument(
            '''--fp16''',    action='''store_true''',    help='''Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit''',    )
        lowercase			:   Any					   =		parser.parse_args()
  
        lowercase			:   int					   =		torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu'''     )
        lowercase			:   List[str]					   =		0 if args.no_cuda else torch.cuda.device_count()
  
        logger.warning(f"""device: {args.device}, n_gpu: {args.n_gpu}, 16-bits training: {args.fpaa}"""     )
  
        set_seed(_UpperCamelCase     )
  
        # Initialize the model and tokenizer
        lowercase			:   Optional[Any]					   =		AutoTokenizer.from_pretrained(args.model_name_or_path     )
        lowercase			:   List[Any]					   =		AutoModelForCausalLM.from_pretrained(args.model_name_or_path     )
  
        # tokenizer = GPT2Tokenizer.from_pretrained(args.model_name_or_path)
        # model = OPTForCausalLM.from_pretrained(args.model_name_or_path)
        model.to(args.device     )
  
        if args.fpaa:
              model.half()
  
        logger.info(_UpperCamelCase     )
        lowercase			:   Union[str, Any]					   =		args.prompt if args.prompt else input('''Model prompt >>> '''     )
  
        lowercase			:   List[Any]					   =		tokenizer(_UpperCamelCase,    return_tensors='''pt''',    add_special_tokens=_UpperCamelCase     )
        lowercase			:   Union[str, Any]					   =		{key: value.to(args.device     ) for key, value in inputs.items()}
  
        lowercase			:   Optional[int]					   =		model.generate(
            **_UpperCamelCase,    max_length=args.length + len(inputs['''input_ids'''][0]     ),    penalty_alpha=args.penalty_alpha,    top_k=args.k,    )
  
        lowercase			:   Union[str, Any]					   =		[]
        for generated_sequence_idx, generated_sequence in enumerate(_UpperCamelCase     ):
              print(f"""=== GENERATED SEQUENCE {generated_sequence_idx + 1} ==="""     )
              lowercase			:   Optional[Any]					   =		generated_sequence.tolist()
  
              # Decode text
              lowercase			:   int					   =		tokenizer.decode(_UpperCamelCase,    clean_up_tokenization_spaces=_UpperCamelCase,    add_special_tokens=_UpperCamelCase     )
  
              # Remove all text after the stop token
              lowercase			:   Optional[int]					   =		text[: text.find(args.stop_token     ) if args.stop_token else None]
  
              # Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
              lowercase			:   Optional[Any]					   =		(
                  prompt_text + text[len(tokenizer.decode(inputs['''input_ids'''][0],    clean_up_tokenization_spaces=_UpperCamelCase     )     ) :]
              )
  
              generated_sequences.append(_UpperCamelCase     )
              print(_UpperCamelCase     )
  
        return generated_sequences
  
  
  if __name__ == "__main__":
    main()
  
  