





'''simple docstring'''



import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_SCREAMING_SNAKE_CASE   =      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''')
_SCREAMING_SNAKE_CASE   =      parser.parse_args()


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

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

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

# optimize with ipex
_SCREAMING_SNAKE_CASE   =      torch.randn(2, 4, 6_4, 6_4)
_SCREAMING_SNAKE_CASE   =      torch.rand(1) * 9_9_9
_SCREAMING_SNAKE_CASE   =      torch.randn(2, 7_7, 7_6_8)
_SCREAMING_SNAKE_CASE   =      (sample, timestep, encoder_hidden_status)
try:
 _SCREAMING_SNAKE_CASE   =      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
 _SCREAMING_SNAKE_CASE   =      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
_SCREAMING_SNAKE_CASE   =      ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
_SCREAMING_SNAKE_CASE   =      ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
 _SCREAMING_SNAKE_CASE   =      ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
_SCREAMING_SNAKE_CASE   =      6_6_6
_SCREAMING_SNAKE_CASE   =      torch.Generator(device).manual_seed(seed)
_SCREAMING_SNAKE_CASE   =      {'''generator''': generator}
if args.steps is not None:
 _SCREAMING_SNAKE_CASE   =      args.steps

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

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












'''simple docstring'''



from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import BaseOutput, is_torch_available, is_transformers_available






@dataclass
class 							__lowercase    ( lowerCAmelCase__  ):







	'''simple docstring'''

	a      :			Union[List[PIL.Image.Image], np.ndarray]
	a      :			Optional[List[bool]]


if is_transformers_available() and is_torch_available():
	from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline





