


'''simple docstring'''






import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


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


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

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

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

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

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

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

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



'''simple docstring'''






def       _snake_case     (  A )		->					bool:
							if not isinstance(A				,					A ):
														raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' )
							if len(A ) == 0:
														raise ValueError('''Input list must be a non empty list''' )
							if len(A ) == 1:
														return True
							lowerCAmelCase__      =			series[1] - series[0]
							for index in range(len(A ) - 1 ):
														if series[index + 1] - series[index] != common_diff:
																					return False
							return True


def       _snake_case     (  A )		->					float:
							if not isinstance(A				,					A ):
														raise ValueError('''Input series is not valid, valid series - [2, 4, 6]''' )
							if len(A ) == 0:
														raise ValueError('''Input list must be a non empty list''' )
							lowerCAmelCase__      =			0
							for val in series:
														answer += val
							return answer / len(A )


if __name__ == "__main__":
		import doctest

		doctest.testmod()