


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 			SCREAMING_SNAKE_CASE__  :
      def __init__(	self    :	Any					,							SCREAMING_SNAKE_CASE__    :	str					,							SCREAMING_SNAKE_CASE__    :	List[str]=1_3					,							SCREAMING_SNAKE_CASE__    :	Any=1_0					,							SCREAMING_SNAKE_CASE__    :	List[Any]=3					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=2					,							SCREAMING_SNAKE_CASE__    :	int=2					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=True					,							SCREAMING_SNAKE_CASE__    :	Tuple=True					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=3_2					,							SCREAMING_SNAKE_CASE__    :	str=5					,							SCREAMING_SNAKE_CASE__    :	List[Any]=4					,							SCREAMING_SNAKE_CASE__    :	Dict=3_7					,							SCREAMING_SNAKE_CASE__    :	List[str]="gelu"					,							SCREAMING_SNAKE_CASE__    :	Any=0.1					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=0.1					,							SCREAMING_SNAKE_CASE__    :	str=1_0					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=0.02					,							SCREAMING_SNAKE_CASE__    :	Any="divided_space_time"					,							SCREAMING_SNAKE_CASE__    :	str=None					,							)		->      str:
             a_ :		int							=  parent
             a_ :		Tuple							=  batch_size
             a_ :		Union[str, Any]							=  image_size
             a_ :		Optional[int]							=  num_channels
             a_ :		Optional[int]							=  patch_size
             a_ :		List[str]							=  num_frames
             a_ :		int							=  is_training
             a_ :		Union[str, Any]							=  use_labels
             a_ :		Tuple							=  hidden_size
             a_ :		Optional[Any]							=  num_hidden_layers
             a_ :		int							=  num_attention_heads
             a_ :		Any							=  intermediate_size
             a_ :		str							=  hidden_act
             a_ :		Any							=  hidden_dropout_prob
             a_ :		Optional[int]							=  attention_probs_dropout_prob
             a_ :		Dict							=  attention_type
             a_ :		List[Any]							=  initializer_range
             a_ :		Optional[Any]							=  scope
             a_ :		Optional[Any]							=  num_labels

             # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
             a_ :		Any							=  (image_size // patch_size) ** 2
             a_ :		Union[str, Any]							=  (num_frames) * self.num_patches_per_frame + 1
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      List[Any]:
             a_ :		Tuple							=  floats_tensor(
                 [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

             a_ :		str							=  None
             if self.use_labels:
                    a_ :		Any							=  ids_tensor([self.batch_size]					,							self.num_labels					)

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

             return config, pixel_values, labels
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					)		->      Optional[int]:
             a_ :		int							=  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 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	int					,							SCREAMING_SNAKE_CASE__    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]					)		->      Union[str, Any]:
             a_ :		Optional[Any]							=  TimesformerModel(config=SCREAMING_SNAKE_CASE__					)
             model.to(SCREAMING_SNAKE_CASE__					)
             model.eval()
             a_ :		int							=  model(SCREAMING_SNAKE_CASE__					)
             self.parent.assertEqual(result.last_hidden_state.shape					,							(self.batch_size, self.seq_length, self.hidden_size)					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]					,							SCREAMING_SNAKE_CASE__    :	List[str]					,							SCREAMING_SNAKE_CASE__    :	int					)		->      Union[str, Any]:
             a_ :		Union[str, Any]							=  TimesformerForVideoClassification(SCREAMING_SNAKE_CASE__					)
             model.to(SCREAMING_SNAKE_CASE__					)
             model.eval()

             a_ :		Optional[int]							=  model(SCREAMING_SNAKE_CASE__					)

             # verify the logits shape
             a_ :		Tuple							=  torch.Size((self.batch_size, self.num_labels)					)
             self.parent.assertEqual(result.logits.shape					,							SCREAMING_SNAKE_CASE__					)

      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					)		->      Optional[int]:
             a_ :		int							=  self.prepare_config_and_inputs()
             a_							,		a_							,		a_ :		List[Any]							=  config_and_inputs
             a_ :		Any							=  {'pixel_values': pixel_values}
             return config, inputs_dict
