Files
tiny-gpt/mm/split.py
T
liuhuajie 5d2d3a2fb5 feat(eval): 行为探针与统一训练报告
核心教训:loss 判断不了可控性,必须看行为。

- colour_eval: 色相统计(粗指标,会被场景带偏)
- probe_ctrl: 反事实换词,只改颜色词看 64 个 VQ 码变多少
- probe_simple / probe_body: 单轴 / 双轴贪心生成
- probe_cross: 颜色x车身 4x5 交叉积,唯一能照出“只会背格子”的工具
- probe_cf_tv: 词级 TV,测一个词进入预测的程度
- pos_eval: 图像码按位置分桶,区分 prompt 依赖与上下文续写
- stream_probe: 实测 4 路流耦合度(层 7-9 余弦 0.995+)
- train_report: 四层体检合成一条命令

chk/curve/split/distcheck/modecheck/vqres/smoke 为早期诊断小工具,一并归档。
2026-12-03 10:37:55 +08:00

24 lines
1.4 KiB
Python

import numpy as np, torch, torch.nn.functional as F
from model import GPT, TEXT_VOCAB, IMAGE_OFFSET, IMAGE_VOCAB
from train import get_batch_rows
raw = np.memmap("../data_mm/val.bin", dtype=np.uint16, mode="r").reshape(-1,320)
for p in ["out_mm2/ckpt_iter2000.pt","out_mm2/ckpt_iter6000.pt"]:
ck = torch.load(p, map_location="cuda", weights_only=False)
m = GPT(ck["cfg"]).cuda(); m.load_state_dict(ck["model"]); m.eval()
st = {"txt_n":0,"txt_l":0.0,"img_n":0,"img_l":0.0}
with torch.no_grad():
for _ in range(20):
x,y,_ = get_batch_rows(raw, 320, 32, "cuda", False)
with torch.amp.autocast("cuda", dtype=torch.bfloat16):
logits = m(x)["logits"].float()
l = F.cross_entropy(logits.view(-1,logits.size(-1)), y.reshape(-1), ignore_index=-100, reduction="none")
valid = (y.reshape(-1) != -100)
tg = y.reshape(-1)
is_img = valid & (tg >= IMAGE_OFFSET) & (tg < IMAGE_OFFSET+IMAGE_VOCAB)
is_txt = valid & (tg < TEXT_VOCAB)
st["img_n"] += is_img.sum().item(); st["img_l"] += l[is_img].sum().item()
st["txt_n"] += is_txt.sum().item(); st["txt_l"] += l[is_txt].sum().item()
print("%s text %.4f (ppl %.1f) image %.4f (ppl %.1f)" % (
p.split("/")[-1], st["txt_l"]/st["txt_n"], np.exp(st["txt_l"]/st["txt_n"]),
st["img_l"]/st["img_n"], np.exp(st["img_l"]/st["img_n"])))