


"""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     A:

		"""simple docstring"""





		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__=7_68				,							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.0_2				,							SCREAMING_SNAKE_CASE__=1E-06				,							SCREAMING_SNAKE_CASE__=True				,							SCREAMING_SNAKE_CASE__=None				,							)			->  Union[str, Any]:

					"""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      _UpperCamelCase(   self      )			->  Any:

					"""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      _UpperCamelCase(   self      )			->  List[Any]:

					"""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      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__      )			->  str:

					"""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      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							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      _UpperCamelCase(   self      )			->  Union[str, Any]:

					"""simple docstring"""



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






@require_torch
class     A(     A__							,       unittest.TestCase		):

		"""simple docstring"""





		A         =       (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

		A         =       False
		A         =       False
		A         =       False
		A         =       False
		def      _UpperCamelCase(   self      )			->  str:

					"""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      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__=False      )			->  List[str]:

					"""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      _UpperCamelCase(   self      )			->  str:

					"""simple docstring"""



					self.config_tester.run_common_tests()
		@unittest.skip(reason='''Vivit does not use inputs_embeds'''      )
		def      _UpperCamelCase(   self      )			->  Union[str, Any]:

					"""simple docstring"""



					pass
		def      _UpperCamelCase(   self      )			->  Dict:

					"""simple docstring"""



					_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      _UpperCamelCase(   self      )			->  Tuple:

					"""simple docstring"""



					_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      _UpperCamelCase(   self      )			->  Union[str, Any]:

					"""simple docstring"""



					_UpperCamelCase				:List[str]				    =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__      )
		def      _UpperCamelCase(   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      _UpperCamelCase(   self      )			->  Dict:

					"""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      _UpperCamelCase(   self      )			->  Optional[int]:

					"""simple docstring"""



					_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      _UpperCamelCase(   self      )			->  Dict:

					"""simple docstring"""



					def check_hidden_states_output(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__      ):
								_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				: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      _UpperCamelCase(   self      )			->  Optional[int]:

					"""simple docstring"""



					pass





def 							A_					(   )				->     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		):

		"""simple docstring"""





		@cached_property
		def      _UpperCamelCase(   self      )			->  Tuple:

					"""simple docstring"""



					return VivitImageProcessor() if is_vision_available() else None





		@slow
		def      _UpperCamelCase(   self      )			->  Union[str, 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      )      )





"""simple docstring"""






class     A:

		"""simple docstring"""





		def __init__(   self      )			->  List[str]:

					"""simple docstring"""



					_UpperCamelCase				:List[Any]				    =  []
					_UpperCamelCase				:Dict				    =  0
					_UpperCamelCase				:List[Any]				    =  0
		def __str__(   self      )			->  Optional[int]:

					"""simple docstring"""



					_UpperCamelCase				:Optional[Any]				    =  '''<''' + str(self.entries      )[1:-1] + '''>'''
					return printed
		def      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__      )			->  Tuple:

					"""simple docstring"""



					self.entries.append(SCREAMING_SNAKE_CASE__      )
					_UpperCamelCase				:Union[str, Any]				    =  self.length + 1
		def      _UpperCamelCase(   self      )			->  Dict:

					"""simple docstring"""



					_UpperCamelCase				:str				    =  self.length - 1
					_UpperCamelCase				:Optional[int]				    =  self.entries[self.front]
					# self.front-=1
					# self.entries = self.entries[self.front:]
					_UpperCamelCase				:Tuple				    =  self.entries[1:]
					return dequeued
		def      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__      )			->  str:

					"""simple docstring"""



					for _ in range(SCREAMING_SNAKE_CASE__      ):
								self.put(self.get()      )
		def      _UpperCamelCase(   self      )			->  str:

					"""simple docstring"""



					return self.entries[0]
		def      _UpperCamelCase(   self      )			->  Union[str, Any]:

					"""simple docstring"""



					return self.length

