



from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)


SCREAMING_SNAKE_CASE_:Optional[int]             =						logging.get_logger(__name__)  # pylint: disable=invalid-name

SCREAMING_SNAKE_CASE_:str             =						"""
    Examples:
        ```py
        >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline
        >>> from diffusers.utils import load_image
        >>> import torch

        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
        ...     \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16
        ... )
        >>> pipe_prior.to(\"cuda\")

        >>> prompt = \"A red cartoon frog, 4k\"
        >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)

        >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(
        ...     \"kandinsky-community/kandinsky-2-2-decoder\", torch_dtype=torch.float16
        ... )
        >>> pipe.to(\"cuda\")

        >>> init_image = load_image(
        ...     \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\"
        ...     \"/kandinsky/frog.png\"
        ... )

        >>> image = pipe(
        ...     image=init_image,
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=zero_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=100,
        ...     strength=0.2,
        ... ).images

        >>> image[0].save(\"red_frog.png\")
        ```
"""




def 	__UpperCamelCase	(  _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase=8		)       ->  Union[str, Any]:



    """simple docstring"""






    A :       Dict          =   height // scale_factor**2
    if height % scale_factor**2 != 0:
        new_height += 1
    A :       Optional[Any]          =   width // scale_factor**2
    if width % scale_factor**2 != 0:
        new_width += 1
    return new_height * scale_factor, new_width * scale_factor




def 	__UpperCamelCase	(  _lowerCAmelCase      ,     _lowerCAmelCase=512      ,     _lowerCAmelCase=512		)       ->  Dict:



    """simple docstring"""






    A :       str          =   pil_image.resize((w, h)      ,     resample=Image.BICUBIC      ,     reducing_gap=1		)
    A :       Optional[int]          =   np.array(pil_image.convert("""RGB"""		)		)
    A :       Optional[int]          =   arr.astype(np.floataa		) / 127.5 - 1
    A :       Optional[Any]          =   np.transpose(_lowerCAmelCase      ,     [2, 0, 1]		)
    A :       List[Any]          =   torch.from_numpy(_lowerCAmelCase		).unsqueeze(0		)
    return image






