




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






			'''simple docstring'''





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

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

								# use this variable to define bool_masked_pos
								__magic_name__							:	Union[str, Any]    							=							int(mask_ratio * self.seq_length  )

			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Tuple:
								__magic_name__							:	Union[str, Any]    							=							floats_tensor(
								    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]  )

								__magic_name__							:	Any    							=							None
								if self.use_labels:
													__magic_name__							:	Tuple    							=							ids_tensor([self.batch_size]       ,						self.type_sequence_label_size  )

								__magic_name__							:	Union[str, Any]    							=							self.get_config()

								return config, pixel_values, labels

			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   int:
								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 							UpperCAmelCase__       ( self			:   int       ,						lowerCamelCase_			:   Dict       ,						lowerCamelCase_			:   Optional[int]       ,						lowerCamelCase_			:   Any  )						->   Dict:
								__magic_name__							:	Dict    							=							VideoMAEModel(config=lowerCamelCase_  )
								model.to(lowerCamelCase_  )
								model.eval()
								__magic_name__							:	Tuple    							=							model(lowerCamelCase_  )
								self.parent.assertEqual(result.last_hidden_state.shape       ,						(self.batch_size, self.seq_length, self.hidden_size)  )

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

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






@require_torch
class    lowerCamelCase    (		_lowerCamelCase				,_lowerCamelCase				,unittest.TestCase						):






			'''simple docstring'''





			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 							UpperCAmelCase__       ( self			:   List[str]  )						->   Dict:
								__magic_name__							:	Optional[int]    							=							VideoMAEModelTester(self  )
								__magic_name__							:	int    							=							ConfigTester(self       ,						config_class=lowerCamelCase_       ,						has_text_modality=lowerCamelCase_       ,						hidden_size=37  )

			def 							UpperCAmelCase__       ( self			:   Optional[int]       ,						lowerCamelCase_			:   Dict       ,						lowerCamelCase_			:   Optional[Any]       ,						lowerCamelCase_			:   Any=False  )						->   Optional[int]:
								__magic_name__							:	Tuple    							=							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
													__magic_name__							:	List[Any]    							=							torch.ones((self.model_tester.num_masks,)  )
													__magic_name__							:	Tuple    							=							torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0  )  )]  )
													__magic_name__							:	Any    							=							mask.expand(self.model_tester.batch_size       ,						-1  ).bool()
													__magic_name__							:	List[Any]    							=							bool_masked_pos.to(lowerCamelCase_  )

								if return_labels:
													if model_class in [
													    *get_values(lowerCamelCase_  ),
													]:
																		__magic_name__							:	Optional[int]    							=							torch.zeros(
																		    self.model_tester.batch_size       ,						dtype=torch.long       ,						device=lowerCamelCase_  )

								return inputs_dict

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

			@unittest.skip(reason='''VideoMAE does not use inputs_embeds'''  )
			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   int:
								pass

			def 							UpperCAmelCase__       ( self			:   int  )						->   Optional[int]:
								__magic_name__							,    __magic_name__							:	Any    							=							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__magic_name__							:	Any    							=							model_class(lowerCamelCase_  )
													self.assertIsInstance(model.get_input_embeddings()       ,						(nn.Module)  )
													__magic_name__							:	List[Any]    							=							model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase_       ,						nn.Linear  )  )

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Any:
								__magic_name__							,    __magic_name__							:	List[Any]    							=							self.model_tester.prepare_config_and_inputs_for_common()

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

													__magic_name__							:	int    							=							['''pixel_values''']
													self.assertListEqual(arg_names[:1]       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Union[str, Any]:
								__magic_name__							:	Union[str, Any]    							=							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Dict  )						->   Any:
								__magic_name__							:	Dict    							=							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*lowerCamelCase_  )

			@slow
			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Tuple:
								for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__magic_name__							:	Dict    							=							VideoMAEModel.from_pretrained(lowerCamelCase_  )
													self.assertIsNotNone(lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Optional[int]:
								if not self.has_attentions:
													pass

								else:
													__magic_name__							,    __magic_name__							:	Optional[Any]    							=							self.model_tester.prepare_config_and_inputs_for_common()
													__magic_name__							:	List[Any]    							=							True

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

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

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

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

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

													with torch.no_grad():
																		__magic_name__							:	Optional[Any]    							=							model(**self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_  )  )

													__magic_name__							:	int    							=							outputs.hidden_states
													__magic_name__							:	List[str]    							=							self.model_tester.num_hidden_layers + 1
													self.assertEqual(len(lowerCamelCase_  )       ,						lowerCamelCase_  )

													__magic_name__							:	str    							=							self.model_tester.seq_length - self.model_tester.num_masks
													__magic_name__							:	Tuple    							=							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]       ,						)

								__magic_name__							,    __magic_name__							:	List[str]    							=							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__magic_name__							:	Dict    							=							True
													check_hidden_states_output(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_  )

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







def 		lowercase__   (		):





					'''simple docstring'''






					__magic_name__							:	str    							=							hf_hub_download(
					    repo_id='''hf-internal-testing/spaghetti-video'''							,filename='''eating_spaghetti.npy'''							,repo_type='''dataset'''				)
					__magic_name__							:	Union[str, Any]    							=							np.load(__A				)
					return list(__A				)


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






			'''simple docstring'''





			@cached_property
			def 							UpperCAmelCase__       ( 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 							UpperCAmelCase__       ( self			:   Dict  )						->   List[str]:
								__magic_name__							:	List[Any]    							=							VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''  ).to(
								    lowerCamelCase_  )

								__magic_name__							:	Dict    							=							self.default_image_processor
								__magic_name__							:	str    							=							prepare_video()
								__magic_name__							:	str    							=							image_processor(lowerCamelCase_       ,						return_tensors='''pt'''  ).to(lowerCamelCase_  )

								# forward pass
								with torch.no_grad():
													__magic_name__							:	str    							=							model(**lowerCamelCase_  )

								# verify the logits
								__magic_name__							:	List[Any]    							=							torch.Size((1, 400)  )
								self.assertEqual(outputs.logits.shape       ,						lowerCamelCase_  )

								__magic_name__							:	Optional[int]    							=							torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1]  ).to(lowerCamelCase_  )

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


			@slow
			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Union[str, Any]:
								__magic_name__							:	List[Any]    							=							VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''  ).to(lowerCamelCase_  )

								__magic_name__							:	Optional[int]    							=							self.default_image_processor
								__magic_name__							:	str    							=							prepare_video()
								__magic_name__							:	Union[str, Any]    							=							image_processor(lowerCamelCase_       ,						return_tensors='''pt'''  ).to(lowerCamelCase_  )

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

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

								# verify the logits
								__magic_name__							:	Optional[int]    							=							torch.Size([1, 1408, 1536]  )
								__magic_name__							:	Optional[int]    							=							torch.tensor(
								    [[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]]       ,						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`)
								__magic_name__							:	Dict    							=							torch.tensor([0.5_1_4_2]       ,						device=lowerCamelCase_  )
								self.assertTrue(torch.allclose(outputs.loss       ,						lowerCamelCase_       ,						atol=1E-4  )  )

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

								with torch.no_grad():
													__magic_name__							:	str    							=							model(**lowerCamelCase_  )

								__magic_name__							:	Any    							=							torch.tensor(torch.tensor([0.6_4_6_9]  )       ,						device=lowerCamelCase_  )
								self.assertTrue(torch.allclose(outputs.loss       ,						lowerCamelCase_       ,						atol=1E-4  )  )









import functools







def 		lowercase__   (		__A:	list[int]							,__A:	list[int]				):





					'''simple docstring'''






					if not isinstance(__A							,__A				) or not all(isinstance(__A							,__A				) for day in days				):
										raise ValueError('''The parameter days should be a list of integers'''				)

					if len(__A				) != 3 or not all(isinstance(__A							,__A				) for cost in costs				):
										raise ValueError('''The parameter costs should be a list of three integers'''				)

					if len(__A				) == 0:
										return 0

					if min(__A				) <= 0:
										raise ValueError('''All days elements should be greater than 0'''				)

					if max(__A				) >= 3_6_6:
										raise ValueError('''All days elements should be less than 366'''				)

					__magic_name__							:	Tuple    							=							set(__A				)

					@functools.cache
					def dynamic_programming(__A:	int				) -> int:
										if index > 3_6_5:
															return 0

										if index not in days_set:
															return dynamic_programming(index + 1				)

										return min(
										    costs[0] + dynamic_programming(index + 1				)							,costs[1] + dynamic_programming(index + 7				)							,costs[2] + dynamic_programming(index + 3_0				)							,)

					return dynamic_programming(1				)


if __name__ == "__main__":
	import doctest

	doctest.testmod()



