





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







							def __init__(				self					:     Any		, A					:     Optional[Any]		, A					:     List[Any]=1_3		, A					:     Union[str, Any]=1_0		, A					:     List[str]=3		, A					:     Any=2		, A					:     List[Any]=2		, A					:     List[str]=2		, A					:     Union[str, Any]=True		, A					:     Tuple=True		, A					:     Any=3_2		, A					:     Tuple=5		, A					:     Any=4		, A					:     str=3_7		, A					:     Union[str, Any]="gelu"		, A					:     int=0.1		, A					:     Optional[Any]=0.1		, A					:     str=1_0		, A					:     Optional[Any]=0.02		, A					:     Union[str, Any]=0.9		, A					:     List[Any]=None		, )       ->List[Any]:
										lowerCamelCase__       :						List[str]        =							parent
										lowerCamelCase__       :						Tuple        =							batch_size
										lowerCamelCase__       :						Any        =							image_size
										lowerCamelCase__       :						Optional[int]        =							num_channels
										lowerCamelCase__       :						List[Any]        =							patch_size
										lowerCamelCase__       :						str        =							tubelet_size
										lowerCamelCase__       :						Tuple        =							num_frames
										lowerCamelCase__       :						List[str]        =							is_training
										lowerCamelCase__       :						int        =							use_labels
										lowerCamelCase__       :						int        =							hidden_size
										lowerCamelCase__       :						Union[str, Any]        =							num_hidden_layers
										lowerCamelCase__       :						Optional[Any]        =							num_attention_heads
										lowerCamelCase__       :						Union[str, Any]        =							intermediate_size
										lowerCamelCase__       :						List[str]        =							hidden_act
										lowerCamelCase__       :						str        =							hidden_dropout_prob
										lowerCamelCase__       :						Optional[Any]        =							attention_probs_dropout_prob
										lowerCamelCase__       :						str        =							type_sequence_label_size
										lowerCamelCase__       :						Optional[int]        =							initializer_range
										lowerCamelCase__       :						Any        =							mask_ratio
										lowerCamelCase__       :						Optional[int]        =							scope

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

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







							def 						__lowerCamelCase   (				self					:     Any      )       ->Optional[int]:
										lowerCamelCase__       :						Optional[int]        =							floats_tensor(
										    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

										lowerCamelCase__       :						List[str]        =							None
										if self.use_labels:
													lowerCamelCase__       :						Union[str, Any]        =							ids_tensor([self.batch_size]		, self.type_sequence_label_size      )

										lowerCamelCase__       :						Dict        =							self.get_config()

										return config, pixel_values, labels







							def 						__lowerCamelCase   (				self					:     Any      )       ->str:
										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					:     int		, A					:     str		, A					:     Dict		, A					:     List[Any]      )       ->Dict:
										lowerCamelCase__       :						Any        =							VideoMAEModel(config=__a      )
										model.to(__a      )
										model.eval()
										lowerCamelCase__       :						Union[str, Any]        =							model(__a      )
										self.parent.assertEqual(result.last_hidden_state.shape		, (self.batch_size, self.seq_length, self.hidden_size)      )







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

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


@require_torch
class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_					,lowerCAmelCase_					,unittest.TestCase ):
							_UpperCAmelCase				:  List[str]			   =   (
							    (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
							)
							_UpperCAmelCase				:  Dict			   =   (
							    {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification}
							    if is_torch_available()
							    else {}
							)

							_UpperCAmelCase				:  Union[str, Any]			   =   False
							_UpperCAmelCase				:  int			   =   False
							_UpperCAmelCase				:  Dict			   =   False
							_UpperCAmelCase				:  Any			   =   False







							def 						__lowerCamelCase   (				self					:     Dict      )       ->Any:
										lowerCamelCase__       :						Optional[Any]        =							VideoMAEModelTester(self      )
										lowerCamelCase__       :						List[Any]        =							ConfigTester(self		, config_class=__a		, has_text_modality=__a		, hidden_size=3_7      )







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

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

										return inputs_dict







							def 						__lowerCamelCase   (				self					:     Any      )       ->Optional[int]:
										self.config_tester.run_common_tests()







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







							def 						__lowerCamelCase   (				self					:     str      )       ->Union[str, Any]:
										lowerCamelCase__       ,							lowerCamelCase__       :						Any        =							self.model_tester.prepare_config_and_inputs_for_common()

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







							def 						__lowerCamelCase   (				self					:     Dict      )       ->Optional[Any]:
										lowerCamelCase__       ,							lowerCamelCase__       :						List[Any]        =							self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													lowerCamelCase__       :						List[Any]        =							model_class(__a      )
													lowerCamelCase__       :						str        =							inspect.signature(model.forward      )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													lowerCamelCase__       :						Union[str, Any]        =							[*signature.parameters.keys()]

													lowerCamelCase__       :						str        =							['''pixel_values''']
													self.assertListEqual(arg_names[:1]		, __a      )







							def 						__lowerCamelCase   (				self					:     Tuple      )       ->Any:
										lowerCamelCase__       :						str        =							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__a      )







							def 						__lowerCamelCase   (				self					:     int      )       ->int:
										lowerCamelCase__       :						int        =							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_pretraining(*__a      )







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







							def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Tuple:
										if not self.has_attentions:
													pass

										else:
													lowerCamelCase__       ,							lowerCamelCase__       :						Dict        =							self.model_tester.prepare_config_and_inputs_for_common()
													lowerCamelCase__       :						Optional[int]        =							True

													for model_class in self.all_model_classes:
																lowerCamelCase__       :						List[str]        =							self.model_tester.seq_length - self.model_tester.num_masks
																lowerCamelCase__       :						int        =							(
																    num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
																)

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

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

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

																lowerCamelCase__       :						List[Any]        =							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					:     Tuple      )       ->Optional[Any]:
										def check_hidden_states_output(A					:     Union[str, Any]		, A					:     Tuple		, A					:     Tuple      ):
													lowerCamelCase__       :						Dict        =							model_class(__a      )
													model.to(__a      )
													model.eval()

													with torch.no_grad():
																lowerCamelCase__       :						List[Any]        =							model(**self._prepare_for_class(__a		, __a      )      )

													lowerCamelCase__       :						Optional[Any]        =							outputs.hidden_states
													lowerCamelCase__       :						List[Any]        =							self.model_tester.num_hidden_layers + 1
													self.assertEqual(len(__a      )		, __a      )

													lowerCamelCase__       :						Union[str, Any]        =							self.model_tester.seq_length - self.model_tester.num_masks
													lowerCamelCase__       :						Optional[int]        =							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__       :						int        =							self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													lowerCamelCase__       :						Optional[Any]        =							True
													check_hidden_states_output(__a		, __a		, __a      )

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



