



import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer
from ...utils import logging


lowercase_													=      logging.get_logger(__name__)

lowercase_													=      '''▁'''

lowercase_													=      {'''vocab_file''': '''sentencepiece.bpe.model'''}

lowercase_													=      {
    '''vocab_file''': {
        '''facebook/mbart-large-50-one-to-many-mmt''': (
            '''https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model'''
        ),
    }
}

lowercase_													=      {
    '''facebook/mbart-large-50-one-to-many-mmt''': 10_24,
}

# fmt: off
lowercase_													=      ['''ar_AR''', '''cs_CZ''', '''de_DE''', '''en_XX''', '''es_XX''', '''et_EE''', '''fi_FI''', '''fr_XX''', '''gu_IN''', '''hi_IN''', '''it_IT''', '''ja_XX''', '''kk_KZ''', '''ko_KR''', '''lt_LT''', '''lv_LV''', '''my_MM''', '''ne_NP''', '''nl_XX''', '''ro_RO''', '''ru_RU''', '''si_LK''', '''tr_TR''', '''vi_VN''', '''zh_CN''', '''af_ZA''', '''az_AZ''', '''bn_IN''', '''fa_IR''', '''he_IL''', '''hr_HR''', '''id_ID''', '''ka_GE''', '''km_KH''', '''mk_MK''', '''ml_IN''', '''mn_MN''', '''mr_IN''', '''pl_PL''', '''ps_AF''', '''pt_XX''', '''sv_SE''', '''sw_KE''', '''ta_IN''', '''te_IN''', '''th_TH''', '''tl_XX''', '''uk_UA''', '''ur_PK''', '''xh_ZA''', '''gl_ES''', '''sl_SI''']






