Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tools/ci/hrx_f16_accumulators.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.
#
# 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
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
82 changes: 78 additions & 4 deletions tools/ci/hrx_f16_narrowing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<init>[^\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
Expand Down Expand Up @@ -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<init>`, 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()
Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -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<init> %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<init> %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<init> %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<init> %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
Expand Down
3 changes: 3 additions & 0 deletions tools/copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading