"""simple docstring"""
from typing import Callable, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__ 	=     logging.get_logger(__name__)

lowercase__ 	=     {
    'microsoft/xprophetnet-large-wiki100-cased': (
        'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json'
    ),
}




class        __snake_case	(					__lowerCAmelCase    ):
      a__				=		"""xlm-prophetnet"""
      a__				=		["""past_key_values"""]
      a__				=		{
          """num_attention_heads""": """num_encoder_attention_heads""",
      }



      def __init__(						self   ,      lowercase = 0.1   ,      lowercase = "gelu"   ,      lowercase = 3_05_22   ,      lowercase = 10_24   ,      lowercase = 40_96   ,      lowercase = 12   ,      lowercase = 16   ,      lowercase = 40_96   ,      lowercase = 12   ,      lowercase = 16   ,      lowercase = 0.1   ,      lowercase = 0.1   ,      lowercase = 5_12   ,      lowercase = 0.02   ,      lowercase = True   ,      lowercase = True   ,      lowercase = 0   ,      lowercase = 2   ,      lowercase = 32   ,      lowercase = 1_28   ,      lowercase = False   ,      lowercase = 0.0   ,      lowercase = True   ,      lowercase = 0   ,      lowercase = 1   ,      lowercase = 2   ,      **lowercase   ,      )     ->      Any:





        '''simple docstring'''





        a__:					Optional[Any]      							=     vocab_size
        a__:					int      							=     hidden_size
        a__:					List[str]      							=     encoder_ffn_dim
        a__:					Optional[int]      							=     num_encoder_layers
        a__:					List[Any]      							=     num_encoder_attention_heads
        a__:					Optional[int]      							=     decoder_ffn_dim
        a__:					Union[str, Any]      							=     num_decoder_layers
        a__:					Tuple      							=     num_decoder_attention_heads
        a__:					Tuple      							=     max_position_embeddings
        a__:					Optional[int]      							=     init_std  # Normal(0, this parameter)
        a__:					Union[str, Any]      							=     activation_function

        # parameters for xlmprophetnet
        a__:					str      							=     ngram
        a__:					List[str]      							=     num_buckets
        a__:					Dict      							=     relative_max_distance
        a__:					Dict      							=     disable_ngram_loss
        a__:					Any      							=     eps

        # 3 Types of Dropout
        a__:					int      							=     attention_dropout
        a__:					List[str]      							=     activation_dropout
        a__:					Optional[Any]      							=     dropout

        a__:					List[str]      							=     use_cache

        super().__init__(
            pad_token_id=lowercase   ,      bos_token_id=lowercase   ,      eos_token_id=lowercase   ,      is_encoder_decoder=lowercase   ,      add_cross_attention=lowercase   ,      decoder_start_token_id=lowercase   ,      **lowercase   ,      )



      @property
      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        return self.num_encoder_layers + self.num_decoder_layers



      @num_hidden_layers.setter
      def   lowerCamelCase_       (						self   ,      lowercase)     ->      Union[str, Any]:





        '''simple docstring'''





        raise NotImplementedError(
            'This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and'
            ' `num_decoder_layers`.')



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



      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   ,      )     ->      Optional[Any]:





        '''simple docstring'''





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

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

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



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





        '''simple docstring'''





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

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

        a__:					Optional[int]      							=     self.get_config()

        return config, pixel_values, labels



      def   lowerCamelCase_       (						self)     ->      Union[str, 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   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase)     ->      str:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase)     ->      int:





        '''simple docstring'''





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

        a__:					Union[str, Any]      							=     model(lowercase   ,      lowercase)
        # model only returns predictions for masked patches
        a__:					List[str]      							=     mask.sum().item()
        a__:					str      							=     3 * self.tubelet_size * self.patch_size**2
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, num_masked_patches, decoder_num_labels))



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





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







@require_torch
class        __snake_case	(					__lowerCAmelCase	,				__lowerCAmelCase	,				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   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase=False)     ->      Any:





        '''simple docstring'''





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

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

        return inputs_dict



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





        '''simple docstring'''





        self.config_tester.run_common_tests()



      @unittest.skip(reason='VideoMAE does not use inputs_embeds')
      def   lowerCamelCase_       (						self)     ->      Optional[Any]:





        '''simple docstring'''





        pass



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





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

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



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





        '''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__:					Any      							=     model_class(lowercase)
          a__:					int      							=     inspect.signature(model.forward)
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          a__:					Optional[Any]      							=     [*signature.parameters.keys()]

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



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





        '''simple docstring'''





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



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





        '''simple docstring'''





        a__:					Optional[Any]      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*lowercase)



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





        '''simple docstring'''





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



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





        '''simple docstring'''





        if not self.has_attentions:
          pass

        else:
          a__							,   a__:					Any      							=     self.model_tester.prepare_config_and_inputs_for_common()
          a__:					str      							=     True

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

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

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

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

            a__:					int      							=     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   lowerCamelCase_       (						self)     ->      List[Any]:





        '''simple docstring'''





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

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

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

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

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

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





        '''simple docstring'''





        pass







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




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



      @cached_property
      def   lowerCamelCase_       (						self)     ->      str:





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





        '''simple docstring'''





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

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

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

        # verify the logits
        a__:					str      							=     torch.Size((1, 4_00))
        self.assertEqual(outputs.logits.shape   ,      lowercase)

        a__:					Optional[Any]      							=     torch.tensor([0.3669, -0.0688, -0.2421]).to(lowercase)

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



      @slow
      def   lowerCamelCase_       (						self)     ->      Dict:





        '''simple docstring'''





        a__:					Tuple      							=     VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short').to(lowercase)

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

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

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

        # verify the logits
        a__:					Union[str, Any]      							=     torch.Size([1, 14_08, 15_36])
        a__:					Union[str, Any]      							=     torch.tensor(
            [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]   ,      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__:					Optional[int]      							=     torch.tensor([0.5142]   ,      device=lowercase)
        self.assertTrue(torch.allclose(outputs.loss   ,      lowercase   ,      atol=1e-4))

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

        with torch.no_grad():
          a__:					Union[str, Any]      							=     model(**lowercase)

        a__:					Optional[int]      							=     torch.tensor(torch.tensor([0.6469])   ,      device=lowercase)
        self.assertTrue(torch.allclose(outputs.loss   ,      lowercase   ,      atol=1e-4))


