






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






      '''simple docstring'''

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




          '''simple docstring'''


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

          # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	(image_size // patch_size) ** 2
          SCREAMING_SNAKE_CASE_   :						Dict							=	(num_frames) * self.num_patches_per_frame + 1
      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size])

          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	None
          if self.use_labels:
              SCREAMING_SNAKE_CASE_   :						Any							=	ids_tensor([self.batch_size]		,			self.num_labels)

          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	self.get_config()

          return config, pixel_values, labels
      def _SCREAMING_SNAKE_CASE				(		self  :						Union[str, Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Optional[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		,			)
          SCREAMING_SNAKE_CASE_   :						int							=	self.num_labels
          return config
      def _SCREAMING_SNAKE_CASE				(		self  :						List[Any]		,			lowercase_  :						List[Any]		,			lowercase_  :						Optional[int]		,			lowercase_  :						Optional[int]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						str							=	TimesformerModel(config=lowercase_)
          model.to(lowercase_)
          model.eval()
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	model(lowercase_)
          self.parent.assertEqual(result.last_hidden_state.shape		,			(self.batch_size, self.seq_length, self.hidden_size))
      def _SCREAMING_SNAKE_CASE				(		self  :						Any		,			lowercase_  :						str		,			lowercase_  :						Optional[Any]		,			lowercase_  :						List[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	TimesformerForVideoClassification(lowercase_)
          model.to(lowercase_)
          model.eval()

          SCREAMING_SNAKE_CASE_   :						Dict							=	model(lowercase_)

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



      def _SCREAMING_SNAKE_CASE				(		self  :						List[str]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Any							=	self.prepare_config_and_inputs()
          SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						int							=	config_and_inputs
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	{'''pixel_values''': pixel_values}
          return config, inputs_dict




@require_torch
class 							lowerCAmelCase__       (    UpperCAmelCase__       ,	UpperCAmelCase__       ,	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 _SCREAMING_SNAKE_CASE				(		self  :						str):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	TimesformerModelTester(self)
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	ConfigTester(
              self		,			config_class=lowercase_		,			has_text_modality=lowercase_		,			hidden_size=37)
      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple		,			lowercase_  :						Tuple		,			lowercase_  :						List[Any]		,			lowercase_  :						Any=False):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						List[Any]							=	copy.deepcopy(lowercase_)

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

          return inputs_dict
      def _SCREAMING_SNAKE_CASE				(		self  :						Tuple):




          '''simple docstring'''


          self.config_tester.run_common_tests()
      @unittest.skip(reason='''TimeSformer does not use inputs_embeds''')
      def _SCREAMING_SNAKE_CASE				(		self  :						str):




          '''simple docstring'''


          pass
      def _SCREAMING_SNAKE_CASE				(		self  :						str):




          '''simple docstring'''


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

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




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						str							=	self.model_tester.prepare_config_and_inputs_for_common()

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

              SCREAMING_SNAKE_CASE_   :						Dict							=	['''pixel_values''']
              self.assertListEqual(arg_names[:1]		,			lowercase_)
      def _SCREAMING_SNAKE_CASE				(		self  :						List[str]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						int							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase_)
      def _SCREAMING_SNAKE_CASE				(		self  :						int):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						str							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_video_classification(*lowercase_)
      @slow
      def _SCREAMING_SNAKE_CASE				(		self  :						Any):




          '''simple docstring'''


          for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	TimesformerModel.from_pretrained(lowercase_)
              self.assertIsNotNone(lowercase_)
      def _SCREAMING_SNAKE_CASE				(		self  :						Union[str, Any]):




          '''simple docstring'''


          if not self.has_attentions:
              pass

          else:
              SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	self.model_tester.prepare_config_and_inputs_for_common()
              SCREAMING_SNAKE_CASE_   :						Dict							=	True

              for model_class in self.all_model_classes:
                  SCREAMING_SNAKE_CASE_   :						Any							=	self.model_tester.seq_length
                  SCREAMING_SNAKE_CASE_   :						Tuple							=	self.model_tester.num_frames

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

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

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

                  SCREAMING_SNAKE_CASE_   :						int							=	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 _SCREAMING_SNAKE_CASE				(		self  :						Any):




          '''simple docstring'''


          def check_hidden_states_output(lowercase_  :						Optional[int]		,			lowercase_  :						Dict		,			lowercase_  :						Optional[int]):
              SCREAMING_SNAKE_CASE_   :						Any							=	model_class(lowercase_)
              model.to(lowercase_)
              model.eval()

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

              SCREAMING_SNAKE_CASE_   :						Dict							=	outputs.hidden_states
              SCREAMING_SNAKE_CASE_   :						List[str]							=	self.model_tester.num_hidden_layers + 1
              self.assertEqual(len(lowercase_)		,			lowercase_)

              SCREAMING_SNAKE_CASE_   :						int							=	self.model_tester.seq_length

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

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

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

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	True

              check_hidden_states_output(lowercase_		,			lowercase_		,			lowercase_)



def       _A    ()				-> List[str]:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						Tuple							=	hf_hub_download(
        repo_id='''hf-internal-testing/spaghetti-video'''					,       filename='''eating_spaghetti.npy'''					,       repo_type='''dataset'''							)
    SCREAMING_SNAKE_CASE_   :						Optional[int]							=	np.load(__a							)
    return list(__a							)





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






      '''simple docstring'''

      @cached_property
      def _SCREAMING_SNAKE_CASE				(		self  :						List[Any]):




          '''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 _SCREAMING_SNAKE_CASE				(		self  :						Dict):




          '''simple docstring'''


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

          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	self.default_image_processor
          SCREAMING_SNAKE_CASE_   :						int							=	prepare_video()
          SCREAMING_SNAKE_CASE_   :						int							=	image_processor(video[:8]		,			return_tensors='''pt''').to(lowercase_)

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

          # verify the logits
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	torch.Size((1, 400))
          self.assertEqual(outputs.logits.shape		,			lowercase_)

          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	torch.tensor([-0.30_16, -0.77_13, -0.42_05]).to(lowercase_)

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










"""simple docstring"""






# Algorithm for the pigeonhole sorting





def       _A    (__a							)				-> Dict:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						Dict							=	min(__a							)  # min() finds the minimum value
    SCREAMING_SNAKE_CASE_   :						int							=	max(__a							)  # max() finds the maximum value

    SCREAMING_SNAKE_CASE_   :						Dict							=	max_val - min_val + 1  # size is difference of max and min values plus one

    # list of pigeonholes of size equal to the variable size
    SCREAMING_SNAKE_CASE_   :						Any							=	[0] * size

    # Populate the pigeonholes.
    for x in a:
        assert isinstance(__a					,       __a							), "integers only please"
        holes[x - min_val] += 1

    # Putting the elements back into the array in an order.
    SCREAMING_SNAKE_CASE_   :						Any							=	0
    for count in range(__a							):
        while holes[count] > 0:
            holes[count] -= 1
            SCREAMING_SNAKE_CASE_   :						Dict							=	count + min_val
            i += 1



def       _A    ()				-> Optional[int]:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						List[str]							=	[8, 3, 2, 7, 4, 6, 8]
    pigeonhole_sort(__a							)
    print('''Sorted order is:'''					,       ''' '''.join(__a							)							)


if __name__ == "__main__":
  main()


