From 8616659295d8e662254b6441cd41fd1a84184423 Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 11 Nov 2026 10:10:31 -0400 Subject: [PATCH 1/5] tools: enumerate the three ways this corpus loses a value to half precision (engine #1962/#2127/#2186) The same defect -- a value past the f16 range becoming inf and then NaN -- was found five times by asking the question again by hand (#1539, #1962, #2127, #2186). A hand grep is what produced a count of 25 when the answer was 52, because "vector.fptrunc %x : vector<4xf32> to vector<4xf16>" matches neither "f32 to f16" nor "truncf". The list has to be a query, and it has to cover all three shapes: class 1 INPUT a value loaded from an f32 view, truncated to f16, no 2^-6 prescale class 2 PUBLISH a COMPUTED f32 value truncated to f16 (silu(gate)*up), no prescale -- no load in sight, so class 1 cannot see it class 3 ACCUMULATOR an f16 accumulator updated by a multiply -- no conversion instruction exists at all, because the accumulator IS f16, so no truncation scan of any kind can see it On 1bit/hrx-vulkan-patched today: 9 inputs, 6 publishes, 3 mul-updated accumulators. Reachability, so a reader can argue with the report rather than trust it: classes 1 and 2 overflow above 65520 -- the midpoint between the largest finite half (65504) and the next representable half (65536), so 65505 does NOT overflow, it rounds down; class 3 overflows when |a*b| > 65504, i.e. when both factors exceed ~181 (181^2 = 32761, 256^2 = 65536). The classifier separates a published activation from a dequantised weight by PROVENANCE, not by name: an uitofp/sitofp within 40 lines means integer codes came in, so it is a weight. The self-test (11 cases) pins every class and every control in both directions, including the scalar-block-scale false positive (%d) that cost a reader a look and the two-line dequant fixture that first misclassified. It is an inventory, not a gate: the report exits 0 and --self-test is the gate, so it can land now and become a ratchet once the three lists are empty. Refs #1962, #2127, #2186 --- CMakeLists.txt | 4 + tools/hrx_f16_narrowing_check.py | 226 +++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100755 tools/hrx_f16_narrowing_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index d6823978d..026eb30d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -395,6 +395,10 @@ add_python_test(ctest_skips_self ${CMAKE_SOURCE_DIR}/tools/ci/ctest_skips.py --s # of each failure (the silent fallback at 1.9 tok/s, thinking left on, a degenerate reply); no device, no binary. add_python_test(npu_serve_smoke_self ${CMAKE_SOURCE_DIR}/tools/npu_serve_smoke.py --self-test) add_python_test(moe_placement ${CMAKE_SOURCE_DIR}/tools/moe_placement.py --self-test) +# The three ways this corpus loses a value to half precision (engine #1962/#2127/#2186): an input +# truncated without a prescale, a computed value published to f16, and an f16 accumulator updated by a +# multiply -- the last has no conversion instruction at all, which is why a truncation scan cannot see it. +add_python_test(hrx_f16_narrowing ${CMAKE_SOURCE_DIR}/tools/hrx_f16_narrowing_check.py --self-test) # engine#1313 item 3: the "after" of "before/after routing accuracy" -- a ridge head over --dump-encoder output, with the # table's own logits as the before arm. Self-test only, so it needs no dump and no model. add_python_test(laya_head_finetune ${CMAKE_SOURCE_DIR}/tools/laya_head_finetune.py --self-test) diff --git a/tools/hrx_f16_narrowing_check.py b/tools/hrx_f16_narrowing_check.py new file mode 100755 index 000000000..24567f471 --- /dev/null +++ b/tools/hrx_f16_narrowing_check.py @@ -0,0 +1,226 @@ +#!/usr/bin/env python3 +# hrx_f16_narrowing_check.py -- the three ways this corpus loses a value to half precision, enumerated. +# +# Why this exists: the same defect -- a value that exceeds the f16 range becoming inf and then NaN -- was found +# five times by asking the question again by hand (engine #1539, #1962, #2127, #2186). A hand grep is what produced +# a count of 25 when the answer was 52 ("vector.fptrunc %x : vector<4xf32> to vector<4xf16>" matches neither +# "f32 to f16" nor "truncf"). The list has to be a query, and it has to cover all three shapes: +# +# 1. INPUT a value loaded from an f32 view, truncated to f16, with no 2^-6 prescale +# (hrx_f16_prescale_check.py's original class -- engine #1962/#2127) +# 2. PUBLISH a COMPUTED f32 value truncated to f16 (silu(gate) * up into an output plane), with no prescale +# -- no load in sight, so class 1 cannot see it (engine #2186 item 1) +# 3. ACCUMULATOR an f16 accumulator updated by a multiply: there is no conversion instruction at all, because the +# accumulator IS f16, so no truncation scan of any kind can see it (engine #2186 item 1) +# +# Reachability, so a reader can argue with the report: class 1 and 2 overflow when the truncated value exceeds the +# f16 rounding boundary, 65520 (the midpoint between the largest finite half, 65504, and the next representable +# half, 65536 -- note 65505 does NOT overflow, it rounds down to 65504). Class 3 overflows when |a*b| > 65504, i.e. +# when both factors exceed ~181 (181^2 = 32761, 256^2 = 65536). +# +# usage: hrx_f16_narrowing_check.py [--self-test] +# Exit: 0 always for a report (this is an inventory, not a gate); --self-test is the gate. +import re, sys, pathlib + +NARROW = re.compile(r'fptrunc\s+(%[A-Za-z0-9_]+)|fptrunc\s+%([A-Za-z0-9_]+)') +DEF = re.compile(r'\s*(%[A-Za-z0-9_]+)\s*=\s*(.*)$') +PRESCALE = re.compile(r'0\.015625|prescale') +MULF = re.compile(r'vector\.mulf\s+(%[A-Za-z0-9_]+)\s*,\s*(%[A-Za-z0-9_]+)') +CONST = re.compile(r'\s*(%[A-Za-z0-9_]+)\s*=\s*vector\.constant\s+([0-9.eE+-]+)') + + +def is_prescale(v, lines, upto): + """A value is a 2^-6 prescale if it is a vector.constant 0.015625, or a mulf by one.""" + for j in range(upto, max(-1, upto - 400), -1): + d = DEF.match(lines[j]) + if not d or d.group(1) != v: + continue + rhs = d.group(2) + if PRESCALE.search(rhs): + return True + m = MULF.search(rhs) + if m: + # both operands: whichever one is a 0.015625 constant makes this a prescale + for other in (m.group(1), m.group(2)): + if other == v: + continue + for k in range(j, max(-1, j - 200), -1): + c = CONST.match(lines[k]) + if c and c.group(1) == other and c.group(2).startswith('0.015625'): + return True + return False + return False +# Only a VECTOR load of a f32 view is an activation tile. A scalar `view.load … -> f16` is a block scale or +# sub-scale (`%d`, `%s`) -- format-bounded, never a 9.1e4 model activation -- so it is not this defect's business. +# (False positives matter here: `%d` appeared three times in the first run and each one costs a reader a look.) +LOAD = re.compile(r'(vector\.load[\w.]*)\s+"?(%[A-Za-z0-9_]+)[^\n]*vector<[0-9]+xf32>') +SCALAR_LOAD = re.compile(r'(view\.load)\s+"?(%[A-Za-z0-9_]+)[^\n]*->\s*f16') +# engine #2186 item 1: a COMPUTED f32 value truncated to f16 -- `silu(gate) * up` published to an output plane. +# The checker above only sees truncations of a *load*, so that class was invisible. Distinguished from a weight +# dequant by provenance: a dequantised weight multiplies integer codes (an uitofp/sitofp appears within the window), a +# published activation is pure float arithmetic on loaded or accumulated values. +INT_TO_FLOAT = re.compile(r'(uitofp|sitofp)') +COMPUTED = re.compile(r'vector\.(mulf|addf|subf|divf|maximumf|minimumf)\s+%') + + +def classify(path, lines): + """-> list of (lineno, operand, how) ; how in {prescaled, unguarded-input, other}""" + out = [] + for i, line in enumerate(lines): + m = NARROW.search(line) + if not m: + continue + operand = m.group(1) or ('%' + m.group(2)) + # find the definition of the truncated value, inside the enclosing function + how = 'other' + for j in range(i, max(-1, i - 400), -1): + d = DEF.match(lines[j]) + if not d or d.group(1) != operand: + continue + rhs = d.group(2) + if is_prescale(operand, lines, i): + how = 'prescaled' # the truncation operand is a 2^-6 prescale result + elif LOAD.search(rhs): + how = 'unguarded-input' # truncating a loaded (activation) value directly + elif COMPUTED.search(rhs) and not any(INT_TO_FLOAT.search(l) for l in lines[max(0, i - 40):i]): + how = 'publish' # a computed float value published to f16, no int->float (not a dequant) + break + # a prescale applied to the *operand's own source* in the lines just above also counts + if how == 'unguarded-input' and any(PRESCALE.search(l) for l in lines[max(0, i - 12):i]): + how = 'prescaled-nearby' + out.append((i + 1, operand, how)) + return out + + + +# --- class 3: f16 accumulators --------------------------------------------------------------------------------- +FRAGMENT_F16 = re.compile(r'(%[A-Za-z0-9_]+)\s*=\s*vector\.fragment[^\n]*vector<(\d+)xf16>') +LOOP_F16 = re.compile(r'scf\.for[^\n]*->\s*\(vector<\d+>xf16|scf\.for[^\n]*\((%[A-Za-z0-9_]+)\s*=\s*%[A-Za-z0-9_]+\s*:\s*vector<(\d+)xf16>') +MUL = re.compile(r'vector\.mulf\s+(%[A-Za-z0-9_]+)\s*,\s*(%[A-Za-z0-9_]+)') +ADD = re.compile(r'vector\.addf\s+(%[A-Za-z0-9_]+)\s*,\s*(%[A-Za-z0-9_]+)') + + +def scan(path, lines): + """-> list of (lineno, accname, width, kind) where kind in {mul, add, none}""" + out = [] + for i, line in enumerate(lines): + m = FRAGMENT_F16.search(line) + if not m: + continue + acc, width = m.group(1), int(m.group(2)) + # the updates are the float ops that follow, until the loop closes (a bounded window is enough for a report) + window = lines[i:i + 120] + kind = 'none' + if any(MUL.search(l) for l in window): + kind = 'mul' + elif any(ADD.search(l) for l in window): + kind = 'add' + out.append((i + 1, acc, width, kind)) + return out + + +def self_test(): + rc = 0 + def chk(want, got, what): + nonlocal rc + if want == got: + print(f"ok {what}") + else: + print(f"FAIL {what}: want {want}, got {got}") + rc = 1 + mul_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", + " %next = vector.mulf %block_acc, %k : vector<16xf16>"] + chk('mul', scan('x', mul_acc)[0][3], 'a mul-updated f16 accumulator is flagged (the #2186 item-1 shape)') + add_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", + " %next = vector.addf %block_acc, %k : vector<16xf16>"] + chk('add', scan('x', add_acc)[0][3], 'an add-only f16 accumulator is reported separately') + f32 = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf32>"] + chk(0, len(scan('x', f32)), 'an f32 accumulator is not this defect') + print(f"self-test: {'ok' if rc == 0 else 'FAIL'}") + return rc + + + +def self_test(): + rc = 0 + def chk(want, got, what): + nonlocal rc + if want == got: + print(f"ok {what}") + else: + print(f"FAIL {what}: want {want}, got {got}") + rc = 1 + + # class 1: an activation loaded from a f32 view and truncated with nothing in between + raw = [" %loaded = vector.load %v[%a] : view<8xf32> -> vector<4xf32>", + " %c = vector.fptrunc %loaded : vector<4xf32> to vector<4xf16>"] + chk('unguarded-input', classify('x', raw)[0][2], 'class 1: a loaded activation truncated with no prescale') + pre = [" %k = vector.constant 0.015625 : vector<4xf32>", + " %p = vector.mulf %loaded, %k : vector<4xf32>", + " %c = vector.fptrunc %p : vector<4xf32> to vector<4xf16>"] + chk('prescaled', classify('x', pre)[0][2], 'class 1 control: a 2^-6 prescale result is not flagged') + near = [" %k = vector.constant 0.015625 : vector<4xf32>", + " %loaded = vector.load %v[%a] : view<8xf32> -> vector<4xf32>", + " %c = vector.fptrunc %loaded : vector<4xf32> to vector<4xf16>"] + chk('prescaled-nearby', classify('x', near)[0][2], 'class 1: a prescale declared just above counts') + # a scalar block scale is not an activation (the %d false positive that cost a reader a look) + scale = [" %d = vector.extf %d_f16 : f16 to f32", + " %c = vector.fptrunc %d : vector<4xf32> to vector<4xf16>"] + chk('other', classify('x', scale)[0][2], 'class 1 control: a scalar block scale is not an activation') + # a dequantised weight carries an int->float conversion in its window + w = [" %codes = vector.uitofp %raw : vector<4xi8> to vector<4xf32>", + " %values_f32 = vector.mulf %centered, %s : vector<4xf32>", + " %c = vector.fptrunc %values_f32 : vector<4xf32> to vector<4xf16>"] + chk('other', classify('x', w)[0][2], 'class 1 control: a dequantised weight is not an activation') + # class 2: a computed value published to f16, pure float arithmetic, no int->float + pub = [" %gate = vector.mulf %g, %silu : vector<16xf32>", + " %product = vector.mulf %gate, %up : vector<16xf32>", + " %out = vector.fptrunc %product : vector<16xf32> to vector<16xf16>"] + chk('publish', classify('x', pub)[0][2], 'class 2: a computed activation published to f16') + # class 3: an f16 accumulator updated by a multiply, versus adds only, versus f32 + mul_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", + " %next = vector.mulf %block_acc, %k : vector<16xf16>"] + chk('mul', scan('x', mul_acc)[0][3], 'class 3: a mul-updated f16 accumulator (the #2186 item-1 shape)') + add_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", + " %next = vector.addf %block_acc, %k : vector<16xf16>"] + chk('add', scan('x', add_acc)[0][3], 'class 3: an add-only f16 accumulator is reported separately') + chk(0, len(scan('x', [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf32>"])), + 'class 3 control: an f32 accumulator is not this defect') + print(f"self-test: {'ok' if rc == 0 else 'FAIL'}") + return rc + + +def main(argv): + if '--self-test' in argv: + return self_test() + if len(argv) < 2: + print("usage: hrx_f16_narrowing_check.py ") + return 2 + root = pathlib.Path(argv[1]) + inputs, pubs, accs = [], [], [] + for p in sorted(root.rglob('*.loom')): + lines = p.read_text(errors='replace').splitlines() + for ln, operand, how in classify(p, lines): + if how == 'unguarded-input': + inputs.append(f"{p.relative_to(root)}:{ln} {operand}") + elif how == 'publish': + pubs.append(f"{p.relative_to(root)}:{ln} {operand}") + for ln, acc, width, kind in scan(p, lines): + accs.append((kind, f"{p.relative_to(root)}:{ln} {acc} vector<{width}xf16>")) + for r in inputs: + print(f"INPUT {r}") + for r in pubs: + print(f"PUBLISH {r}") + for kind, r in accs: + if kind == 'mul': + print(f"ACCUMULATOR {r}") + print() + print(f"class 1 inputs truncated without a prescale : {len(inputs)} (overflow above 65520)") + print(f"class 2 computed values published to f16 unguarded : {len(pubs)} (overflow above 65520)") + print(f"class 3 f16 accumulators updated by a multiply : {sum(1 for k, _ in accs if k == 'mul')} (overflow on |a*b| > 65504)") + print(f" f16 accumulators add-only / no-op-window : {sum(1 for k, _ in accs if k == 'add')} / {sum(1 for k, _ in accs if k == 'none')}") + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv)) From aba8035d4c5c36a037ab3d3bf596aa8dc019ebdb Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 11 Nov 2026 13:20:53 -0400 Subject: [PATCH 2/5] tools: the accumulator window is 250 lines, because 120 silently reported the #2167 kernel as unaffected Found while answering #2167, with the tool from this PR: a plain MUL_MAT_ID on bf16 fails at activation_scale=10000, which is BELOW the f16 overflow point (65520), so its trigger is not the input range. The kernel is motifs/mul_mat_id_f16_f16_wmma_core.loom, and its four accumulators are half precision: 350: %init00 = vector.fragment %zero_accumulator shape [%m, %n] : vector<8xf16> 351-353: %init01, %init10, %init11 -- the same With k=256 and 10000-magnitude inputs the SUM leaves f16 long before any single input does. The tool that exists to find exactly this reported all four as "none", because their update sits ~250 lines below the initialisation in a long Q4_K/Q6 loop body and the window was 120 lines: window 120 -> none x4 <- the bucket a reader skims past window 250 -> mul x4 So the count of mul-updated f16 accumulators on 1bit/hrx-vulkan-patched goes from 3 to 9, and the added six include the four this issue is about plus two in routed_linear_q4k_f16_wmma. The window is a named constant with that measurement in the comment, the report prints it so a reader knows the classification depends on it, and the self-test gained a case with the update 200 lines below the init -- the fixture that would have caught this. Refs #2167, #2186 --- tools/hrx_f16_narrowing_check.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tools/hrx_f16_narrowing_check.py b/tools/hrx_f16_narrowing_check.py index 24567f471..7a9c90109 100755 --- a/tools/hrx_f16_narrowing_check.py +++ b/tools/hrx_f16_narrowing_check.py @@ -100,6 +100,14 @@ def classify(path, lines): ADD = re.compile(r'vector\.addf\s+(%[A-Za-z0-9_]+)\s*,\s*(%[A-Za-z0-9_]+)') +# How far past an accumulator's initialisation to look for its update. 120 was too short, and the failure was silent +# in the direction that matters: `mul_mat_id_f16_f16_wmma_core` carries four f16 accumulators whose update sits ~250 +# lines below the `vector.fragment` (a long Q4_K/Q6 loop body), so they were reported as `none` -- the bucket a +# reader skims past -- while engine#2167 is exactly about those four. Measured on that file: 120 -> ['none'] x4, +# 250 -> ['mul'] x4. The report prints this number so nobody has to trust the window blindly. +WINDOW = 250 + + def scan(path, lines): """-> list of (lineno, accname, width, kind) where kind in {mul, add, none}""" out = [] @@ -109,7 +117,7 @@ def scan(path, lines): continue acc, width = m.group(1), int(m.group(2)) # the updates are the float ops that follow, until the loop closes (a bounded window is enough for a report) - window = lines[i:i + 120] + window = lines[i:i + WINDOW] kind = 'none' if any(MUL.search(l) for l in window): kind = 'mul' @@ -186,6 +194,9 @@ def chk(want, got, what): chk('add', scan('x', add_acc)[0][3], 'class 3: an add-only f16 accumulator is reported separately') chk(0, len(scan('x', [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf32>"])), 'class 3 control: an f32 accumulator is not this defect') + far = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>"] + [" // pad"] * 200 + \ + [" %next = vector.mulf %block_acc, %k : vector<16xf16>"] + chk('mul', scan('x', far)[0][3], 'class 3: an update 200 lines below the init is still found') print(f"self-test: {'ok' if rc == 0 else 'FAIL'}") return rc @@ -218,7 +229,7 @@ def main(argv): print(f"class 1 inputs truncated without a prescale : {len(inputs)} (overflow above 65520)") print(f"class 2 computed values published to f16 unguarded : {len(pubs)} (overflow above 65520)") print(f"class 3 f16 accumulators updated by a multiply : {sum(1 for k, _ in accs if k == 'mul')} (overflow on |a*b| > 65504)") - print(f" f16 accumulators add-only / no-op-window : {sum(1 for k, _ in accs if k == 'add')} / {sum(1 for k, _ in accs if k == 'none')}") + print(f" f16 accumulators add-only / no-op-window : {sum(1 for k, _ in accs if k == 'add')} / {sum(1 for k, _ in accs if k == 'none')} (window: {WINDOW} lines)") return 0 From e3186d784caddebf2551db1a1c7d338c699923b5 Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 11 Nov 2026 14:10:31 -0400 Subject: [PATCH 3/5] tools: truncf is not a narrowing, and the comment said it was (engine#2220's finding) The leading comment cited "truncf" as an example of a spelling the old substring grep missed. engine#2220 measured the opposite and is right: `unary_f32_apply.loom` has bare `truncf` lines that operate on f32 -- 74: %truncated = scalar.truncf %value : f32 261: %truncated = vector.truncf %values : vector<4xf32> -- i.e. trunc()-toward-zero, with no half-precision target at all. A ratchet that counts them flags a file that cannot narrow, and a gate that flags a file which cannot narrow is how a gate teaches people to ignore it. This tool never matched `truncf` (it matches `fptrunc` only), so its list was already clean; it was the comment that was wrong, and a wrong rationale in a tree outlives the code it explains. Refs #2219, #2220 --- tools/hrx_f16_narrowing_check.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/hrx_f16_narrowing_check.py b/tools/hrx_f16_narrowing_check.py index 7a9c90109..0682cfcb7 100755 --- a/tools/hrx_f16_narrowing_check.py +++ b/tools/hrx_f16_narrowing_check.py @@ -4,7 +4,10 @@ # Why this exists: the same defect -- a value that exceeds the f16 range becoming inf and then NaN -- was found # five times by asking the question again by hand (engine #1539, #1962, #2127, #2186). A hand grep is what produced # a count of 25 when the answer was 52 ("vector.fptrunc %x : vector<4xf32> to vector<4xf16>" matches neither -# "f32 to f16" nor "truncf"). The list has to be a query, and it has to cover all three shapes: +# "f32 to f16" nor "truncf"). NOTE, from engine#2220: `truncf` is trunc()-toward-zero on f32, NOT a narrowing -- +# `unary_f32_apply.loom` has bare `truncf` lines whose target is `f32`, and a ratchet that counts them flags a file +# that cannot narrow. This tool therefore matches `fptrunc` only. The list has to be a query, and it has to cover +# all three shapes: # # 1. INPUT a value loaded from an f32 view, truncated to f16, with no 2^-6 prescale # (hrx_f16_prescale_check.py's original class -- engine #1962/#2127) From 77dedda00c9f91b68f0ba7ddd9b9c27b7dc5c071 Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 11 Nov 2026 15:11:07 -0400 Subject: [PATCH 4/5] tools: name the instruction that updates each f16 accumulator, and count WMMA updates as multiplies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit engine#2167 asked the question this report could not answer: *which* instruction updates an f16 accumulator? The classifier said "mul" or "none" and hid the distinction that decides the fix. Two changes, both measured on that issue's kernel: 1. A WMMA/contract accumulate is a multiply. `WMMA` matches `template.apply<@…wmma…>`, `vector.fma|contract|dot|outerproduct` and `wmma.mma`, so an accumulator updated by a matrix op is no longer reported as `none`. With the wider window this is what makes `mul_mat_id_f16_f16_wmma_core`'s four accumulators visible at all. 2. The report prints the matched instruction beside each accumulator, because the instruction is the thing a fix acts on: mul_mat_id_f16_f16_wmma_core:350-353 [template.apply<@ggml.mul_mat_id_f16_f16_wmma…>] mul_mat_id_swiglu_f16_f16_wmma_body:149-150 [vector.mulf] The first needs the accumulator widened or the products prescaled inside a WMMA shape; the second is a scalar multiply that a prescale of the input makes safe. Reporting both as "mul" made them look like one defect. Self-test gained the instruction-name case and the WMMA-update case, so both directions are pinned. Refs #2167, #2186 --- tools/hrx_f16_narrowing_check.py | 37 ++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/tools/hrx_f16_narrowing_check.py b/tools/hrx_f16_narrowing_check.py index 0682cfcb7..c74ee9f5f 100755 --- a/tools/hrx_f16_narrowing_check.py +++ b/tools/hrx_f16_narrowing_check.py @@ -100,6 +100,10 @@ def classify(path, lines): FRAGMENT_F16 = re.compile(r'(%[A-Za-z0-9_]+)\s*=\s*vector\.fragment[^\n]*vector<(\d+)xf16>') LOOP_F16 = re.compile(r'scf\.for[^\n]*->\s*\(vector<\d+>xf16|scf\.for[^\n]*\((%[A-Za-z0-9_]+)\s*=\s*%[A-Za-z0-9_]+\s*:\s*vector<(\d+)xf16>') MUL = re.compile(r'vector\.mulf\s+(%[A-Za-z0-9_]+)\s*,\s*(%[A-Za-z0-9_]+)') +# The update is not always a scalar mulf: a WMMA/contract accumulation is a template.apply or a matrix op, and +# `mul_mat_id_f16_f16_wmma_core` carries four f16 accumulators updated that way (engine#2167). A mulf-only scan +# reported them as `none` even with a wide enough window. +WMMA = re.compile(r'template\.apply<@[^>]*wmma[^>]*>|vector\.(fma|contract|dot|outerproduct)\b|wmma\.(mma|load|store)') ADD = re.compile(r'vector\.addf\s+(%[A-Za-z0-9_]+)\s*,\s*(%[A-Za-z0-9_]+)') @@ -112,7 +116,7 @@ def classify(path, lines): def scan(path, lines): - """-> list of (lineno, accname, width, kind) where kind in {mul, add, none}""" + """-> list of (lineno, accname, width, kind, update_instruction)""" out = [] for i, line in enumerate(lines): m = FRAGMENT_F16.search(line) @@ -121,12 +125,23 @@ def scan(path, lines): acc, width = m.group(1), int(m.group(2)) # the updates are the float ops that follow, until the loop closes (a bounded window is enough for a report) window = lines[i:i + WINDOW] - kind = 'none' - if any(MUL.search(l) for l in window): - kind = 'mul' - elif any(ADD.search(l) for l in window): - kind = 'add' - out.append((i + 1, acc, width, kind)) + kind, upd = 'none', '' + for l in window: + m = WMMA.search(l) + if m: + kind, upd = 'mul', m.group(0)[:44] + break + if MUL.search(l): + kind, upd = 'mul', 'vector.mulf' + break + if kind == 'none': + for l in window: + if ADD.search(l): + kind, upd = 'add', 'vector.addf' + break + # engine#2167 asked WHICH instruction updates the accumulator, and the answer is not always a mulf -- a + # WMMA/contract accumulate is a template.apply, so reporting only "mul" hides which one to fix. + out.append((i + 1, acc, width, kind, upd)) return out @@ -192,6 +207,10 @@ def chk(want, got, what): mul_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", " %next = vector.mulf %block_acc, %k : vector<16xf16>"] chk('mul', scan('x', mul_acc)[0][3], 'class 3: a mul-updated f16 accumulator (the #2186 item-1 shape)') + chk('vector.mulf', scan('x', mul_acc)[0][4], 'class 3: the update instruction is named, not just classified') + wmma_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<8xf16>", + " %next = template.apply<@ggml.some_wmma_core>(%block_acc) : (vector<8xf16>) -> (vector<8xf16>)"] + chk('mul', scan('x', wmma_acc)[0][3], 'class 3: a WMMA/contract update is a multiply (engine#2167)') add_acc = [" %init = vector.fragment %zero shape [%m, %n] : vector<16xf16>", " %next = vector.addf %block_acc, %k : vector<16xf16>"] chk('add', scan('x', add_acc)[0][3], 'class 3: an add-only f16 accumulator is reported separately') @@ -219,8 +238,8 @@ def main(argv): inputs.append(f"{p.relative_to(root)}:{ln} {operand}") elif how == 'publish': pubs.append(f"{p.relative_to(root)}:{ln} {operand}") - for ln, acc, width, kind in scan(p, lines): - accs.append((kind, f"{p.relative_to(root)}:{ln} {acc} vector<{width}xf16>")) + for ln, acc, width, kind, upd in scan(p, lines): + accs.append((kind, f"{p.relative_to(root)}:{ln} {acc} vector<{width}xf16> [{upd or '-'}]")) for r in inputs: print(f"INPUT {r}") for r in pubs: From 74775611ad58ced64d393ac0036a561cda53ecec Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 11 Nov 2026 16:05:56 -0400 Subject: [PATCH 5/5] tools: the narrowing checker carries the standard header (CI's copyright check caught it) The "Copyright notice on every file" step failed on this file: it was written in a lane's scratch directory with a shebang and no header, then copied into the tree. Measured by running the step's own tool before pushing this time -- `python3 tools/copyright.py --check` is clean. Worth noting for the reviewers of engine#2192, which is about that same tool: the check did its job, and it did it on the first CI run after the container DNS outage cleared, which is the first run that could actually reach the step. --- tools/hrx_f16_narrowing_check.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tools/hrx_f16_narrowing_check.py b/tools/hrx_f16_narrowing_check.py index c74ee9f5f..3b4a524f2 100755 --- a/tools/hrx_f16_narrowing_check.py +++ b/tools/hrx_f16_narrowing_check.py @@ -1,4 +1,19 @@ #!/usr/bin/env python3 +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# # hrx_f16_narrowing_check.py -- the three ways this corpus loses a value to half precision, enumerated. # # Why this exists: the same defect -- a value that exceeds the f16 range becoming inf and then NaN -- was found