


'''simple docstring'''







import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin







class A__       (						UpperCAmelCase__ ,     UpperCAmelCase__      ):






      @register_to_config
      def __init__( self			:Optional[Any]     ,						*,
          SCREAMING_SNAKE_CASE			:int = 4     ,						SCREAMING_SNAKE_CASE			:int = 7_6_8     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:List[Any]     ,						)       ->					Union[str, Any]:




             '''simple docstring'''
             super().__init__()

             _a    : Optional[int]     					=nn.Parameter(torch.zeros(SCREAMING_SNAKE_CASE	)	)

             # parameters for additional clip time embeddings
             _a    : str     					=nn.Linear(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)
             _a    : Optional[Any]     					=nn.Linear(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)

             # parameters for encoder hidden states
             _a    : List[Any]     					=clip_extra_context_tokens
             _a    : List[str]     					=nn.Linear(
                 SCREAMING_SNAKE_CASE     ,						self.clip_extra_context_tokens * cross_attention_dim	)
             _a    : List[Any]     					=nn.Linear(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)
             _a    : List[str]     					=nn.LayerNorm(SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Any     ,						*, SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:List[Any]     ,						SCREAMING_SNAKE_CASE			:Dict     ,						SCREAMING_SNAKE_CASE			:List[str]	)       ->					Optional[int]:




             '''simple docstring'''
             if do_classifier_free_guidance:
                    # Add the classifier free guidance embeddings to the image embeddings
                    _a    : Tuple     					=image_embeddings.shape[0]
                    _a    : List[Any]     					=self.learned_classifier_free_guidance_embeddings.unsqueeze(0	)
                    _a    : List[str]     					=classifier_free_guidance_embeddings.expand(
                        SCREAMING_SNAKE_CASE     ,						-1	)
                    _a    : str     					=torch.cat([classifier_free_guidance_embeddings, image_embeddings]     ,						dim=0	)

             # The image embeddings batch size and the text embeddings batch size are equal
             assert image_embeddings.shape[0] == prompt_embeds.shape[0]

             _a    : List[Any]     					=prompt_embeds.shape[0]

             # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
             # adding CLIP embeddings to the existing timestep embedding, ...
             _a    : Any     					=self.embedding_proj(SCREAMING_SNAKE_CASE	)
             _a    : Any     					=self.clip_image_embeddings_project_to_time_embeddings(SCREAMING_SNAKE_CASE	)
             _a    : List[str]     					=time_projected_image_embeddings + time_projected_prompt_embeds

             # ... and by projecting CLIP embeddings into four
             # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
             _a    : str     					=self.clip_extra_context_tokens_proj(SCREAMING_SNAKE_CASE	)
             _a    : List[str]     					=clip_extra_context_tokens.reshape(SCREAMING_SNAKE_CASE     ,						-1     ,						self.clip_extra_context_tokens	)
             _a    : int     					=clip_extra_context_tokens.permute(0     ,						2     ,						1	)

             _a    : Optional[Any]     					=self.encoder_hidden_states_proj(SCREAMING_SNAKE_CASE	)
             _a    : str     					=self.text_encoder_hidden_states_norm(SCREAMING_SNAKE_CASE	)
             _a    : Union[str, Any]     					=torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]     ,						dim=1	)

             return text_encoder_hidden_states, additive_clip_time_embeddings






'''simple docstring'''







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

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer







@dataclass
class A__       (						UpperCAmelCase__      ):
      __UpperCamelCase       :  torch.FloatTensor







