Repository navigation
fix(hip): a prompt tile row with only NaN scores gives NaN - #5192
Conversation
9a8aaaf to
032decd
Compare
032decd to
efa39e3
Compare
|
Thanks, @AbrahamCete, for a small, well-argued fix and a test that fails without it. Confirmed on this head:
On your question: going from 255 to 1, with one known entry left, is the right call. Keeping #1821 open for the remaining zero-weight case and the all −inf row makes sense, and "Refs #1821" already says that. What's left is CI on this head (it's still a draft). We'll add the changelog line when it lands.
|
|
READY to land on this head. Reviewed at this head: finite inputs can't change (the new branch runs only while a row has seen nothing but -inf or NaN, and a masked key still gets +0); only a NaN score now carries NaN, as float64 does. This head's CI passed.
|
The 64-row prompt tiles (tf_fa_paged_*, tf_fa_wide_*) took p = 0 for every key of a row whose running max was still -inf, so a row that met a NaN score before any finite one wrote 0, or a finite value from its later tiles, where float64 gives NaN. Such a row now subtracts 0 from its scores: a masked key's p is still 0 and a NaN score's p is NaN. Finite rows keep their bits. A kernel test plants a NaN query row and a NaN first page and expects NaN in every dim of those rows, in both cache types, paged and flat. The all-NaN-row case leaves the tile's known gate-1 finding; the seen-key zero-weight case stays known, since skipping a zero-weight key's product gives a finite output where float64 gives NaN or inf. Refs ashhart#1821.
efa39e3 to
b5400ac
Compare
|
Thank you @AbrahamCete. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
What this changes
In the HIP prompt attention tiles (
tf_fa_paged_*,tf_fa_wide_*), a row whose scores are NaN before any finite onewrote 0 or a finite value, where float64 gives NaN. It now gives NaN. This is the first fix agreed on #1821. The second
fix, skipping a key's product in P×V when its weight is exactly 0, is left out, because it moves gate 1 away from
float64 instead of toward it (below). Its
knownentry stays, narrowed to the one case it still covers.The change (
zig/kernels/hip/attention/prefill.hpp, three lines infa_dot2). While a row's running max is still-inf, the tile set every weight to 0. Now it subtracts 0 from the scores, so a masked key's weight is still
exp(-inf) = +0 and a NaN score's weight is NaN, which then reaches the row's sum and output. The running max itself is
unchanged. float64's own max drops NaN too (
@max(-inf, NaN)is -inf), and the reference gets its NaN fromexp(NaN - max), the same route.
A row with finite inputs cannot take the new path: every stored row sees key 0, whose score is then finite, so its max
leaves -inf at the first key. Masked keys and padded rows still give +0, as before.
Gate 1 class differences on gfx1151, per bf16 prompt tile, out of 49,152 outputs:
mainb9e6025#2357 already removed 90 of the 91 zero-weight differences #1821 counted, so one remains. Skipping that key cannot reach
0. For a key the row sees, float64 gives NaN where the key's weight also underflows in float64 (the
reachplant) and+inf where the weight is tiny but positive (the
underplant). Skipping the product gives a finite value in bothcases, so it turns one difference into four. So the bar on #1821 is not fully met: gate 1 goes from 255 to 1, not to 0,
and one of the two
knownentries stays, reworded to that case alone. Is that acceptable, or would you rather keep #1821open for the remaining case?
One more case, not planted and not registered, exists before and after this change: a row whose every seen score is
-inf, such as an infinite query element against negative key elements, gives 0 where float64 gives NaN. Metal Flash
Next registers the same case. I left it out of this PR.
Speed. The tile has about 175-200 fewer instructions, with the same VGPRs, SGPRs and LDS. Timed on
tf_fa_paged_bf16at the 27B's shape in a standalone harness, in the order main, this head, this head, main, fiverounds: within noise at 2,048 tokens (-0.93%), and 1.49% faster at 8,192 tokens (the two main runs were 1.33% apart;
faster in 5 of 5 rounds). A kernel time, not a served receipt.
main67ae36c. Commit:atc overlap --json(ATC 0.9.6, 2026-11-26 08:36Z, base b9e6025, before the rebase onto 67ae36c): 16overlaps. Eight report a conflict: qwen27: serve the 27B pair, refuse what a pair cannot keep, and die with the peer #1434, Implement GLP control vectors with a fused Metal kernel for Qwen3.8-27B #1835, app: control vector picker and strength in Engine advanced #1951 and test(cuda): opt-in --jobs and --shard for kernel-contract #3093 share
CHANGELOG.md, where open pull requests addtheir lines under Unreleased; idol: BiRefNet background removal and preprocessing on Metal, so tf-idol-generate takes a raw photo (stacked on #1780) #1787, gemma4 Metal: Gemma 4 E4B (NVIDIA Nemotron 3.5 Bolt E4B) on M1 to M4, per-layer inputs and shared KV #2273, ricursive: serve Ripple-65B on two GB10s with up to 1M tokens of context #2522, ricursive: speed up Ripple-65B on two GB10s #2523 and test(cuda): opt-in --jobs and --shard for kernel-contract #3093 share
docs/kernel-contract.md, where this PRrewords the prompt tiles' known-findings paragraph. Whichever lands second rebases. Two merge clean, and six report unknown because the run is incomplete (limits on pull
requests, their files and time).
Checks on this head
main67ae36c. The kernel, test, contract and docs files are the same blobs as thepre-rebase commit b0f8436af, where the GPU checks below ran; the rebase changed only
CHANGELOG.md.python3 tools/zig/lean_check.py, on the base and on this head: 152 and 152, 0 new.zig build hip-kernel-test -Dhip=trueon gfx1151: 241 of 248 passed, 7 skipped, 0 failed. The test binaryrun directly at b0f8436af lists
attention_pin,tail_pin,plan_pin, the affine golden and the new test OK.and flat", fails with main's
prefill.hpp(head 0 row 0 dim 0: 0 where float64 gives nan) and passes at thishead. It is the fix's only guard: gate 1 reports this tile's findings as known, so the contract alone would not
notice a revert.
zig build teston a clean tree of b0f8436af's code: 13,948 of 14,051 passed, 102 skipped, 1 failed. The failure isstage_hip_test"every staged call on HIP memory equals the reference, whole and in parts", which fails onmainon this machine as well.
zig build test -Dhip=trueon the same tree: 1,498 of the 1,520 tests it ran passed, 21 skipped, and the samestage_hip_testfailed.zig fmt --check build.zig zigandgit diff --check: clean.tf_fa_full_bf16(the vision tile, which sharesfa_dot2) changed, with the same VGPRs, SGPRs and LDS and nospill or scratch; their arithmetic instruction counts are identical, and only compares, moves and branches differ.
The tool's only other FAIL lines are
affine_dot2_block<DotBF16, N>instantiations intiles/dot2.hip, whichdiffer between two builds of the same sources.
limit records are unchanged.
in the attention inputs.
Receipt
8,192 tokens of the 27B's shape, hash to 0x54585c6c2f506b11 and 0x96c8d1110e42d7ac on both.
gfx1201 limits only.
Kernel contract, only when a GPU kernel is new or changed
Kernels registered with the contract: unchanged registrations. The prompt tiles'
knownfinding now reads "aninfinite value at a key the row sees under fp32's least weight gives NaN where float64 gives inf, as 0 x inf enters
P x V".
Planted controls: unchanged, 74 of 74 red as planted.
Each backend's
SUMMARYline and exit status (gfx1151, ROCm 10.1's hipcc, HIP 7.16), the full contract:On
mainb9e6025:SUMMARY gfx1151 210 kernels: 210 pass gates 1-4, 0 fail, 2 known findings; 74 controls: 74 red as planted, 0 missed; 2 limit problems; gates 828503 ms, limits 2974 ms(exit 1)With this fix:
SUMMARY gfx1151 210 kernels: 210 pass gates 1-4, 0 fail, 2 known findings; 74 controls: 74 red as planted, 0 missed; 2 limit problems; gates 811154 ms, limits 2928 ms(exit 1)The exit status and the 2 limit problems are the same stale live records on
main(tf_moe_combine_gate,tf_vl_resize). The 2 known findings are this tile's remaining case, paged and wide; with this fix each shows1 of 49152 outputs differ in class(it was 255).Pins:
attention_pindigest 0xe9e35ddbb2a6ad10 onmainand with this fix, equal to its pinned value;tail_pin,plan_pinand the affine golden pass on both.zig build hip-kernel-contract-limits: the same 11 problems as onmain, all in other groups; the prompt tiles'records are current on the four chips.
Chips I could not run: gfx1030, gfx1100 and gfx1201 (limits only), and every CUDA SM and Metal chip.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (not run on a served model; finite bytes are pinned equal)--drafter, no slower than without it, greedy and sampled. (tile timing only, above)