





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

  """simple docstring"""


  def __init__(							self					:						Dict   ,    lowerCAmelCase_					:						str   ,    lowerCAmelCase_					:						Tuple=1_3   ,    lowerCAmelCase_					:						Union[str, Any]=1_0   ,    lowerCAmelCase_					:						Dict=3   ,    lowerCAmelCase_					:						int=2   ,    lowerCAmelCase_					:						List[str]=2   ,    lowerCAmelCase_					:						int=2   ,    lowerCAmelCase_					:						Tuple=True   ,    lowerCAmelCase_					:						Union[str, Any]=True   ,    lowerCAmelCase_					:						List[Any]=3_2   ,    lowerCAmelCase_					:						str=5   ,    lowerCAmelCase_					:						Union[str, Any]=4   ,    lowerCAmelCase_					:						Dict=3_7   ,    lowerCAmelCase_					:						int="gelu"   ,    lowerCAmelCase_					:						int=0.1   ,    lowerCAmelCase_					:						Tuple=0.1   ,    lowerCAmelCase_					:						Union[str, Any]=1_0   ,    lowerCAmelCase_					:						Dict=0.02   ,    lowerCAmelCase_					:						Any=0.9   ,    lowerCAmelCase_					:						Union[str, Any]=None   ,    )				->		Optional[int]:
     __lowerCAmelCase				      =      parent
     __lowerCAmelCase				      =      batch_size
     __lowerCAmelCase				      =      image_size
     __lowerCAmelCase				      =      num_channels
     __lowerCAmelCase				      =      patch_size
     __lowerCAmelCase				      =      tubelet_size
     __lowerCAmelCase				      =      num_frames
     __lowerCAmelCase				      =      is_training
     __lowerCAmelCase				      =      use_labels
     __lowerCAmelCase				      =      hidden_size
     __lowerCAmelCase				      =      num_hidden_layers
     __lowerCAmelCase				      =      num_attention_heads
     __lowerCAmelCase				      =      intermediate_size
     __lowerCAmelCase				      =      hidden_act
     __lowerCAmelCase				      =      hidden_dropout_prob
     __lowerCAmelCase				      =      attention_probs_dropout_prob
     __lowerCAmelCase				      =      type_sequence_label_size
     __lowerCAmelCase				      =      initializer_range
     __lowerCAmelCase				      =      mask_ratio
     __lowerCAmelCase				      =      scope

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

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






  def 							lowercase						(							self					:						Tuple	)				->		List[str]:
     __lowerCAmelCase				      =      floats_tensor(
         [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]	)

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

     __lowerCAmelCase				      =      self.get_config()

     return config, pixel_values, labels






  def 							lowercase						(							self					:						List[Any]	)				->		List[str]:
     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 							lowercase						(							self					:						List[str]   ,    lowerCAmelCase_					:						int   ,    lowerCAmelCase_					:						Optional[Any]   ,    lowerCAmelCase_					:						int	)				->		Any:
     __lowerCAmelCase				      =      VideoMAEModel(config=lowerCAmelCase_	)
     model.to(lowerCAmelCase_	)
     model.eval()
     __lowerCAmelCase				      =      model(lowerCAmelCase_	)
     self.parent.assertEqual(result.last_hidden_state.shape   ,    (self.batch_size, self.seq_length, self.hidden_size)	)






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

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





