import argparse from pathlib import Path import torch from diffusers import UNet2DConditionModel from PIL import Image from transformers import CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer import creaprompt_sdxl_dwd as dwd parser = argparse.ArgumentParser() parser.add_argument("--prompt", required=True) parser.add_argument("--seed", type=int, default=42) parser.add_argument("--width", type=int, default=1024) parser.add_argument("--height", type=int, default=1024) parser.add_argument("--output", default="image.png") args = parser.parse_args() dwd.check_size(args.width, args.height) root = Path(__file__).resolve().parent device = torch.device("cuda" if torch.cuda.is_available() else "cpu") dtype = (torch.bfloat16 if device.type == "cuda" and torch.cuda.is_bf16_supported() else torch.float32) unet = UNet2DConditionModel.from_pretrained( root / "unet", torch_dtype=dtype, use_safetensors=True, local_files_only=True, ).to(device) model = dwd.SDXLDWD(unet).eval() state = torch.load(root / "dwd_inference.pt", map_location="cpu", weights_only=True) model.tuned.load_state_dict(state["ema"], strict=True) tokenizers = tuple( CLIPTokenizer.from_pretrained(root / name, local_files_only=True) for name in ("tokenizer", "tokenizer_2") ) encoders = tuple( cls.from_pretrained(root / name, torch_dtype=dtype, use_safetensors=True, local_files_only=True).to(device).eval() for cls, name in ( (CLIPTextModel, "text_encoder"), (CLIPTextModelWithProjection, "text_encoder_2"), ) ) ids = torch.tensor([[args.height, args.width, 0, 0, args.height, args.width]]) cond = dwd.text_embeddings(tokenizers, encoders, [args.prompt], ids) schedule = dwd.NormalSDESchedule(state["sampling_steps"], device) rgb = dwd.render(model, schedule, cond, args.width, args.height, args.seed) pixels = rgb[0].permute(1, 2, 0).clamp(-1, 1).add(1).mul(127.5).round().byte().cpu().numpy() Image.fromarray(pixels).save(args.output) print(f"Saved {args.output}")