


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

 '''simple docstring'''

 def __init__(     self		,				_UpperCAmelCase		,				_UpperCAmelCase=13		,				_UpperCAmelCase=10		,				_UpperCAmelCase=3		,				_UpperCAmelCase=2		,				_UpperCAmelCase=2		,				_UpperCAmelCase=2		,				_UpperCAmelCase=True		,				_UpperCAmelCase=True		,				_UpperCAmelCase=32		,				_UpperCAmelCase=5		,				_UpperCAmelCase=4		,				_UpperCAmelCase=37		,				_UpperCAmelCase="gelu"		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=0.1		,				_UpperCAmelCase=10		,				_UpperCAmelCase=0.02		,				_UpperCAmelCase=0.9		,				_UpperCAmelCase=None		,				):
       snake_case_	=				parent
       snake_case_	=				batch_size
       snake_case_	=				image_size
       snake_case_	=				num_channels
       snake_case_	=				patch_size
       snake_case_	=				tubelet_size
       snake_case_	=				num_frames
       snake_case_	=				is_training
       snake_case_	=				use_labels
       snake_case_	=				hidden_size
       snake_case_	=				num_hidden_layers
       snake_case_	=				num_attention_heads
       snake_case_	=				intermediate_size
       snake_case_	=				hidden_act
       snake_case_	=				hidden_dropout_prob
       snake_case_	=				attention_probs_dropout_prob
       snake_case_	=				type_sequence_label_size
       snake_case_	=				initializer_range
       snake_case_	=				mask_ratio
       snake_case_	=				scope

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

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



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				floats_tensor(
           [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] )

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

       snake_case_	=				self.get_config()

       return config, pixel_values, labels



 def 						UpperCamelCase__					(     self ):
       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=_snake_case		,				initializer_range=self.initializer_range		,				)



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				VideoMAEModel(config=_snake_case )
       model.to(_snake_case )
       model.eval()
       snake_case_	=				model(_snake_case )
       self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size) )



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
       snake_case_	=				VideoMAEForPreTraining(_snake_case )
       model.to(_snake_case )
       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
       snake_case_	=				torch.ones((self.num_masks,) )
       snake_case_	=				torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] )
       snake_case_	=				mask.expand(self.batch_size		,				-1 ).bool()

       snake_case_	=				model(_snake_case		,				_snake_case )
       # model only returns predictions for masked patches
       snake_case_	=				mask.sum().item()
       snake_case_	=				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 ):
       snake_case_	=				self.prepare_config_and_inputs()
       snake_case_	,   snake_case_	,   snake_case_	=				config_and_inputs
       snake_case_	=				{'''pixel_values''': pixel_values}
       return config, inputs_dict







