"""fp32 CPU reference: prompt -> 4-step LoRA sample -> image, saving every stage.""" from __future__ import annotations import argparse import time import numpy as np import torch from PIL import Image import dfl p = argparse.ArgumentParser() p.add_argument("--prompt", default="A snow leopard walking along a rocky ridge in falling snow, " "telephoto wildlife photograph, natural light") p.add_argument("--width", type=int, default=512) p.add_argument("--height", type=int, default=512) p.add_argument("--seed", type=int, default=7) p.add_argument("--out", default="ref_512") p.add_argument("--no-lora", action="store_true") a = p.parse_args() torch.set_num_threads(4) out = dfl.SP / "work" / a.out t0 = time.time() tok, enc = dfl.load_text_encoder() model, adapter = dfl.load_denoiser(lora=not a.no_lora) vae = dfl.load_vae() print(f"loaded in {time.time()-t0:.0f}s", flush=True) with torch.no_grad(): ids, cond = dfl.text_conditioning(tok, enc, adapter, a.prompt) prepared = model.prepare_text(dfl.TextConditioning(tokens=cond, key_bias=None, value_scale=None)) gh, gw = a.height // 16, a.width // 16 noise = torch.randn((1, 96, gh, gw), generator=torch.Generator("cpu").manual_seed(a.seed), dtype=torch.float32) ts = dfl.timesteps(a.width, a.height) gain = torch.tensor(dfl.gain_for(a.width, a.height), dtype=torch.float32) print("ids", len(ids), "times", ts.tolist(), "gain", float(gain), flush=True) x = noise.clone() vels = [] for i in range(len(ts) - 1): t1 = time.time() v = model(x, ts[i].expand(1), prepared, path=dfl.DFlowPath.FULL, self_attention_gain=gain, suffix_caption_bias=0.0, generator=None) vels.append(v.numpy().copy()) x = x + float(ts[i + 1] - ts[i]) * v print(f"step {i} t={float(ts[i]):.4f} {time.time()-t1:.1f}s |v|={v.std():.3f}", flush=True) t1 = time.time() refined = prepared.tokens.numpy().copy() del model, prepared import gc; gc.collect() rgb = vae.decode(x, a.height, a.width).clamp(-1, 1) print(f"decode {time.time()-t1:.1f}s", flush=True) img = ((rgb[0].permute(1, 2, 0).numpy() + 1) * 127.5).round().clip(0, 255).astype(np.uint8) Image.fromarray(img).save(f"{out}.png") np.savez(f"{out}.npz", ids=np.array(ids, np.int64), cond=cond.numpy(), refined=refined, noise=noise.numpy(), times=ts.numpy(), gain=np.float32(gain), vels=np.stack(vels), latent=x.numpy(), rgb=rgb.numpy()) print("saved", out, f"total {time.time()-t0:.0f}s")