@require_torch
class 			SCREAMING_SNAKE_CASE__  (    lowercase__						,   lowercase__						,   unittest.TestCase      ):
      snake_case__    :					Optional[int]					       =						(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
      snake_case__    :					List[Any]					       =						(
          {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
          if is_torch_available()
          else {}
      )

      snake_case__    :					Optional[Any]					       =						False
      snake_case__    :					Union[str, Any]					       =						False
      snake_case__    :					Union[str, Any]					       =						False
      snake_case__    :					Dict					       =						False
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      Tuple:
             a_ :		Tuple							=  TimesformerModelTester(self					)
             a_ :		List[str]							=  ConfigTester(
                 self					,							config_class=SCREAMING_SNAKE_CASE__					,							has_text_modality=SCREAMING_SNAKE_CASE__					,							hidden_size=3_7					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					,							SCREAMING_SNAKE_CASE__    :	str					,							SCREAMING_SNAKE_CASE__    :	List[str]					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=False					)		->      Tuple:
             a_ :		List[Any]							=  copy.deepcopy(SCREAMING_SNAKE_CASE__					)

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

             return inputs_dict
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      Union[str, Any]:
             self.config_tester.run_common_tests()
      @unittest.skip(reason='TimeSformer does not use inputs_embeds'					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					)		->      Optional[int]:
             pass
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      Optional[int]:
             a_							,		a_ :		Optional[Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    a_ :		Tuple							=  model_class(SCREAMING_SNAKE_CASE__					)
                    self.assertIsInstance(model.get_input_embeddings()					,							(nn.Module)					)
                    a_ :		str							=  model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE__					,							nn.Linear					)					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					)		->      Tuple:
             a_							,		a_ :		Any							=  self.model_tester.prepare_config_and_inputs_for_common()

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

                    a_ :		Optional[Any]							=  ['pixel_values']
                    self.assertListEqual(arg_names[:1]					,							SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      Optional[Any]:
             a_ :		Any							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Any					)		->      List[str]:
             a_ :		Optional[Any]							=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_video_classification(*SCREAMING_SNAKE_CASE__					)
      @slow
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					)		->      List[Any]:
             for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    a_ :		Any							=  TimesformerModel.from_pretrained(SCREAMING_SNAKE_CASE__					)
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Any					)		->      Any:
             if not self.has_attentions:
                    pass

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

                    for model_class in self.all_model_classes:
                           a_ :		Optional[Any]							=  self.model_tester.seq_length
                           a_ :		List[str]							=  self.model_tester.num_frames

                           a_ :		Optional[int]							=  True
                           a_ :		Optional[Any]							=  False
                           a_ :		Tuple							=  True
                           a_ :		int							=  model_class(SCREAMING_SNAKE_CASE__					)
                           model.to(SCREAMING_SNAKE_CASE__					)
                           model.eval()
                           with torch.no_grad():
                                  a_ :		Optional[int]							=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)					)
                           a_ :		Tuple							=  outputs.attentions
                           self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							self.model_tester.num_hidden_layers					)

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

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

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

                           a_ :		List[str]							=  outputs.attentions

                           self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							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    :	str					)		->      List[str]:
             def check_hidden_states_output(SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	List[str]					,							SCREAMING_SNAKE_CASE__    :	Dict					):
                    a_ :		Any							=  model_class(SCREAMING_SNAKE_CASE__					)
                    model.to(SCREAMING_SNAKE_CASE__					)
                    model.eval()

                    with torch.no_grad():
                           a_ :		List[str]							=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)					)

                    a_ :		Optional[int]							=  outputs.hidden_states
                    a_ :		Dict							=  self.model_tester.num_hidden_layers + 1
                    self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							SCREAMING_SNAKE_CASE__					)

                    a_ :		int							=  self.model_tester.seq_length

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

             a_							,		a_ :		List[Any]							=  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    a_ :		str							=  True
                    check_hidden_states_output(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)

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

                    check_hidden_states_output(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)







def 	SCREAMING_SNAKE_CASE_						(      )     ->		Any:

       """simple docstring"""

       a_ :		Optional[int]							=  hf_hub_download(
           repo_id='hf-internal-testing/spaghetti-video'  ,   filename='eating_spaghetti.npy'  ,   repo_type='dataset'    )
       a_ :		Any							=  np.load(__A    )
       return list(__A    )
@require_torch
@require_vision
class 			SCREAMING_SNAKE_CASE__  (    unittest.TestCase      ):
      @cached_property
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      Optional[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 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      Dict:
             a_ :		List[str]							=  TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'					).to(
                 SCREAMING_SNAKE_CASE__					)

             a_ :		List[str]							=  self.default_image_processor
             a_ :		int							=  prepare_video()
             a_ :		List[str]							=  image_processor(video[:8]					,							return_tensors='pt'					).to(SCREAMING_SNAKE_CASE__					)

             # forward pass
             with torch.no_grad():
                    a_ :		int							=  model(**SCREAMING_SNAKE_CASE__					)

             # verify the logits
             a_ :		Any							=  torch.Size((1, 4_0_0)					)
             self.assertEqual(outputs.logits.shape					,							SCREAMING_SNAKE_CASE__					)

             a_ :		List[str]							=  torch.tensor([-0.3016, -0.7713, -0.4205]					).to(SCREAMING_SNAKE_CASE__					)

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






def 	SCREAMING_SNAKE_CASE_						(      __A			:      list    )     ->		list:

       """simple docstring"""

       a_ :		int							=  len(__A    )
       for _ in range(__A    ):
              for i in range(_ % 2  ,   arr_size - 1  ,   2    ):
                     if arr[i + 1] < arr[i]:
                            a_							,		a_ :		int							=  arr[i + 1], arr[i]

       return arr


if __name__ == "__main__":
   UpperCAmelCase_				:			int   =	list(range(10, 0, -1))
   print(F'Original: {arr}. Sorted: {odd_even_transposition(arr)}')


