



'''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    lowerCamelCase_       :
  """simple docstring"""




  def __init__(					self :      Union[str, Any]     ,      _a :      Any     ,      _a :      Union[str, Any]=13     ,      _a :      Union[str, Any]=10     ,      _a :      Union[str, Any]=3     ,      _a :      Optional[int]=2     ,      _a :      int=2     ,      _a :      Tuple=2     ,      _a :      Union[str, Any]=True     ,      _a :      Any=True     ,      _a :      Optional[Any]=32     ,      _a :      int=5     ,      _a :      Optional[int]=4     ,      _a :      Optional[int]=37     ,      _a :      List[Any]="gelu"     ,      _a :      List[Any]=0.1     ,      _a :      Dict=0.1     ,      _a :      str=10     ,      _a :      Any=0.02     ,      _a :      Optional[int]=0.9     ,      _a :      Tuple=None     ,      )   ->   str:
    __lowerCamelCase			:			Tuple       			=					parent
    __lowerCamelCase			:			Any       			=					batch_size
    __lowerCamelCase			:			str       			=					image_size
    __lowerCamelCase			:			Optional[Any]       			=					num_channels
    __lowerCamelCase			:			Union[str, Any]       			=					patch_size
    __lowerCamelCase			:			Optional[Any]       			=					tubelet_size
    __lowerCamelCase			:			Optional[Any]       			=					num_frames
    __lowerCamelCase			:			int       			=					is_training
    __lowerCamelCase			:			Tuple       			=					use_labels
    __lowerCamelCase			:			Union[str, Any]       			=					hidden_size
    __lowerCamelCase			:			Optional[int]       			=					num_hidden_layers
    __lowerCamelCase			:			Dict       			=					num_attention_heads
    __lowerCamelCase			:			Optional[int]       			=					intermediate_size
    __lowerCamelCase			:			Dict       			=					hidden_act
    __lowerCamelCase			:			Tuple       			=					hidden_dropout_prob
    __lowerCamelCase			:			Union[str, Any]       			=					attention_probs_dropout_prob
    __lowerCamelCase			:			str       			=					type_sequence_label_size
    __lowerCamelCase			:			Any       			=					initializer_range
    __lowerCamelCase			:			Union[str, Any]       			=					mask_ratio
    __lowerCamelCase			:			Tuple       			=					scope

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

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




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

    __lowerCamelCase			:			List[Any]       			=					None
    if self.use_labels:
      __lowerCamelCase			:			Optional[int]       			=					ids_tensor([self.batch_size]     ,      self.type_sequence_label_size  )

    __lowerCamelCase			:			int       			=					self.get_config()

    return config, pixel_values, labels




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

    __lowerCamelCase			:			Dict       			=					model(_a     ,      _a  )
    # model only returns predictions for masked patches
    __lowerCamelCase			:			Tuple       			=					mask.sum().item()
    __lowerCamelCase			:			Union[str, Any]       			=					3 * self.tubelet_size * self.patch_size**2
    self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, num_masked_patches, decoder_num_labels)  )




  def 		_lowercase	(					self :      Optional[Any]  )   ->   Dict:
    __lowerCamelCase			:			Any       			=					self.prepare_config_and_inputs()
    __lowerCamelCase			,__lowerCamelCase			,__lowerCamelCase			:			str       			=					config_and_inputs
    __lowerCamelCase			:			Dict       			=					{'pixel_values': pixel_values}
    return config, inputs_dict






@require_torch
class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_  ,  SCREAMING_SNAKE_CASE_  ,  unittest.TestCase	):
  """simple docstring"""




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

  a_				   =False
  a_				   =False
  a_				   =False
  a_				   =False




  def 		_lowercase	(					self :      Union[str, Any]  )   ->   Optional[int]:
    __lowerCamelCase			:			Union[str, Any]       			=					VideoMAEModelTester(self  )
    __lowerCamelCase			:			Union[str, Any]       			=					ConfigTester(self     ,      config_class=_a     ,      has_text_modality=_a     ,      hidden_size=37  )




  def 		_lowercase	(					self :      Any     ,      _a :      str     ,      _a :      int     ,      _a :      Dict=False  )   ->   Optional[int]:
    __lowerCamelCase			:			List[Any]       			=					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			:			str       			=					torch.ones((self.model_tester.num_masks,)  )
      __lowerCamelCase			:			Tuple       			=					torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0  )  )]  )
      __lowerCamelCase			:			Any       			=					mask.expand(self.model_tester.batch_size     ,      -1  ).bool()
      __lowerCamelCase			:			Optional[int]       			=					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 		_lowercase	(					self :      int  )   ->   Optional[Any]:
    self.config_tester.run_common_tests()




  @unittest.skip(reason='VideoMAE does not use inputs_embeds'  )
  def 		_lowercase	(					self :      int  )   ->   str:
    pass




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

    for model_class in self.all_model_classes:
      __lowerCamelCase			:			Optional[int]       			=					model_class(_a  )
      self.assertIsInstance(model.get_input_embeddings()     ,      (nn.Module)  )
      __lowerCamelCase			:			Optional[Any]       			=					model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(_a     ,      nn.Linear  )  )




  def 		_lowercase	(					self :      Dict  )   ->   Dict:
    __lowerCamelCase			,__lowerCamelCase			:			Optional[int]       			=					self.model_tester.prepare_config_and_inputs_for_common()

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

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




  def 		_lowercase	(					self :      str  )   ->   Union[str, Any]:
    __lowerCamelCase			:			Tuple       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_a  )




  def 		_lowercase	(					self :      Optional[int]  )   ->   Union[str, Any]:
    __lowerCamelCase			:			int       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_pretraining(*_a  )




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




  def 		_lowercase	(					self :      int  )   ->   Any:
    if not self.has_attentions:
      pass

    else:
      __lowerCamelCase			,__lowerCamelCase			:			Optional[Any]       			=					self.model_tester.prepare_config_and_inputs_for_common()
      __lowerCamelCase			:			int       			=					True

      for model_class in self.all_model_classes:
        __lowerCamelCase			:			Optional[int]       			=					self.model_tester.seq_length - self.model_tester.num_masks
        __lowerCamelCase			:			Any       			=					(
            num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
        )

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

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

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

        __lowerCamelCase			:			int       			=					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 		_lowercase	(					self :      Tuple  )   ->   str:
    def check_hidden_states_output(_a :      List[Any]     ,      _a :      Union[str, Any]     ,      _a :      Optional[int]  ):
      __lowerCamelCase			:			Optional[Any]       			=					model_class(_a  )
      model.to(_a  )
      model.eval()

      with torch.no_grad():
        __lowerCamelCase			:			Union[str, Any]       			=					model(**self._prepare_for_class(_a     ,      _a  )  )

      __lowerCamelCase			:			Optional[Any]       			=					outputs.hidden_states
      __lowerCamelCase			:			Union[str, Any]       			=					self.model_tester.num_hidden_layers + 1
      self.assertEqual(len(_a  )     ,      _a  )

      __lowerCamelCase			:			str       			=					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			:			Tuple       			=					self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __lowerCamelCase			:			Optional[int]       			=					True
      check_hidden_states_output(_a     ,      _a     ,      _a  )

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




def 			a_	(				) -> Tuple:
  __lowerCamelCase			:			List[str]       			=					hf_hub_download(
      repo_id='hf-internal-testing/spaghetti-video'			,filename='eating_spaghetti.npy'			,repo_type='dataset'			)
  __lowerCamelCase			:			List[Any]       			=					np.load(_lowerCAmelCase			)
  return list(_lowerCAmelCase			)






@require_torch
@require_vision
class    lowerCamelCase_       (							unittest.TestCase	):
  """simple docstring"""




  @cached_property
  def 		_lowercase	(					self :      Tuple  )   ->   List[Any]:
    # 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 		_lowercase	(					self :      Tuple  )   ->   Dict:
    __lowerCamelCase			:			Union[str, Any]       			=					VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics'  ).to(
        _a  )

    __lowerCamelCase			:			Optional[Any]       			=					self.default_image_processor
    __lowerCamelCase			:			Any       			=					prepare_video()
    __lowerCamelCase			:			str       			=					image_processor(_a     ,      return_tensors='pt'  ).to(_a  )

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

    # verify the logits
    __lowerCamelCase			:			int       			=					torch.Size((1, 400)  )
    self.assertEqual(outputs.logits.shape     ,      _a  )

    __lowerCamelCase			:			Optional[int]       			=					torch.tensor([0.3669, -0.0688, -0.2421]  ).to(_a  )

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




  @slow
  def 		_lowercase	(					self :      List[str]  )   ->   Any:
    __lowerCamelCase			:			Optional[int]       			=					VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'  ).to(_a  )

    __lowerCamelCase			:			Optional[Any]       			=					self.default_image_processor
    __lowerCamelCase			:			int       			=					prepare_video()
    __lowerCamelCase			:			Any       			=					image_processor(_a     ,      return_tensors='pt'  ).to(_a  )

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

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

    # verify the logits
    __lowerCamelCase			:			Union[str, Any]       			=					torch.Size([1, 1408, 1536]  )
    __lowerCamelCase			:			int       			=					torch.tensor(
        [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]     ,      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			:			str       			=					torch.tensor([0.5142]     ,      device=_a  )
    self.assertTrue(torch.allclose(outputs.loss     ,      _a     ,      atol=1e-4  )  )

    # verify the loss (`config.norm_pix_loss` = `False`)
    __lowerCamelCase			:			Optional[int]       			=					VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short'     ,      norm_pix_loss=_a  ).to(
        _a  )

    with torch.no_grad():
      __lowerCamelCase			:			Union[str, Any]       			=					model(**_a  )

    __lowerCamelCase			:			List[Any]       			=					torch.tensor(torch.tensor([0.6469]  )     ,      device=_a  )
    self.assertTrue(torch.allclose(outputs.loss     ,      _a     ,      atol=1e-4  )  )











'''simple docstring'''





from __future__ import annotations

_UpperCamelCase											=		10

def 			a_	(				_lowerCAmelCase			) -> list[int]:
  __lowerCamelCase			:			str       			=					1
  __lowerCamelCase			:			Union[str, Any]       			=					max(_lowerCAmelCase			)
  while placement <= max_digit:
    # declare and initialize empty buckets
    __lowerCamelCase			:			list[list]       			=					[[] for _ in range(_lowerCAmelCase			)]
    # split list_of_ints between the buckets
    for i in list_of_ints:
      __lowerCamelCase			:			List[str]       			=					int((i / placement) % RADIX			)
      buckets[tmp].append(_lowerCAmelCase			)
    # put each buckets' contents into list_of_ints
    __lowerCamelCase			:			Dict       			=					0
    for b in range(_lowerCAmelCase			):
      for i in buckets[b]:
        __lowerCamelCase			:			List[str]       			=					i
        a += 1
        # move to next
    placement *= RADIX
  return list_of_ints


if __name__ == "__main__":
   import doctest

   doctest.testmod()






