
"""simple docstring"""




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 	lowerCAmelCase_						(UpperCAmelCase__  ):


				"""simple docstring"""



				__UpperCamelCase      :						torch.FloatTensor






class 	lowerCAmelCase_						(UpperCAmelCase__ ,					UpperCAmelCase__  ):


				"""simple docstring"""



				@register_to_config
				def __init__(self   ,       SCREAMING_SNAKE_CASE__ = 32   ,       SCREAMING_SNAKE_CASE__ = 64   ,       SCREAMING_SNAKE_CASE__ = 20   ,       SCREAMING_SNAKE_CASE__ = 7_68   ,       SCREAMING_SNAKE_CASE__=77   ,       SCREAMING_SNAKE_CASE__=4   ,       SCREAMING_SNAKE_CASE__ = 0.0   ,       SCREAMING_SNAKE_CASE__ = "silu"   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = "linear"   ,       SCREAMING_SNAKE_CASE__ = "prd"   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = None   ,       )					->		Dict:



											"""simple docstring"""




											super().__init__()
											SCREAMING_SNAKE_CASE__						:       int           =   num_attention_heads
											SCREAMING_SNAKE_CASE__						:       Any           =   attention_head_dim
											SCREAMING_SNAKE_CASE__						:       List[str]           =   num_attention_heads * attention_head_dim
											SCREAMING_SNAKE_CASE__						:       List[str]           =   additional_embeddings

											SCREAMING_SNAKE_CASE__						:       str           =   time_embed_dim or inner_dim
											SCREAMING_SNAKE_CASE__						:       int           =   embedding_proj_dim or embedding_dim
											SCREAMING_SNAKE_CASE__						:       int           =   clip_embed_dim or embedding_dim

											SCREAMING_SNAKE_CASE__						:       Dict           =   Timesteps(__lowercase   ,       __lowercase   ,       0    )
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   TimestepEmbedding(__lowercase   ,       __lowercase   ,       out_dim=__lowercase   ,       act_fn=__lowercase    )

											SCREAMING_SNAKE_CASE__						:       int           =   nn.Linear(__lowercase   ,       __lowercase    )

											if embedding_proj_norm_type is None:
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   None
											elif embedding_proj_norm_type == "layer":
																		SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   nn.LayerNorm(__lowercase    )
											else:
																		raise ValueError(F'''unsupported embedding_proj_norm_type: {embedding_proj_norm_type}'''    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   nn.Linear(__lowercase   ,       __lowercase    )

											if encoder_hid_proj_type is None:
																		SCREAMING_SNAKE_CASE__						:       List[Any]           =   None
											elif encoder_hid_proj_type == "linear":
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   nn.Linear(__lowercase   ,       __lowercase    )
											else:
																		raise ValueError(F'''unsupported encoder_hid_proj_type: {encoder_hid_proj_type}'''    )

											SCREAMING_SNAKE_CASE__						:       Any           =   nn.Parameter(torch.zeros(1   ,       num_embeddings + additional_embeddings   ,       __lowercase    )    )

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

											SCREAMING_SNAKE_CASE__						:       List[str]           =   nn.ModuleList(
											    [
											        BasicTransformerBlock(
											            __lowercase   ,       __lowercase   ,       __lowercase   ,       dropout=__lowercase   ,       activation_fn="""gelu"""   ,       attention_bias=__lowercase   ,       )
											        for d in range(__lowercase    )
											    ]    )

											if norm_in_type == "layer":
																		SCREAMING_SNAKE_CASE__						:       Any           =   nn.LayerNorm(__lowercase    )
											elif norm_in_type is None:
																		SCREAMING_SNAKE_CASE__						:       List[str]           =   None
											else:
																		raise ValueError(F'''Unsupported norm_in_type: {norm_in_type}.'''    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   nn.LayerNorm(__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Any           =   nn.Linear(__lowercase   ,       __lowercase    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   torch.full(
											    [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings]   ,       -1_00_00.0    )
											causal_attention_mask.triu_(1    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   causal_attention_mask[None, ...]
											self.register_buffer("""causal_attention_mask"""   ,       __lowercase   ,       persistent=__lowercase    )

											SCREAMING_SNAKE_CASE__						:       int           =   nn.Parameter(torch.zeros(1   ,       __lowercase    )    )
											SCREAMING_SNAKE_CASE__						:       Any           =   nn.Parameter(torch.zeros(1   ,       __lowercase    )    )



				@property
				# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
				def 	__magic_name__    (self    )					->		Dict[str, AttentionProcessor]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   {}

											def fn_recursive_add_processors(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ):
																		if hasattr(__lowercase   ,       """set_processor"""    ):
																									SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   module.processor

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

																		return processors

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

											return processors



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   len(self.attn_processors.keys()    )

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

											def fn_recursive_attn_processor(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ):
																		if hasattr(__lowercase   ,       """set_processor"""    ):
																									if not isinstance(__lowercase   ,       __lowercase    ):
																																module.set_processor(__lowercase    )
																									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}'''   ,       __lowercase   ,       __lowercase    )

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



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



											"""simple docstring"""




											self.set_attn_processor(AttnProcessor()    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = None   ,       SCREAMING_SNAKE_CASE__ = True   ,       )					->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   hidden_states.shape[0]

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

											# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
											SCREAMING_SNAKE_CASE__						:       str           =   timesteps * torch.ones(__lowercase   ,       dtype=timesteps.dtype   ,       device=timesteps.device    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.time_proj(__lowercase    )

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

											if self.embedding_proj_norm is not None:
																		SCREAMING_SNAKE_CASE__						:       int           =   self.embedding_proj_norm(__lowercase    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.embedding_proj(__lowercase    )
											if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None:
																		SCREAMING_SNAKE_CASE__						:       Dict           =   self.encoder_hidden_states_proj(__lowercase    )
											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"""    )

											SCREAMING_SNAKE_CASE__						:       int           =   self.proj_in(__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Any           =   self.positional_embedding.to(hidden_states.dtype    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   []
											SCREAMING_SNAKE_CASE__						:       List[str]           =   0

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

											if len(proj_embeddings.shape    ) == 2:
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   proj_embeddings[:, None, :]

											if len(hidden_states.shape    ) == 2:
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   hidden_states[:, None, :]

											SCREAMING_SNAKE_CASE__						:       Tuple           =   additional_embeds + [
											    proj_embeddings,
											    time_embeddings[:, None, :],
											    hidden_states,
											]

											if self.prd_embedding is not None:
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   self.prd_embedding.to(hidden_states.dtype    ).expand(__lowercase   ,       -1   ,       -1    )
																		additional_embeds.append(__lowercase    )

											SCREAMING_SNAKE_CASE__						:       str           =   torch.cat(
											    __lowercase   ,       dim=1   ,       )

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

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   hidden_states + positional_embeddings

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

											if self.norm_in is not None:
																		SCREAMING_SNAKE_CASE__						:       Any           =   self.norm_in(__lowercase    )

											for block in self.transformer_blocks:
																		SCREAMING_SNAKE_CASE__						:       Dict           =   block(__lowercase   ,       attention_mask=__lowercase    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   self.norm_out(__lowercase    )

											if self.prd_embedding is not None:
																		SCREAMING_SNAKE_CASE__						:       Any           =   hidden_states[:, -1]
											else:
																		SCREAMING_SNAKE_CASE__						:       Dict           =   hidden_states[:, additional_embeddings_len:]

											SCREAMING_SNAKE_CASE__						:       Dict           =   self.proj_to_clip_embeddings(__lowercase    )

											if not return_dict:
																		return (predicted_image_embedding,)

											return PriorTransformerOutput(predicted_image_embedding=__lowercase    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   (prior_latents * self.clip_std) + self.clip_mean
											return prior_latents








from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
						if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
						from .pipeline_unclip import UnCLIPPipeline
						from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
						from .text_proj import UnCLIPTextProjModel