

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







     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.0_2 ,						UpperCamelCase_=0.9 ,						UpperCamelCase_=None ,						):
      __magic_name__     =      parent
      __magic_name__     =      batch_size
      __magic_name__     =      image_size
      __magic_name__     =      num_channels
      __magic_name__     =      patch_size
      __magic_name__     =      tubelet_size
      __magic_name__     =      num_frames
      __magic_name__     =      is_training
      __magic_name__     =      use_labels
      __magic_name__     =      hidden_size
      __magic_name__     =      num_hidden_layers
      __magic_name__     =      num_attention_heads
      __magic_name__     =      intermediate_size
      __magic_name__     =      hidden_act
      __magic_name__     =      hidden_dropout_prob
      __magic_name__     =      attention_probs_dropout_prob
      __magic_name__     =      type_sequence_label_size
      __magic_name__     =      initializer_range
      __magic_name__     =      mask_ratio
      __magic_name__     =      scope

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

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







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      floats_tensor(
          [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]       )

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

      __magic_name__     =      self.get_config()

      return config, pixel_values, labels







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







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      VideoMAEModel(config=UpperCamelCase_       )
      model.to(UpperCamelCase_       )
      model.eval()
      __magic_name__     =      model(UpperCamelCase_       )
      self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)       )







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

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


@require_torch
class        _lowercase					(     __UpperCAmelCase   , __UpperCAmelCase   , unittest.TestCase     ):
     _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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      VideoMAEModelTester(self       )
      __magic_name__     =      ConfigTester(self ,						config_class=UpperCamelCase_ ,						has_text_modality=UpperCamelCase_ ,						hidden_size=37       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_=False       ):
      __magic_name__     =      copy.deepcopy(UpperCamelCase_       )

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

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

      return inputs_dict







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







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







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__   ,						__magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()

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







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__   ,						__magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()

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

       __magic_name__     =      ['''pixel_values''']
       self.assertListEqual(arg_names[:1] ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*UpperCamelCase_       )







     @slow
     def 	lowerCAmelCase__    (      self       ):
      for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __magic_name__     =      VideoMAEModel.from_pretrained(UpperCamelCase_       )
       self.assertIsNotNone(UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      if not self.has_attentions:
       pass

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

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

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

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

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

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

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

        __magic_name__     =      outputs.attentions

        self.assertEqual(len(UpperCamelCase_       ) ,						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       ):
      def check_hidden_states_output(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
       __magic_name__     =      model_class(UpperCamelCase_       )
       model.to(UpperCamelCase_       )
       model.eval()

       with torch.no_grad():
        __magic_name__     =      model(**self._prepare_for_class(UpperCamelCase_ ,						UpperCamelCase_       )       )

       __magic_name__     =      outputs.hidden_states
       __magic_name__     =      self.model_tester.num_hidden_layers + 1
       self.assertEqual(len(UpperCamelCase_       ) ,						UpperCamelCase_       )

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

      __magic_name__   ,						__magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       __magic_name__     =      True
       check_hidden_states_output(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       )

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

       check_hidden_states_output(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       )







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





def 	lowercase      (   ) ->      Optional[Any]:
 __magic_name__     =      hf_hub_download(
     repo_id='''hf-internal-testing/spaghetti-video'''      ,   filename='''eating_spaghetti.npy'''      ,   repo_type='''dataset'''						)
 __magic_name__     =      np.load(__UpperCamelCase						)
 return list(__UpperCamelCase						)
@require_torch
@require_vision
class        _lowercase					(     unittest.TestCase     ):







     @cached_property
     def 	lowerCAmelCase__    (      self       ):
      # logits were tested with a different mean and std, so we use the same here
      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       ):
      __magic_name__     =      VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''       ).to(
          UpperCamelCase_       )

      __magic_name__     =      self.default_image_processor
      __magic_name__     =      prepare_video()
      __magic_name__     =      image_processor(UpperCamelCase_ ,						return_tensors='''pt'''       ).to(UpperCamelCase_       )

      # forward pass
      with torch.no_grad():
       __magic_name__     =      model(**UpperCamelCase_       )

      # verify the logits
      __magic_name__     =      torch.Size((1, 400)       )
      self.assertEqual(outputs.logits.shape ,						UpperCamelCase_       )

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

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







     @slow
     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''       ).to(UpperCamelCase_       )

      __magic_name__     =      self.default_image_processor
      __magic_name__     =      prepare_video()
      __magic_name__     =      image_processor(UpperCamelCase_ ,						return_tensors='''pt'''       ).to(UpperCamelCase_       )

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

      # forward pass
      with torch.no_grad():
       __magic_name__     =      model(**UpperCamelCase_       )

      # verify the logits
      __magic_name__     =      torch.Size([1, 1408, 1536]       )
      __magic_name__     =      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=UpperCamelCase_       )
      self.assertEqual(outputs.logits.shape ,						UpperCamelCase_       )
      self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] ,						UpperCamelCase_ ,						atol=1E-4       )       )

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

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

      with torch.no_grad():
       __magic_name__     =      model(**UpperCamelCase_       )

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




"""simple docstring"""






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


__lowerCamelCase										=		logging.get_logger(__name__)

__lowerCamelCase										=		{
    "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json",
    "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json"
    # See all FNet models at https://huggingface.co/models?filter=fnet
}
class        _lowercase					(     __UpperCAmelCase     ):
     _lowerCamelCase     =       '''fnet'''







     def __init__(      self ,						UpperCamelCase_=3_2000 ,						UpperCamelCase_=768 ,						UpperCamelCase_=12 ,						UpperCamelCase_=3072 ,						UpperCamelCase_="gelu_new" ,						UpperCamelCase_=0.1 ,						UpperCamelCase_=512 ,						UpperCamelCase_=4 ,						UpperCamelCase_=0.0_2 ,						UpperCamelCase_=1E-1_2 ,						UpperCamelCase_=False ,						UpperCamelCase_=512 ,						UpperCamelCase_=3 ,						UpperCamelCase_=1 ,						UpperCamelCase_=2 ,						**UpperCamelCase_ ,						):
      super().__init__(pad_token_id=UpperCamelCase_ ,						bos_token_id=UpperCamelCase_ ,						eos_token_id=UpperCamelCase_ ,						**UpperCamelCase_       )

      __magic_name__     =      vocab_size
      __magic_name__     =      max_position_embeddings
      __magic_name__     =      hidden_size
      __magic_name__     =      num_hidden_layers
      __magic_name__     =      intermediate_size
      __magic_name__     =      hidden_act
      __magic_name__     =      hidden_dropout_prob
      __magic_name__     =      initializer_range
      __magic_name__     =      type_vocab_size
      __magic_name__     =      layer_norm_eps
      __magic_name__     =      use_tpu_fourier_optimizations
      __magic_name__     =      tpu_short_seq_length

