



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







       '''simple docstring'''


       def __init__(							self      :  Optional[Any]		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  int=13		,    _UpperCAmelCase      :  int=10		,    _UpperCAmelCase      :  List[Any]=3		,    _UpperCAmelCase      :  Optional[int]=2		,    _UpperCAmelCase      :  Optional[int]=2		,    _UpperCAmelCase      :  Tuple=True		,    _UpperCAmelCase      :  Dict=True		,    _UpperCAmelCase      :  Optional[Any]=32		,    _UpperCAmelCase      :  Union[str, Any]=5		,    _UpperCAmelCase      :  int=4		,    _UpperCAmelCase      :  Any=37		,    _UpperCAmelCase      :  int="gelu"		,    _UpperCAmelCase      :  Optional[Any]=0.1		,    _UpperCAmelCase      :  Tuple=0.1		,    _UpperCAmelCase      :  Dict=10		,    _UpperCAmelCase      :  Optional[int]=0.02		,    _UpperCAmelCase      :  Dict="divided_space_time"		,    _UpperCAmelCase      :  List[Any]=None		,    )			->						Union[str, Any]:


            '''simple docstring'''




            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 					lowercase__    (							self      :  List[str]   )			->						Optional[int]:


            '''simple docstring'''




            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 					lowercase__    (							self      :  Union[str, Any]   )			->						Optional[int]:


            '''simple docstring'''




            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 					lowercase__    (							self      :  List[Any]		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Tuple   )			->						Optional[int]:


            '''simple docstring'''




            UpperCAmelCase_      			=							TimesformerModel(config=lowercase_   )
            model.to(lowercase_   )
            model.eval()
            UpperCAmelCase_      			=							model(lowercase_   )
            self.parent.assertEqual(result.last_hidden_state.shape		,    (self.batch_size, self.seq_length, self.hidden_size)   )



       def 					lowercase__    (							self      :  Any		,    _UpperCAmelCase      :  str		,    _UpperCAmelCase      :  Union[str, Any]		,    _UpperCAmelCase      :  Any   )			->						List[Any]:


            '''simple docstring'''




            UpperCAmelCase_      			=							TimesformerForVideoClassification(lowercase_   )
            model.to(lowercase_   )
            model.eval()

            UpperCAmelCase_      			=							model(lowercase_   )

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







       def 					lowercase__    (							self      :  Optional[Any]   )			->						Tuple:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.prepare_config_and_inputs()
            UpperCAmelCase_			,   UpperCAmelCase_			,   UpperCAmelCase_      			=							config_and_inputs
            UpperCAmelCase_      			=							{"pixel_values": pixel_values}
            return config, inputs_dict



