





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 ,			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.0_2 ,			UpperCAmelCase_=1E-06 ,			UpperCAmelCase_=True ,			UpperCAmelCase_=None ,			):
								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 _snake_case							(		self				):
								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 _snake_case							(		self				):
								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 _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=VivitModel(config=UpperCAmelCase_				)
								model.to(UpperCAmelCase_				)
								model.eval()
								lowerCamelCase									=model(UpperCAmelCase_				)
								self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=VivitForVideoClassification(UpperCAmelCase_				)
								model.to(UpperCAmelCase_				)
								model.eval()

								lowerCamelCase									=model(UpperCAmelCase_				)

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


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




@require_torch
class    __A  (    __snake_case     ,       unittest.TestCase     ):
				__A  =					(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

				__A  =					False
				__A  =					False
				__A  =					False
				__A  =					False


				def _snake_case							(		self				):
								lowerCamelCase									=VivitModelTester(self				)
								lowerCamelCase									=ConfigTester(self ,			config_class=UpperCAmelCase_ ,			has_text_modality=UpperCAmelCase_ ,			hidden_size=37				)


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_=False				):
								lowerCamelCase									=copy.deepcopy(UpperCAmelCase_				)

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

								return inputs_dict


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


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


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

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


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

								for model_class in self.all_model_classes:
												lowerCamelCase									=model_class(UpperCAmelCase_				)
												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] ,			UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase_				)


				def _snake_case							(		self				):
								lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_video_classification(*UpperCAmelCase_				)


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


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

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

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

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

												with torch.no_grad():
																lowerCamelCase									=model(**self._prepare_for_class(UpperCAmelCase_ ,			UpperCAmelCase_				)				)

												lowerCamelCase									=outputs.hidden_states
												lowerCamelCase									=self.model_tester.num_hidden_layers + 1
												self.assertEqual(len(UpperCAmelCase_				) ,			UpperCAmelCase_				)

												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(UpperCAmelCase_ ,			UpperCAmelCase_ ,			UpperCAmelCase_				)

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





def   _lowercase       (    )    ->	Tuple:
				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    __A  (    unittest.TestCase     ):


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


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

								lowerCamelCase									=self.default_image_processor
								lowerCamelCase									=prepare_video()
								lowerCamelCase									=image_processor(UpperCAmelCase_ ,			return_tensors="""pt"""				).to(UpperCAmelCase_				)

								# forward pass
								with torch.no_grad():
												lowerCamelCase									=model(**UpperCAmelCase_				)

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

								# taken from original model
								lowerCamelCase									=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				)				)








from math import pi



def   UpperCamelCase(   lowercase_				)      ->							float:





  '''simple docstring'''





  return degree / (180 / pi)


if __name__ == "__main__":
    from doctest import testmod

    testmod()