


'''simple docstring'''







from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


A__:    Optional[int]					    =       '''CompVis/stable-diffusion-v1-1'''
A__:    Union[str, Any]					    =       '''CompVis/stable-diffusion-v1-2'''
A__:    List[Any]					    =       '''CompVis/stable-diffusion-v1-3'''
A__:    Dict					    =       '''CompVis/stable-diffusion-v1-4'''







class A__       (						UpperCAmelCase__      ):






      def __init__( self			:List[str]     ,						SCREAMING_SNAKE_CASE			:AutoencoderKL     ,						SCREAMING_SNAKE_CASE			:CLIPTextModel     ,						SCREAMING_SNAKE_CASE			:CLIPTokenizer     ,						SCREAMING_SNAKE_CASE			:UNetaDConditionModel     ,						SCREAMING_SNAKE_CASE			:Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]     ,						SCREAMING_SNAKE_CASE			:StableDiffusionSafetyChecker     ,						SCREAMING_SNAKE_CASE			:CLIPImageProcessor     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						)       ->					int:




             '''simple docstring'''
             super()._init_()

             _a    : Union[str, Any]     					=StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE	)
             _a    : Any     					=StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE	)
             _a    : List[Any]     					=StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE	)
             _a    : List[Any]     					=StableDiffusionPipeline(
                 vae=SCREAMING_SNAKE_CASE     ,						text_encoder=SCREAMING_SNAKE_CASE     ,						tokenizer=SCREAMING_SNAKE_CASE     ,						unet=SCREAMING_SNAKE_CASE     ,						scheduler=SCREAMING_SNAKE_CASE     ,						safety_checker=SCREAMING_SNAKE_CASE     ,						feature_extractor=SCREAMING_SNAKE_CASE     ,						requires_safety_checker=SCREAMING_SNAKE_CASE     ,						)

             self.register_modules(pipelinea=self.pipea     ,						pipelinea=self.pipea     ,						pipelinea=self.pipea     ,						pipelinea=self.pipea	)






      @property
      def __UpperCAmelCase       ( self			:List[Any]	)       ->					Dict[str, Any]:




             '''simple docstring'''
             return {k: getattr(self     ,						SCREAMING_SNAKE_CASE	) for k in self.config.keys() if not k.startswith("""_"""	)}






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, int]] = "auto"	)       ->					List[str]:




             '''simple docstring'''
             if slice_size == "auto":
                    # half the attention head size is usually a good trade-off between
                    # speed and memory
                    _a    : Dict     					=self.unet.config.attention_head_dim // 2
             self.unet.set_attention_slice(SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Any	)       ->					List[str]:




             '''simple docstring'''
             self.enable_attention_slicing(SCREAMING_SNAKE_CASE	)






      @torch.no_grad()
      def __UpperCAmelCase       ( self			:List[str]     ,						SCREAMING_SNAKE_CASE			:Union[str, List[str]]     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_0     ,						SCREAMING_SNAKE_CASE			:float = 7.5     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, List[str]]] = None     ,						SCREAMING_SNAKE_CASE			:Optional[int] = 1     ,						SCREAMING_SNAKE_CASE			:float = 0.0     ,						SCREAMING_SNAKE_CASE			:Optional[torch.Generator] = None     ,						SCREAMING_SNAKE_CASE			:Optional[torch.FloatTensor] = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = "pil"     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						SCREAMING_SNAKE_CASE			:Optional[Callable[[int, int, torch.FloatTensor], None]] = None     ,						SCREAMING_SNAKE_CASE			:int = 1     ,						**SCREAMING_SNAKE_CASE			:int     ,						)       ->					int:




             '''simple docstring'''
             return self.pipea(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)






      @torch.no_grad()
      def __UpperCAmelCase       ( self			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:Union[str, List[str]]     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_0     ,						SCREAMING_SNAKE_CASE			:float = 7.5     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, List[str]]] = None     ,						SCREAMING_SNAKE_CASE			:Optional[int] = 1     ,						SCREAMING_SNAKE_CASE			:float = 0.0     ,						SCREAMING_SNAKE_CASE			:Optional[torch.Generator] = None     ,						SCREAMING_SNAKE_CASE			:Optional[torch.FloatTensor] = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = "pil"     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						SCREAMING_SNAKE_CASE			:Optional[Callable[[int, int, torch.FloatTensor], None]] = None     ,						SCREAMING_SNAKE_CASE			:int = 1     ,						**SCREAMING_SNAKE_CASE			:List[str]     ,						)       ->					int:




             '''simple docstring'''
             return self.pipea(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)






      @torch.no_grad()
      def __UpperCAmelCase       ( self			:str     ,						SCREAMING_SNAKE_CASE			:Union[str, List[str]]     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_0     ,						SCREAMING_SNAKE_CASE			:float = 7.5     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, List[str]]] = None     ,						SCREAMING_SNAKE_CASE			:Optional[int] = 1     ,						SCREAMING_SNAKE_CASE			:float = 0.0     ,						SCREAMING_SNAKE_CASE			:Optional[torch.Generator] = None     ,						SCREAMING_SNAKE_CASE			:Optional[torch.FloatTensor] = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = "pil"     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						SCREAMING_SNAKE_CASE			:Optional[Callable[[int, int, torch.FloatTensor], None]] = None     ,						SCREAMING_SNAKE_CASE			:int = 1     ,						**SCREAMING_SNAKE_CASE			:str     ,						)       ->					Union[str, Any]:




             '''simple docstring'''
             return self.pipea(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)






      @torch.no_grad()
      def __UpperCAmelCase       ( self			:Any     ,						SCREAMING_SNAKE_CASE			:Union[str, List[str]]     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_0     ,						SCREAMING_SNAKE_CASE			:float = 7.5     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, List[str]]] = None     ,						SCREAMING_SNAKE_CASE			:Optional[int] = 1     ,						SCREAMING_SNAKE_CASE			:float = 0.0     ,						SCREAMING_SNAKE_CASE			:Optional[torch.Generator] = None     ,						SCREAMING_SNAKE_CASE			:Optional[torch.FloatTensor] = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = "pil"     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						SCREAMING_SNAKE_CASE			:Optional[Callable[[int, int, torch.FloatTensor], None]] = None     ,						SCREAMING_SNAKE_CASE			:int = 1     ,						**SCREAMING_SNAKE_CASE			:Dict     ,						)       ->					Optional[Any]:




             '''simple docstring'''
             return self.pipea(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)






      @torch.no_grad()
      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Union[str, List[str]]     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_1_2     ,						SCREAMING_SNAKE_CASE			:int = 5_0     ,						SCREAMING_SNAKE_CASE			:float = 7.5     ,						SCREAMING_SNAKE_CASE			:Optional[Union[str, List[str]]] = None     ,						SCREAMING_SNAKE_CASE			:Optional[int] = 1     ,						SCREAMING_SNAKE_CASE			:float = 0.0     ,						SCREAMING_SNAKE_CASE			:Optional[torch.Generator] = None     ,						SCREAMING_SNAKE_CASE			:Optional[torch.FloatTensor] = None     ,						SCREAMING_SNAKE_CASE			:Optional[str] = "pil"     ,						SCREAMING_SNAKE_CASE			:bool = True     ,						SCREAMING_SNAKE_CASE			:Optional[Callable[[int, int, torch.FloatTensor], None]] = None     ,						SCREAMING_SNAKE_CASE			:int = 1     ,						**SCREAMING_SNAKE_CASE			:Optional[int]     ,						)       ->					Union[str, Any]:




             '''simple docstring'''
             _a    : List[Any]     					="""cuda""" if torch.cuda.is_available() else """cpu"""
             self.to(SCREAMING_SNAKE_CASE	)

             # Checks if the height and width are divisible by 8 or not
             if height % 8 != 0 or width % 8 != 0:
                    raise ValueError(f"`height` and `width` must be divisible by 8 but are {height} and {width}."	)

             # Get first result from Stable Diffusion Checkpoint v1.1
             _a    : Optional[Any]     					=self.textaimg_sda_a(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)

             # Get first result from Stable Diffusion Checkpoint v1.2
             _a    : Dict     					=self.textaimg_sda_a(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)

             # Get first result from Stable Diffusion Checkpoint v1.3
             _a    : Dict     					=self.textaimg_sda_a(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)

             # Get first result from Stable Diffusion Checkpoint v1.4
             _a    : Dict     					=self.textaimg_sda_a(
                 prompt=SCREAMING_SNAKE_CASE     ,						height=SCREAMING_SNAKE_CASE     ,						width=SCREAMING_SNAKE_CASE     ,						num_inference_steps=SCREAMING_SNAKE_CASE     ,						guidance_scale=SCREAMING_SNAKE_CASE     ,						negative_prompt=SCREAMING_SNAKE_CASE     ,						num_images_per_prompt=SCREAMING_SNAKE_CASE     ,						eta=SCREAMING_SNAKE_CASE     ,						generator=SCREAMING_SNAKE_CASE     ,						latents=SCREAMING_SNAKE_CASE     ,						output_type=SCREAMING_SNAKE_CASE     ,						return_dict=SCREAMING_SNAKE_CASE     ,						callback=SCREAMING_SNAKE_CASE     ,						callback_steps=SCREAMING_SNAKE_CASE     ,						**SCREAMING_SNAKE_CASE     ,						)

             # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
             return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]	)






'''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	)