def 	_a						(   )    ->    Tuple:







			"""simple docstring"""
			lowerCamelCase__       :						List[Any]        =							hf_hub_download(
			    repo_id='''hf-internal-testing/spaghetti-video'''      ,  filename='''eating_spaghetti.npy'''      ,  repo_type='''dataset'''					)
			lowerCamelCase__       :						Dict        =							np.load(__snake_case					)
			return list(__snake_case					)




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







							@cached_property
							def 						__lowerCamelCase   (				self					:     int      )       ->List[str]:
										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					:     Optional[int]      )       ->str:
										lowerCamelCase__       :						int        =							VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''      ).to(
										    __a      )

										lowerCamelCase__       :						Tuple        =							self.default_image_processor
										lowerCamelCase__       :						str        =							prepare_video()
										lowerCamelCase__       :						Optional[int]        =							image_processor(__a		, return_tensors='''pt'''      ).to(__a      )

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

										# verify the logits
										lowerCamelCase__       :						Optional[Any]        =							torch.Size((1, 4_0_0)      )
										self.assertEqual(outputs.logits.shape		, __a      )

										lowerCamelCase__       :						Optional[Any]        =							torch.tensor([0.36_69, -0.06_88, -0.24_21]      ).to(__a      )

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







							@slow
							def 						__lowerCamelCase   (				self					:     Tuple      )       ->Tuple:
										lowerCamelCase__       :						str        =							VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''      ).to(__a      )

										lowerCamelCase__       :						Any        =							self.default_image_processor
										lowerCamelCase__       :						List[Any]        =							prepare_video()
										lowerCamelCase__       :						Optional[Any]        =							image_processor(__a		, return_tensors='''pt'''      ).to(__a      )

										# add boolean mask, indicating which patches to mask
										lowerCamelCase__       :						Tuple        =							hf_hub_download(repo_id='''hf-internal-testing/bool-masked-pos'''		, filename='''bool_masked_pos.pt'''      )
										lowerCamelCase__       :						List[str]        =							torch.load(__a      )

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

										# verify the logits
										lowerCamelCase__       :						Dict        =							torch.Size([1, 1_4_0_8, 1_5_3_6]      )
										lowerCamelCase__       :						Tuple        =							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=__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__       :						Optional[Any]        =							torch.tensor([0.51_42]		, device=__a      )
										self.assertTrue(torch.allclose(outputs.loss		, __a		, atol=1e-4      )      )

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

										with torch.no_grad():
													lowerCamelCase__       :						Any        =							model(**__a      )

										lowerCamelCase__       :						Union[str, Any]        =							torch.tensor(torch.tensor([0.64_69]      )		, device=__a      )
										self.assertTrue(torch.allclose(outputs.loss		, __a		, atol=1e-4      )      )










def 	_a						(   UpperCAmelCase					)    ->    int:







   """simple docstring"""

   if not isinstance(UpperCAmelCase      ,  UpperCAmelCase					):
      raise TypeError('''only integers accepted as input'''					)
   else:
      lowerCamelCase__       :						Any        =							str(abs(UpperCAmelCase					)					)
      lowerCamelCase__       :						Union[str, Any]        =							[list(UpperCAmelCase					) for char in range(len(UpperCAmelCase					)					)]
      for index in range(len(UpperCAmelCase					)					):
         num_transpositions[index].pop(UpperCAmelCase					)
      return max(
          int(''''''.join(list(UpperCAmelCase					)					)					) for transposition in num_transpositions					)


if __name__ == "__main__":
       __import__('doctest').testmod()



