




'''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     A					:







							def __init__(						self :						List[str]							,    lowerCAmelCase_ :						List[Any]							,    lowerCAmelCase_ :						List[Any]=13							,    lowerCAmelCase_ :						Optional[int]=10							,    lowerCAmelCase_ :						Tuple=3							,    lowerCAmelCase_ :						Optional[int]=2							,    lowerCAmelCase_ :						Optional[int]=2							,    lowerCAmelCase_ :						Optional[int]=2							,    lowerCAmelCase_ :						str=True							,    lowerCAmelCase_ :						List[Any]=True							,    lowerCAmelCase_ :						List[Any]=32							,    lowerCAmelCase_ :						List[Any]=5							,    lowerCAmelCase_ :						str=4							,    lowerCAmelCase_ :						Dict=37							,    lowerCAmelCase_ :						Union[str, Any]="gelu"							,    lowerCAmelCase_ :						Any=0.1							,    lowerCAmelCase_ :						List[Any]=0.1							,    lowerCAmelCase_ :						List[str]=10							,    lowerCAmelCase_ :						List[Any]=0.0_2							,    lowerCAmelCase_ :						Union[str, Any]=0.9							,    lowerCAmelCase_ :						Any=None							,    )    ->							int:


														"""simple docstring"""


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

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

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







							def      __lowerCAmelCase    (						self :						Tuple      )    ->							Any:


														"""simple docstring"""


														_a			  =       floats_tensor(
														    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

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

														_a			  =       self.get_config()

														return config, pixel_values, labels







							def      __lowerCAmelCase    (						self :						Dict      )    ->							int:


														"""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      __lowerCAmelCase    (						self :						int							,    lowerCAmelCase_ :						Tuple							,    lowerCAmelCase_ :						Any							,    lowerCAmelCase_ :						str      )    ->							Union[str, Any]:


														"""simple docstring"""


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







							def      __lowerCAmelCase    (						self :						int							,    lowerCAmelCase_ :						Any							,    lowerCAmelCase_ :						Any							,    lowerCAmelCase_ :						Optional[Any]      )    ->							Tuple:


														"""simple docstring"""


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

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


														"""simple docstring"""


														_a			  =       self.prepare_config_and_inputs()
														_a  ,							_a  ,							_a			  =       config_and_inputs
														_a			  =       {'''pixel_values''': pixel_values}
														return config, inputs_dict



@require_torch
class     A					(	_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      __lowerCAmelCase    (						self :						List[str]      )    ->							Union[str, Any]:


														"""simple docstring"""


														_a			  =       VideoMAEModelTester(self      )
														_a			  =       ConfigTester(self							,    config_class=lowerCAmelCase_							,    has_text_modality=lowerCAmelCase_							,    hidden_size=37      )







							def      __lowerCAmelCase    (						self :						Any							,    lowerCAmelCase_ :						List[str]							,    lowerCAmelCase_ :						Optional[int]							,    lowerCAmelCase_ :						Optional[Any]=False      )    ->							int:


														"""simple docstring"""


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

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

														return inputs_dict







							def      __lowerCAmelCase    (						self :						int      )    ->							Any:


														"""simple docstring"""


														self.config_tester.run_common_tests()







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


														"""simple docstring"""


														pass







							def      __lowerCAmelCase    (						self :						Union[str, Any]      )    ->							Dict:


														"""simple docstring"""


														_a  ,							_a			  =       self.model_tester.prepare_config_and_inputs_for_common()

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







							def      __lowerCAmelCase    (						self :						List[str]      )    ->							Dict:


														"""simple docstring"""


														_a  ,							_a			  =       self.model_tester.prepare_config_and_inputs_for_common()

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

																					_a			  =       ['''pixel_values''']
																					self.assertListEqual(arg_names[:1]							,    lowerCAmelCase_      )







							def      __lowerCAmelCase    (						self :						str      )    ->							int:


														"""simple docstring"""


														_a			  =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_      )







							def      __lowerCAmelCase    (						self :						str      )    ->							Union[str, Any]:


														"""simple docstring"""


														_a			  =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase_      )







							@slow
							def      __lowerCAmelCase    (						self :						Optional[Any]      )    ->							Optional[Any]:


														"""simple docstring"""


														for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_a			  =       VideoMAEModel.from_pretrained(lowerCAmelCase_      )
																					self.assertIsNotNone(lowerCAmelCase_      )







							def      __lowerCAmelCase    (						self :						Tuple      )    ->							Optional[int]:


														"""simple docstring"""


														if not self.has_attentions:
																					pass

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

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

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

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

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

																												_a			  =       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      __lowerCAmelCase    (						self :						Dict      )    ->							Dict:


														"""simple docstring"""


														def check_hidden_states_output(lowerCAmelCase_ :						Tuple							,    lowerCAmelCase_ :						Optional[Any]							,    lowerCAmelCase_ :						str      ):
																					_a			  =       model_class(lowerCAmelCase_      )
																					model.to(lowerCAmelCase_      )
																					model.eval()

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

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

																					_a			  =       self.model_tester.seq_length - self.model_tester.num_masks
																					_a			  =       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]							,    )

														_a  ,							_a			  =       self.model_tester.prepare_config_and_inputs_for_common()

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

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


														"""simple docstring"""


														pass



def 	snake_case_					():





							'''simple docstring'''


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

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







							@cached_property
							def      __lowerCAmelCase    (						self :						Tuple      )    ->							Tuple:


														"""simple docstring"""


														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 :						int      )    ->							Any:


														"""simple docstring"""


														_a			  =       VideoMAEForVideoClassification.from_pretrained('''MCG-NJU/videomae-base-finetuned-kinetics'''      ).to(
														    lowerCAmelCase_      )

														_a			  =       self.default_image_processor
														_a			  =       prepare_video()
														_a			  =       image_processor(lowerCAmelCase_							,    return_tensors='''pt'''      ).to(lowerCAmelCase_      )

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

														# verify the logits
														_a			  =       torch.Size((1, 4_00)      )
														self.assertEqual(outputs.logits.shape							,    lowerCAmelCase_      )

														_a			  =       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      __lowerCAmelCase    (						self :						List[Any]      )    ->							Optional[int]:


														"""simple docstring"""


														_a			  =       VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''      ).to(lowerCAmelCase_      )

														_a			  =       self.default_image_processor
														_a			  =       prepare_video()
														_a			  =       image_processor(lowerCAmelCase_							,    return_tensors='''pt'''      ).to(lowerCAmelCase_      )

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

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

														# verify the logits
														_a			  =       torch.Size([1, 14_08, 15_36]      )
														_a			  =       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`)
														_a			  =       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`)
														_a			  =       VideoMAEForPreTraining.from_pretrained('''MCG-NJU/videomae-base-short'''							,    norm_pix_loss=lowerCAmelCase_      ).to(
														    lowerCAmelCase_      )

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

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







'''simple docstring'''


_snake_case   :   Any     							=	tuple[float, float, float]
_snake_case   :   Optional[int]     							=	tuple[float, float, float]
def 	snake_case_					(UpperCamelCase :						Pointad				,		UpperCamelCase :						Pointad   ):





							'''simple docstring'''


							_a			  =       end_pointa[0] - end_pointa[0]
							_a			  =       end_pointa[1] - end_pointa[1]
							_a			  =       end_pointa[2] - end_pointa[2]
							return (x, y, z)
def 	snake_case_					(UpperCamelCase :						Vectorad				,		UpperCamelCase :						Vectorad   ):





							'''simple docstring'''


							_a			  =       ab[1] * ac[2] - ab[2] * ac[1]  # *i
							_a			  =       (ab[0] * ac[2] - ab[2] * ac[0]) * -1  # *j
							_a			  =       ab[0] * ac[1] - ab[1] * ac[0]  # *k
							return (x, y, z)
def 	snake_case_					(UpperCamelCase :						Vectorad				,		UpperCamelCase :						int   ):





							'''simple docstring'''


							return tuple(round(UpperCamelCase				,		UpperCamelCase   ) for x in vector   ) == (0, 0, 0)

def 	snake_case_					(UpperCamelCase :						Pointad				,		UpperCamelCase :						Pointad				,		UpperCamelCase :						Pointad				,		UpperCamelCase :						int = 10   ):





							'''simple docstring'''


							_a			  =       create_vector(UpperCamelCase				,		UpperCamelCase   )
							_a			  =       create_vector(UpperCamelCase				,		UpperCamelCase   )
							return is_zero_vector(get_ad_vectors_cross(UpperCamelCase				,		UpperCamelCase   )				,		UpperCamelCase   )

