




from typing import Callable, List, Optional, Union

import PIL
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPSegForImageSegmentation,
    CLIPSegProcessor,
    CLIPTextModel,
    CLIPTokenizer,
)

from diffusers import DiffusionPipeline
from diffusers.configuration_utils import FrozenDict
from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion import StableDiffusionInpaintPipeline
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
from diffusers.utils import deprecate, is_accelerate_available, logging


_snake_case				:				List[Any]       							=		logging.get_logger(__name__)  # pylint: disable=invalid-name

class       A   (		_a   ):







							def __init__(  self	:  Dict							,      lowerCAmelCase_	:  CLIPSegForImageSegmentation							,      lowerCAmelCase_	:  CLIPSegProcessor							,      lowerCAmelCase_	:  AutoencoderKL							,      lowerCAmelCase_	:  CLIPTextModel							,      lowerCAmelCase_	:  CLIPTokenizer							,      lowerCAmelCase_	:  UNetaDConditionModel							,      lowerCAmelCase_	:  Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]							,      lowerCAmelCase_	:  StableDiffusionSafetyChecker							,      lowerCAmelCase_	:  CLIPImageProcessor							,      )      ->							int:


														"""simple docstring"""


														super().__init__()

														if hasattr(scheduler.config							,      '''steps_offset''' ) and scheduler.config.steps_offset != 1:
																					_a     					=    (
																					    F'The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`'
																					    F' should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure '
																					    '''to update the config accordingly as leaving `steps_offset` might led to incorrect results'''
																					    ''' in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,'''
																					    ''' it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`'''
																					    ''' file'''
																					)
																					deprecate('''steps_offset!=1'''							,      '''1.0.0'''							,      lowerCAmelCase_							,      standard_warn=lowerCAmelCase_ )
																					_a     					=    dict(scheduler.config )
																					_a     					=    1
																					_a     					=    FrozenDict(lowerCAmelCase_ )

														if hasattr(scheduler.config							,      '''skip_prk_steps''' ) and scheduler.config.skip_prk_steps is False:
																					_a     					=    (
																					    F'The configuration file of this scheduler: {scheduler} has not set the configuration'
																					    ''' `skip_prk_steps`. `skip_prk_steps` should be set to True in the configuration file. Please make'''
																					    ''' sure to update the config accordingly as not setting `skip_prk_steps` in the config might lead to'''
																					    ''' incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face'''
																					    ''' Hub, it would be very nice if you could open a Pull request for the'''
																					    ''' `scheduler/scheduler_config.json` file'''
																					)
																					deprecate('''skip_prk_steps not set'''							,      '''1.0.0'''							,      lowerCAmelCase_							,      standard_warn=lowerCAmelCase_ )
																					_a     					=    dict(scheduler.config )
																					_a     					=    True
																					_a     					=    FrozenDict(lowerCAmelCase_ )

														if safety_checker is None:
																					logger.warning(
																					    F'You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure'
																					    ''' that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered'''
																					    ''' results in services or applications open to the public. Both the diffusers team and Hugging Face'''
																					    ''' strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling'''
																					    ''' it only for use-cases that involve analyzing network behavior or auditing its results. For more'''
																					    ''' information, please have a look at https://github.com/huggingface/diffusers/pull/254 .''' )

														self.register_modules(
														    segmentation_model=lowerCAmelCase_							,      segmentation_processor=lowerCAmelCase_							,      vae=lowerCAmelCase_							,      text_encoder=lowerCAmelCase_							,      tokenizer=lowerCAmelCase_							,      unet=lowerCAmelCase_							,      scheduler=lowerCAmelCase_							,      safety_checker=lowerCAmelCase_							,      feature_extractor=lowerCAmelCase_							,      )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  Optional[Union[str, int]] = "auto" )      ->							Any:


														"""simple docstring"""


														if slice_size == "auto":
																					# half the attention head size is usually a good trade-off between
																					# speed and memory
																					_a     					=    self.unet.config.attention_head_dim // 2
														self.unet.set_attention_slice(lowerCAmelCase_ )







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


														"""simple docstring"""


														self.enable_attention_slicing(lowerCAmelCase_ )







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


														"""simple docstring"""


														if is_accelerate_available():
																					from accelerate import cpu_offload
														else:
																					raise ImportError('''Please install accelerate via `pip install accelerate`''' )

														_a     					=    torch.device('''cuda''' )

														for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae, self.safety_checker]:
																					if cpu_offloaded_model is not None:
																												cpu_offload(lowerCAmelCase_							,      lowerCAmelCase_ )







							@property
							# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
							def        __lowerCAmelCase      (  self	:  Dict )      ->							List[str]:


														"""simple docstring"""


														if self.device != torch.device('''meta''' ) or not hasattr(self.unet							,      '''_hf_hook''' ):
																					return self.device
														for module in self.unet.modules():
																					if (
																					    hasattr(lowerCAmelCase_							,      '''_hf_hook''' )
																					    and hasattr(module._hf_hook							,      '''execution_device''' )
																					    and module._hf_hook.execution_device is not None
																					):
																												return torch.device(module._hf_hook.execution_device )
														return self.device







							@torch.no_grad()
							def __call__(  self	:  List[Any]							,      lowerCAmelCase_	:  Union[str, List[str]]							,      lowerCAmelCase_	:  Union[torch.FloatTensor, PIL.Image.Image]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  int = 5_12							,      lowerCAmelCase_	:  int = 5_12							,      lowerCAmelCase_	:  int = 50							,      lowerCAmelCase_	:  float = 7.5							,      lowerCAmelCase_	:  Optional[Union[str, List[str]]] = None							,      lowerCAmelCase_	:  Optional[int] = 1							,      lowerCAmelCase_	:  float = 0.0							,      lowerCAmelCase_	:  Optional[torch.Generator] = None							,      lowerCAmelCase_	:  Optional[torch.FloatTensor] = None							,      lowerCAmelCase_	:  Optional[str] = "pil"							,      lowerCAmelCase_	:  bool = True							,      lowerCAmelCase_	:  Optional[Callable[[int, int, torch.FloatTensor], None]] = None							,      lowerCAmelCase_	:  int = 1							,      **lowerCAmelCase_	:  Any							,      )      ->							Tuple:


														"""simple docstring"""


														_a     					=    self.segmentation_processor(
														    text=[text]							,      images=[image]							,      padding='''max_length'''							,      return_tensors='''pt''' ).to(self.device )
														_a     					=    self.segmentation_model(**lowerCAmelCase_ )
														_a     					=    torch.sigmoid(outputs.logits ).cpu().detach().unsqueeze(-1 ).numpy()
														_a     					=    self.numpy_to_pil(lowerCAmelCase_ )[0].resize(image.size )

														# Run inpainting pipeline with the generated mask
														_a     					=    StableDiffusionInpaintPipeline(
														    vae=self.vae							,      text_encoder=self.text_encoder							,      tokenizer=self.tokenizer							,      unet=self.unet							,      scheduler=self.scheduler							,      safety_checker=self.safety_checker							,      feature_extractor=self.feature_extractor							,      )
														return inpainting_pipeline(
														    prompt=lowerCAmelCase_							,      image=lowerCAmelCase_							,      mask_image=lowerCAmelCase_							,      height=lowerCAmelCase_							,      width=lowerCAmelCase_							,      num_inference_steps=lowerCAmelCase_							,      guidance_scale=lowerCAmelCase_							,      negative_prompt=lowerCAmelCase_							,      num_images_per_prompt=lowerCAmelCase_							,      eta=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      latents=lowerCAmelCase_							,      output_type=lowerCAmelCase_							,      return_dict=lowerCAmelCase_							,      callback=lowerCAmelCase_							,      callback_steps=lowerCAmelCase_							,      )







