





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       A_			:
	def __init__(       self				,   _A				,   _A=1_3				,   _A=1_0				,   _A=3				,   _A=2				,   _A=2				,   _A=2				,   _A=True				,   _A=True				,   _A=3_2				,   _A=5				,   _A=4				,   _A=3_7				,   _A="gelu"				,   _A=0.1				,   _A=0.1				,   _A=1_0				,   _A=0.02				,   _A=0.9				,   _A=None				,   ):






				'''simple docstring'''







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

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

				# use this variable to define bool_masked_pos
				UpperCAmelCase  						=							int(mask_ratio * self.seq_length						)
	def 	_lowercase							(       self						):






				'''simple docstring'''







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

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

				UpperCAmelCase  						=							self.get_config()

				return config, pixel_values, labels
	def 	_lowercase							(       self						):






				'''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 	_lowercase							(       self				,   _A				,   _A				,   _A						):






				'''simple docstring'''







				UpperCAmelCase  						=							VideoMAEModel(config=lowerCamelCase__						)
				model.to(lowerCamelCase__						)
				model.eval()
				UpperCAmelCase  						=							model(lowerCamelCase__						)
				self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)						)
	def 	_lowercase							(       self				,   _A				,   _A				,   _A						):






				'''simple docstring'''







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

				UpperCAmelCase  						=							model(lowerCamelCase__				,   lowerCamelCase__						)
				# model only returns predictions for masked patches
				UpperCAmelCase  						=							mask.sum().item()
				UpperCAmelCase  						=							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						):






				'''simple docstring'''







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

@require_torch
class       A_			(a_	,				a_	,				unittest.TestCase     ):
	UpperCAmelCase__   =     (
	    (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
	)
	UpperCAmelCase__   =     (
	    {'''feature-extraction''': VideoMAEModel, '''video-classification''': VideoMAEForVideoClassification}
	    if is_torch_available()
	    else {}
	)

	UpperCAmelCase__   =     False
	UpperCAmelCase__   =     False
	UpperCAmelCase__   =     False
	UpperCAmelCase__   =     False
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							VideoMAEModelTester(self						)
				UpperCAmelCase  						=							ConfigTester(self				,   config_class=lowerCamelCase__				,   has_text_modality=lowerCamelCase__				,   hidden_size=3_7						)
	def 	_lowercase							(       self				,   _A				,   _A				,   _A=False						):






				'''simple docstring'''







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

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

				return inputs_dict
	def 	_lowercase							(       self						):






				'''simple docstring'''







				self.config_tester.run_common_tests()
	@unittest.skip(reason='''VideoMAE does not use inputs_embeds'''						)
	def 	_lowercase							(       self						):






				'''simple docstring'''







				pass
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase						,     UpperCAmelCase  						=							self.model_tester.prepare_config_and_inputs_for_common()

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






				'''simple docstring'''







				UpperCAmelCase						,     UpperCAmelCase  						=							self.model_tester.prepare_config_and_inputs_for_common()

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

							UpperCAmelCase  						=							['''pixel_values''']
							self.assertListEqual(arg_names[:1]				,   lowerCamelCase__						)
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCamelCase__						)
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__						)
	@slow
	def 	_lowercase							(       self						):






				'''simple docstring'''







				for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase  						=							VideoMAEModel.from_pretrained(lowerCamelCase__						)
							self.assertIsNotNone(lowerCamelCase__						)
	def 	_lowercase							(       self						):






				'''simple docstring'''







				if not self.has_attentions:
							pass

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

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

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

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

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

										UpperCAmelCase  						=							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						):






				'''simple docstring'''







				def check_hidden_states_output(_A				,   _A				,   _A						):
							UpperCAmelCase  						=							model_class(lowerCamelCase__						)
							model.to(lowerCamelCase__						)
							model.eval()

							with torch.no_grad():
										UpperCAmelCase  						=							model(**self._prepare_for_class(lowerCamelCase__				,   lowerCamelCase__						)						)

							UpperCAmelCase  						=							outputs.hidden_states
							UpperCAmelCase  						=							self.model_tester.num_hidden_layers + 1
							self.assertEqual(len(lowerCamelCase__						)				,   lowerCamelCase__						)

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

				UpperCAmelCase						,     UpperCAmelCase  						=							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							UpperCAmelCase  						=							True
							check_hidden_states_output(lowerCamelCase__				,   lowerCamelCase__				,   lowerCamelCase__						)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							UpperCAmelCase  						=							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						):






				'''simple docstring'''







				pass





def 		__SCREAMING_SNAKE_CASE    (							)    ->							Optional[int]:







			'''simple docstring'''

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






@require_torch
@require_vision
class       A_			(unittest.TestCase     ):
	@cached_property
	def 	_lowercase							(       self						):






				'''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 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''						).to(
				    lowerCamelCase__						)

				UpperCAmelCase  						=							self.default_image_processor
				UpperCAmelCase  						=							prepare_video()
				UpperCAmelCase  						=							image_processor(lowerCamelCase__				,   return_tensors='''pt'''						).to(lowerCamelCase__						)

				# forward pass
				with torch.no_grad():
							UpperCAmelCase  						=							model(**lowerCamelCase__						)

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

				UpperCAmelCase  						=							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						):






				'''simple docstring'''







				UpperCAmelCase  						=							VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''						).to(lowerCamelCase__						)

				UpperCAmelCase  						=							self.default_image_processor
				UpperCAmelCase  						=							prepare_video()
				UpperCAmelCase  						=							image_processor(lowerCamelCase__				,   return_tensors='''pt'''						).to(lowerCamelCase__						)

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

				# forward pass
				with torch.no_grad():
							UpperCAmelCase  						=							model(**lowerCamelCase__						)

				# verify the logits
				UpperCAmelCase  						=							torch.Size([1, 1_4_0_8, 1_5_3_6]						)
				UpperCAmelCase  						=							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`)
				UpperCAmelCase  						=							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`)
				UpperCAmelCase  						=							VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''				,   norm_pix_loss=lowerCamelCase__						).to(
				    lowerCamelCase__						)

				with torch.no_grad():
							UpperCAmelCase  						=							model(**lowerCamelCase__						)

				UpperCAmelCase  						=							torch.tensor(torch.tensor([0.64_69]						)				,   device=lowerCamelCase__						)
				self.assertTrue(torch.allclose(outputs.loss				,   lowerCamelCase__				,   atol=1E-4						)						)









