


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





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






         """simple docstring"""







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

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

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





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






         """simple docstring"""







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

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

         UpperCAmelCase      					=  self.get_config()

         return config, pixel_values, labels





      def 							_lowercase					(		self    :       Dict							)     -> 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=__lowerCamelCase       ,							initializer_range=self.initializer_range       ,							)





      def 							_lowercase					(		self    :       Dict       ,							__lowerCamelCase    :       List[Any]       ,							__lowerCamelCase    :       List[Any]       ,							__lowerCamelCase    :       Optional[Any]							)     -> Any:






         """simple docstring"""







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





      def 							_lowercase					(		self    :       int       ,							__lowerCamelCase    :       Optional[Any]       ,							__lowerCamelCase    :       Any       ,							__lowerCamelCase    :       str							)     -> Dict:






         """simple docstring"""







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

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






         """simple docstring"""







         UpperCAmelCase      					=  self.prepare_config_and_inputs()
         UpperCAmelCase					,	UpperCAmelCase					,	UpperCAmelCase      					=  config_and_inputs
         UpperCAmelCase      					=  {"""pixel_values""": pixel_values}
         return config, inputs_dict






@require_torch
class 	__lowercase					(	__snake_case			,       __snake_case			,       unittest.TestCase	):
      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 							_lowercase					(		self    :       str							)     -> Optional[int]:






         """simple docstring"""







         UpperCAmelCase      					=  VideoMAEModelTester(self							)
         UpperCAmelCase      					=  ConfigTester(self       ,							config_class=__lowerCamelCase       ,							has_text_modality=__lowerCamelCase       ,							hidden_size=3_7							)





      def 							_lowercase					(		self    :       Optional[int]       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       Union[str, Any]=False							)     -> Dict:






         """simple docstring"""







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

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

         return inputs_dict





      def 							_lowercase					(		self    :       int							)     -> Optional[int]:






         """simple docstring"""







         self.config_tester.run_common_tests()





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






         """simple docstring"""







         pass





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






         """simple docstring"""







         UpperCAmelCase					,	UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs_for_common()

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





      def 							_lowercase					(		self    :       Optional[int]							)     -> Dict:






         """simple docstring"""







         UpperCAmelCase					,	UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs_for_common()

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

            UpperCAmelCase      					=  ["""pixel_values"""]
            self.assertListEqual(arg_names[:1]       ,							__lowerCamelCase							)





      def 							_lowercase					(		self    :       Optional[int]							)     -> List[str]:






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCamelCase							)





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






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase							)





      @slow
      def 							_lowercase					(		self    :       Optional[Any]							)     -> Union[str, Any]:






         """simple docstring"""







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





      def 							_lowercase					(		self    :       Optional[Any]							)     -> Tuple:






         """simple docstring"""







         if not self.has_attentions:
            pass

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

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

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

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

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

               UpperCAmelCase      					=  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 							_lowercase					(		self    :       Union[str, Any]							)     -> Dict:






         """simple docstring"""







         def check_hidden_states_output(__lowerCamelCase    :       List[Any]       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       int							):
            UpperCAmelCase      					=  model_class(__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()

            with torch.no_grad():
               UpperCAmelCase      					=  model(**self._prepare_for_class(__lowerCamelCase       ,							__lowerCamelCase							)							)

            UpperCAmelCase      					=  outputs.hidden_states
            UpperCAmelCase      					=  self.model_tester.num_hidden_layers + 1
            self.assertEqual(len(__lowerCamelCase							)       ,							__lowerCamelCase							)

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

         UpperCAmelCase					,	UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            UpperCAmelCase      					=  True
            check_hidden_states_output(__lowerCamelCase       ,							__lowerCamelCase       ,							__lowerCamelCase							)

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






         """simple docstring"""







         pass

def 	_UpperCamelCase					(    )    ->Optional[int]:
   UpperCAmelCase      					=  hf_hub_download(
       repo_id="""hf-internal-testing/spaghetti-video"""	,    filename="""eating_spaghetti.npy"""	,    repo_type="""dataset""" )
   UpperCAmelCase      					=  np.load(lowerCAmelCase_ )
   return list(lowerCAmelCase_ )







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





      @cached_property
      def 							_lowercase					(		self    :       Optional[int]							)     -> List[str]:






         """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 							_lowercase					(		self    :       Any							)     -> List[Any]:






         """simple docstring"""







         UpperCAmelCase      					=  VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics"""							).to(
             __lowerCamelCase							)

         UpperCAmelCase      					=  self.default_image_processor
         UpperCAmelCase      					=  prepare_video()
         UpperCAmelCase      					=  image_processor(__lowerCamelCase       ,							return_tensors="""pt"""							).to(__lowerCamelCase							)

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

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

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

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





      @slow
      def 							_lowercase					(		self    :       Dict							)     -> str:






         """simple docstring"""







         UpperCAmelCase      					=  VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short"""							).to(__lowerCamelCase							)

         UpperCAmelCase      					=  self.default_image_processor
         UpperCAmelCase      					=  prepare_video()
         UpperCAmelCase      					=  image_processor(__lowerCamelCase       ,							return_tensors="""pt"""							).to(__lowerCamelCase							)

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

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

         # verify the logits
         UpperCAmelCase      					=  torch.Size([1, 1_4_0_8, 1_5_3_6]							)
         UpperCAmelCase      					=  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=__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`)
         UpperCAmelCase      					=  torch.tensor([0.5_142]       ,							device=__lowerCamelCase							)
         self.assertTrue(torch.allclose(outputs.loss       ,							__lowerCamelCase       ,							atol=1e-4							)							)

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

         with torch.no_grad():
            UpperCAmelCase      					=  model(**__lowerCamelCase							)

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







from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from transformers import CLIPImageProcessor, CLIPVisionModel

from ...models import PriorTransformer
from ...pipelines import DiffusionPipeline
from ...schedulers import HeunDiscreteScheduler
from ...utils import (
    BaseOutput,
    is_accelerate_available,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .renderer import ShapERenderer


__a	 =      logging.get_logger(__name__)  # pylint: disable=invalid-name

__a	 =      """
    Examples:
        ```py
        >>> from PIL import Image
        >>> import torch
        >>> from diffusers import DiffusionPipeline
        >>> from diffusers.utils import export_to_gif, load_image

        >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")

        >>> repo = \"openai/shap-e-img2img\"
        >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)
        >>> pipe = pipe.to(device)

        >>> guidance_scale = 3.0
        >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"
        >>> image = load_image(image_url).convert(\"RGB\")

        >>> images = pipe(
        ...     image,
        ...     guidance_scale=guidance_scale,
        ...     num_inference_steps=64,
        ...     frame_size=256,
        ... ).images

        >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")
        ```
