

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







	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.02 ,				_lowerCAmelCase=0.9 ,				_lowerCAmelCase=None ,				)    ->    List[Any]:
							'''simple docstring'''






							lowercase        =							parent
							lowercase        =							batch_size
							lowercase        =							image_size
							lowercase        =							num_channels
							lowercase        =							patch_size
							lowercase        =							tubelet_size
							lowercase        =							num_frames
							lowercase        =							is_training
							lowercase        =							use_labels
							lowercase        =							hidden_size
							lowercase        =							num_hidden_layers
							lowercase        =							num_attention_heads
							lowercase        =							intermediate_size
							lowercase        =							hidden_act
							lowercase        =							hidden_dropout_prob
							lowercase        =							attention_probs_dropout_prob
							lowercase        =							type_sequence_label_size
							lowercase        =							initializer_range
							lowercase        =							mask_ratio
							lowercase        =							scope

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

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







	def   _a   (    self						)    ->    Union[str, Any]:
							'''simple docstring'''






							lowercase        =							floats_tensor(
							    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]						)

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

							lowercase        =							self.get_config()

							return config, pixel_values, labels







	def   _a   (    self						)    ->    List[str]:
							'''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   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    str:
							'''simple docstring'''






							lowercase        =							VideoMAEModel(config=_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()
							lowercase        =							model(_lowerCAmelCase						)
							self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)						)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    List[Any]:
							'''simple docstring'''






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

							lowercase        =							model(_lowerCAmelCase ,				_lowerCAmelCase						)
							# model only returns predictions for masked patches
							lowercase        =							mask.sum().item()
							lowercase        =							3 * self.tubelet_size * self.patch_size**2
							self.parent.assertEqual(result.logits.shape ,				(self.batch_size, num_masked_patches, decoder_num_labels)						)







	def   _a   (    self						)    ->    Optional[Any]:
							'''simple docstring'''






							lowercase        =							self.prepare_config_and_inputs()
							lowercase     ,   lowercase     ,   lowercase        =							config_and_inputs
							lowercase        =							{"""pixel_values""": pixel_values}
							return config, inputs_dict




@require_torch
class      __UpperCamelCase       (_UpperCAmelCase			,   _UpperCAmelCase			,   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   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase        =							VideoMAEModelTester(self						)
							lowercase        =							ConfigTester(self ,				config_class=_lowerCAmelCase ,				has_text_modality=_lowerCAmelCase ,				hidden_size=37						)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase=False						)    ->    Tuple:
							'''simple docstring'''






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

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

							return inputs_dict







	def   _a   (    self						)    ->    List[Any]:
							'''simple docstring'''






							self.config_tester.run_common_tests()







	@unittest.skip(reason="""VideoMAE does not use inputs_embeds"""						)
	def   _a   (    self						)    ->    str:
							'''simple docstring'''






							pass







	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()

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







	def   _a   (    self						)    ->    List[str]:
							'''simple docstring'''






							lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()

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

													lowercase        =							["""pixel_values"""]
													self.assertListEqual(arg_names[:1] ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    List[Any]:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowerCAmelCase						)







	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_lowerCAmelCase						)







	@slow
	def   _a   (    self						)    ->    int:
							'''simple docstring'''






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







	def   _a   (    self						)    ->    Optional[int]:
							'''simple docstring'''






							if not self.has_attentions:
													pass

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

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

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

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

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

																			lowercase        =							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   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							def check_hidden_states_output(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						):
													lowercase        =							model_class(_lowerCAmelCase						)
													model.to(_lowerCAmelCase						)
													model.eval()

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

													lowercase        =							outputs.hidden_states
													lowercase        =							self.model_tester.num_hidden_layers + 1
													self.assertEqual(len(_lowerCAmelCase						) ,				_lowerCAmelCase						)

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

							lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()

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

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													lowercase        =							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   _a   (    self						)    ->    List[Any]:
							'''simple docstring'''






							pass







def      SCREAMING_SNAKE_CASE				(				):
						lowercase        =							hf_hub_download(
						    repo_id="""hf-internal-testing/spaghetti-video""" ,  filename="""eating_spaghetti.npy""" ,  repo_type="""dataset"""						)
						lowercase        =							np.load(lowercase_						)
						return list(lowercase_						)




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







	@cached_property
	def   _a   (    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   _a   (    self						)    ->    Optional[int]:
							'''simple docstring'''






							lowercase        =							VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics"""						).to(
							    _lowerCAmelCase						)

							lowercase        =							self.default_image_processor
							lowercase        =							prepare_video()
							lowercase        =							image_processor(_lowerCAmelCase ,				return_tensors="""pt"""						).to(_lowerCAmelCase						)

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

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

							lowercase        =							torch.tensor([0.3669, -0.0688, -0.2421]						).to(_lowerCAmelCase						)

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







	@slow
	def   _a   (    self						)    ->    List[Any]:
							'''simple docstring'''






							lowercase        =							VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short"""						).to(_lowerCAmelCase						)

							lowercase        =							self.default_image_processor
							lowercase        =							prepare_video()
							lowercase        =							image_processor(_lowerCAmelCase ,				return_tensors="""pt"""						).to(_lowerCAmelCase						)

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

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

							# verify the logits
							lowercase        =							torch.Size([1, 1408, 1536]						)
							lowercase        =							torch.tensor(
							    [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]] ,				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`)
							lowercase        =							torch.tensor([0.5142] ,				device=_lowerCAmelCase						)
							self.assertTrue(torch.allclose(outputs.loss ,				_lowerCAmelCase ,				atol=1E-4						)						)

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

							with torch.no_grad():
													lowercase        =							model(**_lowerCAmelCase						)

							lowercase        =							torch.tensor(torch.tensor([0.6469]						) ,				device=_lowerCAmelCase						)
							self.assertTrue(torch.allclose(outputs.loss ,				_lowerCAmelCase ,				atol=1E-4						)						)







'''simple docstring'''


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


lowercase_      :				Any   					=    logging.get_logger(__name__)

lowercase_      :				str   					=    {
    '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''',
    # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn
}




class      __UpperCamelCase       (_UpperCAmelCase			):
	__A			  =					'''vit_msn'''







	def __init__(    self ,				_lowerCAmelCase=768 ,				_lowerCAmelCase=12 ,				_lowerCAmelCase=12 ,				_lowerCAmelCase=3072 ,				_lowerCAmelCase="gelu" ,				_lowerCAmelCase=0.0 ,				_lowerCAmelCase=0.0 ,				_lowerCAmelCase=0.02 ,				_lowerCAmelCase=1E-06 ,				_lowerCAmelCase=224 ,				_lowerCAmelCase=16 ,				_lowerCAmelCase=3 ,				_lowerCAmelCase=True ,				**_lowerCAmelCase ,				)    ->    List[Any]:
							'''simple docstring'''






							super().__init__(**_lowerCAmelCase						)

							lowercase        =							hidden_size
							lowercase        =							num_hidden_layers
							lowercase        =							num_attention_heads
							lowercase        =							intermediate_size
							lowercase        =							hidden_act
							lowercase        =							hidden_dropout_prob
							lowercase        =							attention_probs_dropout_prob
							lowercase        =							initializer_range
							lowercase        =							layer_norm_eps
							lowercase        =							image_size
							lowercase        =							patch_size
							lowercase        =							num_channels
							lowercase        =							qkv_bias


