
import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_lowerCamelCase	:    Tuple          =      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''')
_lowerCamelCase	:    List[str]          =      parser.parse_args()


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

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

# to channels last
_lowerCamelCase	:    Optional[Any]          =      pipe.unet.to(memory_format=torch.channels_last)
_lowerCamelCase	:    int          =      pipe.vae.to(memory_format=torch.channels_last)
_lowerCamelCase	:    Union[str, Any]          =      pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
			_lowerCamelCase	:    str          =      pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
_lowerCamelCase	:    Any          =      torch.randn(2, 4, 64, 64)
_lowerCamelCase	:    str          =      torch.rand(1) * 999
_lowerCamelCase	:    Optional[Any]          =      torch.randn(2, 77, 768)
_lowerCamelCase	:    List[str]          =      (sample, timestep, encoder_hidden_status)
try:
			_lowerCamelCase	:    Tuple          =      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
			_lowerCamelCase	:    Union[str, Any]          =      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
_lowerCamelCase	:    Optional[int]          =      ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
_lowerCamelCase	:    Tuple          =      ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
			_lowerCamelCase	:    List[Any]          =      ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
_lowerCamelCase	:    List[str]          =      666
_lowerCamelCase	:    List[Any]          =      torch.Generator(device).manual_seed(seed)
_lowerCamelCase	:    Dict          =      {'''generator''': generator}
if args.steps is not None:
			_lowerCamelCase	:    Optional[Any]          =      args.steps

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

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

import baseaa






def 	a_		(	__lowercase       :     str			)			->  bytes:
			return baseaa.aaaencode(string.encode('utf-8'			)			)






def 	a_		(	__lowercase       :     bytes			)			->  str:
			return baseaa.aaadecode(__lowercase			).decode('utf-8'			)


if __name__ == "__main__":
			import doctest

			doctest.testmod()