





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





 """simple docstring"""


 def __init__(    self					,						__lowerCamelCase					,						__lowerCamelCase=13					,						__lowerCamelCase=10					,						__lowerCamelCase=3					,						__lowerCamelCase=2					,						__lowerCamelCase=2					,						__lowerCamelCase=2					,						__lowerCamelCase=True					,						__lowerCamelCase=True					,						__lowerCamelCase=32					,						__lowerCamelCase=5					,						__lowerCamelCase=4					,						__lowerCamelCase=37					,						__lowerCamelCase="gelu"					,						__lowerCamelCase=0.1					,						__lowerCamelCase=0.1					,						__lowerCamelCase=10					,						__lowerCamelCase=0.0_2					,						__lowerCamelCase=0.9					,						__lowerCamelCase=None					,						):



     '''simple docstring'''




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

     # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
     __A    :						List[str]						 =     (image_size // patch_size) ** 2
     __A    :						Any						 =     (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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						List[str]						 =     floats_tensor(
         [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]  )

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

     __A    :						Union[str, Any]						 =     self.get_config()

     return config, pixel_values, labels

 def 					UpperCamelCase__(    self  ):



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

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




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

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




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

     __A    :						Dict						 =     model(__lowerCamelCase					,						__lowerCamelCase  )
     # model only returns predictions for masked patches
     __A    :						Optional[Any]						 =     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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Optional[Any]						 =     self.prepare_config_and_inputs()
     __A ,       __A ,       __A    :						Union[str, Any]						 =     config_and_inputs
     __A    :						str						 =     {'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_torch
class 		__snake_case					(		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase     ):





 """simple docstring"""


 _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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						int						 =     VideoMAEModelTester(self  )
     __A    :						Optional[Any]						 =     ConfigTester(self					,						config_class=__lowerCamelCase					,						has_text_modality=__lowerCamelCase					,						hidden_size=37  )

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase=False  ):



     '''simple docstring'''




     __A    :						Any						 =     copy.deepcopy(__lowerCamelCase  )

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

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

     return inputs_dict

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

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



     '''simple docstring'''




     pass

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A ,       __A    :						List[Any]						 =     self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         __A    :						Union[str, Any]						 =     model_class(__lowerCamelCase  )
         self.assertIsInstance(model.get_input_embeddings()					,						(nn.Module)  )
         __A    :						Union[str, Any]						 =     model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(__lowerCamelCase					,						nn.Linear  )  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

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

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

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						int						 =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Tuple						 =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase  )

 @slow
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     if not self.has_attentions:
         pass

     else:
         __A ,       __A    :						int						 =     self.model_tester.prepare_config_and_inputs_for_common()
         __A    :						Tuple						 =     True

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

             __A    :						Tuple						 =     True
             __A    :						Optional[int]						 =     False
             __A    :						List[Any]						 =     True
             __A    :						Optional[int]						 =     model_class(__lowerCamelCase  )
             model.to(__lowerCamelCase  )
             model.eval()
             with torch.no_grad():
                 __A    :						Optional[Any]						 =     model(**self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase  )  )
             __A    :						str						 =     outputs.attentions
             self.assertEqual(len(__lowerCamelCase  )					,						self.model_tester.num_hidden_layers  )

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

             self.assertListEqual(
                 list(attentions[0].shape[-3:]  )					,						[self.model_tester.num_attention_heads, seq_len, seq_len]					,						)
             __A    :						List[str]						 =     len(__lowerCamelCase  )

             # Check attention is always last and order is fine
             __A    :						str						 =     True
             __A    :						Tuple						 =     True
             __A    :						List[str]						 =     model_class(__lowerCamelCase  )
             model.to(__lowerCamelCase  )
             model.eval()
             with torch.no_grad():
                 __A    :						str						 =     model(**self._prepare_for_class(__lowerCamelCase					,						__lowerCamelCase  )  )

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

             __A    :						Any						 =     outputs.attentions

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



     '''simple docstring'''




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

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

         __A    :						List[str]						 =     outputs.hidden_states
         __A    :						str						 =     self.model_tester.num_hidden_layers + 1
         self.assertEqual(len(__lowerCamelCase  )					,						__lowerCamelCase  )

         __A    :						List[Any]						 =     self.model_tester.seq_length - self.model_tester.num_masks
         __A    :						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    :						Dict						 =     self.model_tester.prepare_config_and_inputs_for_common()

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

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

         check_hidden_states_output(__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  )







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



     '''simple docstring'''




     pass







