






'''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 a_    :







   def __init__(self					,   __a					,   __a=1_3					,   __a=1_0					,   __a=3					,   __a=2					,   __a=2					,   __a=True					,   __a=True					,   __a=3_2					,   __a=5					,   __a=4					,   __a=3_7					,   __a="gelu"					,   __a=0.1					,   __a=0.1					,   __a=1_0					,   __a=0.02					,   __a="divided_space_time"					,   __a=None					,   )			->      str:
      """simple docstring"""






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

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







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[Any]:
      """simple docstring"""






      __snake_case				:      List[str]     							= floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size])

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

      __snake_case				:      int     							= self.get_config()

      return config, pixel_values, labels







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[Any]:
      """simple docstring"""






      __snake_case				:      List[Any]     							= TimesformerConfig(
          image_size=self.image_size					,   patch_size=self.patch_size					,   num_channels=self.num_channels					,   num_frames=self.num_frames					,   hidden_size=self.hidden_size					,   num_hidden_layers=self.num_hidden_layers					,   num_attention_heads=self.num_attention_heads					,   intermediate_size=self.intermediate_size					,   hidden_act=self.hidden_act					,   hidden_dropout_prob=self.hidden_dropout_prob					,   attention_probs_dropout_prob=self.attention_probs_dropout_prob					,   initializer_range=self.initializer_range					,   attention_type=self.attention_type					,   )
      __snake_case				:      str     							= self.num_labels
      return config







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a)			->      Tuple:
      """simple docstring"""






      __snake_case				:      Optional[Any]     							= TimesformerModel(config=__a)
      model.to(__a)
      model.eval()
      __snake_case				:      Optional[int]     							= model(__a)
      self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size))







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a)			->      Dict:
      """simple docstring"""






      __snake_case				:      str     							= TimesformerForVideoClassification(__a)
      model.to(__a)
      model.eval()

      __snake_case				:      Union[str, Any]     							= model(__a)

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







   def     SCREAMING_SNAKE_CASE__					(self)			->      Union[str, Any]:
      """simple docstring"""






      __snake_case				:      int     							= self.prepare_config_and_inputs()
      __snake_case							,__snake_case							,__snake_case				:      Optional[int]     							= config_and_inputs
      __snake_case				:      Dict     							= {'pixel_values': pixel_values}
      return config, inputs_dict




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

   _snake_case        =							False
   _snake_case        =							False
   _snake_case        =							False
   _snake_case        =							False







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[Any]:
      """simple docstring"""






      __snake_case				:      Tuple     							= TimesformerModelTester(self)
      __snake_case				:      str     							= ConfigTester(
          self					,   config_class=__a					,   has_text_modality=__a					,   hidden_size=3_7)







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a=False)			->      Tuple:
      """simple docstring"""






      __snake_case				:      Tuple     							= copy.deepcopy(__a)

      if return_labels:
         if model_class in get_values(__a):
            __snake_case				:      Tuple     							= torch.zeros(
                self.model_tester.batch_size					,   dtype=torch.long					,   device=__a)

      return inputs_dict







   def     SCREAMING_SNAKE_CASE__					(self)			->      List[str]:
      """simple docstring"""






      self.config_tester.run_common_tests()







   @unittest.skip(reason='TimeSformer does not use inputs_embeds')
   def     SCREAMING_SNAKE_CASE__					(self)			->      str:
      """simple docstring"""






      pass







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[int]:
      """simple docstring"""






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

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







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[Any]:
      """simple docstring"""






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

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

         __snake_case				:      Union[str, Any]     							= ['pixel_values']
         self.assertListEqual(arg_names[:1]					,   __a)







   def     SCREAMING_SNAKE_CASE__					(self)			->      Dict:
      """simple docstring"""






      __snake_case				:      str     							= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__a)







   def     SCREAMING_SNAKE_CASE__					(self)			->      str:
      """simple docstring"""






      __snake_case				:      Optional[Any]     							= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_video_classification(*__a)







   @slow
   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[Any]:
      """simple docstring"""






      for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __snake_case				:      int     							= TimesformerModel.from_pretrained(__a)
         self.assertIsNotNone(__a)







   def     SCREAMING_SNAKE_CASE__					(self)			->      str:
      """simple docstring"""






      if not self.has_attentions:
         pass

      else:
         __snake_case							,__snake_case				:      Any     							= self.model_tester.prepare_config_and_inputs_for_common()
         __snake_case				:      int     							= True

         for model_class in self.all_model_classes:
            __snake_case				:      Tuple     							= self.model_tester.seq_length
            __snake_case				:      str     							= self.model_tester.num_frames

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

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

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

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

            __snake_case				:      Optional[Any]     							= outputs.attentions

            self.assertEqual(len(__a)					,   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     SCREAMING_SNAKE_CASE__					(self)			->      List[str]:
      """simple docstring"""






      def check_hidden_states_output(__a					,   __a					,   __a):
         __snake_case				:      List[str]     							= model_class(__a)
         model.to(__a)
         model.eval()

         with torch.no_grad():
            __snake_case				:      List[Any]     							= model(**self._prepare_for_class(__a					,   __a))

         __snake_case				:      int     							= outputs.hidden_states
         __snake_case				:      List[Any]     							= self.model_tester.num_hidden_layers + 1
         self.assertEqual(len(__a)					,   __a)

         __snake_case				:      int     							= self.model_tester.seq_length

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

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

      for model_class in self.all_model_classes:
         __snake_case				:      Optional[Any]     							= True
         check_hidden_states_output(__a					,   __a					,   __a)

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

         check_hidden_states_output(__a					,   __a					,   __a)




def        _SCREAMING_SNAKE_CASE  (				)    -> Dict:







   """simple docstring"""
   __snake_case				:      Optional[int]     							= hf_hub_download(
       repo_id='hf-internal-testing/spaghetti-video'  ,		filename='eating_spaghetti.npy'  ,		repo_type='dataset'  )
   __snake_case				:      Dict     							= np.load(A  )
   return list(A  )





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







   @cached_property
   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[Any]:
      """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     SCREAMING_SNAKE_CASE__					(self)			->      List[str]:
      """simple docstring"""






      __snake_case				:      List[Any]     							= TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400').to(
          __a)

      __snake_case				:      Optional[Any]     							= self.default_image_processor
      __snake_case				:      Optional[Any]     							= prepare_video()
      __snake_case				:      Optional[int]     							= image_processor(video[:8]					,   return_tensors='pt').to(__a)

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

      # verify the logits
      __snake_case				:      Dict     							= torch.Size((1, 4_0_0))
      self.assertEqual(outputs.logits.shape					,   __a)

      __snake_case				:      Tuple     							= torch.tensor([-0.3_016, -0.7_713, -0.4_205]).to(__a)

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







'''simple docstring'''






import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
      from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
      from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
      from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


__A    	=   DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

__A    	=   '''main'''
# Default branch name
__A    	=   '''f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'''
# One particular commit (not the top of `main`)
__A    	=   '''aaaaaaa'''
# This commit does not exist, so we should 404.

__A    	=   '''d9e9f15bc825e4b2c9249e9578f884bbcb5e3684'''
# Sha-1 of config.json on the top of `main`, for checking purposes
__A    	=   '''4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'''




@contextlib.contextmanager
def        _SCREAMING_SNAKE_CASE  (				)    -> List[Any]:







   """simple docstring"""
   print('Welcome!'  )
   yield
   print('Bye!'  )




@contextlib.contextmanager
def        _SCREAMING_SNAKE_CASE  (				)    -> Optional[Any]:







   """simple docstring"""
   print('Bonjour!'  )
   yield
   print('Au revoir!'  )




class a_    (    unittest.TestCase							):







   def     SCREAMING_SNAKE_CASE__					(self)			->      int:
      """simple docstring"""






      assert transformers.__spec__ is not None
      assert importlib.util.find_spec('transformers') is not None




class a_    (    unittest.TestCase							):







   @unittest.mock.patch('sys.stdout'					,   new_callable=io.StringIO)
   def     SCREAMING_SNAKE_CASE__					(self					,   __a)			->      int:
      """simple docstring"""






      with ContextManagers([]):
         print('Transformers are awesome!')
      # The print statement adds a new line at the end of the output
      self.assertEqual(mock_stdout.getvalue()					,   'Transformers are awesome!\n')







   @unittest.mock.patch('sys.stdout'					,   new_callable=io.StringIO)
   def     SCREAMING_SNAKE_CASE__					(self					,   __a)			->      List[str]:
      """simple docstring"""






      with ContextManagers([context_en()]):
         print('Transformers are awesome!')
      # The output should be wrapped with an English welcome and goodbye
      self.assertEqual(mock_stdout.getvalue()					,   'Welcome!\nTransformers are awesome!\nBye!\n')







   @unittest.mock.patch('sys.stdout'					,   new_callable=io.StringIO)
   def     SCREAMING_SNAKE_CASE__					(self					,   __a)			->      Tuple:
      """simple docstring"""






      with ContextManagers([context_fr(), context_en()]):
         print('Transformers are awesome!')
      # The output should be wrapped with an English and French welcome and goodbye
      self.assertEqual(mock_stdout.getvalue()					,   'Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n')







   @require_torch
   def     SCREAMING_SNAKE_CASE__					(self)			->      Union[str, Any]:
      """simple docstring"""






      self.assertEqual(find_labels(__a)					,   ['labels'])
      self.assertEqual(find_labels(__a)					,   ['labels', 'next_sentence_label'])
      self.assertEqual(find_labels(__a)					,   ['start_positions', 'end_positions'])




      class a_    (    UpperCamelCase_							):
         pass


      self.assertEqual(find_labels(__a)					,   ['labels'])







   @require_tf
   def     SCREAMING_SNAKE_CASE__					(self)			->      List[Any]:
      """simple docstring"""






      self.assertEqual(find_labels(__a)					,   ['labels'])
      self.assertEqual(find_labels(__a)					,   ['labels', 'next_sentence_label'])
      self.assertEqual(find_labels(__a)					,   ['start_positions', 'end_positions'])




      class a_    (    UpperCamelCase_							):
         pass


      self.assertEqual(find_labels(__a)					,   ['labels'])







   @require_flax
   def     SCREAMING_SNAKE_CASE__					(self)			->      int:
      """simple docstring"""






      self.assertEqual(find_labels(__a)					,   [])
      self.assertEqual(find_labels(__a)					,   [])
      self.assertEqual(find_labels(__a)					,   [])




      class a_    (    UpperCamelCase_							):
         pass


      self.assertEqual(find_labels(__a)					,   [])