@require_torch
class 							_UpperCAmelCase       (       _UpperCamelCase						,       _UpperCamelCase						,       unittest.TestCase       ):

  """simple docstring"""


  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 							lowercase						(							self					:						List[Any]	)				->		Any:
     __lowerCAmelCase				      =      VideoMAEModelTester(self	)
     __lowerCAmelCase				      =      ConfigTester(self   ,    config_class=lowerCAmelCase_   ,    has_text_modality=lowerCAmelCase_   ,    hidden_size=3_7	)






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

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

     return inputs_dict






  def 							lowercase						(							self					:						Any	)				->		Union[str, Any]:
     self.config_tester.run_common_tests()






  @unittest.skip(reason='VideoMAE does not use inputs_embeds'	)
  def 							lowercase						(							self					:						Union[str, Any]	)				->		str:
     pass






  def 							lowercase						(							self					:						Optional[int]	)				->		List[str]:
     __lowerCAmelCase      ,      __lowerCAmelCase				      =      self.model_tester.prepare_config_and_inputs_for_common()

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






  def 							lowercase						(							self					:						Optional[int]	)				->		List[Any]:
     __lowerCAmelCase      ,      __lowerCAmelCase				      =      self.model_tester.prepare_config_and_inputs_for_common()

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

        __lowerCAmelCase				      =      ['pixel_values']
        self.assertListEqual(arg_names[:1]   ,    lowerCAmelCase_	)






  def 							lowercase						(							self					:						Optional[Any]	)				->		Any:
     __lowerCAmelCase				      =      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase_	)






  def 							lowercase						(							self					:						Any	)				->		Optional[Any]:
     __lowerCAmelCase				      =      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase_	)






  @slow
  def 							lowercase						(							self					:						Any	)				->		Tuple:
     for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCAmelCase				      =      VideoMAEModel.from_pretrained(lowerCAmelCase_	)
        self.assertIsNotNone(lowerCAmelCase_	)






  def 							lowercase						(							self					:						Union[str, Any]	)				->		Dict:
     if not self.has_attentions:
        pass

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

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

           __lowerCAmelCase				      =      True
           __lowerCAmelCase				      =      False
           __lowerCAmelCase				      =      True
           __lowerCAmelCase				      =      model_class(lowerCAmelCase_	)
           model.to(lowerCAmelCase_	)
           model.eval()
           with torch.no_grad():
              __lowerCAmelCase				      =      model(**self._prepare_for_class(lowerCAmelCase_   ,    lowerCAmelCase_	)	)
           __lowerCAmelCase				      =      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"]
           __lowerCAmelCase				      =      True
           __lowerCAmelCase				      =      model_class(lowerCAmelCase_	)
           model.to(lowerCAmelCase_	)
           model.eval()
           with torch.no_grad():
              __lowerCAmelCase				      =      model(**self._prepare_for_class(lowerCAmelCase_   ,    lowerCAmelCase_	)	)
           __lowerCAmelCase				      =      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]   ,    )
           __lowerCAmelCase				      =      len(lowerCAmelCase_	)

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

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

           __lowerCAmelCase				      =      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 							lowercase						(							self					:						List[Any]	)				->		str:
     def check_hidden_states_output(lowerCAmelCase_					:						Tuple   ,    lowerCAmelCase_					:						Tuple   ,    lowerCAmelCase_					:						int	):
        __lowerCAmelCase				      =      model_class(lowerCAmelCase_	)
        model.to(lowerCAmelCase_	)
        model.eval()

        with torch.no_grad():
           __lowerCAmelCase				      =      model(**self._prepare_for_class(lowerCAmelCase_   ,    lowerCAmelCase_	)	)

        __lowerCAmelCase				      =      outputs.hidden_states
        __lowerCAmelCase				      =      self.model_tester.num_hidden_layers + 1
        self.assertEqual(len(lowerCAmelCase_	)   ,    lowerCAmelCase_	)

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

     __lowerCAmelCase      ,      __lowerCAmelCase				      =      self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        __lowerCAmelCase				      =      True
        check_hidden_states_output(lowerCAmelCase_   ,    lowerCAmelCase_   ,    lowerCAmelCase_	)

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        __lowerCAmelCase				      =      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 							lowercase						(							self					:						Tuple	)				->		Optional[Any]:
     pass





