


"""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 	lowercase__				:





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

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





   def 		UpperCamelCase_  (							self)  ->  Optional[int]:
      _lowerCamelCase	:					Any   =		floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size])

      _lowerCamelCase	:					Optional[Any]   =		None
      if self.use_labels:
         _lowerCamelCase	:					Union[str, Any]   =		ids_tensor([self.batch_size]      ,   self.num_labels)

      _lowerCamelCase	:					List[Any]   =		self.get_config()

      return config, pixel_values, labels





   def 		UpperCamelCase_  (							self)  ->  int:
      _lowerCamelCase	:					List[str]   =		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      ,   )
      _lowerCamelCase	:					Optional[Any]   =		self.num_labels
      return config





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  Tuple:
      _lowerCamelCase	:					Dict   =		TimesformerModel(config=SCREAMING_SNAKE_CASE)
      model.to(SCREAMING_SNAKE_CASE)
      model.eval()
      _lowerCamelCase	:					Optional[Any]   =		model(SCREAMING_SNAKE_CASE)
      self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size))





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)  ->  Union[str, Any]:
      _lowerCamelCase	:					str   =		TimesformerForVideoClassification(SCREAMING_SNAKE_CASE)
      model.to(SCREAMING_SNAKE_CASE)
      model.eval()

      _lowerCamelCase	:					str   =		model(SCREAMING_SNAKE_CASE)

      # verify the logits shape
      _lowerCamelCase	:					Dict   =		torch.Size((self.batch_size, self.num_labels))
      self.parent.assertEqual(result.logits.shape      ,   SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  List[str]:
      _lowerCamelCase	:					int   =		self.prepare_config_and_inputs()
      _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase	:					Dict   =		config_and_inputs
      _lowerCamelCase	:					List[Any]   =		{"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_torch
class 	lowercase__				(					A_ ,A_ ,unittest.TestCase				):
   __UpperCAmelCase							       =					(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
   __UpperCAmelCase							       =					(
       {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
       if is_torch_available()
       else {}
   )

   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False
   __UpperCAmelCase							       =					False





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      _lowerCamelCase	:					Any   =		TimesformerModelTester(self)
      _lowerCamelCase	:					List[Any]   =		ConfigTester(
          self      ,   config_class=SCREAMING_SNAKE_CASE      ,   has_text_modality=SCREAMING_SNAKE_CASE      ,   hidden_size=37)





   def 		UpperCamelCase_  (							self      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE=False)  ->  str:
      _lowerCamelCase	:					List[str]   =		copy.deepcopy(SCREAMING_SNAKE_CASE)

      if return_labels:
         if model_class in get_values(SCREAMING_SNAKE_CASE):
            _lowerCamelCase	:					Dict   =		torch.zeros(
                self.model_tester.batch_size      ,   dtype=torch.long      ,   device=SCREAMING_SNAKE_CASE)

      return inputs_dict





   def 		UpperCamelCase_  (							self)  ->  str:
      self.config_tester.run_common_tests()





   @unittest.skip(reason="""TimeSformer does not use inputs_embeds""")
   def 		UpperCamelCase_  (							self)  ->  Tuple:
      pass





   def 		UpperCamelCase_  (							self)  ->  List[Any]:
      _lowerCamelCase   ,    _lowerCamelCase	:					Optional[Any]   =		self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         _lowerCamelCase	:					Union[str, Any]   =		model_class(SCREAMING_SNAKE_CASE)
         self.assertIsInstance(model.get_input_embeddings()      ,   (nn.Module))
         _lowerCamelCase	:					List[str]   =		model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE      ,   nn.Linear))





   def 		UpperCamelCase_  (							self)  ->  Any:
      _lowerCamelCase   ,    _lowerCamelCase	:					Optional[int]   =		self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowerCamelCase	:					int   =		["""pixel_values"""]
         self.assertListEqual(arg_names[:1]      ,   SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Any:
      _lowerCamelCase	:					Optional[Any]   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  Union[str, Any]:
      _lowerCamelCase	:					Tuple   =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_video_classification(*SCREAMING_SNAKE_CASE)





   @slow
   def 		UpperCamelCase_  (							self)  ->  str:
      for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCamelCase	:					str   =		TimesformerModel.from_pretrained(SCREAMING_SNAKE_CASE)
         self.assertIsNotNone(SCREAMING_SNAKE_CASE)





   def 		UpperCamelCase_  (							self)  ->  int:
      if not self.has_attentions:
         pass

      else:
         _lowerCamelCase   ,    _lowerCamelCase	:					str   =		self.model_tester.prepare_config_and_inputs_for_common()
         _lowerCamelCase	:					Tuple   =		True

         for model_class in self.all_model_classes:
            _lowerCamelCase	:					int   =		self.model_tester.seq_length
            _lowerCamelCase	:					Optional[int]   =		self.model_tester.num_frames

            _lowerCamelCase	:					List[str]   =		True
            _lowerCamelCase	:					Optional[int]   =		False
            _lowerCamelCase	:					List[Any]   =		True
            _lowerCamelCase	:					Optional[Any]   =		model_class(SCREAMING_SNAKE_CASE)
            model.to(SCREAMING_SNAKE_CASE)
            model.eval()
            with torch.no_grad():
               _lowerCamelCase	:					int   =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE))
            _lowerCamelCase	:					int   =		outputs.attentions
            self.assertEqual(len(SCREAMING_SNAKE_CASE)      ,   self.model_tester.num_hidden_layers)

            # check that output_attentions also work using config
            del inputs_dict["output_attentions"]
            _lowerCamelCase	:					List[str]   =		True
            _lowerCamelCase	:					int   =		model_class(SCREAMING_SNAKE_CASE)
            model.to(SCREAMING_SNAKE_CASE)
            model.eval()
            with torch.no_grad():
               _lowerCamelCase	:					int   =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE))
            _lowerCamelCase	:					str   =		outputs.attentions
            self.assertEqual(len(SCREAMING_SNAKE_CASE)      ,   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]      ,   )
            _lowerCamelCase	:					str   =		len(SCREAMING_SNAKE_CASE)

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

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

            _lowerCamelCase	:					Optional[Any]   =		outputs.attentions

            self.assertEqual(len(SCREAMING_SNAKE_CASE)      ,   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)  ->  str:
      def check_hidden_states_output(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE):
         _lowerCamelCase	:					List[Any]   =		model_class(SCREAMING_SNAKE_CASE)
         model.to(SCREAMING_SNAKE_CASE)
         model.eval()

         with torch.no_grad():
            _lowerCamelCase	:					Optional[int]   =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE))

         _lowerCamelCase	:					List[Any]   =		outputs.hidden_states
         _lowerCamelCase	:					Union[str, Any]   =		self.model_tester.num_hidden_layers + 1
         self.assertEqual(len(SCREAMING_SNAKE_CASE)      ,   SCREAMING_SNAKE_CASE)

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

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

      _lowerCamelCase   ,    _lowerCamelCase	:					Optional[int]   =		self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         _lowerCamelCase	:					List[str]   =		True
         check_hidden_states_output(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)

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

         check_hidden_states_output(SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE      ,   SCREAMING_SNAKE_CASE)







def 				_snake_case      (   ):




   """simple docstring"""







   _lowerCamelCase	:					List[Any]   =		hf_hub_download(
       repo_id="""hf-internal-testing/spaghetti-video"""						, filename="""eating_spaghetti.npy"""						, repo_type="""dataset"""   )
   _lowerCamelCase	:					Optional[Any]   =		np.load(__snake_case   )
   return list(__snake_case   )





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





   @cached_property
   def 		UpperCamelCase_  (							self)  ->  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[int]:
      _lowerCamelCase	:					Union[str, Any]   =		TimesformerForVideoClassification.from_pretrained("""facebook/timesformer-base-finetuned-k400""").to(
          SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					Union[str, Any]   =		self.default_image_processor
      _lowerCamelCase	:					Dict   =		prepare_video()
      _lowerCamelCase	:					int   =		image_processor(video[:8]      ,   return_tensors="""pt""").to(SCREAMING_SNAKE_CASE)

      # forward pass
      with torch.no_grad():
         _lowerCamelCase	:					Tuple   =		model(**SCREAMING_SNAKE_CASE)

      # verify the logits
      _lowerCamelCase	:					Tuple   =		torch.Size((1, 400))
      self.assertEqual(outputs.logits.shape      ,   SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					Tuple   =		torch.tensor([-0.30_16, -0.77_13, -0.42_05]).to(SCREAMING_SNAKE_CASE)

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



'''simple docstring'''

import warnings
from functools import wraps
from typing import Callable


def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


 @wraps(lowerCamelCase_			)
 def _inner_fn(*lowerCamelCase_      ,			**lowerCamelCase_			):
  warnings.warn(
      (f'''\'{fn.__name__}\' is experimental and might be subject to breaking changes in the future.''')      ,			lowerCamelCase_      ,			)
  return fn(*lowerCamelCase_      ,			**lowerCamelCase_			)

 return _inner_fn





