

import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


__lowercase     : 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''')
__lowercase     : int       =						parser.parse_args()


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

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

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

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

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

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
     __lowercase     : Dict       =						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


__magic_name__  :  Optional[Any]					=       {
    """configuration_informer""": [
        """INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """InformerConfig""",
    ],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __magic_name__  :  int					=       [
          """INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """InformerForPrediction""",
          """InformerModel""",
          """InformerPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_informer import (
                INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
                InformerForPrediction,
                InformerModel,
                InformerPreTrainedModel,
            )

else:
      import sys

      __magic_name__  :  Any					=       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

