





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				,							_UpperCamelCase				,							_UpperCamelCase=13				,							_UpperCamelCase=10				,							_UpperCamelCase=3				,							_UpperCamelCase=2				,							_UpperCamelCase=2				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=32				,							_UpperCamelCase=5				,							_UpperCamelCase=4				,							_UpperCamelCase=37				,							_UpperCamelCase="gelu"				,							_UpperCamelCase=0.1				,							_UpperCamelCase=0.1				,							_UpperCamelCase=10				,							_UpperCamelCase=0.02				,							_UpperCamelCase="divided_space_time"				,							_UpperCamelCase=None				,							):
        _UpperCAmelCase           =   parent
        _UpperCAmelCase           =   batch_size
        _UpperCAmelCase           =   image_size
        _UpperCAmelCase           =   num_channels
        _UpperCAmelCase           =   patch_size
        _UpperCAmelCase           =   num_frames
        _UpperCAmelCase           =   is_training
        _UpperCAmelCase           =   use_labels
        _UpperCAmelCase           =   hidden_size
        _UpperCAmelCase           =   num_hidden_layers
        _UpperCAmelCase           =   num_attention_heads
        _UpperCAmelCase           =   intermediate_size
        _UpperCAmelCase           =   hidden_act
        _UpperCAmelCase           =   hidden_dropout_prob
        _UpperCAmelCase           =   attention_probs_dropout_prob
        _UpperCAmelCase           =   attention_type
        _UpperCAmelCase           =   initializer_range
        _UpperCAmelCase           =   scope
        _UpperCAmelCase           =   num_labels

        # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
        _UpperCAmelCase           =   (image_size // patch_size) ** 2
        _UpperCAmelCase           =   (num_frames) * self.num_patches_per_frame + 1





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   floats_tensor(
            [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

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

        _UpperCAmelCase           =   self.get_config()

        return config, pixel_values, labels





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   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				,							)
        _UpperCAmelCase           =   self.num_labels
        return config





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					):
        _UpperCAmelCase           =   TimesformerModel(config=_UpperCamelCase					)
        model.to(_UpperCamelCase					)
        model.eval()
        _UpperCAmelCase           =   model(_UpperCamelCase					)
        self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					):
        _UpperCAmelCase           =   TimesformerForVideoClassification(_UpperCamelCase					)
        model.to(_UpperCamelCase					)
        model.eval()

        _UpperCAmelCase           =   model(_UpperCamelCase					)

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





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.prepare_config_and_inputs()
        _UpperCAmelCase       ,      _UpperCAmelCase       ,      _UpperCAmelCase           =   config_and_inputs
        _UpperCAmelCase           =   {'''pixel_values''': pixel_values}
        return config, inputs_dict




