





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






 '''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,		)	->       Optional[int]:
    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		)	->       List[str]:
    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		)	->       Dict:
    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,		A_,		A_,		A_		)	->       Union[str, Any]:
    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,		A_,		A_,		A_		)	->       Optional[Any]:
    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		)	->       Union[str, Any]:
    UpperCAmelCase__						=self.prepare_config_and_inputs()
    UpperCAmelCase__			,							UpperCAmelCase__			,							UpperCAmelCase__						=config_and_inputs
    UpperCAmelCase__						={"pixel_values": pixel_values}
    return config, inputs_dict

@require_torch
class     snake_case_		(   A__,		A__,		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  __UpperCAmelCase						(  self		)	->       Union[str, Any]:
    UpperCAmelCase__						=TimesformerModelTester(self		)
    UpperCAmelCase__						=ConfigTester(
        self,		config_class=_UpperCamelCase,		has_text_modality=_UpperCamelCase,		hidden_size=37		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_=False		)	->       Optional[Any]:
    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		)	->       List[str]:
    self.config_tester.run_common_tests()

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

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    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		)	->       Dict:
    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		)	->       List[str]:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_UpperCamelCase		)

 def  __UpperCAmelCase						(  self		)	->       Union[str, Any]:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_video_classification(*_UpperCamelCase		)

 @slow
 def  __UpperCAmelCase						(  self		)	->       int:
    for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase__						=TimesformerModel.from_pretrained(_UpperCamelCase		)
       self.assertIsNotNone(_UpperCamelCase		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    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		)	->       str:
    def check_hidden_states_output(A_,		A_,		A_		):
       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 				_UpperCAmelCase			(    ):




   '''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     snake_case_		(   unittest.TestCase							):






 '''simple docstring'''





 @cached_property
 def  __UpperCAmelCase						(  self		)	->       int:
    # 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		)	->       int:
    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.30_16, -0.77_13, -0.42_05]		).to(_UpperCamelCase		)

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










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase_           =	{
    "configuration_xlm": ["XLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMConfig", "XLMOnnxConfig"],
    "tokenization_xlm": ["XLMTokenizer"],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase_           =	[
       "XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
       "XLMForMultipleChoice",
       "XLMForQuestionAnswering",
       "XLMForQuestionAnsweringSimple",
       "XLMForSequenceClassification",
       "XLMForTokenClassification",
       "XLMModel",
       "XLMPreTrainedModel",
       "XLMWithLMHeadModel",
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase_           =	[
       "TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
       "TFXLMForMultipleChoice",
       "TFXLMForQuestionAnsweringSimple",
       "TFXLMForSequenceClassification",
       "TFXLMForTokenClassification",
       "TFXLMMainLayer",
       "TFXLMModel",
       "TFXLMPreTrainedModel",
       "TFXLMWithLMHeadModel",
   ]


if TYPE_CHECKING:
   from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
   from .tokenization_xlm import XLMTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_xlm import (
          XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
          XLMForMultipleChoice,
          XLMForQuestionAnswering,
          XLMForQuestionAnsweringSimple,
          XLMForSequenceClassification,
          XLMForTokenClassification,
          XLMModel,
          XLMPreTrainedModel,
          XLMWithLMHeadModel,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_xlm import (
          TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFXLMForMultipleChoice,
          TFXLMForQuestionAnsweringSimple,
          TFXLMForSequenceClassification,
          TFXLMForTokenClassification,
          TFXLMMainLayer,
          TFXLMModel,
          TFXLMPreTrainedModel,
          TFXLMWithLMHeadModel,
      )

else:
   import sys

   UpperCAmelCase_           =	_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)