code: 
  '''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   snake_case__					:
  
  
  
  
  
  
  
  		"""simple docstring"""
  
  		def __init__(			self							: str     ,  UpperCamelCase__							: List[str]     ,  UpperCamelCase__							: List[str]=2     ,  UpperCamelCase__							: Any=True     ,  UpperCamelCase__							: List[str]=True     ,  UpperCamelCase__							: Dict=10     ,  UpperCamelCase__							: int=10     ,  UpperCamelCase__							: str=8     ,  UpperCamelCase__							: Tuple=[2, 4, 4]     ,  UpperCamelCase__							: Any=3     ,  UpperCamelCase__							: Optional[int]=768     ,  UpperCamelCase__							: int=5     ,  UpperCamelCase__							: Optional[Any]=4     ,  UpperCamelCase__							: Tuple=37     ,  UpperCamelCase__							: Optional[Any]="gelu_fast"     ,  UpperCamelCase__							: Optional[Any]=0.0     ,  UpperCamelCase__							: str=0.0     ,  UpperCamelCase__							: Optional[Any]=0.02     ,  UpperCamelCase__							: int=1e-0_6     ,  UpperCamelCase__							: int=True     ,  UpperCamelCase__							: Tuple=None     ,  )	-> int:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Any     =	parent
  				snake_case     :					str     =	batch_size
  				snake_case     :					Optional[Any]     =	is_training
  				snake_case     :					Optional[Any]     =	use_labels
  				snake_case     :					Any     =	num_labels
  				snake_case     :					Dict     =	image_size
  				snake_case     :					List[Any]     =	num_frames
  				snake_case     :					List[str]     =	tubelet_size
  				snake_case     :					List[str]     =	num_channels
  				snake_case     :					List[Any]     =	hidden_size
  				snake_case     :					Dict     =	num_hidden_layers
  				snake_case     :					List[str]     =	num_attention_heads
  				snake_case     :					Tuple     =	intermediate_size
  				snake_case     :					int     =	hidden_act
  				snake_case     :					List[Any]     =	hidden_dropout_prob
  				snake_case     :					Union[str, Any]     =	attention_probs_dropout_prob
  				snake_case     :					Optional[Any]     =	initializer_range
  				snake_case     :					List[Any]     =	layer_norm_eps
  				snake_case     :					Optional[int]     =	qkv_bias
  				snake_case     :					str     =	scope
  
  				snake_case     :					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							: List[Any]			)	-> Tuple:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Union[str, Any]     =	floats_tensor(
  				    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]			)
  
  				snake_case     :					str     =	None
  				if self.use_labels:
  						snake_case     :					Union[str, Any]     =	ids_tensor([self.batch_size]     ,  self.num_labels			)
  
  				snake_case     :					int     =	self.get_config()
  
  				return config, pixel_values, labels
  
  
  
  		def 			lowerCAmelCase							(			self							: List[Any]			)	-> Union[str, Any]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					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     ,  )
  				snake_case     :					str     =	self.num_labels
  				return config
  
  
  
  		def 			lowerCAmelCase							(			self							: Any     ,  UpperCamelCase__							: Union[str, Any]     ,  UpperCamelCase__							: Optional[int]     ,  UpperCamelCase__							: List[str]			)	-> Union[str, Any]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Dict     =	VivitModel(config=UpperCamelCase__			)
  				model.to(UpperCamelCase__			)
  				model.eval()
  				snake_case     :					List[Any]     =	model(UpperCamelCase__			)
  				self.parent.assertEqual(result.last_hidden_state.shape     ,  (self.batch_size, self.seq_length, self.hidden_size)			)
  
  
  
  		def 			lowerCAmelCase							(			self							: List[str]     ,  UpperCamelCase__							: str     ,  UpperCamelCase__							: Dict     ,  UpperCamelCase__							: Optional[int]			)	-> Dict:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					List[Any]     =	VivitForVideoClassification(UpperCamelCase__			)
  				model.to(UpperCamelCase__			)
  				model.eval()
  
  				snake_case     :					List[str]     =	model(UpperCamelCase__			)
  
  				# verify the logits shape
  				snake_case     :					List[str]     =	torch.Size((self.batch_size, self.num_labels)			)
  				self.parent.assertEqual(result.logits.shape     ,  UpperCamelCase__			)
  
  
  
  		def 			lowerCAmelCase							(			self							: Any			)	-> int:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Union[str, Any]     =	self.prepare_config_and_inputs()
  				snake_case ,snake_case ,snake_case     :					int     =	config_and_inputs
  				snake_case     :					Dict     =	{'''pixel_values''': pixel_values}
  				return config, inputs_dict
  
  
  
  
  
  
  
  @require_torch
  class   snake_case__					(			__SCREAMING_SNAKE_CASE						,				unittest.TestCase				):
  
  
  
  
  
  
  
  		"""simple docstring"""
  
  		lowerCamelCase           =	(VivitModel, VivitForVideoClassification) if is_torch_available() else ()
  
  		lowerCamelCase           =	False
  		lowerCamelCase           =	False
  		lowerCamelCase           =	False
  		lowerCamelCase           =	False
  
  
  
  		def 			lowerCAmelCase							(			self							: Any			)	-> List[str]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Tuple     =	VivitModelTester(self			)
  				snake_case     :					List[Any]     =	ConfigTester(self     ,  config_class=UpperCamelCase__     ,  has_text_modality=UpperCamelCase__     ,  hidden_size=37			)
  
  
  
  		def 			lowerCAmelCase							(			self							: Union[str, Any]     ,  UpperCamelCase__							: int     ,  UpperCamelCase__							: List[Any]     ,  UpperCamelCase__							: int=False			)	-> Union[str, Any]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Tuple     =	copy.deepcopy(UpperCamelCase__			)
  
  				if return_labels:
  						if model_class in get_values(UpperCamelCase__			):
  								snake_case     :					Optional[Any]     =	torch.zeros(
  								    self.model_tester.batch_size     ,  dtype=torch.long     ,  device=UpperCamelCase__			)
  
  				return inputs_dict
  
  
  
  		def 			lowerCAmelCase							(			self							: Optional[Any]			)	-> int:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				self.config_tester.run_common_tests()
  
  
  
  		@unittest.skip(reason='''Vivit does not use inputs_embeds'''			)
  		def 			lowerCAmelCase							(			self							: List[str]			)	-> Dict:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				pass
  
  
  
  		def 			lowerCAmelCase							(			self							: Dict			)	-> List[str]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case ,snake_case     :					Dict     =	self.model_tester.prepare_config_and_inputs_for_common()
  
  				for model_class in self.all_model_classes:
  						snake_case     :					Any     =	model_class(UpperCamelCase__			)
  						self.assertIsInstance(model.get_input_embeddings()     ,  (nn.Module)			)
  						snake_case     :					Union[str, Any]     =	model.get_output_embeddings()
  						self.assertTrue(x is None or isinstance(UpperCamelCase__     ,  nn.Linear			)			)
  
  
  
  		def 			lowerCAmelCase							(			self							: Optional[Any]			)	-> int:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case ,snake_case     :					Dict     =	self.model_tester.prepare_config_and_inputs_for_common()
  
  				for model_class in self.all_model_classes:
  						snake_case     :					str     =	model_class(UpperCamelCase__			)
  						snake_case     :					Any     =	inspect.signature(model.forward			)
  						# signature.parameters is an OrderedDict => so arg_names order is deterministic
  						snake_case     :					List[Any]     =	[*signature.parameters.keys()]
  
  						snake_case     :					Tuple     =	['''pixel_values''', '''head_mask''']
  						self.assertListEqual(arg_names[:2]     ,  UpperCamelCase__			)
  
  
  
  		def 			lowerCAmelCase							(			self							: Optional[Any]			)	-> Any:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Union[str, Any]     =	self.model_tester.prepare_config_and_inputs()
  				self.model_tester.create_and_check_model(*UpperCamelCase__			)
  
  
  
  		def 			lowerCAmelCase							(			self							: str			)	-> int:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					Optional[int]     =	self.model_tester.prepare_config_and_inputs()
  				self.model_tester.create_and_check_for_video_classification(*UpperCamelCase__			)
  
  
  
  		@slow
  		def 			lowerCAmelCase							(			self							: Dict			)	-> List[Any]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  						snake_case     :					Optional[Any]     =	VivitModel.from_pretrained(UpperCamelCase__			)
  						self.assertIsNotNone(UpperCamelCase__			)
  
  
  
  		def 			lowerCAmelCase							(			self							: List[Any]			)	-> Union[str, Any]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case ,snake_case     :					int     =	self.model_tester.prepare_config_and_inputs_for_common()
  				snake_case     :					Dict     =	True
  
  				for model_class in self.all_model_classes:
  						snake_case     :					Dict     =	self.model_tester.seq_length
  
  						snake_case     :					Tuple     =	True
  						snake_case     :					List[Any]     =	False
  						snake_case     :					Dict     =	True
  						snake_case     :					Dict     =	model_class(UpperCamelCase__			)
  						model.to(UpperCamelCase__			)
  						model.eval()
  						with torch.no_grad():
  								snake_case     :					str     =	model(**self._prepare_for_class(UpperCamelCase__     ,  UpperCamelCase__			)			)
  						snake_case     :					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"]
  						snake_case     :					Union[str, Any]     =	True
  						snake_case     :					str     =	model_class(UpperCamelCase__			)
  						model.to(UpperCamelCase__			)
  						model.eval()
  						with torch.no_grad():
  								snake_case     :					int     =	model(**self._prepare_for_class(UpperCamelCase__     ,  UpperCamelCase__			)			)
  						snake_case     :					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]     ,  )
  						snake_case     :					Optional[Any]     =	len(UpperCamelCase__			)
  
  						# Check attention is always last and order is fine
  						snake_case     :					Optional[Any]     =	True
  						snake_case     :					Tuple     =	True
  						snake_case     :					List[str]     =	model_class(UpperCamelCase__			)
  						model.to(UpperCamelCase__			)
  						model.eval()
  						with torch.no_grad():
  								snake_case     :					Optional[Any]     =	model(**self._prepare_for_class(UpperCamelCase__     ,  UpperCamelCase__			)			)
  
  						self.assertEqual(out_len + 1     ,  len(UpperCamelCase__			)			)
  
  						snake_case     :					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							: Optional[Any]			)	-> List[Any]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				def check_hidden_states_output(UpperCamelCase__							: str     ,  UpperCamelCase__							: Optional[int]     ,  UpperCamelCase__							: Tuple			):
  						snake_case     :					int     =	model_class(UpperCamelCase__			)
  						model.to(UpperCamelCase__			)
  						model.eval()
  
  						with torch.no_grad():
  								snake_case     :					List[str]     =	model(**self._prepare_for_class(UpperCamelCase__     ,  UpperCamelCase__			)			)
  
  						snake_case     :					Optional[int]     =	outputs.hidden_states
  						snake_case     :					Any     =	self.model_tester.num_hidden_layers + 1
  						self.assertEqual(len(UpperCamelCase__			)     ,  UpperCamelCase__			)
  
  						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 ,snake_case     :					Optional[int]     =	self.model_tester.prepare_config_and_inputs_for_common()
  
  				for model_class in self.all_model_classes:
  						snake_case     :					str     =	True
  						check_hidden_states_output(UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__			)
  
  						# check that output_hidden_states also work using config
  						del inputs_dict["output_hidden_states"]
  						snake_case     :					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							: Dict			)	-> Any:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				pass
  
  
  
  
  
  
  
  def       _UpperCamelCase	(					)     ->       Optional[Any]:
  
  
  		'''simple docstring'''
  
  
  		snake_case     :					Union[str, Any]     =	hf_hub_download(
  		    repo_id='''hf-internal-testing/spaghetti-video'''   ,					filename='''eating_spaghetti_32_frames.npy'''   ,					repo_type='''dataset''' )
  		snake_case     :					Any     =	np.load(SCREAMING_SNAKE_CASE__ )
  		return list(SCREAMING_SNAKE_CASE__ )
  
  
  
  
  
  
  @require_torch
  @require_vision
  class   snake_case__					(			unittest.TestCase				):
  
  
  
  
  
  
  
  		"""simple docstring"""
  
  		@cached_property
  		def 			lowerCAmelCase							(			self							: List[Any]			)	-> Optional[Any]:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				return VivitImageProcessor() if is_vision_available() else None
  
  
  
  		@slow
  		def 			lowerCAmelCase							(			self							: List[str]			)	-> Tuple:
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  
  				snake_case     :					List[Any]     =	VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''			).to(UpperCamelCase__			)
  
  				snake_case     :					Union[str, Any]     =	self.default_image_processor
  				snake_case     :					str     =	prepare_video()
  				snake_case     :					List[str]     =	image_processor(UpperCamelCase__     ,  return_tensors='''pt'''			).to(UpperCamelCase__			)
  
  				# forward pass
  				with torch.no_grad():
  						snake_case     :					str     =	model(**UpperCamelCase__			)
  
  				# verify the logits
  				snake_case     :					Optional[int]     =	torch.Size((1, 400)			)
  				self.assertEqual(outputs.logits.shape     ,  UpperCamelCase__			)
  
  				# taken from original model
  				snake_case     :					str     =	torch.tensor([-1.0_543, 2.0_764, -0.2_104, 0.4_439, -0.9_658]			).to(UpperCamelCase__			)
  
  				self.assertTrue(torch.allclose(outputs.logits[0, :5]     ,  UpperCamelCase__     ,  atol=1e-4			)			)
  
  
  
style_context: 
  '''simple docstring'''
  import numpy as np
  
  
  
  
  def       _UpperCamelCase	(					SCREAMING_SNAKE_CASE__ )     ->       Optional[Any]:
  
  
  		'''simple docstring'''
  
  
  		snake_case ,snake_case     :					str     =	a.shape
  		snake_case     :					Union[str, Any]     =	min(SCREAMING_SNAKE_CASE__   ,					SCREAMING_SNAKE_CASE__ )
  		snake_case     :					List[Any]     =	np.eye(SCREAMING_SNAKE_CASE__ )
  		snake_case     :					Union[str, Any]     =	a.copy()
  
  		for k in range(t - 1 ):
  				# select a column of modified matrix A':
  				snake_case     :					Union[str, Any]     =	r[k:, [k]]
  				# construct first basis vector
  				snake_case     :					str     =	np.zeros_like(SCREAMING_SNAKE_CASE__ )
  				snake_case     :					List[str]     =	1.0
  				# determine scaling factor
  				snake_case     :					int     =	np.linalg.norm(SCREAMING_SNAKE_CASE__ )
  				# construct vector v for Householder reflection
  				snake_case     :					Optional[int]     =	x + np.sign(x[0] ) * alpha * ea
  				v /= np.linalg.norm(SCREAMING_SNAKE_CASE__ )
  
  				# construct the Householder matrix
  				snake_case     :					Tuple     =	np.eye(m - k ) - 2.0 * v @ v.T
  				# pad with ones and zeros as necessary
  				snake_case     :					int     =	np.block([[np.eye(SCREAMING_SNAKE_CASE__ ), np.zeros((k, m - k) )], [np.zeros((m - k, k) ), q_k]] )
  
  				snake_case     :					int     =	q @ q_k.T
  				snake_case     :					Dict     =	q_k @ r
  
  		return q, r
  
  
  if __name__ == "__main__":
  						import doctest
  
  						doctest.testmod()
  
  
  