


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

   '''simple docstring'''




   def __init__(						self		,							__A		,							__A=13		,							__A=10		,							__A=3		,							__A=2		,							__A=2		,							__A=True		,							__A=True		,							__A=32		,							__A=5		,							__A=4		,							__A=37		,							__A="gelu"		,							__A=0.1		,							__A=0.1		,							__A=10		,							__A=0.02		,							__A="divided_space_time"		,							__A=None		,							):






     """simple docstring"""





     lowerCamelCase					:   int       =							parent
     lowerCamelCase					:   Tuple       =							batch_size
     lowerCamelCase					:   List[str]       =							image_size
     lowerCamelCase					:   str       =							num_channels
     lowerCamelCase					:   Dict       =							patch_size
     lowerCamelCase					:   int       =							num_frames
     lowerCamelCase					:   Optional[int]       =							is_training
     lowerCamelCase					:   str       =							use_labels
     lowerCamelCase					:   Dict       =							hidden_size
     lowerCamelCase					:   Optional[int]       =							num_hidden_layers
     lowerCamelCase					:   Any       =							num_attention_heads
     lowerCamelCase					:   Optional[int]       =							intermediate_size
     lowerCamelCase					:   Dict       =							hidden_act
     lowerCamelCase					:   List[str]       =							hidden_dropout_prob
     lowerCamelCase					:   Dict       =							attention_probs_dropout_prob
     lowerCamelCase					:   Tuple       =							attention_type
     lowerCamelCase					:   Optional[Any]       =							initializer_range
     lowerCamelCase					:   List[Any]       =							scope
     lowerCamelCase					:   Tuple       =							num_labels

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






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Dict       =							floats_tensor(
         [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

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

     lowerCamelCase					:   str       =							self.get_config()

     return config, pixel_values, labels






   def _snake_case       (						self					):






     """simple docstring"""





     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					:   List[str]       =							self.num_labels
     return config






   def _snake_case       (						self		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Any       =							TimesformerModel(config=__lowerCamelCase					)
     model.to(__lowerCamelCase					)
     model.eval()
     lowerCamelCase					:   str       =							model(__lowerCamelCase					)
     self.parent.assertEqual(result.last_hidden_state.shape		,							(self.batch_size, self.seq_length, self.hidden_size)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   str       =							TimesformerForVideoClassification(__lowerCamelCase					)
     model.to(__lowerCamelCase					)
     model.eval()

     lowerCamelCase					:   Tuple       =							model(__lowerCamelCase					)

     # verify the logits shape
     lowerCamelCase					:   Any       =							torch.Size((self.batch_size, self.num_labels)					)
     self.parent.assertEqual(result.logits.shape		,							__lowerCamelCase					)






   def _snake_case       (						self					):






     """simple docstring"""





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



@require_torch
class       UpperCAmelCase_   (   __snake_case						,					__snake_case						,					unittest.TestCase       ):

   '''simple docstring'''




   __A					: List[str]							=						(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
   __A					: int							=						(
       {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification}
       if is_torch_available()
       else {}
   )

   __A					: List[Any]							=						False
   __A					: Union[str, Any]							=						False
   __A					: Optional[int]							=						False
   __A					: str							=						False






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Optional[Any]       =							TimesformerModelTester(self					)
     lowerCamelCase					:   Optional[int]       =							ConfigTester(
         self		,							config_class=__lowerCamelCase		,							has_text_modality=__lowerCamelCase		,							hidden_size=37					)






   def _snake_case       (						self		,							__A		,							__A		,							__A=False					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							copy.deepcopy(__lowerCamelCase					)

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

     return inputs_dict






   def _snake_case       (						self					):






     """simple docstring"""





     self.config_tester.run_common_tests()






   @unittest.skip(reason="TimeSformer does not use inputs_embeds"					)
   def _snake_case       (						self					):






     """simple docstring"""





     pass






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase					:   List[Any]       =							self.model_tester.prepare_config_and_inputs_for_common()

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






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase					:   Tuple       =							self.model_tester.prepare_config_and_inputs_for_common()

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

       lowerCamelCase					:   Optional[int]       =							["pixel_values"]
       self.assertListEqual(arg_names[:1]		,							__lowerCamelCase					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Optional[Any]       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Tuple       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_video_classification(*__lowerCamelCase					)






   @slow
   def _snake_case       (						self					):






     """simple docstring"""





     for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowerCamelCase					:   str       =							TimesformerModel.from_pretrained(__lowerCamelCase					)
       self.assertIsNotNone(__lowerCamelCase					)






   def _snake_case       (						self					):






     """simple docstring"""





     if not self.has_attentions:
       pass

     else:
       lowerCamelCase       ,      lowerCamelCase					:   Any       =							self.model_tester.prepare_config_and_inputs_for_common()
       lowerCamelCase					:   str       =							True

       for model_class in self.all_model_classes:
         lowerCamelCase					:   List[Any]       =							self.model_tester.seq_length
         lowerCamelCase					:   int       =							self.model_tester.num_frames

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

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

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

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

         lowerCamelCase					:   int       =							outputs.attentions

         self.assertEqual(len(__lowerCamelCase					)		,							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 _snake_case       (						self					):






     """simple docstring"""





     def check_hidden_states_output(__A		,							__A		,							__A					):
       lowerCamelCase					:   Any       =							model_class(__lowerCamelCase					)
       model.to(__lowerCamelCase					)
       model.eval()

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

       lowerCamelCase					:   str       =							outputs.hidden_states
       lowerCamelCase					:   Any       =							self.model_tester.num_hidden_layers + 1
       self.assertEqual(len(__lowerCamelCase					)		,							__lowerCamelCase					)

       lowerCamelCase					:   Optional[Any]       =							self.model_tester.seq_length

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

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

     for model_class in self.all_model_classes:
       lowerCamelCase					:   int       =							True
       check_hidden_states_output(__lowerCamelCase		,							__lowerCamelCase		,							__lowerCamelCase					)

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

       check_hidden_states_output(__lowerCamelCase		,							__lowerCamelCase		,							__lowerCamelCase					)






def 						lowercase_(    ):


  '''simple docstring'''

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







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

   '''simple docstring'''




   @cached_property
   def _snake_case       (						self					):






     """simple docstring"""





     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 _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Any       =							TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"					).to(
         __lowerCamelCase					)

     lowerCamelCase					:   int       =							self.default_image_processor
     lowerCamelCase					:   List[Any]       =							prepare_video()
     lowerCamelCase					:   Optional[Any]       =							image_processor(video[:8]		,							return_tensors="pt"					).to(__lowerCamelCase					)

     # forward pass
     with torch.no_grad():
       lowerCamelCase					:   int       =							model(**__lowerCamelCase					)

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

     lowerCamelCase					:   Tuple       =							torch.tensor([-0.3016, -0.7713, -0.4205]					).to(__lowerCamelCase					)

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






def 	__a     (      A__			:  float			,  A__			:  float       ):
    if mass < 0:
        raise ValueError("The mass of a body cannot be negative"       )
    return 0.5 * mass * abs(A__       ) * abs(A__       )


if __name__ == "__main__":
    import doctest

    doctest.testmod(verbose=True)