class     __a			(  SCREAMING_SNAKE_CASE					):
	SCREAMING_SNAKE_CASE  	=							VOCAB_FILES_NAMES
	SCREAMING_SNAKE_CASE  	=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
	SCREAMING_SNAKE_CASE  	=							PRETRAINED_VOCAB_FILES_MAP
	SCREAMING_SNAKE_CASE  	=							["input_ids", "attention_mask"]

	SCREAMING_SNAKE_CASE  	=							[]
	SCREAMING_SNAKE_CASE  	=							[]



	def __init__(    self				:     Tuple					,						snake_case_				:     Optional[int]					,						snake_case_				:     Any=None					,						snake_case_				:     Optional[Any]=None					,						snake_case_				:     List[Any]="</s>"					,						snake_case_				:     Tuple="</s>"					,						snake_case_				:     int="<s>"					,						snake_case_				:     int="<unk>"					,						snake_case_				:     Union[str, Any]="<pad>"					,						snake_case_				:     List[str]="<mask>"					,						snake_case_				:     Optional[Dict[str, Any]] = None					,						**snake_case_				:     Any					,						)->				None:
						# Mask token behave like a normal word, i.e. include the space before it
						__lowerCAmelCase						 =AddedToken(snake_case_					,						lstrip=snake_case_					,						rstrip=snake_case_) if isinstance(snake_case_					,						snake_case_) else mask_token

						__lowerCAmelCase						 ={} if sp_model_kwargs is None else sp_model_kwargs

						__lowerCAmelCase						 =kwargs.get("""additional_special_tokens"""					,						[])
						kwargs["additional_special_tokens"] += [
						    code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"]
						]

						super().__init__(
						    src_lang=snake_case_					,						tgt_lang=snake_case_					,						eos_token=snake_case_					,						unk_token=snake_case_					,						sep_token=snake_case_					,						cls_token=snake_case_					,						pad_token=snake_case_					,						mask_token=snake_case_					,						sp_model_kwargs=self.sp_model_kwargs					,						**snake_case_					,						)

						__lowerCAmelCase						 =spm.SentencePieceProcessor(**self.sp_model_kwargs)
						self.sp_model.Load(str(snake_case_))
						__lowerCAmelCase						 =vocab_file

						# Original fairseq vocab and spm vocab must be "aligned":
						# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
						# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
						# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
						# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

						# Mimic fairseq token-to-id alignment for the first 4 token
						__lowerCAmelCase						 ={"""<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3}

						# The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
						__lowerCAmelCase						 =1

						__lowerCAmelCase						 =len(self.sp_model)
						__lowerCAmelCase						 ={
						    code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(snake_case_)
						}
						__lowerCAmelCase						 ={v: k for k, v in self.lang_code_to_id.items()}
						__lowerCAmelCase						 =len(self.sp_model) + len(self.lang_code_to_id) + self.fairseq_offset

						self.fairseq_tokens_to_ids.update(self.lang_code_to_id)
						__lowerCAmelCase						 ={v: k for k, v in self.fairseq_tokens_to_ids.items()}

						__lowerCAmelCase						 =src_lang if src_lang is not None else """en_XX"""
						__lowerCAmelCase						 =self.lang_code_to_id[self._src_lang]
						__lowerCAmelCase						 =tgt_lang
						self.set_src_lang_special_tokens(self._src_lang)



	@property
	def 		UpperCamelCase				(    self				:     List[Any])->				int:
						return len(self.sp_model) + len(self.lang_code_to_id) + self.fairseq_offset + 1  # Plus 1 for the mask token



	@property
	def 		UpperCamelCase				(    self				:     int)->				str:
						return self._src_lang



	@src_lang.setter
	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     str)->				None:
						__lowerCAmelCase						 =new_src_lang
						self.set_src_lang_special_tokens(self._src_lang)



	def __getstate__(    self				:     Optional[Any])->				Dict:
						__lowerCAmelCase						 =self.__dict__.copy()
						__lowerCAmelCase						 =None
						return state



	def __setstate__(    self				:     Union[str, Any]					,						snake_case_				:     Dict)->				None:
						__lowerCAmelCase						 =d

						# for backward compatibility
						if not hasattr(self					,						"""sp_model_kwargs"""):
											__lowerCAmelCase						 ={}

						__lowerCAmelCase						 =spm.SentencePieceProcessor(**self.sp_model_kwargs)
						self.sp_model.Load(self.vocab_file)



	def 		UpperCamelCase				(    self				:     Optional[int])->				Dict:
						__lowerCAmelCase						 ={self.convert_ids_to_tokens(snake_case_): i for i in range(self.vocab_size)}
						vocab.update(self.added_tokens_encoder)
						return vocab



	def 		UpperCamelCase				(    self				:     List[Any]					,						snake_case_				:     str)->				List[str]:
						return self.sp_model.encode(snake_case_					,						out_type=snake_case_)



	def 		UpperCamelCase				(    self				:     int					,						snake_case_				:     str)->				int:
						if token in self.fairseq_tokens_to_ids:
											return self.fairseq_tokens_to_ids[token]
						__lowerCAmelCase						 =self.sp_model.PieceToId(snake_case_)

						# Need to return unknown token if the SP model returned 0
						return spm_id + self.fairseq_offset if spm_id else self.unk_token_id



	def 		UpperCamelCase				(    self				:     int					,						snake_case_				:     int)->				str:
						if index in self.fairseq_ids_to_tokens:
											return self.fairseq_ids_to_tokens[index]
						return self.sp_model.IdToPiece(index - self.fairseq_offset)



	def 		UpperCamelCase				(    self				:     Optional[int]					,						snake_case_				:     str)->				Any:
						__lowerCAmelCase						 =[]
						__lowerCAmelCase						 =""""""
						__lowerCAmelCase						 =False
						for token in tokens:
											# make sure that special tokens are not decoded using sentencepiece model
											if token in self.all_special_tokens:
																if not prev_is_special:
																					out_string += " "
																out_string += self.sp_model.decode(snake_case_) + token
																__lowerCAmelCase						 =True
																__lowerCAmelCase						 =[]
											else:
																current_sub_tokens.append(snake_case_)
																__lowerCAmelCase						 =False
						out_string += self.sp_model.decode(snake_case_)
						return out_string.strip()



	def 		UpperCamelCase				(    self				:     Union[str, Any]					,						snake_case_				:     str					,						snake_case_				:     Optional[str] = None)->				Tuple[str]:
						if not os.path.isdir(snake_case_):
											logger.error(F"""Vocabulary path ({save_directory}) should be a directory""")
											return
						__lowerCAmelCase						 =os.path.join(
						    snake_case_					,						(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""])

						if os.path.abspath(self.vocab_file) != os.path.abspath(snake_case_) and os.path.isfile(self.vocab_file):
											copyfile(self.vocab_file					,						snake_case_)
						elif not os.path.isfile(self.vocab_file):
											with open(snake_case_					,						"""wb""") as fi:
																__lowerCAmelCase						 =self.sp_model.serialized_model_proto()
																fi.write(snake_case_)

						return (out_vocab_file,)



	def 		UpperCamelCase				(    self				:     Optional[Any]					,						snake_case_				:     List[int]					,						snake_case_				:     Optional[List[int]] = None					,						snake_case_				:     bool = False)->				List[int]:

						if already_has_special_tokens:
											return super().get_special_tokens_mask(
											    token_ids_a=snake_case_					,						token_ids_a=snake_case_					,						already_has_special_tokens=snake_case_)

						__lowerCAmelCase						 =[1] * len(self.prefix_tokens)
						__lowerCAmelCase						 =[1] * len(self.suffix_tokens)
						if token_ids_a is None:
											return prefix_ones + ([0] * len(snake_case_)) + suffix_ones
						return prefix_ones + ([0] * len(snake_case_)) + ([0] * len(snake_case_)) + suffix_ones



	def 		UpperCamelCase				(    self				:     Union[str, Any]					,						snake_case_				:     List[int]					,						snake_case_				:     Optional[List[int]] = None)->				List[int]:
						if token_ids_a is None:
											return self.prefix_tokens + token_ids_a + self.suffix_tokens
						# We don't expect to process pairs, but leave the pair logic for API consistency
						return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens



	def 		UpperCamelCase				(    self				:     Tuple					,						snake_case_				:     Any					,						snake_case_				:     str					,						snake_case_				:     Optional[str]					,						snake_case_				:     Optional[str]					,						**snake_case_				:     Optional[int])->				Union[str, Any]:
						if src_lang is None or tgt_lang is None:
											raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""")
						__lowerCAmelCase						 =src_lang
						__lowerCAmelCase						 =self(snake_case_					,						add_special_tokens=snake_case_					,						return_tensors=snake_case_					,						**snake_case_)
						__lowerCAmelCase						 =self.convert_tokens_to_ids(snake_case_)
						__lowerCAmelCase						 =tgt_lang_id
						return inputs



	def 		UpperCamelCase				(    self				:     Optional[Any]					,						snake_case_				:     List[str]					,						snake_case_				:     str = "en_XX"					,						snake_case_				:     Optional[List[str]] = None					,						snake_case_				:     str = "ro_RO"					,						**snake_case_				:     str					,						)->				BatchEncoding:
						__lowerCAmelCase						 =src_lang
						__lowerCAmelCase						 =tgt_lang
						return super().prepare_seqaseq_batch(snake_case_					,						snake_case_					,						**snake_case_)



	def 		UpperCamelCase				(    self				:     List[Any])->				int:
						return self.set_src_lang_special_tokens(self.src_lang)



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				str:
						return self.set_tgt_lang_special_tokens(self.tgt_lang)



	def 		UpperCamelCase				(    self				:     Optional[int]					,						snake_case_				:     str)->				None:
						__lowerCAmelCase						 =self.lang_code_to_id[src_lang]
						__lowerCAmelCase						 =[self.cur_lang_code_id]
						__lowerCAmelCase						 =[self.eos_token_id]



	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     str)->				None:
						__lowerCAmelCase						 =self.lang_code_to_id[tgt_lang]
						__lowerCAmelCase						 =[self.cur_lang_code_id]
						__lowerCAmelCase						 =[self.eos_token_id]









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[Any]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Tuple=13					,						snake_case_				:     Optional[int]=10					,						snake_case_				:     str=3					,						snake_case_				:     Optional[int]=2					,						snake_case_				:     List[Any]=2					,						snake_case_				:     int=2					,						snake_case_				:     Any=True					,						snake_case_				:     Optional[Any]=True					,						snake_case_				:     List[str]=32					,						snake_case_				:     Any=5					,						snake_case_				:     List[Any]=4					,						snake_case_				:     Optional[Any]=37					,						snake_case_				:     Tuple="gelu"					,						snake_case_				:     str=0.1					,						snake_case_				:     Any=0.1					,						snake_case_				:     List[Any]=10					,						snake_case_				:     Tuple=0.0_2					,						snake_case_				:     List[str]=0.9					,						snake_case_				:     List[Any]=None					,						)->				Tuple:
						__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 		UpperCamelCase				(    self				:     Any)->				int:
						__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 		UpperCamelCase				(    self				:     Optional[int])->				List[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=snake_case_					,						initializer_range=self.initializer_range					,						)



	def 		UpperCamelCase				(    self				:     Optional[Any]					,						snake_case_				:     str					,						snake_case_				:     List[Any]					,						snake_case_				:     Union[str, Any])->				Union[str, Any]:
						__lowerCAmelCase						 =VideoMAEModel(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =model(snake_case_)
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size))



	def 		UpperCamelCase				(    self				:     Any					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     List[Any]					,						snake_case_				:     Tuple)->				Optional[int]:
						__lowerCAmelCase						 =VideoMAEForPreTraining(snake_case_)
						model.to(snake_case_)
						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(snake_case_					,						snake_case_)
						# 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 		UpperCamelCase				(    self				:     List[Any])->				List[str]:
						__lowerCAmelCase						 =self.prepare_config_and_inputs()
						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase						 =config_and_inputs
						__lowerCAmelCase						 ={"""pixel_values""": pixel_values}
						return config, inputs_dict