@require_torch
class 			__UpperCamelCase    (							A__	, A__	, unittest.TestCase    ):
     __A	:  List[Any]							  =   (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
     __A	:  Union[str, Any]							  =   (
         {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
         if is_torch_available()
         else {}
     )

     __A	:  int							  =   False
     __A	:  List[str]							  =   False
     __A	:  int							  =   False
     __A	:  str							  =   False





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   TimesformerModelTester(self					)
        _UpperCAmelCase           =   ConfigTester(
            self				,							config_class=_UpperCamelCase				,							has_text_modality=_UpperCamelCase				,							hidden_size=37					)





     def 		UpperCamelCase(     self				,							_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase=False					):
        _UpperCAmelCase           =   copy.deepcopy(_UpperCamelCase					)

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

        return inputs_dict





     def 		UpperCamelCase(     self					):
        self.config_tester.run_common_tests()





     @unittest.skip(reason='''TimeSformer does not use inputs_embeds'''					)
     def 		UpperCamelCase(     self					):
        pass





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase       ,      _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs_for_common()

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





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase       ,      _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs_for_common()

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

           _UpperCAmelCase           =   ['''pixel_values''']
           self.assertListEqual(arg_names[:1]				,							_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_video_classification(*_UpperCamelCase					)





     @slow
     def 		UpperCamelCase(     self					):
        for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _UpperCAmelCase           =   TimesformerModel.from_pretrained(_UpperCamelCase					)
           self.assertIsNotNone(_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        if not self.has_attentions:
           pass

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

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

              _UpperCAmelCase           =   True
              _UpperCAmelCase           =   False
              _UpperCAmelCase           =   True
              _UpperCAmelCase           =   model_class(_UpperCamelCase					)
              model.to(_UpperCamelCase					)
              model.eval()
              with torch.no_grad():
                 _UpperCAmelCase           =   model(**self._prepare_for_class(_UpperCamelCase				,							_UpperCamelCase					)					)
              _UpperCAmelCase           =   outputs.attentions
              self.assertEqual(len(_UpperCamelCase					)				,							self.model_tester.num_hidden_layers					)

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

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

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

              _UpperCAmelCase           =   outputs.attentions

              self.assertEqual(len(_UpperCamelCase					)				,							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					):
        def check_hidden_states_output(_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					):
           _UpperCAmelCase           =   model_class(_UpperCamelCase					)
           model.to(_UpperCamelCase					)
           model.eval()

           with torch.no_grad():
              _UpperCAmelCase           =   model(**self._prepare_for_class(_UpperCamelCase				,							_UpperCamelCase					)					)

           _UpperCAmelCase           =   outputs.hidden_states
           _UpperCAmelCase           =   self.model_tester.num_hidden_layers + 1
           self.assertEqual(len(_UpperCamelCase					)				,							_UpperCamelCase					)

           _UpperCAmelCase           =   self.model_tester.seq_length

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

        _UpperCAmelCase       ,      _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           _UpperCAmelCase           =   True
           check_hidden_states_output(_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					)

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

           check_hidden_states_output(_UpperCamelCase				,							_UpperCamelCase				,							_UpperCamelCase					)




def      A__       (    )  ->  Optional[int]:






   """simple docstring"""







   _UpperCAmelCase           =   hf_hub_download(
       repo_id='''hf-internal-testing/spaghetti-video'''				,	filename='''eating_spaghetti.npy'''				,	repo_type='''dataset'''						)
   _UpperCAmelCase           =   np.load(SCREAMING_SNAKE_CASE_						)
   return list(SCREAMING_SNAKE_CASE_						)




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





     @cached_property
     def 		UpperCamelCase(     self					):
        # 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					):
        _UpperCAmelCase           =   TimesformerForVideoClassification.from_pretrained('''facebook/timesformer-base-finetuned-k400'''					).to(
            _UpperCamelCase					)

        _UpperCAmelCase           =   self.default_image_processor
        _UpperCAmelCase           =   prepare_video()
        _UpperCAmelCase           =   image_processor(video[:8]				,							return_tensors='''pt'''					).to(_UpperCamelCase					)

        # forward pass
        with torch.no_grad():
           _UpperCAmelCase           =   model(**_UpperCamelCase					)

        # verify the logits
        _UpperCAmelCase           =   torch.Size((1, 400)					)
        self.assertEqual(outputs.logits.shape				,							_UpperCamelCase					)

        _UpperCAmelCase           =   torch.tensor([-0.3016, -0.7713, -0.4205]					).to(_UpperCamelCase					)

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






from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class 			__UpperCamelCase    (							A__    ):
     __A	:  Dict							  =   ["""image_processor""", """tokenizer"""]
     __A	:  List[str]							  =   """BridgeTowerImageProcessor"""
     __A	:  str							  =   ("""RobertaTokenizer""", """RobertaTokenizerFast""")





     def __init__(     self				,							_UpperCamelCase				,							_UpperCamelCase					):
        super().__init__(_UpperCamelCase				,							_UpperCamelCase					)





     def __call__(     self				,							_UpperCamelCase				,							_UpperCamelCase = None				,							_UpperCamelCase = True				,							_UpperCamelCase = False				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = 0				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = None				,							_UpperCamelCase = False				,							_UpperCamelCase = False				,							_UpperCamelCase = False				,							_UpperCamelCase = False				,							_UpperCamelCase = True				,							_UpperCamelCase = None				,							**_UpperCamelCase				,							):
        _UpperCAmelCase           =   self.tokenizer(
            text=_UpperCamelCase				,							add_special_tokens=_UpperCamelCase				,							padding=_UpperCamelCase				,							truncation=_UpperCamelCase				,							max_length=_UpperCamelCase				,							stride=_UpperCamelCase				,							pad_to_multiple_of=_UpperCamelCase				,							return_token_type_ids=_UpperCamelCase				,							return_attention_mask=_UpperCamelCase				,							return_overflowing_tokens=_UpperCamelCase				,							return_special_tokens_mask=_UpperCamelCase				,							return_offsets_mapping=_UpperCamelCase				,							return_length=_UpperCamelCase				,							verbose=_UpperCamelCase				,							return_tensors=_UpperCamelCase				,							**_UpperCamelCase				,							)
        # add pixel_values + pixel_mask
        _UpperCAmelCase           =   self.image_processor(
            _UpperCamelCase				,							return_tensors=_UpperCamelCase				,							do_normalize=_UpperCamelCase				,							do_center_crop=_UpperCamelCase				,							**_UpperCamelCase					)
        encoding.update(_UpperCamelCase					)

        return encoding





     def 		UpperCamelCase(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        return self.tokenizer.batch_decode(*_UpperCamelCase				,							**_UpperCamelCase					)





     def 		UpperCamelCase(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        return self.tokenizer.decode(*_UpperCamelCase				,							**_UpperCamelCase					)





     @property
     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.tokenizer.model_input_names
        _UpperCAmelCase           =   self.image_processor.model_input_names
        return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names					)					)