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

				def __init__(  self							:     int							,     _lowercase							:     Union[str, Any]							,     _lowercase							:     List[str]=2							,     _lowercase							:     Dict=True							,     _lowercase							:     int=True							,     _lowercase							:     List[str]=1_0							,     _lowercase							:     int=1_0							,     _lowercase							:     List[Any]=8							,     _lowercase							:     Union[str, Any]=[2, 4, 4]							,     _lowercase							:     str=3							,     _lowercase							:     List[Any]=7_6_8							,     _lowercase							:     Union[str, Any]=5							,     _lowercase							:     Union[str, Any]=4							,     _lowercase							:     Optional[Any]=3_7							,     _lowercase							:     Dict="gelu_fast"							,     _lowercase							:     List[str]=0.0							,     _lowercase							:     Optional[int]=0.0							,     _lowercase							:     List[str]=0.02							,     _lowercase							:     Optional[int]=1e-06							,     _lowercase							:     Union[str, Any]=True							,     _lowercase							:     Dict=None							,     ):


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

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


										'''simple docstring'''
										UpperCAmelCase__       						=		floats_tensor(
										    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]  )

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

										UpperCAmelCase__       						=		self.get_config()

										return config, pixel_values, labels

				def 							A			(  self							:     Dict  ):


										'''simple docstring'''
										UpperCAmelCase__       						=		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__       						=		self.num_labels
										return config

				def 							A			(  self							:     List[Any]							,     _lowercase							:     Any							,     _lowercase							:     Any							,     _lowercase							:     Union[str, Any]  ):


										'''simple docstring'''
										UpperCAmelCase__       						=		VivitModel(config=_lowercase  )
										model.to(_lowercase  )
										model.eval()
										UpperCAmelCase__       						=		model(_lowercase  )
										self.parent.assertEqual(result.last_hidden_state.shape							,     (self.batch_size, self.seq_length, self.hidden_size)  )

				def 							A			(  self							:     List[Any]							,     _lowercase							:     str							,     _lowercase							:     Optional[Any]							,     _lowercase							:     int  ):


										'''simple docstring'''
										UpperCAmelCase__       						=		VivitForVideoClassification(_lowercase  )
										model.to(_lowercase  )
										model.eval()

										UpperCAmelCase__       						=		model(_lowercase  )

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

				def 							A			(  self							:     List[Any]  ):


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