@require_torch
class     __a			(  SCREAMING_SNAKE_CASE						,     SCREAMING_SNAKE_CASE						,     unittest.TestCase					):
	SCREAMING_SNAKE_CASE  	=							(
	    (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
	)
	SCREAMING_SNAKE_CASE  	=							(
	    {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification}
	    if is_torch_available()
	    else {}
	)

	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False



	def 		UpperCamelCase				(    self				:     List[Any])->				List[Any]:
						__lowerCAmelCase						 =VideoMAEModelTester(self)
						__lowerCAmelCase						 =ConfigTester(self					,						config_class=snake_case_					,						has_text_modality=snake_case_					,						hidden_size=37)



	def 		UpperCamelCase				(    self				:     Tuple					,						snake_case_				:     str					,						snake_case_				:     Tuple					,						snake_case_				:     List[str]=False)->				Tuple:
						__lowerCAmelCase						 =copy.deepcopy(snake_case_)

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

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

						return inputs_dict



	def 		UpperCamelCase				(    self				:     List[str])->				Any:
						self.config_tester.run_common_tests()



	@unittest.skip(reason="""VideoMAE does not use inputs_embeds""")
	def 		UpperCamelCase				(    self				:     Dict)->				str:
						pass



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				Tuple:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

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



	def 		UpperCamelCase				(    self				:     Optional[int])->				List[str]:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

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



	def 		UpperCamelCase				(    self				:     str)->				List[Any]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*snake_case_)



	def 		UpperCamelCase				(    self				:     List[Any])->				Any:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_pretraining(*snake_case_)



	@slow
	def 		UpperCamelCase				(    self				:     List[Any])->				Any:
						for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase						 =VideoMAEModel.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)



	def 		UpperCamelCase				(    self				:     List[Any])->				List[str]:
						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(snake_case_)
																model.to(snake_case_)
																model.eval()
																with torch.no_grad():
																					__lowerCAmelCase						 =model(**self._prepare_for_class(snake_case_					,						snake_case_))
																__lowerCAmelCase						 =outputs.attentions
																self.assertEqual(len(snake_case_)					,						self.model_tester.num_hidden_layers)

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

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

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

																__lowerCAmelCase						 =outputs.attentions

																self.assertEqual(len(snake_case_)					,						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 		UpperCamelCase				(    self				:     Tuple)->				Any:
						def check_hidden_states_output(snake_case_				:     Tuple					,						snake_case_				:     Tuple					,						snake_case_				:     int):
											__lowerCAmelCase						 =model_class(snake_case_)
											model.to(snake_case_)
											model.eval()

											with torch.no_grad():
																__lowerCAmelCase						 =model(**self._prepare_for_class(snake_case_					,						snake_case_))

											__lowerCAmelCase						 =outputs.hidden_states
											__lowerCAmelCase						 =self.model_tester.num_hidden_layers + 1
											self.assertEqual(len(snake_case_)					,						snake_case_)

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

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

											check_hidden_states_output(snake_case_					,						snake_case_					,						snake_case_)



	@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""")
	def 		UpperCamelCase				(    self				:     Optional[Any])->				int:
						pass






def        __lowerCAmelCase		(	)	->    Any:
					__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     __a			(  unittest.TestCase					):



	@cached_property
	def 		UpperCamelCase				(    self				:     str)->				List[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 		UpperCamelCase				(    self				:     Optional[Any])->				Tuple:
						__lowerCAmelCase						 =VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics""").to(
						    snake_case_)

						__lowerCAmelCase						 =self.default_image_processor
						__lowerCAmelCase						 =prepare_video()
						__lowerCAmelCase						 =image_processor(snake_case_					,						return_tensors="""pt""").to(snake_case_)

						# forward pass
						with torch.no_grad():
											__lowerCAmelCase						 =model(**snake_case_)

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

						__lowerCAmelCase						 =torch.tensor([0.3_6_6_9, -0.0_6_8_8, -0.2_4_2_1]).to(snake_case_)

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



	@slow
	def 		UpperCamelCase				(    self				:     str)->				str:
						__lowerCAmelCase						 =VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short""").to(snake_case_)

						__lowerCAmelCase						 =self.default_image_processor
						__lowerCAmelCase						 =prepare_video()
						__lowerCAmelCase						 =image_processor(snake_case_					,						return_tensors="""pt""").to(snake_case_)

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

						# forward pass
						with torch.no_grad():
											__lowerCAmelCase						 =model(**snake_case_)

						# verify the logits
						__lowerCAmelCase						 =torch.Size([1, 14_08, 15_36])
						__lowerCAmelCase						 =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=snake_case_)
						self.assertEqual(outputs.logits.shape					,						snake_case_)
						self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]					,						snake_case_					,						atol=1e-4))

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

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

						with torch.no_grad():
											__lowerCAmelCase						 =model(**snake_case_)

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




