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







    """simple docstring"""



    def __init__(       self   :  Optional[Any]   ,						lowerCamelCase   :  int   ,						lowerCamelCase   :  Tuple=13   ,						lowerCamelCase   :  Tuple=10   ,						lowerCamelCase   :  Optional[Any]=3   ,						lowerCamelCase   :  str=2   ,						lowerCamelCase   :  Dict=2   ,						lowerCamelCase   :  Optional[Any]=True   ,						lowerCamelCase   :  int=True   ,						lowerCamelCase   :  Union[str, Any]=32   ,						lowerCamelCase   :  Tuple=5   ,						lowerCamelCase   :  Optional[int]=4   ,						lowerCamelCase   :  Optional[Any]=37   ,						lowerCamelCase   :  Any="gelu"   ,						lowerCamelCase   :  Tuple=0.1   ,						lowerCamelCase   :  str=0.1   ,						lowerCamelCase   :  List[Any]=10   ,						lowerCamelCase   :  Dict=0.02   ,						lowerCamelCase   :  Tuple="divided_space_time"   ,						lowerCamelCase   :  Optional[int]=None   ,						)       ->  Optional[int]:
      lowerCAmelCase_      :				Dict      =       parent
      lowerCAmelCase_      :				Tuple      =       batch_size
      lowerCAmelCase_      :				int      =       image_size
      lowerCAmelCase_      :				List[Any]      =       num_channels
      lowerCAmelCase_      :				Dict      =       patch_size
      lowerCAmelCase_      :				List[Any]      =       num_frames
      lowerCAmelCase_      :				List[str]      =       is_training
      lowerCAmelCase_      :				Tuple      =       use_labels
      lowerCAmelCase_      :				List[str]      =       hidden_size
      lowerCAmelCase_      :				Union[str, Any]      =       num_hidden_layers
      lowerCAmelCase_      :				Optional[int]      =       num_attention_heads
      lowerCAmelCase_      :				Union[str, Any]      =       intermediate_size
      lowerCAmelCase_      :				Tuple      =       hidden_act
      lowerCAmelCase_      :				Union[str, Any]      =       hidden_dropout_prob
      lowerCAmelCase_      :				Optional[Any]      =       attention_probs_dropout_prob
      lowerCAmelCase_      :				str      =       attention_type
      lowerCAmelCase_      :				Optional[Any]      =       initializer_range
      lowerCAmelCase_      :				Optional[int]      =       scope
      lowerCAmelCase_      :				str      =       num_labels

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

    def __lowercase       (       self   :  Optional[Any]		)       ->  Union[str, Any]:
      lowerCAmelCase_      :				str      =       floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]		)

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

      lowerCAmelCase_      :				Any      =       self.get_config()

      return config, pixel_values, labels

    def __lowercase       (       self   :  Dict		)       ->  Any:
      lowerCAmelCase_      :				Dict      =       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_      :				Any      =       self.num_labels
      return config

    def __lowercase       (       self   :  Optional[int]   ,						lowerCamelCase   :  Tuple   ,						lowerCamelCase   :  List[Any]   ,						lowerCamelCase   :  List[str]		)       ->  int:
      lowerCAmelCase_      :				Optional[int]      =       TimesformerModel(config=lowerCAmelCase__		)
      model.to(lowerCAmelCase__		)
      model.eval()
      lowerCAmelCase_      :				Union[str, Any]      =       model(lowerCAmelCase__		)
      self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, self.seq_length, self.hidden_size)		)

    def __lowercase       (       self   :  Dict   ,						lowerCamelCase   :  str   ,						lowerCamelCase   :  List[Any]   ,						lowerCamelCase   :  Union[str, Any]		)       ->  Union[str, Any]:
      lowerCAmelCase_      :				str      =       TimesformerForVideoClassification(lowerCAmelCase__		)
      model.to(lowerCAmelCase__		)
      model.eval()

      lowerCAmelCase_      :				Optional[Any]      =       model(lowerCAmelCase__		)

      # verify the logits shape
      lowerCAmelCase_      :				Union[str, Any]      =       torch.Size((self.batch_size, self.num_labels)		)
      self.parent.assertEqual(result.logits.shape   ,						lowerCAmelCase__		)





    def __lowercase       (       self   :  int		)       ->  List[str]:
      lowerCAmelCase_      :				Dict      =       self.prepare_config_and_inputs()
      lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_      :				int      =       config_and_inputs
      lowerCAmelCase_      :				List[str]      =       {"""pixel_values""": pixel_values}
      return config, inputs_dict







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







    """simple docstring"""



    lowercase       			= (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
    lowercase       			= (
        {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
        if is_torch_available()
        else {}
    )

    lowercase       			= False
    lowercase       			= False
    lowercase       			= False
    lowercase       			= False

    def __lowercase       (       self   :  Tuple		)       ->  Optional[Any]:
      lowerCAmelCase_      :				Union[str, Any]      =       TimesformerModelTester(self		)
      lowerCAmelCase_      :				int      =       ConfigTester(
          self   ,						config_class=lowerCAmelCase__   ,						has_text_modality=lowerCAmelCase__   ,						hidden_size=37		)

    def __lowercase       (       self   :  Union[str, Any]   ,						lowerCamelCase   :  Dict   ,						lowerCamelCase   :  Optional[Any]   ,						lowerCamelCase   :  List[str]=False		)       ->  str:
      lowerCAmelCase_      :				str      =       copy.deepcopy(lowerCAmelCase__		)

      if return_labels:
        if model_class in get_values(lowerCAmelCase__		):
          lowerCAmelCase_      :				Any      =       torch.zeros(
              self.model_tester.batch_size   ,						dtype=torch.long   ,						device=lowerCAmelCase__		)

      return inputs_dict

    def __lowercase       (       self   :  Union[str, Any]		)       ->  List[Any]:
      self.config_tester.run_common_tests()

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

    def __lowercase       (       self   :  Tuple		)       ->  Union[str, Any]:
      lowerCAmelCase_,							lowerCAmelCase_      :				Optional[int]      =       self.model_tester.prepare_config_and_inputs_for_common()

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

    def __lowercase       (       self   :  Dict		)       ->  int:
      lowerCAmelCase_,							lowerCAmelCase_      :				Union[str, Any]      =       self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCAmelCase_      :				Union[str, Any]      =       model_class(lowerCAmelCase__		)
        lowerCAmelCase_      :				str      =       inspect.signature(model.forward		)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        lowerCAmelCase_      :				List[Any]      =       [*signature.parameters.keys()]

        lowerCAmelCase_      :				List[Any]      =       ["""pixel_values"""]
        self.assertListEqual(arg_names[:1]   ,						lowerCAmelCase__		)

    def __lowercase       (       self   :  Any		)       ->  Optional[Any]:
      lowerCAmelCase_      :				int      =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCAmelCase__		)

    def __lowercase       (       self   :  List[Any]		)       ->  List[str]:
      lowerCAmelCase_      :				int      =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_video_classification(*lowerCAmelCase__		)

    @slow
    def __lowercase       (       self   :  str		)       ->  List[Any]:
      for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCAmelCase_      :				int      =       TimesformerModel.from_pretrained(lowerCAmelCase__		)
        self.assertIsNotNone(lowerCAmelCase__		)

    def __lowercase       (       self   :  Union[str, Any]		)       ->  List[Any]:
      if not self.has_attentions:
        pass

      else:
        lowerCAmelCase_,							lowerCAmelCase_      :				List[Any]      =       self.model_tester.prepare_config_and_inputs_for_common()
        lowerCAmelCase_      :				Any      =       True

        for model_class in self.all_model_classes:
          lowerCAmelCase_      :				Any      =       self.model_tester.seq_length
          lowerCAmelCase_      :				str      =       self.model_tester.num_frames

          lowerCAmelCase_      :				Tuple      =       True
          lowerCAmelCase_      :				Optional[Any]      =       False
          lowerCAmelCase_      :				int      =       True
          lowerCAmelCase_      :				int      =       model_class(lowerCAmelCase__		)
          model.to(lowerCAmelCase__		)
          model.eval()
          with torch.no_grad():
            lowerCAmelCase_      :				List[str]      =       model(**self._prepare_for_class(lowerCAmelCase__   ,						lowerCAmelCase__		)		)
          lowerCAmelCase_      :				List[str]      =       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_      :				List[Any]      =       True
          lowerCAmelCase_      :				Optional[int]      =       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.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_      :				List[str]      =       len(lowerCAmelCase__		)

          # Check attention is always last and order is fine
          lowerCAmelCase_      :				Tuple      =       True
          lowerCAmelCase_      :				Optional[Any]      =       True
          lowerCAmelCase_      :				Tuple      =       model_class(lowerCAmelCase__		)
          model.to(lowerCAmelCase__		)
          model.eval()
          with torch.no_grad():
            lowerCAmelCase_      :				List[Any]      =       model(**self._prepare_for_class(lowerCAmelCase__   ,						lowerCAmelCase__		)		)

          self.assertEqual(out_len + 1   ,						len(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(self_attentions[0].shape[-3:]		)   ,						[self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]   ,						)





    def __lowercase       (       self   :  Optional[int]		)       ->  int:
      def check_hidden_states_output(lowerCamelCase   :  Tuple   ,						lowerCamelCase   :  List[str]   ,						lowerCamelCase   :  str		):
        lowerCAmelCase_      :				Optional[int]      =       model_class(lowerCAmelCase__		)
        model.to(lowerCAmelCase__		)
        model.eval()

        with torch.no_grad():
          lowerCAmelCase_      :				Optional[int]      =       model(**self._prepare_for_class(lowerCAmelCase__   ,						lowerCAmelCase__		)		)

        lowerCAmelCase_      :				Optional[int]      =       outputs.hidden_states
        lowerCAmelCase_      :				Union[str, 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_      :				Optional[Any]      =       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     UpperCamelCase_      (   ):
  '''simple docstring'''





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






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







    """simple docstring"""



    @cached_property
    def __lowercase       (       self   :  List[str]		)       ->  List[Any]:
      # 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 __lowercase       (       self   :  Union[str, Any]		)       ->  Tuple:
      lowerCAmelCase_      :				str      =       TimesformerForVideoClassification.from_pretrained("""facebook/timesformer-base-finetuned-k400"""		).to(
          lowerCAmelCase__		)

      lowerCAmelCase_      :				Optional[Any]      =       self.default_image_processor
      lowerCAmelCase_      :				str      =       prepare_video()
      lowerCAmelCase_      :				Union[str, Any]      =       image_processor(video[:8]   ,						return_tensors="""pt"""		).to(lowerCAmelCase__		)

      # forward pass
      with torch.no_grad():
        lowerCAmelCase_      :				Union[str, Any]      =       model(**lowerCAmelCase__		)

      # verify the logits
      lowerCAmelCase_      :				Any      =       torch.Size((1, 4_00)		)
      self.assertEqual(outputs.logits.shape   ,						lowerCAmelCase__		)

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

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







'''simple docstring'''






from typing import TYPE_CHECKING

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


__A :  List[Any]    							=				{
    "configuration_roc_bert": ["ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoCBertConfig"],
    "tokenization_roc_bert": ["RoCBertTokenizer"],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  pass

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __A :  Union[str, Any]    							=				[
      "ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "RoCBertForCausalLM",
      "RoCBertForMaskedLM",
      "RoCBertForMultipleChoice",
      "RoCBertForPreTraining",
      "RoCBertForQuestionAnswering",
      "RoCBertForSequenceClassification",
      "RoCBertForTokenClassification",
      "RoCBertLayer",
      "RoCBertModel",
      "RoCBertPreTrainedModel",
      "load_tf_weights_in_roc_bert",
  ]

if TYPE_CHECKING:
  from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig
  from .tokenization_roc_bert import RoCBertTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    raise OptionalDependencyNotAvailable()

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_roc_bert import (
        ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        RoCBertForCausalLM,
        RoCBertForMaskedLM,
        RoCBertForMultipleChoice,
        RoCBertForPreTraining,
        RoCBertForQuestionAnswering,
        RoCBertForSequenceClassification,
        RoCBertForTokenClassification,
        RoCBertLayer,
        RoCBertModel,
        RoCBertPreTrainedModel,
        load_tf_weights_in_roc_bert,
    )


else:
  import sys

  __A :  int    							=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



