


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

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

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

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

      lowerCAmelCase_			:      Optional[Any]			=   self.get_config()

      return config, pixel_values, labels

    def __lowercase   (   self    :  List[Any]		)   ->  str:
      lowerCAmelCase_			:      Union[str, 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    ,						)
      lowerCAmelCase_			:      str			=   self.num_labels
      return config

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

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

      lowerCAmelCase_			:      List[str]			=   model(lowerCamelCase		)

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





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







@require_torch
class    __snake_case       (   _SCREAMING_SNAKE_CASE	,_SCREAMING_SNAKE_CASE	,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    :  Any		)   ->  Tuple:
      lowerCAmelCase_			:      Optional[Any]			=   TimesformerModelTester(self		)
      lowerCAmelCase_			:      Optional[int]			=   ConfigTester(
          self    ,						config_class=lowerCamelCase    ,						has_text_modality=lowerCamelCase    ,						hidden_size=37		)

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

      if return_labels:
        if model_class in get_values(lowerCamelCase		):
          lowerCAmelCase_			:      Union[str, Any]			=   torch.zeros(
              self.model_tester.batch_size    ,						dtype=torch.long    ,						device=lowerCamelCase		)

      return inputs_dict

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

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

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

      for model_class in self.all_model_classes:
        lowerCAmelCase_			:      List[Any]			=   model_class(lowerCamelCase		)
        self.assertIsInstance(model.get_input_embeddings()    ,						(nn.Module)		)
        lowerCAmelCase_			:      Any			=   model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(lowerCamelCase    ,						nn.Linear		)		)

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

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

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

    def __lowercase   (   self    :  List[str]		)   ->  Tuple:
      lowerCAmelCase_			:      List[Any]			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase		)

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

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

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

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

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

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

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

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

          lowerCAmelCase_			:      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    :  Union[str, Any]		)   ->  Any:
      def check_hidden_states_output(lowerCamelCase    :  int    ,						lowerCamelCase    :  Optional[Any]    ,						lowerCamelCase    :  List[str]		):
        lowerCAmelCase_			:      int			=   model_class(lowerCamelCase		)
        model.to(lowerCamelCase		)
        model.eval()

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

        lowerCAmelCase_			:      Union[str, Any]			=   outputs.hidden_states
        lowerCAmelCase_			:      List[str]			=   self.model_tester.num_hidden_layers + 1
        self.assertEqual(len(lowerCamelCase		)    ,						lowerCamelCase		)

        lowerCAmelCase_			:      Tuple			=   self.model_tester.seq_length

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

      lowerCAmelCase_,							lowerCAmelCase_			:      Any			=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        lowerCAmelCase_			:      str			=   True
        check_hidden_states_output(lowerCamelCase    ,						lowerCamelCase    ,						lowerCamelCase		)

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        lowerCAmelCase_			:      Optional[Any]			=   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(A__					)
  return list(A__					)






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







    """simple docstring"""



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

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

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

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

      lowerCAmelCase_			:      Optional[int]			=   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 ...configuration_utils import PretrainedConfig
from ...utils import logging


__A					:  List[Any]											=      logging.get_logger(__name__)

__A					:  Optional[Any]											=      {
    "EleutherAI/gpt-neox-20b": "https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json",
    # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox
}






class    __snake_case       (   _SCREAMING_SNAKE_CASE):







    """simple docstring"""



    lowercase    =					'gpt_neox'

    def __init__(   self    :  Optional[int]    ,						lowerCamelCase    :  Tuple=5_04_32    ,						lowerCamelCase    :  Optional[int]=61_44    ,						lowerCamelCase    :  Tuple=44    ,						lowerCamelCase    :  Any=64    ,						lowerCamelCase    :  List[Any]=2_45_76    ,						lowerCamelCase    :  List[Any]="gelu"    ,						lowerCamelCase    :  Optional[Any]=0.25    ,						lowerCamelCase    :  Any=1_00_00    ,						lowerCamelCase    :  Any=0.0    ,						lowerCamelCase    :  str=0.0    ,						lowerCamelCase    :  Optional[int]=0.1    ,						lowerCamelCase    :  List[Any]=20_48    ,						lowerCamelCase    :  List[Any]=0.02    ,						lowerCamelCase    :  Any=1E-5    ,						lowerCamelCase    :  Dict=True    ,						lowerCamelCase    :  Optional[int]=0    ,						lowerCamelCase    :  List[str]=2    ,						lowerCamelCase    :  Dict=False    ,						lowerCamelCase    :  Tuple=True    ,						lowerCamelCase    :  Optional[int]=None    ,						**lowerCamelCase    :  int    ,						)   ->  Optional[Any]:
      super().__init__(bos_token_id=lowerCamelCase    ,						eos_token_id=lowerCamelCase    ,						**lowerCamelCase		)
      lowerCAmelCase_			:      Tuple			=   vocab_size
      lowerCAmelCase_			:      Union[str, Any]			=   max_position_embeddings
      lowerCAmelCase_			:      Any			=   hidden_size
      lowerCAmelCase_			:      List[Any]			=   num_hidden_layers
      lowerCAmelCase_			:      Optional[int]			=   num_attention_heads
      lowerCAmelCase_			:      str			=   intermediate_size
      lowerCAmelCase_			:      int			=   hidden_act
      lowerCAmelCase_			:      List[Any]			=   rotary_pct
      lowerCAmelCase_			:      Any			=   rotary_emb_base
      lowerCAmelCase_			:      List[str]			=   attention_dropout
      lowerCAmelCase_			:      Union[str, Any]			=   hidden_dropout
      lowerCAmelCase_			:      Tuple			=   classifier_dropout
      lowerCAmelCase_			:      Union[str, Any]			=   initializer_range
      lowerCAmelCase_			:      Any			=   layer_norm_eps
      lowerCAmelCase_			:      str			=   use_cache
      lowerCAmelCase_			:      str			=   tie_word_embeddings
      lowerCAmelCase_			:      str			=   use_parallel_residual
      lowerCAmelCase_			:      Any			=   rope_scaling
      self._rope_scaling_validation()

      if self.hidden_size % self.num_attention_heads != 0:
        raise ValueError(
            """The hidden size is not divisble by the number of attention heads! Make sure to update them!"""		)





    def __lowercase   (   self    :  List[str]		)   ->  List[str]:
      if self.rope_scaling is None:
        return

      if not isinstance(self.rope_scaling    ,						lowerCamelCase		) or len(self.rope_scaling		) != 2:
        raise ValueError(
            """`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, """
            F'got {self.rope_scaling}'		)
      lowerCAmelCase_			:      Optional[Any]			=   self.rope_scaling.get("""type"""    ,						lowerCamelCase		)
      lowerCAmelCase_			:      int			=   self.rope_scaling.get("""factor"""    ,						lowerCamelCase		)
      if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
        raise ValueError(
            F'`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}'		)
      if rope_scaling_factor is None or not isinstance(lowerCamelCase    ,						lowerCamelCase		) or rope_scaling_factor <= 1.0:
        raise ValueError(F'`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}'		)



