





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					:  Tuple				,						a					:  Tuple				,						a					:  Optional[int]=2				,						a					:  List[Any]=True				,						a					:  Any=True				,						a					:  List[Any]=10				,						a					:  List[str]=10				,						a					:  Dict=8				,						a					:  List[Any]=[2, 4, 4]				,						a					:  Dict=3				,						a					:  Optional[int]=7_68				,						a					:  int=5				,						a					:  str=4				,						a					:  Union[str, Any]=37				,						a					:  str="gelu_fast"				,						a					:  Optional[Any]=0.0				,						a					:  Tuple=0.0				,						a					:  str=0.02				,						a					:  List[str]=1E-06				,						a					:  int=True				,						a					:  List[str]=None				,						)						->Tuple:
													SCREAMING_SNAKE_CASE__  :					List[str] =       parent
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       batch_size
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       is_training
													SCREAMING_SNAKE_CASE__  :					Tuple =       use_labels
													SCREAMING_SNAKE_CASE__  :					List[Any] =       num_labels
													SCREAMING_SNAKE_CASE__  :					List[str] =       image_size
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       num_frames
													SCREAMING_SNAKE_CASE__  :					Dict =       tubelet_size
													SCREAMING_SNAKE_CASE__  :					str =       num_channels
													SCREAMING_SNAKE_CASE__  :					List[str] =       hidden_size
													SCREAMING_SNAKE_CASE__  :					str =       num_hidden_layers
													SCREAMING_SNAKE_CASE__  :					List[Any] =       num_attention_heads
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       intermediate_size
													SCREAMING_SNAKE_CASE__  :					Tuple =       hidden_act
													SCREAMING_SNAKE_CASE__  :					Dict =       hidden_dropout_prob
													SCREAMING_SNAKE_CASE__  :					Tuple =       attention_probs_dropout_prob
													SCREAMING_SNAKE_CASE__  :					Tuple =       initializer_range
													SCREAMING_SNAKE_CASE__  :					List[Any] =       layer_norm_eps
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       qkv_bias
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       scope

													SCREAMING_SNAKE_CASE__  :					int =       (
													    (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   A_	(   self					:  Tuple     )						->Union[str, Any]:
													SCREAMING_SNAKE_CASE__  :					List[str] =       floats_tensor(
													    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]     )

													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       None
													if self.use_labels:
																			SCREAMING_SNAKE_CASE__  :					List[Any] =       ids_tensor([self.batch_size]				,						self.num_labels     )

													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       self.get_config()

													return config, pixel_values, labels

							def   A_	(   self					:  List[Any]     )						->str:
													SCREAMING_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				,						)
													SCREAMING_SNAKE_CASE__  :					List[Any] =       self.num_labels
													return config

							def   A_	(   self					:  List[Any]				,						a					:  Optional[int]				,						a					:  Optional[Any]				,						a					:  Tuple     )						->Optional[Any]:
													SCREAMING_SNAKE_CASE__  :					Any =       VivitModel(config=a     )
													model.to(a     )
													model.eval()
													SCREAMING_SNAKE_CASE__  :					Tuple =       model(a     )
													self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)     )

							def   A_	(   self					:  Union[str, Any]				,						a					:  List[Any]				,						a					:  Dict				,						a					:  Optional[Any]     )						->Optional[int]:
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       VivitForVideoClassification(a     )
													model.to(a     )
													model.eval()

													SCREAMING_SNAKE_CASE__  :					Any =       model(a     )

													# verify the logits shape
													SCREAMING_SNAKE_CASE__  :					Any =       torch.Size((self.batch_size, self.num_labels)     )
													self.parent.assertEqual(result.logits.shape				,						a     )






							def   A_	(   self					:  List[Any]     )						->List[str]:
													SCREAMING_SNAKE_CASE__  :					str =       self.prepare_config_and_inputs()
													SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__  :					Any =       config_and_inputs
													SCREAMING_SNAKE_CASE__  :					List[str] =       {"pixel_values": pixel_values}
													return config, inputs_dict







