




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

import numpy as np
import PIL
import torch
from transformers import CLIPTextModelWithProjection, CLIPTokenizer

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


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

SCREAMING_SNAKE_CASE__ :   List[str]						=     '\n    Examples:\n        ```py\n        >>> import torch\n        >>> from diffusers import DiffusionPipeline\n        >>> from diffusers.utils import export_to_gif\n\n        >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")\n\n        >>> repo = "openai/shap-e"\n        >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n        >>> pipe = pipe.to(device)\n\n        >>> guidance_scale = 15.0\n        >>> prompt = "a shark"\n\n        >>> images = pipe(\n        ...     prompt,\n        ...     guidance_scale=guidance_scale,\n        ...     num_inference_steps=64,\n        ...     frame_size=256,\n        ... ).images\n\n        >>> gif_path = export_to_gif(images[0], "shark_3d.gif")\n        ```\n'




@dataclass
class 	UpperCamelCase__			(A_  ):

    '''simple docstring'''



    lowerCamelCase_   :			Tuple			  =   4_2




class 	UpperCamelCase__			(A_  ):

    '''simple docstring'''



    def __init__(					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						)  ->   List[Any]:
           super().__init__()

           self.register_modules(
               prior=UpperCamelCase__  ,						text_encoder=UpperCamelCase__  ,						tokenizer=UpperCamelCase__  ,						scheduler=UpperCamelCase__  ,						renderer=UpperCamelCase__  ,						)







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )  ->   List[str]:



           if latents is None:
                  lowerCamelCase     :   int    				=      randn_tensor(UpperCamelCase__  ,						generator=UpperCamelCase__  ,						device=UpperCamelCase__  ,						dtype=UpperCamelCase__  )
           else:
                  if latents.shape != shape:
                         raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}'''  )
                  lowerCamelCase     :   Optional[int]    				=      latents.to(UpperCamelCase__  )

           lowerCamelCase     :   str    				=      latents * scheduler.init_noise_sigma
           return latents







    def 					_lowercase  (					self  ,						UpperCamelCase__=0  )  ->   Tuple:



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

           lowerCamelCase     :   Union[str, Any]    				=      torch.device(F'''cuda:{gpu_id}'''  )

           lowerCamelCase     :   Tuple    				=      [self.text_encoder, self.prior]
           for cpu_offloaded_model in models:
                  if cpu_offloaded_model is not None:
                         cpu_offload(UpperCamelCase__  ,						UpperCamelCase__  )







    def 					_lowercase  (					self  ,						UpperCamelCase__=0  )  ->   Tuple:



           if is_accelerate_available() and is_accelerate_version(">="  ,						"0.17.0.dev0"  ):
                  from accelerate import cpu_offload_with_hook
           else:
                  raise ImportError("`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."  )

           lowerCamelCase     :   List[Any]    				=      torch.device(F'''cuda:{gpu_id}'''  )

           if self.device.type != "cpu":
                  self.to("cpu"  ,						silence_dtype_warnings=UpperCamelCase__  )
                  torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

           lowerCamelCase     :   Any    				=      None
           for cpu_offloaded_model in [self.text_encoder, self.prior, self.renderer]:
                  lowerCamelCase     :   Tuple    				=      cpu_offload_with_hook(UpperCamelCase__  ,						UpperCamelCase__  ,						prev_module_hook=UpperCamelCase__  )

           if self.safety_checker is not None:
                  lowerCamelCase     :   Dict    				=      cpu_offload_with_hook(self.safety_checker  ,						UpperCamelCase__  ,						prev_module_hook=UpperCamelCase__  )

           # We'll offload the last model manually.
           lowerCamelCase     :   str    				=      hook







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



           if self.device != torch.device("meta"  ) or not hasattr(self.text_encoder  ,						"_hf_hook"  ):
                  return self.device
           for module in self.text_encoder.modules():
                  if (
                      hasattr(UpperCamelCase__  ,						"_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  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						)  ->   Dict:
           len(UpperCamelCase__  ) if isinstance(UpperCamelCase__  ,						UpperCamelCase__  ) else 1

           # YiYi Notes: set pad_token_id to be 0, not sure why I can't set in the config file
           lowerCamelCase     :   Dict    				=      0
           # get prompt text embeddings
           lowerCamelCase     :   Optional[int]    				=      self.tokenizer(
               UpperCamelCase__  ,						padding="max_length"  ,						max_length=self.tokenizer.model_max_length  ,						truncation=UpperCamelCase__  ,						return_tensors="pt"  ,						)
           lowerCamelCase     :   int    				=      text_inputs.input_ids
           lowerCamelCase     :   Union[str, Any]    				=      self.tokenizer(UpperCamelCase__  ,						padding="longest"  ,						return_tensors="pt"  ).input_ids

           if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(UpperCamelCase__  ,						UpperCamelCase__  ):
                  lowerCamelCase     :   Any    				=      self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]  )
                  logger.warning(
                      "The following part of your input was truncated because CLIP can only handle sequences up to"
                      F''' {self.tokenizer.model_max_length} tokens: {removed_text}'''  )

           lowerCamelCase     :   str    				=      self.text_encoder(text_input_ids.to(UpperCamelCase__  )  )
           lowerCamelCase     :   str    				=      text_encoder_output.text_embeds

           lowerCamelCase     :   Dict    				=      prompt_embeds.repeat_interleave(UpperCamelCase__  ,						dim=0  )
           # in Shap-E it normalize the prompt_embeds and then later rescale it
           lowerCamelCase     :   int    				=      prompt_embeds / torch.linalg.norm(UpperCamelCase__  ,						dim=-1  ,						keepdim=UpperCamelCase__  )

           if do_classifier_free_guidance:
                  lowerCamelCase     :   Union[str, Any]    				=      torch.zeros_like(UpperCamelCase__  )

                  # 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
                  lowerCamelCase     :   int    				=      torch.cat([negative_prompt_embeds, prompt_embeds]  )

           # Rescale the features to have unit variance
           lowerCamelCase     :   List[str]    				=      math.sqrt(prompt_embeds.shape[1]  ) * prompt_embeds

           return prompt_embeds







    @torch.no_grad()
    @replace_example_docstring(UpperCamelCase__  )
    def __call__(					self  ,						UpperCamelCase__  ,						UpperCamelCase__ = 1  ,						UpperCamelCase__ = 25  ,						UpperCamelCase__ = None  ,						UpperCamelCase__ = None  ,						UpperCamelCase__ = 4.0  ,						UpperCamelCase__ = 64  ,						UpperCamelCase__ = "pil"  ,						UpperCamelCase__ = True  ,						)  ->   int:



           if isinstance(UpperCamelCase__  ,						UpperCamelCase__  ):
                  lowerCamelCase     :   Optional[Any]    				=      1
           elif isinstance(UpperCamelCase__  ,						UpperCamelCase__  ):
                  lowerCamelCase     :   Any    				=      len(UpperCamelCase__  )
           else:
                  raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(UpperCamelCase__  )}'''  )

           lowerCamelCase     :   Optional[Any]    				=      self._execution_device

           lowerCamelCase     :   List[Any]    				=      batch_size * num_images_per_prompt

           lowerCamelCase     :   List[str]    				=      guidance_scale > 1.0
           lowerCamelCase     :   List[str]    				=      self._encode_prompt(UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  )

           # prior

           self.scheduler.set_timesteps(UpperCamelCase__  ,						device=UpperCamelCase__  )
           lowerCamelCase     :   Any    				=      self.scheduler.timesteps

           lowerCamelCase     :   int    				=      self.prior.config.num_embeddings
           lowerCamelCase     :   Any    				=      self.prior.config.embedding_dim

           lowerCamelCase     :   int    				=      self.prepare_latents(
               (batch_size, num_embeddings * embedding_dim)  ,						prompt_embeds.dtype  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						UpperCamelCase__  ,						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
           lowerCamelCase     :   Optional[Any]    				=      latents.reshape(latents.shape[0]  ,						UpperCamelCase__  ,						UpperCamelCase__  )

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

                  lowerCamelCase     :   Any    				=      self.prior(
                      UpperCamelCase__  ,						timestep=UpperCamelCase__  ,						proj_embedding=UpperCamelCase__  ,						).predicted_image_embedding

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

                  if do_classifier_free_guidance is not None:
                         lowerCamelCase     :   Union[str, Any]    				=      noise_pred.chunk(2  )
                         lowerCamelCase     :   List[str]    				=      noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)

                  lowerCamelCase     :   Union[str, Any]    				=      self.scheduler.step(
                      UpperCamelCase__  ,						timestep=UpperCamelCase__  ,						sample=UpperCamelCase__  ,						).prev_sample

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

           lowerCamelCase     :   Optional[int]    				=      []
           for i, latent in enumerate(UpperCamelCase__  ):
                  lowerCamelCase     :   str    				=      self.renderer.decode(
                      latent[None, :]  ,						UpperCamelCase__  ,						size=UpperCamelCase__  ,						ray_batch_size=4096  ,						n_coarse_samples=64  ,						n_fine_samples=128  ,						)
                  images.append(UpperCamelCase__  )

           lowerCamelCase     :   Any    				=      torch.stack(UpperCamelCase__  )

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

           lowerCamelCase     :   Tuple    				=      images.cpu().numpy()

           if output_type == "pil":
                  lowerCamelCase     :   List[str]    				=      [self.numpy_to_pil(UpperCamelCase__  ) 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=UpperCamelCase__  )












