From cf2528f3e3c66b997babef502f7811f6d9449046 Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 11 Nov 2026 17:52:22 -0400 Subject: [PATCH 1/2] ci: the ratchet also covers f16 accumulators, the half-precision shape no truncation scan can see (engine#2186, engine#2167) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ratchet in #2114/#2220 matches `fptrunc` -- a value converted to f16. That is the first two of the three shapes that lose a value to half precision, and it is blind to the third **by construction**: an f16 accumulator has no conversion instruction anywhere, because the accumulator already IS f16. The sum leaves 65,504 inside `vector.mulf`/a WMMA accumulate. engine#2167 is exactly that: a plain MUL_MAT_ID on bf16 fails at `activation_scale=10000`, which is well below the f16 overflow point, because 256 terms of that magnitude accumulate past it. engine#2186 item 1 is the same class in the routed gate/up. So this adds a second baseline beside the body list: ACC_INIT `vector.fragment … vector` ACC_MUL `vector.mulf`, `template.apply<@…wmma…>`, `vector.fma|contract|dot|outerproduct`, `wmma.mma` ACC_WINDOW 250 lines past the init The window is the part that matters and it is measured, not chosen: at 120 lines, `mul_mat_id_f16_f16_wmma_core`'s four `vector<8xf16>` accumulators read as unaffected -- their update sits ~250 lines below in a long Q4_K/Q6 loop body -- and a gate that reports a real overflow as unaffected is the failure mode this whole family keeps producing. The self-test has a fixture whose update is 200 lines below the init, plus a WMMA-update case and an add-only negative. The baseline is 7 files, and three of them are worth naming: `flash_attention_f32_f16_wmma.loom`, `flash_attention_decode_f32_f16_wmma.loom` and their qwen_moe siblings accumulate in f16 as well. For attention that is a *sum over the context*, so the longer the prompt the more room the accumulator needs -- a candidate this issue family had not had named for the long-context NaNs (#1536, #1346), and one the body-granularity ratchet could never have surfaced because attention's logits are f32 and its narrowing is not the thing overflowing. Verified end to end against a real corpus through a temp root: with the baseline, `51 narrowing bodies, none new, none stale; 7 f16 accumulator bodies, none new, none stale`; adding one new accumulator file fails with `NEW f16 accumulator … add the line and say what the accumulator holds`. Refs #2186, #2167 --- tools/ci/hrx_f16_accumulators.txt | 20 ++++++++ tools/ci/hrx_f16_narrowing.py | 82 +++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 tools/ci/hrx_f16_accumulators.txt diff --git a/tools/ci/hrx_f16_accumulators.txt b/tools/ci/hrx_f16_accumulators.txt new file mode 100644 index 000000000..d01f42c94 --- /dev/null +++ b/tools/ci/hrx_f16_accumulators.txt @@ -0,0 +1,20 @@ +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# f16 fragment accumulators updated by a multiply (engine#2186 item 1, engine#2167). +# +# This is the third shape that loses a value to half precision, and the only one a truncation scan cannot see: the +# accumulator IS f16, so there is no `fptrunc` to find. The sum over the reduction leaves 65,504 long before any single +# input does -- engine#2167's case fails at activation_scale=10000, well below the f16 overflow point, because k=256 +# terms of that magnitude accumulate past it. +# +# One line per .loom that has such an accumulator. `tools/ci/hrx_f16_narrowing.py --write` regenerates both this file +# and tools/ci/hrx_f16_narrowing.txt; a new entry is drift the ratchet refuses, and a line whose file no longer has a +# mul-updated f16 fragment is stale and should be dropped as the corpus is fixed. +loom-libs/motifs/mul_mat_id_f16_f16_wmma_core.loom +loom-libs/motifs/mul_mat_id_swiglu_f16_f16_wmma_body.loom +loom-libs/ops/flash_attention_f32_f16_wmma.loom +loom-libs/ops/mul_mat_q6_k_packed_f16_wmma.loom +qwen_moe/qwen3_moe/flash_attention_decode_f32_f16_wmma.loom +qwen_moe/qwen3_moe/flash_attention_f32_f16_wmma.loom +qwen_moe/qwen3_moe/routed_linear_q4k_f16_wmma.loom diff --git a/tools/ci/hrx_f16_narrowing.py b/tools/ci/hrx_f16_narrowing.py index 5f7ad5185..434fa081a 100755 --- a/tools/ci/hrx_f16_narrowing.py +++ b/tools/ci/hrx_f16_narrowing.py @@ -47,6 +47,16 @@ CORPUS = pathlib.Path("third_party/llama.cpp/ggml/src/ggml-hrx/kernel-corpus/kernels") BASELINE = pathlib.Path("tools/ci/hrx_f16_narrowing.txt") +# engine#2186 item 1 / engine#2167: a THIRD shape loses a value to half precision and no truncation scan can see it -- +# an f16 *accumulator* updated by a multiply. There is no `fptrunc` anywhere in it, because the accumulator already IS +# f16, so the body list above is blind to it by construction. Same mechanism as that list: a committed set of the files +# that have one, so a new one is drift. +ACC_BASELINE = pathlib.Path("tools/ci/hrx_f16_accumulators.txt") +ACC_INIT = re.compile(r"vector\.fragment[^\n]*vector<(\d+)xf16>") +ACC_MUL = re.compile(r"vector\.mulf|template\.apply<@[^>]*wmma[^>]*>|vector\.(fma|contract|dot|outerproduct)\b|wmma\.(mma|load|store)") +# Measured (engine#2167): at 120 lines past the init, `mul_mat_id_f16_f16_wmma_core`'s four accumulators read as +# unaffected, because their update sits ~250 lines below in a long Q4_K/Q6 loop body. 250 finds them. +ACC_WINDOW = 250 # The vector form is most of the corpus: `vector.fptrunc %x : vector<4xf32> to vector<4xf16>` contains neither # "f32 to f16" nor "truncf", and at llama.cpp 2b7960bae 132 of the 233 narrowing lines are written that way. # Matching the scalar spelling alone listed 25 bodies; the corpus has 51 (engine#2204's review). A bare `truncf` is @@ -89,6 +99,25 @@ def unclassified_truncations(corpus): return out +def accumulator_bodies(corpus): + """Every .loom with an f16 fragment accumulator updated by a multiply or a WMMA/contract accumulate. + + The window matters more than the matcher: the update of `mul_mat_id_f16_f16_wmma_core`'s four `vector<8xf16>` + accumulators sits ~250 lines below their `vector.fragment`, so a short window reports them as unaffected -- + the bucket a reader skims past -- and engine#2167 is exactly about those four. + """ + found = set() + for path in corpus.rglob("*.loom"): + lines = path.read_text(encoding="utf-8", errors="replace").splitlines() + for i, line in enumerate(lines): + if not ACC_INIT.search(line): + continue + if any(ACC_MUL.search(later) for later in lines[i:i + ACC_WINDOW]): + found.add(path.relative_to(corpus).as_posix()) + break + return found + + def guarded(bodies, corpus): """Bodies that also carry a producer-side guard marker -- reported, never asserted.""" out = set() @@ -112,6 +141,15 @@ def check(root): baseline = baseline_paths(base_text) found = narrowing_bodies(corpus) new, stale = drift(baseline, found) + # engine#2186 item 1 / engine#2167: the accumulator class, which no truncation scan can see. + acc_base_file = root / ACC_BASELINE + if not acc_base_file.is_file(): + print(f"hrx_f16_narrowing: {ACC_BASELINE} is missing -- run `--write` once to record the accumulators this " + f"corpus has (engine#2186)") + return 1 + acc_baseline = baseline_paths(acc_base_file.read_text(encoding="utf-8")) + acc_found = accumulator_bodies(corpus) + acc_new, acc_stale = drift(acc_baseline, acc_found) guard = guarded(found, corpus) unknown = unclassified_truncations(corpus) for where, text in unknown: @@ -123,12 +161,21 @@ def check(root): for rel in stale: print(f"hrx_f16_narrowing: {rel} is in the baseline but no longer narrows -- drop the line, the " f"list should shrink") - if new or stale or unknown: + for rel in acc_new: + print(f"hrx_f16_narrowing: NEW f16 accumulator {rel} -- an f16 fragment is updated by a multiply, so the sum " + f"overflows f16 long before any single input does and no fptrunc is involved; add the line and say what " + f"the accumulator holds (engine#2186, engine#2167)") + for rel in acc_stale: + print(f"hrx_f16_narrowing: {rel} is in the accumulator baseline but has no mul-updated f16 fragment -- drop " + f"the line, the list should shrink") + if new or stale or unknown or acc_new or acc_stale: print(f"hrx_f16_narrowing: FAIL ({len(new)} new, {len(stale)} stale of {len(baseline)}, " - f"{len(unknown)} unclassified fptrunc)") + f"{len(unknown)} unclassified fptrunc; {len(acc_new)} new, {len(acc_stale)} stale of " + f"{len(acc_baseline)} accumulator(s))") return 1 print(f"hrx_f16_narrowing: {len(baseline)} narrowing body(ies), none new, none stale; " - f"{len(guard)} of them carry a guard marker" + + f"{len(acc_baseline)} f16 accumulator body(ies), none new, none stale; " + f"{len(guard)} of the narrowing bodies carry a guard marker" + (f" ({', '.join(sorted(guard))})" if guard else " (the guard sits on the producer, engine#1962)")) return 0 @@ -142,7 +189,11 @@ def write(root): if line.lstrip().startswith("#")) bodies = sorted(narrowing_bodies(corpus)) (root / BASELINE).write_text(head + "\n".join(bodies) + "\n", encoding="utf-8") - print(f"hrx_f16_narrowing: wrote {len(bodies)} body(ies) to {BASELINE}") + accs = sorted(accumulator_bodies(corpus)) + acc_head = "".join(line for line in (root / ACC_BASELINE).read_text(encoding="utf-8").splitlines(True) + if line.lstrip().startswith("#")) if (root / ACC_BASELINE).is_file() else "" + (root / ACC_BASELINE).write_text(acc_head + "\n".join(accs) + "\n", encoding="utf-8") + print(f"hrx_f16_narrowing: wrote {len(bodies)} body(ies) to {BASELINE} and {len(accs)} to {ACC_BASELINE}") return 0 @@ -205,6 +256,29 @@ def check_that(ok, what): empty = root / "empty" empty.mkdir() check_that(check(empty) == 77, "a missing corpus returns 77, not 0") + # engine#2186 item 1 / engine#2167: the accumulator class. A mul-updated f16 fragment is found; an add-only one + # is not; a WMMA/contract update counts as a multiply; and an update far below the init is still found -- the + # last is the fixture that would have caught the 120-line window, which reported four real accumulators as + # unaffected. + accdir = root / "acc" + accdir.mkdir() + (accdir / "mul.loom").write_text( + " %init = vector.fragment %z shape [%m, %n] : vector<8xf16>\n" + " %next = vector.mulf %acc, %k : vector<8xf16>\n") + check_that(accumulator_bodies(accdir) == {"mul.loom"}, "a mul-updated f16 accumulator is found") + (accdir / "add.loom").write_text( + " %init = vector.fragment %z shape [%m, %n] : vector<16xf16>\n" + " %next = vector.addf %acc, %k : vector<16xf16>\n") + check_that("add.loom" not in accumulator_bodies(accdir), "an add-only f16 accumulator is not this class") + (accdir / "wmma.loom").write_text( + " %init = vector.fragment %z shape [%m, %n] : vector<16xf16>\n" + " %next = template.apply<@ggml.some_wmma_core>(%acc) : (vector<16xf16>) -> (vector<16xf16>)\n") + check_that("wmma.loom" in accumulator_bodies(accdir), "a WMMA/contract update counts as a multiply (engine#2167)") + (accdir / "far.loom").write_text( + " %init = vector.fragment %z shape [%m, %n] : vector<16xf16>\n" + + " // pad\n" * 200 + + " %next = vector.mulf %acc, %k : vector<16xf16>\n") + check_that("far.loom" in accumulator_bodies(accdir), "an update 200 lines below the init is found") print(f"hrx_f16_narrowing self-test: {'ok' if not failures else f'{failures} failure(s)'}") return 1 if failures else 0 From a7051bcde70beddaf661e408f5586edb626dd13a Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 11 Nov 2026 18:11:33 -0400 Subject: [PATCH 2/2] copyright: the accumulator baseline is data with a '#' style, like its sibling (CI caught this) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two failures in one CI step, both worth recording because both are the guard doing its job: 1. `no comment style known … tools/ci/hrx_f16_accumulators.txt` -- the tool refuses an unknown style rather than passing it (the behaviour its own comment praises). A `.txt` baseline is data, so it needs a line beside `hrx_f16_narrowing.txt` in the known-style list. 2. `missing copyright notice` -- my header had the copyright and SPDX lines plus an explanation and no licence body. `--fix` supplied it. Then `--fix` collapsed the blank `#` separator lines in my explanation (the defect engine#2228 fixes), so the paragraphs were rewritten by hand afterwards; the notice is unchanged and `copyright.py --check` is clean. That is the second and third time this guard has caught this lane tonight -- a missing header on a new .py, an unknown style on a new .txt, then a missing licence body -- and each time the right response was to run the step's own tool locally, which now takes ten seconds. --- tools/ci/hrx_f16_accumulators.txt | 28 +++++++++++++++++++++------- tools/copyright.py | 3 +++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tools/ci/hrx_f16_accumulators.txt b/tools/ci/hrx_f16_accumulators.txt index d01f42c94..e3158e67b 100644 --- a/tools/ci/hrx_f16_accumulators.txt +++ b/tools/ci/hrx_f16_accumulators.txt @@ -1,16 +1,30 @@ # 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. +# # f16 fragment accumulators updated by a multiply (engine#2186 item 1, engine#2167). # -# This is the third shape that loses a value to half precision, and the only one a truncation scan cannot see: the -# accumulator IS f16, so there is no `fptrunc` to find. The sum over the reduction leaves 65,504 long before any single -# input does -- engine#2167's case fails at activation_scale=10000, well below the f16 overflow point, because k=256 -# terms of that magnitude accumulate past it. +# This is the third shape that loses a value to half precision, and the only one a truncation scan +# cannot see: the accumulator IS f16, so there is no `fptrunc` to find. The sum over the reduction +# leaves 65,504 long before any single input does -- engine#2167's case fails at +# activation_scale=10000, well below the f16 overflow point, because k=256 terms of that magnitude +# accumulate past it. # -# One line per .loom that has such an accumulator. `tools/ci/hrx_f16_narrowing.py --write` regenerates both this file -# and tools/ci/hrx_f16_narrowing.txt; a new entry is drift the ratchet refuses, and a line whose file no longer has a -# mul-updated f16 fragment is stale and should be dropped as the corpus is fixed. +# One line per .loom that has such an accumulator. `tools/ci/hrx_f16_narrowing.py --write` +# regenerates this file and tools/ci/hrx_f16_narrowing.txt; a new entry is drift the ratchet +# refuses, and a line whose file no longer has a mul-updated f16 fragment is stale and should be +# dropped as the corpus is fixed. loom-libs/motifs/mul_mat_id_f16_f16_wmma_core.loom loom-libs/motifs/mul_mat_id_swiglu_f16_f16_wmma_body.loom loom-libs/ops/flash_attention_f32_f16_wmma.loom diff --git a/tools/copyright.py b/tools/copyright.py index 2e3888f6a..14ff7f5c9 100755 --- a/tools/copyright.py +++ b/tools/copyright.py @@ -51,6 +51,9 @@ # engine#2018: the test-registration baseline of `tests/*_test.py`, same shape and the same reason -- data, "unreferenced_py_tests.txt", "hrx_f16_narrowing.txt", + # engine#2186: the accumulator baseline of `tools/ci/hrx_f16_narrowing.py --write`, same shape and the same + # reason -- data, `.txt` has no comment style of its own, and the notice is already at the top of the file. + "hrx_f16_accumulators.txt", # engine#2027: the `#`-comment baseline for hardcoded /tmp paths, same shape and the same reason -- # data, `.txt` has no style, and the notice it wants is already at the top of the file. "tmp_paths.txt",