@require_torch
class       _lowercase		( __lowerCamelCase   ,   unittest.TestCase	):
				lowerCamelCase__							=							(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

				lowerCamelCase__							=							False
				lowerCamelCase__							=							False
				lowerCamelCase__							=							False
				lowerCamelCase__							=							False

				def 							A			(  self							:     Optional[int]  ):


										'''simple docstring'''
										UpperCAmelCase__       						=		VivitModelTester(self  )
										UpperCAmelCase__       						=		ConfigTester(self							,     config_class=_lowercase							,     has_text_modality=_lowercase							,     hidden_size=3_7  )

				def 							A			(  self							:     Tuple							,     _lowercase							:     Optional[Any]							,     _lowercase							:     Tuple							,     _lowercase							:     str=False  ):


										'''simple docstring'''
										UpperCAmelCase__       						=		copy.deepcopy(_lowercase  )

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

										return inputs_dict

				def 							A			(  self							:     Optional[Any]  ):


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

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


										'''simple docstring'''
										pass

				def 							A			(  self							:     Optional[int]  ):


										'''simple docstring'''
										UpperCAmelCase__						,   UpperCAmelCase__       						=		self.model_tester.prepare_config_and_inputs_for_common()

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

				def 							A			(  self							:     Optional[int]  ):


										'''simple docstring'''
										UpperCAmelCase__						,   UpperCAmelCase__       						=		self.model_tester.prepare_config_and_inputs_for_common()

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

																UpperCAmelCase__       						=		["""pixel_values""", """head_mask"""]
																self.assertListEqual(arg_names[:2]							,     _lowercase  )

				def 							A			(  self							:     Tuple  ):


										'''simple docstring'''
										UpperCAmelCase__       						=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowercase  )

				def 							A			(  self							:     Dict  ):


										'''simple docstring'''
										UpperCAmelCase__       						=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_video_classification(*_lowercase  )

				@slow
				def 							A			(  self							:     str  ):


										'''simple docstring'''
										for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																UpperCAmelCase__       						=		VivitModel.from_pretrained(_lowercase  )
																self.assertIsNotNone(_lowercase  )

				def 							A			(  self							:     Any  ):


										'''simple docstring'''
										UpperCAmelCase__						,   UpperCAmelCase__       						=		self.model_tester.prepare_config_and_inputs_for_common()
										UpperCAmelCase__       						=		True

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

																UpperCAmelCase__       						=		True
																UpperCAmelCase__       						=		False
																UpperCAmelCase__       						=		True
																UpperCAmelCase__       						=		model_class(_lowercase  )
																model.to(_lowercase  )
																model.eval()
																with torch.no_grad():
																						UpperCAmelCase__       						=		model(**self._prepare_for_class(_lowercase							,     _lowercase  )  )
																UpperCAmelCase__       						=		outputs.attentions
																self.assertEqual(len(_lowercase  )							,     self.model_tester.num_hidden_layers  )

																# check that output_attentions also work using config
																del inputs_dict["output_attentions"]
																UpperCAmelCase__       						=		True
																UpperCAmelCase__       						=		model_class(_lowercase  )
																model.to(_lowercase  )
																model.eval()
																with torch.no_grad():
																						UpperCAmelCase__       						=		model(**self._prepare_for_class(_lowercase							,     _lowercase  )  )
																UpperCAmelCase__       						=		outputs.attentions
																self.assertEqual(len(_lowercase  )							,     self.model_tester.num_hidden_layers  )

																self.assertListEqual(
																    list(attentions[0].shape[-3:]  )							,     [self.model_tester.num_attention_heads, seq_len, seq_len]							,     )
																UpperCAmelCase__       						=		len(_lowercase  )

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

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

																UpperCAmelCase__       						=		outputs.attentions

																self.assertEqual(len(_lowercase  )							,     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							:     int  ):


										'''simple docstring'''
										def check_hidden_states_output(_lowercase							:     Optional[int]							,     _lowercase							:     int							,     _lowercase							:     Union[str, Any]  ):
																UpperCAmelCase__       						=		model_class(_lowercase  )
																model.to(_lowercase  )
																model.eval()

																with torch.no_grad():
																						UpperCAmelCase__       						=		model(**self._prepare_for_class(_lowercase							,     _lowercase  )  )

																UpperCAmelCase__       						=		outputs.hidden_states
																UpperCAmelCase__       						=		self.model_tester.num_hidden_layers + 1
																self.assertEqual(len(_lowercase  )							,     _lowercase  )

																UpperCAmelCase__       						=		self.model_tester.seq_length

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

										UpperCAmelCase__						,   UpperCAmelCase__       						=		self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																UpperCAmelCase__       						=		True
																check_hidden_states_output(_lowercase							,     _lowercase							,     _lowercase  )

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																UpperCAmelCase__       						=		True

																check_hidden_states_output(_lowercase							,     _lowercase							,     _lowercase  )

				@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""  )
				def 							A			(  self							:     List[str]  ):


										'''simple docstring'''
										pass


def 	a__   (		)->						List[str]:
						UpperCAmelCase__       						=		hf_hub_download(
						    repo_id="""hf-internal-testing/spaghetti-video"""		,      filename="""eating_spaghetti_32_frames.npy"""		,      repo_type="""dataset"""      )
						UpperCAmelCase__       						=		np.load(UpperCAmelCase__      )
						return list(UpperCAmelCase__      )







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

				@cached_property
				def 							A			(  self							:     int  ):


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

				@slow
				def 							A			(  self							:     Optional[Any]  ):


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

										UpperCAmelCase__       						=		self.default_image_processor
										UpperCAmelCase__       						=		prepare_video()
										UpperCAmelCase__       						=		image_processor(_lowercase							,     return_tensors="""pt"""  ).to(_lowercase  )

										# forward pass
										with torch.no_grad():
																UpperCAmelCase__       						=		model(**_lowercase  )

										# verify the logits
										UpperCAmelCase__       						=		torch.Size((1, 4_0_0)  )
										self.assertEqual(outputs.logits.shape							,     _lowercase  )

										# taken from original model
										UpperCAmelCase__       						=		torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]  ).to(_lowercase  )

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


"""simple docstring"""







from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
						if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.27.0""")):
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						from ...utils.dummy_torch_and_transformers_objects import (
						    AudioLDMPipeline,
						)
else:
						from .pipeline_audioldm import AudioLDMPipeline