import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch

					from transformers.activations import gelu_new, gelu_python, get_activation







@require_torch
class        __lowerCAmelCase					(			unittest.TestCase					):

			"""simple docstring"""







			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertTrue(torch.allclose(gelu_python(lowerCamelCase__				)     ,		torch_builtin(lowerCamelCase__				)				)				)
							self.assertFalse(torch.allclose(gelu_python(lowerCamelCase__				)     ,		gelu_new(lowerCamelCase__				)				)				)


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



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       get_activation('gelu_10'				)

							__lowerCamelCase									=       torch_builtin(lowerCamelCase__				)
							__lowerCamelCase									=       geluaa(lowerCamelCase__				)

							__lowerCamelCase									=       torch.where(y_gelu_aa < 10.0     ,		1     ,		0				)

							self.assertTrue(torch.max(lowerCamelCase__				).item() == 10.0				)
							self.assertTrue(torch.allclose(y_gelu * clipped_mask     ,		y_gelu_aa * clipped_mask				)				)


			def  lowercase_       ( self				)       -> Any:



							'''simple docstring'''
							get_activation('gelu'				)
							get_activation('gelu_10'				)
							get_activation('gelu_fast'				)
							get_activation('gelu_new'				)
							get_activation('gelu_python'				)
							get_activation('gelu_pytorch_tanh'				)
							get_activation('linear'				)
							get_activation('mish'				)
							get_activation('quick_gelu'				)
							get_activation('relu'				)
							get_activation('sigmoid'				)
							get_activation('silu'				)
							get_activation('swish'				)
							get_activation('tanh'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation('bogus'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation(lowerCamelCase__				)






			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       1
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertEqual(acta.a     ,		1				)
							with self.assertRaises(lowerCamelCase__				):
											__lowerCamelCase									=       acta.a


