





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




   """simple docstring"""



   def __init__(					self					,  lowercase					,  lowercase=13					,  lowercase=10					,  lowercase=3					,  lowercase=2					,  lowercase=2					,  lowercase=2					,  lowercase=True					,  lowercase=True					,  lowercase=32					,  lowercase=5					,  lowercase=4					,  lowercase=37					,  lowercase="gelu"					,  lowercase=0.1					,  lowercase=0.1					,  lowercase=10					,  lowercase=0.02					,  lowercase=0.9					,  lowercase=None					,  )			->						List[str]:







          '''simple docstring'''

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

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

          # use this variable to define bool_masked_pos
          a__						:	List[str] =				int(mask_ratio * self.seq_length)

   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          a__						:	Union[str, Any] =				floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size])

          a__						:	Union[str, Any] =				None
          if self.use_labels:
                 a__						:	List[str] =				ids_tensor([self.batch_size]					,  self.type_sequence_label_size)

          a__						:	List[str] =				self.get_config()

          return config, pixel_values, labels

   def 							__lowercase		(					self)			->						Dict:







          '''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 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase)			->						int:







          '''simple docstring'''

          a__						:	Any =				VideoMAEModel(config=lowercase)
          model.to(lowercase)
          model.eval()
          a__						:	List[Any] =				model(lowercase)
          self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size))

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase)			->						List[str]:







          '''simple docstring'''

          a__						:	Dict =				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
          a__						:	Optional[int] =				torch.ones((self.num_masks,))
          a__						:	Tuple =				torch.cat([mask, torch.zeros(self.seq_length - mask.size(0))])
          a__						:	Tuple =				mask.expand(self.batch_size					,  -1).bool()

          a__						:	Any =				model(lowercase					,  lowercase)
          # model only returns predictions for masked patches
          a__						:	Optional[int] =				mask.sum().item()
          a__						:	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)			->						List[Any]:







          '''simple docstring'''

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





@require_torch
class  A__    (							__UpperCAmelCase ,  __UpperCAmelCase ,  unittest.TestCase	):




   """simple docstring"""



   __A   :				Optional[int]         =  (
       (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
   )
   __A   :				Optional[int]         =  (
       {'''feature-extraction''': VideoMAEModel, '''video-classification''': VideoMAEForVideoClassification}
       if is_torch_available()
       else {}
   )

   __A   :				int         =  False
   __A   :				Tuple         =  False
   __A   :				int         =  False
   __A   :				List[str]         =  False

   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          a__						:	Union[str, Any] =				VideoMAEModelTester(self)
          a__						:	str =				ConfigTester(self					,  config_class=lowercase					,  has_text_modality=lowercase					,  hidden_size=37)

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase=False)			->						List[str]:







          '''simple docstring'''

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

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

          return inputs_dict

   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          self.config_tester.run_common_tests()

   @unittest.skip(reason='VideoMAE does not use inputs_embeds')
   def 							__lowercase		(					self)			->						List[str]:







          '''simple docstring'''

          pass

   def 							__lowercase		(					self)			->						Optional[int]:







          '''simple docstring'''

          a__ ,							a__						:	str =				self.model_tester.prepare_config_and_inputs_for_common()

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

   def 							__lowercase		(					self)			->						str:







          '''simple docstring'''

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

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

                 a__						:	Dict =				['pixel_values']
                 self.assertListEqual(arg_names[:1]					,  lowercase)

   def 							__lowercase		(					self)			->						Optional[Any]:







          '''simple docstring'''

          a__						:	Any =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase)

   def 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          a__						:	int =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*lowercase)

   @slow
   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a__						:	Dict =				VideoMAEModel.from_pretrained(lowercase)
                 self.assertIsNotNone(lowercase)

   def 							__lowercase		(					self)			->						List[str]:







          '''simple docstring'''

          if not self.has_attentions:
                 pass

          else:
                 a__ ,							a__						:	Optional[Any] =				self.model_tester.prepare_config_and_inputs_for_common()
                 a__						:	Optional[Any] =				True

                 for model_class in self.all_model_classes:
                        a__						:	Optional[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
                        )

                        a__						:	Optional[Any] =				True
                        a__						:	List[str] =				False
                        a__						:	Dict =				True
                        a__						:	Any =				model_class(lowercase)
                        model.to(lowercase)
                        model.eval()
                        with torch.no_grad():
                               a__						:	str =				model(**self._prepare_for_class(lowercase					,  lowercase))
                        a__						:	List[str] =				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"]
                        a__						:	List[Any] =				True
                        a__						:	int =				model_class(lowercase)
                        model.to(lowercase)
                        model.eval()
                        with torch.no_grad():
                               a__						:	Any =				model(**self._prepare_for_class(lowercase					,  lowercase))
                        a__						:	Tuple =				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]					,  )
                        a__						:	str =				len(lowercase)

                        # Check attention is always last and order is fine
                        a__						:	Tuple =				True
                        a__						:	int =				True
                        a__						:	Optional[int] =				model_class(lowercase)
                        model.to(lowercase)
                        model.eval()
                        with torch.no_grad():
                               a__						:	Dict =				model(**self._prepare_for_class(lowercase					,  lowercase))

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

                        a__						:	str =				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 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          def check_hidden_states_output(lowercase					,  lowercase					,  lowercase):
                 a__						:	Optional[Any] =				model_class(lowercase)
                 model.to(lowercase)
                 model.eval()

                 with torch.no_grad():
                        a__						:	int =				model(**self._prepare_for_class(lowercase					,  lowercase))

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

                 a__						:	str =				self.model_tester.seq_length - self.model_tester.num_masks
                 a__						:	Union[str, Any] =				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__ ,							a__						:	Optional[Any] =				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 a__						:	int =				True
                 check_hidden_states_output(lowercase					,  lowercase					,  lowercase)

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 a__						:	str =				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 							__lowercase		(					self)			->						Any:







          '''simple docstring'''

          pass



def A_	(		)  ->		int:
       a__						:	Union[str, Any] =				hf_hub_download(
           repo_id='hf-internal-testing/spaghetti-video'		,      filename='eating_spaghetti.npy'		,      repo_type='dataset'				)
       a__						:	Optional[int] =				np.load(A__				)
       return list(A__				)





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




   """simple docstring"""



   @cached_property
   def 							__lowercase		(					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 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          a__						:	Any =				VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics').to(
              lowercase)

          a__						:	int =				self.default_image_processor
          a__						:	Optional[int] =				prepare_video()
          a__						:	str =				image_processor(lowercase					,  return_tensors='pt').to(lowercase)

          # forward pass
          with torch.no_grad():
                 a__						:	Dict =				model(**lowercase)

          # verify the logits
          a__						:	Any =				torch.Size((1, 400))
          self.assertEqual(outputs.logits.shape					,  lowercase)

          a__						:	Any =				torch.tensor([0.36_69, -0.06_88, -0.24_21]).to(lowercase)

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

   @slow
   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          a__						:	Optional[Any] =				VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short').to(lowercase)

          a__						:	Tuple =				self.default_image_processor
          a__						:	List[str] =				prepare_video()
          a__						:	List[str] =				image_processor(lowercase					,  return_tensors='pt').to(lowercase)

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

          # forward pass
          with torch.no_grad():
                 a__						:	Optional[int] =				model(**lowercase)

          # verify the logits
          a__						:	int =				torch.Size([1, 1408, 1536])
          a__						:	List[str] =				torch.tensor(
              [[0.79_94, 0.96_12, 0.85_08], [0.74_01, 0.89_58, 0.83_02], [0.58_62, 0.74_68, 0.73_25]]					,  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`)
          a__						:	int =				torch.tensor([0.51_42]					,  device=lowercase)
          self.assertTrue(torch.allclose(outputs.loss					,  lowercase					,  atol=1e-4))

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

          with torch.no_grad():
                 a__						:	Optional[int] =				model(**lowercase)

          a__						:	Tuple =				torch.tensor(torch.tensor([0.64_69])					,  device=lowercase)
          self.assertTrue(torch.allclose(outputs.loss					,  lowercase					,  atol=1e-4))






'''simple docstring'''


def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase      ):

		if density <= 0:
				raise ValueError("Impossible fluid density"      )
		if bulk_modulus <= 0:
				raise ValueError("Impossible bulk modulus"      )

		return (bulk_modulus / density) ** 0.5


if __name__ == "__main__":
						import doctest

						doctest.testmod()



