






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






						def __init__(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=2   ,  _UpperCamelCase=True   ,  _UpperCamelCase=True   ,  _UpperCamelCase=10   ,  _UpperCamelCase=10   ,  _UpperCamelCase=8   ,  _UpperCamelCase=[2, 4, 4]   ,  _UpperCamelCase=3   ,  _UpperCamelCase=7_68   ,  _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   ,  ):






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

												lowerCAmelCase__			  =  (
												    (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      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  floats_tensor(
												    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

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

												lowerCAmelCase__			  =  self.get_config()

												return config, pixel_values, labels






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  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   ,  )
												lowerCAmelCase__			  =  self.num_labels
												return config






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  VivitModel(config=a__      )
												model.to(a__      )
												model.eval()
												lowerCAmelCase__			  =  model(a__      )
												self.parent.assertEqual(result.last_hidden_state.shape   ,  (self.batch_size, self.seq_length, self.hidden_size)      )






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  VivitForVideoClassification(a__      )
												model.to(a__      )
												model.eval()

												lowerCAmelCase__			  =  model(a__      )

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







						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.prepare_config_and_inputs()
												lowerCAmelCase__  ,				lowerCAmelCase__  ,				lowerCAmelCase__			  =  config_and_inputs
												lowerCAmelCase__			  =  {'pixel_values': pixel_values}
												return config, inputs_dict




@require_torch
class 			__SCREAMING_SNAKE_CASE    (      _UpperCamelCase						,    unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:				Union[str, Any]            =    (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

						_SCREAMING_SNAKE_CASE		:				Any            =    False
						_SCREAMING_SNAKE_CASE		:				Dict            =    False
						_SCREAMING_SNAKE_CASE		:				Optional[Any]            =    False
						_SCREAMING_SNAKE_CASE		:				List[Any]            =    False






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  VivitModelTester(self      )
												lowerCAmelCase__			  =  ConfigTester(self   ,  config_class=a__   ,  has_text_modality=a__   ,  hidden_size=37      )






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase=False      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  copy.deepcopy(a__      )

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

												return inputs_dict






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												self.config_tester.run_common_tests()






						@unittest.skip(reason='Vivit does not use inputs_embeds'      )
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__  ,				lowerCAmelCase__			  =  self.model_tester.prepare_config_and_inputs_for_common()

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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__  ,				lowerCAmelCase__			  =  self.model_tester.prepare_config_and_inputs_for_common()

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

																		lowerCAmelCase__			  =  ['pixel_values', 'head_mask']
																		self.assertListEqual(arg_names[:2]   ,  a__      )






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*a__      )






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_video_classification(*a__      )






						@slow
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""







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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__  ,				lowerCAmelCase__			  =  self.model_tester.prepare_config_and_inputs_for_common()
												lowerCAmelCase__			  =  True

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

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

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

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

																		lowerCAmelCase__			  =  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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""







												def check_hidden_states_output(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      ):
																		lowerCAmelCase__			  =  model_class(a__      )
																		model.to(a__      )
																		model.eval()

																		with torch.no_grad():
																								lowerCAmelCase__			  =  model(**self._prepare_for_class(a__   ,  a__      )      )

																		lowerCAmelCase__			  =  outputs.hidden_states
																		lowerCAmelCase__			  =  self.model_tester.num_hidden_layers + 1
																		self.assertEqual(len(a__      )   ,  a__      )

																		lowerCAmelCase__			  =  self.model_tester.seq_length

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

												lowerCAmelCase__  ,				lowerCAmelCase__			  =  self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowerCAmelCase__			  =  True
																		check_hidden_states_output(a__   ,  a__   ,  a__      )

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		lowerCAmelCase__			  =  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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass







def 		_UpperCamelCase      (			)     ->      Any:

						"""simple docstring"""



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


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






						@cached_property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												return VivitImageProcessor() if is_vision_available() else None







						@slow
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400'      ).to(a__      )

												lowerCAmelCase__			  =  self.default_image_processor
												lowerCAmelCase__			  =  prepare_video()
												lowerCAmelCase__			  =  image_processor(a__   ,  return_tensors='pt'      ).to(a__      )

												# forward pass
												with torch.no_grad():
																		lowerCAmelCase__			  =  model(**a__      )

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

												# taken from original model
												lowerCAmelCase__			  =  torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]      ).to(a__      )

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









"""simple docstring"""


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
class snake_case					(     _UpperCamelCase):
		__UpperCamelCase							=	'AutoFeatureExtractor'
		__UpperCamelCase							=	'Speech2Text2Tokenizer'







		def __init__(    self					:				int			,      a__					:				Tuple			,      a__					:				Optional[Any]						)				->  str:





						'''simple docstring'''







						super().__init__(a__			,      a__						)
						_A         =	self.feature_extractor
						_A         =	False







		def __call__(    self					:				Optional[int]			,      *a__					:				List[str]			,      **a__					:				Any						)				->  List[Any]:





						'''simple docstring'''







						if self._in_target_context_manager:
										return self.current_processor(*a__			,      **a__						)

						if "raw_speech" in kwargs:
										warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."						)
										_A         =	kwargs.pop("raw_speech"						)
						else:
										_A         =	kwargs.pop("audio"			,      a__						)
						_A         =	kwargs.pop("sampling_rate"			,      a__						)
						_A         =	kwargs.pop("text"			,      a__						)
						if len(a__						) > 0:
										_A         =	args[0]
										_A         =	args[1:]

						if audio is None and text is None:
										raise ValueError("You need to specify either an `audio` or `text` input to process."						)

						if audio is not None:
										_A         =	self.feature_extractor(a__			,      *a__			,      sampling_rate=a__			,      **a__						)
						if text is not None:
										_A         =	self.tokenizer(a__			,      **a__						)

						if text is None:
										return inputs
						elif audio is None:
										return encodings
						else:
										_A         =	encodings["input_ids"]
										return inputs







		def        a_       (    self					:				List[Any]			,      *a__					:				Tuple			,      **a__					:				List[Any]						)				->  List[Any]:





						'''simple docstring'''







						return self.tokenizer.batch_decode(*a__			,      **a__						)







		def        a_       (    self					:				Any			,      *a__					:				Dict			,      **a__					:				Tuple						)				->  Union[str, Any]:





						'''simple docstring'''







						return self.tokenizer.decode(*a__			,      **a__						)







		@contextmanager
		def        a_       (    self					:				Optional[int]						)				->  Optional[int]:





						'''simple docstring'''







						warnings.warn(
						    "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
						    "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
						    "your audio inputs, or in a separate call."						)
						_A         =	True
						_A         =	self.tokenizer
						yield
						_A         =	self.feature_extractor
						_A         =	False