From 8fc8d40712f2f5e930309369091becd0e97ba8fa Mon Sep 17 00:00:00 2001 From: bcloud Date: Sat, 14 Nov 2026 05:24:52 -0400 Subject: [PATCH] hip_f16_audit: count (_Float16) casts, the spelling the add-on actually 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. --- tools/ci/hip_f16_audit.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tools/ci/hip_f16_audit.py b/tools/ci/hip_f16_audit.py index 06da4a8ae..6afde46b1 100644 --- a/tools/ci/hip_f16_audit.py +++ b/tools/ci/hip_f16_audit.py @@ -23,7 +23,10 @@ Two properties, the same two the Loom ratchet counts: - narrowing -- an f32 value written into f16 (`__float2half*`, `(half)x`, `half(x)`) + narrowing -- an f32 value written into f16 (`__float2half*`, `(half)x`, `half(x)`, + `(_Float16)x`). 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 accumulator -- an f16 accumulator (`half acc`, `__half acc`, `half sum`, `half_t acc`) A narrowing is not automatically a defect: writing a block's `d` scale into a q4_0 block is the format, and @@ -49,7 +52,8 @@ KERNEL_SUBDIR = Path("addons/hip-hrx/kernels") # `__float2half`, `__float2half_rn`, `(half)v`, `half(v)`. Deliberately not `half x = ...`, which declares. -NARROWING = re.compile(r"__float2half\w*\s*\(|\(\s*(?:__)?half\s*\)\s*[A-Za-z_(]|\b(?:__)?half\s*\(\s*[A-Za-z_]") +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) # an accumulator declared in f16: `half acc`, `__half acc =`, `half_t acc`, and `acc` declared as such. # `half`, `__half`, `half_t`, `_Float16`, and the vector spellings these kernels actually use # (`half16`/`half8`/`half2`, `typedef _Float16 half16 __attribute__((ext_vector_type(16)))`). Measured on @@ -149,12 +153,19 @@ def chk(want, got, what): # the defect case (kernels / "f16acc.hip").write_text( "__half acc = 0;\nfor (int i = 0; i < n; ++i) acc = acc + __float2half(v[i]);\n") + # the `_Float16` spelling the add-on actually uses, and the numeric-literal cast beside it + (kernels / "f116.hip").write_text("_Float16 h = (_Float16) value[k];\n") + (kernels / "f116lit.hip").write_text("_Float16 h = (_Float16) 0.0f;\n") rows = audit(tmp) - chk(3, len(rows), "three kernels audited") + chk(5, len(rows), "five kernels audited") by = {row["file"]: row for row in rows} chk(1, by["format.hip"]["narrowings"], "the block scale is one narrowing") chk([], by["format.hip"]["accumulators"], "a narrowing alone is not an accumulator") chk(0, by["clean.hip"]["narrowings"], "fmaf accumulation narrows nothing") + # `_Float16` is the spelling the add-on's kernels actually use (engine#2405: every cast in its three new + # kernels is `(_Float16)`), so a pattern that only knew `half`/`__half` reported zero of them. + chk(1, by["f116.hip"]["narrowings"], "(_Float16)x is a narrowing") + chk(0, by["f116lit.hip"]["narrowings"], "(_Float16) 0.0f is a constant conversion, not a value narrowing") chk([], by["clean.hip"]["accumulators"], "a float accumulator is not f16") chk(["__half acc"], by["f16acc.hip"]["accumulators"], "the actual defect is found") bad = [row["file"] for row in rows if row["accumulators"]] @@ -182,7 +193,7 @@ def chk(want, got, what): rows = audit(tmp) by = {row["file"]: row for row in rows} chk(0, by["comment.hip"]["narrowings"], "a comment mentioning 'half (' is not a narrowing") - chk(6, len(rows), "every file in the tree is audited (4 originals + 2 vector cases)") + chk(8, len(rows), "every file in the tree is audited (4 originals + 2 vector cases + 2 _Float16 cases)") chk(None, audit("/nonexistent-path-xyz"), "an unreadable tree is None, not an empty pass")