



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							:			Optional[Any]	,	a_							:			Optional[Any]	,	a_							:			Any=2	,	a_							:			Optional[int]=True	,	a_							:			List[str]=True	,	a_							:			Optional[int]=10	,	a_							:			int=10	,	a_							:			Optional[Any]=8	,	a_							:			Optional[Any]=[2, 4, 4]	,	a_							:			List[str]=3	,	a_							:			Union[str, Any]=7_68	,	a_							:			Union[str, Any]=5	,	a_							:			str=4	,	a_							:			Optional[int]=37	,	a_							:			str="gelu_fast"	,	a_							:			Union[str, Any]=0.0	,	a_							:			str=0.0	,	a_							:			Tuple=0.0_2	,	a_							:			List[Any]=1e-06	,	a_							:			str=True	,	a_							:			int=None	,	):
					'''simple docstring'''





					__UpperCAmelCase   :					List[Any]													=     parent
					__UpperCAmelCase   :					Optional[Any]													=     batch_size
					__UpperCAmelCase   :					List[Any]													=     is_training
					__UpperCAmelCase   :					Optional[int]													=     use_labels
					__UpperCAmelCase   :					Union[str, Any]													=     num_labels
					__UpperCAmelCase   :					str													=     image_size
					__UpperCAmelCase   :					Tuple													=     num_frames
					__UpperCAmelCase   :					Dict													=     tubelet_size
					__UpperCAmelCase   :					Optional[Any]													=     num_channels
					__UpperCAmelCase   :					str													=     hidden_size
					__UpperCAmelCase   :					Tuple													=     num_hidden_layers
					__UpperCAmelCase   :					Union[str, Any]													=     num_attention_heads
					__UpperCAmelCase   :					str													=     intermediate_size
					__UpperCAmelCase   :					Any													=     hidden_act
					__UpperCAmelCase   :					Optional[int]													=     hidden_dropout_prob
					__UpperCAmelCase   :					List[str]													=     attention_probs_dropout_prob
					__UpperCAmelCase   :					List[Any]													=     initializer_range
					__UpperCAmelCase   :					str													=     layer_norm_eps
					__UpperCAmelCase   :					Dict													=     qkv_bias
					__UpperCAmelCase   :					Optional[int]													=     scope

					__UpperCAmelCase   :					Optional[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							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Union[str, Any]													=     floats_tensor(
					    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]			)

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

					__UpperCAmelCase   :					Dict													=     self.get_config()

					return config, pixel_values, labels




			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





					__UpperCAmelCase   :					Union[str, Any]													=     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   :					Tuple													=     self.num_labels
					return config




			def 			snake_case__					(       self							:			List[Any]	,	a_							:			Any	,	a_							:			List[Any]	,	a_							:			Optional[Any]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Optional[Any]													=     VivitModel(config=a_			)
					model.to(a_			)
					model.eval()
					__UpperCAmelCase   :					Dict													=     model(a_			)
					self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.hidden_size)			)




			def 			snake_case__					(       self							:			Any	,	a_							:			Optional[Any]	,	a_							:			int	,	a_							:			str			):
					'''simple docstring'''





					__UpperCAmelCase   :					Optional[int]													=     VivitForVideoClassification(a_			)
					model.to(a_			)
					model.eval()

					__UpperCAmelCase   :					Optional[Any]													=     model(a_			)

					# verify the logits shape
					__UpperCAmelCase   :					List[Any]													=     torch.Size((self.batch_size, self.num_labels)			)
					self.parent.assertEqual(result.logits.shape	,	a_			)




			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





					__UpperCAmelCase   :					Optional[Any]													=     self.prepare_config_and_inputs()
					__UpperCAmelCase							, __UpperCAmelCase							, __UpperCAmelCase   :					Optional[int]													=     config_and_inputs
					__UpperCAmelCase   :					Optional[Any]													=     {'''pixel_values''': pixel_values}
					return config, inputs_dict

