
'''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      _lowerCAmelCase					:







							'''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.02						,						UpperCAmelCase=1e-0_6						,						UpperCAmelCase=True						,						UpperCAmelCase=None						,						)  ->     Union[str, Any]:
														_snake_case     =						parent
														_snake_case     =						batch_size
														_snake_case     =						is_training
														_snake_case     =						use_labels
														_snake_case     =						num_labels
														_snake_case     =						image_size
														_snake_case     =						num_frames
														_snake_case     =						tubelet_size
														_snake_case     =						num_channels
														_snake_case     =						hidden_size
														_snake_case     =						num_hidden_layers
														_snake_case     =						num_attention_heads
														_snake_case     =						intermediate_size
														_snake_case     =						hidden_act
														_snake_case     =						hidden_dropout_prob
														_snake_case     =						attention_probs_dropout_prob
														_snake_case     =						initializer_range
														_snake_case     =						layer_norm_eps
														_snake_case     =						qkv_bias
														_snake_case     =						scope

														_snake_case     =						(
														    (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  lowercase							(self						)  ->     Any:
														_snake_case     =						floats_tensor(
														    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]						)

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

														_snake_case     =						self.get_config()

														return config, pixel_values, labels

							def  lowercase							(self						)  ->     Union[str, Any]:
														_snake_case     =						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						,						)
														_snake_case     =						self.num_labels
														return config

							def  lowercase							(self						,						UpperCAmelCase						,						UpperCAmelCase						,						UpperCAmelCase						)  ->     List[Any]:
														_snake_case     =						VivitModel(config=UpperCAmelCase						)
														model.to(UpperCAmelCase						)
														model.eval()
														_snake_case     =						model(UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape						,						(self.batch_size, self.seq_length, self.hidden_size)						)

							def  lowercase							(self						,						UpperCAmelCase						,						UpperCAmelCase						,						UpperCAmelCase						)  ->     Tuple:
														_snake_case     =						VivitForVideoClassification(UpperCAmelCase						)
														model.to(UpperCAmelCase						)
														model.eval()

														_snake_case     =						model(UpperCAmelCase						)

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





							def  lowercase							(self						)  ->     Tuple:
														_snake_case     =						self.prepare_config_and_inputs()
														_snake_case,							_snake_case,							_snake_case     =						config_and_inputs
														_snake_case     =						{"""pixel_values""": pixel_values}
														return config, inputs_dict


