


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     lowercase__  :






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




         """simple docstring"""







         UpperCAmelCase__										=     parent
         UpperCAmelCase__										=     batch_size
         UpperCAmelCase__										=     image_size
         UpperCAmelCase__										=     num_channels
         UpperCAmelCase__										=     patch_size
         UpperCAmelCase__										=     tubelet_size
         UpperCAmelCase__										=     num_frames
         UpperCAmelCase__										=     is_training
         UpperCAmelCase__										=     use_labels
         UpperCAmelCase__										=     hidden_size
         UpperCAmelCase__										=     num_hidden_layers
         UpperCAmelCase__										=     num_attention_heads
         UpperCAmelCase__										=     intermediate_size
         UpperCAmelCase__										=     hidden_act
         UpperCAmelCase__										=     hidden_dropout_prob
         UpperCAmelCase__										=     attention_probs_dropout_prob
         UpperCAmelCase__										=     type_sequence_label_size
         UpperCAmelCase__										=     initializer_range
         UpperCAmelCase__										=     mask_ratio
         UpperCAmelCase__										=     scope

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

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






  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







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

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

         UpperCAmelCase__										=     self.get_config()

         return config, pixel_values, labels






  def 			_UpperCAmelCase  (					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=_lowercase		,						initializer_range=self.initializer_range		,						)






  def 			_UpperCAmelCase  (					self      :					Optional[int]		,						_lowercase      :					int		,						_lowercase      :					Optional[Any]		,						_lowercase      :					Any ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					List[str]		,						_lowercase      :					List[Any]		,						_lowercase      :					Optional[Any]		,						_lowercase      :					Optional[int] ):




         """simple docstring"""







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

         UpperCAmelCase__										=     model(_lowercase		,						_lowercase )
         # model only returns predictions for masked patches
         UpperCAmelCase__										=     mask.sum().item()
         UpperCAmelCase__										=     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      :					Tuple ):




         """simple docstring"""







         UpperCAmelCase__										=     self.prepare_config_and_inputs()
         UpperCAmelCase__						,			UpperCAmelCase__						,			UpperCAmelCase__										=     config_and_inputs
         UpperCAmelCase__										=     {"pixel_values": pixel_values}
         return config, inputs_dict




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

  A__=  False
  A__=  False
  A__=  False
  A__=  False






  def 			_UpperCAmelCase  (					self      :					str ):




         """simple docstring"""







         UpperCAmelCase__										=     VideoMAEModelTester(self )
         UpperCAmelCase__										=     ConfigTester(self		,						config_class=_lowercase		,						has_text_modality=_lowercase		,						hidden_size=37 )






  def 			_UpperCAmelCase  (					self      :					Any		,						_lowercase      :					List[Any]		,						_lowercase      :					int		,						_lowercase      :					Dict=False ):




         """simple docstring"""







         UpperCAmelCase__										=     copy.deepcopy(_lowercase )

         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__										=     torch.ones((self.model_tester.num_masks,) )
                UpperCAmelCase__										=     torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] )
                UpperCAmelCase__										=     mask.expand(self.model_tester.batch_size		,						-1 ).bool()
                UpperCAmelCase__										=     bool_masked_pos.to(_lowercase )

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

         return inputs_dict






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         self.config_tester.run_common_tests()






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




         """simple docstring"""







         pass






  def 			_UpperCAmelCase  (					self      :					Union[str, Any] ):




         """simple docstring"""







         UpperCAmelCase__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

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






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

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

                UpperCAmelCase__										=     ["pixel_values"]
                self.assertListEqual(arg_names[:1]		,						_lowercase )






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowercase )






  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*_lowercase )






  @slow
  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase__										=     VideoMAEModel.from_pretrained(_lowercase )
                self.assertIsNotNone(_lowercase )






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         if not self.has_attentions:
                pass

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

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

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

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

                       # Check attention is always last and order is fine
                       UpperCAmelCase__										=     True
                       UpperCAmelCase__										=     True
                       UpperCAmelCase__										=     model_class(_lowercase )
                       model.to(_lowercase )
                       model.eval()
                       with torch.no_grad():
                              UpperCAmelCase__										=     model(**self._prepare_for_class(_lowercase		,						_lowercase ) )

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

                       UpperCAmelCase__										=     outputs.attentions

                       self.assertEqual(len(_lowercase )		,						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      :					List[Any] ):




         """simple docstring"""







         def check_hidden_states_output(_lowercase      :					List[str]		,						_lowercase      :					Optional[int]		,						_lowercase      :					List[Any] ):
                UpperCAmelCase__										=     model_class(_lowercase )
                model.to(_lowercase )
                model.eval()

                with torch.no_grad():
                       UpperCAmelCase__										=     model(**self._prepare_for_class(_lowercase		,						_lowercase ) )

                UpperCAmelCase__										=     outputs.hidden_states
                UpperCAmelCase__										=     self.model_tester.num_hidden_layers + 1
                self.assertEqual(len(_lowercase )		,						_lowercase )

                UpperCAmelCase__										=     self.model_tester.seq_length - self.model_tester.num_masks
                UpperCAmelCase__										=     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__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                UpperCAmelCase__										=     True
                check_hidden_states_output(_lowercase		,						_lowercase		,						_lowercase )

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

                check_hidden_states_output(_lowercase		,						_lowercase		,						_lowercase )






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




         """simple docstring"""







         pass





