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







	'''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.0_2					,							UpperCamelCase__=1e-06					,							UpperCamelCase__=True					,							UpperCamelCase__=None					,							):
					A__	:						int								=				parent
					A__	:						Dict								=				batch_size
					A__	:						Union[str, Any]								=				is_training
					A__	:						List[str]								=				use_labels
					A__	:						Tuple								=				num_labels
					A__	:						Dict								=				image_size
					A__	:						Dict								=				num_frames
					A__	:						int								=				tubelet_size
					A__	:						Optional[int]								=				num_channels
					A__	:						str								=				hidden_size
					A__	:						Any								=				num_hidden_layers
					A__	:						int								=				num_attention_heads
					A__	:						int								=				intermediate_size
					A__	:						Any								=				hidden_act
					A__	:						int								=				hidden_dropout_prob
					A__	:						Optional[Any]								=				attention_probs_dropout_prob
					A__	:						Union[str, Any]								=				initializer_range
					A__	:						int								=				layer_norm_eps
					A__	:						List[Any]								=				qkv_bias
					A__	:						List[Any]								=				scope

					A__	:						int								=				(
					    (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		):
					A__	:						List[str]								=				floats_tensor(
					    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]		)

					A__	:						Tuple								=				None
					if self.use_labels:
									A__	:						Optional[int]								=				ids_tensor([self.batch_size]					,							self.num_labels		)

					A__	:						Any								=				self.get_config()

					return config, pixel_values, labels





	def 	__snake_case					(      self		):
					A__	:						Optional[int]								=				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__	:						Dict								=				self.num_labels
					return config





	def 	__snake_case					(      self					,							UpperCamelCase__					,							UpperCamelCase__					,							UpperCamelCase__		):
					A__	:						str								=				VivitModel(config=UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()
					A__	:						List[str]								=				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__		):
					A__	:						Tuple								=				VivitForVideoClassification(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()

					A__	:						Optional[Any]								=				model(UpperCamelCase__		)

					# verify the logits shape
					A__	:						int								=				torch.Size((self.batch_size, self.num_labels)		)
					self.parent.assertEqual(result.logits.shape					,							UpperCamelCase__		)





	def 	__snake_case					(      self		):
					A__	:						int								=				self.prepare_config_and_inputs()
					A__							,				A__							,				A__	:						Tuple								=				config_and_inputs
					A__	:						int								=				{'''pixel_values''': pixel_values}
					return config, inputs_dict







@require_torch
class 			UpperCamelCase__		(     SCREAMING_SNAKE_CASE_,      unittest.TestCase ):







	'''simple docstring'''
	_lowerCAmelCase											=					(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

	_lowerCAmelCase											=					False
	_lowerCAmelCase											=					False
	_lowerCAmelCase											=					False
	_lowerCAmelCase											=					False





	def 	__snake_case					(      self		):
					A__	:						Union[str, Any]								=				VivitModelTester(self		)
					A__	:						int								=				ConfigTester(self					,							config_class=UpperCamelCase__					,							has_text_modality=UpperCamelCase__					,							hidden_size=37		)





	def 	__snake_case					(      self					,							UpperCamelCase__					,							UpperCamelCase__					,							UpperCamelCase__=False		):
					A__	:						Optional[int]								=				copy.deepcopy(UpperCamelCase__		)

					if return_labels:
									if model_class in get_values(UpperCamelCase__		):
													A__	:						int								=				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		):
					A__							,				A__	:						Optional[int]								=				self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									A__	:						Optional[Any]								=				model_class(UpperCamelCase__		)
									self.assertIsInstance(model.get_input_embeddings()					,							(nn.Module)		)
									A__	:						Dict								=				model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(UpperCamelCase__					,							nn.Linear		)		)





	def 	__snake_case					(      self		):
					A__							,				A__	:						List[str]								=				self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									A__	:						Union[str, Any]								=				model_class(UpperCamelCase__		)
									A__	:						Dict								=				inspect.signature(model.forward		)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									A__	:						List[Any]								=				[*signature.parameters.keys()]

									A__	:						Optional[Any]								=				['''pixel_values''', '''head_mask''']
									self.assertListEqual(arg_names[:2]					,							UpperCamelCase__		)





	def 	__snake_case					(      self		):
					A__	:						str								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCamelCase__		)





	def 	__snake_case					(      self		):
					A__	:						Union[str, Any]								=				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]:
									A__	:						Tuple								=				VivitModel.from_pretrained(UpperCamelCase__		)
									self.assertIsNotNone(UpperCamelCase__		)





	def 	__snake_case					(      self		):
					A__							,				A__	:						Union[str, Any]								=				self.model_tester.prepare_config_and_inputs_for_common()
					A__	:						Tuple								=				True

					for model_class in self.all_model_classes:
									A__	:						List[Any]								=				self.model_tester.seq_length

									A__	:						Union[str, Any]								=				True
									A__	:						Union[str, Any]								=				False
									A__	:						Optional[int]								=				True
									A__	:						Optional[int]								=				model_class(UpperCamelCase__		)
									model.to(UpperCamelCase__		)
									model.eval()
									with torch.no_grad():
													A__	:						Any								=				model(**self._prepare_for_class(UpperCamelCase__					,							UpperCamelCase__		)		)
									A__	:						List[Any]								=				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__	:						str								=				True
									A__	:						Optional[Any]								=				model_class(UpperCamelCase__		)
									model.to(UpperCamelCase__		)
									model.eval()
									with torch.no_grad():
													A__	:						List[Any]								=				model(**self._prepare_for_class(UpperCamelCase__					,							UpperCamelCase__		)		)
									A__	:						int								=				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__	:						List[str]								=				len(UpperCamelCase__		)

									# Check attention is always last and order is fine
									A__	:						int								=				True
									A__	:						str								=				True
									A__	:						List[str]								=				model_class(UpperCamelCase__		)
									model.to(UpperCamelCase__		)
									model.eval()
									with torch.no_grad():
													A__	:						Union[str, Any]								=				model(**self._prepare_for_class(UpperCamelCase__					,							UpperCamelCase__		)		)

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

									A__	:						str								=				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__		):
									A__	:						Optional[int]								=				model_class(UpperCamelCase__		)
									model.to(UpperCamelCase__		)
									model.eval()

									with torch.no_grad():
													A__	:						List[Any]								=				model(**self._prepare_for_class(UpperCamelCase__					,							UpperCamelCase__		)		)

									A__	:						int								=				outputs.hidden_states
									A__	:						Dict								=				self.model_tester.num_hidden_layers + 1
									self.assertEqual(len(UpperCamelCase__		)					,							UpperCamelCase__		)

									A__	:						List[str]								=				self.model_tester.seq_length

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

					A__							,				A__	:						Any								=				self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									A__	:						Any								=				True
									check_hidden_states_output(UpperCamelCase__					,							UpperCamelCase__					,							UpperCamelCase__		)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									A__	:						Optional[int]								=				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 					SCREAMING_SNAKE_CASE  (   )			->							str:

				"""simple docstring"""







				A__	:						List[Any]								=				hf_hub_download(
				    repo_id='''hf-internal-testing/spaghetti-video'''				,    filename='''eating_spaghetti_32_frames.npy'''				,    repo_type='''dataset'''							)
				A__	:						Union[str, Any]								=				np.load(__UpperCamelCase							)
				return list(__UpperCamelCase							)







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







	'''simple docstring'''





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





	@slow
	def 	__snake_case					(      self		):
					A__	:						Tuple								=				VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''		).to(UpperCamelCase__		)

					A__	:						Optional[int]								=				self.default_image_processor
					A__	:						Dict								=				prepare_video()
					A__	:						str								=				image_processor(UpperCamelCase__					,							return_tensors='''pt'''		).to(UpperCamelCase__		)

					# forward pass
					with torch.no_grad():
									A__	:						str								=				model(**UpperCamelCase__		)

					# verify the logits
					A__	:						str								=				torch.Size((1, 400)		)
					self.assertEqual(outputs.logits.shape					,							UpperCamelCase__		)

					# taken from original model
					A__	:						Any								=				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		)		)

import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
					import torch

					from transformers import GPTaLMHeadModel







@require_torch
@require_sentencepiece
@require_tokenizers
class 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





	@slow
	@unittest.skip('''Model is not available.'''		)
	def 	__snake_case					(      self		):
					A__	:						int								=				'''nvidia/megatron-gpt2-345m/'''
					if "MYDIR" in os.environ:
									A__	:						Union[str, Any]								=				os.path.join(os.environ['''MYDIR''']					,							UpperCamelCase__		)
					A__	:						Union[str, Any]								=				GPTaLMHeadModel.from_pretrained(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.half()

					A__	:						Dict								=				torch.tensor(
					    [[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]					,							device=UpperCamelCase__					,							dtype=torch.long					,							)

					with torch.no_grad():
									A__	:						str								=				model(UpperCamelCase__		).logits

					A__	:						int								=				torch.Size((1, 9, 5_0257)		)
					self.assertEqual(output.shape					,							UpperCamelCase__		)

					A__	:						Dict								=				torch.tensor(
					    [
					        4.9_4_1_4,
					        -0.2_9_2_0,
					        -1.2_1_4_8,
					        -4.0_2_7_3,
					        -0.5_1_6_1,
					        -5.2_1_0_9,
					        -1.2_4_1_2,
					        -1.8_3_0_1,
					        -1.7_7_3_4,
					        -4.7_1_4_8,
					        -0.2_3_1_7,
					        -1.0_8_1_1,
					        -2.1_7_7_7,
					        0.4_1_4_1,
					        -3.7_9_6_9,
					        -4.0_5_8_6,
					        -2.5_3_3_2,
					        -3.3_8_0_9,
					        4.3_8_6_7,
					    ]					,							device=UpperCamelCase__					,							dtype=torch.half					,							)

					for i in range(19		):
									A__							,				A__	:						int								=				8 * i // 17, 2792 * i  # along the diagonal
									A__							,				A__	:						Optional[Any]								=				output[0, r, c], expected_diag[i]
									A__	:						Dict								=				F"row={r} col={c} computed={computed} expected={expected}"
									self.assertAlmostEqual(UpperCamelCase__					,							UpperCamelCase__					,							delta=1e-4					,							msg=UpperCamelCase__		)