




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






				'''simple docstring'''





				def __init__(       self		, _a		, _a=13		, _a=10		, _a=3		, _a=2		, _a=2		, _a=2		, _a=True		, _a=True		, _a=32		, _a=5		, _a=4		, _a=37		, _a="gelu"		, _a=0.1		, _a=0.1		, _a=10		, _a=0.02		, _a=0.9		, _a=None		, ):





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

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

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





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					floats_tensor(
							    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] )

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

							lowerCamelCase			      =					self.get_config()

							return config, pixel_values, labels





				def    _lowerCAmelCase				(       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=_a		, initializer_range=self.initializer_range		, )





				def    _lowerCAmelCase				(       self		, _a		, _a		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					VideoMAEModel(config=_a )
							model.to(_a )
							model.eval()
							lowerCamelCase			      =					model(_a )
							self.parent.assertEqual(result.last_hidden_state.shape		, (self.batch_size, self.seq_length, self.hidden_size) )





				def    _lowerCAmelCase				(       self		, _a		, _a		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					VideoMAEForPreTraining(_a )
							model.to(_a )
							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			      =					torch.ones((self.num_masks,) )
							lowerCamelCase			      =					torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] )
							lowerCamelCase			      =					mask.expand(self.batch_size		, -1 ).bool()

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





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






@require_torch
class     __magic_name__							(       UpperCAmelCase__						,					UpperCAmelCase__						,					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    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					VideoMAEModelTester(self )
							lowerCamelCase			      =					ConfigTester(self		, config_class=_a		, has_text_modality=_a		, hidden_size=37 )





				def    _lowerCAmelCase				(       self		, _a		, _a		, _a=False ):





							"""simple docstring"""
							lowerCamelCase			      =					copy.deepcopy(_a )

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

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

							return inputs_dict





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							self.config_tester.run_common_tests()





				@unittest.skip(reason="""VideoMAE does not use inputs_embeds""" )
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							pass





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase      ,   lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs_for_common()

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





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase      ,   lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs_for_common()

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

										lowerCamelCase			      =					["""pixel_values"""]
										self.assertListEqual(arg_names[:1]		, _a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_a )





				@slow
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCamelCase			      =					VideoMAEModel.from_pretrained(_a )
										self.assertIsNotNone(_a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							if not self.has_attentions:
										pass

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

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

													lowerCamelCase			      =					True
													lowerCamelCase			      =					False
													lowerCamelCase			      =					True
													lowerCamelCase			      =					model_class(_a )
													model.to(_a )
													model.eval()
													with torch.no_grad():
																lowerCamelCase			      =					model(**self._prepare_for_class(_a		, _a ) )
													lowerCamelCase			      =					outputs.attentions
													self.assertEqual(len(_a )		, self.model_tester.num_hidden_layers )

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													lowerCamelCase			      =					True
													lowerCamelCase			      =					model_class(_a )
													model.to(_a )
													model.eval()
													with torch.no_grad():
																lowerCamelCase			      =					model(**self._prepare_for_class(_a		, _a ) )
													lowerCamelCase			      =					outputs.attentions
													self.assertEqual(len(_a )		, self.model_tester.num_hidden_layers )

													self.assertListEqual(
													    list(attentions[0].shape[-3:] )		, [self.model_tester.num_attention_heads, seq_len, seq_len]		, )
													lowerCamelCase			      =					len(_a )

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

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

													lowerCamelCase			      =					outputs.attentions

													self.assertEqual(len(_a )		, 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 ):





							"""simple docstring"""
							def check_hidden_states_output(_a		, _a		, _a ):
										lowerCamelCase			      =					model_class(_a )
										model.to(_a )
										model.eval()

										with torch.no_grad():
													lowerCamelCase			      =					model(**self._prepare_for_class(_a		, _a ) )

										lowerCamelCase			      =					outputs.hidden_states
										lowerCamelCase			      =					self.model_tester.num_hidden_layers + 1
										self.assertEqual(len(_a )		, _a )

										lowerCamelCase			      =					self.model_tester.seq_length - self.model_tester.num_masks
										lowerCamelCase			      =					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			      =					self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCamelCase			      =					True
										check_hidden_states_output(_a		, _a		, _a )

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

										check_hidden_states_output(_a		, _a		, _a )





				@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" )
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							pass




def     a__ (				)						->					Dict:
			lowerCamelCase			      =					hf_hub_download(
			    repo_id="""hf-internal-testing/spaghetti-video"""       , filename="""eating_spaghetti.npy"""       , repo_type="""dataset"""				)
			lowerCamelCase			      =					np.load(snake_case__				)
			return list(snake_case__				)






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






				'''simple docstring'''





				@cached_property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							# 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 ):





							"""simple docstring"""
							lowerCamelCase			      =					VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics""" ).to(
							    _a )

							lowerCamelCase			      =					self.default_image_processor
							lowerCamelCase			      =					prepare_video()
							lowerCamelCase			      =					image_processor(_a		, return_tensors="""pt""" ).to(_a )

							# forward pass
							with torch.no_grad():
										lowerCamelCase			      =					model(**_a )

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

							lowerCamelCase			      =					torch.tensor([0.3_669, -0.0_688, -0.2_421] ).to(_a )

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





				@slow
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short""" ).to(_a )

							lowerCamelCase			      =					self.default_image_processor
							lowerCamelCase			      =					prepare_video()
							lowerCamelCase			      =					image_processor(_a		, return_tensors="""pt""" ).to(_a )

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

							# forward pass
							with torch.no_grad():
										lowerCamelCase			      =					model(**_a )

							# verify the logits
							lowerCamelCase			      =					torch.Size([1, 1_408, 1_536] )
							lowerCamelCase			      =					torch.tensor(
							    [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]]		, device=_a )
							self.assertEqual(outputs.logits.shape		, _a )
							self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		, _a		, atol=1e-4 ) )

							# verify the loss (`config.norm_pix_loss` = `True`)
							lowerCamelCase			      =					torch.tensor([0.5_142]		, device=_a )
							self.assertTrue(torch.allclose(outputs.loss		, _a		, atol=1e-4 ) )

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

							with torch.no_grad():
										lowerCamelCase			      =					model(**_a )

							lowerCamelCase			      =					torch.tensor(torch.tensor([0.6_469] )		, device=_a )
							self.assertTrue(torch.allclose(outputs.loss		, _a		, atol=1e-4 ) )









"""simple docstring"""




def     a__ (				snake_case__				)						->					bool:
			lowerCamelCase			      =					(1 + 24 * n) ** 0.5
			return ((1 + root) / 6) % 1 == 0



def     a__ (				snake_case__ = 50_00				)						->					int:
			lowerCamelCase			      =					[(i * (3 * i - 1)) // 2 for i in range(1       , snake_case__				)]
			for i, pentagonal_i in enumerate(snake_case__				):
						for j in range(snake_case__       , len(snake_case__				)				):
									lowerCamelCase			      =					pentagonal_nums[j]
									lowerCamelCase			      =					pentagonal_i + pentagonal_j
									lowerCamelCase			      =					pentagonal_j - pentagonal_i
									if is_pentagonal(snake_case__				) and is_pentagonal(snake_case__				):
												return b

			return -1


if __name__ == "__main__":
					print(F"""{solution() = }""")



