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



	"""simple docstring"""




	def __init__(       self  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]       ,							SCREAMING_SNAKE_CASE_  :			List[str]=1_3       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]=1_0       ,							SCREAMING_SNAKE_CASE_  :			str=3       ,							SCREAMING_SNAKE_CASE_  :			List[str]=2       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=2       ,							SCREAMING_SNAKE_CASE_  :			Tuple=2       ,							SCREAMING_SNAKE_CASE_  :			List[Any]=True       ,							SCREAMING_SNAKE_CASE_  :			Dict=True       ,							SCREAMING_SNAKE_CASE_  :			Any=3_2       ,							SCREAMING_SNAKE_CASE_  :			str=5       ,							SCREAMING_SNAKE_CASE_  :			Tuple=4       ,							SCREAMING_SNAKE_CASE_  :			List[str]=3_7       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]="gelu"       ,							SCREAMING_SNAKE_CASE_  :			Dict=0.1       ,							SCREAMING_SNAKE_CASE_  :			str=0.1       ,							SCREAMING_SNAKE_CASE_  :			List[str]=1_0       ,							SCREAMING_SNAKE_CASE_  :			Any=0.02       ,							SCREAMING_SNAKE_CASE_  :			str=0.9       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=None       ,							)       ->     Any:
					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 						_lowercase  (       self  :			str       )       ->     int:
					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 						_lowercase  (       self  :			Any       )       ->     Tuple:
					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=SCREAMING_SNAKE_CASE_       ,							initializer_range=self.initializer_range       ,							)
	def 						_lowercase  (       self  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]       ,							SCREAMING_SNAKE_CASE_  :			Tuple       ,							SCREAMING_SNAKE_CASE_  :			List[Any]       )       ->     str:
					lowercase_       =					VideoMAEModel(config=SCREAMING_SNAKE_CASE_       )
					model.to(SCREAMING_SNAKE_CASE_       )
					model.eval()
					lowercase_       =					model(SCREAMING_SNAKE_CASE_       )
					self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, self.seq_length, self.hidden_size)       )
	def 						_lowercase  (       self  :			int       ,							SCREAMING_SNAKE_CASE_  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			Tuple       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]       )       ->     Tuple:
					lowercase_       =					VideoMAEForPreTraining(SCREAMING_SNAKE_CASE_       )
					model.to(SCREAMING_SNAKE_CASE_       )
					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(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )
					# 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 						_lowercase  (       self  :			str       )       ->     Any:
					lowercase_       =					self.prepare_config_and_inputs()
					lowercase_  ,	lowercase_  ,	lowercase_       =					config_and_inputs
					lowercase_       =					{'''pixel_values''': pixel_values}
					return config, inputs_dict

@require_torch
class  lowercase__(				UpperCAmelCase ,						UpperCAmelCase ,						unittest.TestCase							):



	"""simple docstring"""




	a			:Optional[int]         =	(
	    (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
	)
	a			:Optional[int]         =	(
	    {'feature-extraction': VideoMAEModel, 'video-classification': VideoMAEForVideoClassification}
	    if is_torch_available()
	    else {}
	)

	a			:Optional[int]         =	False
	a			:int         =	False
	a			:str         =	False
	a			:int         =	False
	def 						_lowercase  (       self  :			int       )       ->     Optional[int]:
					lowercase_       =					VideoMAEModelTester(self       )
					lowercase_       =					ConfigTester(self       ,							config_class=SCREAMING_SNAKE_CASE_       ,							has_text_modality=SCREAMING_SNAKE_CASE_       ,							hidden_size=3_7       )
	def 						_lowercase  (       self  :			str       ,							SCREAMING_SNAKE_CASE_  :			str       ,							SCREAMING_SNAKE_CASE_  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			Any=False       )       ->     str:
					lowercase_       =					copy.deepcopy(SCREAMING_SNAKE_CASE_       )

					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(SCREAMING_SNAKE_CASE_       )

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

					return inputs_dict
	def 						_lowercase  (       self  :			Optional[int]       )       ->     Optional[int]:
					self.config_tester.run_common_tests()
	@unittest.skip(reason='''VideoMAE does not use inputs_embeds'''       )
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     Union[str, Any]:
					pass
	def 						_lowercase  (       self  :			List[str]       )       ->     List[str]:
					lowercase_  ,	lowercase_       =					self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									lowercase_       =					model_class(SCREAMING_SNAKE_CASE_       )
									self.assertIsInstance(model.get_input_embeddings()       ,							(nn.Module)       )
									lowercase_       =					model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_       ,							nn.Linear       )       )
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     Tuple:
					lowercase_  ,	lowercase_       =					self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									lowercase_       =					model_class(SCREAMING_SNAKE_CASE_       )
									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]       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			int       )       ->     List[Any]:
					lowercase_       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Any       )       ->     Dict:
					lowercase_       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*SCREAMING_SNAKE_CASE_       )
	@slow
	def 						_lowercase  (       self  :			Any       )       ->     Optional[Any]:
					for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_       =					VideoMAEModel.from_pretrained(SCREAMING_SNAKE_CASE_       )
									self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     List[Any]:
					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(SCREAMING_SNAKE_CASE_       )
													model.to(SCREAMING_SNAKE_CASE_       )
													model.eval()
													with torch.no_grad():
																	lowercase_       =					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )       )
													lowercase_       =					outputs.attentions
													self.assertEqual(len(SCREAMING_SNAKE_CASE_       )       ,							self.model_tester.num_hidden_layers       )

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													lowercase_       =					True
													lowercase_       =					model_class(SCREAMING_SNAKE_CASE_       )
													model.to(SCREAMING_SNAKE_CASE_       )
													model.eval()
													with torch.no_grad():
																	lowercase_       =					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )       )
													lowercase_       =					outputs.attentions
													self.assertEqual(len(SCREAMING_SNAKE_CASE_       )       ,							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(SCREAMING_SNAKE_CASE_       )

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

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

													lowercase_       =					outputs.attentions

													self.assertEqual(len(SCREAMING_SNAKE_CASE_       )       ,							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[str]       )       ->     List[str]:
					def check_hidden_states_output(SCREAMING_SNAKE_CASE_  :			Union[str, Any]       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			str       ):
									lowercase_       =					model_class(SCREAMING_SNAKE_CASE_       )
									model.to(SCREAMING_SNAKE_CASE_       )
									model.eval()

									with torch.no_grad():
													lowercase_       =					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )       )

									lowercase_       =					outputs.hidden_states
									lowercase_       =					self.model_tester.num_hidden_layers + 1
									self.assertEqual(len(SCREAMING_SNAKE_CASE_       )       ,							SCREAMING_SNAKE_CASE_       )

									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(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )

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

									check_hidden_states_output(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )




	@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''       )
	def 						_lowercase  (       self  :			Tuple       )       ->     List[Any]:
					pass




