import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
      from PIL import Image

      from transformers import CLIPImageProcessor, CLIPProcessor






@require_vision
class 		_lowerCamelCase		(   unittest.TestCase    ):
    def 						UpperCamelCase_				(  self   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      tempfile.mkdtemp()

     # fmt: off
     SCREAMING_SNAKE_CASE__:   Optional[int]=      ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
     # fmt: on
     SCREAMING_SNAKE_CASE__:   Tuple=      dict(zip(_lowercase , range(len(_lowercase   )   )   )   )
     SCREAMING_SNAKE_CASE__:   int=      ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
     SCREAMING_SNAKE_CASE__:   List[str]=      {"""unk_token""": """<unk>"""}

     SCREAMING_SNAKE_CASE__:   Tuple=      os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file''']   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file''']   )
     with open(self.vocab_file , '''w''' , encoding='''utf-8'''   ) as fp:
      fp.write(json.dumps(_lowercase   ) + '''\n'''   )
     with open(self.merges_file , '''w''' , encoding='''utf-8'''   ) as fp:
      fp.write('''\n'''.join(_lowercase   )   )

     SCREAMING_SNAKE_CASE__:   Optional[Any]=      {
         """do_resize""": True,
         """size""": 20,
         """do_center_crop""": True,
         """crop_size""": 18,
         """do_normalize""": True,
         """image_mean""": [0.48145466, 0.4578275, 0.40821073],
         """image_std""": [0.26862954, 0.26130258, 0.27577711],
     }
     SCREAMING_SNAKE_CASE__:   Tuple=      os.path.join(self.tmpdirname , _lowercase   )
     with open(self.image_processor_file , '''w''' , encoding='''utf-8'''   ) as fp:
      json.dump(_lowercase , _lowercase   )
    def 						UpperCamelCase_				(  self , **lowerCAmelCase   )       ->					str:
     return CLIPTokenizer.from_pretrained(self.tmpdirname , **_lowercase   )
    def 						UpperCamelCase_				(  self , **lowerCAmelCase   )       ->					str:
     return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_lowercase   )
    def 						UpperCamelCase_				(  self , **lowerCAmelCase   )       ->					Any:
     return CLIPImageProcessor.from_pretrained(self.tmpdirname , **_lowercase   )
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     shutil.rmtree(self.tmpdirname   )
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Tuple=      [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta   )]

     SCREAMING_SNAKE_CASE__:   Optional[int]=      [Image.fromarray(np.moveaxis(_lowercase , 0 , -1   )   ) for x in image_inputs]

     return image_inputs
    def 						UpperCamelCase_				(  self   )       ->					int:
     SCREAMING_SNAKE_CASE__:   Any=      self.get_tokenizer()
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      self.get_rust_tokenizer()
     SCREAMING_SNAKE_CASE__:   List[Any]=      self.get_image_processor()

     SCREAMING_SNAKE_CASE__:   Dict=      CLIPProcessor(tokenizer=_lowercase , image_processor=_lowercase   )
     processor_slow.save_pretrained(self.tmpdirname   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=_lowercase   )

     SCREAMING_SNAKE_CASE__:   Optional[int]=      CLIPProcessor(tokenizer=_lowercase , image_processor=_lowercase   )
     processor_fast.save_pretrained(self.tmpdirname   )
     SCREAMING_SNAKE_CASE__:   int=      CLIPProcessor.from_pretrained(self.tmpdirname   )

     self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab()   )
     self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab()   )
     self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab()   )
     self.assertIsInstance(processor_slow.tokenizer , _lowercase   )
     self.assertIsInstance(processor_fast.tokenizer , _lowercase   )

     self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string()   )
     self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string()   )
     self.assertIsInstance(processor_slow.image_processor , _lowercase   )
     self.assertIsInstance(processor_fast.image_processor , _lowercase   )
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor()   )
     processor.save_pretrained(self.tmpdirname   )

     SCREAMING_SNAKE_CASE__:   Tuple=      self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)'''   )
     SCREAMING_SNAKE_CASE__:   List[str]=      self.get_image_processor(do_normalize=_lowercase , padding_value=1.0   )

     SCREAMING_SNAKE_CASE__:   int=      CLIPProcessor.from_pretrained(
         self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=_lowercase , padding_value=1.0   )

     self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()   )
     self.assertIsInstance(processor.tokenizer , _lowercase   )

     self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string()   )
     self.assertIsInstance(processor.image_processor , _lowercase   )
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      self.get_image_processor()
     SCREAMING_SNAKE_CASE__:   Dict=      self.get_tokenizer()

     SCREAMING_SNAKE_CASE__:   Dict=      CLIPProcessor(tokenizer=_lowercase , image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__:   str=      self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      image_processor(_lowercase , return_tensors='''np'''   )
     SCREAMING_SNAKE_CASE__:   Any=      processor(images=_lowercase , return_tensors='''np'''   )

     for key in input_image_proc.keys():
      self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2   )
    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     SCREAMING_SNAKE_CASE__:   List[Any]=      self.get_image_processor()
     SCREAMING_SNAKE_CASE__:   List[str]=      self.get_tokenizer()

     SCREAMING_SNAKE_CASE__:   Optional[int]=      CLIPProcessor(tokenizer=_lowercase , image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__:   int=      """lower newer"""

     SCREAMING_SNAKE_CASE__:   List[str]=      processor(text=_lowercase   )

     SCREAMING_SNAKE_CASE__:   Optional[int]=      tokenizer(_lowercase   )

     for key in encoded_tok.keys():
      self.assertListEqual(encoded_tok[key] , encoded_processor[key]   )
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   int=      self.get_image_processor()
     SCREAMING_SNAKE_CASE__:   str=      self.get_tokenizer()

     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      CLIPProcessor(tokenizer=_lowercase , image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__:   str=      """lower newer"""
     SCREAMING_SNAKE_CASE__:   Tuple=      self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE__:   Dict=      processor(text=_lowercase , images=_lowercase   )

     self.assertListEqual(list(inputs.keys()   ) , ['''input_ids''', '''attention_mask''', '''pixel_values''']   )

     # test if it raises when no input is passed
     with pytest.raises(_lowercase   ):
      processor()
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Tuple=      self.get_image_processor()
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      self.get_tokenizer()

     SCREAMING_SNAKE_CASE__:   Optional[int]=      CLIPProcessor(tokenizer=_lowercase , image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__:   Tuple=      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     SCREAMING_SNAKE_CASE__:   str=      processor.batch_decode(_lowercase   )
     SCREAMING_SNAKE_CASE__:   Any=      tokenizer.batch_decode(_lowercase   )

     self.assertListEqual(_lowercase , _lowercase   )



    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   Dict=      self.get_image_processor()
     SCREAMING_SNAKE_CASE__:   int=      self.get_tokenizer()

     SCREAMING_SNAKE_CASE__:   List[Any]=      CLIPProcessor(tokenizer=_lowercase , image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      """lower newer"""
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE__:   Optional[Any]=      processor(text=_lowercase , images=_lowercase   )

     self.assertListEqual(list(inputs.keys()   ) , processor.model_input_names   )


import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
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,
          VideoMAEForPreTraining,
          VideoMAEForVideoClassification,
          VideoMAEModel,
      )
      from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from transformers import VideoMAEImageProcessor






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

     # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
     SCREAMING_SNAKE_CASE__:   Tuple=      (image_size // patch_size) ** 2
     SCREAMING_SNAKE_CASE__:   Dict=      (num_frames // tubelet_size) * self.num_patches_per_frame

     # use this variable to define bool_masked_pos
     SCREAMING_SNAKE_CASE__:   str=      int(mask_ratio * self.seq_length   )
    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      floats_tensor(
         [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]   )

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

     SCREAMING_SNAKE_CASE__:   Dict=      self.get_config()

     return config, pixel_values, labels
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     return VideoMAEConfig(
         image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_size , 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 , is_decoder=lowerCAmelCase , initializer_range=self.initializer_range , )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__:   Any=      VideoMAEModel(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      model(lowerCAmelCase   )
     self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__:   Any=      VideoMAEForPreTraining(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     # important: each video needs to have the same number of masked patches
     # hence we define a single mask, which we then repeat for each example in the batch
     SCREAMING_SNAKE_CASE__:   str=      torch.ones((self.num_masks,)   )
     SCREAMING_SNAKE_CASE__:   str=      torch.cat([mask, torch.zeros(self.seq_length - mask.size(0   )   )]   )
     SCREAMING_SNAKE_CASE__:   List[str]=      mask.expand(self.batch_size , -1   ).bool()

     SCREAMING_SNAKE_CASE__:   List[str]=      model(lowerCAmelCase , lowerCAmelCase   )
     # model only returns predictions for masked patches
     SCREAMING_SNAKE_CASE__:   int=      mask.sum().item()
     SCREAMING_SNAKE_CASE__:   Dict=      3 * self.tubelet_size * self.patch_size**2
     self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels)   )



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






@require_torch
class 		_lowerCamelCase		(   UpperCamelCase_    ,     UpperCamelCase_    ,     unittest.TestCase    ):
    __a  =      (
        (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
    )
    __a  =      (
        {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification}
        if is_torch_available()
        else {}
    )

    __a  =      False
    __a  =      False
    __a  =      False
    __a  =      False
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Optional[int]=      VideoMAEModelTester(self   )
     SCREAMING_SNAKE_CASE__:   Tuple=      ConfigTester(self , config_class=lowerCAmelCase , has_text_modality=lowerCAmelCase , hidden_size=37   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=False   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      copy.deepcopy(lowerCAmelCase   )

     if model_class == VideoMAEForPreTraining:
      # important: each video needs to have the same number of masked patches
      # hence we define a single mask, which we then repeat for each example in the batch
      SCREAMING_SNAKE_CASE__:   str=      torch.ones((self.model_tester.num_masks,)   )
      SCREAMING_SNAKE_CASE__:   int=      torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0   )   )]   )
      SCREAMING_SNAKE_CASE__:   int=      mask.expand(self.model_tester.batch_size , -1   ).bool()
      SCREAMING_SNAKE_CASE__:   Union[str, Any]=      bool_masked_pos.to(lowerCAmelCase   )

     if return_labels:
      if model_class in [
          *get_values(lowerCAmelCase   ),
      ]:
       SCREAMING_SNAKE_CASE__:   Dict=      torch.zeros(
           self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase   )

     return inputs_dict
    def 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     self.config_tester.run_common_tests()
    @unittest.skip(reason='''VideoMAE does not use inputs_embeds'''   )
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     pass
    def 						UpperCamelCase_				(  self   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Tuple=      self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
      SCREAMING_SNAKE_CASE__:   Optional[Any]=      model_class(lowerCAmelCase   )
      self.assertIsInstance(model.get_input_embeddings() , (nn.Module)   )
      SCREAMING_SNAKE_CASE__:   Dict=      model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(lowerCAmelCase , nn.Linear   )   )
    def 						UpperCamelCase_				(  self   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Optional[Any]=      self.model_tester.prepare_config_and_inputs_for_common()

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

      SCREAMING_SNAKE_CASE__:   Tuple=      ['''pixel_values''']
      self.assertListEqual(arg_names[:1] , lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__:   Any=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   List[Any]=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase   )
    @slow
    def 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      SCREAMING_SNAKE_CASE__:   int=      VideoMAEModel.from_pretrained(lowerCAmelCase   )
      self.assertIsNotNone(lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					int:
     if not self.has_attentions:
      pass

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

      for model_class in self.all_model_classes:
       SCREAMING_SNAKE_CASE__:   Dict=      self.model_tester.seq_length - self.model_tester.num_masks
       SCREAMING_SNAKE_CASE__:   Union[str, Any]=      (
           num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
       )

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

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

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

       SCREAMING_SNAKE_CASE__:   Optional[int]=      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 						UpperCamelCase_				(  self   )       ->					Tuple:
     def check_hidden_states_output(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   ):
      SCREAMING_SNAKE_CASE__:   int=      model_class(lowerCAmelCase   )
      model.to(lowerCAmelCase   )
      model.eval()

      with torch.no_grad():
       SCREAMING_SNAKE_CASE__:   Dict=      model(**self._prepare_for_class(lowerCAmelCase , lowerCAmelCase   )   )

      SCREAMING_SNAKE_CASE__:   Any=      outputs.hidden_states
      SCREAMING_SNAKE_CASE__:   int=      self.model_tester.num_hidden_layers + 1
      self.assertEqual(len(lowerCAmelCase   ) , lowerCAmelCase   )

      SCREAMING_SNAKE_CASE__:   str=      self.model_tester.seq_length - self.model_tester.num_masks
      SCREAMING_SNAKE_CASE__:   Optional[int]=      num_visible_patches if model_class == VideoMAEForPreTraining else 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__:   List[str]=      True
      check_hidden_states_output(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )

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







def 			A__      (	):
 SCREAMING_SNAKE_CASE__:   Optional[int]=      hf_hub_download(
     repo_id='''hf-internal-testing/spaghetti-video'''       ,				filename='''eating_spaghetti.npy'''       ,				repo_type='''dataset'''      )
 SCREAMING_SNAKE_CASE__:   List[str]=      np.load(snake_case_      )
 return list(snake_case_      )






@require_torch
@require_vision
class 		_lowerCamelCase		(   unittest.TestCase    ):
    @cached_property
    def 						UpperCamelCase_				(  self   )       ->					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 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''   ).to(
         lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Dict=      self.default_image_processor
     SCREAMING_SNAKE_CASE__:   Any=      prepare_video()
     SCREAMING_SNAKE_CASE__:   List[Any]=      image_processor(lowerCAmelCase , return_tensors='''pt'''   ).to(lowerCAmelCase   )

     # forward pass
     with torch.no_grad():
      SCREAMING_SNAKE_CASE__:   Union[str, Any]=      model(**lowerCAmelCase   )

     # verify the logits
     SCREAMING_SNAKE_CASE__:   Any=      torch.Size((1, 400)   )
     self.assertEqual(outputs.logits.shape , lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Optional[Any]=      torch.tensor([0.3669, -0.0688, -0.2421]   ).to(lowerCAmelCase   )

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



    @slow
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   int=      VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''   ).to(lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   List[Any]=      self.default_image_processor
     SCREAMING_SNAKE_CASE__:   Dict=      prepare_video()
     SCREAMING_SNAKE_CASE__:   int=      image_processor(lowerCAmelCase , return_tensors='''pt'''   ).to(lowerCAmelCase   )

     # add boolean mask, indicating which patches to mask
     SCREAMING_SNAKE_CASE__:   List[Any]=      hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos''' , filename='''bool_masked_pos.pt'''   )
     SCREAMING_SNAKE_CASE__:   List[Any]=      torch.load(lowerCAmelCase   )

     # forward pass
     with torch.no_grad():
      SCREAMING_SNAKE_CASE__:   str=      model(**lowerCAmelCase   )

     # verify the logits
     SCREAMING_SNAKE_CASE__:   Tuple=      torch.Size([1, 1408, 1536]   )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      torch.tensor(
         [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]] , device=lowerCAmelCase   )
     self.assertEqual(outputs.logits.shape , lowerCAmelCase   )
     self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , lowerCAmelCase , atol=1e-4   )   )

     # verify the loss (`config.norm_pix_loss` = `True`)
     SCREAMING_SNAKE_CASE__:   str=      torch.tensor([0.5142] , device=lowerCAmelCase   )
     self.assertTrue(torch.allclose(outputs.loss , lowerCAmelCase , atol=1e-4   )   )

     # verify the loss (`config.norm_pix_loss` = `False`)
     SCREAMING_SNAKE_CASE__:   Dict=      VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' , norm_pix_loss=lowerCAmelCase   ).to(
         lowerCAmelCase   )

     with torch.no_grad():
      SCREAMING_SNAKE_CASE__:   List[Any]=      model(**lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Optional[Any]=      torch.tensor(torch.tensor([0.6469]   ) , device=lowerCAmelCase   )
     self.assertTrue(torch.allclose(outputs.loss , lowerCAmelCase , atol=1e-4   )   )

