




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




					'''simple docstring'''






					def __init__(    self,  __UpperCamelCase,  __UpperCamelCase=2,  __UpperCamelCase=True,  __UpperCamelCase=True,  __UpperCamelCase=10,  __UpperCamelCase=10,  __UpperCamelCase=8,  __UpperCamelCase=[2, 4, 4],  __UpperCamelCase=3,  __UpperCamelCase=768,  __UpperCamelCase=5,  __UpperCamelCase=4,  __UpperCamelCase=37,  __UpperCamelCase="gelu_fast",  __UpperCamelCase=0.0,  __UpperCamelCase=0.0,  __UpperCamelCase=0.02,  __UpperCamelCase=1E-06,  __UpperCamelCase=True,  __UpperCamelCase=None,  ):
							_snake_case	       =       parent
							_snake_case	       =       batch_size
							_snake_case	       =       is_training
							_snake_case	       =       use_labels
							_snake_case	       =       num_labels
							_snake_case	       =       image_size
							_snake_case	       =       num_frames
							_snake_case	       =       tubelet_size
							_snake_case	       =       num_channels
							_snake_case	       =       hidden_size
							_snake_case	       =       num_hidden_layers
							_snake_case	       =       num_attention_heads
							_snake_case	       =       intermediate_size
							_snake_case	       =       hidden_act
							_snake_case	       =       hidden_dropout_prob
							_snake_case	       =       attention_probs_dropout_prob
							_snake_case	       =       initializer_range
							_snake_case	       =       layer_norm_eps
							_snake_case	       =       qkv_bias
							_snake_case	       =       scope

							_snake_case	       =       (
							    (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 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       floats_tensor(
							    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] )

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

							_snake_case	       =       self.get_config()

							return config, pixel_values, labels






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       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	       =       self.num_labels
							return config






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






					def 				__SCREAMING_SNAKE_CASE	(    self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase ):
							_snake_case	       =       VivitForVideoClassification(__UpperCamelCase )
							model.to(__UpperCamelCase )
							model.eval()

							_snake_case	       =       model(__UpperCamelCase )

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






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       self.prepare_config_and_inputs()
							_snake_case       ,    _snake_case       ,    _snake_case	       =       config_and_inputs
							_snake_case	       =       {"pixel_values": pixel_values}
							return config, inputs_dict





@require_torch
class      lowerCamelCase_							(       UpperCAmelCase_,       unittest.TestCase							):




					'''simple docstring'''






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

					lowercase__    = False
					lowercase__    = False
					lowercase__    = False
					lowercase__    = False






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       VivitModelTester(self )
							_snake_case	       =       ConfigTester(self,  config_class=__UpperCamelCase,  has_text_modality=__UpperCamelCase,  hidden_size=37 )






					def 				__SCREAMING_SNAKE_CASE	(    self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase=False ):
							_snake_case	       =       copy.deepcopy(__UpperCamelCase )

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

							return inputs_dict






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							self.config_tester.run_common_tests()






					@unittest.skip(reason="Vivit does not use inputs_embeds" )
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							pass






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()

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






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()

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

									_snake_case	       =       ["pixel_values", "head_mask"]
									self.assertListEqual(arg_names[:2],  __UpperCamelCase )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCamelCase )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_video_classification(*__UpperCamelCase )






					@slow
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_snake_case	       =       VivitModel.from_pretrained(__UpperCamelCase )
									self.assertIsNotNone(__UpperCamelCase )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case	       =       True

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

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

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

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

									_snake_case	       =       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 				__SCREAMING_SNAKE_CASE	(    self ):
							def check_hidden_states_output(__UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase ):
									_snake_case	       =       model_class(__UpperCamelCase )
									model.to(__UpperCamelCase )
									model.eval()

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

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

									_snake_case	       =       self.model_tester.seq_length

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

							_snake_case       ,    _snake_case	       =       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									_snake_case	       =       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	       =       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 				__SCREAMING_SNAKE_CASE	(    self ):
							pass






def 		_lowercase		(	):



		'''simple docstring'''







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





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




					'''simple docstring'''






					@cached_property
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							return VivitImageProcessor() if is_vision_available() else None






					@slow
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400" ).to(__UpperCamelCase )

							_snake_case	       =       self.default_image_processor
							_snake_case	       =       prepare_video()
							_snake_case	       =       image_processor(__UpperCamelCase,  return_tensors="pt" ).to(__UpperCamelCase )

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

							# verify the logits
							_snake_case	       =       torch.Size((1, 400) )
							self.assertEqual(outputs.logits.shape,  __UpperCamelCase )

							# taken from original model
							_snake_case	       =       torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58] ).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)





