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

		def __init__(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE=13	,		_SCREAMING_SNAKE_CASE=10	,		_SCREAMING_SNAKE_CASE=3	,		_SCREAMING_SNAKE_CASE=2	,		_SCREAMING_SNAKE_CASE=2	,		_SCREAMING_SNAKE_CASE=2	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=True	,		_SCREAMING_SNAKE_CASE=32	,		_SCREAMING_SNAKE_CASE=5	,		_SCREAMING_SNAKE_CASE=4	,		_SCREAMING_SNAKE_CASE=37	,		_SCREAMING_SNAKE_CASE="gelu"	,		_SCREAMING_SNAKE_CASE=0.1	,		_SCREAMING_SNAKE_CASE=0.1	,		_SCREAMING_SNAKE_CASE=10	,		_SCREAMING_SNAKE_CASE=0.02	,		_SCREAMING_SNAKE_CASE=0.9	,		_SCREAMING_SNAKE_CASE=None	,		):
			__lowerCAmelCase      :						int						      =				parent
			__lowerCAmelCase      :						List[Any]						      =				batch_size
			__lowerCAmelCase      :						str						      =				image_size
			__lowerCAmelCase      :						List[Any]						      =				num_channels
			__lowerCAmelCase      :						List[str]						      =				patch_size
			__lowerCAmelCase      :						Optional[Any]						      =				tubelet_size
			__lowerCAmelCase      :						Dict						      =				num_frames
			__lowerCAmelCase      :						int						      =				is_training
			__lowerCAmelCase      :						Optional[int]						      =				use_labels
			__lowerCAmelCase      :						Tuple						      =				hidden_size
			__lowerCAmelCase      :						List[str]						      =				num_hidden_layers
			__lowerCAmelCase      :						Union[str, Any]						      =				num_attention_heads
			__lowerCAmelCase      :						str						      =				intermediate_size
			__lowerCAmelCase      :						Tuple						      =				hidden_act
			__lowerCAmelCase      :						Union[str, Any]						      =				hidden_dropout_prob
			__lowerCAmelCase      :						Dict						      =				attention_probs_dropout_prob
			__lowerCAmelCase      :						Optional[int]						      =				type_sequence_label_size
			__lowerCAmelCase      :						str						      =				initializer_range
			__lowerCAmelCase      :						str						      =				mask_ratio
			__lowerCAmelCase      :						Any						      =				scope

			# in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
			__lowerCAmelCase      :						Union[str, Any]						      =				(image_size // patch_size) ** 2
			__lowerCAmelCase      :						int						      =				(num_frames // tubelet_size) * self.num_patches_per_frame

			# use this variable to define bool_masked_pos
			__lowerCAmelCase      :						Optional[Any]						      =				int(mask_ratio * self.seq_length			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						int						      =				floats_tensor(
			    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]			)

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

			__lowerCAmelCase      :						List[str]						      =				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=_SCREAMING_SNAKE_CASE	,		initializer_range=self.initializer_range	,		)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Union[str, Any]						      =				VideoMAEModel(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Optional[Any]						      =				model(_SCREAMING_SNAKE_CASE			)
			self.parent.assertEqual(result.last_hidden_state.shape	,		(self.batch_size, self.seq_length, self.hidden_size)			)

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

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




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

		A_						:	List[str]           =     False
		A_						:	Optional[Any]           =     False
		A_						:	str           =     False
		A_						:	Tuple           =     False

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Union[str, Any]						      =				VideoMAEModelTester(self			)
			__lowerCAmelCase      :						Optional[int]						      =				ConfigTester(self	,		config_class=_SCREAMING_SNAKE_CASE	,		has_text_modality=_SCREAMING_SNAKE_CASE	,		hidden_size=37			)

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

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

			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			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						Union[str, Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()

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

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						int						      =				self.model_tester.prepare_config_and_inputs_for_common()

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

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

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[str]						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Dict						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE			)

		@slow
		def 							__lowerCamelCase							(				self			):
			for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__lowerCAmelCase      :						List[str]						      =				VideoMAEModel.from_pretrained(_SCREAMING_SNAKE_CASE			)
				self.assertIsNotNone(_SCREAMING_SNAKE_CASE			)

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

			else:
				__lowerCAmelCase      ,       __lowerCAmelCase      :						Union[str, Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()
				__lowerCAmelCase      :						Union[str, Any]						      =				True

				for model_class in self.all_model_classes:
					__lowerCAmelCase      :						Union[str, Any]						      =				self.model_tester.seq_length - self.model_tester.num_masks
					__lowerCAmelCase      :						Optional[Any]						      =				(
					    num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
					)

					__lowerCAmelCase      :						List[Any]						      =				True
					__lowerCAmelCase      :						List[str]						      =				False
					__lowerCAmelCase      :						Optional[Any]						      =				True
					__lowerCAmelCase      :						Dict						      =				model_class(_SCREAMING_SNAKE_CASE			)
					model.to(_SCREAMING_SNAKE_CASE			)
					model.eval()
					with torch.no_grad():
						__lowerCAmelCase      :						Tuple						      =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)			)
					__lowerCAmelCase      :						Any						      =				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"]
					__lowerCAmelCase      :						Union[str, Any]						      =				True
					__lowerCAmelCase      :						Dict						      =				model_class(_SCREAMING_SNAKE_CASE			)
					model.to(_SCREAMING_SNAKE_CASE			)
					model.eval()
					with torch.no_grad():
						__lowerCAmelCase      :						List[Any]						      =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)			)
					__lowerCAmelCase      :						str						      =				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]	,		)
					__lowerCAmelCase      :						Tuple						      =				len(_SCREAMING_SNAKE_CASE			)

					# Check attention is always last and order is fine
					__lowerCAmelCase      :						Union[str, Any]						      =				True
					__lowerCAmelCase      :						int						      =				True
					__lowerCAmelCase      :						Optional[int]						      =				model_class(_SCREAMING_SNAKE_CASE			)
					model.to(_SCREAMING_SNAKE_CASE			)
					model.eval()
					with torch.no_grad():
						__lowerCAmelCase      :						List[Any]						      =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)			)

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

					__lowerCAmelCase      :						Union[str, Any]						      =				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 							__lowerCamelCase							(				self			):
			def check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
				__lowerCAmelCase      :						List[Any]						      =				model_class(_SCREAMING_SNAKE_CASE			)
				model.to(_SCREAMING_SNAKE_CASE			)
				model.eval()

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

				__lowerCAmelCase      :						str						      =				outputs.hidden_states
				__lowerCAmelCase      :						str						      =				self.model_tester.num_hidden_layers + 1
				self.assertEqual(len(_SCREAMING_SNAKE_CASE			)	,		_SCREAMING_SNAKE_CASE			)

				__lowerCAmelCase      :						str						      =				self.model_tester.seq_length - self.model_tester.num_masks
				__lowerCAmelCase      :						Any						      =				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      :						Optional[Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__lowerCAmelCase      :						List[Any]						      =				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"]
				__lowerCAmelCase      :						int						      =				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 							__lowerCamelCase							(				self			):
			pass



def    __lowerCAmelCase    ():
	__lowerCAmelCase      :						List[Any]						      =				hf_hub_download(
	    repo_id='hf-internal-testing/spaghetti-video'	,  filename='eating_spaghetti.npy'	,  repo_type='dataset'		)
	__lowerCAmelCase      :						int						      =				np.load(_UpperCamelCase		)
	return list(_UpperCamelCase		)




@require_torch
@require_vision
class 			A__	(     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			):
			__lowerCAmelCase      :						Union[str, Any]						      =				VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics'			).to(
			    _SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						Optional[int]						      =				self.default_image_processor
			__lowerCAmelCase      :						List[Any]						      =				prepare_video()
			__lowerCAmelCase      :						Optional[int]						      =				image_processor(_SCREAMING_SNAKE_CASE	,		return_tensors='pt'			).to(_SCREAMING_SNAKE_CASE			)

			# forward pass
			with torch.no_grad():
				__lowerCAmelCase      :						List[Any]						      =				model(**_SCREAMING_SNAKE_CASE			)

			# verify the logits
			__lowerCAmelCase      :						Dict						      =				torch.Size((1, 4_00)			)
			self.assertEqual(outputs.logits.shape	,		_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						Optional[Any]						      =				torch.tensor([0.3669, -0.0688, -0.2421]			).to(_SCREAMING_SNAKE_CASE			)

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







		@slow
		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						int						      =				VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'			).to(_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						Optional[int]						      =				self.default_image_processor
			__lowerCAmelCase      :						str						      =				prepare_video()
			__lowerCAmelCase      :						str						      =				image_processor(_SCREAMING_SNAKE_CASE	,		return_tensors='pt'			).to(_SCREAMING_SNAKE_CASE			)

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

			# forward pass
			with torch.no_grad():
				__lowerCAmelCase      :						Union[str, Any]						      =				model(**_SCREAMING_SNAKE_CASE			)

			# verify the logits
			__lowerCAmelCase      :						Union[str, Any]						      =				torch.Size([1, 14_08, 15_36]			)
			__lowerCAmelCase      :						Dict						      =				torch.tensor(
			    [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]	,		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`)
			__lowerCAmelCase      :						Tuple						      =				torch.tensor([0.5142]	,		device=_SCREAMING_SNAKE_CASE			)
			self.assertTrue(torch.allclose(outputs.loss	,		_SCREAMING_SNAKE_CASE	,		atol=1E-4			)			)

			# verify the loss (`config.norm_pix_loss` = `False`)
			__lowerCAmelCase      :						List[Any]						      =				VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'	,		norm_pix_loss=_SCREAMING_SNAKE_CASE			).to(
			    _SCREAMING_SNAKE_CASE			)

			with torch.no_grad():
				__lowerCAmelCase      :						Dict						      =				model(**_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						Optional[Any]						      =				torch.tensor(torch.tensor([0.6469]			)	,		device=_SCREAMING_SNAKE_CASE			)
			self.assertTrue(torch.allclose(outputs.loss	,		_SCREAMING_SNAKE_CASE	,		atol=1E-4			)			)

"""simple docstring"""







import pytest

import datasets.config
from datasets.utils.info_utils import is_small_dataset
@pytest.mark.parametrize('dataset_size'	,  [None, 400 * 2**20, 600 * 2**20]		)
@pytest.mark.parametrize('input_in_memory_max_size'	,  ['default', 0, 100 * 2**20, 900 * 2**20]		)
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase		):
	if input_in_memory_max_size != "default":
		monkeypatch.setattr(datasets.config	,  'IN_MEMORY_MAX_SIZE'	,  _UpperCamelCase		)
	__lowerCAmelCase      :						Union[str, Any]						      =				datasets.config.IN_MEMORY_MAX_SIZE
	if input_in_memory_max_size == "default":
		assert in_memory_max_size == 0
	else:
		assert in_memory_max_size == input_in_memory_max_size
	if dataset_size and in_memory_max_size:
		__lowerCAmelCase      :						Tuple						      =				dataset_size < in_memory_max_size
	else:
		__lowerCAmelCase      :						str						      =				False
	__lowerCAmelCase      :						Optional[int]						      =				is_small_dataset(_UpperCamelCase		)
	assert result == expected