
"""simple docstring"""







import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


lowerCamelCase__   					=		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__   					=		parser.parse_args()


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

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

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

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

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

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

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






'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase					:     Optional[Any]												={
    '''configuration_nllb_moe''': [
        '''NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''NllbMoeConfig''',
    ]
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase					:     Tuple												=[
     '''NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''NllbMoeForConditionalGeneration''',
     '''NllbMoeModel''',
     '''NllbMoePreTrainedModel''',
     '''NllbMoeTop2Router''',
     '''NllbMoeSparseMLP''',
 ]


if TYPE_CHECKING:
 from .configuration_nllb_moe import (
     NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
     NllbMoeConfig,
 )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_nllb_moe import (
      NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
      NllbMoeForConditionalGeneration,
      NllbMoeModel,
      NllbMoePreTrainedModel,
      NllbMoeSparseMLP,
      NllbMoeTopaRouter,
  )


else:
 import sys

 lowerCAmelCase					:     Tuple												=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