@require_torch
class 				lowercase__       (   SCREAMING_SNAKE_CASE    , SCREAMING_SNAKE_CASE    , unittest.TestCase				):







       '''simple docstring'''


       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 					lowercase__    (							self      :  List[Any]   )			->						Union[str, Any]:


            '''simple docstring'''




            UpperCAmelCase_      			=							TimesformerModelTester(self   )
            UpperCAmelCase_      			=							ConfigTester(
                self		,    config_class=lowercase_		,    has_text_modality=lowercase_		,    hidden_size=37   )



       def 					lowercase__    (							self      :  Union[str, Any]		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  Optional[Any]=False   )			->						Dict:


            '''simple docstring'''




            UpperCAmelCase_      			=							copy.deepcopy(lowercase_   )

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

            return inputs_dict



       def 					lowercase__    (							self      :  str   )			->						List[str]:


            '''simple docstring'''




            self.config_tester.run_common_tests()



       @unittest.skip(reason="TimeSformer does not use inputs_embeds"   )
       def 					lowercase__    (							self      :  Optional[int]   )			->						Union[str, Any]:


            '''simple docstring'''




            pass



       def 					lowercase__    (							self      :  List[Any]   )			->						Union[str, Any]:


            '''simple docstring'''




            UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs_for_common()

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



       def 					lowercase__    (							self      :  List[str]   )			->						Any:


            '''simple docstring'''




            UpperCAmelCase_			,   UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 UpperCAmelCase_      			=							model_class(lowercase_   )
                 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]		,    lowercase_   )



       def 					lowercase__    (							self      :  Optional[int]   )			->						Any:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowercase_   )



       def 					lowercase__    (							self      :  str   )			->						Dict:


            '''simple docstring'''




            UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_video_classification(*lowercase_   )



       @slow
       def 					lowercase__    (							self      :  Dict   )			->						Dict:


            '''simple docstring'''




            for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 UpperCAmelCase_      			=							TimesformerModel.from_pretrained(lowercase_   )
                 self.assertIsNotNone(lowercase_   )



       def 					lowercase__    (							self      :  Optional[Any]   )			->						Union[str, Any]:


            '''simple docstring'''




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

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

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

                      UpperCAmelCase_      			=							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 					lowercase__    (							self      :  Tuple   )			->						str:


            '''simple docstring'''




            def check_hidden_states_output(_UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  Union[str, Any]		,    _UpperCAmelCase      :  str   ):
                 UpperCAmelCase_      			=							model_class(lowercase_   )
                 model.to(lowercase_   )
                 model.eval()

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

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

                 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(lowercase_		,    lowercase_		,    lowercase_   )

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

                 check_hidden_states_output(lowercase_		,    lowercase_		,    lowercase_   )



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


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







       '''simple docstring'''


       @cached_property
       def 					lowercase__    (							self      :  str   )			->						Optional[int]:


            '''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 					lowercase__    (							self      :  str   )			->						Tuple:


            '''simple docstring'''




            UpperCAmelCase_      			=							TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"   ).to(
                lowercase_   )

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

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

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

            UpperCAmelCase_      			=							torch.tensor([-0.3016, -0.7713, -0.4205]   ).to(lowercase_   )

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









"""simple docstring"""





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
  import torch

  from transformers import XLMRobertaModel
@require_sentencepiece
@require_tokenizers
@require_torch
class 				lowercase__       (   unittest.TestCase				):







       '''simple docstring'''


       @slow
       def 					lowercase__    (							self      :  Tuple   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							XLMRobertaModel.from_pretrained("xlm-roberta-base"   )
             UpperCAmelCase_      			=							torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]   )
             # The dog is cute and lives in the garden house

             UpperCAmelCase_      			=							torch.Size((1, 12, 768)   )  # batch_size, sequence_length, embedding_vector_dim
             UpperCAmelCase_      			=							torch.tensor(
                 [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]]   )
             #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
             #  xlmr.eval()
             #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
             with torch.no_grad():
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )["last_hidden_state"].detach()
             self.assertEqual(output.shape		,    _UpperCAmelCase   )
             # compare the actual values for a slice of last dim
             self.assertTrue(torch.allclose(output[:, :, -1]		,    _UpperCAmelCase		,    atol=1e-3   )   )







       @slow
       def 					lowercase__    (							self      :  List[Any]   )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							XLMRobertaModel.from_pretrained("xlm-roberta-large"   )
             UpperCAmelCase_      			=							torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]   )
             # The dog is cute and lives in the garden house

             UpperCAmelCase_      			=							torch.Size((1, 12, 1024)   )  # batch_size, sequence_length, embedding_vector_dim
             UpperCAmelCase_      			=							torch.tensor(
                 [[-0.0699, -0.0318, 0.0705, -0.1241, 0.0999, -0.0520, 0.1004, -0.1838, -0.4704, 0.1437, 0.0821, 0.0126]]   )
             #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
             #  xlmr.eval()
             #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
             with torch.no_grad():
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )["last_hidden_state"].detach()
             self.assertEqual(output.shape		,    _UpperCAmelCase   )
             # compare the actual values for a slice of last dim
             self.assertTrue(torch.allclose(output[:, :, -1]		,    _UpperCAmelCase		,    atol=1e-3   )   )




