
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      :




				def __init__(      self       ,       _UpperCamelCase       ,       _UpperCamelCase=13       ,       _UpperCamelCase=10       ,       _UpperCamelCase=3       ,       _UpperCamelCase=2       ,       _UpperCamelCase=2       ,       _UpperCamelCase=2       ,       _UpperCamelCase=True       ,       _UpperCamelCase=True       ,       _UpperCamelCase=32       ,       _UpperCamelCase=5       ,       _UpperCamelCase=4       ,       _UpperCamelCase=37       ,       _UpperCamelCase="gelu"       ,       _UpperCamelCase=0.1       ,       _UpperCamelCase=0.1       ,       _UpperCamelCase=10       ,       _UpperCamelCase=0.02       ,       _UpperCamelCase=0.9       ,       _UpperCamelCase=None       ,       )       ->   Dict:
							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   __a		(      self  )       ->   Dict:
							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   __a		(      self  )       ->   Any:
							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=_UpperCamelCase       ,       initializer_range=self.initializer_range       ,       )




				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase  )       ->   int:
							lowerCAmelCase_      			=			VideoMAEModel(config=_UpperCamelCase  )
							model.to(_UpperCamelCase  )
							model.eval()
							lowerCAmelCase_      			=			model(_UpperCamelCase  )
							self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, self.seq_length, self.hidden_size)  )




				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase  )       ->   Optional[Any]:
							lowerCAmelCase_      			=			VideoMAEForPreTraining(_UpperCamelCase  )
							model.to(_UpperCamelCase  )
							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(_UpperCamelCase       ,       _UpperCamelCase  )
							# 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   __a		(      self  )       ->   Dict:
							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      (			__a      ,   __a      ,   unittest.TestCase ):
				_lowercase    	=(
				    (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
				)
				_lowercase    	=(
				    {'''feature-extraction''': VideoMAEModel, '''video-classification''': VideoMAEForVideoClassification}
				    if is_torch_available()
				    else {}
				)

				_lowercase    	=False
				_lowercase    	=False
				_lowercase    	=False
				_lowercase    	=False




				def   __a		(      self  )       ->   Tuple:
							lowerCAmelCase_      			=			VideoMAEModelTester(self  )
							lowerCAmelCase_      			=			ConfigTester(self       ,       config_class=_UpperCamelCase       ,       has_text_modality=_UpperCamelCase       ,       hidden_size=37  )




				def   __a		(      self       ,       _UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase=False  )       ->   int:
							lowerCAmelCase_      			=			copy.deepcopy(_UpperCamelCase  )

							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(_UpperCamelCase  )

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

							return inputs_dict




				def   __a		(      self  )       ->   Tuple:
							self.config_tester.run_common_tests()




				@unittest.skip(reason="VideoMAE does not use inputs_embeds"  )
				def   __a		(      self  )       ->   List[str]:
							pass




				def   __a		(      self  )       ->   Dict:
							lowerCAmelCase_			,						lowerCAmelCase_      			=			self.model_tester.prepare_config_and_inputs_for_common()

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




				def   __a		(      self  )       ->   int:
							lowerCAmelCase_			,						lowerCAmelCase_      			=			self.model_tester.prepare_config_and_inputs_for_common()

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




				def   __a		(      self  )       ->   Any:
							lowerCAmelCase_      			=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase  )




				def   __a		(      self  )       ->   str:
							lowerCAmelCase_      			=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_UpperCamelCase  )




				@slow
				def   __a		(      self  )       ->   Optional[Any]:
							for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase_      			=			VideoMAEModel.from_pretrained(_UpperCamelCase  )
										self.assertIsNotNone(_UpperCamelCase  )




				def   __a		(      self  )       ->   Optional[Any]:
							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(_UpperCamelCase  )
													model.to(_UpperCamelCase  )
													model.eval()
													with torch.no_grad():
																lowerCAmelCase_      			=			model(**self._prepare_for_class(_UpperCamelCase       ,       _UpperCamelCase  )  )
													lowerCAmelCase_      			=			outputs.attentions
													self.assertEqual(len(_UpperCamelCase  )       ,       self.model_tester.num_hidden_layers  )

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													lowerCAmelCase_      			=			True
													lowerCAmelCase_      			=			model_class(_UpperCamelCase  )
													model.to(_UpperCamelCase  )
													model.eval()
													with torch.no_grad():
																lowerCAmelCase_      			=			model(**self._prepare_for_class(_UpperCamelCase       ,       _UpperCamelCase  )  )
													lowerCAmelCase_      			=			outputs.attentions
													self.assertEqual(len(_UpperCamelCase  )       ,       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(_UpperCamelCase  )

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

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

													lowerCAmelCase_      			=			outputs.attentions

													self.assertEqual(len(_UpperCamelCase  )       ,       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   __a		(      self  )       ->   List[str]:
							def check_hidden_states_output(_UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase  ):
										lowerCAmelCase_      			=			model_class(_UpperCamelCase  )
										model.to(_UpperCamelCase  )
										model.eval()

										with torch.no_grad():
													lowerCAmelCase_      			=			model(**self._prepare_for_class(_UpperCamelCase       ,       _UpperCamelCase  )  )

										lowerCAmelCase_      			=			outputs.hidden_states
										lowerCAmelCase_      			=			self.model_tester.num_hidden_layers + 1
										self.assertEqual(len(_UpperCamelCase  )       ,       _UpperCamelCase  )

										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(_UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase  )

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

										check_hidden_states_output(_UpperCamelCase       ,       _UpperCamelCase       ,       _UpperCamelCase  )




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






def      lowerCamelCase__       (       ):




			"""simple docstring"""


			lowerCAmelCase_      			=			hf_hub_download(
			    repo_id="hf-internal-testing/spaghetti-video"					,							filename="eating_spaghetti.npy"					,							repo_type="dataset"       )
			lowerCAmelCase_      			=			np.load(__lowerCAmelCase       )
			return list(__lowerCAmelCase       )





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




				@cached_property
				def   __a		(      self  )       ->   str:
							# 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   __a		(      self  )       ->   Any:
							lowerCAmelCase_      			=			VideoMAEForVideoClassification.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics"  ).to(
							    _UpperCamelCase  )

							lowerCAmelCase_      			=			self.default_image_processor
							lowerCAmelCase_      			=			prepare_video()
							lowerCAmelCase_      			=			image_processor(_UpperCamelCase       ,       return_tensors="pt"  ).to(_UpperCamelCase  )

							# forward pass
							with torch.no_grad():
										lowerCAmelCase_      			=			model(**_UpperCamelCase  )

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

							lowerCAmelCase_      			=			torch.tensor([0.3669, -0.0688, -0.2421]  ).to(_UpperCamelCase  )

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




				@slow
				def   __a		(      self  )       ->   Tuple:
							lowerCAmelCase_      			=			VideoMAEForPreTraining.from_pretrained("MCG-NJU/videomae-base-short"  ).to(_UpperCamelCase  )

							lowerCAmelCase_      			=			self.default_image_processor
							lowerCAmelCase_      			=			prepare_video()
							lowerCAmelCase_      			=			image_processor(_UpperCamelCase       ,       return_tensors="pt"  ).to(_UpperCamelCase  )

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

							# forward pass
							with torch.no_grad():
										lowerCAmelCase_      			=			model(**_UpperCamelCase  )

							# verify the logits
							lowerCAmelCase_      			=			torch.Size([1, 1_408, 1_536]  )
							lowerCAmelCase_      			=			torch.tensor(
							    [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]       ,       device=_UpperCamelCase  )
							self.assertEqual(outputs.logits.shape       ,       _UpperCamelCase  )
							self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]       ,       _UpperCamelCase       ,       atol=1e-4  )  )

							# verify the loss (`config.norm_pix_loss` = `True`)
							lowerCAmelCase_      			=			torch.tensor([0.5142]       ,       device=_UpperCamelCase  )
							self.assertTrue(torch.allclose(outputs.loss       ,       _UpperCamelCase       ,       atol=1e-4  )  )

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

							with torch.no_grad():
										lowerCAmelCase_      			=			model(**_UpperCamelCase  )

							lowerCAmelCase_      			=			torch.tensor(torch.tensor([0.6469]  )       ,       device=_UpperCamelCase  )
							self.assertTrue(torch.allclose(outputs.loss       ,       _UpperCamelCase       ,       atol=1e-4  )  )





def      lowerCamelCase__       (       __lowerCAmelCase						:						int       ):




			"""simple docstring"""


			if upper_limit < 0:
						raise ValueError("Limit for the Catalan sequence must be ≥ 0"       )

			lowerCAmelCase_      			=			[0] * (upper_limit + 1)

			# Base case: C(0) = C(1) = 1
			lowerCAmelCase_      			=			1
			if upper_limit > 0:
						lowerCAmelCase_      			=			1

			# Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
			for i in range(2					,							upper_limit + 1       ):
						for j in range(__lowerCAmelCase       ):
									catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

			return catalan_list


if __name__ == "__main__":
			print("\n********* Catalan Numbers Using Dynamic Programming ************\n")
			print("\n*** Enter -1 at any time to quit ***")
			print("\nEnter the upper limit (≥ 0) for the Catalan number sequence: ", end="")
			try:
						while True:
									_A     =  int(input().strip())
									if N < 0:
												print("\n********* Goodbye!! ************")
												break
									else:
												print(f"""The Catalan numbers from 0 through {N} are:""")
												print(catalan_numbers(N))
												print("Try another upper limit for the sequence: ", end="")
			except (NameError, ValueError):
						print("\n********* Invalid input, goodbye! ************\n")

			import doctest

			doctest.testmod()



