






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





    def __init__(							self			:      Optional[int]       ,	A			:      int       ,	A			:      Tuple=1_3       ,	A			:      List[str]=1_0       ,	A			:      int=3       ,	A			:      Tuple=2       ,	A			:      str=2       ,	A			:      List[str]=True       ,	A			:      Any=True       ,	A			:      Optional[Any]=3_2       ,	A			:      str=5       ,	A			:      Optional[Any]=4       ,	A			:      int=3_7       ,	A			:      Union[str, Any]="gelu"       ,	A			:      str=0.1       ,	A			:      Optional[Any]=0.1       ,	A			:      Tuple=1_0       ,	A			:      Optional[int]=0.02       ,	A			:      Tuple="divided_space_time"       ,	A			:      Any=None       ,	):


      '''simple docstring'''




      a  :    int       =     parent
      a  :    List[Any]       =     batch_size
      a  :    List[Any]       =     image_size
      a  :    int       =     num_channels
      a  :    Union[str, Any]       =     patch_size
      a  :    str       =     num_frames
      a  :    Any       =     is_training
      a  :    Optional[int]       =     use_labels
      a  :    List[str]       =     hidden_size
      a  :    Union[str, Any]       =     num_hidden_layers
      a  :    int       =     num_attention_heads
      a  :    str       =     intermediate_size
      a  :    Optional[int]       =     hidden_act
      a  :    List[Any]       =     hidden_dropout_prob
      a  :    Optional[Any]       =     attention_probs_dropout_prob
      a  :    Any       =     attention_type
      a  :    Dict       =     initializer_range
      a  :    List[str]       =     scope
      a  :    List[str]       =     num_labels

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





    def        lowerCamelCase__   (							self			:      Dict    ):


      '''simple docstring'''




      a  :    Optional[int]       =     floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]    )

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

      a  :    Union[str, Any]       =     self.get_config()

      return config, pixel_values, labels





    def        lowerCamelCase__   (							self			:      Union[str, Any]    ):


      '''simple docstring'''




      a  :    str       =     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       ,	)
      a  :    str       =     self.num_labels
      return config





    def        lowerCamelCase__   (							self			:      Union[str, Any]       ,	A			:      int       ,	A			:      int       ,	A			:      List[Any]    ):


      '''simple docstring'''




      a  :    Optional[Any]       =     TimesformerModel(config=A    )
      model.to(A    )
      model.eval()
      a  :    Optional[Any]       =     model(A    )
      self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size)    )





    def        lowerCamelCase__   (							self			:      int       ,	A			:      Dict       ,	A			:      Optional[Any]       ,	A			:      List[str]    ):


      '''simple docstring'''




      a  :    Tuple       =     TimesformerForVideoClassification(A    )
      model.to(A    )
      model.eval()

      a  :    Any       =     model(A    )

      # verify the logits shape
      a  :    Dict       =     torch.Size((self.batch_size, self.num_labels)    )
      self.parent.assertEqual(result.logits.shape       ,	A    )





    def        lowerCamelCase__   (							self			:      Dict    ):


      '''simple docstring'''




      a  :    List[Any]       =     self.prepare_config_and_inputs()
      a,  a,  a  :    Optional[int]       =     config_and_inputs
      a  :    Optional[int]       =     {'pixel_values': pixel_values}
      return config, inputs_dict






