from __future__ import annotations import argparse import os from typing import Optional import torch from blockdiffusion.diffusion.gaussian_diffusion import GaussianDiffusion from blockdiffusion.diffusion.schedules import get_beta_schedule from blockdiffusion.models.unet16 import UNet16, UNet16Config from blockdiffusion.models.text_unet16 import TextCondConfig, TextCondUNet16 from blockdiffusion.text.simple_tokenizer import SimpleVocab from blockdiffusion.train.guidance import CFGuidanceModel from blockdiffusion.utils.checkpoint import load_checkpoint from blockdiffusion.utils.ema import EMA from blockdiffusion.utils.image_io import save_tensor_grid from blockdiffusion.utils.seed import get_device, seed_everything def parse_args() -> argparse.Namespace: p = argparse.ArgumentParser(description="BlockDiffusion 16×16 纹理采样入口") p.add_argument("--ckpt", type=str, required=True, help="训练生成的 checkpoint 路径(.pt)") p.add_argument("--out_dir", type=str, default="samples", help="输出目录") p.add_argument("--n", type=int, default=256, help="生成张数") p.add_argument("--batch", type=int, default=256, help="单次采样 batch(受显存影响)") p.add_argument("--use_ema", action="store_true", help="使用 EMA 权重采样(推荐)") p.add_argument("--use_ddim", action="store_true", help="使用 DDIM 快速采样") p.add_argument("--ddim_steps", type=int, default=50) p.add_argument("--ddim_eta", type=float, default=0.0) p.add_argument("--prompt", type=str, default=None, help="提示词(仅条件模型可用)") p.add_argument("--cfg_scale", type=float, default=3.0, help="CFG scale(仅条件模型可用)") p.add_argument("--seed", type=int, default=123) p.add_argument("--device", type=str, default=None) return p.parse_args() @torch.no_grad() def main() -> None: args = parse_args() seed_everything(args.seed, deterministic=False) device = get_device(args.device) ckpt = load_checkpoint(args.ckpt, map_location="cpu") meta = ckpt.get("meta", {}) model_type = str(meta.get("model_type", "uncond")) unet_cfg_dict = meta.get("unet_cfg", None) if unet_cfg_dict is None: # 兼容:若旧 checkpoint 未保存配置,则使用默认 16×16 RGB 配置 unet_cfg = UNet16Config() timesteps = 1000 beta_schedule = "cosine" image_size = 16 channels = 3 model_type = "uncond" else: unet_cfg = UNet16Config(**unet_cfg_dict) diff_meta = meta.get("diffusion", {}) timesteps = int(diff_meta.get("timesteps", 1000)) beta_schedule = str(diff_meta.get("beta_schedule", "cosine")) data_meta = meta.get("data", {}) image_size = int(data_meta.get("image_size", 16)) channels = int(data_meta.get("channels", unet_cfg.in_channels)) vocab = None text_cfg = None if model_type == "text_cond": tok_dict = meta.get("tokenizer", None) text_cfg_dict = meta.get("text_cfg", None) if tok_dict is None or text_cfg_dict is None: raise ValueError("该 checkpoint 标记为 text_cond,但缺少 tokenizer/text_cfg") vocab = SimpleVocab.from_dict(tok_dict) text_cfg = TextCondConfig(**text_cfg_dict) model = TextCondUNet16(unet_cfg=unet_cfg, vocab=vocab, text_cfg=text_cfg).to(device) else: model = UNet16(unet_cfg).to(device) model.load_state_dict(ckpt["model"], strict=True) if args.use_ema and ("ema" in ckpt): ema = EMA(model, decay=0.9999) ema.load_state_dict(ckpt["ema"]) ema.copy_to(model) betas = get_beta_schedule(beta_schedule, timesteps).to(device) diffusion = GaussianDiffusion(betas) os.makedirs(args.out_dir, exist_ok=True) # 条件采样:如果提供 prompt 且模型支持,则启用 CFG sampler_model = model model_kwargs = None if args.prompt and model_type == "text_cond": if vocab is None or text_cfg is None: raise ValueError("内部错误:条件模型缺少 vocab/text_cfg") tok = torch.tensor([vocab.encode(args.prompt, max_len=text_cfg.max_text_len)], dtype=torch.long, device=device) m = (tok != vocab.pad_id) cond_kwargs = {"tokens": tok, "mask": m} un_tok = torch.full_like(tok, fill_value=vocab.pad_id) un_m = torch.zeros_like(m) uncond_kwargs = {"tokens": un_tok, "mask": un_m} sampler_model = CFGuidanceModel(model, cond_kwargs=cond_kwargs, uncond_kwargs=uncond_kwargs, scale=args.cfg_scale).to(device) model_kwargs = None elif args.prompt and model_type != "text_cond": raise ValueError("当前 checkpoint 是无条件模型,不支持 --prompt;请用带 captions 训练的模型") all_imgs = [] remaining = args.n while remaining > 0: b = min(args.batch, remaining) shape = (b, channels, image_size, image_size) if args.use_ddim: x = diffusion.ddim_sample_loop( sampler_model, shape=shape, device=device, steps=args.ddim_steps, eta=args.ddim_eta, model_kwargs=model_kwargs, progress=False, ) else: x = diffusion.p_sample_loop(sampler_model, shape=shape, device=device, model_kwargs=model_kwargs, progress=False) x = (x.clamp(-1, 1) + 1) * 0.5 all_imgs.append(x.cpu()) remaining -= b imgs = torch.cat(all_imgs, dim=0) out_path = os.path.join(args.out_dir, "samples.png") save_tensor_grid(imgs, out_path, nrow=int(max(1, args.n) ** 0.5), padding=1) if __name__ == "__main__": main()