Skip to content

hip_f16_audit: count (_Float16) casts, the spelling the add-on actually uses (engine#2409) - #2415

Merged
bong-water-water-bong merged 1 commit into
mainfrom
audit-narrow-f16
Nov 14, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
audit-narrow-f16

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

What

NARROWING now counts a (_Float16)-cast alongside a (half)-cast. One arm, one spelling — and that spelling is the only one the add-on's kernels use.

Why

engine#2405's bump added three kernels where every cast is (_Float16):

file _Float16 occurrences __float2half narrowings reported before
fa_prompt_q8w.hip 10 0 0
moe_down_q4k.hip 2 0 0
moe_down_q6k.hip 4 0 0

fa_prompt_q8w.hip:70,154,273 · moe_down_q4k.hip:86 · moe_down_q6k.hip:88,190 — six casts, reported as zero. Since the summary line (0 narrowing(s)) is the only cast signal a reader gets for a file the Loom ratchet cannot see, "none found" was indistinguishable from "none present".

The residual, stated rather than absorbed

After the fix the tool reports 2 / 1 / 1 where a naive grep says 3 / 1 / 2. The difference is the cast of a numeric literal:

_Float16 h = (_Float16) 0.0f;      // not counted
_Float16 h = (_Float16) value[k];  // counted

Both cast arms — this one and the pre-existing (half) arm — require an identifier or ( after the cast, so constants are excluded. That is the right reading: a constant conversion is not an f32 value being narrowed. But it was implicit and undocumented, so it is now in the docstring and pinned by a self-test, where before it was folklore that would make the next person file the same bug I did.

Measured, before and after, on the real kernels

before   fa_prompt_q8w 0 · moe_down_q4k 0 · moe_down_q6k 0    total 0
after    fa_prompt_q8w 2 · moe_down_q4k 1 · moe_down_q6k 1    total 4

And the half that mattered is untouched: a fixture with _Float16 acc = 0; … acc += (_Float16)x[i]; still FAILs as an f16 accumulator, which is the property that overflows. --self-test gains two fixtures plus the fixture-count assertions updated for them.

Refs engine#2409 (where this was found and the accumulator half was fixed), engine#2406, engine#2405.

…ly uses

The narrowing pattern knew __float2half*, (half)x and half(x). Every cast in the three kernels engine#2405 added is
(_Float16) -- 10 / 2 / 4 occurrences of the token, __float2half zero times -- so the audit reported "0 narrowing(s)"
over files with six, and the summary line was the only cast signal a reader gets for a file the Loom ratchet cannot
see. "None found" reads exactly like "none present" (engine#2409).

  NARROWING  now accepts a (_Float16)-cast alongside a (half)-cast, in the same arm.

Residual count, stated rather than absorbed: on those kernels the tool now reports 2 / 1 / 1 against a naive grep's
3 / 1 / 2. The difference is the cast of a NUMERIC LITERAL: "(_Float16) 0.0f". The cast arms (both this one and the
existing (half) arm) require an identifier or '(' after the cast, so constants are excluded, which is the right
reading -- a constant conversion is not an f32 value being narrowed. That behaviour was implicit and undocumented,
so it is now in the docstring and pinned by a self-test rather than left as folklore.

--self-test: two new fixtures (f116.hip, f116lit.hip) plus the fixture-count assertions updated to match.

Verified on the real kernels, before/after:
  before  fa_prompt_q8w 0 . moe_down_q4k 0 . moe_down_q6k 0   (total 0)
  after   fa_prompt_q8w 2 . moe_down_q4k 1 . moe_down_q6k 1   (total 4)
and the f16-accumulator case still FAILs, so the half that mattered is untouched.
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing _Float16 support in regex

The regular expression for detecting f16 narrowings was updated to include (_Float16) casts, but the pattern does not properly handle the case where a numeric literal is cast to _Float16. The regex should ensure that only casts of identifiers or expressions (not literals) are counted as narrowings. This was addressed in the updated regex and confirmed with new test cases.

NARROWING = re.compile(r"__float2half\w*\s*\(|\(\s*(?:(?:__)?half|_Float16)\s*\)\s*[A-Za-z_(]"   # (half)x, (_Float16)x
                       r"|\b(?:__)?half\s*\(\s*[A-Za-z_]")                                                   # half(x)
Test case for numeric literal cast

A new test case (f116lit.hip) was added to verify that casting a numeric literal like (_Float16) 0.0f is not counted as a narrowing. This ensures that the tool correctly distinguishes between constant conversions and actual value narrowing, which is important for accurate auditing.

(kernels / "f116.hip").write_text("_Float16 h = (_Float16) value[k];\n")
(kernels / "f116lit.hip").write_text("_Float16 h = (_Float16) 0.0f;\n")

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY -- the second spelling is the one that would have been missed

Counting (_Float16) alongside the vector casts is the coverage fix, not a tidy-up: a body can narrow with a scalar cast and never mention f32 to f16, so an audit that only knows the vector spelling reports a clean tree for exactly the code an add-on author writes by hand. That is the same lesson the Loom ratchet learned in its own NARROW regex -- a bare truncf is not a narrowing, and a narrowing does not have to be spelled as a vector op.

The risk this removes is specific: the add-on is where new .hip lands under pin bumps, and a hand-written scalar cast is more likely there than in generated code.

One ask: if the audit can distinguish the two spellings in its output (vector vs scalar), worth a count each rather than a total -- a tree with 40 scalar casts and 0 vector ones is telling you something a combined number hides.

Nothing else: +15/-4, and it is the same shape as #2406's scope line and #2409's corpus statement: the tool learning what it was not looking at.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Both of these are READY, and together they are the best pair tonight — each one demonstrates a premise instead of asserting it, and each one was written because an earlier version of itself could pass without testing anything.

#2415 — (_Float16) is the spelling the add-on's kernels actually use, and the old pattern only knew half/__half, so it reported zero of them. Two assertions make that concrete: (_Float16) value[k] counts as a narrowing, (_Float16) 0.0f does not (a constant conversion, not an f32 value being narrowed), and the count moves 4 → 5 files with the two new fixtures. The second half is the part most regex changes get wrong — a cast of a literal is the shape that inflates a count and makes the next real one invisible — and requiring an identifier or ( after the cast is the right way to exclude it.

#2414 — the docstring is the whole argument and it is worth keeping verbatim: "a version of this test using the setUp history could not [tell the answers apart]: the branch carried the class [already], so a mutant that swapped the merge for a checkout still passed." Building the fixture so the branch is cut before the class exists means the two candidate implementations disagree on it — checkout loses it, merge keeps it — and a mutation of the merge would now fail. That is precisely the standard I have been asking other PRs to meet all night (#341, #351, #352, #366), applied to a test that exists to justify a relaxation rather than a fix.

One shared observation, since they are in the same area: both are correcting a checker whose output is consistent with "clean" and with "did not look" — the pattern I have recorded five times today (submodule_pin "0 compared", config_pin_check --online silence, the f16 ratchet's corpus scope, extend(dict) in pr_review_queue, and an audit that could scan the wrong tree). The distinguishing feature of these two is that the fixture now contains the thing that would otherwise be missed, so the check cannot pass by never matching.

Nothing blocking on either.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 8fc8d40

Small regex fix, and the two judgements inside it are exactly the ones an audit tool lives or dies by:

1. One arm per spelling, matching what the kernels actually use. (_Float16) alongside (half) — and the docstring says why only those spellings: "that spelling is the only one the add-on's kernels use." That is the right scope for a pattern that will be read as authoritative: match what exists, and name the boundary so the next spelling is an explicit addition rather than an assumed completeness.

2. The literal exclusion is the subtle part and it is correct:

the cast arms require an identifier or `(` after the cast, so a cast of a NUMERIC LITERAL
(`(_Float16) 0.0f`) is not counted: that converts a constant, not an f32 value

A cast of a constant cannot lose a value — it is a compile-time conversion of a literal, and counting it would inflate the audit with rows nobody can act on. Requiring [A-Za-z_(] after the cast is a clean way to separate the two, and stating the reason prevents the next person from "fixing" the apparent gap.

What I would add, and it is the same ask as #2411/#2413: a self-test for the new arm — one synthetic line per spelling that must be counted, and one literal cast that must not. A regex like this is exactly the kind of code that is correct for months and then silently stops matching after an unrelated edit; --self-test (which this file may already have) is where such a case belongs, and the literal-exclusion is the assertion most likely to be lost in a future refactor.

Also worth keeping from the description: the framing "engine#2405's bump added three …" — a tool change justified by what the pin bump revealed is a pin gate doing its job on itself.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at 8fc8d40

The before/after reproduces exactly, measured by checking the tool out at each side rather than by stashing (the change is a commit, so a stash measures nothing):

main's tool        fa_prompt_q8w 0 · moe_down_q4k 0 · moe_down_q6k 0      ) 94 (_Float16) casts in the tree
this PR            fa_prompt_q8w 2 · moe_down_q4k 1 · moe_down_q6k 1      ) 39 of them followed by a constant
whole tree total   3  ->  62 narrowings

And the residual is real, quantified, and stated instead of absorbed — which is the part worth crediting. The tool reports 62 where a naive grep -oE '\(_Float16\)' says 94, and the gap is the cast of a literal:

_Float16 h = (_Float16) 0.0f;      // not counted
_Float16 h = (_Float16) value[k];  // counted

I checked the arithmetic of that claim on the tree: 94 casts, 39 immediately followed by a digit, so the tool is deliberately skipping roughly the fraction the body says it is. Both cast arms require an identifier or ( after the cast, so a declaration (typedef _Float16 half16 …) is not counted either — which matters, because the add-on's vector types are declared exactly that way and counting them would have inflated the number further.

The half that mattered is untouched: an _Float16 acc still FAILs, and the self-test is green. Widening the narrowing arm cannot mask an accumulator, because the two properties are counted by different patterns and only one of them fails the run.

One observation, not a blocker: at 62 narrowings the closing line — "which are format writes unless a file says otherwise" — is a footnote over a table nobody will read in full. If the count keeps growing, a --top N or a per-directory rollup would keep the summary meaningful; today the per-file table is still short enough to scan.

Refs engine#2409 (the tool and the accumulator half), engine#2406 (the scope line this audit discharges), engine#2405 (the bump whose kernels exposed it).

@bong-water-water-bong
bong-water-water-bong merged commit 3ed0f63 into main Nov 14, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the audit-narrow-f16 branch November 14, 2026 09:39
bong-water-water-bong added a commit that referenced this pull request Nov 14, 2026
…#2417)

engine#2415 made the narrowing arm count (_Float16) casts, which on gpu-kernels
96593660718d is 62 rows across 68 kernel files. A closing line saying '3
narrowing(s), which are format writes unless a file says otherwise' was a footnote
under a table nobody reads in full, and the files worth looking at -- 11 and 10
narrowings in hrx_dense_prefill.hip and hrx_prefill_gemm.hip -- were buried
alphabetically. I raised this on #2415; this is the change.

  rows sort by (-narrowings, -accumulators, file)
  the first 20 print, then '... 48 more file(s), 4 narrowing(s) among 4 of them (--top 0 for every row)'
  --top N overrides, --top 0 prints every row (the pre-rollup behaviour)
  --json unchanged: still exactly the JSON, no rollup line

Measured on the real tree: the first four rows are hrx_dense_prefill 11,
hrx_prefill_gemm 10, gptoss_attn_sink_decode 4, gptoss_rope_kv_decode 3; --top 0
still prints 68 rows; --json still parses as 68.

Two arms added to --self-test, and the ctest row (hip_f16_audit_self) passes.

Co-authored-by: bong-water-water-bong <bong-water-water-bong@1bit.gg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant