




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     ,		lowerCamelCase__     ,		lowerCamelCase__=13     ,		lowerCamelCase__=10     ,		lowerCamelCase__=3     ,		lowerCamelCase__=2     ,		lowerCamelCase__=2     ,		lowerCamelCase__=2     ,		lowerCamelCase__=True     ,		lowerCamelCase__=True     ,		lowerCamelCase__=32     ,		lowerCamelCase__=5     ,		lowerCamelCase__=4     ,		lowerCamelCase__=37     ,		lowerCamelCase__="gelu"     ,		lowerCamelCase__=0.1     ,		lowerCamelCase__=0.1     ,		lowerCamelCase__=10     ,		lowerCamelCase__=0.02     ,		lowerCamelCase__=0.9     ,		lowerCamelCase__=None     ,		)       -> List[str]:



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



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



							'''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     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> List[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       VideoMAEModel(config=lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							model.eval()
							__lowerCamelCase									=       model(lowerCamelCase__				)
							self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       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
							__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(lowerCamelCase__     ,		lowerCamelCase__				)
							# 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  lowercase_       ( self				)       -> str:



							'''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        __lowerCAmelCase					(			__magic_name__     ,							__magic_name__     ,							unittest.TestCase					):

			"""simple docstring"""







			snake_case_  						=			(
			    (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
			)
			snake_case_  						=			(
			    {'''feature-extraction''': VideoMAEModel, '''video-classification''': VideoMAEForVideoClassification}
			    if is_torch_available()
			    else {}
			)

			snake_case_  						=			False
			snake_case_  						=			False
			snake_case_  						=			False
			snake_case_  						=			False


			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       VideoMAEModelTester(self				)
							__lowerCamelCase									=       ConfigTester(self     ,		config_class=lowerCamelCase__     ,		has_text_modality=lowerCamelCase__     ,		hidden_size=37				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__=False				)       -> List[str]:



							'''simple docstring'''
							__lowerCamelCase									=       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
											__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(lowerCamelCase__				)

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

							return inputs_dict


			def  lowercase_       ( self				)       -> Tuple:



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


			@unittest.skip(reason='VideoMAE does not use inputs_embeds'				)
			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							pass


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



							'''simple docstring'''
							__lowerCamelCase			,		__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs_for_common()

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


			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase			,		__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
											__lowerCamelCase									=       model_class(lowerCamelCase__				)
											__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]     ,		lowerCamelCase__				)


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



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__				)


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



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__				)


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



							'''simple docstring'''
							for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCamelCase									=       VideoMAEModel.from_pretrained(lowerCamelCase__				)
											self.assertIsNotNone(lowerCamelCase__				)


			def  lowercase_       ( self				)       -> Any:



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

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

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

															__lowerCamelCase									=       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				)       -> Any:



							'''simple docstring'''
							def check_hidden_states_output(lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				):
											__lowerCamelCase									=       model_class(lowerCamelCase__				)
											model.to(lowerCamelCase__				)
											model.eval()

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

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

											__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(lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)

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



							'''simple docstring'''
							pass







def      lowerCamelCase_						(			)	->			Optional[int]:





				"""simple docstring"""


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







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

			"""simple docstring"""







			@cached_property
			def  lowercase_       ( self				)       -> Union[str, Any]:



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



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

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

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

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

							__lowerCamelCase									=       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				)       -> Optional[int]:



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

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

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

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

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

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

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








def      lowerCamelCase_						(			UpperCamelCase__							: str )	->			list:





				"""simple docstring"""


				if n_term == "":
								return []
				__lowerCamelCase									=       []
				for temp in range(int(UpperCamelCase__ ) ):
								series.append(F"""1/{temp + 1}""" if series else '1' )
				return series


if __name__ == "__main__":
					__A								=       input("Enter the last number (nth term) of the Harmonic Series")
					print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n")
					print(harmonic_series(nth_term))


