




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







							def __init__(  self       :  Union[str, Any]							,			lowerCAmelCase_       :  Any							,			lowerCAmelCase_       :  List[Any]=2							,			lowerCAmelCase_       :  Dict=True							,			lowerCAmelCase_       :  Union[str, Any]=True							,			lowerCAmelCase_       :  Tuple=10							,			lowerCAmelCase_       :  Optional[Any]=10							,			lowerCAmelCase_       :  Optional[int]=8							,			lowerCAmelCase_       :  Optional[int]=[2, 4, 4]							,			lowerCAmelCase_       :  List[str]=3							,			lowerCAmelCase_       :  Optional[int]=7_68							,			lowerCAmelCase_       :  Optional[Any]=5							,			lowerCAmelCase_       :  Optional[int]=4							,			lowerCAmelCase_       :  str=37							,			lowerCAmelCase_       :  Optional[int]="gelu_fast"							,			lowerCAmelCase_       :  str=0.0							,			lowerCAmelCase_       :  str=0.0							,			lowerCAmelCase_       :  Tuple=0.0_2							,			lowerCAmelCase_       :  str=1e-06							,			lowerCAmelCase_       :  Union[str, Any]=True							,			lowerCAmelCase_       :  str=None							,			)			->							Dict:


														"""simple docstring"""


														_a	     =				parent
														_a	     =				batch_size
														_a	     =				is_training
														_a	     =				use_labels
														_a	     =				num_labels
														_a	     =				image_size
														_a	     =				num_frames
														_a	     =				tubelet_size
														_a	     =				num_channels
														_a	     =				hidden_size
														_a	     =				num_hidden_layers
														_a	     =				num_attention_heads
														_a	     =				intermediate_size
														_a	     =				hidden_act
														_a	     =				hidden_dropout_prob
														_a	     =				attention_probs_dropout_prob
														_a	     =				initializer_range
														_a	     =				layer_norm_eps
														_a	     =				qkv_bias
														_a	     =				scope

														_a	     =				(
														    (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       :  Dict					)			->							List[Any]:


														"""simple docstring"""


														_a	     =				floats_tensor(
														    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

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

														_a	     =				self.get_config()

														return config, pixel_values, labels







							def    __lowerCAmelCase			(  self       :  Union[str, Any]					)			->							Union[str, Any]:


														"""simple docstring"""


														_a	     =				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							,			)
														_a	     =				self.num_labels
														return config







							def    __lowerCAmelCase			(  self       :  Any							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  str							,			lowerCAmelCase_       :  Tuple					)			->							List[str]:


														"""simple docstring"""


														_a	     =				VivitModel(config=__UpperCamelCase					)
														model.to(__UpperCamelCase					)
														model.eval()
														_a	     =				model(__UpperCamelCase					)
														self.parent.assertEqual(result.last_hidden_state.shape							,			(self.batch_size, self.seq_length, self.hidden_size)					)







							def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  Tuple							,			lowerCAmelCase_       :  int							,			lowerCAmelCase_       :  int					)			->							Optional[Any]:


														"""simple docstring"""


														_a	     =				VivitForVideoClassification(__UpperCamelCase					)
														model.to(__UpperCamelCase					)
														model.eval()

														_a	     =				model(__UpperCamelCase					)

														# verify the logits shape
														_a	     =				torch.Size((self.batch_size, self.num_labels)					)
														self.parent.assertEqual(result.logits.shape							,			__UpperCamelCase					)







							def    __lowerCAmelCase			(  self       :  int					)			->							Optional[Any]:


														"""simple docstring"""


														_a	     =				self.prepare_config_and_inputs()
														_a     ,							_a     ,							_a	     =				config_and_inputs
														_a	     =				{'''pixel_values''': pixel_values}
														return config, inputs_dict



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

							lowercase_							=				False
							lowercase_							=				False
							lowercase_							=				False
							lowercase_							=				False







							def    __lowerCAmelCase			(  self       :  Dict					)			->							Tuple:


														"""simple docstring"""


														_a	     =				VivitModelTester(self					)
														_a	     =				ConfigTester(self							,			config_class=__UpperCamelCase							,			has_text_modality=__UpperCamelCase							,			hidden_size=37					)







							def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  int							,			lowerCAmelCase_       :  Optional[int]							,			lowerCAmelCase_       :  Tuple=False					)			->							Dict:


														"""simple docstring"""


														_a	     =				copy.deepcopy(__UpperCamelCase					)

														if return_labels:
																					if model_class in get_values(__UpperCamelCase					):
																												_a	     =				torch.zeros(
																												    self.model_tester.batch_size							,			dtype=torch.long							,			device=__UpperCamelCase					)

														return inputs_dict







							def    __lowerCAmelCase			(  self       :  Any					)			->							Optional[int]:


														"""simple docstring"""


														self.config_tester.run_common_tests()







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


														"""simple docstring"""


														pass







							def    __lowerCAmelCase			(  self       :  Dict					)			->							Optional[int]:


														"""simple docstring"""


														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()

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







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Optional[Any]:


														"""simple docstring"""


														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()

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

																					_a	     =				['''pixel_values''', '''head_mask''']
																					self.assertListEqual(arg_names[:2]							,			__UpperCamelCase					)







							def    __lowerCAmelCase			(  self       :  int					)			->							List[str]:


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCamelCase					)







							def    __lowerCAmelCase			(  self       :  List[str]					)			->							Union[str, Any]:


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_video_classification(*__UpperCamelCase					)







							@slow
							def    __lowerCAmelCase			(  self       :  Optional[Any]					)			->							Optional[int]:


														"""simple docstring"""


														for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_a	     =				VivitModel.from_pretrained(__UpperCamelCase					)
																					self.assertIsNotNone(__UpperCamelCase					)







							def    __lowerCAmelCase			(  self       :  List[Any]					)			->							List[str]:


														"""simple docstring"""


														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()
														_a	     =				True

														for model_class in self.all_model_classes:
																					_a	     =				self.model_tester.seq_length

																					_a	     =				True
																					_a	     =				False
																					_a	     =				True
																					_a	     =				model_class(__UpperCamelCase					)
																					model.to(__UpperCamelCase					)
																					model.eval()
																					with torch.no_grad():
																												_a	     =				model(**self._prepare_for_class(__UpperCamelCase							,			__UpperCamelCase					)					)
																					_a	     =				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"]
																					_a	     =				True
																					_a	     =				model_class(__UpperCamelCase					)
																					model.to(__UpperCamelCase					)
																					model.eval()
																					with torch.no_grad():
																												_a	     =				model(**self._prepare_for_class(__UpperCamelCase							,			__UpperCamelCase					)					)
																					_a	     =				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]							,			)
																					_a	     =				len(__UpperCamelCase					)

																					# Check attention is always last and order is fine
																					_a	     =				True
																					_a	     =				True
																					_a	     =				model_class(__UpperCamelCase					)
																					model.to(__UpperCamelCase					)
																					model.eval()
																					with torch.no_grad():
																												_a	     =				model(**self._prepare_for_class(__UpperCamelCase							,			__UpperCamelCase					)					)

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

																					_a	     =				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       :  Any					)			->							int:


														"""simple docstring"""


														def check_hidden_states_output(lowerCAmelCase_       :  Dict							,			lowerCAmelCase_       :  Tuple							,			lowerCAmelCase_       :  List[Any]					):
																					_a	     =				model_class(__UpperCamelCase					)
																					model.to(__UpperCamelCase					)
																					model.eval()

																					with torch.no_grad():
																												_a	     =				model(**self._prepare_for_class(__UpperCamelCase							,			__UpperCamelCase					)					)

																					_a	     =				outputs.hidden_states
																					_a	     =				self.model_tester.num_hidden_layers + 1
																					self.assertEqual(len(__UpperCamelCase					)							,			__UpperCamelCase					)

																					_a	     =				self.model_tester.seq_length

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

														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					_a	     =				True
																					check_hidden_states_output(__UpperCamelCase							,			__UpperCamelCase							,			__UpperCamelCase					)

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


														"""simple docstring"""


														pass



