import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


__SCREAMING_SNAKE_CASE						:   Union[str, 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')
__SCREAMING_SNAKE_CASE						:   Union[str, Any]         =			parser.parse_args()


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

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

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

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

# compute
__SCREAMING_SNAKE_CASE						:   str         =			6_6_6
__SCREAMING_SNAKE_CASE						:   int         =			torch.Generator(device).manual_seed(seed)
__SCREAMING_SNAKE_CASE						:   Union[str, Any]         =			{'generator': generator}
if args.steps is not None:
    __SCREAMING_SNAKE_CASE						:   Tuple         =			args.steps

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

# save image
image.save('generated.png')
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__SCREAMING_SNAKE_CASE						:   Optional[Any]         =			{
    'configuration_autoformer': [
        'AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'AutoformerConfig',
    ],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__SCREAMING_SNAKE_CASE						:   Union[str, Any]         =			[
				    'AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'AutoformerForPrediction',
				    'AutoformerModel',
				    'AutoformerPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_autoformer import (
				    AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
				    AutoformerConfig,
				)

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_autoformer import (
								    AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    AutoformerForPrediction,
								    AutoformerModel,
								    AutoformerPreTrainedModel,
								)

else:
				import sys

				__SCREAMING_SNAKE_CASE						:   str         =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)