


import json
import os
import re
import warnings
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


A :       Any						=   logging.get_logger(__name__)

A :       int						=   {
    "source_spm": "source.spm",
    "target_spm": "target.spm",
    "vocab": "vocab.json",
    "target_vocab_file": "target_vocab.json",
    "tokenizer_config_file": "tokenizer_config.json",
}

A :       Dict						=   {
    "source_spm": {
        "Helsinki-NLP/opus-mt-en-de": "https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/source.spm"
    },
    "target_spm": {
        "Helsinki-NLP/opus-mt-en-de": "https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/target.spm"
    },
    "vocab": {
        "Helsinki-NLP/opus-mt-en-de": "https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/vocab.json"
    },
    "tokenizer_config_file": {
        "Helsinki-NLP/opus-mt-en-de": (
            "https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/tokenizer_config.json"
        )
    },
}

A :       Tuple						=   {"Helsinki-NLP/opus-mt-en-de": 512}
A :       Any						=   {}







class      _lowercase					(				lowercase__):


       """simple docstring"""




       A__   		=     VOCAB_FILES_NAMES
       A__   		=     PRETRAINED_VOCAB_FILES_MAP
       A__   		=     PRETRAINED_INIT_CONFIGURATION
       A__   		=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       A__   		=     ["input_ids", "attention_mask"]
       A__   		=     re.compile(">>.+<<")  # type: re.Pattern





       def __init__(  self				:     Dict   , __lowerCamelCase				:     str   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     Tuple=None   , __lowerCamelCase				:     Optional[Any]=None   , __lowerCamelCase				:     Optional[Any]=None   , __lowerCamelCase				:     Tuple="<unk>"   , __lowerCamelCase				:     Dict="</s>"   , __lowerCamelCase				:     Optional[Any]="<pad>"   , __lowerCamelCase				:     List[Any]=512   , __lowerCamelCase				:     Optional[Dict[str, Any]] = None   , __lowerCamelCase				:     Union[str, Any]=False   , **__lowerCamelCase				:     Tuple   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    {} if sp_model_kwargs is None else sp_model_kwargs

             super().__init__(
                 # bos_token=bos_token,  unused. Start decoding with config.decoder_start_token_id
                 source_lang=__lowerCamelCase   , target_lang=__lowerCamelCase   , unk_token=__lowerCamelCase   , eos_token=__lowerCamelCase   , pad_token=__lowerCamelCase   , model_max_length=__lowerCamelCase   , sp_model_kwargs=self.sp_model_kwargs   , target_vocab_file=__lowerCamelCase   , separate_vocabs=__lowerCamelCase   , **__lowerCamelCase   , )
             assert Path(__lowerCamelCase			).exists(), f"cannot find spm source {source_spm}"

             lowerCamelCase__						:							int       =    separate_vocabs
             lowerCamelCase__						:							List[str]       =    load_json(__lowerCamelCase			)
             if self.unk_token not in self.encoder:
                   raise KeyError("<unk> token must be in vocab"			)
             assert self.pad_token in self.encoder

             if separate_vocabs:
                   lowerCamelCase__						:							Tuple       =    load_json(__lowerCamelCase			)
                   lowerCamelCase__						:							Any       =    {v: k for k, v in self.target_encoder.items()}
                   lowerCamelCase__						:							str       =    []
             else:
                   lowerCamelCase__						:							str       =    {v: k for k, v in self.encoder.items()}
                   lowerCamelCase__						:							list       =    [k for k in self.encoder if k.startswith(">>"			) and k.endswith("<<"			)]

             lowerCamelCase__						:							int       =    source_lang
             lowerCamelCase__						:							Tuple       =    target_lang
             lowerCamelCase__						:							List[Any]       =    [source_spm, target_spm]

             # load SentencePiece model for pre-processing
             lowerCamelCase__						:							Dict       =    load_spm(__lowerCamelCase   , self.sp_model_kwargs			)
             lowerCamelCase__						:							Tuple       =    load_spm(__lowerCamelCase   , self.sp_model_kwargs			)
             lowerCamelCase__						:							List[str]       =    self.spm_source
             lowerCamelCase__						:							Dict       =    self.encoder

             # Multilingual target side: default to using first supported language code.

             self._setup_normalizer()





       def 		lowerCAmelCase	(  self				:     Union[str, Any]			):


             '''simple docstring'''



             try:
                   from sacremoses import MosesPunctNormalizer

                   lowerCamelCase__						:							str       =    MosesPunctNormalizer(self.source_lang			).normalize
             except (ImportError, FileNotFoundError):
                   warnings.warn("Recommended: pip install sacremoses."			)
                   lowerCamelCase__						:							Any       =    lambda __lowerCamelCase			: x





       def 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     str			):


             '''simple docstring'''



             return self.punc_normalizer(__lowerCamelCase			) if x else ""





       def 		lowerCAmelCase	(  self				:     Dict   , __lowerCamelCase				:     Optional[Any]			):


             '''simple docstring'''



             return self.current_encoder.get(__lowerCamelCase   , self.current_encoder[self.unk_token]			)





       def 		lowerCAmelCase	(  self				:     List[str]   , __lowerCamelCase				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    self.language_code_re.match(__lowerCamelCase			)
             lowerCamelCase__						:							list       =    [match.group(0			)] if match else []
             return code, self.language_code_re.sub(""   , __lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[int]   , __lowerCamelCase				:     str			):


             '''simple docstring'''



             lowerCamelCase__       ,  lowerCamelCase__						:							Optional[int]       =    self.remove_language_code(__lowerCamelCase			)
             lowerCamelCase__						:							List[Any]       =    self.current_spm.encode(__lowerCamelCase   , out_type=__lowerCamelCase			)
             return code + pieces





       def 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     int			):


             '''simple docstring'''



             return self.decoder.get(__lowerCamelCase   , self.unk_token			)





       def 		lowerCAmelCase	(  self				:     Union[str, Any]   , __lowerCamelCase				:     Optional[Any]   , **__lowerCamelCase				:     List[Any]			):


             '''simple docstring'''



             return super().batch_decode(__lowerCamelCase   , **__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Dict   , __lowerCamelCase				:     Tuple   , **__lowerCamelCase				:     List[Any]			):


             '''simple docstring'''



             return super().decode(__lowerCamelCase   , **__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[Any]   , __lowerCamelCase				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							str       =    self.spm_source if self._decode_use_source_tokenizer else self.spm_target
             lowerCamelCase__						:							str       =    []
             lowerCamelCase__						:							List[Any]       =    ""
             for token in tokens:
                   # make sure that special tokens are not decoded using sentencepiece model
                   if token in self.all_special_tokens:
                         out_string += sp_model.decode_pieces(__lowerCamelCase			) + token + " "
                         lowerCamelCase__						:							str       =    []
                   else:
                         current_sub_tokens.append(__lowerCamelCase			)
             out_string += sp_model.decode_pieces(__lowerCamelCase			)
             return out_string.strip()





       def 		lowerCAmelCase	(  self				:     str   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     Union[str, Any]=None			):


             '''simple docstring'''



             if token_ids_a is None:
                   return token_ids_a + [self.eos_token_id]
             # We don't expect to process pairs, but leave the pair logic for API consistency
             return token_ids_a + token_ids_a + [self.eos_token_id]





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    self.spm_source
             lowerCamelCase__						:							Optional[int]       =    self.encoder





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.spm_target
             if self.separate_vocabs:
                   lowerCamelCase__						:							Optional[Any]       =    self.target_encoder





       @property
       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             return len(self.encoder			)





       def 		lowerCAmelCase	(  self				:     Tuple   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Optional[str] = None			):


             '''simple docstring'''



             if not os.path.isdir(__lowerCamelCase			):
                   logger.error(f"Vocabulary path ({save_directory}) should be a directory"			)
                   return
             lowerCamelCase__						:							Optional[int]       =    []

             if self.separate_vocabs:
                   lowerCamelCase__						:							List[Any]       =    os.path.join(
                       __lowerCamelCase   , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab"]   , )
                   lowerCamelCase__						:							List[str]       =    os.path.join(
                       __lowerCamelCase   , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["target_vocab_file"]   , )
                   save_json(self.encoder   , __lowerCamelCase			)
                   save_json(self.target_encoder   , __lowerCamelCase			)
                   saved_files.append(__lowerCamelCase			)
                   saved_files.append(__lowerCamelCase			)
             else:
                   lowerCamelCase__						:							str       =    os.path.join(
                       __lowerCamelCase   , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab"]			)
                   save_json(self.encoder   , __lowerCamelCase			)
                   saved_files.append(__lowerCamelCase			)

             for spm_save_filename, spm_orig_path, spm_model in zip(
                 [VOCAB_FILES_NAMES["source_spm"], VOCAB_FILES_NAMES["target_spm"]]   , self.spm_files   , [self.spm_source, self.spm_target]   , ):
                   lowerCamelCase__						:							str       =    os.path.join(
                       __lowerCamelCase   , (filename_prefix + "-" if filename_prefix else "") + spm_save_filename			)
                   if os.path.abspath(__lowerCamelCase			) != os.path.abspath(__lowerCamelCase			) and os.path.isfile(__lowerCamelCase			):
                         copyfile(__lowerCamelCase   , __lowerCamelCase			)
                         saved_files.append(__lowerCamelCase			)
                   elif not os.path.isfile(__lowerCamelCase			):
                         with open(__lowerCamelCase   , "wb"			) as fi:
                               lowerCamelCase__						:							Optional[int]       =    spm_model.serialized_model_proto()
                               fi.write(__lowerCamelCase			)
                         saved_files.append(__lowerCamelCase			)

             return tuple(__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             return self.get_src_vocab()





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             return dict(self.encoder   , **self.added_tokens_encoder			)





       def 		lowerCAmelCase	(  self				:     Any			):


             '''simple docstring'''



             return dict(self.target_encoder   , **self.added_tokens_decoder			)





       def __getstate__(  self				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    self.__dict__.copy()
             state.update(
                 {k: None for k in ["spm_source", "spm_target", "current_spm", "punc_normalizer", "target_vocab_file"]}			)
             return state





       def __setstate__(  self				:     Tuple   , __lowerCamelCase				:     Dict			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    d

             # for backward compatibility
             if not hasattr(self   , "sp_model_kwargs"			):
                   lowerCamelCase__						:							int       =    {}

             lowerCamelCase__       ,  lowerCamelCase__						:							Any       =    (load_spm(__lowerCamelCase   , self.sp_model_kwargs			) for f in self.spm_files)
             lowerCamelCase__						:							Any       =    self.spm_source
             self._setup_normalizer()





       def 		lowerCAmelCase	(  self				:     Dict   , *__lowerCamelCase				:     List[str]   , **__lowerCamelCase				:     List[Any]			):


             '''simple docstring'''



             return 1





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							Tuple       =    set(self.all_special_ids			)  # call it once instead of inside list comp
             all_special_ids.remove(self.unk_token_id			)  # <unk> is only sometimes special
             return [1 if x in all_special_ids else 0 for x in seq]





       def 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     List   , __lowerCamelCase				:     Optional[List] = None   , __lowerCamelCase				:     bool = False			):


             '''simple docstring'''



             if already_has_special_tokens:
                   return self._special_token_mask(__lowerCamelCase			)
             elif token_ids_a is None:
                   return self._special_token_mask(__lowerCamelCase			) + [1]
             else:
                   return self._special_token_mask(token_ids_a + token_ids_a			) + [1]



def  lowercase_						(						_A     :       str       ,   _A     :       Dict[str, Any]		):

      """simple docstring"""




      lowerCamelCase__						:							Union[str, Any]       =    sentencepiece.SentencePieceProcessor(**_A		)
      spm.Load(_A		)
      return spm

def  lowercase_						(						_A     :       Any       ,   _A     :       str		):

      """simple docstring"""




      with open(_A       ,   "w"		) as f:
            json.dump(_A       ,   _A       ,   indent=2		)


def  lowercase_						(						_A     :       str		):

      """simple docstring"""




      with open(_A       ,   "r"		) as f:
            return json.load(_A		)






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


       """simple docstring"""




       def __init__(  self				:     Union[str, Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     int=2   , __lowerCamelCase				:     Any=True   , __lowerCamelCase				:     Union[str, Any]=True   , __lowerCamelCase				:     int=10   , __lowerCamelCase				:     str=10   , __lowerCamelCase				:     int=8   , __lowerCamelCase				:     Optional[int]=[2, 4, 4]   , __lowerCamelCase				:     List[Any]=3   , __lowerCamelCase				:     str=768   , __lowerCamelCase				:     int=5   , __lowerCamelCase				:     Any=4   , __lowerCamelCase				:     int=37   , __lowerCamelCase				:     int="gelu_fast"   , __lowerCamelCase				:     int=0.0   , __lowerCamelCase				:     Optional[int]=0.0   , __lowerCamelCase				:     Union[str, Any]=0.0_2   , __lowerCamelCase				:     Any=1E-0_6   , __lowerCamelCase				:     Optional[int]=True   , __lowerCamelCase				:     Tuple=None   , ):


             '''simple docstring'''



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

             lowerCamelCase__						:							Union[str, Any]       =    (
                 (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 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    floats_tensor(
                 [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]			)

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

             lowerCamelCase__						:							Any       =    self.get_config()

             return config, pixel_values, labels





       def 		lowerCAmelCase	(  self				:     Union[str, Any]			):


             '''simple docstring'''



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





       def 		lowerCAmelCase	(  self				:     Optional[int]   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Union[str, Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Tuple       =    VivitModel(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Any       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size)			)





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     Optional[int]			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    VivitForVideoClassification(__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()

             lowerCamelCase__						:							List[str]       =    model(__lowerCamelCase			)

             # verify the logits shape
             lowerCamelCase__						:							Optional[int]       =    torch.Size((self.batch_size, self.num_labels)			)
             self.parent.assertEqual(result.logits.shape   , __lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



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







@require_torch
class      _lowercase					(				lowercase__				,    unittest.TestCase):


       """simple docstring"""




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

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





       def 		lowerCAmelCase	(  self				:     Optional[int]			):


             '''simple docstring'''



             lowerCamelCase__						:							Any       =    VivitModelTester(self			)
             lowerCamelCase__						:							Any       =    ConfigTester(self   , config_class=__lowerCamelCase   , has_text_modality=__lowerCamelCase   , hidden_size=37			)





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     Union[str, Any]=False			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    copy.deepcopy(__lowerCamelCase			)

             if return_labels:
                   if model_class in get_values(__lowerCamelCase			):
                         lowerCamelCase__						:							List[Any]       =    torch.zeros(
                             self.model_tester.batch_size   , dtype=torch.long   , device=__lowerCamelCase			)

             return inputs_dict





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             self.config_tester.run_common_tests()





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


             '''simple docstring'''



             pass





       def 		lowerCAmelCase	(  self				:     Union[str, Any]			):


             '''simple docstring'''



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

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





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__       ,  lowerCamelCase__						:							str       =    self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   lowerCamelCase__						:							Tuple       =    model_class(__lowerCamelCase			)
                   lowerCamelCase__						:							List[Any]       =    inspect.signature(model.forward			)
                   # signature.parameters is an OrderedDict => so arg_names order is deterministic
                   lowerCamelCase__						:							int       =    [*signature.parameters.keys()]

                   lowerCamelCase__						:							Any       =    ["pixel_values", "head_mask"]
                   self.assertListEqual(arg_names[:2]   , __lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Dict			):


             '''simple docstring'''



             lowerCamelCase__						:							str       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_video_classification(*__lowerCamelCase			)





       @slow
       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   lowerCamelCase__						:							str       =    VivitModel.from_pretrained(__lowerCamelCase			)
                   self.assertIsNotNone(__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__       ,  lowerCamelCase__						:							Dict       =    self.model_tester.prepare_config_and_inputs_for_common()
             lowerCamelCase__						:							List[str]       =    True

             for model_class in self.all_model_classes:
                   lowerCamelCase__						:							Any       =    self.model_tester.seq_length

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

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

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

                   lowerCamelCase__						:							Any       =    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 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             def check_hidden_states_output(__lowerCamelCase				:     str   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Tuple			):
                   lowerCamelCase__						:							Union[str, Any]       =    model_class(__lowerCamelCase			)
                   model.to(__lowerCamelCase			)
                   model.eval()

                   with torch.no_grad():
                         lowerCamelCase__						:							Optional[Any]       =    model(**self._prepare_for_class(__lowerCamelCase   , __lowerCamelCase			)			)

                   lowerCamelCase__						:							List[Any]       =    outputs.hidden_states
                   lowerCamelCase__						:							Optional[int]       =    self.model_tester.num_hidden_layers + 1
                   self.assertEqual(len(__lowerCamelCase			)   , __lowerCamelCase			)

                   lowerCamelCase__						:							Optional[int]       =    self.model_tester.seq_length

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

             lowerCamelCase__       ,  lowerCamelCase__						:							Optional[Any]       =    self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   lowerCamelCase__						:							Tuple       =    True
                   check_hidden_states_output(__lowerCamelCase   , __lowerCamelCase   , __lowerCamelCase			)

                   # check that output_hidden_states also work using config
                   del inputs_dict["output_hidden_states"]
                   lowerCamelCase__						:							int       =    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 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             pass



def  lowercase_						(						):

      """simple docstring"""




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







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


       """simple docstring"""




       @cached_property
       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             return VivitImageProcessor() if is_vision_available() else None





       @slow
       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"			).to(__lowerCamelCase			)

             lowerCamelCase__						:							Tuple       =    self.default_image_processor
             lowerCamelCase__						:							List[str]       =    prepare_video()
             lowerCamelCase__						:							Optional[int]       =    image_processor(__lowerCamelCase   , return_tensors="pt"			).to(__lowerCamelCase			)

             # forward pass
             with torch.no_grad():
                   lowerCamelCase__						:							Optional[int]       =    model(**__lowerCamelCase			)

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

             # taken from original model
             lowerCamelCase__						:							str       =    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(__lowerCamelCase			)

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