'''simple docstring'''


import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import (
				    MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
				    VideoMAEForPreTraining,
				    VideoMAEForVideoClassification,
				    VideoMAEModel,
				)
				from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from transformers import VideoMAEImageProcessor

class       A   :







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


														"""simple docstring"""


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

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

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







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


														"""simple docstring"""


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

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

														_a     					=    self.get_config()

														return config, pixel_values, labels







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


														"""simple docstring"""


														return VideoMAEConfig(
														    image_size=self.image_size							,      patch_size=self.patch_size							,      num_channels=self.num_channels							,      num_frames=self.num_frames							,      tubelet_size=self.tubelet_size							,      hidden_size=self.hidden_size							,      num_hidden_layers=self.num_hidden_layers							,      num_attention_heads=self.num_attention_heads							,      intermediate_size=self.intermediate_size							,      hidden_act=self.hidden_act							,      hidden_dropout_prob=self.hidden_dropout_prob							,      attention_probs_dropout_prob=self.attention_probs_dropout_prob							,      is_decoder=lowerCAmelCase_							,      initializer_range=self.initializer_range							,      )







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


														"""simple docstring"""


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







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


														"""simple docstring"""


														_a     					=    VideoMAEForPreTraining(lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()
														# important: each video needs to have the same number of masked patches
														# hence we define a single mask, which we then repeat for each example in the batch
														_a     					=    torch.ones((self.num_masks,) )
														_a     					=    torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] )
														_a     					=    mask.expand(self.batch_size							,      -1 ).bool()

														_a     					=    model(lowerCAmelCase_							,      lowerCAmelCase_ )
														# model only returns predictions for masked patches
														_a     					=    mask.sum().item()
														_a     					=    3 * self.tubelet_size * self.patch_size**2
														self.parent.assertEqual(result.logits.shape							,      (self.batch_size, num_masked_patches, decoder_num_labels) )







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


														"""simple docstring"""


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



