

"""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		="""CompVis/stable-diffusion-v1-1"""
a		="""CompVis/stable-diffusion-v1-2"""
a		="""CompVis/stable-diffusion-v1-3"""
a		="""CompVis/stable-diffusion-v1-4"""






class 	A_		(	SCREAMING_SNAKE_CASE    ):
							def __init__(		self		:      Any       ,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       ,):
								super()._init_()

								__lowerCamelCase    :    List[Any] 	=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    str 	=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    Union[str, Any] 	=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    Dict 	=			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    lowerCAmelCase					(		self		:      int):
								return {k: getattr(self       ,SCREAMING_SNAKE_CASE__) for k in self.config.keys() if not k.startswith('_')}
							def    lowerCAmelCase					(		self		:      int       ,SCREAMING_SNAKE_CASE__		:      Optional[Union[str, int]] = "auto"):
								if slice_size == "auto":
									# half the attention head size is usually a good trade-off between
									# speed and memory
									__lowerCamelCase    :    Any 	=			self.unet.config.attention_head_dim // 2
								self.unet.set_attention_slice(SCREAMING_SNAKE_CASE__)
							def    lowerCAmelCase					(		self		:      Optional[Any]):
								self.enable_attention_slicing(SCREAMING_SNAKE_CASE__)
							@torch.no_grad()
							def    lowerCAmelCase					(		self		:      List[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__		:      Any       ,):
								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    lowerCAmelCase					(		self		:      List[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]       ,):
								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    lowerCAmelCase					(		self		:      Tuple       ,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       ,):
								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    lowerCAmelCase					(		self		:      Optional[int]       ,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]       ,):
								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    lowerCAmelCase					(		self		:      Dict       ,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[Any]       ,):
								__lowerCamelCase    :    Optional[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
								__lowerCamelCase    :    str 	=			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
								__lowerCamelCase    :    str 	=			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
								__lowerCamelCase    :    List[str] 	=			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
								__lowerCamelCase    :    str 	=			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]])







import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


a		=argparse.ArgumentParser("""Stable Diffusion script with intel optimization""", add_help=False)
parser.add_argument("""--dpm""", action="""store_true""", help="""Enable DPMSolver or not""")
parser.add_argument("""--steps""", default=None, type=int, help="""Num inference steps""")
a		=parser.parse_args()


a		="""cpu"""
a		="""a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings"""

a		="""path-to-your-trained-model"""
a		=StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
					a		=DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
a		=pipe.to(device)

# to channels last
a		=pipe.unet.to(memory_format=torch.channels_last)
a		=pipe.vae.to(memory_format=torch.channels_last)
a		=pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
					a		=pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
a		=torch.randn(2, 4, 64, 64)
a		=torch.rand(1) * 999
a		=torch.randn(2, 77, 768)
a		=(sample, timestep, encoder_hidden_status)
try:
					a		=ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
					a		=ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
a		=ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
a		=ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
					a		=ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
a		=666
a		=torch.Generator(device).manual_seed(seed)
a		={"""generator""": generator}
if args.steps is not None:
					a		=args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
					a		=pipe(prompt, **generate_kwargs).images[0]

# save image
image.save("""generated.png""")