@require_torch
class      _lowerCAmelCase					(					__snake_case  ,							unittest.TestCase   ):







							'''simple docstring'''

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

							lowerCAmelCase_      		=		False
							lowerCAmelCase_      		=		False
							lowerCAmelCase_      		=		False
							lowerCAmelCase_      		=		False

							def  lowercase							(self						)  ->     Optional[Any]:
														_snake_case     =						VivitModelTester(self						)
														_snake_case     =						ConfigTester(self						,						config_class=UpperCAmelCase						,						has_text_modality=UpperCAmelCase						,						hidden_size=37						)

							def  lowercase							(self						,						UpperCAmelCase						,						UpperCAmelCase						,						UpperCAmelCase=False						)  ->     Optional[Any]:
														_snake_case     =						copy.deepcopy(UpperCAmelCase						)

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

														return inputs_dict

							def  lowercase							(self						)  ->     List[str]:
														self.config_tester.run_common_tests()

							@unittest.skip(reason="""Vivit does not use inputs_embeds"""						)
							def  lowercase							(self						)  ->     List[str]:
														pass

							def  lowercase							(self						)  ->     Optional[Any]:
														_snake_case,							_snake_case     =						self.model_tester.prepare_config_and_inputs_for_common()

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

							def  lowercase							(self						)  ->     List[str]:
														_snake_case,							_snake_case     =						self.model_tester.prepare_config_and_inputs_for_common()

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

																					_snake_case     =						["""pixel_values""", """head_mask"""]
																					self.assertListEqual(arg_names[:2]						,						UpperCAmelCase						)

							def  lowercase							(self						)  ->     Optional[Any]:
														_snake_case     =						self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*UpperCAmelCase						)

							def  lowercase							(self						)  ->     List[str]:
														_snake_case     =						self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_video_classification(*UpperCAmelCase						)

							@slow
							def  lowercase							(self						)  ->     Tuple:
														for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_snake_case     =						VivitModel.from_pretrained(UpperCAmelCase						)
																					self.assertIsNotNone(UpperCAmelCase						)

							def  lowercase							(self						)  ->     Optional[int]:
														_snake_case,							_snake_case     =						self.model_tester.prepare_config_and_inputs_for_common()
														_snake_case     =						True

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

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

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

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

																					_snake_case     =						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  lowercase							(self						)  ->     Union[str, Any]:
														def check_hidden_states_output(UpperCAmelCase						,						UpperCAmelCase						,						UpperCAmelCase						):
																					_snake_case     =						model_class(UpperCAmelCase						)
																					model.to(UpperCAmelCase						)
																					model.eval()

																					with torch.no_grad():
																												_snake_case     =						model(**self._prepare_for_class(UpperCAmelCase						,						UpperCAmelCase						)						)

																					_snake_case     =						outputs.hidden_states
																					_snake_case     =						self.model_tester.num_hidden_layers + 1
																					self.assertEqual(len(UpperCAmelCase						)						,						UpperCAmelCase						)

																					_snake_case     =						self.model_tester.seq_length

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

														_snake_case,							_snake_case     =						self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					_snake_case     =						True
																					check_hidden_states_output(UpperCAmelCase						,						UpperCAmelCase						,						UpperCAmelCase						)

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






def       __SCREAMING_SNAKE_CASE   (						):
							_snake_case     =						hf_hub_download(
							    repo_id="""hf-internal-testing/spaghetti-video"""							,      filename="""eating_spaghetti_32_frames.npy"""							,      repo_type="""dataset"""   )
							_snake_case     =						np.load(_SCREAMING_SNAKE_CASE   )
							return list(_SCREAMING_SNAKE_CASE   )


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







							'''simple docstring'''

							@cached_property
							def  lowercase							(self						)  ->     Optional[Any]:
														return VivitImageProcessor() if is_vision_available() else None





							@slow
							def  lowercase							(self						)  ->     Optional[int]:
														_snake_case     =						VivitForVideoClassification.from_pretrained("""google/vivit-b-16x2-kinetics400"""						).to(UpperCAmelCase						)

														_snake_case     =						self.default_image_processor
														_snake_case     =						prepare_video()
														_snake_case     =						image_processor(UpperCAmelCase						,						return_tensors="""pt"""						).to(UpperCAmelCase						)

														# forward pass
														with torch.no_grad():
																					_snake_case     =						model(**UpperCAmelCase						)

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

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

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

'''simple docstring'''
import os
from shutil import copyfile
from typing import Optional, Tuple

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


if is_sentencepiece_available():
				from .tokenization_deberta_va import DebertaVaTokenizer
else:
				__lowerCAmelCase  							= None

__lowerCAmelCase  							= logging.get_logger(__name__)

__lowerCAmelCase  							= {'vocab_file': 'spm.model', 'tokenizer_file': 'tokenizer.json'}

__lowerCAmelCase  							= {
    'vocab_file': {
        'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/spm.model',
        'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/spm.model',
        'microsoft/deberta-v2-xlarge-mnli': (
            'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/spm.model'
        ),
        'microsoft/deberta-v2-xxlarge-mnli': (
            'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/spm.model'
        ),
    }
}

__lowerCAmelCase  							= {
    'microsoft/deberta-v2-xlarge': 512,
    'microsoft/deberta-v2-xxlarge': 512,
    'microsoft/deberta-v2-xlarge-mnli': 512,
    'microsoft/deberta-v2-xxlarge-mnli': 512,
}

