怎么验证 agent 生成的 kernel 是正确的?
容差怎么定,谁说了算?
先弄清楚:每个概念是什么
- Reference
- 被替换掉的 PyTorch 实现。新 kernel 和它吃同样的输入,输出逐个元素比。
- 容差
- 允许的数值差。
assert_close的判定是 \(|a - e| \le \text{atol} + \text{rtol}\cdot|e|\),\(e\) 是 reference 的值。 - Bit-identical
- 输出每个 bit 都一样,容差是 0。
30 秒回答
I compare the kernel against the PyTorch reference across seeds, scaled and negated inputs, and shapes the agent never saw. If the rewrite keeps the order of operations, the output must be bit-identical. If it reorders floating point, I use a per-dtype tolerance, about 1.3e-6 relative for fp32 and 1.6e-2 for bf16, because one loose 1e-2 lets a precision downgrade pass. NaN and inf are checked separately. The harness decides, not tests the agent wrote, and it re-checks inside the timing loop. Random testing is evidence, not proof.
FlashVector 的 verify 四档
| 改动类型 | 判定标准 | 说明 |
|---|---|---|
| Deterministic rewrite | 输出逐元素相等 | 计算顺序和累加精度都没变 |
| 改变浮点顺序的 rewrite | 按 dtype 的容差 | FP16 serving 时,element-wise 偏差超过 10⁻³ 的元素最多 0.1%;FP32 serving 的门槛是 10⁻⁴;non-finite 的输出直接判不过 |
| 有意的近似 | business metrics | 输出按设计就不同 |
| 配置改动 | shadow-traffic canary | 预测值没变,验的是固定 latency SLO 下的表现 |
Verify 回放录下来的 artifact,也就是 model weights、有代表性的 input 和对应的 prediction。容差只能收紧,接受的阈值写回 model card。
自己搭 harness:测什么
| 测什么 | 怎么做 | 防什么 |
|---|---|---|
| 输入 | 多个 seed,输入再 ×3、×0.01、×(−1) | overflow、underflow、符号分支 |
| Shape | 多个 shape,含 agent 看不到的 hidden shape、非 2 的幂、tail tile | 只对测试 shape 走捷径(见 KernelBench) |
| 容差 | 按 dtype 设:fp32 rtol 约 1.3e-6,fp16 1e-3,bf16 约 1.6e-2 | precision downgrade |
| 特殊值 | NaN 和 inf 单独查 | assert_close 默认 equal_nan=False |
| 计时循环 | 每次迭代重新生成输入并重新校验(GPU MODE) | 第一次算对,之后跳过计算 |
| 整个模型 | whole-model parity,必要时含 backward | 组合后不一定对 |
| Memory | 比 peak memory | 写对的 kernel 里有 28% 提高了 peak memory(KernelBench-Verified,最好的模型) |
reference 的值是 100 时,fp32 的默认容差允许差 \(10^{-5} + 1.3\times10^{-6}\times100 = 1.4\times10^{-4}\)。KernelBench 原来 atol 和 rtol 都是 1e-2,允许差 \(0.01 + 0.01\times100 = 1.01\),宽了约 7000 倍,用 TF32 或 BF16 算的 kernel 也能过。
必答点
- 和 PyTorch reference 比:多个 seed、多种分布、hidden shape
- 计算顺序不变就要求 bit-identical,变了才按 dtype 给容差
- 一刀切 1e-2 会放过 precision downgrade
- NaN 和 inf 单独查
- 判定权在 harness,不在 agent 自己写的 test
- random test 不等于证明
追问
让 agent 自己写 test 行不行?
不行。KernelFalcon 让 LLM 生成 test harness,原文承认这是漏洞(见 reward hacking)。
random test 全过了,能说 kernel 一定对吗?
不能。KernelBench 把 formal verification 列为 future work,AlphaEvolve 的 FlashAttention 改动最后还是请人类专家确认。
FlashVector 的四档有哪些没写清?
Figure 3 把 GPU kernel 层写成 "bit-exact tensor compare",正文却允许改变浮点顺序的 rewrite 走容差。有意的近似由谁批准、看哪些 business metrics 也没写。
来源FlashVector §4.2 Verify、Figure 3 · PyTorch testing(assert_close 的默认容差)· KernelBench-Verified · KernelBench blog · GPU MODE eval.py · KernelFalcon · ml-八股 08 第 15 题 · 文献笔记 5.1 节