import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union




class 	UpperCamelCase__			(enum.Enum  ):

    '''simple docstring'''



    lowerCamelCase_   :			Tuple			  =   """timm"""
    lowerCamelCase_   :			Union[str, Any]			  =   """transformers"""




def   A   (   _SCREAMING_SNAKE_CASE				,_SCREAMING_SNAKE_CASE				,_SCREAMING_SNAKE_CASE					)			->				Optional[int]:
       if stage_names is None:
              raise ValueError("Stage_names must be set for transformers backbones"					)

       if out_features is not None:
              if not isinstance(_SCREAMING_SNAKE_CASE				,(list,)					):
                     raise ValueError(f'''out_features must be a list {type(_SCREAMING_SNAKE_CASE					)}'''					)
              if any(feat not in stage_names for feat in out_features					):
                     raise ValueError(f'''out_features must be a subset of stage_names: {stage_names} got {out_features}'''					)

       if out_indices is not None:
              if not isinstance(_SCREAMING_SNAKE_CASE				,(list, tuple)					):
                     raise ValueError(f'''out_indices must be a list or tuple, got {type(_SCREAMING_SNAKE_CASE					)}'''					)
              if any(idx >= len(_SCREAMING_SNAKE_CASE					) for idx in out_indices					):
                     raise ValueError("out_indices must be valid indices for stage_names {stage_names}, got {out_indices}"					)

       if out_features is not None and out_indices is not None:
              if len(_SCREAMING_SNAKE_CASE					) != len(_SCREAMING_SNAKE_CASE					):
                     raise ValueError("out_features and out_indices should have the same length if both are set"					)
              if out_features != [stage_names[idx] for idx in out_indices]:
                     raise ValueError("out_features and out_indices should correspond to the same stages if both are set"					)

