Spaces:
Sleeping
Sleeping
File size: 7,568 Bytes
c388140 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | from __future__ import annotations
"""
BlockDiffusion:CPU 推理/采样脚本(强制使用 CPU)
设计目标:
- 即使机器有 GPU,也只使用 CPU(避免 CUDA 初始化与显存占用)
- 复用项目现有的 ckpt 元信息/模型构建/EMA/CFG/扩散采样逻辑
注意:
- CPU 采样会明显更慢,建议配合 --use_ddim 与较小的 --ddim_steps / --batch
"""
import argparse
import os
from typing import Optional
# 必须在 import torch 之前设置,否则可能已经触发 CUDA 初始化
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "")
import torch
from blockdiffusion.diffusion.gaussian_diffusion import GaussianDiffusion
from blockdiffusion.diffusion.schedules import get_beta_schedule
from blockdiffusion.models.text_unet16 import TextCondConfig, TextCondUNet16
from blockdiffusion.models.unet16 import UNet16, UNet16Config
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 seed_everything
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description="BlockDiffusion CPU 采样入口(16×16 纹理)")
p.add_argument("--ckpt", type=str, required=True, help="训练生成的 checkpoint 路径(.pt)")
p.add_argument("--out_dir", type=str, default="samples_cpu", help="输出目录")
p.add_argument("--out_name", type=str, default="samples.png", help="输出文件名(建议 .png)")
p.add_argument("--n", type=int, default=16, help="生成张数(CPU 建议较小)")
p.add_argument("--batch", type=int, default=16, help="单次采样 batch(CPU 建议较小)")
p.add_argument("--threads", type=int, default=0, help="torch CPU 线程数(0 表示不设置)")
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, help="DDIM steps(越小越快)")
p.add_argument("--ddim_eta", type=float, default=0.0, help="DDIM eta(0=确定性)")
p.add_argument("--prompt", type=str, default=None, help="提示词(仅 text_cond 模型可用)")
p.add_argument("--cfg_scale", type=float, default=3.0, help="CFG scale(仅 text_cond 模型可用)")
p.add_argument("--seed", type=int, default=123, help="随机种子")
return p.parse_args()
def _set_cpu_threads(threads: int) -> None:
"""
可选设置 CPU 线程数,便于在不同机器上控制推理占用。
"""
threads = int(threads)
if threads <= 0:
return
try:
torch.set_num_threads(threads)
except Exception:
# 某些环境可能限制 set_num_threads;这里静默忽略即可
pass
try:
# inter-op 线程在部分版本/平台上可能不可设置
torch.set_num_interop_threads(threads)
except Exception:
pass
@torch.no_grad()
def main() -> None:
args = parse_args()
_set_cpu_threads(args.threads)
seed_everything(args.seed, deterministic=False)
# 强制 CPU
device = torch.device("cpu")
ckpt = load_checkpoint(args.ckpt, map_location="cpu")
meta = ckpt.get("meta", {}) or {}
model_type = str(meta.get("model_type", "uncond"))
unet_cfg_dict = meta.get("unet_cfg", None)
if unet_cfg_dict is None:
# 兼容:旧 checkpoint 未保存配置
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", {}) or {}
timesteps = int(diff_meta.get("timesteps", 1000))
beta_schedule = str(diff_meta.get("beta_schedule", "cosine"))
data_meta = meta.get("data", {}) or {}
image_size = int(data_meta.get("image_size", 16))
channels = int(data_meta.get("channels", unet_cfg.in_channels))
vocab: Optional[SimpleVocab] = None
text_cfg: Optional[TextCondConfig] = 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)
model.eval()
if args.use_ema and ("ema" in ckpt):
ema = EMA(model, decay=0.9999)
ema.load_state_dict(ckpt["ema"])
ema.copy_to(model)
model.eval()
betas = get_beta_schedule(beta_schedule, timesteps).to(device)
diffusion = GaussianDiffusion(betas)
# 条件采样:如果提供 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=float(args.cfg_scale),
).to(device)
model_kwargs = None
elif args.prompt and model_type != "text_cond":
raise ValueError("当前 checkpoint 是无条件模型,不支持 --prompt;请用带 captions 训练的模型")
os.makedirs(args.out_dir, exist_ok=True)
out_path = os.path.join(args.out_dir, args.out_name)
all_imgs = []
remaining = int(args.n)
while remaining > 0:
b = min(int(args.batch), remaining)
shape = (b, int(channels), int(image_size), int(image_size))
if args.use_ddim:
x = diffusion.ddim_sample_loop(
sampler_model,
shape=shape,
device=device,
steps=int(args.ddim_steps),
eta=float(args.ddim_eta),
model_kwargs=model_kwargs,
progress=True,
)
else:
x = diffusion.p_sample_loop(
sampler_model,
shape=shape,
device=device,
model_kwargs=model_kwargs,
progress=True,
)
x = (x.clamp(-1, 1) + 1) * 0.5
all_imgs.append(x.cpu())
remaining -= b
imgs = torch.cat(all_imgs, dim=0)
nrow = int(max(1, int(args.n)) ** 0.5)
save_tensor_grid(imgs, out_path, nrow=nrow, padding=1)
if __name__ == "__main__":
main()
|