class SCREAMING_SNAKE_CASE__     (		SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    def __init__(       self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		):
        super().__init__()

        self.register_modules(
            unet=lowerCamelCase__,		scheduler=lowerCamelCase__,		movq=lowerCamelCase__,		)
        A :       int          =   2 ** (len(self.movq.config.block_out_channels		) - 1)






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		):
        # get the original timestep using init_timestep
        A :       int          =   min(int(num_inference_steps * strength		),		lowerCamelCase__		)

        A :       Dict          =   max(num_inference_steps - init_timestep,		0		)
        A :       Union[str, Any]          =   self.scheduler.timesteps[t_start:]

        return timesteps, num_inference_steps - t_start






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__=None		):
        if not isinstance(lowerCamelCase__,		(torch.Tensor, PIL.Image.Image, list)		):
            raise ValueError(
                f'''`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(lowerCamelCase__		)}'''		)

        A :       Optional[Any]          =   image.to(device=lowerCamelCase__,		dtype=lowerCamelCase__		)

        A :       Tuple          =   batch_size * num_images_per_prompt

        if image.shape[1] == 4:
            A :       List[Any]          =   image

        else:
            if isinstance(lowerCamelCase__,		lowerCamelCase__		) and len(lowerCamelCase__		) != batch_size:
                raise ValueError(
                    f'''You have passed a list of generators of length {len(lowerCamelCase__		)}, but requested an effective batch'''
                    f''' size of {batch_size}. Make sure the batch size matches the length of the generators.'''		)

            elif isinstance(lowerCamelCase__,		lowerCamelCase__		):
                A :       List[str]          =   [
                    self.movq.encode(image[i : i + 1]		).latent_dist.sample(generator[i]		) for i in range(lowerCamelCase__		)
                ]
                A :       int          =   torch.cat(lowerCamelCase__,		dim=0		)
            else:
                A :       Union[str, Any]          =   self.movq.encode(lowerCamelCase__		).latent_dist.sample(lowerCamelCase__		)

            A :       Dict          =   self.movq.config.scaling_factor * init_latents

        A :       List[str]          =   torch.cat([init_latents],		dim=0		)

        A :       str          =   init_latents.shape
        A :       Union[str, Any]          =   randn_tensor(lowerCamelCase__,		generator=lowerCamelCase__,		device=lowerCamelCase__,		dtype=lowerCamelCase__		)

        # get latents
        A :       Optional[int]          =   self.scheduler.add_noise(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		)

        A :       List[str]          =   init_latents

        return latents






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__=0		):
        if is_accelerate_available():
            from accelerate import cpu_offload
        else:
            raise ImportError("""Please install accelerate via `pip install accelerate`"""		)

        A :       Optional[Any]          =   torch.device(f'''cuda:{gpu_id}'''		)

        A :       Optional[int]          =   [
            self.unet,
            self.movq,
        ]
        for cpu_offloaded_model in models:
            if cpu_offloaded_model is not None:
                cpu_offload(lowerCamelCase__,		lowerCamelCase__		)






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__=0		):
        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."""		)

        A :       Dict          =   torch.device(f'''cuda:{gpu_id}'''		)

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

        A :       List[str]          =   None
        for cpu_offloaded_model in [self.unet, self.movq]:
            A     ,							A :       Any          =   cpu_offload_with_hook(lowerCamelCase__,		lowerCamelCase__,		prev_module_hook=lowerCamelCase__		)

        # We'll offload the last model manually.
        A :       Union[str, Any]          =   hook






    @property
    # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
    def 			_lowerCAmelCase					(       self		):
        if 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()
    @replace_example_docstring(lowerCamelCase__		)
    def __call__(       self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__ = 512,		lowerCamelCase__ = 512,		lowerCamelCase__ = 100,		lowerCamelCase__ = 4.0,		lowerCamelCase__ = 0.3,		lowerCamelCase__ = 1,		lowerCamelCase__ = None,		lowerCamelCase__ = "pil",		lowerCamelCase__ = True,		):
        A :       str          =   self._execution_device

        A :       List[Any]          =   guidance_scale > 1.0

        if isinstance(lowerCamelCase__,		lowerCamelCase__		):
            A :       Optional[Any]          =   torch.cat(lowerCamelCase__,		dim=0		)
        A :       Optional[int]          =   image_embeds.shape[0]
        if isinstance(lowerCamelCase__,		lowerCamelCase__		):
            A :       List[str]          =   torch.cat(lowerCamelCase__,		dim=0		)

        if do_classifier_free_guidance:
            A :       Tuple          =   image_embeds.repeat_interleave(lowerCamelCase__,		dim=0		)
            A :       Optional[int]          =   negative_image_embeds.repeat_interleave(lowerCamelCase__,		dim=0		)

        A :       int          =   torch.cat([negative_image_embeds, image_embeds],		dim=0		).to(dtype=self.unet.dtype,		device=lowerCamelCase__		)

        if not isinstance(lowerCamelCase__,		lowerCamelCase__		):
            A :       Tuple          =   [image]
        if not all(isinstance(lowerCamelCase__,		(PIL.Image.Image, torch.Tensor)		) for i in image		):
            raise ValueError(
                f'''Input is in incorrect format: {[type(lowerCamelCase__		) for i in image]}. Currently, we only support  PIL image and pytorch tensor'''		)

        A :       int          =   torch.cat([prepare_image(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		) for i in image],		dim=0		)
        A :       Dict          =   image.to(dtype=image_embeds.dtype,		device=lowerCamelCase__		)

        A :       Any          =   self.movq.encode(lowerCamelCase__		)["""latents"""]
        A :       List[str]          =   latents.repeat_interleave(lowerCamelCase__,		dim=0		)
        self.scheduler.set_timesteps(lowerCamelCase__,		device=lowerCamelCase__		)
        A     ,							A :       List[Any]          =   self.get_timesteps(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		)
        A :       Any          =   timesteps[:1].repeat(batch_size * num_images_per_prompt		)
        A     ,							A :       Union[str, Any]          =   downscale_height_and_width(lowerCamelCase__,		lowerCamelCase__,		self.movq_scale_factor		)
        A :       Tuple          =   self.prepare_latents(
            lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		image_embeds.dtype,		lowerCamelCase__,		lowerCamelCase__		)
        for i, t in enumerate(self.progress_bar(lowerCamelCase__		)		):
            # expand the latents if we are doing classifier free guidance
            A :       List[Any]          =   torch.cat([latents] * 2		) if do_classifier_free_guidance else latents

            A :       str          =   {"""image_embeds""": image_embeds}
            A :       Tuple          =   self.unet(
                sample=lowerCamelCase__,		timestep=lowerCamelCase__,		encoder_hidden_states=lowerCamelCase__,		added_cond_kwargs=lowerCamelCase__,		return_dict=lowerCamelCase__,		)[0]

            if do_classifier_free_guidance:
                A     ,							A :       Tuple          =   noise_pred.split(latents.shape[1],		dim=1		)
                A     ,							A :       Union[str, Any]          =   noise_pred.chunk(2		)
                A     ,							A :       str          =   variance_pred.chunk(2		)
                A :       Optional[int]          =   noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
                A :       Optional[int]          =   torch.cat([noise_pred, variance_pred_text],		dim=1		)

            if not (
                hasattr(self.scheduler.config,		"""variance_type"""		)
                and self.scheduler.config.variance_type in ["learned", "learned_range"]
            ):
                A     ,							A :       List[Any]          =   noise_pred.split(latents.shape[1],		dim=1		)

            # compute the previous noisy sample x_t -> x_t-1
            A :       Dict          =   self.scheduler.step(
                lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		generator=lowerCamelCase__,		)[0]

        # post-processing
        A :       List[Any]          =   self.movq.decode(lowerCamelCase__,		force_not_quantize=lowerCamelCase__		)["""sample"""]

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

        if output_type in ["np", "pil"]:
            A :       Dict          =   image * 0.5 + 0.5
            A :       List[str]          =   image.clamp(0,		1		)
            A :       str          =   image.cpu().permute(0,		2,		3,		1		).float().numpy()

        if output_type == "pil":
            A :       Tuple          =   self.numpy_to_pil(lowerCamelCase__		)

        if not return_dict:
            return (image,)

        return ImagePipelineOutput(images=lowerCamelCase__		)







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

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .attention_processor import AttentionProcessor, AttnProcessor
from .embeddings import TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin






@dataclass
class SCREAMING_SNAKE_CASE__     (		SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: torch.FloatTensor






class SCREAMING_SNAKE_CASE__     (		SCREAMING_SNAKE_CASE__ ,						SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    @register_to_config
    def __init__(       self,		lowerCamelCase__ = 32,		lowerCamelCase__ = 64,		lowerCamelCase__ = 20,		lowerCamelCase__ = 768,		lowerCamelCase__=77,		lowerCamelCase__=4,		lowerCamelCase__ = 0.0,		lowerCamelCase__ = "silu",		lowerCamelCase__ = None,		lowerCamelCase__ = None,		lowerCamelCase__ = "linear",		lowerCamelCase__ = "prd",		lowerCamelCase__ = None,		lowerCamelCase__ = None,		lowerCamelCase__ = None,		):
        super().__init__()
        A :       int          =   num_attention_heads
        A :       Tuple          =   attention_head_dim
        A :       Optional[int]          =   num_attention_heads * attention_head_dim
        A :       List[str]          =   additional_embeddings

        A :       int          =   time_embed_dim or inner_dim
        A :       Tuple          =   embedding_proj_dim or embedding_dim
        A :       Dict          =   clip_embed_dim or embedding_dim

        A :       List[str]          =   Timesteps(lowerCamelCase__,		lowerCamelCase__,		0		)
        A :       Any          =   TimestepEmbedding(lowerCamelCase__,		lowerCamelCase__,		out_dim=lowerCamelCase__,		act_fn=lowerCamelCase__		)

        A :       List[Any]          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)

        if embedding_proj_norm_type is None:
            A :       Union[str, Any]          =   None
        elif embedding_proj_norm_type == "layer":
            A :       str          =   nn.LayerNorm(lowerCamelCase__		)
        else:
            raise ValueError(f'''unsupported embedding_proj_norm_type: {embedding_proj_norm_type}'''		)

        A :       Optional[Any]          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)

        if encoder_hid_proj_type is None:
            A :       Dict          =   None
        elif encoder_hid_proj_type == "linear":
            A :       Union[str, Any]          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)
        else:
            raise ValueError(f'''unsupported encoder_hid_proj_type: {encoder_hid_proj_type}'''		)

        A :       List[str]          =   nn.Parameter(torch.zeros(1,		num_embeddings + additional_embeddings,		lowerCamelCase__		)		)

        if added_emb_type == "prd":
            A :       Union[str, Any]          =   nn.Parameter(torch.zeros(1,		1,		lowerCamelCase__		)		)
        elif added_emb_type is None:
            A :       int          =   None
        else:
            raise ValueError(
                f'''`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`.'''		)

        A :       int          =   nn.ModuleList(
            [
                BasicTransformerBlock(
                    lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		dropout=lowerCamelCase__,		activation_fn="""gelu""",		attention_bias=lowerCamelCase__,		)
                for d in range(lowerCamelCase__		)
            ]		)

        if norm_in_type == "layer":
            A :       int          =   nn.LayerNorm(lowerCamelCase__		)
        elif norm_in_type is None:
            A :       Tuple          =   None
        else:
            raise ValueError(f'''Unsupported norm_in_type: {norm_in_type}.'''		)

        A :       Optional[Any]          =   nn.LayerNorm(lowerCamelCase__		)

        A :       str          =   nn.Linear(lowerCamelCase__,		lowerCamelCase__		)

        A :       Union[str, Any]          =   torch.full(
            [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings],		-1_0000.0		)
        causal_attention_mask.triu_(1		)
        A :       Optional[Any]          =   causal_attention_mask[None, ...]
        self.register_buffer("""causal_attention_mask""",		lowerCamelCase__,		persistent=lowerCamelCase__		)

        A :       Dict          =   nn.Parameter(torch.zeros(1,		lowerCamelCase__		)		)
        A :       Tuple          =   nn.Parameter(torch.zeros(1,		lowerCamelCase__		)		)






    @property
    # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
    def 			_lowerCAmelCase					(       self		):
        A :       Optional[int]          =   {}

        def fn_recursive_add_processors(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		):
            if hasattr(lowerCamelCase__,		"""set_processor"""		):
                A :       Tuple          =   module.processor

            for sub_name, child in module.named_children():
                fn_recursive_add_processors(f'''{name}.{sub_name}''',		lowerCamelCase__,		lowerCamelCase__		)

            return processors

        for name, module in self.named_children():
            fn_recursive_add_processors(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		)

        return processors






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__		):
        A :       Optional[Any]          =   len(self.attn_processors.keys()		)

        if isinstance(lowerCamelCase__,		lowerCamelCase__		) and len(lowerCamelCase__		) != count:
            raise ValueError(
                f'''A dict of processors was passed, but the number of processors {len(lowerCamelCase__		)} does not match the'''
                f''' number of attention layers: {count}. Please make sure to pass {count} processor classes.'''		)

        def fn_recursive_attn_processor(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		):
            if hasattr(lowerCamelCase__,		"""set_processor"""		):
                if not isinstance(lowerCamelCase__,		lowerCamelCase__		):
                    module.set_processor(lowerCamelCase__		)
                else:
                    module.set_processor(processor.pop(f'''{name}.processor'''		)		)

            for sub_name, child in module.named_children():
                fn_recursive_attn_processor(f'''{name}.{sub_name}''',		lowerCamelCase__,		lowerCamelCase__		)

        for name, module in self.named_children():
            fn_recursive_attn_processor(lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		)






    def 			_lowerCAmelCase					(       self		):
        self.set_attn_processor(AttnProcessor()		)






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__ = None,		lowerCamelCase__ = None,		lowerCamelCase__ = True,		):
        A :       Optional[int]          =   hidden_states.shape[0]

        A :       Tuple          =   timestep
        if not torch.is_tensor(lowerCamelCase__		):
            A :       List[Any]          =   torch.tensor([timesteps],		dtype=torch.long,		device=hidden_states.device		)
        elif torch.is_tensor(lowerCamelCase__		) and len(timesteps.shape		) == 0:
            A :       Optional[int]          =   timesteps[None].to(hidden_states.device		)

        # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
        A :       Optional[Any]          =   timesteps * torch.ones(lowerCamelCase__,		dtype=timesteps.dtype,		device=timesteps.device		)

        A :       str          =   self.time_proj(lowerCamelCase__		)

        # timesteps does not contain any weights and will always return f32 tensors
        # but time_embedding might be fp16, so we need to cast here.
        A :       Union[str, Any]          =   timesteps_projected.to(dtype=self.dtype		)
        A :       Tuple          =   self.time_embedding(lowerCamelCase__		)

        if self.embedding_proj_norm is not None:
            A :       Optional[Any]          =   self.embedding_proj_norm(lowerCamelCase__		)

        A :       Any          =   self.embedding_proj(lowerCamelCase__		)
        if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
            A :       List[str]          =   self.encoder_hidden_states_proj(lowerCamelCase__		)
        elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None:
            raise ValueError("""`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set"""		)

        A :       Tuple          =   self.proj_in(lowerCamelCase__		)

        A :       Dict          =   self.positional_embedding.to(hidden_states.dtype		)

        A :       List[str]          =   []
        A :       Optional[Any]          =   0

        if encoder_hidden_states is not None:
            additional_embeds.append(lowerCamelCase__		)
            additional_embeddings_len += encoder_hidden_states.shape[1]

        if len(proj_embeddings.shape		) == 2:
            A :       Optional[Any]          =   proj_embeddings[:, None, :]

        if len(hidden_states.shape		) == 2:
            A :       List[str]          =   hidden_states[:, None, :]

        A :       Dict          =   additional_embeds + [
            proj_embeddings,
            time_embeddings[:, None, :],
            hidden_states,
        ]

        if self.prd_embedding is not None:
            A :       List[Any]          =   self.prd_embedding.to(hidden_states.dtype		).expand(lowerCamelCase__,		-1,		-1		)
            additional_embeds.append(lowerCamelCase__		)

        A :       Union[str, Any]          =   torch.cat(
            lowerCamelCase__,		dim=1,		)

        # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens
        A :       Dict          =   additional_embeddings_len + proj_embeddings.shape[1] + 1
        if positional_embeddings.shape[1] < hidden_states.shape[1]:
            A :       Any          =   F.pad(
                lowerCamelCase__,		(
                    0,
                    0,
                    additional_embeddings_len,
                    self.prd_embedding.shape[1] if self.prd_embedding is not None else 0,
                ),		value=0.0,		)

        A :       Union[str, Any]          =   hidden_states + positional_embeddings

        if attention_mask is not None:
            A :       List[Any]          =   (1 - attention_mask.to(hidden_states.dtype		)) * -1_0000.0
            A :       Tuple          =   F.pad(lowerCamelCase__,		(0, self.additional_embeddings),		value=0.0		)
            A :       List[Any]          =   (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype		)
            A :       Any          =   attention_mask.repeat_interleave(self.config.num_attention_heads,		dim=0		)

        if self.norm_in is not None:
            A :       str          =   self.norm_in(lowerCamelCase__		)

        for block in self.transformer_blocks:
            A :       Optional[Any]          =   block(lowerCamelCase__,		attention_mask=lowerCamelCase__		)

        A :       Optional[int]          =   self.norm_out(lowerCamelCase__		)

        if self.prd_embedding is not None:
            A :       Dict          =   hidden_states[:, -1]
        else:
            A :       Optional[int]          =   hidden_states[:, additional_embeddings_len:]

        A :       Optional[Any]          =   self.proj_to_clip_embeddings(lowerCamelCase__		)

        if not return_dict:
            return (predicted_image_embedding,)

        return PriorTransformerOutput(predicted_image_embedding=lowerCamelCase__		)






    def 			_lowerCAmelCase					(       self,		lowerCamelCase__		):
        A :       str          =   (prior_latents * self.clip_std) + self.clip_mean
        return prior_latents