@require_torch
class 		UpperCAmelCase__      (      __UpperCamelCase						,unittest.TestCase       ):




			'''simple docstring'''






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

			UpperCamelCase      						=	False
			UpperCamelCase      						=	False
			UpperCamelCase      						=	False
			UpperCamelCase      						=	False




			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





					__UpperCAmelCase   :					str													=     VivitModelTester(self			)
					__UpperCAmelCase   :					str													=     ConfigTester(self	,	config_class=a_	,	has_text_modality=a_	,	hidden_size=37			)




			def 			snake_case__					(       self							:			List[Any]	,	a_							:			Optional[int]	,	a_							:			Dict	,	a_							:			int=False			):
					'''simple docstring'''





					__UpperCAmelCase   :					str													=     copy.deepcopy(a_			)

					if return_labels:
							if model_class in get_values(a_			):
									__UpperCAmelCase   :					Any													=     torch.zeros(
									    self.model_tester.batch_size	,	dtype=torch.long	,	device=a_			)

					return inputs_dict




			def 			snake_case__					(       self							:			Union[str, Any]			):
					'''simple docstring'''





					self.config_tester.run_common_tests()




			@unittest.skip(reason='''Vivit does not use inputs_embeds'''			)
			def 			snake_case__					(       self							:			Dict			):
					'''simple docstring'''





					pass




			def 			snake_case__					(       self							:			Optional[Any]			):
					'''simple docstring'''





					__UpperCAmelCase							, __UpperCAmelCase   :					int													=     self.model_tester.prepare_config_and_inputs_for_common()

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




			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





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

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

							__UpperCAmelCase   :					List[Any]													=     ['''pixel_values''', '''head_mask''']
							self.assertListEqual(arg_names[:2]	,	a_			)




			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





					__UpperCAmelCase   :					Optional[int]													=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*a_			)




			def 			snake_case__					(       self							:			Dict			):
					'''simple docstring'''





					__UpperCAmelCase   :					Union[str, Any]													=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_video_classification(*a_			)




			@slow
			def 			snake_case__					(       self							:			Tuple			):
					'''simple docstring'''





					for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__UpperCAmelCase   :					Tuple													=     VivitModel.from_pretrained(a_			)
							self.assertIsNotNone(a_			)




			def 			snake_case__					(       self							:			Dict			):
					'''simple docstring'''





					__UpperCAmelCase							, __UpperCAmelCase   :					Tuple													=     self.model_tester.prepare_config_and_inputs_for_common()
					__UpperCAmelCase   :					List[str]													=     True

					for model_class in self.all_model_classes:
							__UpperCAmelCase   :					int													=     self.model_tester.seq_length

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

							# Check attention is always last and order is fine
							__UpperCAmelCase   :					Dict													=     True
							__UpperCAmelCase   :					List[str]													=     True
							__UpperCAmelCase   :					Optional[int]													=     model_class(a_			)
							model.to(a_			)
							model.eval()
							with torch.no_grad():
									__UpperCAmelCase   :					int													=     model(**self._prepare_for_class(a_	,	a_			)			)

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

							__UpperCAmelCase   :					str													=     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 			snake_case__					(       self							:			Tuple			):
					'''simple docstring'''





					def check_hidden_states_output(a_							:			Dict	,	a_							:			List[Any]	,	a_							:			Any			):
							__UpperCAmelCase   :					Union[str, Any]													=     model_class(a_			)
							model.to(a_			)
							model.eval()

							with torch.no_grad():
									__UpperCAmelCase   :					Tuple													=     model(**self._prepare_for_class(a_	,	a_			)			)

							__UpperCAmelCase   :					Optional[int]													=     outputs.hidden_states
							__UpperCAmelCase   :					str													=     self.model_tester.num_hidden_layers + 1
							self.assertEqual(len(a_			)	,	a_			)

							__UpperCAmelCase   :					Any													=     self.model_tester.seq_length

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

					__UpperCAmelCase							, __UpperCAmelCase   :					Optional[int]													=     self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							__UpperCAmelCase   :					Optional[Any]													=     True
							check_hidden_states_output(a_	,	a_	,	a_			)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							__UpperCAmelCase   :					int													=     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 			snake_case__					(       self							:			Optional[Any]			):
					'''simple docstring'''





					pass