__lowerCAmelCase  							= {
    'microsoft/deberta-v2-xlarge': {'do_lower_case': False},
    'microsoft/deberta-v2-xxlarge': {'do_lower_case': False},
    'microsoft/deberta-v2-xlarge-mnli': {'do_lower_case': False},
    'microsoft/deberta-v2-xxlarge-mnli': {'do_lower_case': False},
}


class     _lowerCAmelCase					(					__snake_case   ):







							'''simple docstring'''

							lowerCAmelCase_      		=		VOCAB_FILES_NAMES
							lowerCAmelCase_      		=		PRETRAINED_VOCAB_FILES_MAP
							lowerCAmelCase_      		=		PRETRAINED_INIT_CONFIGURATION
							lowerCAmelCase_      		=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							lowerCAmelCase_      		=		DebertaVaTokenizer

							def __init__(self						,						UpperCAmelCase=None						,						UpperCAmelCase=None						,						UpperCAmelCase=False						,						UpperCAmelCase=False						,						UpperCAmelCase="[CLS]"						,						UpperCAmelCase="[SEP]"						,						UpperCAmelCase="[UNK]"						,						UpperCAmelCase="[SEP]"						,						UpperCAmelCase="[PAD]"						,						UpperCAmelCase="[CLS]"						,						UpperCAmelCase="[MASK]"						,						**UpperCAmelCase						,						)  ->     None:
														super().__init__(
														    UpperCAmelCase						,						tokenizer_file=UpperCAmelCase						,						do_lower_case=UpperCAmelCase						,						bos_token=UpperCAmelCase						,						eos_token=UpperCAmelCase						,						unk_token=UpperCAmelCase						,						sep_token=UpperCAmelCase						,						pad_token=UpperCAmelCase						,						cls_token=UpperCAmelCase						,						mask_token=UpperCAmelCase						,						split_by_punct=UpperCAmelCase						,						**UpperCAmelCase						,						)

														_snake_case     =						do_lower_case
														_snake_case     =						split_by_punct
														_snake_case     =						vocab_file
														_snake_case     =						False if not self.vocab_file else True

							def  lowercase							(self						,						UpperCAmelCase						,						UpperCAmelCase=None						)  ->     List[Any]:

														if token_ids_a is None:
																					return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
														_snake_case     =						[self.cls_token_id]
														_snake_case     =						[self.sep_token_id]
														return cls + token_ids_a + sep + token_ids_a + sep

							def  lowercase							(self						,						UpperCAmelCase						,						UpperCAmelCase=None						,						UpperCAmelCase=False						)  ->     Union[str, Any]:

														if already_has_special_tokens:
																					return super().get_special_tokens_mask(
																					    token_ids_a=UpperCAmelCase						,						token_ids_a=UpperCAmelCase						,						already_has_special_tokens=UpperCAmelCase						)

														if token_ids_a is not None:
																					return [1] + ([0] * len(UpperCAmelCase						)) + [1] + ([0] * len(UpperCAmelCase						)) + [1]
														return [1] + ([0] * len(UpperCAmelCase						)) + [1]

							def  lowercase							(self						,						UpperCAmelCase						,						UpperCAmelCase=None						)  ->     str:
														_snake_case     =						[self.sep_token_id]
														_snake_case     =						[self.cls_token_id]
														if token_ids_a is None:
																					return len(cls + token_ids_a + sep						) * [0]
														return len(cls + token_ids_a + sep						) * [0] + len(token_ids_a + sep						) * [1]





							def  lowercase							(self						,						UpperCAmelCase						,						UpperCAmelCase = None						)  ->     Tuple[str]:
														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(UpperCAmelCase						):
																					logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""						)
																					return
														_snake_case     =						os.path.join(
														    UpperCAmelCase						,						(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]						)

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

														return (out_vocab_file,)