class A__       (						UpperCAmelCase__ ,     UpperCAmelCase__      ):






      @register_to_config
      def __init__( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:int = 3     ,						SCREAMING_SNAKE_CASE			:int = 3     ,						SCREAMING_SNAKE_CASE			:Tuple[str] = ("DownEncoderBlock2D",)     ,						SCREAMING_SNAKE_CASE			:Tuple[str] = ("UpDecoderBlock2D",)     ,						SCREAMING_SNAKE_CASE			:Tuple[int] = (6_4,)     ,						SCREAMING_SNAKE_CASE			:int = 1     ,						SCREAMING_SNAKE_CASE			:str = "silu"     ,						SCREAMING_SNAKE_CASE			:int = 3     ,						SCREAMING_SNAKE_CASE			:int = 3_2     ,						SCREAMING_SNAKE_CASE			:int = 2_5_6     ,						SCREAMING_SNAKE_CASE			:int = 3_2     ,						SCREAMING_SNAKE_CASE			:Optional[int] = None     ,						SCREAMING_SNAKE_CASE			:float = 0.18_215     ,						SCREAMING_SNAKE_CASE			:str = "group"     ,						)       ->					Optional[int]:




             '''simple docstring'''
             super().__init__()

             # pass init params to Encoder
             _a    : Union[str, Any]     					=Encoder(
                 in_channels=SCREAMING_SNAKE_CASE     ,						out_channels=SCREAMING_SNAKE_CASE     ,						down_block_types=SCREAMING_SNAKE_CASE     ,						block_out_channels=SCREAMING_SNAKE_CASE     ,						layers_per_block=SCREAMING_SNAKE_CASE     ,						act_fn=SCREAMING_SNAKE_CASE     ,						norm_num_groups=SCREAMING_SNAKE_CASE     ,						double_z=SCREAMING_SNAKE_CASE     ,						)

             _a    : Optional[int]     					=vq_embed_dim if vq_embed_dim is not None else latent_channels

             _a    : Optional[int]     					=nn.Convad(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						1	)
             _a    : str     					=VectorQuantizer(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						beta=0.25     ,						remap=SCREAMING_SNAKE_CASE     ,						sane_index_shape=SCREAMING_SNAKE_CASE	)
             _a    : List[str]     					=nn.Convad(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						1	)

             # pass init params to Decoder
             _a    : List[str]     					=Decoder(
                 in_channels=SCREAMING_SNAKE_CASE     ,						out_channels=SCREAMING_SNAKE_CASE     ,						up_block_types=SCREAMING_SNAKE_CASE     ,						block_out_channels=SCREAMING_SNAKE_CASE     ,						layers_per_block=SCREAMING_SNAKE_CASE     ,						act_fn=SCREAMING_SNAKE_CASE     ,						norm_num_groups=SCREAMING_SNAKE_CASE     ,						norm_type=SCREAMING_SNAKE_CASE     ,						)






      @apply_forward_hook
      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:torch.FloatTensor     ,						SCREAMING_SNAKE_CASE			:bool = True	)       ->					VQEncoderOutput:




             '''simple docstring'''
             _a    : Optional[int]     					=self.encoder(SCREAMING_SNAKE_CASE	)
             _a    : int     					=self.quant_conv(SCREAMING_SNAKE_CASE	)

             if not return_dict:
                    return (h,)

             return VQEncoderOutput(latents=SCREAMING_SNAKE_CASE	)






      @apply_forward_hook
      def __UpperCAmelCase       ( self			:List[Any]     ,						SCREAMING_SNAKE_CASE			:torch.FloatTensor     ,						SCREAMING_SNAKE_CASE			:bool = False     ,						SCREAMING_SNAKE_CASE			:bool = True	)       ->					Union[DecoderOutput, torch.FloatTensor]:




             '''simple docstring'''
             # also go through quantization layer
             if not force_not_quantize:
                    _a					,    _a					,    _a    : Tuple     					=self.quantize(SCREAMING_SNAKE_CASE	)
             else:
                    _a    : str     					=h
             _a    : Dict     					=self.post_quant_conv(SCREAMING_SNAKE_CASE	)
             _a    : Union[str, Any]     					=self.decoder(SCREAMING_SNAKE_CASE     ,						quant if self.config.norm_type == """spatial""" else None	)

             if not return_dict:
                    return (dec,)

             return DecoderOutput(sample=SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:torch.FloatTensor     ,						SCREAMING_SNAKE_CASE			:bool = True	)       ->					Union[DecoderOutput, torch.FloatTensor]:




             '''simple docstring'''
             _a    : Tuple     					=sample
             _a    : int     					=self.encode(SCREAMING_SNAKE_CASE	).latents
             _a    : List[Any]     					=self.decode(SCREAMING_SNAKE_CASE	).sample

             if not return_dict:
                    return (dec,)

             return DecoderOutput(sample=SCREAMING_SNAKE_CASE	)


