






'''simple docstring'''
import json
import os
from functools import lru_cache
from typing import List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


a_					= logging.get_logger(__name__)


a_					= {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'}

a_					= {
    'vocab_file': {
        'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json',
        'allenai/longformer-large-4096': (
            'https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json'
        ),
        'allenai/longformer-large-4096-finetuned-triviaqa': (
            'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json'
        ),
        'allenai/longformer-base-4096-extra.pos.embd.only': (
            'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json'
        ),
        'allenai/longformer-large-4096-extra.pos.embd.only': (
            'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json'
        ),
    },
    'merges_file': {
        'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt',
        'allenai/longformer-large-4096': (
            'https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt'
        ),
        'allenai/longformer-large-4096-finetuned-triviaqa': (
            'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt'
        ),
        'allenai/longformer-base-4096-extra.pos.embd.only': (
            'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt'
        ),
        'allenai/longformer-large-4096-extra.pos.embd.only': (
            'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt'
        ),
    },
}

a_					= {
    'allenai/longformer-base-4096': 4_0_9_6,
    'allenai/longformer-large-4096': 4_0_9_6,
    'allenai/longformer-large-4096-finetuned-triviaqa': 4_0_9_6,
    'allenai/longformer-base-4096-extra.pos.embd.only': 4_0_9_6,
    'allenai/longformer-large-4096-extra.pos.embd.only': 4_0_9_6,
}





@lru_cache()
# Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode
def  _a(  )     ->   Optional[Any]:




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__     :				str									=(
          list(range(ord('''!'''							),	ord('''~'''							) + 1							)							) + list(range(ord('''¡'''							),	ord('''¬'''							) + 1							)							) + list(range(ord('''®'''							),	ord('''ÿ'''							) + 1							)							)
      )
      SCREAMING_SNAKE_CASE__     :				List[str]									=bs[:]
      SCREAMING_SNAKE_CASE__     :				Any									=0
      for b in range(2**8							):
            if b not in bs:
                  bs.append(UpperCamelCase__							)
                  cs.append(2**8 + n							)
                  n += 1
      SCREAMING_SNAKE_CASE__     :				int									=[chr(UpperCamelCase__							) for n in cs]
      return dict(zip(UpperCamelCase__,	UpperCamelCase__							)							)





def  _a(  UpperCamelCase__				:						Optional[Any]							)     ->   int:




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__     :				Any									=set()
      SCREAMING_SNAKE_CASE__     :				Optional[Any]									=word[0]
      for char in word[1:]:
            pairs.add((prev_char, char)							)
            SCREAMING_SNAKE_CASE__     :				int									=char
      return pairs







class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):
 snake_case_  							=							VOCAB_FILES_NAMES
 snake_case_  							=							PRETRAINED_VOCAB_FILES_MAP
 snake_case_  							=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 snake_case_  							=							["""input_ids""", """attention_mask"""]




 def __init__(      self	:							Optional[Any]					,      __lowercase	:							Union[str, Any]					,      __lowercase	:							List[Any]					,      __lowercase	:							List[Any]="replace"					,      __lowercase	:							Tuple="<s>"					,      __lowercase	:							List[Any]="</s>"					,      __lowercase	:							Any="</s>"					,      __lowercase	:							Dict="<s>"					,      __lowercase	:							Dict="<unk>"					,      __lowercase	:							Optional[Any]="<pad>"					,      __lowercase	:							Optional[int]="<mask>"					,      __lowercase	:							Union[str, Any]=False					,      **__lowercase	:							str					,      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=AddedToken(__lowercase					,      lstrip=__lowercase					,      rstrip=__lowercase      ) if isinstance(__lowercase					,      __lowercase      ) else bos_token
       SCREAMING_SNAKE_CASE__     :				List[Any]									=AddedToken(__lowercase					,      lstrip=__lowercase					,      rstrip=__lowercase      ) if isinstance(__lowercase					,      __lowercase      ) else eos_token
       SCREAMING_SNAKE_CASE__     :				int									=AddedToken(__lowercase					,      lstrip=__lowercase					,      rstrip=__lowercase      ) if isinstance(__lowercase					,      __lowercase      ) else sep_token
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=AddedToken(__lowercase					,      lstrip=__lowercase					,      rstrip=__lowercase      ) if isinstance(__lowercase					,      __lowercase      ) else cls_token
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=AddedToken(__lowercase					,      lstrip=__lowercase					,      rstrip=__lowercase      ) if isinstance(__lowercase					,      __lowercase      ) else unk_token
       SCREAMING_SNAKE_CASE__     :				List[Any]									=AddedToken(__lowercase					,      lstrip=__lowercase					,      rstrip=__lowercase      ) if isinstance(__lowercase					,      __lowercase      ) else pad_token

       # Mask token behave like a normal word, i.e. include the space before it
       SCREAMING_SNAKE_CASE__     :				Dict									=AddedToken(__lowercase					,      lstrip=__lowercase					,      rstrip=__lowercase      ) if isinstance(__lowercase					,      __lowercase      ) else mask_token

       super().__init__(
           errors=__lowercase					,      bos_token=__lowercase					,      eos_token=__lowercase					,      unk_token=__lowercase					,      sep_token=__lowercase					,      cls_token=__lowercase					,      pad_token=__lowercase					,      mask_token=__lowercase					,      add_prefix_space=__lowercase					,      **__lowercase					,      )

       with open(__lowercase					,      encoding='''utf-8'''      ) as vocab_handle:
             SCREAMING_SNAKE_CASE__     :				str									=json.load(__lowercase      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									={v: k for k, v in self.encoder.items()}
       SCREAMING_SNAKE_CASE__     :				Dict									=errors  # how to handle errors in decoding
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=bytes_to_unicode()
       SCREAMING_SNAKE_CASE__     :				List[str]									={v: k for k, v in self.byte_encoder.items()}
       with open(__lowercase					,      encoding='''utf-8'''      ) as merges_handle:
             SCREAMING_SNAKE_CASE__     :				int									=merges_handle.read().split('''\n'''      )[1:-1]
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=[tuple(merge.split()      ) for merge in bpe_merges]
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=dict(zip(__lowercase					,      range(len(__lowercase      )      )      )      )
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									={}
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=add_prefix_space

       # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=re.compile(r'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'''      )




 @property
 def      __magic_name__	(      self	:							Optional[Any]      )	->						List[Any]:
       return len(self.encoder      )




 def      __magic_name__	(      self	:							Optional[int]      )	->						Dict:
       return dict(self.encoder					,      **self.added_tokens_encoder      )




 def      __magic_name__	(      self	:							Optional[int]					,      __lowercase	:							str      )	->						Tuple:
       if token in self.cache:
             return self.cache[token]
       SCREAMING_SNAKE_CASE__     :				Any									=tuple(__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=get_pairs(__lowercase      )

       if not pairs:
             return token

       while True:
             SCREAMING_SNAKE_CASE__     :				Any									=min(__lowercase					,      key=lambda __lowercase      : self.bpe_ranks.get(__lowercase					,      float('''inf'''      )      )      )
             if bigram not in self.bpe_ranks:
                   break
             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=bigram
             SCREAMING_SNAKE_CASE__     :				List[str]									=[]
             SCREAMING_SNAKE_CASE__     :				List[Any]									=0
             while i < len(__lowercase      ):
                   try:
                         SCREAMING_SNAKE_CASE__     :				Any									=word.index(__lowercase					,      __lowercase      )
                   except ValueError:
                         new_word.extend(word[i:]      )
                         break
                   else:
                         new_word.extend(word[i:j]      )
                         SCREAMING_SNAKE_CASE__     :				Optional[int]									=j

                   if word[i] == first and i < len(__lowercase      ) - 1 and word[i + 1] == second:
                         new_word.append(first + second      )
                         i += 2
                   else:
                         new_word.append(word[i]      )
                         i += 1
             SCREAMING_SNAKE_CASE__     :				Tuple									=tuple(__lowercase      )
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=new_word
             if len(__lowercase      ) == 1:
                   break
             else:
                   SCREAMING_SNAKE_CASE__     :				Optional[int]									=get_pairs(__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=''' '''.join(__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Dict									=word
       return word




 def      __magic_name__	(      self	:							Dict					,      __lowercase	:							List[Any]      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Any									=[]
       for token in re.findall(self.pat					,      __lowercase      ):
             SCREAMING_SNAKE_CASE__     :				List[str]									=''''''.join(
                 self.byte_encoder[b] for b in token.encode('''utf-8'''      )      )  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
             bpe_tokens.extend(bpe_token for bpe_token in self.bpe(__lowercase      ).split(''' '''      )      )
       return bpe_tokens




 def      __magic_name__	(      self	:							Union[str, Any]					,      __lowercase	:							Optional[int]      )	->						Optional[int]:
       return self.encoder.get(__lowercase					,      self.encoder.get(self.unk_token      )      )




 def      __magic_name__	(      self	:							Tuple					,      __lowercase	:							Dict      )	->						Any:
       return self.decoder.get(__lowercase      )




 def      __magic_name__	(      self	:							Any					,      __lowercase	:							str      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=''''''.join(__lowercase      )
       SCREAMING_SNAKE_CASE__     :				int									=bytearray([self.byte_decoder[c] for c in text]      ).decode('''utf-8'''					,      errors=self.errors      )
       return text




 def      __magic_name__	(      self	:							Optional[int]					,      __lowercase	:							str					,      __lowercase	:							Optional[str] = None      )	->						Tuple[str]:
       if not os.path.isdir(__lowercase      ):
             logger.error(F"Vocabulary path ({save_directory}) should be a directory"      )
             return
       SCREAMING_SNAKE_CASE__     :				Dict									=os.path.join(
           __lowercase					,      (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']      )
       SCREAMING_SNAKE_CASE__     :				Dict									=os.path.join(
           __lowercase					,      (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file''']      )

       with open(__lowercase					,      '''w'''					,      encoding='''utf-8'''      ) as f:
             f.write(json.dumps(self.encoder					,      indent=2					,      sort_keys=__lowercase					,      ensure_ascii=__lowercase      ) + '''\n'''      )

       SCREAMING_SNAKE_CASE__     :				Optional[int]									=0
       with open(__lowercase					,      '''w'''					,      encoding='''utf-8'''      ) as writer:
             writer.write('''#version: 0.2\n'''      )
             for bpe_tokens, token_index in sorted(self.bpe_ranks.items()					,      key=lambda __lowercase      : kv[1]      ):
                   if index != token_index:
                         logger.warning(
                             F"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
                             ''' Please check that the tokenizer is not corrupted!'''      )
                         SCREAMING_SNAKE_CASE__     :				Dict									=token_index
                   writer.write(''' '''.join(__lowercase      ) + '''\n'''      )
                   index += 1

       return vocab_file, merge_file




 def      __magic_name__	(      self	:							Any					,      __lowercase	:							List[int]					,      __lowercase	:							Optional[List[int]] = None      )	->						List[int]:
       if token_ids_a is None:
             return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
       SCREAMING_SNAKE_CASE__     :				str									=[self.cls_token_id]
       SCREAMING_SNAKE_CASE__     :				List[str]									=[self.sep_token_id]
       return cls + token_ids_a + sep + sep + token_ids_a + sep




 def      __magic_name__	(      self	:							List[str]					,      __lowercase	:							List[int]					,      __lowercase	:							Optional[List[int]] = None					,      __lowercase	:							bool = False      )	->						List[int]:
       if already_has_special_tokens:
             return super().get_special_tokens_mask(
                 token_ids_a=__lowercase					,      token_ids_a=__lowercase					,      already_has_special_tokens=__lowercase      )

       if token_ids_a is None:
             return [1] + ([0] * len(__lowercase      )) + [1]
       return [1] + ([0] * len(__lowercase      )) + [1, 1] + ([0] * len(__lowercase      )) + [1]




 def      __magic_name__	(      self	:							Union[str, Any]					,      __lowercase	:							List[int]					,      __lowercase	:							Optional[List[int]] = None      )	->						List[int]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=[self.sep_token_id]
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[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 + sep + token_ids_a + sep      ) * [0]




 def      __magic_name__	(      self	:							Tuple					,      __lowercase	:							Union[str, Any]					,      __lowercase	:							Optional[int]=False					,      **__lowercase	:							List[str]      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=kwargs.pop('''add_prefix_space'''					,      self.add_prefix_space      )
       if (is_split_into_words or add_prefix_space) and (len(__lowercase      ) > 0 and not text[0].isspace()):
             SCREAMING_SNAKE_CASE__     :				int									=''' ''' + text
       return (text, kwargs)







'''simple docstring'''
import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import TimesformerConfig
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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import (
      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
      TimesformerForVideoClassification,
      TimesformerModel,
  )
  from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from transformers import VideoMAEImageProcessor






class      __SCREAMING_SNAKE_CASE      :




 def __init__(      self	:							Optional[int]					,      __lowercase	:							Optional[int]					,      __lowercase	:							str=13					,      __lowercase	:							int=10					,      __lowercase	:							List[Any]=3					,      __lowercase	:							List[str]=2					,      __lowercase	:							int=2					,      __lowercase	:							Dict=True					,      __lowercase	:							Optional[Any]=True					,      __lowercase	:							int=32					,      __lowercase	:							List[Any]=5					,      __lowercase	:							Union[str, Any]=4					,      __lowercase	:							Any=37					,      __lowercase	:							Optional[Any]="gelu"					,      __lowercase	:							Union[str, Any]=0.1					,      __lowercase	:							Tuple=0.1					,      __lowercase	:							Dict=10					,      __lowercase	:							int=0.02					,      __lowercase	:							str="divided_space_time"					,      __lowercase	:							Union[str, Any]=None					,      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=parent
       SCREAMING_SNAKE_CASE__     :				List[str]									=batch_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_size
       SCREAMING_SNAKE_CASE__     :				List[Any]									=num_channels
       SCREAMING_SNAKE_CASE__     :				int									=patch_size
       SCREAMING_SNAKE_CASE__     :				Tuple									=num_frames
       SCREAMING_SNAKE_CASE__     :				List[Any]									=is_training
       SCREAMING_SNAKE_CASE__     :				List[str]									=use_labels
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=hidden_size
       SCREAMING_SNAKE_CASE__     :				str									=num_hidden_layers
       SCREAMING_SNAKE_CASE__     :				int									=num_attention_heads
       SCREAMING_SNAKE_CASE__     :				Tuple									=intermediate_size
       SCREAMING_SNAKE_CASE__     :				List[str]									=hidden_act
       SCREAMING_SNAKE_CASE__     :				Dict									=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__     :				List[Any]									=attention_type
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=initializer_range
       SCREAMING_SNAKE_CASE__     :				Any									=scope
       SCREAMING_SNAKE_CASE__     :				int									=num_labels

       # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
       SCREAMING_SNAKE_CASE__     :				List[str]									=(image_size // patch_size) ** 2
       SCREAMING_SNAKE_CASE__     :				str									=(num_frames) * self.num_patches_per_frame + 1




 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=floats_tensor(
           [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=None
       if self.use_labels:
             SCREAMING_SNAKE_CASE__     :				Any									=ids_tensor([self.batch_size]					,      self.num_labels      )

       SCREAMING_SNAKE_CASE__     :				int									=self.get_config()

       return config, pixel_values, labels




 def      __magic_name__	(      self	:							int      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=TimesformerConfig(
           image_size=self.image_size					,      patch_size=self.patch_size					,      num_channels=self.num_channels					,      num_frames=self.num_frames					,      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					,      attention_type=self.attention_type					,      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=self.num_labels
       return config




 def      __magic_name__	(      self	:							int					,      __lowercase	:							Optional[int]					,      __lowercase	:							Dict					,      __lowercase	:							List[Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Tuple									=TimesformerModel(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=model(__lowercase      )
       self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)      )




 def      __magic_name__	(      self	:							List[str]					,      __lowercase	:							str					,      __lowercase	:							Tuple					,      __lowercase	:							List[Any]      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				str									=TimesformerForVideoClassification(__lowercase      )
       model.to(__lowercase      )
       model.eval()

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=model(__lowercase      )

       # verify the logits shape
       SCREAMING_SNAKE_CASE__     :				Tuple									=torch.Size((self.batch_size, self.num_labels)      )
       self.parent.assertEqual(result.logits.shape					,      __lowercase      )




 def      __magic_name__	(      self	:							Any      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				str									=config_and_inputs
       SCREAMING_SNAKE_CASE__     :				Any									={'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_torch
class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase	,				lowerCamelCase	,				unittest.TestCase				):
 snake_case_  							=							(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
 snake_case_  							=							(
     {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
     if is_torch_available()
     else {}
 )

 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False
 snake_case_  							=							False




 def      __magic_name__	(      self	:							str      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Dict									=TimesformerModelTester(self      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=ConfigTester(
           self					,      config_class=__lowercase					,      has_text_modality=__lowercase					,      hidden_size=37      )




 def      __magic_name__	(      self	:							str					,      __lowercase	:							Dict					,      __lowercase	:							str					,      __lowercase	:							Optional[int]=False      )	->						int:
       SCREAMING_SNAKE_CASE__     :				str									=copy.deepcopy(__lowercase      )

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

       return inputs_dict




 def      __magic_name__	(      self	:							List[Any]      )	->						Any:
       self.config_tester.run_common_tests()




 @unittest.skip(reason='''TimeSformer does not use inputs_embeds'''      )
 def      __magic_name__	(      self	:							List[Any]      )	->						Optional[int]:
       pass




 def      __magic_name__	(      self	:							str      )	->						Tuple:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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




 def      __magic_name__	(      self	:							Any      )	->						Any:
       SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				str									=self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE__     :				Dict									=['''pixel_values''']
             self.assertListEqual(arg_names[:1]					,      __lowercase      )




 def      __magic_name__	(      self	:							int      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowercase      )




 def      __magic_name__	(      self	:							List[str]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_video_classification(*__lowercase      )




 @slow
 def      __magic_name__	(      self	:							Optional[int]      )	->						Optional[Any]:
       for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=TimesformerModel.from_pretrained(__lowercase      )
             self.assertIsNotNone(__lowercase      )




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						List[str]:
       if not self.has_attentions:
             pass

       else:
             SCREAMING_SNAKE_CASE__		,	SCREAMING_SNAKE_CASE__     :				Any									=self.model_tester.prepare_config_and_inputs_for_common()
             SCREAMING_SNAKE_CASE__     :				List[Any]									=True

             for model_class in self.all_model_classes:
                   SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.seq_length
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.model_tester.num_frames

                   SCREAMING_SNAKE_CASE__     :				Optional[Any]									=True
                   SCREAMING_SNAKE_CASE__     :				str									=False
                   SCREAMING_SNAKE_CASE__     :				Tuple									=True
                   SCREAMING_SNAKE_CASE__     :				Dict									=model_class(__lowercase      )
                   model.to(__lowercase      )
                   model.eval()
                   with torch.no_grad():
                         SCREAMING_SNAKE_CASE__     :				Optional[Any]									=model(**self._prepare_for_class(__lowercase					,      __lowercase      )      )
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=outputs.attentions
                   self.assertEqual(len(__lowercase      )					,      self.model_tester.num_hidden_layers      )

                   # check that output_attentions also work using config
                   del inputs_dict["output_attentions"]
                   SCREAMING_SNAKE_CASE__     :				List[Any]									=True
                   SCREAMING_SNAKE_CASE__     :				List[str]									=model_class(__lowercase      )
                   model.to(__lowercase      )
                   model.eval()
                   with torch.no_grad():
                         SCREAMING_SNAKE_CASE__     :				Any									=model(**self._prepare_for_class(__lowercase					,      __lowercase      )      )
                   SCREAMING_SNAKE_CASE__     :				List[Any]									=outputs.attentions
                   self.assertEqual(len(__lowercase      )					,      self.model_tester.num_hidden_layers      )

                   # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
                   self.assertListEqual(
                       list(attentions[0].shape[-3:]      )					,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]					,      )
                   SCREAMING_SNAKE_CASE__     :				Optional[int]									=len(__lowercase      )

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

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

                   SCREAMING_SNAKE_CASE__     :				List[str]									=outputs.attentions

                   self.assertEqual(len(__lowercase      )					,      self.model_tester.num_hidden_layers      )

                   # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
                   self.assertListEqual(
                       list(self_attentions[0].shape[-3:]      )					,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]					,      )




 def      __magic_name__	(      self	:							Tuple      )	->						List[Any]:
       def check_hidden_states_output(__lowercase	:							Tuple					,      __lowercase	:							Dict					,      __lowercase	:							Optional[int]      ):
             SCREAMING_SNAKE_CASE__     :				List[Any]									=model_class(__lowercase      )
             model.to(__lowercase      )
             model.eval()

             with torch.no_grad():
                   SCREAMING_SNAKE_CASE__     :				int									=model(**self._prepare_for_class(__lowercase					,      __lowercase      )      )

             SCREAMING_SNAKE_CASE__     :				List[Any]									=outputs.hidden_states
             SCREAMING_SNAKE_CASE__     :				Tuple									=self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(__lowercase      )					,      __lowercase      )

             SCREAMING_SNAKE_CASE__     :				int									=self.model_tester.seq_length

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

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

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__     :				Tuple									=True
             check_hidden_states_output(__lowercase					,      __lowercase					,      __lowercase      )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             SCREAMING_SNAKE_CASE__     :				List[str]									=True

             check_hidden_states_output(__lowercase					,      __lowercase					,      __lowercase      )





def  _a(  ):




      '''simple docstring'''






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







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




 @cached_property
 def      __magic_name__	(      self	:							Any      )	->						List[str]:
       # logits were tested with a different mean and std, so we use the same here
       return (
           VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]					,      image_std=[0.5, 0.5, 0.5]      )
           if is_vision_available()
           else None
       )




 @slow
 def      __magic_name__	(      self	:							Any      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				int									=TimesformerForVideoClassification.from_pretrained('''facebook/timesformer-base-finetuned-k400'''      ).to(
           __lowercase      )

       SCREAMING_SNAKE_CASE__     :				List[str]									=self.default_image_processor
       SCREAMING_SNAKE_CASE__     :				Tuple									=prepare_video()
       SCREAMING_SNAKE_CASE__     :				Any									=image_processor(video[:8]					,      return_tensors='''pt'''      ).to(__lowercase      )

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

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				List[str]									=torch.Size((1, 4_00)      )
       self.assertEqual(outputs.logits.shape					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=torch.tensor([-0.3016, -0.7713, -0.4205]      ).to(__lowercase      )

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