def 		snake_case_    ():





							'''simple docstring'''


							_a	     =				hf_hub_download(
							    repo_id='''hf-internal-testing/spaghetti-video''' ,						filename='''eating_spaghetti_32_frames.npy''' ,						repo_type='''dataset'''      )
							_a	     =				np.load(lowercase_      )
							return list(lowercase_      )

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







							@cached_property
							def    __lowerCAmelCase			(  self       :  Tuple					)			->							int:


														"""simple docstring"""


														return VivitImageProcessor() if is_vision_available() else None







							@slow
							def    __lowerCAmelCase			(  self       :  Union[str, Any]					)			->							str:


														"""simple docstring"""


														_a	     =				VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''					).to(__UpperCamelCase					)

														_a	     =				self.default_image_processor
														_a	     =				prepare_video()
														_a	     =				image_processor(__UpperCamelCase							,			return_tensors='''pt'''					).to(__UpperCamelCase					)

														# forward pass
														with torch.no_grad():
																					_a	     =				model(**__UpperCamelCase					)

														# verify the logits
														_a	     =				torch.Size((1, 4_00)					)
														self.assertEqual(outputs.logits.shape							,			__UpperCamelCase					)

														# taken from original model
														_a	     =				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(__UpperCamelCase					)

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







'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_lowerCamelCase							:						Optional[Any]								=  {'configuration_bit': ['BIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BitConfig', 'BitOnnxConfig']}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						Dict								=  [
						    'BIT_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'BitForImageClassification',
						    'BitModel',
						    'BitPreTrainedModel',
						    'BitBackbone',
						]


try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						Union[str, Any]								=  ['BitImageProcessor']


if TYPE_CHECKING:
						from .configuration_bit import BIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BitConfig, BitOnnxConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_bit import (
												    BIT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    BitBackbone,
												    BitForImageClassification,
												    BitModel,
												    BitPreTrainedModel,
												)

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .image_processing_bit import BitImageProcessor

else:
						import sys

						_lowerCamelCase							:						Optional[Any]								=  _LazyModule(__name__, globals()['__file__'], _import_structure)