@require_torch
class  _a  (					lowercase__       ,				unittest.TestCase				):






							"""simple docstring"""

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

							snake_case_							=    False
							snake_case_							=    False
							snake_case_							=    False
							snake_case_							=    False

							def   A_	(   self					:  Any     )						->Tuple:
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       VivitModelTester(self     )
													SCREAMING_SNAKE_CASE__  :					List[str] =       ConfigTester(self				,						config_class=a				,						has_text_modality=a				,						hidden_size=37     )

							def   A_	(   self					:  Union[str, Any]				,						a					:  int				,						a					:  str				,						a					:  List[Any]=False     )						->Optional[int]:
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       copy.deepcopy(a     )

													if return_labels:
																			if model_class in get_values(a     ):
																									SCREAMING_SNAKE_CASE__  :					Tuple =       torch.zeros(
																									    self.model_tester.batch_size				,						dtype=torch.long				,						device=a     )

													return inputs_dict

							def   A_	(   self					:  Dict     )						->Dict:
													self.config_tester.run_common_tests()

							@unittest.skip(reason="Vivit does not use inputs_embeds"     )
							def   A_	(   self					:  Tuple     )						->int:
													pass

							def   A_	(   self					:  Optional[int]     )						->Optional[Any]:
													SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__  :					List[str] =       self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			SCREAMING_SNAKE_CASE__  :					Tuple =       model_class(a     )
																			self.assertIsInstance(model.get_input_embeddings()				,						(nn.Module)     )
																			SCREAMING_SNAKE_CASE__  :					int =       model.get_output_embeddings()
																			self.assertTrue(x is None or isinstance(a				,						nn.Linear     )     )

							def   A_	(   self					:  Optional[int]     )						->Tuple:
													SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       self.model_tester.prepare_config_and_inputs_for_common()

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

																			SCREAMING_SNAKE_CASE__  :					Any =       ["pixel_values", "head_mask"]
																			self.assertListEqual(arg_names[:2]				,						a     )

							def   A_	(   self					:  Tuple     )						->Tuple:
													SCREAMING_SNAKE_CASE__  :					List[str] =       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*a     )

							def   A_	(   self					:  Optional[Any]     )						->str:
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_video_classification(*a     )

							@slow
							def   A_	(   self					:  Any     )						->Optional[int]:
													for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			SCREAMING_SNAKE_CASE__  :					List[str] =       VivitModel.from_pretrained(a     )
																			self.assertIsNotNone(a     )

							def   A_	(   self					:  Dict     )						->List[str]:
													SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__  :					str =       self.model_tester.prepare_config_and_inputs_for_common()
													SCREAMING_SNAKE_CASE__  :					Any =       True

													for model_class in self.all_model_classes:
																			SCREAMING_SNAKE_CASE__  :					Optional[Any] =       self.model_tester.seq_length

																			SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       True
																			SCREAMING_SNAKE_CASE__  :					int =       False
																			SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       True
																			SCREAMING_SNAKE_CASE__  :					List[str] =       model_class(a     )
																			model.to(a     )
																			model.eval()
																			with torch.no_grad():
																									SCREAMING_SNAKE_CASE__  :					Optional[int] =       model(**self._prepare_for_class(a				,						a     )     )
																			SCREAMING_SNAKE_CASE__  :					Tuple =       outputs.attentions
																			self.assertEqual(len(a     )				,						self.model_tester.num_hidden_layers     )

																			# check that output_attentions also work using config
																			del inputs_dict["output_attentions"]
																			SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       True
																			SCREAMING_SNAKE_CASE__  :					Optional[int] =       model_class(a     )
																			model.to(a     )
																			model.eval()
																			with torch.no_grad():
																									SCREAMING_SNAKE_CASE__  :					Any =       model(**self._prepare_for_class(a				,						a     )     )
																			SCREAMING_SNAKE_CASE__  :					Tuple =       outputs.attentions
																			self.assertEqual(len(a     )				,						self.model_tester.num_hidden_layers     )

																			self.assertListEqual(
																			    list(attentions[0].shape[-3:]     )				,						[self.model_tester.num_attention_heads, seq_len, seq_len]				,						)
																			SCREAMING_SNAKE_CASE__  :					Optional[Any] =       len(a     )

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

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

																			SCREAMING_SNAKE_CASE__  :					Tuple =       outputs.attentions

																			self.assertEqual(len(a     )				,						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   A_	(   self					:  Dict     )						->int:
													def check_hidden_states_output(a					:  Optional[int]				,						a					:  Union[str, Any]				,						a					:  Tuple     ):
																			SCREAMING_SNAKE_CASE__  :					Optional[int] =       model_class(a     )
																			model.to(a     )
																			model.eval()

																			with torch.no_grad():
																									SCREAMING_SNAKE_CASE__  :					Tuple =       model(**self._prepare_for_class(a				,						a     )     )

																			SCREAMING_SNAKE_CASE__  :					Tuple =       outputs.hidden_states
																			SCREAMING_SNAKE_CASE__  :					Dict =       self.model_tester.num_hidden_layers + 1
																			self.assertEqual(len(a     )				,						a     )

																			SCREAMING_SNAKE_CASE__  :					Optional[int] =       self.model_tester.seq_length

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

													SCREAMING_SNAKE_CASE__,				SCREAMING_SNAKE_CASE__  :					List[str] =       self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			SCREAMING_SNAKE_CASE__  :					Tuple =       True
																			check_hidden_states_output(a				,						a				,						a     )

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			SCREAMING_SNAKE_CASE__  :					Dict =       True

																			check_hidden_states_output(a				,						a				,						a     )






							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."     )
							def   A_	(   self					:  str     )						->Dict:
													pass







def    UpperCAmelCase				(  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					Optional[int] =       hf_hub_download(
						    repo_id="hf-internal-testing/spaghetti-video"      , filename="eating_spaghetti_32_frames.npy"      , repo_type="dataset"  )
						SCREAMING_SNAKE_CASE__  :					str =       np.load(_lowerCamelCase  )
						return list(_lowerCamelCase  )



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






							"""simple docstring"""

							@cached_property
							def   A_	(   self					:  List[Any]     )						->Tuple:
													return VivitImageProcessor() if is_vision_available() else None






							@slow
							def   A_	(   self					:  List[Any]     )						->int:
													SCREAMING_SNAKE_CASE__  :					List[str] =       VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"     ).to(a     )

													SCREAMING_SNAKE_CASE__  :					List[str] =       self.default_image_processor
													SCREAMING_SNAKE_CASE__  :					List[str] =       prepare_video()
													SCREAMING_SNAKE_CASE__  :					Dict =       image_processor(a				,						return_tensors="pt"     ).to(a     )

													# forward pass
													with torch.no_grad():
																			SCREAMING_SNAKE_CASE__  :					str =       model(**a     )

													# verify the logits
													SCREAMING_SNAKE_CASE__  :					List[str] =       torch.Size((1, 4_00)     )
													self.assertEqual(outputs.logits.shape				,						a     )

													# taken from original model
													SCREAMING_SNAKE_CASE__  :					Tuple =       torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]     ).to(a     )

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






