






"""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        UpperCamelCase__ :







    """simple docstring"""







    def __init__(		self :	Optional[Any]				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	List[Any]=1_3				,    UpperCamelCase_ :	str=1_0				,    UpperCamelCase_ :	int=3				,    UpperCamelCase_ :	List[str]=2				,    UpperCamelCase_ :	Tuple=2				,    UpperCamelCase_ :	List[Any]=2				,    UpperCamelCase_ :	List[str]=True				,    UpperCamelCase_ :	int=True				,    UpperCamelCase_ :	Dict=3_2				,    UpperCamelCase_ :	Tuple=5				,    UpperCamelCase_ :	Optional[Any]=4				,    UpperCamelCase_ :	List[str]=3_7				,    UpperCamelCase_ :	Union[str, Any]="gelu"				,    UpperCamelCase_ :	str=0.1				,    UpperCamelCase_ :	Any=0.1				,    UpperCamelCase_ :	Optional[Any]=1_0				,    UpperCamelCase_ :	Union[str, Any]=0.02				,    UpperCamelCase_ :	Tuple=0.9				,    UpperCamelCase_ :	Optional[Any]=None				,    ):





           '''simple docstring'''

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

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

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







    def 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

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

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

           __magic_name__      				=    self.get_config()

           return config, pixel_values, labels







    def 				a__			(		self :	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=UpperCamelCase_				,    initializer_range=self.initializer_range				,    )







    def 				a__			(		self :	Any				,    UpperCamelCase_ :	Optional[Any]				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	str ):





           '''simple docstring'''

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







    def 				a__			(		self :	Optional[Any]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	Any				,    UpperCamelCase_ :	List[Any] ):





           '''simple docstring'''

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

           __magic_name__      				=    model(UpperCamelCase_				,    UpperCamelCase_ )
           # model only returns predictions for masked patches
           __magic_name__      				=    mask.sum().item()
           __magic_name__      				=    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 :	List[Any] ):





           '''simple docstring'''

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



@require_torch
class        UpperCamelCase__ (				a_							,	a_							,	unittest.TestCase):







    """simple docstring"""
    __UpperCAmelCase           = (
        (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
    )
    __UpperCAmelCase           = (
        {"""feature-extraction""": VideoMAEModel, """video-classification""": VideoMAEForVideoClassification}
        if is_torch_available()
        else {}
    )

    __UpperCAmelCase           = False
    __UpperCAmelCase           = False
    __UpperCAmelCase           = False
    __UpperCAmelCase           = False







    def 				a__			(		self :	int ):





           '''simple docstring'''

           __magic_name__      				=    VideoMAEModelTester(self )
           __magic_name__      				=    ConfigTester(self				,    config_class=UpperCamelCase_				,    has_text_modality=UpperCamelCase_				,    hidden_size=3_7 )







    def 				a__			(		self :	Optional[Any]				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	Optional[Any]				,    UpperCamelCase_ :	Tuple=False ):





           '''simple docstring'''

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

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

           return inputs_dict







    def 				a__			(		self :	Optional[int] ):





           '''simple docstring'''

           self.config_tester.run_common_tests()







    @unittest.skip(reason='VideoMAE does not use inputs_embeds' )
    def 				a__			(		self :	int ):





           '''simple docstring'''

           pass







    def 				a__			(		self :	Tuple ):





           '''simple docstring'''

           __magic_name__				,  __magic_name__      				=    self.model_tester.prepare_config_and_inputs_for_common()

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







    def 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

           __magic_name__				,  __magic_name__      				=    self.model_tester.prepare_config_and_inputs_for_common()

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

                  __magic_name__      				=    ['pixel_values']
                  self.assertListEqual(arg_names[:1]				,    UpperCamelCase_ )







    def 				a__			(		self :	int ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase_ )







    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*UpperCamelCase_ )







    @slow
    def 				a__			(		self :	str ):





           '''simple docstring'''

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







    def 				a__			(		self :	int ):





           '''simple docstring'''

           if not self.has_attentions:
                  pass

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

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

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

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

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

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





           '''simple docstring'''

           def check_hidden_states_output(UpperCamelCase_ :	Any				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	int ):
                  __magic_name__      				=    model_class(UpperCamelCase_ )
                  model.to(UpperCamelCase_ )
                  model.eval()

                  with torch.no_grad():
                         __magic_name__      				=    model(**self._prepare_for_class(UpperCamelCase_				,    UpperCamelCase_ ) )

                  __magic_name__      				=    outputs.hidden_states
                  __magic_name__      				=    self.model_tester.num_hidden_layers + 1
                  self.assertEqual(len(UpperCamelCase_ )				,    UpperCamelCase_ )

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

           __magic_name__				,  __magic_name__      				=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  __magic_name__      				=    True
                  check_hidden_states_output(UpperCamelCase_				,    UpperCamelCase_				,    UpperCamelCase_ )

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





           '''simple docstring'''

           pass


def 							A						(				)    ->		Dict:

       """simple docstring"""





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






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







    """simple docstring"""







    @cached_property
    def 				a__			(		self :	Any ):





           '''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 				a__			(		self :	Tuple ):





           '''simple docstring'''

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

           __magic_name__      				=    self.default_image_processor
           __magic_name__      				=    prepare_video()
           __magic_name__      				=    image_processor(UpperCamelCase_				,    return_tensors='pt' ).to(UpperCamelCase_ )

           # forward pass
           with torch.no_grad():
                  __magic_name__      				=    model(**UpperCamelCase_ )

           # verify the logits
           __magic_name__      				=    torch.Size((1, 4_0_0) )
           self.assertEqual(outputs.logits.shape				,    UpperCamelCase_ )

           __magic_name__      				=    torch.tensor([0.3_669, -0.0_688, -0.2_421] ).to(UpperCamelCase_ )

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







    @slow
    def 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

           __magic_name__      				=    VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short' ).to(UpperCamelCase_ )

           __magic_name__      				=    self.default_image_processor
           __magic_name__      				=    prepare_video()
           __magic_name__      				=    image_processor(UpperCamelCase_				,    return_tensors='pt' ).to(UpperCamelCase_ )

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

           # forward pass
           with torch.no_grad():
                  __magic_name__      				=    model(**UpperCamelCase_ )

           # verify the logits
           __magic_name__      				=    torch.Size([1, 1_4_0_8, 1_5_3_6] )
           __magic_name__      				=    torch.tensor(
               [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]]				,    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`)
           __magic_name__      				=    torch.tensor([0.5_142]				,    device=UpperCamelCase_ )
           self.assertTrue(torch.allclose(outputs.loss				,    UpperCamelCase_				,    atol=1e-4 ) )

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

           with torch.no_grad():
                  __magic_name__      				=    model(**UpperCamelCase_ )

           __magic_name__      				=    torch.tensor(torch.tensor([0.6_469] )				,    device=UpperCamelCase_ )
           self.assertTrue(torch.allclose(outputs.loss				,    UpperCamelCase_				,    atol=1e-4 ) )







"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case : int     		=		{}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 snake_case : str     		=		["""NllbTokenizer"""]

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 snake_case : Dict     		=		["""NllbTokenizerFast"""]


if TYPE_CHECKING:
 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_nllb import NllbTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_nllb_fast import NllbTokenizerFast

else:
 import sys

 snake_case : Dict     		=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)