def   A   (   _SCREAMING_SNAKE_CASE				,_SCREAMING_SNAKE_CASE				,_SCREAMING_SNAKE_CASE				,)			->				Optional[Any]:
       if out_indices is None and out_features is None:
              lowerCamelCase     :   Dict    				=      [len(_SCREAMING_SNAKE_CASE					) - 1]
              lowerCamelCase     :   Tuple    				=      [stage_names[-1]]
       elif out_indices is None and out_features is not None:
              lowerCamelCase     :   List[Any]    				=      [stage_names.index(_SCREAMING_SNAKE_CASE					) for layer in out_features]
       elif out_features is None and out_indices is not None:
              lowerCamelCase     :   Union[str, Any]    				=      [stage_names[idx] for idx in out_indices]
       return out_features, out_indices

def   A   (   _SCREAMING_SNAKE_CASE				,_SCREAMING_SNAKE_CASE				,_SCREAMING_SNAKE_CASE				,)			->				Tuple[List[str], List[int]]:
       verify_out_features_out_indices(out_features=_SCREAMING_SNAKE_CASE				,out_indices=_SCREAMING_SNAKE_CASE				,stage_names=_SCREAMING_SNAKE_CASE					)
       lowerCamelCase				,   lowerCamelCase     :   str    				=      _align_output_features_output_indices(
           out_features=_SCREAMING_SNAKE_CASE				,out_indices=_SCREAMING_SNAKE_CASE				,stage_names=_SCREAMING_SNAKE_CASE					)
       # Verify that the aligned out_features and out_indices are valid
       verify_out_features_out_indices(out_features=_SCREAMING_SNAKE_CASE				,out_indices=_SCREAMING_SNAKE_CASE				,stage_names=_SCREAMING_SNAKE_CASE					)
       return output_features, output_indices




