
import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


snake_case						: Any         =  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""")
snake_case						: Tuple         =  parser.parse_args()


snake_case						: str         =  'cpu'
snake_case						: List[Any]         =  'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'

snake_case						: Any         =  'path-to-your-trained-model'
snake_case						: List[Any]         =  StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
							snake_case						: Any         =  DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
snake_case						: Dict         =  pipe.to(device)

# to channels last
snake_case						: int         =  pipe.unet.to(memory_format=torch.channels_last)
snake_case						: int         =  pipe.vae.to(memory_format=torch.channels_last)
snake_case						: Dict         =  pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
							snake_case						: List[str]         =  pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
snake_case						: Any         =  torch.randn(2, 4, 64, 64)
snake_case						: Union[str, Any]         =  torch.rand(1) * 9_99
snake_case						: List[Any]         =  torch.randn(2, 77, 7_68)
snake_case						: List[Any]         =  (sample, timestep, encoder_hidden_status)
try:
							snake_case						: List[Any]         =  ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
							snake_case						: str         =  ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
snake_case						: Dict         =  ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
snake_case						: str         =  ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
							snake_case						: Dict         =  ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
snake_case						: Any         =  6_66
snake_case						: int         =  torch.Generator(device).manual_seed(seed)
snake_case						: List[str]         =  {'generator': generator}
if args.steps is not None:
							snake_case						: List[str]         =  args.steps

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

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








import os

from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home


lowerCamelCase_						: Optional[int]         =  HUGGINGFACE_HUB_CACHE


lowerCamelCase_						: List[str]         =  """config.json"""
lowerCamelCase_						: Any         =  """diffusion_pytorch_model.bin"""
lowerCamelCase_						: Union[str, Any]         =  """diffusion_flax_model.msgpack"""
lowerCamelCase_						: Dict         =  """model.onnx"""
lowerCamelCase_						: List[Any]         =  """diffusion_pytorch_model.safetensors"""
lowerCamelCase_						: Optional[Any]         =  """weights.pb"""
lowerCamelCase_						: Optional[Any]         =  """https://huggingface.co"""
lowerCamelCase_						: Union[str, Any]         =  default_cache_path
lowerCamelCase_						: Tuple         =  """diffusers_modules"""
lowerCamelCase_						: Optional[Any]         =  os.getenv("""HF_MODULES_CACHE""", os.path.join(hf_cache_home, """modules"""))
lowerCamelCase_						: str         =  ["""fp16""", """non-ema"""]
lowerCamelCase_						: List[Any]         =  """.self_attn"""