def 		__lowercase     (   )   ->Optional[Any]:



    '''simple docstring'''







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






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





 """simple docstring"""


 @cached_property
 def 					UpperCamelCase__(    self  ):



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



     '''simple docstring'''




     __A    :						Optional[Any]						 =     VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''  ).to(
         __lowerCamelCase  )

     __A    :						Tuple						 =     self.default_image_processor
     __A    :						int						 =     prepare_video()
     __A    :						Tuple						 =     image_processor(__lowerCamelCase					,						return_tensors='''pt'''  ).to(__lowerCamelCase  )

     # forward pass
     with torch.no_grad():
         __A    :						Dict						 =     model(**__lowerCamelCase  )

     # verify the logits
     __A    :						List[Any]						 =     torch.Size((1, 400)  )
     self.assertEqual(outputs.logits.shape					,						__lowerCamelCase  )

     __A    :						Tuple						 =     torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1]  ).to(__lowerCamelCase  )

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







 @slow
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						List[str]						 =     VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''  ).to(__lowerCamelCase  )

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

     # 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    :						int						 =     torch.load(__lowerCamelCase  )

     # forward pass
     with torch.no_grad():
         __A    :						Tuple						 =     model(**__lowerCamelCase  )

     # verify the logits
     __A    :						Any						 =     torch.Size([1, 1408, 1536]  )
     __A    :						Union[str, 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=__lowerCamelCase  )
     self.assertEqual(outputs.logits.shape					,						__lowerCamelCase  )
     self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]					,						__lowerCamelCase					,						atol=1e-4  )  )

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

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

     with torch.no_grad():
         __A    :						Dict						 =     model(**__lowerCamelCase  )

     __A    :						str						 =     torch.tensor(torch.tensor([0.6_4_6_9]  )					,						device=__lowerCamelCase  )
     self.assertTrue(torch.allclose(outputs.loss					,						__lowerCamelCase					,						atol=1e-4  )  )











"""simple docstring"""



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a_ =			logging.get_logger(__name__)

a_ =			{
    """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""",
    """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""",
    """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""",
    """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""",
    """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""",
    """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""",
}






class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 _lowerCamelCase        =     """roberta"""

 def __init__(    self					,						__lowerCamelCase=5_0265					,						__lowerCamelCase=768					,						__lowerCamelCase=12					,						__lowerCamelCase=12					,						__lowerCamelCase=3072					,						__lowerCamelCase="gelu"					,						__lowerCamelCase=0.1					,						__lowerCamelCase=0.1					,						__lowerCamelCase=512					,						__lowerCamelCase=2					,						__lowerCamelCase=0.0_2					,						__lowerCamelCase=1e-1_2					,						__lowerCamelCase=1					,						__lowerCamelCase=0					,						__lowerCamelCase=2					,						__lowerCamelCase="absolute"					,						__lowerCamelCase=True					,						__lowerCamelCase=None					,						**__lowerCamelCase					,						):



     '''simple docstring'''




     super().__init__(pad_token_id=__lowerCamelCase					,						bos_token_id=__lowerCamelCase					,						eos_token_id=__lowerCamelCase					,						**__lowerCamelCase  )

     __A    :						str						 =     vocab_size
     __A    :						Any						 =     hidden_size
     __A    :						Any						 =     num_hidden_layers
     __A    :						int						 =     num_attention_heads
     __A    :						List[Any]						 =     hidden_act
     __A    :						Any						 =     intermediate_size
     __A    :						List[str]						 =     hidden_dropout_prob
     __A    :						List[Any]						 =     attention_probs_dropout_prob
     __A    :						Optional[Any]						 =     max_position_embeddings
     __A    :						str						 =     type_vocab_size
     __A    :						int						 =     initializer_range
     __A    :						Tuple						 =     layer_norm_eps
     __A    :						str						 =     position_embedding_type
     __A    :						int						 =     use_cache
     __A    :						Optional[int]						 =     classifier_dropout






class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 @property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     if self.task == "multiple-choice":
         __A    :						List[Any]						 =     {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
     else:
         __A    :						Optional[Any]						 =     {0: '''batch''', 1: '''sequence'''}
     return OrderedDict(
         [
             ('''input_ids''', dynamic_axis),
             ('''attention_mask''', dynamic_axis),
         ]  )