class 	UpperCamelCase__			:

    '''simple docstring'''



    lowerCamelCase_   :			Optional[BackboneType]			  =   None







    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   None:
           if getattr(self  ,						"_backbone"  ,						UpperCamelCase__  ) is None:
                  raise ValueError("self._backbone must be set before calling _init_timm_backbone"  )

           # These will diagree with the defaults for the transformers models e.g. for resnet50
           # the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
           # the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
           lowerCamelCase     :   Any    				=      [stage["module"] for stage in self._backbone.feature_info.info]
           lowerCamelCase     :   Optional[int]    				=      [stage["num_chs"] for stage in self._backbone.feature_info.info]
           lowerCamelCase     :   str    				=      self._backbone.feature_info.out_indices
           lowerCamelCase     :   Any    				=      self._backbone.feature_info.module_name()

           # We verify the out indices and out features are valid
           verify_out_features_out_indices(
               out_features=UpperCamelCase__  ,						out_indices=UpperCamelCase__  ,						stage_names=self.stage_names  )
           lowerCamelCase				,   lowerCamelCase     :   Tuple    				=      out_features, out_indices







    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   None:
           lowerCamelCase     :   Optional[int]    				=      getattr(UpperCamelCase__  ,						"stage_names"  )
           lowerCamelCase     :   Optional[int]    				=      getattr(UpperCamelCase__  ,						"out_features"  ,						UpperCamelCase__  )
           lowerCamelCase     :   Union[str, Any]    				=      getattr(UpperCamelCase__  ,						"out_indices"  ,						UpperCamelCase__  )

           lowerCamelCase     :   List[Any]    				=      stage_names
           lowerCamelCase				,   lowerCamelCase     :   Dict    				=      get_aligned_output_features_output_indices(
               out_features=UpperCamelCase__  ,						out_indices=UpperCamelCase__  ,						stage_names=UpperCamelCase__  )
           # Number of channels for each stage. This is set in the transformer backbone model init
           lowerCamelCase     :   Tuple    				=      None







    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   None:
           lowerCamelCase     :   Any    				=      config

           lowerCamelCase     :   Any    				=      getattr(UpperCamelCase__  ,						"use_timm_backbone"  ,						UpperCamelCase__  )
           lowerCamelCase     :   int    				=      BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

           if self.backbone_type == BackboneType.TIMM:
                  self._init_timm_backbone(UpperCamelCase__  )
           elif self.backbone_type == BackboneType.TRANSFORMERS:
                  self._init_transformers_backbone(UpperCamelCase__  )
           else:
                  raise ValueError(F'''backbone_type {self.backbone_type} not supported.'''  )







    @property
    def 					_lowercase  (					self  )  ->   Optional[Any]:
           return self._out_features







    @out_features.setter
    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   Union[str, Any]:
           lowerCamelCase				,   lowerCamelCase     :   Dict    				=      get_aligned_output_features_output_indices(
               out_features=UpperCamelCase__  ,						out_indices=UpperCamelCase__  ,						stage_names=self.stage_names  )







    @property
    def 					_lowercase  (					self  )  ->   Optional[Any]:
           return self._out_indices







    @out_indices.setter
    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   List[str]:
           lowerCamelCase				,   lowerCamelCase     :   int    				=      get_aligned_output_features_output_indices(
               out_features=UpperCamelCase__  ,						out_indices=UpperCamelCase__  ,						stage_names=self.stage_names  )







    @property
    def 					_lowercase  (					self  )  ->   Tuple:
           # the current backbones will output the number of channels for each stage
           # even if that stage is not in the out_features list.
           return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names  )}







    @property
    def 					_lowercase  (					self  )  ->   List[Any]:
           return [self.out_feature_channels[name] for name in self.out_features]







    def 					_lowercase  (					self  ,						*UpperCamelCase__  ,						**UpperCamelCase__  )  ->   Tuple:
           lowerCamelCase     :   Union[str, Any]    				=      dict(inspect.signature(self.forward  ).parameters  )
           lowerCamelCase     :   Optional[int]    				=      {k: v for k, v in kwargs.items() if k in signature}
           return self(*UpperCamelCase__  ,						**UpperCamelCase__  )







    def 					_lowercase  (					self  ,						UpperCamelCase__  ,						UpperCamelCase__ = None  ,						UpperCamelCase__ = None  ,						UpperCamelCase__ = None  ,						)  ->   int:
           raise NotImplementedError("This method should be implemented by the derived class."  )







    def 					_lowercase  (					self  )  ->   Dict:
           lowerCamelCase     :   List[Any]    				=      super().to_dict()
           lowerCamelCase     :   Optional[int]    				=      output.pop("_out_features"  )
           lowerCamelCase     :   Dict    				=      output.pop("_out_indices"  )
           return output




class 	UpperCamelCase__			:

    '''simple docstring'''



    @property
    def 					_lowercase  (					self  )  ->   int:
           return self._out_features







    @out_features.setter
    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   Optional[Any]:
           lowerCamelCase				,   lowerCamelCase     :   List[Any]    				=      get_aligned_output_features_output_indices(
               out_features=UpperCamelCase__  ,						out_indices=UpperCamelCase__  ,						stage_names=self.stage_names  )







    @property
    def 					_lowercase  (					self  )  ->   Optional[Any]:
           return self._out_indices







    @out_indices.setter
    def 					_lowercase  (					self  ,						UpperCamelCase__  )  ->   int:
           lowerCamelCase				,   lowerCamelCase     :   Optional[Any]    				=      get_aligned_output_features_output_indices(
               out_features=UpperCamelCase__  ,						out_indices=UpperCamelCase__  ,						stage_names=self.stage_names  )







    def 					_lowercase  (					self  )  ->   Any:
           lowerCamelCase     :   str    				=      super().to_dict()
           lowerCamelCase     :   Union[str, Any]    				=      output.pop("_out_features"  )
           lowerCamelCase     :   Optional[int]    				=      output.pop("_out_indices"  )
           return output