@require_torch
class 		snake_case						(						UpperCAmelCase     ,				UpperCAmelCase     ,				unittest.TestCase ):
    __magic_name__					  =  (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
    __magic_name__					  =  (
        {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
        if is_torch_available()
        else {}
    )

    __magic_name__					  =  False
    __magic_name__					  =  False
    __magic_name__					  =  False
    __magic_name__					  =  False





    def        lowerCamelCase__   (							self			:      Tuple    ):


      '''simple docstring'''




      a  :    List[Any]       =     TimesformerModelTester(self    )
      a  :    Optional[Any]       =     ConfigTester(
          self       ,	config_class=A       ,	has_text_modality=A       ,	hidden_size=3_7    )





    def        lowerCamelCase__   (							self			:      Optional[Any]       ,	A			:      str       ,	A			:      Dict       ,	A			:      List[str]=False    ):


      '''simple docstring'''




      a  :    Optional[Any]       =     copy.deepcopy(A    )

      if return_labels:
        if model_class in get_values(A    ):
          a  :    List[Any]       =     torch.zeros(
              self.model_tester.batch_size       ,	dtype=torch.long       ,	device=A    )

      return inputs_dict





    def        lowerCamelCase__   (							self			:      List[str]    ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    @unittest.skip(reason='TimeSformer does not use inputs_embeds'    )
    def        lowerCamelCase__   (							self			:      Optional[int]    ):


      '''simple docstring'''




      pass





    def        lowerCamelCase__   (							self			:      Optional[Any]    ):


      '''simple docstring'''




      a,  a  :    Optional[int]       =     self.model_tester.prepare_config_and_inputs_for_common()

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





    def        lowerCamelCase__   (							self			:      Any    ):


      '''simple docstring'''




      a,  a  :    Tuple       =     self.model_tester.prepare_config_and_inputs_for_common()

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

        a  :    List[Any]       =     ['pixel_values']
        self.assertListEqual(arg_names[:1]       ,	A    )





    def        lowerCamelCase__   (							self			:      List[Any]    ):


      '''simple docstring'''




      a  :    str       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A    )





    def        lowerCamelCase__   (							self			:      Any    ):


      '''simple docstring'''




      a  :    Union[str, Any]       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_video_classification(*A    )





    @slow
    def        lowerCamelCase__   (							self			:      Dict    ):


      '''simple docstring'''




      for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a  :    List[Any]       =     TimesformerModel.from_pretrained(A    )
        self.assertIsNotNone(A    )





    def        lowerCamelCase__   (							self			:      Tuple    ):


      '''simple docstring'''




      if not self.has_attentions:
        pass

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

        for model_class in self.all_model_classes:
          a  :    Dict       =     self.model_tester.seq_length
          a  :    int       =     self.model_tester.num_frames

          a  :    Optional[int]       =     True
          a  :    Dict       =     False
          a  :    Union[str, Any]       =     True
          a  :    List[str]       =     model_class(A    )
          model.to(A    )
          model.eval()
          with torch.no_grad():
            a  :    str       =     model(**self._prepare_for_class(A       ,	A    )    )
          a  :    Dict       =     outputs.attentions
          self.assertEqual(len(A    )       ,	self.model_tester.num_hidden_layers    )

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

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

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

          a  :    Optional[int]       =     outputs.attentions

          self.assertEqual(len(A    )       ,	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        lowerCamelCase__   (							self			:      Any    ):


      '''simple docstring'''




      def check_hidden_states_output(A			:      List[str]       ,	A			:      Optional[int]       ,	A			:      List[str]    ):
        a  :    Tuple       =     model_class(A    )
        model.to(A    )
        model.eval()

        with torch.no_grad():
          a  :    Any       =     model(**self._prepare_for_class(A       ,	A    )    )

        a  :    Optional[int]       =     outputs.hidden_states
        a  :    List[Any]       =     self.model_tester.num_hidden_layers + 1
        self.assertEqual(len(A    )       ,	A    )

        a  :    str       =     self.model_tester.seq_length

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

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

      for model_class in self.all_model_classes:
        a  :    Optional[int]       =     True
        check_hidden_states_output(A       ,	A       ,	A    )

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        a  :    str       =     True

        check_hidden_states_output(A       ,	A       ,	A    )




def     snake_case						():







  '''simple docstring'''

  a  :    List[Any]       =     hf_hub_download(
      repo_id='hf-internal-testing/spaghetti-video'			,    filename='eating_spaghetti.npy'			,    repo_type='dataset'   )
  a  :    List[str]       =     np.load(A_   )
  return list(A_   )






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





    @cached_property
    def        lowerCamelCase__   (							self			:      List[str]    ):


      '''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        lowerCamelCase__   (							self			:      int    ):


      '''simple docstring'''




      a  :    Any       =     TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'    ).to(
          A    )

      a  :    Any       =     self.default_image_processor
      a  :    Any       =     prepare_video()
      a  :    Optional[Any]       =     image_processor(video[:8]       ,	return_tensors='pt'    ).to(A    )

      # forward pass
      with torch.no_grad():
        a  :    Optional[Any]       =     model(**A    )

      # verify the logits
      a  :    List[Any]       =     torch.Size((1, 4_0_0)    )
      self.assertEqual(outputs.logits.shape       ,	A    )

      a  :    List[str]       =     torch.tensor([-0.30_16, -0.77_13, -0.42_05]    ).to(A    )

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












"""simple docstring"""


import warnings

from ...utils import logging
from .image_processing_yolos import YolosImageProcessor


_UpperCamelCase       :					List[str]									=		logging.get_logger(__name__)






class 		snake_case						(						UpperCAmelCase ):





    def __init__(							self			:      Dict       ,	*A			:      Union[str, Any]       ,	**A			:      List[str]    ):


      '''simple docstring'''




      warnings.warn(
          'The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
          ' use YolosImageProcessor instead.'       ,	A       ,	)
      super().__init__(*A       ,	**A    )