@require_torch
class lowerCAmelCase_						(			lowerCamelCase__ ,						lowerCamelCase__ ,						unittest.TestCase	):

 '''simple docstring'''

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

 __snake_case												=  False
 __snake_case												=  False
 __snake_case												=  False
 __snake_case												=  False



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				VideoMAEModelTester(self )
       snake_case_	=				ConfigTester(self		,				config_class=_snake_case		,				has_text_modality=_snake_case		,				hidden_size=37 )



 def 						UpperCamelCase__					(     self		,				_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase=False ):
       snake_case_	=				copy.deepcopy(_snake_case )

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

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

       return inputs_dict



 def 						UpperCamelCase__					(     self ):
       self.config_tester.run_common_tests()



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



 def 						UpperCamelCase__					(     self ):
       snake_case_	,   snake_case_	=				self.model_tester.prepare_config_and_inputs_for_common()

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



 def 						UpperCamelCase__					(     self ):
       snake_case_	,   snake_case_	=				self.model_tester.prepare_config_and_inputs_for_common()

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

             snake_case_	=				['''pixel_values''']
             self.assertListEqual(arg_names[:1]		,				_snake_case )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_snake_case )



 def 						UpperCamelCase__					(     self ):
       snake_case_	=				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*_snake_case )



 @slow
 def 						UpperCamelCase__					(     self ):




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



 def 						UpperCamelCase__					(     self ):




       if not self.has_attentions:
             pass

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

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

                   snake_case_	=				True
                   snake_case_	=				False
                   snake_case_	=				True
                   snake_case_	=				model_class(_snake_case )
                   model.to(_snake_case )
                   model.eval()
                   with torch.no_grad():
                         snake_case_	=				model(**self._prepare_for_class(_snake_case		,				_snake_case ) )
                   snake_case_	=				outputs.attentions
                   self.assertEqual(len(_snake_case )		,				self.model_tester.num_hidden_layers )

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

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

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

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

                   snake_case_	=				outputs.attentions

                   self.assertEqual(len(_snake_case )		,				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 ):




       def check_hidden_states_output(_UpperCAmelCase		,				_UpperCAmelCase		,				_UpperCAmelCase ):
             snake_case_	=				model_class(_snake_case )
             model.to(_snake_case )
             model.eval()

             with torch.no_grad():
                   snake_case_	=				model(**self._prepare_for_class(_snake_case		,				_snake_case ) )

             snake_case_	=				outputs.hidden_states
             snake_case_	=				self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(_snake_case )		,				_snake_case )

             snake_case_	=				self.model_tester.seq_length - self.model_tester.num_masks
             snake_case_	=				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]		,				)

       snake_case_	,   snake_case_	=				self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             snake_case_	=				True
             check_hidden_states_output(_snake_case		,				_snake_case		,				_snake_case )

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

             check_hidden_states_output(_snake_case		,				_snake_case		,				_snake_case )





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






def 	__lowerCAmelCase   ()->  int:




      """simple docstring"""

      snake_case_	=				hf_hub_download(
          repo_id='''hf-internal-testing/spaghetti-video''' , filename='''eating_spaghetti.npy''' , repo_type='''dataset''' )
      snake_case_	=				np.load(a__ )
      return list(a__ )







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

 '''simple docstring'''

 @cached_property
 def 						UpperCamelCase__					(     self ):
       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 ):
       snake_case_	=				VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics''' ).to(
           _snake_case )

       snake_case_	=				self.default_image_processor
       snake_case_	=				prepare_video()
       snake_case_	=				image_processor(_snake_case		,				return_tensors='''pt''' ).to(_snake_case )

       # forward pass
       with torch.no_grad():
             snake_case_	=				model(**_snake_case )

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

       snake_case_	=				torch.tensor([0.3_669, -0.0_688, -0.2_421] ).to(_snake_case )

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





 @slow
 def 						UpperCamelCase__					(     self ):
       snake_case_	=				VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short''' ).to(_snake_case )

       snake_case_	=				self.default_image_processor
       snake_case_	=				prepare_video()
       snake_case_	=				image_processor(_snake_case		,				return_tensors='''pt''' ).to(_snake_case )

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

       # forward pass
       with torch.no_grad():
             snake_case_	=				model(**_snake_case )

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

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

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

       with torch.no_grad():
             snake_case_	=				model(**_snake_case )

       snake_case_	=				torch.tensor(torch.tensor([0.6_469] )		,				device=_snake_case )
       self.assertTrue(torch.allclose(outputs.loss		,				_snake_case		,				atol=1E-4 ) )



def 	__lowerCAmelCase   (SCREAMING_SNAKE_CASE )->  int:




      """simple docstring"""

      if not isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ):
            snake_case_	=				f'''Input value of [number={number}] must be an integer'''
            raise TypeError(SCREAMING_SNAKE_CASE )

      if number < 1:
            snake_case_	=				f'''Input value of [number={number}] must be > 0'''
            raise ValueError(SCREAMING_SNAKE_CASE )

      snake_case_	=				1

      for i in range(1 , SCREAMING_SNAKE_CASE ):
            current_number *= 4 * i - 2
            current_number //= i + 1

      return current_number


if __name__ == "__main__":
   import doctest

   doctest.testmod()