@require_torch
class       A   (		_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (
							    (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
							)
							lowercase_          =    (
							    {'feature-extraction': VideoMAEModel, 'video-classification': VideoMAEForVideoClassification}
							    if is_torch_available()
							    else {}
							)

							lowercase_          =    False
							lowercase_          =    False
							lowercase_          =    False
							lowercase_          =    False







							def        __lowerCAmelCase      (  self	:  Dict )      ->							int:


														"""simple docstring"""


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







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


														"""simple docstring"""


														_a     					=    copy.deepcopy(lowerCAmelCase_ )

														if model_class == VideoMAEForPreTraining:
																					# important: each video needs to have the same number of masked patches
																					# hence we define a single mask, which we then repeat for each example in the batch
																					_a     					=    torch.ones((self.model_tester.num_masks,) )
																					_a     					=    torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] )
																					_a     					=    mask.expand(self.model_tester.batch_size							,      -1 ).bool()
																					_a     					=    bool_masked_pos.to(lowerCAmelCase_ )

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

														return inputs_dict







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


														"""simple docstring"""


														self.config_tester.run_common_tests()







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


														"""simple docstring"""


														pass







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


														"""simple docstring"""


														_a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

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







							def        __lowerCAmelCase      (  self	:  Any )      ->							Any:


														"""simple docstring"""


														_a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

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

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







							def        __lowerCAmelCase      (  self	:  int )      ->							Any:


														"""simple docstring"""


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







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


														"""simple docstring"""


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







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


														"""simple docstring"""


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







							def        __lowerCAmelCase      (  self	:  int )      ->							Any:


														"""simple docstring"""


														if not self.has_attentions:
																					pass

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

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

																												_a     					=    True
																												_a     					=    False
																												_a     					=    True
																												_a     					=    model_class(lowerCAmelCase_ )
																												model.to(lowerCAmelCase_ )
																												model.eval()
																												with torch.no_grad():
																																			_a     					=    model(**self._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_ ) )
																												_a     					=    outputs.attentions
																												self.assertEqual(len(lowerCAmelCase_ )							,      self.model_tester.num_hidden_layers )

																												# check that output_attentions also work using config
																												del inputs_dict["output_attentions"]
																												_a     					=    True
																												_a     					=    model_class(lowerCAmelCase_ )
																												model.to(lowerCAmelCase_ )
																												model.eval()
																												with torch.no_grad():
																																			_a     					=    model(**self._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_ ) )
																												_a     					=    outputs.attentions
																												self.assertEqual(len(lowerCAmelCase_ )							,      self.model_tester.num_hidden_layers )

																												self.assertListEqual(
																												    list(attentions[0].shape[-3:] )							,      [self.model_tester.num_attention_heads, seq_len, seq_len]							,      )
																												_a     					=    len(lowerCAmelCase_ )

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

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

																												_a     					=    outputs.attentions

																												self.assertEqual(len(lowerCAmelCase_ )							,      self.model_tester.num_hidden_layers )
																												self.assertListEqual(
																												    list(self_attentions[0].shape[-3:] )							,      [self.model_tester.num_attention_heads, seq_len, seq_len]							,      )







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Tuple:


														"""simple docstring"""


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

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

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

																					_a     					=    self.model_tester.seq_length - self.model_tester.num_masks
																					_a     					=    num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

																					self.assertListEqual(
																					    list(hidden_states[0].shape[-2:] )							,      [seq_length, self.model_tester.hidden_size]							,      )

														_a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

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

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

																					check_hidden_states_output(lowerCAmelCase_							,      lowerCAmelCase_							,      lowerCAmelCase_ )







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


														"""simple docstring"""


														pass



def 		snake_case_   ():





							'''simple docstring'''


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

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







							@cached_property
							def        __lowerCAmelCase      (  self	:  str )      ->							List[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        __lowerCAmelCase      (  self	:  Dict )      ->							Dict:


														"""simple docstring"""


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

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

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

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

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

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







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


														"""simple docstring"""


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

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

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

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

														# verify the logits
														_a     					=    torch.Size([1, 14_08, 15_36] )
														_a     					=    torch.tensor(
														    [[0.7_9_9_4, 0.9_6_1_2, 0.8_5_0_8], [0.7_4_0_1, 0.8_9_5_8, 0.8_3_0_2], [0.5_8_6_2, 0.7_4_6_8, 0.7_3_2_5]]							,      device=lowerCAmelCase_ )
														self.assertEqual(outputs.logits.shape							,      lowerCAmelCase_ )
														self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]							,      lowerCAmelCase_							,      atol=1e-4 ) )

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

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

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

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