def 	a						(				):







		'''simple docstring'''





		__UpperCAmelCase   :					Optional[int]													=     hf_hub_download(
		    repo_id='''hf-internal-testing/spaghetti-video'''			,       filename='''eating_spaghetti_32_frames.npy'''			,       repo_type='''dataset'''						)
		__UpperCAmelCase   :					str													=     np.load(_UpperCAmelCase						)
		return list(_UpperCAmelCase						)

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




			'''simple docstring'''






			@cached_property
			def 			snake_case__					(       self							:			Union[str, Any]			):
					'''simple docstring'''





					return VivitImageProcessor() if is_vision_available() else None




			@slow
			def 			snake_case__					(       self							:			List[str]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Union[str, Any]													=     VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''			).to(a_			)

					__UpperCAmelCase   :					Union[str, Any]													=     self.default_image_processor
					__UpperCAmelCase   :					Dict													=     prepare_video()
					__UpperCAmelCase   :					int													=     image_processor(a_	,	return_tensors='''pt'''			).to(a_			)

					# forward pass
					with torch.no_grad():
							__UpperCAmelCase   :					Tuple													=     model(**a_			)

					# verify the logits
					__UpperCAmelCase   :					Optional[Any]													=     torch.Size((1, 4_00)			)
					self.assertEqual(outputs.logits.shape	,	a_			)

					# taken from original model
					__UpperCAmelCase   :					Optional[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(a_			)

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










import os
from shutil import copyfile
from typing import Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
				from .tokenization_reformer import ReformerTokenizer
else:
				__A             =None


__A             =logging.get_logger(__name__)


__A             ="▁"

__A             ={"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

__A             ={
    "vocab_file": {
        "google/reformer-crime-and-punishment": (
            "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model"
        )
    },
    "tokenizer_file": {
        "google/reformer-crime-and-punishment": (
            "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/tokenizer.json"
        )
    },
}

__A             ={
    "google/reformer-crime-and-punishment": 5_2_4_2_8_8,
}

class 		UpperCAmelCase__      (      __UpperCamelCase       ):




			'''simple docstring'''






			UpperCamelCase      						=	VOCAB_FILES_NAMES
			UpperCamelCase      						=	PRETRAINED_VOCAB_FILES_MAP
			UpperCamelCase      						=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			UpperCamelCase      						=	["""input_ids""", """attention_mask"""]
			UpperCamelCase      						=	ReformerTokenizer




			def __init__(       self							:			Tuple	,	a_							:			Optional[int]=None	,	a_							:			int=None	,	a_							:			Dict="</s>"	,	a_							:			Dict="<unk>"	,	a_							:			List[Any]=[]	,	**a_							:			int	,	):
					'''simple docstring'''





					super().__init__(
					    a_	,	tokenizer_file=a_	,	eos_token=a_	,	unk_token=a_	,	additional_special_tokens=a_	,	**a_	,	)

					__UpperCAmelCase   :					Optional[Any]													=     vocab_file
					__UpperCAmelCase   :					Optional[Any]													=     False if not self.vocab_file else True




			def 			snake_case__					(       self							:			Dict	,	a_							:			str	,	a_							:			Optional[str] = None			):
					'''simple docstring'''





					if not self.can_save_slow_tokenizer:
							raise ValueError(
							    '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
							    '''tokenizer.'''			)

					if not os.path.isdir(a_			):
							logger.error(F'Vocabulary path ({save_directory}) should be a directory'			)
							return
					__UpperCAmelCase   :					Tuple													=     os.path.join(
					    a_	,	(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']			)

					if os.path.abspath(self.vocab_file			) != os.path.abspath(a_			):
							copyfile(self.vocab_file	,	a_			)

					return (out_vocab_file,)





