





'''simple docstring'''

from __future__ import annotations

import copy
import inspect
import json
import math
import os
import tempfile
import unittest
from importlib import import_module

import numpy as np

from transformers import ViTMAEConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFViTMAEForPreTraining, TFViTMAEModel


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor






class        UpperCAmelCase     :
 def __init__(    self				:Union[str, Any]						,	lowercase_				:Tuple						,	lowercase_				:str=13						,	lowercase_				:Tuple=30						,	lowercase_				:List[Any]=2						,	lowercase_				:Any=3						,	lowercase_				:int=True						,	lowercase_				:List[Any]=True						,	lowercase_				:int=32						,	lowercase_				:int=2						,	lowercase_				:int=4						,	lowercase_				:List[str]=37						,	lowercase_				:Optional[Any]="gelu"						,	lowercase_				:List[str]=0.1						,	lowercase_				:Tuple=0.1						,	lowercase_				:Tuple=10						,	lowercase_				:Optional[Any]=0.0_2						,	lowercase_				:List[str]=3						,	lowercase_				:Optional[Any]=0.6						,	lowercase_				:Union[str, Any]=None						,	)->							Optional[int]:
     A__    =      parent
     A__    =      batch_size
     A__    =      image_size
     A__    =      patch_size
     A__    =      num_channels
     A__    =      is_training
     A__    =      use_labels
     A__    =      hidden_size
     A__    =      num_hidden_layers
     A__    =      num_attention_heads
     A__    =      intermediate_size
     A__    =      hidden_act
     A__    =      hidden_dropout_prob
     A__    =      attention_probs_dropout_prob
     A__    =      type_sequence_label_size
     A__    =      initializer_range
     A__    =      mask_ratio
     A__    =      scope

     # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
     # (we add 1 for the [CLS] token)
     A__    =      (image_size // patch_size) ** 2
     A__    =      int(math.ceil((1 - mask_ratio) * (num_patches + 1)							)							)
 def 					UpperCAmelCase_							(    self				:str							)->							List[str]:
     A__    =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

     A__    =      None
     if self.use_labels:
         A__    =      ids_tensor([self.batch_size]						,	self.type_sequence_label_size							)

     A__    =      self.get_config()

     return config, pixel_values, labels
 def 					UpperCAmelCase_							(    self				:Optional[int]							)->							int:
     return ViTMAEConfig(
         image_size=self.image_size						,	patch_size=self.patch_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						,	decoder_hidden_size=self.hidden_size						,	decoder_num_hidden_layers=self.num_hidden_layers						,	decoder_num_attention_heads=self.num_attention_heads						,	decoder_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						,	mask_ratio=self.mask_ratio						,	)
 def 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:Optional[int]						,	lowercase_				:List[Any]						,	lowercase_				:Tuple							)->							Any:
     A__    =      TFViTMAEModel(config=_lowerCamelCase							)
     A__    =      model(_lowerCamelCase						,	training=_lowerCamelCase							)
     self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)							)
 def 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:str						,	lowercase_				:Optional[int]						,	lowercase_				:Union[str, Any]							)->							Optional[Any]:
     A__    =      TFViTMAEForPreTraining(_lowerCamelCase							)
     A__    =      model(_lowerCamelCase						,	training=_lowerCamelCase							)
     # expected sequence length = num_patches
     A__    =      (self.image_size // self.patch_size) ** 2
     A__    =      self.patch_size**2 * self.num_channels
     self.parent.assertEqual(result.logits.shape						,	(self.batch_size, num_patches, expected_num_channels)							)

     # test greyscale images
     A__    =      1
     A__    =      TFViTMAEForPreTraining(_lowerCamelCase							)

     A__    =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
     A__    =      model(_lowerCamelCase						,	training=_lowerCamelCase							)
     A__    =      self.patch_size**2
     self.parent.assertEqual(result.logits.shape						,	(self.batch_size, num_patches, expected_num_channels)							)



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






@require_tf
class        UpperCAmelCase     (	a__      ,	a__      ,	unittest.TestCase    ):
 __lowercase       		=    (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
 __lowercase       		=    {"""feature-extraction""": TFViTMAEModel} if is_tf_available() else {}

 __lowercase       		=    False
 __lowercase       		=    False
 __lowercase       		=    False
 __lowercase       		=    False
 def 					UpperCAmelCase_							(    self				:Dict							)->							Any:
     A__    =      TFViTMAEModelTester(self							)
     A__    =      ConfigTester(self						,	config_class=_lowerCamelCase						,	has_text_modality=_lowerCamelCase						,	hidden_size=37							)
 def 					UpperCAmelCase_							(    self				:Tuple							)->							Tuple:
     self.config_tester.run_common_tests()
 @unittest.skip(reason="ViTMAE does not use inputs_embeds"							)
 def 					UpperCAmelCase_							(    self				:Any							)->							Any:
     pass
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							Optional[int]:
     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         A__    =      model_class(_lowerCamelCase							)
         self.assertIsInstance(model.get_input_embeddings()						,	(tf.keras.layers.Layer)							)
         A__    =      model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(_lowerCamelCase						,	tf.keras.layers.Layer							)							)
 def 					UpperCAmelCase_							(    self				:Any							)->							Union[str, Any]:
     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

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

         A__    =      ['''pixel_values''']
         self.assertListEqual(arg_names[:1]						,	_lowerCamelCase							)
 def 					UpperCAmelCase_							(    self				:str							)->							int:
     A__    =      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCamelCase							)
 def 					UpperCAmelCase_							(    self				:Any							)->							List[Any]:
     A__    =      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*_lowerCamelCase							)
 def 					UpperCAmelCase_							(    self				:Union[str, Any]							)->							str:
     np.random.seed(2							)

     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

     A__    =      int((config.image_size // config.patch_size) ** 2							)
     A__    =      np.random.uniform(size=(self.model_tester.batch_size, num_patches)							)

     for model_class in self.all_model_classes:
         A__    =      model_class(_lowerCamelCase							)
         A__    =      self._prepare_for_class(_lowerCamelCase						,	_lowerCamelCase							)

         A__    =      model(_lowerCamelCase						,	noise=_lowerCamelCase							)

         A__    =      copy.deepcopy(self._prepare_for_class(_lowerCamelCase						,	_lowerCamelCase							)							)
         A__    =      model(**_lowerCamelCase						,	noise=_lowerCamelCase							)
         A__    =      outputs_dict[0].numpy()
         A__    =      outputs_keywords[0].numpy()

         self.assertLess(np.sum(np.abs(output_dict - output_keywords							)							)						,	1E-6							)
 def 					UpperCAmelCase_							(    self				:List[Any]							)->							Optional[Any]:
     np.random.seed(2							)

     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

     A__    =      int((config.image_size // config.patch_size) ** 2							)
     A__    =      np.random.uniform(size=(self.model_tester.batch_size, num_patches)							)

     def prepare_numpy_arrays(lowercase_				:Optional[Any]							):
         A__    =      {}
         for k, v in inputs_dict.items():
             if tf.is_tensor(_lowerCamelCase							):
                 A__    =      v.numpy()
             else:
                 A__    =      np.array(_lowerCamelCase							)

         return inputs_np_dict

     for model_class in self.all_model_classes:
         A__    =      model_class(_lowerCamelCase							)

         A__    =      self._prepare_for_class(_lowerCamelCase						,	_lowerCamelCase							)
         A__    =      prepare_numpy_arrays(_lowerCamelCase							)

         A__    =      model(_lowerCamelCase						,	noise=_lowerCamelCase							)
         A__    =      model(**_lowerCamelCase						,	noise=_lowerCamelCase							)
         self.assert_outputs_same(_lowerCamelCase						,	_lowerCamelCase							)
 def 					UpperCAmelCase_							(    self				:List[str]						,	lowercase_				:Any						,	lowercase_				:List[str]						,	lowercase_				:Optional[Any]							)->							Optional[int]:
     np.random.seed(2							)

     A__    =      int((tf_model.config.image_size // tf_model.config.patch_size) ** 2							)
     A__    =      np.random.uniform(size=(self.model_tester.batch_size, num_patches)							)
     A__    =      tf.constant(_lowerCamelCase							)

     # Add `noise` argument.
     # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
     A__    =      tf_noise

     super().check_pt_tf_models(_lowerCamelCase						,	_lowerCamelCase						,	_lowerCamelCase							)
 def 					UpperCAmelCase_							(    self				:Dict							)->							Tuple:
     np.random.seed(2							)

     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

     A__    =      {
         module_member
         for model_class in self.all_model_classes
         for module in (import_module(model_class.__module__							),)
         for module_member_name in dir(_lowerCamelCase							)
         if module_member_name.endswith("MainLayer"							)
         # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`.
         and module_member_name[: -len("MainLayer"							)] == model_class.__name__[: -len("Model"							)]
         for module_member in (getattr(_lowerCamelCase						,	_lowerCamelCase							),)
         if isinstance(_lowerCamelCase						,	_lowerCamelCase							)
         and tf.keras.layers.Layer in module_member.__bases__
         and getattr(_lowerCamelCase						,	"_keras_serializable"						,	_lowerCamelCase							)
     }

     A__    =      int((config.image_size // config.patch_size) ** 2							)
     A__    =      np.random.uniform(size=(self.model_tester.batch_size, num_patches)							)
     A__    =      tf.convert_to_tensor(_lowerCamelCase							)
     inputs_dict.update({"noise": noise}							)

     for main_layer_class in tf_main_layer_classes:
         A__    =      main_layer_class(_lowerCamelCase							)

         A__    =      {
             name: tf.keras.Input(tensor.shape[1:]						,	dtype=tensor.dtype							) for name, tensor in inputs_dict.items()
         }

         A__    =      tf.keras.Model(_lowerCamelCase						,	outputs=main_layer(_lowerCamelCase							)							)
         A__    =      model(_lowerCamelCase							)

         with tempfile.TemporaryDirectory() as tmpdirname:
             A__    =      os.path.join(_lowerCamelCase						,	"keras_model.h5"							)
             model.save(_lowerCamelCase							)
             A__    =      tf.keras.models.load_model(
                 _lowerCamelCase						,	custom_objects={main_layer_class.__name__: main_layer_class}							)
             assert isinstance(_lowerCamelCase						,	tf.keras.Model							)
             A__    =      model(_lowerCamelCase							)
             self.assert_outputs_same(_lowerCamelCase						,	_lowerCamelCase							)
 @slow
 def 					UpperCAmelCase_							(    self				:str							)->							Optional[int]:
     np.random.seed(2							)

     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

     A__    =      int((config.image_size // config.patch_size) ** 2							)
     A__    =      np.random.uniform(size=(self.model_tester.batch_size, num_patches)							)

     for model_class in self.all_model_classes:
         A__    =      model_class(_lowerCamelCase							)
         A__    =      self._prepare_for_class(_lowerCamelCase						,	_lowerCamelCase							)
         A__    =      model(_lowerCamelCase						,	noise=_lowerCamelCase							)

         if model_class.__name__ == "TFViTMAEModel":
             A__    =      outputs.last_hidden_state.numpy()
             A__    =      0
         else:
             A__    =      outputs.logits.numpy()
             A__    =      0

         with tempfile.TemporaryDirectory() as tmpdirname:
             model.save_pretrained(_lowerCamelCase						,	saved_model=_lowerCamelCase							)
             A__    =      model_class.from_pretrained(_lowerCamelCase							)
             A__    =      model(_lowerCamelCase						,	noise=_lowerCamelCase							)

             if model_class.__name__ == "TFViTMAEModel":
                 A__    =      after_outputs['''last_hidden_state'''].numpy()
                 A__    =      0
             else:
                 A__    =      after_outputs['''logits'''].numpy()
                 A__    =      0

             A__    =      np.amax(np.abs(out_a - out_a							)							)
             self.assertLessEqual(_lowerCamelCase						,	1E-5							)
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							str:
     np.random.seed(2							)

     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

     A__    =      int((config.image_size // config.patch_size) ** 2							)
     A__    =      np.random.uniform(size=(self.model_tester.batch_size, num_patches)							)

     for model_class in self.all_model_classes:
         A__    =      model_class(_lowerCamelCase							)
         A__    =      self._prepare_for_class(_lowerCamelCase						,	_lowerCamelCase							)

         A__    =      model(_lowerCamelCase						,	noise=_lowerCamelCase							)
         A__    =      model.get_config()
         # make sure that returned config is jsonifiable, which is required by keras
         json.dumps(_lowerCamelCase							)
         A__    =      model_class.from_config(model.get_config()							)
         # make sure it also accepts a normal config
         A__    =      model_class.from_config(model.config							)
         A__    =      new_model(_lowerCamelCase							)  # Build model
         new_model.set_weights(model.get_weights()							)
         A__    =      new_model(_lowerCamelCase						,	noise=_lowerCamelCase							)

         self.assert_outputs_same(_lowerCamelCase						,	_lowerCamelCase							)
 @unittest.skip(
     reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results."							)
 def 					UpperCAmelCase_							(    self				:List[str]							)->							str:
     pass
 @unittest.skip(reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load"							)
 def 					UpperCAmelCase_							(    self				:List[Any]							)->							List[str]:
     pass



 @slow
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							List[str]:
     A__    =      TFViTMAEModel.from_pretrained("google/vit-base-patch16-224"							)
     self.assertIsNotNone(_lowerCamelCase							)





def 						UpperCamelCase    (							):
    A__    =      Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
    return image






@require_tf
@require_vision
class        UpperCAmelCase     (	unittest.TestCase    ):
 @cached_property
 def 					UpperCAmelCase_							(    self				:int							)->							Any:
     return ViTImageProcessor.from_pretrained("facebook/vit-mae-base"							) if is_vision_available() else None



 @slow
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							Dict:
     np.random.seed(2							)

     A__    =      TFViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base"							)

     A__    =      self.default_image_processor
     A__    =      prepare_img()
     A__    =      image_processor(images=_lowerCamelCase						,	return_tensors="tf"							)

     # prepare a noise vector that will be also used for testing the TF model
     # (this way we can ensure that the PT and TF models operate on the same inputs)
     A__    =      ViTMAEConfig()
     A__    =      int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2							)
     A__    =      np.random.uniform(size=(1, num_patches)							)

     # forward pass
     A__    =      model(**_lowerCamelCase						,	noise=_lowerCamelCase							)

     # verify the logits
     A__    =      tf.convert_to_tensor([1, 1_96, 7_68]							)
     self.assertEqual(outputs.logits.shape						,	_lowerCamelCase							)

     A__    =      tf.convert_to_tensor(
         [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]]							)

     tf.debugging.assert_near(outputs.logits[0, :3, :3]						,	_lowerCamelCase						,	atol=1E-4							)










'''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        UpperCAmelCase     :
 def __init__(    self				:Optional[Any]						,	lowercase_				:int						,	lowercase_				:Union[str, Any]=13						,	lowercase_				:Union[str, Any]=10						,	lowercase_				:Any=3						,	lowercase_				:Tuple=2						,	lowercase_				:List[Any]=2						,	lowercase_				:int=True						,	lowercase_				:int=True						,	lowercase_				:List[str]=32						,	lowercase_				:Dict=5						,	lowercase_				:List[Any]=4						,	lowercase_				:List[Any]=37						,	lowercase_				:List[Any]="gelu"						,	lowercase_				:int=0.1						,	lowercase_				:List[Any]=0.1						,	lowercase_				:List[Any]=10						,	lowercase_				:int=0.0_2						,	lowercase_				:Union[str, Any]="divided_space_time"						,	lowercase_				:Tuple=None						,	)->							Tuple:
     A__    =      parent
     A__    =      batch_size
     A__    =      image_size
     A__    =      num_channels
     A__    =      patch_size
     A__    =      num_frames
     A__    =      is_training
     A__    =      use_labels
     A__    =      hidden_size
     A__    =      num_hidden_layers
     A__    =      num_attention_heads
     A__    =      intermediate_size
     A__    =      hidden_act
     A__    =      hidden_dropout_prob
     A__    =      attention_probs_dropout_prob
     A__    =      attention_type
     A__    =      initializer_range
     A__    =      scope
     A__    =      num_labels

     # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
     A__    =      (image_size // patch_size) ** 2
     A__    =      (num_frames) * self.num_patches_per_frame + 1
 def 					UpperCAmelCase_							(    self				:str							)->							str:
     A__    =      floats_tensor(
         [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]							)

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

     A__    =      self.get_config()

     return config, pixel_values, labels
 def 					UpperCAmelCase_							(    self				:int							)->							Any:
     A__    =      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						,	)
     A__    =      self.num_labels
     return config
 def 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:List[str]						,	lowercase_				:List[Any]						,	lowercase_				:Tuple							)->							Optional[int]:
     A__    =      TimesformerModel(config=lowercase_							)
     model.to(lowercase_							)
     model.eval()
     A__    =      model(lowercase_							)
     self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)							)
 def 					UpperCAmelCase_							(    self				:List[str]						,	lowercase_				:Tuple						,	lowercase_				:Tuple						,	lowercase_				:Dict							)->							Tuple:
     A__    =      TimesformerForVideoClassification(lowercase_							)
     model.to(lowercase_							)
     model.eval()

     A__    =      model(lowercase_							)

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



 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							str:
     A__    =      self.prepare_config_and_inputs()
     A__,     A__,     A__    =      config_and_inputs
     A__    =      {"pixel_values": pixel_values}
     return config, inputs_dict






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

 __lowercase       		=    False
 __lowercase       		=    False
 __lowercase       		=    False
 __lowercase       		=    False
 def 					UpperCAmelCase_							(    self				:Union[str, Any]							)->							Optional[int]:
     A__    =      TimesformerModelTester(self							)
     A__    =      ConfigTester(
         self						,	config_class=lowercase_						,	has_text_modality=lowercase_						,	hidden_size=37							)
 def 					UpperCAmelCase_							(    self				:Union[str, Any]						,	lowercase_				:int						,	lowercase_				:Dict						,	lowercase_				:int=False							)->							str:
     A__    =      copy.deepcopy(lowercase_							)

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

     return inputs_dict
 def 					UpperCAmelCase_							(    self				:Union[str, Any]							)->							List[Any]:
     self.config_tester.run_common_tests()
 @unittest.skip(reason="TimeSformer does not use inputs_embeds"							)
 def 					UpperCAmelCase_							(    self				:List[Any]							)->							Tuple:
     pass
 def 					UpperCAmelCase_							(    self				:Dict							)->							Optional[Any]:
     A__,     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

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

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

         A__    =      ["pixel_values"]
         self.assertListEqual(arg_names[:1]						,	lowercase_							)
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							Optional[int]:
     A__    =      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_							)
 def 					UpperCAmelCase_							(    self				:Dict							)->							Optional[int]:
     A__    =      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_video_classification(*lowercase_							)
 @slow
 def 					UpperCAmelCase_							(    self				:Any							)->							List[Any]:
     for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         A__    =      TimesformerModel.from_pretrained(lowercase_							)
         self.assertIsNotNone(lowercase_							)
 def 					UpperCAmelCase_							(    self				:List[str]							)->							str:
     if not self.has_attentions:
         pass

     else:
         A__,     A__    =      self.model_tester.prepare_config_and_inputs_for_common()
         A__    =      True

         for model_class in self.all_model_classes:
             A__    =      self.model_tester.seq_length
             A__    =      self.model_tester.num_frames

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

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

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

             A__    =      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 					UpperCAmelCase_							(    self				:List[Any]							)->							List[str]:
     def check_hidden_states_output(lowercase_				:Dict						,	lowercase_				:int						,	lowercase_				:List[Any]							):
         A__    =      model_class(lowercase_							)
         model.to(lowercase_							)
         model.eval()

         with torch.no_grad():
             A__    =      model(**self._prepare_for_class(lowercase_						,	lowercase_							)							)

         A__    =      outputs.hidden_states
         A__    =      self.model_tester.num_hidden_layers + 1
         self.assertEqual(len(lowercase_							)						,	lowercase_							)

         A__    =      self.model_tester.seq_length

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

     A__,     A__    =      self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         A__    =      True
         check_hidden_states_output(lowercase_						,	lowercase_						,	lowercase_							)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         A__    =      True

         check_hidden_states_output(lowercase_						,	lowercase_						,	lowercase_							)





def 						UpperCamelCase    (							):
    A__    =      hf_hub_download(
        repo_id="hf-internal-testing/spaghetti-video"						,	filename="eating_spaghetti.npy"						,	repo_type="dataset"	)
    A__    =      np.load(_lowerCamelCase	)
    return list(_lowerCamelCase	)






@require_torch
@require_vision
class        UpperCAmelCase     (	unittest.TestCase    ):
 @cached_property
 def 					UpperCAmelCase_							(    self				:Optional[Any]							)->							int:
     # 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				:int							)->							Any:
     A__    =      TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"							).to(
         lowercase_							)

     A__    =      self.default_image_processor
     A__    =      prepare_video()
     A__    =      image_processor(video[:8]						,	return_tensors="pt"							).to(lowercase_							)

     # forward pass
     with torch.no_grad():
         A__    =      model(**lowercase_							)

     # verify the logits
     A__    =      torch.Size((1, 4_00)							)
     self.assertEqual(outputs.logits.shape						,	lowercase_							)

     A__    =      torch.tensor([-0.3_0_1_6, -0.7_7_1_3, -0.4_2_0_5]							).to(lowercase_							)

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



