



"""simple docstring"""
import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


__a      =				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")
__a      =				parser.parse_args()


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

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

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

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

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

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

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





"""simple docstring"""







from typing import Union

import fire
import torch
from tqdm import tqdm

def 				UpperCamelCase_    (    lowerCAmelCase__      :				str    ,   lowerCAmelCase__      :				str = "cpu"    ,   lowerCAmelCase__      :				Union[str, None] = None						)						->   None:

  """simple docstring"""




  lowerCAmelCase_   :				Any								= torch.load(lowerCAmelCase__    ,   map_location=lowerCAmelCase__						)
  for k, v in tqdm(state_dict.items()						):
    if not isinstance(lowerCAmelCase__    ,   torch.Tensor						):
      raise TypeError('FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin'						)
    lowerCAmelCase_   :				str								= v.half()
  if save_path is None:  # overwrite src_path
    lowerCAmelCase_   :				Dict								= src_path
  torch.save(lowerCAmelCase__    ,   lowerCAmelCase__						)


if __name__ == "__main__":
     fire.Fire(convert)





