





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





					"""simple docstring"""


					def __init__(			self						,	_lowerCAmelCase						,	_lowerCAmelCase=2						,	_lowerCAmelCase=True						,	_lowerCAmelCase=True						,	_lowerCAmelCase=10						,	_lowerCAmelCase=10						,	_lowerCAmelCase=8						,	_lowerCAmelCase=[2, 4, 4]						,	_lowerCAmelCase=3						,	_lowerCAmelCase=768						,	_lowerCAmelCase=5						,	_lowerCAmelCase=4						,	_lowerCAmelCase=37						,	_lowerCAmelCase="gelu_fast"						,	_lowerCAmelCase=0.0						,	_lowerCAmelCase=0.0						,	_lowerCAmelCase=0.02						,	_lowerCAmelCase=1E-06						,	_lowerCAmelCase=True						,	_lowerCAmelCase=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        __magic_name__			(			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        __magic_name__			(			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        __magic_name__			(			self						,	_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						):
										lowerCamelCase__      =							VivitModel(config=_lowerCAmelCase						)
										model.to(_lowerCAmelCase						)
										model.eval()
										lowerCamelCase__      =							model(_lowerCAmelCase						)
										self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)						)





					def        __magic_name__			(			self						,	_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						):
										lowerCamelCase__      =							VivitForVideoClassification(_lowerCAmelCase						)
										model.to(_lowerCAmelCase						)
										model.eval()

										lowerCamelCase__      =							model(_lowerCAmelCase						)

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






					def        __magic_name__			(			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 SCREAMING_SNAKE_CASE_			(       lowercase_				,							unittest.TestCase ):





					"""simple docstring"""


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

					A__								=     False
					A__								=     False
					A__								=     False
					A__								=     False





					def        __magic_name__			(			self						):
										lowerCamelCase__      =							VivitModelTester(self						)
										lowerCamelCase__      =							ConfigTester(self						,	config_class=_lowerCAmelCase						,	has_text_modality=_lowerCAmelCase						,	hidden_size=37						)





					def        __magic_name__			(			self						,	_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase=False						):
										lowerCamelCase__      =							copy.deepcopy(_lowerCAmelCase						)

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

										return inputs_dict





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





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





					def        __magic_name__			(			self						):
										lowerCamelCase__     ,		lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs_for_common()

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





					def        __magic_name__			(			self						):
										lowerCamelCase__     ,		lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs_for_common()

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





					def        __magic_name__			(			self						):
										lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCAmelCase						)





					def        __magic_name__			(			self						):
										lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_video_classification(*_lowerCAmelCase						)





					@slow
					def        __magic_name__			(			self						):
										for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCamelCase__      =							VivitModel.from_pretrained(_lowerCAmelCase						)
															self.assertIsNotNone(_lowerCAmelCase						)





					def        __magic_name__			(			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(_lowerCAmelCase						)
															model.to(_lowerCAmelCase						)
															model.eval()
															with torch.no_grad():
																				lowerCamelCase__      =							model(**self._prepare_for_class(_lowerCAmelCase						,	_lowerCAmelCase						)						)
															lowerCamelCase__      =							outputs.attentions
															self.assertEqual(len(_lowerCAmelCase						)						,	self.model_tester.num_hidden_layers						)

															# check that output_attentions also work using config
															del inputs_dict["output_attentions"]
															lowerCamelCase__      =							True
															lowerCamelCase__      =							model_class(_lowerCAmelCase						)
															model.to(_lowerCAmelCase						)
															model.eval()
															with torch.no_grad():
																				lowerCamelCase__      =							model(**self._prepare_for_class(_lowerCAmelCase						,	_lowerCAmelCase						)						)
															lowerCamelCase__      =							outputs.attentions
															self.assertEqual(len(_lowerCAmelCase						)						,	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(_lowerCAmelCase						)

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

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

															lowerCamelCase__      =							outputs.attentions

															self.assertEqual(len(_lowerCAmelCase						)						,	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        __magic_name__			(			self						):
										def check_hidden_states_output(_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						):
															lowerCamelCase__      =							model_class(_lowerCAmelCase						)
															model.to(_lowerCAmelCase						)
															model.eval()

															with torch.no_grad():
																				lowerCamelCase__      =							model(**self._prepare_for_class(_lowerCAmelCase						,	_lowerCAmelCase						)						)

															lowerCamelCase__      =							outputs.hidden_states
															lowerCamelCase__      =							self.model_tester.num_hidden_layers + 1
															self.assertEqual(len(_lowerCAmelCase						)						,	_lowerCAmelCase						)

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

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

															check_hidden_states_output(_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						)






					@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
					def        __magic_name__			(			self						):
										pass





def  __UpperCamelCase   (     )			->Tuple:
					lowerCamelCase__      =							hf_hub_download(
					    repo_id="hf-internal-testing/spaghetti-video",  filename="eating_spaghetti_32_frames.npy",  repo_type="dataset")
					lowerCamelCase__      =							np.load(a)
					return list(a)







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





					"""simple docstring"""


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






					@slow
					def        __magic_name__			(			self						):
										lowerCamelCase__      =							VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"						).to(_lowerCAmelCase						)

										lowerCamelCase__      =							self.default_image_processor
										lowerCamelCase__      =							prepare_video()
										lowerCamelCase__      =							image_processor(_lowerCAmelCase						,	return_tensors="pt"						).to(_lowerCAmelCase						)

										# forward pass
										with torch.no_grad():
															lowerCamelCase__      =							model(**_lowerCAmelCase						)

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

										# taken from original model
										lowerCamelCase__      =							torch.tensor([-1.0_543, 2.0_764, -0.2_104, 0.4_439, -0.9_658]						).to(_lowerCAmelCase						)

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








# this script builds a small sample spm file tests/fixtures/test_sentencepiece_no_bos.model, with features needed by pegasus

# 1. pip install sentencepiece
#
# 2. wget https://raw.githubusercontent.com/google/sentencepiece/master/data/botchan.txt

# 3. build
import sentencepiece as spm

# pegasus:
# 1. no bos
# 2. eos_id is 1
# 3. unk_id is 2
# build a sample spm file accordingly
spm.SentencePieceTrainer.train(
    "--input=botchan.txt --model_prefix=test_sentencepiece_no_bos --bos_id=-1 --unk_id=2  --eos_id=1  --vocab_size=1000"
)

# 4. now update the fixture
# mv test_sentencepiece_no_bos.model ../../tests/fixtures/