def 				a						(							):


				'''simple docstring'''
				lowercase_       =					hf_hub_download(
				    repo_id='''hf-internal-testing/spaghetti-video'''						,   filename='''eating_spaghetti.npy'''						,   repo_type='''dataset'''					)
				lowercase_       =					np.load(snake_case__					)
				return list(snake_case__					)


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



	"""simple docstring"""




	@cached_property
	def 						_lowercase  (       self  :			List[str]       )       ->     Optional[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:
					lowercase_       =					VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''       ).to(
					    SCREAMING_SNAKE_CASE_       )

					lowercase_       =					self.default_image_processor
					lowercase_       =					prepare_video()
					lowercase_       =					image_processor(SCREAMING_SNAKE_CASE_       ,							return_tensors='''pt'''       ).to(SCREAMING_SNAKE_CASE_       )

					# forward pass
					with torch.no_grad():
									lowercase_       =					model(**SCREAMING_SNAKE_CASE_       )

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

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

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




	@slow
	def 						_lowercase  (       self  :			str       )       ->     Dict:
					lowercase_       =					VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''       ).to(SCREAMING_SNAKE_CASE_       )

					lowercase_       =					self.default_image_processor
					lowercase_       =					prepare_video()
					lowercase_       =					image_processor(SCREAMING_SNAKE_CASE_       ,							return_tensors='''pt'''       ).to(SCREAMING_SNAKE_CASE_       )

					# 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(SCREAMING_SNAKE_CASE_       )

					# forward pass
					with torch.no_grad():
									lowercase_       =					model(**SCREAMING_SNAKE_CASE_       )

					# verify the logits
					lowercase_       =					torch.Size([1, 1_4_0_8, 1_5_3_6]       )
					lowercase_       =					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=SCREAMING_SNAKE_CASE_       )
					self.assertEqual(outputs.logits.shape       ,							SCREAMING_SNAKE_CASE_       )
					self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]       ,							SCREAMING_SNAKE_CASE_       ,							atol=1e-4       )       )

					# verify the loss (`config.norm_pix_loss` = `True`)
					lowercase_       =					torch.tensor([0.51_42]       ,							device=SCREAMING_SNAKE_CASE_       )
					self.assertTrue(torch.allclose(outputs.loss       ,							SCREAMING_SNAKE_CASE_       ,							atol=1e-4       )       )

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

					with torch.no_grad():
									lowercase_       =					model(**SCREAMING_SNAKE_CASE_       )

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






import logging

from transformers.configuration_utils import PretrainedConfig


__a  			=					logging.getLogger(__name__)
class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	a			:Optional[int]         =	'masked_bert'
	def __init__(       self  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			List[Any]=3_0_5_2_2       ,							SCREAMING_SNAKE_CASE_  :			List[str]=7_6_8       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=1_2       ,							SCREAMING_SNAKE_CASE_  :			Any=1_2       ,							SCREAMING_SNAKE_CASE_  :			str=3_0_7_2       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]="gelu"       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=0.1       ,							SCREAMING_SNAKE_CASE_  :			List[str]=0.1       ,							SCREAMING_SNAKE_CASE_  :			Tuple=5_1_2       ,							SCREAMING_SNAKE_CASE_  :			str=2       ,							SCREAMING_SNAKE_CASE_  :			Dict=0.02       ,							SCREAMING_SNAKE_CASE_  :			Any=1e-12       ,							SCREAMING_SNAKE_CASE_  :			Any=0       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]="topK"       ,							SCREAMING_SNAKE_CASE_  :			Dict="constant"       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]=0.0       ,							**SCREAMING_SNAKE_CASE_  :			Optional[int]       ,							)       ->     Optional[Any]:
					super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_       ,							**SCREAMING_SNAKE_CASE_       )

					lowercase_       =					vocab_size
					lowercase_       =					hidden_size
					lowercase_       =					num_hidden_layers
					lowercase_       =					num_attention_heads
					lowercase_       =					hidden_act
					lowercase_       =					intermediate_size
					lowercase_       =					hidden_dropout_prob
					lowercase_       =					attention_probs_dropout_prob
					lowercase_       =					max_position_embeddings
					lowercase_       =					type_vocab_size
					lowercase_       =					initializer_range
					lowercase_       =					layer_norm_eps
					lowercase_       =					pruning_method
					lowercase_       =					mask_init
					lowercase_       =					mask_scale




