

import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


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


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

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

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

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

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

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

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




import numpy as np

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   str     ,					SCREAMING_SNAKE_CASE_		:   Optional[int]     ,					SCREAMING_SNAKE_CASE_		:   Optional[int]     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]     ,					SCREAMING_SNAKE_CASE_		:   List[Any]      ):



     '''simple docstring'''





     lowercase__   :       List[Any]				=	int(np.ceil((x_end - xa) / h      )      )
     lowercase__   :       Dict				=	np.zeros((n + 1,)      )
     lowercase__   :       Dict				=	ya
     lowercase__   :       Union[str, Any]				=	xa

     for k in range(SCREAMING_SNAKE_CASE_      ):
          lowercase__   :       Optional[int]				=	f(SCREAMING_SNAKE_CASE_     ,					y[k]      )
          lowercase__   :       Dict				=	f(x + 0.5 * h     ,					y[k] + 0.5 * h * ka      )
          lowercase__   :       Any				=	f(x + 0.5 * h     ,					y[k] + 0.5 * h * ka      )
          lowercase__   :       List[Any]				=	f(x + h     ,					y[k] + h * ka      )
          lowercase__   :       Tuple				=	y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka)
          x += h

     return y


if __name__ == "__main__":
       import doctest

       doctest.testmod()