def __UpperCAmelCase       (		)						->					Optional[Any]:



       '''simple docstring'''





       UpperCAmelCase__										=     hf_hub_download(
           repo_id="hf-internal-testing/spaghetti-video" ,						filename="eating_spaghetti.npy" ,						repo_type="dataset"			)
       UpperCAmelCase__										=     np.load(__A			)
       return list(__A			)




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






  @cached_property
  def 			_UpperCAmelCase  (					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 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     VideoMAEForVideoClassification.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics" ).to(
             _lowercase )

         UpperCAmelCase__										=     self.default_image_processor
         UpperCAmelCase__										=     prepare_video()
         UpperCAmelCase__										=     image_processor(_lowercase		,						return_tensors="pt" ).to(_lowercase )

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

         # verify the logits
         UpperCAmelCase__										=     torch.Size((1, 4_00) )
         self.assertEqual(outputs.logits.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1] ).to(_lowercase )

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






  @slow
  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short" ).to(_lowercase )

         UpperCAmelCase__										=     self.default_image_processor
         UpperCAmelCase__										=     prepare_video()
         UpperCAmelCase__										=     image_processor(_lowercase		,						return_tensors="pt" ).to(_lowercase )

         # add boolean mask, indicating which patches to mask
         UpperCAmelCase__										=     hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos"		,						filename="bool_masked_pos.pt" )
         UpperCAmelCase__										=     torch.load(_lowercase )

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

         # verify the logits
         UpperCAmelCase__										=     torch.Size([1, 14_08, 15_36] )
         UpperCAmelCase__										=     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=_lowercase )
         self.assertEqual(outputs.logits.shape		,						_lowercase )
         self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,						_lowercase		,						atol=1E-4 ) )

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

         # verify the loss (`config.norm_pix_loss` = `False`)
         UpperCAmelCase__										=     VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short"		,						norm_pix_loss=_lowercase ).to(
             _lowercase )

         with torch.no_grad():
                UpperCAmelCase__										=     model(**_lowercase )

         UpperCAmelCase__										=     torch.tensor(torch.tensor([0.6_4_6_9] )		,						device=_lowercase )
         self.assertTrue(torch.allclose(outputs.loss		,						_lowercase		,						atol=1E-4 ) )









from __future__ import annotations

import math





def __UpperCAmelCase       (		__A ,						__A			)						->					float:



       '''simple docstring'''





       UpperCAmelCase__										=     u
       for i in range(1 ,						__A			):
              UpperCAmelCase__										=     temp * (u - i)
       return temp





def __UpperCAmelCase       (		)						->					None:



       '''simple docstring'''





       UpperCAmelCase__										=     int(input("enter the numbers of values: "			)			)
       UpperCAmelCase__										=     []
       for _ in range(__A			):
              y.append([]			)
       for i in range(__A			):
              for j in range(__A			):
                     y[i].append(__A			)
                     UpperCAmelCase__										=     0

       print("enter the values of parameters in a list: "			)
       UpperCAmelCase__										=     list(map(__A ,						input().split()			)			)

       print("enter the values of corresponding parameters: "			)
       for i in range(__A			):
              UpperCAmelCase__										=     float(input()			)

       UpperCAmelCase__										=     int(input("enter the value to interpolate: "			)			)
       UpperCAmelCase__										=     (value - x[0]) / (x[1] - x[0])

       # for calculating forward difference table

       for i in range(1 ,						__A			):
              for j in range(n - i			):
                     UpperCAmelCase__										=     y[j + 1][i - 1] - y[j][i - 1]

       UpperCAmelCase__										=     y[0][0]
       for i in range(1 ,						__A			):
              summ += (ucal(__A ,						__A			) * y[0][i]) / math.factorial(__A			)

       print(F"""the value at {value} is {summ}"""			)


if __name__ == "__main__":
    main()