from __future__ import annotations

from enum import Enum, unique
from typing import TypeVar

# Create a generic variable that can be 'Enum', or any subclass.
__lowercase							:List[Any]							=	TypeVar("T", bound="Enum")



@unique
class  _a  (					lowercase__				):






							"""simple docstring"""

							snake_case_							=    80
							snake_case_							=    70
							snake_case_							=    60
							snake_case_							=    50
							snake_case_							=    40
							snake_case_							=    30
							snake_case_							=    20
							snake_case_							=    10







@unique
class  _a  (					lowercase__				):






							"""simple docstring"""

							snake_case_							=    24
							snake_case_							=    21
							snake_case_							=    18
							snake_case_							=    15
							snake_case_							=    12
							snake_case_							=    9
							snake_case_							=    6
							snake_case_							=    3
							snake_case_							=    2
							snake_case_							=    1
							snake_case_							=    -1
							snake_case_							=    -2
							snake_case_							=    -3
							snake_case_							=    -6
							snake_case_							=    -9
							snake_case_							=    -12
							snake_case_							=    -15
							snake_case_							=    -18
							snake_case_							=    -21
							snake_case_							=    -24

							@classmethod
							def   A_	(   cls					:  type[T]     )						->dict:
													return {unit.name: unit.value for unit in cls if unit.value > 0}






							@classmethod
							def   A_	(   cls					:  type[T]     )						->dict:
													return {unit.name: unit.value for unit in cls if unit.value < 0}







def    UpperCAmelCase				(  _lowerCamelCase   :	float  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					str =       SIUnit.get_positive() if value > 0 else SIUnit.get_negative()
						for name_prefix, value_prefix in prefixes.items():
												SCREAMING_SNAKE_CASE__  :					str =       value / (10**value_prefix)
												if numerical_part > 1:
																		return f"""{numerical_part!s} {name_prefix}"""
						return str(_lowerCamelCase  )







def    UpperCAmelCase				(  _lowerCamelCase   :	float  ):





						'''simple docstring'''


						for prefix in BinaryUnit:
												SCREAMING_SNAKE_CASE__  :					List[Any] =       value / (2**prefix.value)
												if numerical_part > 1:
																		return f"""{numerical_part!s} {prefix.name}"""
						return str(_lowerCamelCase  )


if __name__ == "__main__":
				import doctest

				doctest.testmod()