





"""simple docstring"""



import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
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,
           VideoMAEForPreTraining,
           VideoMAEForVideoClassification,
           VideoMAEModel,
       )
       from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from transformers import VideoMAEImageProcessor






class 		__snake_case :





 """simple docstring"""


 def __init__(   self ,						__lowerCamelCase ,						__lowerCamelCase=13 ,						__lowerCamelCase=10 ,						__lowerCamelCase=3 ,						__lowerCamelCase=2 ,						__lowerCamelCase=2 ,						__lowerCamelCase=2 ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=32 ,						__lowerCamelCase=5 ,						__lowerCamelCase=4 ,						__lowerCamelCase=37 ,						__lowerCamelCase="gelu" ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=10 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=0.9 ,						__lowerCamelCase=None ,						):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					parent
     __A   :						str									=					batch_size
     __A   :						int									=					image_size
     __A   :						str									=					num_channels
     __A   :						List[Any]									=					patch_size
     __A   :						Any									=					tubelet_size
     __A   :						int									=					num_frames
     __A   :						List[str]									=					is_training
     __A   :						Union[str, Any]									=					use_labels
     __A   :						Optional[int]									=					hidden_size
     __A   :						Optional[Any]									=					num_hidden_layers
     __A   :						Optional[int]									=					num_attention_heads
     __A   :						Any									=					intermediate_size
     __A   :						str									=					hidden_act
     __A   :						Any									=					hidden_dropout_prob
     __A   :						Union[str, Any]									=					attention_probs_dropout_prob
     __A   :						List[str]									=					type_sequence_label_size
     __A   :						Union[str, Any]									=					initializer_range
     __A   :						Union[str, Any]									=					mask_ratio
     __A   :						Any									=					scope

     # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
     __A   :						Optional[int]									=					(image_size // patch_size) ** 2
     __A   :						List[str]									=					(num_frames // tubelet_size) * self.num_patches_per_frame

     # use this variable to define bool_masked_pos
     __A   :						int									=					int(mask_ratio * self.seq_length  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[str]									=					floats_tensor(
         [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]  )

     __A   :						Tuple									=					None
     if self.use_labels:
         __A   :						int									=					ids_tensor([self.batch_size] ,						self.type_sequence_label_size  )

     __A   :						Dict									=					self.get_config()

     return config, pixel_values, labels

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return VideoMAEConfig(
         image_size=self.image_size ,						patch_size=self.patch_size ,						num_channels=self.num_channels ,						num_frames=self.num_frames ,						tubelet_size=self.tubelet_size ,						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 ,						is_decoder=__lowerCamelCase ,						initializer_range=self.initializer_range ,						)

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						int									=					VideoMAEModel(config=__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						Any									=					model(__lowerCamelCase  )
     self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					VideoMAEForPreTraining(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     # important: each video needs to have the same number of masked patches
     # hence we define a single mask, which we then repeat for each example in the batch
     __A   :						Optional[int]									=					torch.ones((self.num_masks,)  )
     __A   :						Optional[Any]									=					torch.cat([mask, torch.zeros(self.seq_length - mask.size(0  )  )]  )
     __A   :						Optional[Any]									=					mask.expand(self.batch_size ,						-1  ).bool()

     __A   :						Optional[int]									=					model(__lowerCamelCase ,						__lowerCamelCase  )
     # model only returns predictions for masked patches
     __A   :						Union[str, Any]									=					mask.sum().item()
     __A   :						Any									=					3 * self.tubelet_size * self.patch_size**2
     self.parent.assertEqual(result.logits.shape ,						(self.batch_size, num_masked_patches, decoder_num_labels)  )







 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




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






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





 """simple docstring"""


 _lowerCamelCase				=					(
     (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
 )
 _lowerCamelCase				=					(
     {"""feature-extraction""": VideoMAEModel, """video-classification""": VideoMAEForVideoClassification}
     if is_torch_available()
     else {}
 )

 _lowerCamelCase				=					False
 _lowerCamelCase				=					False
 _lowerCamelCase				=					False
 _lowerCamelCase				=					False

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					VideoMAEModelTester(self  )
     __A   :						Any									=					ConfigTester(self ,						config_class=__lowerCamelCase ,						has_text_modality=__lowerCamelCase ,						hidden_size=37  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase=False  ):



     '''simple docstring'''




     __A   :						List[Any]									=					copy.deepcopy(__lowerCamelCase  )

     if model_class == VideoMAEForPreTraining:
         # important: each video needs to have the same number of masked patches
         # hence we define a single mask, which we then repeat for each example in the batch
         __A   :						Union[str, Any]									=					torch.ones((self.model_tester.num_masks,)  )
         __A   :						List[str]									=					torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0  )  )]  )
         __A   :						Union[str, Any]									=					mask.expand(self.model_tester.batch_size ,						-1  ).bool()
         __A   :						int									=					bool_masked_pos.to(__lowerCamelCase  )

     if return_labels:
         if model_class in [
             *get_values(__lowerCamelCase  ),
         ]:
             __A   :						Optional[Any]									=					torch.zeros(
                 self.model_tester.batch_size ,						dtype=torch.long ,						device=__lowerCamelCase  )

     return inputs_dict

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

 @unittest.skip(reason='''VideoMAE does not use inputs_embeds'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Any									=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         __A   :						Tuple									=					model_class(__lowerCamelCase  )
         self.assertIsInstance(model.get_input_embeddings() ,						(nn.Module)  )
         __A   :						Any									=					model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(__lowerCamelCase ,						nn.Linear  )  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Tuple									=					self.model_tester.prepare_config_and_inputs_for_common()

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

         __A   :						Dict									=					['''pixel_values''']
         self.assertListEqual(arg_names[:1] ,						__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						str									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase  )

 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A   :						int									=					VideoMAEModel.from_pretrained(__lowerCamelCase  )
         self.assertIsNotNone(__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     if not self.has_attentions:
         pass

     else:
         __A   :						str									=					self.model_tester.prepare_config_and_inputs_for_common()
         __A   :						List[Any]									=					True

         for model_class in self.all_model_classes:
             __A   :						List[str]									=					self.model_tester.seq_length - self.model_tester.num_masks
             __A   :						Dict									=					(
                 num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
             )

             __A   :						Dict									=					True
             __A   :						Optional[Any]									=					False
             __A   :						List[Any]									=					True
             __A   :						Dict									=					model_class(__lowerCamelCase  )
             model.to(__lowerCamelCase  )
             model.eval()
             with torch.no_grad():
                 __A   :						Optional[Any]									=					model(**self._prepare_for_class(__lowerCamelCase ,						__lowerCamelCase  )  )
             __A   :						Optional[Any]									=					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"]
             __A   :						str									=					True
             __A   :						str									=					model_class(__lowerCamelCase  )
             model.to(__lowerCamelCase  )
             model.eval()
             with torch.no_grad():
                 __A   :						Any									=					model(**self._prepare_for_class(__lowerCamelCase ,						__lowerCamelCase  )  )
             __A   :						List[str]									=					outputs.attentions
             self.assertEqual(len(__lowerCamelCase  ) ,						self.model_tester.num_hidden_layers  )

             self.assertListEqual(
                 list(attentions[0].shape[-3:]  ) ,						[self.model_tester.num_attention_heads, seq_len, seq_len] ,						)
             __A   :						Dict									=					len(__lowerCamelCase  )

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

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

             __A   :						List[str]									=					outputs.attentions

             self.assertEqual(len(__lowerCamelCase  ) ,						self.model_tester.num_hidden_layers  )
             self.assertListEqual(
                 list(self_attentions[0].shape[-3:]  ) ,						[self.model_tester.num_attention_heads, seq_len, seq_len] ,						)

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     def check_hidden_states_output(__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):
         __A   :						Any									=					model_class(__lowerCamelCase  )
         model.to(__lowerCamelCase  )
         model.eval()

         with torch.no_grad():
             __A   :						Dict									=					model(**self._prepare_for_class(__lowerCamelCase ,						__lowerCamelCase  )  )

         __A   :						Optional[Any]									=					outputs.hidden_states
         __A   :						Union[str, Any]									=					self.model_tester.num_hidden_layers + 1
         self.assertEqual(len(__lowerCamelCase  ) ,						__lowerCamelCase  )

         __A   :						List[Any]									=					self.model_tester.seq_length - self.model_tester.num_masks
         __A   :						str									=					num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

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

     __A   :						Union[str, Any]									=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         __A   :						Any									=					True
         check_hidden_states_output(__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  )

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

         check_hidden_states_output(__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  )







 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass







def 		__lowercase					(       )       ->str:



    '''simple docstring'''







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






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





 """simple docstring"""


 @cached_property
 def  UpperCamelCase__(   self  ):



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



     '''simple docstring'''




     __A   :						List[str]									=					VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''  ).to(
         __lowerCamelCase  )

     __A   :						Union[str, Any]									=					self.default_image_processor
     __A   :						str									=					prepare_video()
     __A   :						Optional[Any]									=					image_processor(__lowerCamelCase ,						return_tensors='''pt'''  ).to(__lowerCamelCase  )

     # forward pass
     with torch.no_grad():
         __A   :						str									=					model(**__lowerCamelCase  )

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

     __A   :						List[Any]									=					torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1]  ).to(__lowerCamelCase  )

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







 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Any									=					VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''  ).to(__lowerCamelCase  )

     __A   :						int									=					self.default_image_processor
     __A   :						Tuple									=					prepare_video()
     __A   :						Any									=					image_processor(__lowerCamelCase ,						return_tensors='''pt'''  ).to(__lowerCamelCase  )

     # add boolean mask, indicating which patches to mask
     __A   :						Optional[int]									=					hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos''' ,						filename='''bool_masked_pos.pt'''  )
     __A   :						Optional[Any]									=					torch.load(__lowerCamelCase  )

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

     # verify the logits
     __A   :						int									=					torch.Size([1, 1408, 1536]  )
     __A   :						Dict									=					torch.tensor(
         [[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]] ,						device=__lowerCamelCase  )
     self.assertEqual(outputs.logits.shape ,						__lowerCamelCase  )
     self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] ,						__lowerCamelCase ,						atol=1e-4  )  )

     # verify the loss (`config.norm_pix_loss` = `True`)
     __A   :						Any									=					torch.tensor([0.5_1_4_2] ,						device=__lowerCamelCase  )
     self.assertTrue(torch.allclose(outputs.loss ,						__lowerCamelCase ,						atol=1e-4  )  )

     # verify the loss (`config.norm_pix_loss` = `False`)
     __A   :						List[Any]									=					VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' ,						norm_pix_loss=__lowerCamelCase  ).to(
         __lowerCamelCase  )

     with torch.no_grad():
         __A   :						Tuple									=					model(**__lowerCamelCase  )

     __A   :						List[Any]									=					torch.tensor(torch.tensor([0.6_4_6_9]  ) ,						device=__lowerCamelCase  )
     self.assertTrue(torch.allclose(outputs.loss ,						__lowerCamelCase ,						atol=1e-4  )  )











