Skip to content

fix(hip): a prompt tile row with only NaN scores gives NaN - #5192

Merged
ashhart merged 2 commits into
ashhart:mainfrom
AbrahamCete:fix/hip-prompt-tile-nonfinite
Dec 1, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
AbrahamCete:fix/hip-prompt-tile-nonfinite

Conversation

@AbrahamCete

@AbrahamCete AbrahamCete commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

In the HIP prompt attention tiles (tf_fa_paged_*, tf_fa_wide_*), a row whose scores are NaN before any finite one
wrote 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 known entry stays, narrowed to the one case it still covers.

The change (zig/kernels/hip/attention/prefill.hpp, three lines in fa_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 from
exp(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:

Variant Differences
main b9e6025 255: 254 from the NaN query row, 1 from a seen key at zero weight
this fix 1: the seen key at zero weight
skipping zero-weight keys alone 260
this fix and skipping zero-weight keys 4

#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 reach plant) and
+inf where the weight is tiny but positive (the under plant). Skipping the product gives a finite value in both
cases, 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 known entries stays, reworded to that case alone. Is that acceptable, or would you rather keep #1821
open 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_bf16 at the 27B's shape in a standalone harness, in the order main, this head, this head, main, five
rounds: 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.

Checks on this head

  • Head commit: 032decd, on main 67ae36c. The kernel, test, contract and docs files are the same blobs as the
    pre-rebase commit b0f8436af, where the GPU checks below ran; the rebase changed only CHANGELOG.md.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 152 and 152, 0 new.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build hip-kernel-test -Dhip=true on gfx1151: 241 of 248 passed, 7 skipped, 0 failed. The test binary
      run directly at b0f8436af lists attention_pin, tail_pin, plan_pin, the affine golden and the new test OK.
    • The new test, "a row whose scores are NaN before any finite one gives NaN in every dim, in both cache types, paged
      and flat", fails with main's prefill.hpp (head 0 row 0 dim 0: 0 where float64 gives nan) and passes at this
      head. 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 test on a clean tree of b0f8436af's code: 13,948 of 14,051 passed, 102 skipped, 1 failed. The failure is
      stage_hip_test "every staged call on HIP memory equals the reference, whole and in parts", which fails on main
      on this machine as well.
    • zig build test -Dhip=true on the same tree: 1,498 of the 1,520 tests it ran passed, 21 skipped, and the same
      stage_hip_test failed.
    • zig fmt --check build.zig zig and git diff --check: clean.
    • A code-object comparison of b9e6025 and this fix on gfx1151: the 4 served prompt tiles, their 4 controls and
      tf_fa_full_bf16 (the vision tile, which shares fa_dot2) changed, with the same VGPRs, SGPRs and LDS and no
      spill 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 in tiles/dot2.hip, which
      differ between two builds of the same sources.
  • CI: not run yet on this head; opened as a draft.
  • Not run, and why:
    • Gate 1's NaN plant on the fp16 tiles: the contract plants it on the bf16 tiles only, and there is no fp16 pin.
    • Device runs of the d=128 and 16/2 geometries, and gfx1030, gfx1100 and gfx1201: I have only gfx1151, and their
      limit records are unchanged.
    • A served receipt: finite-input bytes are pinned equal, so replies change only where a NaN or infinity was already
      in the attention inputs.

Receipt

  • Main against branch: finite-input outputs equal byte for byte. The pins below, and the tile's outputs at 2,048 and
    8,192 tokens of the 27B's shape, hash to 0x54585c6c2f506b11 and 0x96c8d1110e42d7ac on both.
  • Boxes it reaches: every HIP family's prompt attention, only where a row meets a NaN score before any finite one.
  • New switch? No.
  • Environment: gfx1151 (Strix Halo), ROCm 10.1's hipcc (HIP 7.16), Zig 0.17.0.
  • Paths and platforms the change reaches, which I ran and which I could not: HIP gfx1151 run; gfx1030, gfx1100 and
    gfx1201 limits only.
  • Exactness: the byte pins below are unchanged.
  • Decode speed before and after: n/a, the prompt tile only.
  • Prompt speed before and after: the tile timing above; no served cold-prompt run.
  • Licence of any new weights or drafter: n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract: unchanged registrations. The prompt tiles' known finding now reads "an
    infinite 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 SUMMARY line and exit status (gfx1151, ROCm 10.1's hipcc, HIP 7.16), the full contract:

    On main b9e6025: 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 shows 1 of 49152 outputs differ in class (it was 255).

  • Pins: attention_pin digest 0xe9e35ddbb2a6ad10 on main and with this fix, equal to its pinned value; tail_pin,
    plan_pin and the affine golden pass on both.

  • zig build hip-kernel-contract-limits: the same 11 problems as on main, 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

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (not run on a served model; finite bytes are pinned equal)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled. (tile timing only, above)

@ashhart

ashhart commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Thanks, @AbrahamCete, for a small, well-argued fix and a test that fails without it. Confirmed on this head:

  • Finite inputs can't change. The new branch only runs while a row has seen nothing but −inf or NaN, and there a masked key still gets +0 exactly as before. Only a NaN score now carries NaN, as float64 does.
  • Main's later change. It also makes a non-causal row with any NaN score NaN, and the two fit together without conflict.

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.

  • Synthena

@ashhart

ashhart commented Dec 1, 2026

Copy link
Copy Markdown
Owner

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.

  • Synthena

AbrahamCete and others added 2 commits December 1, 2026 19:48
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.
@ashhart
ashhart force-pushed the fix/hip-prompt-tile-nonfinite branch from efa39e3 to b5400ac Compare December 1, 2026 19:48
@ashhart
ashhart merged commit b733e94 into ashhart:main Dec 1, 2026
9 checks passed
@ashhart

ashhart commented Dec 1, 2026

Copy link
Copy Markdown
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants