


'''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 			lowerCamelCase_   :






      def __init__(					self				:    str						,						_A				:    Optional[int]						,						_A				:    Optional[Any]=13						,						_A				:    List[str]=10						,						_A				:    List[Any]=3						,						_A				:    str=2						,						_A				:    Union[str, Any]=2						,						_A				:    Dict=2						,						_A				:    Dict=True						,						_A				:    int=True						,						_A				:    Dict=32						,						_A				:    List[Any]=5						,						_A				:    int=4						,						_A				:    Union[str, Any]=37						,						_A				:    List[Any]="gelu"						,						_A				:    Any=0.1						,						_A				:    Any=0.1						,						_A				:    Optional[Any]=10						,						_A				:    int=0.0_2						,						_A				:    Any=0.9						,						_A				:    Optional[int]=None						,						):


          '''simple docstring'''


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

          # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
          UpperCAmelCase__			:		int													=				(image_size // patch_size) ** 2
          UpperCAmelCase__			:		Tuple													=				(num_frames // tubelet_size) * self.num_patches_per_frame

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






      def lowercase_							(					self				:    int ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] )

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

          UpperCAmelCase__			:		List[Any]													=				self.get_config()

          return config, pixel_values, labels






      def lowercase_							(					self				:    List[Any] ):


          '''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=_a						,						initializer_range=self.initializer_range						,						)






      def lowercase_							(					self				:    str						,						_A				:    Optional[Any]						,						_A				:    List[Any]						,						_A				:    int ):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				VideoMAEModel(config=_a )
          model.to(_a )
          model.eval()
          UpperCAmelCase__			:		Any													=				model(_a )
          self.parent.assertEqual(result.last_hidden_state.shape						,						(self.batch_size, self.seq_length, self.hidden_size) )






      def lowercase_							(					self				:    int						,						_A				:    int						,						_A				:    Any						,						_A				:    str ):


          '''simple docstring'''


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

          UpperCAmelCase__			:		Dict													=				model(_a						,						_a )
          # model only returns predictions for masked patches
          UpperCAmelCase__			:		Optional[Any]													=				mask.sum().item()
          UpperCAmelCase__			:		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 lowercase_							(					self				:    int ):


          '''simple docstring'''


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




@require_torch
class 			lowerCamelCase_   (	__lowercase     ,       __lowercase     ,       unittest.TestCase      ):
      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 lowercase_							(					self				:    Dict ):


          '''simple docstring'''


          UpperCAmelCase__			:		int													=				VideoMAEModelTester(self )
          UpperCAmelCase__			:		int													=				ConfigTester(self						,						config_class=_a						,						has_text_modality=_a						,						hidden_size=37 )






      def lowercase_							(					self				:    int						,						_A				:    Any						,						_A				:    Optional[Any]						,						_A				:    Tuple=False ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[str]													=				copy.deepcopy(_a )

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

          if return_labels:
              if model_class in [
                  *get_values(_a ),
              ]:
                  UpperCAmelCase__			:		List[str]													=				torch.zeros(
                      self.model_tester.batch_size						,						dtype=torch.long						,						device=_a )

          return inputs_dict






      def lowercase_							(					self				:    Tuple ):


          '''simple docstring'''


          self.config_tester.run_common_tests()






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


          '''simple docstring'''


          pass






      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		Any													=				self.model_tester.prepare_config_and_inputs_for_common()

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






      def lowercase_							(					self				:    List[str] ):


          '''simple docstring'''


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

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

              UpperCAmelCase__			:		Dict													=				['''pixel_values''']
              self.assertListEqual(arg_names[:1]						,						_a )






      def lowercase_							(					self				:    Tuple ):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_a )






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Tuple													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*_a )






      @slow
      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase__			:		Union[str, Any]													=				VideoMAEModel.from_pretrained(_a )
              self.assertIsNotNone(_a )






      def lowercase_							(					self				:    str ):


          '''simple docstring'''


          if not self.has_attentions:
              pass

          else:
              UpperCAmelCase__			:		Dict													=				self.model_tester.prepare_config_and_inputs_for_common()
              UpperCAmelCase__			:		Dict													=				True

              for model_class in self.all_model_classes:
                  UpperCAmelCase__			:		int													=				self.model_tester.seq_length - self.model_tester.num_masks
                  UpperCAmelCase__			:		Optional[Any]													=				(
                      num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                  )

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

                  self.assertListEqual(
                      list(attentions[0].shape[-3:] )						,						[self.model_tester.num_attention_heads, seq_len, seq_len]						,						)
                  UpperCAmelCase__			:		int													=				len(_a )

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

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

                  UpperCAmelCase__			:		Tuple													=				outputs.attentions

                  self.assertEqual(len(_a )						,						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 lowercase_							(					self				:    List[str] ):


          '''simple docstring'''


          def check_hidden_states_output(_A				:    str						,						_A				:    Dict						,						_A				:    Dict ):
              UpperCAmelCase__			:		Optional[int]													=				model_class(_a )
              model.to(_a )
              model.eval()

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

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

              UpperCAmelCase__			:		Optional[int]													=				self.model_tester.seq_length - self.model_tester.num_masks
              UpperCAmelCase__			:		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]						,						)

          UpperCAmelCase__			:		List[str]													=				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCAmelCase__			:		List[str]													=				True
              check_hidden_states_output(_a						,						_a						,						_a )

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

              check_hidden_states_output(_a						,						_a						,						_a )






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


          '''simple docstring'''


          pass





def 					a__       (					)    ->	Tuple:
    UpperCAmelCase__			:		str													=				hf_hub_download(
        repo_id='''hf-internal-testing/spaghetti-video'''  ,					filename='''eating_spaghetti.npy'''  ,					repo_type='''dataset'''						)
    UpperCAmelCase__			:		List[Any]													=				np.load(__a						)
    return list(__a						)







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






      @cached_property
      def lowercase_							(					self				:    Tuple ):


          '''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 lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[int]													=				VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics''' ).to(
              _a )

          UpperCAmelCase__			:		Any													=				self.default_image_processor
          UpperCAmelCase__			:		Dict													=				prepare_video()
          UpperCAmelCase__			:		List[str]													=				image_processor(_a						,						return_tensors='''pt''' ).to(_a )

          # forward pass
          with torch.no_grad():
              UpperCAmelCase__			:		Any													=				model(**_a )

          # verify the logits
          UpperCAmelCase__			:		List[str]													=				torch.Size((1, 400) )
          self.assertEqual(outputs.logits.shape						,						_a )

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

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






      @slow
      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		int													=				VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' ).to(_a )

          UpperCAmelCase__			:		Dict													=				self.default_image_processor
          UpperCAmelCase__			:		List[Any]													=				prepare_video()
          UpperCAmelCase__			:		Dict													=				image_processor(_a						,						return_tensors='''pt''' ).to(_a )

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

          # forward pass
          with torch.no_grad():
              UpperCAmelCase__			:		List[Any]													=				model(**_a )

          # verify the logits
          UpperCAmelCase__			:		int													=				torch.Size([1, 1_408, 1_536] )
          UpperCAmelCase__			:		Optional[Any]													=				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=_a )
          self.assertEqual(outputs.logits.shape						,						_a )
          self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]						,						_a						,						atol=1e-4 ) )

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

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

          with torch.no_grad():
              UpperCAmelCase__			:		Any													=				model(**_a )

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



def    __UpperCAmelCase (	__a      :			float	)			->     float:

					"""simple docstring"""

					return 10 - x * x



def    __UpperCAmelCase (	__a      :			float		,__a      :			float	)			->     float:

					"""simple docstring"""

					if equation(__a	) * equation(__a	) >= 0:
										raise ValueError('''Wrong space!'''	)

					_a	:    Dict         =		a
					while (b - a) >= 0.01:
										# Find middle point
										_a	:    Any         =		(a + b) / 2
										# Check if middle point is root
										if equation(__a	) == 0.0:
															break
										# Decide the side to repeat the steps
										if equation(__a	) * equation(__a	) < 0:
															_a	:    str         =		c
										else:
															_a	:    Union[str, Any]         =		c
					return c


if __name__ == "__main__":
			import doctest

			doctest.testmod()

			print(bisection(-2, 5))
			print(bisection(0, 6))