def     a_       (					):
   __lowerCAmelCase				      =      hf_hub_download(
       repo_id='hf-internal-testing/spaghetti-video',      filename='eating_spaghetti.npy',      repo_type='dataset'      )
   __lowerCAmelCase				      =      np.load(lowerCAmelCase_      )
   return list(lowerCAmelCase_      )




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

  """simple docstring"""


  @cached_property
  def 							lowercase						(							self					:						str	)				->		int:
     # 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 							lowercase						(							self					:						str	)				->		int:
     __lowerCAmelCase				      =      VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics'	).to(
         lowerCAmelCase_	)

     __lowerCAmelCase				      =      self.default_image_processor
     __lowerCAmelCase				      =      prepare_video()
     __lowerCAmelCase				      =      image_processor(lowerCAmelCase_   ,    return_tensors='pt'	).to(lowerCAmelCase_	)

     # forward pass
     with torch.no_grad():
        __lowerCAmelCase				      =      model(**lowerCAmelCase_	)

     # verify the logits
     __lowerCAmelCase				      =      torch.Size((1, 4_0_0)	)
     self.assertEqual(outputs.logits.shape   ,    lowerCAmelCase_	)

     __lowerCAmelCase				      =      torch.tensor([0.36_69, -0.06_88, -0.24_21]	).to(lowerCAmelCase_	)

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






  @slow
  def 							lowercase						(							self					:						List[Any]	)				->		Optional[int]:
     __lowerCAmelCase				      =      VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'	).to(lowerCAmelCase_	)

     __lowerCAmelCase				      =      self.default_image_processor
     __lowerCAmelCase				      =      prepare_video()
     __lowerCAmelCase				      =      image_processor(lowerCAmelCase_   ,    return_tensors='pt'	).to(lowerCAmelCase_	)

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

     # forward pass
     with torch.no_grad():
        __lowerCAmelCase				      =      model(**lowerCAmelCase_	)

     # verify the logits
     __lowerCAmelCase				      =      torch.Size([1, 1_4_0_8, 1_5_3_6]	)
     __lowerCAmelCase				      =      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=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`)
     __lowerCAmelCase				      =      torch.tensor([0.51_42]   ,    device=lowerCAmelCase_	)
     self.assertTrue(torch.allclose(outputs.loss   ,    lowerCAmelCase_   ,    atol=1e-4	)	)

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

     with torch.no_grad():
        __lowerCAmelCase				      =      model(**lowerCAmelCase_	)

     __lowerCAmelCase				      =      torch.tensor(torch.tensor([0.64_69]	)   ,    device=lowerCAmelCase_	)
     self.assertTrue(torch.allclose(outputs.loss   ,    lowerCAmelCase_   ,    atol=1e-4	)	)





'''simple docstring'''
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
		import torch

		from transformers import XLMRobertaModel
@require_sentencepiece
@require_tokenizers
@require_torch
class   lowercase      (				unittest.TestCase   ):

		"""simple docstring"""

		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Optional[Any]				   =					XLMRobertaModel.from_pretrained('''xlm-roberta-base'''				)
							UpperCamelCase__  :List[str]				   =					torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]				)
							# The dog is cute and lives in the garden house

							UpperCamelCase__  :Union[str, Any]				   =					torch.Size((1, 12, 768)				)  # batch_size, sequence_length, embedding_vector_dim
							UpperCamelCase__  :Union[str, Any]				   =					torch.tensor(
							    [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]]				)
							#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
							#  xlmr.eval()
							#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
							with torch.no_grad():
												UpperCamelCase__  :Any				   =					model(UpperCamelCase_				)['''last_hidden_state'''].detach()
							self.assertEqual(output.shape       ,  UpperCamelCase_				)
							# compare the actual values for a slice of last dim
							self.assertTrue(torch.allclose(output[:, :, -1]       ,  UpperCamelCase_       ,  atol=1e-3				)				)




		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :int				   =					XLMRobertaModel.from_pretrained('''xlm-roberta-large'''				)
							UpperCamelCase__  :List[str]				   =					torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]				)
							# The dog is cute and lives in the garden house

							UpperCamelCase__  :Any				   =					torch.Size((1, 12, 1024)				)  # batch_size, sequence_length, embedding_vector_dim
							UpperCamelCase__  :List[Any]				   =					torch.tensor(
							    [[-0.0699, -0.0318, 0.0705, -0.1241, 0.0999, -0.0520, 0.1004, -0.1838, -0.4704, 0.1437, 0.0821, 0.0126]]				)
							#  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
							#  xlmr.eval()
							#  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
							with torch.no_grad():
												UpperCamelCase__  :Optional[int]				   =					model(UpperCamelCase_				)['''last_hidden_state'''].detach()
							self.assertEqual(output.shape       ,  UpperCamelCase_				)
							# compare the actual values for a slice of last dim
							self.assertTrue(torch.allclose(output[:, :, -1]       ,  UpperCamelCase_       ,  atol=1e-3				)				)