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





       """simple docstring"""




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

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

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






       def        UpperCamelCase				(							self      )    ->			Union[str, Any]:
          _SCREAMING_SNAKE_CASE      =				floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]      )

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

          _SCREAMING_SNAKE_CASE      =				self.get_config()

          return config, pixel_values, labels






       def        UpperCamelCase				(							self      )    ->			List[Any]:
          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        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__      )    ->			Union[str, Any]:
          _SCREAMING_SNAKE_CASE      =				VideoMAEModel(config=A__      )
          model.to(A__      )
          model.eval()
          _SCREAMING_SNAKE_CASE      =				model(A__      )
          self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, self.seq_length, self.hidden_size)      )






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

          _SCREAMING_SNAKE_CASE      =				model(A__       ,							A__      )
          # model only returns predictions for masked patches
          _SCREAMING_SNAKE_CASE      =				mask.sum().item()
          _SCREAMING_SNAKE_CASE      =				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      )    ->			Optional[int]:
          _SCREAMING_SNAKE_CASE      =				self.prepare_config_and_inputs()
          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				config_and_inputs
          _SCREAMING_SNAKE_CASE      =				{"""pixel_values""": pixel_values}
          return config, inputs_dict






@require_torch
class    _a				(_lowerCamelCase  ,					_lowerCamelCase  ,					unittest.TestCase):





       """simple docstring"""




       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      )    ->			Optional[int]:
          _SCREAMING_SNAKE_CASE      =				VideoMAEModelTester(self      )
          _SCREAMING_SNAKE_CASE      =				ConfigTester(self       ,							config_class=A__       ,							has_text_modality=A__       ,							hidden_size=37      )






       def        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__=False      )    ->			Optional[Any]:
          _SCREAMING_SNAKE_CASE      =				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
             _SCREAMING_SNAKE_CASE      =				torch.ones((self.model_tester.num_masks,)      )
             _SCREAMING_SNAKE_CASE      =				torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0      )      )]      )
             _SCREAMING_SNAKE_CASE      =				mask.expand(self.model_tester.batch_size       ,							-1      ).bool()
             _SCREAMING_SNAKE_CASE      =				bool_masked_pos.to(A__      )

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

          return inputs_dict






       def        UpperCamelCase				(							self      )    ->			int:
          self.config_tester.run_common_tests()






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






       def        UpperCamelCase				(							self      )    ->			List[Any]:
          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs_for_common()

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






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

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

             _SCREAMING_SNAKE_CASE      =				["""pixel_values"""]
             self.assertListEqual(arg_names[:1]       ,							A__      )






       def        UpperCamelCase				(							self      )    ->			Dict:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A__      )






       def        UpperCamelCase				(							self      )    ->			Any:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*A__      )






       @slow
       def        UpperCamelCase				(							self      )    ->			Optional[int]:
          for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _SCREAMING_SNAKE_CASE      =				VideoMAEModel.from_pretrained(A__      )
             self.assertIsNotNone(A__      )






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

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

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

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

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

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

                _SCREAMING_SNAKE_CASE      =				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        UpperCamelCase				(							self      )    ->			Dict:
          def check_hidden_states_output(A__       ,							A__       ,							A__      ):
             _SCREAMING_SNAKE_CASE      =				model_class(A__      )
             model.to(A__      )
             model.eval()

             with torch.no_grad():
                _SCREAMING_SNAKE_CASE      =				model(**self._prepare_for_class(A__       ,							A__      )      )

             _SCREAMING_SNAKE_CASE      =				outputs.hidden_states
             _SCREAMING_SNAKE_CASE      =				self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(A__      )       ,							A__      )

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

          _SCREAMING_SNAKE_CASE ,							_SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _SCREAMING_SNAKE_CASE      =				True
             check_hidden_states_output(A__       ,							A__       ,							A__      )

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






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




   """simple docstring"""
   _SCREAMING_SNAKE_CASE      =				hf_hub_download(
       repo_id="""hf-internal-testing/spaghetti-video"""    ,      filename="""eating_spaghetti.npy"""    ,      repo_type="""dataset"""						)
   _SCREAMING_SNAKE_CASE      =				np.load(SCREAMING_SNAKE_CASE_						)
   return list(SCREAMING_SNAKE_CASE_						)







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





       """simple docstring"""




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

          _SCREAMING_SNAKE_CASE      =				self.default_image_processor
          _SCREAMING_SNAKE_CASE      =				prepare_video()
          _SCREAMING_SNAKE_CASE      =				image_processor(A__       ,							return_tensors="""pt"""      ).to(A__      )

          # forward pass
          with torch.no_grad():
             _SCREAMING_SNAKE_CASE      =				model(**A__      )

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

          _SCREAMING_SNAKE_CASE      =				torch.tensor([0.3669, -0.0688, -0.2421]      ).to(A__      )

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






       @slow
       def        UpperCamelCase				(							self      )    ->			int:
          _SCREAMING_SNAKE_CASE      =				VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short"""      ).to(A__      )

          _SCREAMING_SNAKE_CASE      =				self.default_image_processor
          _SCREAMING_SNAKE_CASE      =				prepare_video()
          _SCREAMING_SNAKE_CASE      =				image_processor(A__       ,							return_tensors="""pt"""      ).to(A__      )

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

          # forward pass
          with torch.no_grad():
             _SCREAMING_SNAKE_CASE      =				model(**A__      )

          # verify the logits
          _SCREAMING_SNAKE_CASE      =				torch.Size([1, 14_08, 15_36]      )
          _SCREAMING_SNAKE_CASE      =				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`)
          _SCREAMING_SNAKE_CASE      =				torch.tensor([0.5142]       ,							device=A__      )
          self.assertTrue(torch.allclose(outputs.loss       ,							A__       ,							atol=1E-4      )      )

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

          with torch.no_grad():
             _SCREAMING_SNAKE_CASE      =				model(**A__      )

          _SCREAMING_SNAKE_CASE      =				torch.tensor(torch.tensor([0.6469]      )       ,							device=A__      )
          self.assertTrue(torch.allclose(outputs.loss       ,							A__       ,							atol=1E-4      )      )








'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCamelCase__      :		str       							=  {
    "configuration_canine": ["CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP", "CanineConfig"],
    "tokenization_canine": ["CanineTokenizer"],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase__      :		List[Any]       							=  [
         "CANINE_PRETRAINED_MODEL_ARCHIVE_LIST",
         "CanineForMultipleChoice",
         "CanineForQuestionAnswering",
         "CanineForSequenceClassification",
         "CanineForTokenClassification",
         "CanineLayer",
         "CanineModel",
         "CaninePreTrainedModel",
         "load_tf_weights_in_canine",
     ]


if TYPE_CHECKING:
     from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig
     from .tokenization_canine import CanineTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_canine import (
              CANINE_PRETRAINED_MODEL_ARCHIVE_LIST,
              CanineForMultipleChoice,
              CanineForQuestionAnswering,
              CanineForSequenceClassification,
              CanineForTokenClassification,
              CanineLayer,
              CanineModel,
              CaninePreTrainedModel,
              load_tf_weights_in_canine,
          )


else:
     import sys

     UpperCamelCase__      :		str       							=  _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