"""simple docstring"""



def 		__lowercase					(       snake_case_  :  int						)       ->Optional[Any]:



    '''simple docstring'''







    stooge(snake_case_    ,0    ,len(snake_case_						) - 1						)
    return arr







def 		__lowercase					(       snake_case_  :  Optional[Any]    ,snake_case_  :  Union[str, Any]    ,snake_case_  :  Any						)       ->Tuple:



    '''simple docstring'''







    if i >= h:
        return

    # If first element is smaller than the last then swap them
    if arr[i] > arr[h]:
        __A			,	__A   :						Optional[int]									=					arr[h], arr[i]

    # If there are more than 2 elements in the array
    if h - i + 1 > 2:
        __A   :						Any									=					(int)((h - i + 1) / 3						)

        # Recursively sort first 2/3 elements
        stooge(snake_case_    ,snake_case_    ,(h - t)						)

        # Recursively sort last 2/3 elements
        stooge(snake_case_    ,i + t    ,(snake_case_)						)

        # Recursively sort first 2/3 elements
        stooge(snake_case_    ,snake_case_    ,(h - t)						)


if __name__ == "__main__":
       a_			=      input("""Enter numbers separated by a comma:\n""").strip()
       a_			=      [int(item) for item in user_input.split(""",""")]
       print(stooge_sort(unsorted))