"""






@dataclass
class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							42






class 	__lowercase					(	__snake_case	):





      def __init__(		self    :       Any       ,							__lowerCamelCase    :       PriorTransformer       ,							__lowerCamelCase    :       CLIPVisionModel       ,							__lowerCamelCase    :       CLIPImageProcessor       ,							__lowerCamelCase    :       HeunDiscreteScheduler       ,							__lowerCamelCase    :       ShapERenderer       ,							)     -> str:






         """simple docstring"""







         super().__init__()

         self.register_modules(
             prior=__lowerCamelCase       ,							image_encoder=__lowerCamelCase       ,							image_processor=__lowerCamelCase       ,							scheduler=__lowerCamelCase       ,							renderer=__lowerCamelCase       ,							)





      def 							_lowercase					(		self    :       Dict       ,							__lowerCamelCase    :       Optional[int]       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Dict       ,							__lowerCamelCase    :       int       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       str							)     -> int:






         """simple docstring"""







         if latents is None:
            UpperCAmelCase      					=  randn_tensor(__lowerCamelCase       ,							generator=__lowerCamelCase       ,							device=__lowerCamelCase       ,							dtype=__lowerCamelCase							)
         else:
            if latents.shape != shape:
               raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}"""							)
            UpperCAmelCase      					=  latents.to(__lowerCamelCase							)

         UpperCAmelCase      					=  latents * scheduler.init_noise_sigma
         return latents





      def 							_lowercase					(		self    :       Optional[Any]       ,							__lowerCamelCase    :       Dict=0							)     -> Optional[int]:






         """simple docstring"""







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

         UpperCAmelCase      					=  torch.device(F"""cuda:{gpu_id}"""							)

         UpperCAmelCase      					=  [self.image_encoder, self.prior]
         for cpu_offloaded_model in models:
            if cpu_offloaded_model is not None:
               cpu_offload(__lowerCamelCase       ,							__lowerCamelCase							)





      @property
      def 							_lowercase					(		self    :       List[str]							)     -> int:






         """simple docstring"""







         if self.device != torch.device("""meta"""							) or not hasattr(self.image_encoder       ,							"""_hf_hook"""							):
            return self.device
         for module in self.image_encoder.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





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






         """simple docstring"""







         if isinstance(__lowerCamelCase       ,							__lowerCamelCase							) and isinstance(image[0]       ,							torch.Tensor							):
            UpperCAmelCase      					=  torch.cat(__lowerCamelCase       ,							axis=0							) if image[0].ndim == 4 else torch.stack(__lowerCamelCase       ,							axis=0							)

         if not isinstance(__lowerCamelCase       ,							torch.Tensor							):
            UpperCAmelCase      					=  self.image_processor(__lowerCamelCase       ,							return_tensors="""pt"""							).pixel_values[0].unsqueeze(0							)

         UpperCAmelCase      					=  image.to(dtype=self.image_encoder.dtype       ,							device=__lowerCamelCase							)

         UpperCAmelCase      					=  self.image_encoder(__lowerCamelCase							)["""last_hidden_state"""]
         UpperCAmelCase      					=  image_embeds[:, 1:, :].contiguous()  # batch_size, dim, 256

         UpperCAmelCase      					=  image_embeds.repeat_interleave(__lowerCamelCase       ,							dim=0							)

         if do_classifier_free_guidance:
            UpperCAmelCase      					=  torch.zeros_like(__lowerCamelCase							)

            # For classifier free guidance, we need to do two forward passes.
            # Here we concatenate the unconditional and text embeddings into a single batch
            # to avoid doing two forward passes
            UpperCAmelCase      					=  torch.cat([negative_image_embeds, image_embeds]							)

         return image_embeds





      @torch.no_grad()
      @replace_example_docstring(__lowerCamelCase							)
      def __call__(		self    :       str       ,							__lowerCamelCase    :       Union[PIL.Image.Image, List[PIL.Image.Image]]       ,							__lowerCamelCase    :       int = 1       ,							__lowerCamelCase    :       int = 2_5       ,							__lowerCamelCase    :       Optional[Union[torch.Generator, List[torch.Generator]]] = None       ,							__lowerCamelCase    :       Optional[torch.FloatTensor] = None       ,							__lowerCamelCase    :       float = 4.0       ,							__lowerCamelCase    :       int = 6_4       ,							__lowerCamelCase    :       Optional[str] = "pil"       ,							__lowerCamelCase    :       bool = True       ,							)     -> Tuple:






         """simple docstring"""







         if isinstance(__lowerCamelCase       ,							PIL.Image.Image							):
            UpperCAmelCase      					=  1
         elif isinstance(__lowerCamelCase       ,							torch.Tensor							):
            UpperCAmelCase      					=  image.shape[0]
         elif isinstance(__lowerCamelCase       ,							__lowerCamelCase							) and isinstance(image[0]       ,							(torch.Tensor, PIL.Image.Image)							):
            UpperCAmelCase      					=  len(__lowerCamelCase							)
         else:
            raise ValueError(
                F"""`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(__lowerCamelCase							)}"""							)

         UpperCAmelCase      					=  self._execution_device

         UpperCAmelCase      					=  batch_size * num_images_per_prompt

         UpperCAmelCase      					=  guidance_scale > 1.0
         UpperCAmelCase      					=  self._encode_image(__lowerCamelCase       ,							__lowerCamelCase       ,							__lowerCamelCase       ,							__lowerCamelCase							)

         # prior

         self.scheduler.set_timesteps(__lowerCamelCase       ,							device=__lowerCamelCase							)
         UpperCAmelCase      					=  self.scheduler.timesteps

         UpperCAmelCase      					=  self.prior.config.num_embeddings
         UpperCAmelCase      					=  self.prior.config.embedding_dim

         UpperCAmelCase      					=  self.prepare_latents(
             (batch_size, num_embeddings * embedding_dim)       ,							image_embeds.dtype       ,							__lowerCamelCase       ,							__lowerCamelCase       ,							__lowerCamelCase       ,							self.scheduler       ,							)

         # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim
         UpperCAmelCase      					=  latents.reshape(latents.shape[0]       ,							__lowerCamelCase       ,							__lowerCamelCase							)

         for i, t in enumerate(self.progress_bar(__lowerCamelCase							)							):
            # expand the latents if we are doing classifier free guidance
            UpperCAmelCase      					=  torch.cat([latents] * 2							) if do_classifier_free_guidance else latents
            UpperCAmelCase      					=  self.scheduler.scale_model_input(__lowerCamelCase       ,							__lowerCamelCase							)

            UpperCAmelCase      					=  self.prior(
                __lowerCamelCase       ,							timestep=__lowerCamelCase       ,							proj_embedding=__lowerCamelCase       ,							).predicted_image_embedding

            # remove the variance
            UpperCAmelCase					,	UpperCAmelCase      					=  noise_pred.split(
                scaled_model_input.shape[2]       ,							dim=2							)  # batch_size, num_embeddings, embedding_dim

            if do_classifier_free_guidance is not None:
               UpperCAmelCase					,	UpperCAmelCase      					=  noise_pred.chunk(2							)
               UpperCAmelCase      					=  noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)

            UpperCAmelCase      					=  self.scheduler.step(
                __lowerCamelCase       ,							timestep=__lowerCamelCase       ,							sample=__lowerCamelCase       ,							).prev_sample

         if output_type == "latent":
            return ShapEPipelineOutput(images=__lowerCamelCase							)

         UpperCAmelCase      					=  []
         for i, latent in enumerate(__lowerCamelCase							):
            print()
            UpperCAmelCase      					=  self.renderer.decode(
                latent[None, :]       ,							__lowerCamelCase       ,							size=__lowerCamelCase       ,							ray_batch_size=4_0_9_6       ,							n_coarse_samples=6_4       ,							n_fine_samples=1_2_8       ,							)

            images.append(__lowerCamelCase							)

         UpperCAmelCase      					=  torch.stack(__lowerCamelCase							)

         if output_type not in ["np", "pil"]:
            raise ValueError(F"""Only the output types `pil` and `np` are supported not output_type={output_type}"""							)

         UpperCAmelCase      					=  images.cpu().numpy()

         if output_type == "pil":
            UpperCAmelCase      					=  [self.numpy_to_pil(__lowerCamelCase							) for image in images]

         # Offload last model to CPU
         if hasattr(self       ,							"""final_offload_hook"""							) and self.final_offload_hook is not None:
            self.final_offload_hook.offload()

         if not return_dict:
            return (images,)

         return ShapEPipelineOutput(images=__lowerCamelCase							)



