


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






     def __init__(	self  :					Tuple					,				UpperCamelCase_  :					List[str]					,				UpperCamelCase_  :					List[Any]=1_3					,				UpperCamelCase_  :					Any=1_0					,				UpperCamelCase_  :					Dict=3					,				UpperCamelCase_  :					Union[str, Any]=2					,				UpperCamelCase_  :					Union[str, Any]=2					,				UpperCamelCase_  :					List[Any]=2					,				UpperCamelCase_  :					List[str]=True					,				UpperCamelCase_  :					Tuple=True					,				UpperCamelCase_  :					Tuple=3_2					,				UpperCamelCase_  :					List[str]=5					,				UpperCamelCase_  :					int=4					,				UpperCamelCase_  :					Any=3_7					,				UpperCamelCase_  :					int="gelu"					,				UpperCamelCase_  :					List[str]=0.1					,				UpperCamelCase_  :					Optional[Any]=0.1					,				UpperCamelCase_  :					int=1_0					,				UpperCamelCase_  :					List[str]=0.02					,				UpperCamelCase_  :					Union[str, Any]=0.9					,				UpperCamelCase_  :					List[Any]=None					,				):
           lowerCAmelCase					:	Dict       =   parent
           lowerCAmelCase					:	str       =   batch_size
           lowerCAmelCase					:	Union[str, Any]       =   image_size
           lowerCAmelCase					:	Union[str, Any]       =   num_channels
           lowerCAmelCase					:	Tuple       =   patch_size
           lowerCAmelCase					:	Optional[int]       =   tubelet_size
           lowerCAmelCase					:	Optional[int]       =   num_frames
           lowerCAmelCase					:	int       =   is_training
           lowerCAmelCase					:	str       =   use_labels
           lowerCAmelCase					:	Union[str, Any]       =   hidden_size
           lowerCAmelCase					:	Tuple       =   num_hidden_layers
           lowerCAmelCase					:	Any       =   num_attention_heads
           lowerCAmelCase					:	Dict       =   intermediate_size
           lowerCAmelCase					:	str       =   hidden_act
           lowerCAmelCase					:	List[Any]       =   hidden_dropout_prob
           lowerCAmelCase					:	Optional[int]       =   attention_probs_dropout_prob
           lowerCAmelCase					:	Optional[int]       =   type_sequence_label_size
           lowerCAmelCase					:	Optional[int]       =   initializer_range
           lowerCAmelCase					:	Optional[Any]       =   mask_ratio
           lowerCAmelCase					:	Optional[Any]       =   scope

           # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
           lowerCAmelCase					:	Tuple       =   (image_size // patch_size) ** 2
           lowerCAmelCase					:	Union[str, Any]       =   (num_frames // tubelet_size) * self.num_patches_per_frame

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






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           lowerCAmelCase					:	Dict       =   floats_tensor(
               [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]				)

           lowerCAmelCase					:	List[str]       =   None
           if self.use_labels:
                 lowerCAmelCase					:	Tuple       =   ids_tensor([self.batch_size]					,				self.type_sequence_label_size				)

           lowerCAmelCase					:	Tuple       =   self.get_config()

           return config, pixel_values, labels






     def      lowerCamelCase__     (	self  :					List[Any]				):
           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=UpperCamelCase_					,				initializer_range=self.initializer_range					,				)






     def      lowerCamelCase__     (	self  :					int					,				UpperCamelCase_  :					Union[str, Any]					,				UpperCamelCase_  :					Dict					,				UpperCamelCase_  :					List[Any]				):
           lowerCAmelCase					:	str       =   VideoMAEModel(config=UpperCamelCase_				)
           model.to(UpperCamelCase_				)
           model.eval()
           lowerCAmelCase					:	Dict       =   model(UpperCamelCase_				)
           self.parent.assertEqual(result.last_hidden_state.shape					,				(self.batch_size, self.seq_length, self.hidden_size)				)






     def      lowerCamelCase__     (	self  :					Optional[int]					,				UpperCamelCase_  :					List[str]					,				UpperCamelCase_  :					Optional[Any]					,				UpperCamelCase_  :					Optional[Any]				):
           lowerCAmelCase					:	Optional[Any]       =   VideoMAEForPreTraining(UpperCamelCase_				)
           model.to(UpperCamelCase_				)
           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
           lowerCAmelCase					:	List[Any]       =   torch.ones((self.num_masks,)				)
           lowerCAmelCase					:	Optional[Any]       =   torch.cat([mask, torch.zeros(self.seq_length - mask.size(0				)				)]				)
           lowerCAmelCase					:	Optional[int]       =   mask.expand(self.batch_size					,				-1				).bool()

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






     def      lowerCamelCase__     (	self  :					int				):
           lowerCAmelCase					:	Dict       =   self.prepare_config_and_inputs()
           lowerCAmelCase,      lowerCAmelCase,      lowerCAmelCase					:	int       =   config_and_inputs
           lowerCAmelCase					:	List[Any]       =   {'''pixel_values''': pixel_values}
           return config, inputs_dict



@require_torch
class 		snake_case_(       a__							,					a__							,					unittest.TestCase			):
     __UpperCamelCase  =					(
         (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
     )
     __UpperCamelCase  =					(
         {'''feature-extraction''': VideoMAEModel, '''video-classification''': VideoMAEForVideoClassification}
         if is_torch_available()
         else {}
     )

     __UpperCamelCase  =					False
     __UpperCamelCase  =					False
     __UpperCamelCase  =					False
     __UpperCamelCase  =					False






     def      lowerCamelCase__     (	self  :					int				):
           lowerCAmelCase					:	Tuple       =   VideoMAEModelTester(self				)
           lowerCAmelCase					:	str       =   ConfigTester(self					,				config_class=UpperCamelCase_					,				has_text_modality=UpperCamelCase_					,				hidden_size=3_7				)






     def      lowerCamelCase__     (	self  :					Any					,				UpperCamelCase_  :					Tuple					,				UpperCamelCase_  :					Tuple					,				UpperCamelCase_  :					str=False				):
           lowerCAmelCase					:	Optional[int]       =   copy.deepcopy(UpperCamelCase_				)

           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
                 lowerCAmelCase					:	List[str]       =   torch.ones((self.model_tester.num_masks,)				)
                 lowerCAmelCase					:	Any       =   torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0				)				)]				)
                 lowerCAmelCase					:	List[Any]       =   mask.expand(self.model_tester.batch_size					,				-1				).bool()
                 lowerCAmelCase					:	Optional[int]       =   bool_masked_pos.to(UpperCamelCase_				)

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

           return inputs_dict






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






     @unittest.skip(reason='''VideoMAE does not use inputs_embeds'''				)
     def      lowerCamelCase__     (	self  :					List[Any]				):
           pass






     def      lowerCamelCase__     (	self  :					int				):
           lowerCAmelCase,      lowerCAmelCase					:	Optional[int]       =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 lowerCAmelCase					:	str       =   model_class(UpperCamelCase_				)
                 self.assertIsInstance(model.get_input_embeddings()					,				(nn.Module)				)
                 lowerCAmelCase					:	Optional[int]       =   model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(UpperCamelCase_					,				nn.Linear				)				)






     def      lowerCamelCase__     (	self  :					Dict				):
           lowerCAmelCase,      lowerCAmelCase					:	Tuple       =   self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowerCAmelCase					:	Optional[Any]       =   ['''pixel_values''']
                 self.assertListEqual(arg_names[:1]					,				UpperCamelCase_				)






     def      lowerCamelCase__     (	self  :					Union[str, Any]				):
           lowerCAmelCase					:	int       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase_				)






     def      lowerCamelCase__     (	self  :					Dict				):
           lowerCAmelCase					:	str       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*UpperCamelCase_				)






     @slow
     def      lowerCamelCase__     (	self  :					Dict				):
           for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase					:	Dict       =   VideoMAEModel.from_pretrained(UpperCamelCase_				)
                 self.assertIsNotNone(UpperCamelCase_				)






     def      lowerCamelCase__     (	self  :					int				):
           if not self.has_attentions:
                 pass

           else:
                 lowerCAmelCase,      lowerCAmelCase					:	Union[str, Any]       =   self.model_tester.prepare_config_and_inputs_for_common()
                 lowerCAmelCase					:	Union[str, Any]       =   True

                 for model_class in self.all_model_classes:
                       lowerCAmelCase					:	Tuple       =   self.model_tester.seq_length - self.model_tester.num_masks
                       lowerCAmelCase					:	Dict       =   (
                           num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                       )

                       lowerCAmelCase					:	int       =   True
                       lowerCAmelCase					:	int       =   False
                       lowerCAmelCase					:	int       =   True
                       lowerCAmelCase					:	str       =   model_class(UpperCamelCase_				)
                       model.to(UpperCamelCase_				)
                       model.eval()
                       with torch.no_grad():
                             lowerCAmelCase					:	Tuple       =   model(**self._prepare_for_class(UpperCamelCase_					,				UpperCamelCase_				)				)
                       lowerCAmelCase					:	str       =   outputs.attentions
                       self.assertEqual(len(UpperCamelCase_				)					,				self.model_tester.num_hidden_layers				)

                       # check that output_attentions also work using config
                       del inputs_dict["output_attentions"]
                       lowerCAmelCase					:	Tuple       =   True
                       lowerCAmelCase					:	Dict       =   model_class(UpperCamelCase_				)
                       model.to(UpperCamelCase_				)
                       model.eval()
                       with torch.no_grad():
                             lowerCAmelCase					:	List[Any]       =   model(**self._prepare_for_class(UpperCamelCase_					,				UpperCamelCase_				)				)
                       lowerCAmelCase					:	int       =   outputs.attentions
                       self.assertEqual(len(UpperCamelCase_				)					,				self.model_tester.num_hidden_layers				)

                       self.assertListEqual(
                           list(attentions[0].shape[-3:]				)					,				[self.model_tester.num_attention_heads, seq_len, seq_len]					,				)
                       lowerCAmelCase					:	Optional[Any]       =   len(UpperCamelCase_				)

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

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

                       lowerCAmelCase					:	List[str]       =   outputs.attentions

                       self.assertEqual(len(UpperCamelCase_				)					,				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      lowerCamelCase__     (	self  :					str				):
           def check_hidden_states_output(UpperCamelCase_  :					str					,				UpperCamelCase_  :					Union[str, Any]					,				UpperCamelCase_  :					int				):
                 lowerCAmelCase					:	str       =   model_class(UpperCamelCase_				)
                 model.to(UpperCamelCase_				)
                 model.eval()

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

                 lowerCAmelCase					:	Any       =   outputs.hidden_states
                 lowerCAmelCase					:	Any       =   self.model_tester.num_hidden_layers + 1
                 self.assertEqual(len(UpperCamelCase_				)					,				UpperCamelCase_				)

                 lowerCAmelCase					:	Dict       =   self.model_tester.seq_length - self.model_tester.num_masks
                 lowerCAmelCase					:	Tuple       =   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]					,				)

           lowerCAmelCase,      lowerCAmelCase					:	Optional[Any]       =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 lowerCAmelCase					:	List[Any]       =   True
                 check_hidden_states_output(UpperCamelCase_					,				UpperCamelCase_					,				UpperCamelCase_				)

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

                 check_hidden_states_output(UpperCamelCase_					,				UpperCamelCase_					,				UpperCamelCase_				)






     @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''				)
     def      lowerCamelCase__     (	self  :					Optional[int]				):
           pass



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






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






     @cached_property
     def      lowerCamelCase__     (	self  :					List[str]				):
           # 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      lowerCamelCase__     (	self  :					Dict				):
           lowerCAmelCase					:	Optional[Any]       =   VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''				).to(
               UpperCamelCase_				)

           lowerCAmelCase					:	Tuple       =   self.default_image_processor
           lowerCAmelCase					:	Union[str, Any]       =   prepare_video()
           lowerCAmelCase					:	Union[str, Any]       =   image_processor(UpperCamelCase_					,				return_tensors='''pt'''				).to(UpperCamelCase_				)

           # forward pass
           with torch.no_grad():
                 lowerCAmelCase					:	Tuple       =   model(**UpperCamelCase_				)

           # verify the logits
           lowerCAmelCase					:	int       =   torch.Size((1, 4_0_0)				)
           self.assertEqual(outputs.logits.shape					,				UpperCamelCase_				)

           lowerCAmelCase					:	Optional[int]       =   torch.tensor([0.3_669, -0.0_688, -0.2_421]				).to(UpperCamelCase_				)

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






     @slow
     def      lowerCamelCase__     (	self  :					Any				):
           lowerCAmelCase					:	Optional[Any]       =   VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''				).to(UpperCamelCase_				)

           lowerCAmelCase					:	Optional[Any]       =   self.default_image_processor
           lowerCAmelCase					:	Optional[int]       =   prepare_video()
           lowerCAmelCase					:	Tuple       =   image_processor(UpperCamelCase_					,				return_tensors='''pt'''				).to(UpperCamelCase_				)

           # add boolean mask, indicating which patches to mask
           lowerCAmelCase					:	Union[str, Any]       =   hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos'''					,				filename='''bool_masked_pos.pt'''				)
           lowerCAmelCase					:	Tuple       =   torch.load(UpperCamelCase_				)

           # forward pass
           with torch.no_grad():
                 lowerCAmelCase					:	Tuple       =   model(**UpperCamelCase_				)

           # verify the logits
           lowerCAmelCase					:	Tuple       =   torch.Size([1, 1_4_0_8, 1_5_3_6]				)
           lowerCAmelCase					:	Optional[int]       =   torch.tensor(
               [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]]					,				device=UpperCamelCase_				)
           self.assertEqual(outputs.logits.shape					,				UpperCamelCase_				)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]					,				UpperCamelCase_					,				atol=1E-4				)				)

           # verify the loss (`config.norm_pix_loss` = `True`)
           lowerCAmelCase					:	str       =   torch.tensor([0.5_142]					,				device=UpperCamelCase_				)
           self.assertTrue(torch.allclose(outputs.loss					,				UpperCamelCase_					,				atol=1E-4				)				)

           # verify the loss (`config.norm_pix_loss` = `False`)
           lowerCAmelCase					:	int       =   VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''					,				norm_pix_loss=UpperCamelCase_				).to(
               UpperCamelCase_				)

           with torch.no_grad():
                 lowerCAmelCase					:	Optional[Any]       =   model(**UpperCamelCase_				)

           lowerCAmelCase					:	Optional[Any]       =   torch.tensor(torch.tensor([0.6_469]				)					,				device=UpperCamelCase_				)
           self.assertTrue(torch.allclose(outputs.loss					,				UpperCamelCase_					,				atol=1E-4				)				)






"""simple docstring"""





from __future__ import annotations


def      _snake_case						(						_snake_case				:			list[int] ,   _snake_case				:			int			):
      if len(_snake_case			) == 0:
            return False
      lowerCAmelCase					:	List[Any]       =   len(_snake_case			) // 2
      if a_list[midpoint] == item:
            return True
      if item < a_list[midpoint]:
            return binary_search(a_list[:midpoint] ,   _snake_case			)
      else:
            return binary_search(a_list[midpoint + 1 :] ,   _snake_case			)


if __name__ == "__main__":
 snake_case__		:     List[str]    				=				input('''Enter numbers separated by comma:\n''').strip()
 snake_case__		:     Optional[int]    				=				[int(item.strip()) for item in user_input.split(''',''')]
 snake_case__		:     Dict    				=				int(input('''Enter the number to be found in the list:\n''').strip())
 snake_case__		:     str    				=				'''''' if binary_search(sequence, target) else '''not '''
 print(f"""{target} was {not_str}found in {sequence}""")


