diff --git a/tools/ci/hrx_f16_narrowing.py b/tools/ci/hrx_f16_narrowing.py index 4d4034a0f..5f7ad5185 100755 --- a/tools/ci/hrx_f16_narrowing.py +++ b/tools/ci/hrx_f16_narrowing.py @@ -24,7 +24,8 @@ This is the ratchet: - * every .loom body that narrows f32 to f16 (`f32 to f16` or `truncf`) is listed in + * every .loom body that narrows f32 to f16 (scalar `f32 to f16` or vector `vector to vector`) + is listed in tools/ci/hrx_f16_narrowing.txt, and a body that appears without a line, or a line whose body is gone, fails the check -- the two arms a ratchet needs (a list that can only grow cannot see a rename, engine#1728's lesson); @@ -41,11 +42,17 @@ """ import argparse import pathlib +import re import sys CORPUS = pathlib.Path("third_party/llama.cpp/ggml/src/ggml-hrx/kernel-corpus/kernels") BASELINE = pathlib.Path("tools/ci/hrx_f16_narrowing.txt") -NARROW = ("f32 to f16", "truncf") +# 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 +# not a narrowing to f16: `scalar.truncf %v : f32` rounds within f32 (unary_f32_apply.loom), and every truncf that +# does narrow names its f16 type, which the two patterns below already match (engine#2220's review). +NARROW = re.compile(r"f32 to f16|vector<[0-9x]*f32>\s+to\s+vector<[0-9x]*f16>") GUARD = ("scale_log2", "prescale") @@ -59,11 +66,29 @@ def narrowing_bodies(corpus): found = set() for path in corpus.rglob("*.loom"): body = path.read_text(encoding="utf-8", errors="replace") - if any(marker in body for marker in NARROW): + if NARROW.search(body): found.add(path.relative_to(corpus).as_posix()) return found +# Every `fptrunc` narrows to something, so one whose type pair neither NARROW nor KNOWN_NON_F16 recognises is a +# spelling this tool does not know yet. It fails rather than passing silently, the way the vector form did +# (engine#2219): a new spelling is classified by extending one of the two patterns, never by omission. +FPTRUNC = re.compile(r"\bfptrunc\b") +KNOWN_NON_F16 = re.compile(r"\bbf16\b|xbf16>") + + +def unclassified_truncations(corpus): + """`fptrunc` lines that are neither an f32 -> f16 narrowing nor a known non-f16 target, as (path:line, text).""" + out = [] + for path in sorted(corpus.rglob("*.loom")): + text = path.read_text(encoding="utf-8", errors="replace") + for number, line in enumerate(text.splitlines(), 1): + if FPTRUNC.search(line) and not NARROW.search(line) and not KNOWN_NON_F16.search(line): + out.append((f"{path.relative_to(corpus).as_posix()}:{number}", line.strip())) + return out + + def guarded(bodies, corpus): """Bodies that also carry a producer-side guard marker -- reported, never asserted.""" out = set() @@ -88,14 +113,19 @@ def check(root): found = narrowing_bodies(corpus) new, stale = drift(baseline, found) guard = guarded(found, corpus) + unknown = unclassified_truncations(corpus) + for where, text in unknown: + print(f"hrx_f16_narrowing: UNCLASSIFIED fptrunc at {where}: {text} -- its type pair matches neither the f16 " + f"patterns nor a known non-f16 target; extend NARROW or KNOWN_NON_F16 (engine#2219)") for rel in new: print(f"hrx_f16_narrowing: NEW narrowing body {rel} -- it narrows an f32 value to f16 and the " f"baseline does not name it; add the line and say what feeds it (engine#1962)") 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: - print(f"hrx_f16_narrowing: FAIL ({len(new)} new, {len(stale)} stale of {len(baseline)})") + if new or stale or unknown: + print(f"hrx_f16_narrowing: FAIL ({len(new)} new, {len(stale)} stale of {len(baseline)}, " + f"{len(unknown)} unclassified fptrunc)") 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" + @@ -139,6 +169,23 @@ def check_that(ok, what): (corpus / "c.loom").write_text(" %z = scalar.fptrunc %w : f32 to f16\n") new, stale = drift(baseline_paths((root / BASELINE).read_text()), narrowing_bodies(corpus)) check_that(new == ["c.loom"] and not stale, "a new narrowing body is drift, and only it") + (corpus / "v.loom").write_text(" %h = vector.fptrunc %v : vector<4xf32> to vector<4xf16>\n") + (corpus / "w.loom").write_text(" %h = vector.fptrunc %v : vector<4xf32> to vector<4xbf16>\n") + new, _ = drift(baseline_paths((root / BASELINE).read_text()), narrowing_bodies(corpus)) + check_that(new == ["c.loom", "v.loom"], + "a vector<4xf32> to vector<4xf16> narrowing is found, and a bf16 one is not") + (corpus / "t.loom").write_text(" %t = scalar.truncf %v : f32\n %u = vector.truncf %w : vector<4xf32>\n") + new, _ = drift(baseline_paths((root / BASELINE).read_text()), narrowing_bodies(corpus)) + check_that("t.loom" not in new, "a truncf that stays in f32 is not a narrowing") + check_that(unclassified_truncations(corpus) == [], + "an f16 and a bf16 fptrunc are both classified") + (corpus / "x.loom").write_text(" %h = vector.fptrunc %v : vector<[4]xf32> to vector<[4]xf16>\n") + check_that([w for w, _ in unclassified_truncations(corpus)] == ["x.loom:1"], + "an fptrunc spelling the patterns do not know fails as unclassified, not silently") + (corpus / "x.loom").unlink() + (corpus / "v.loom").unlink() + (corpus / "w.loom").unlink() + (corpus / "t.loom").unlink() (corpus / "c.loom").write_text(" %z = scalar.constant 0.0 : f32\n") (corpus / "a.loom").rename(corpus / "renamed.loom") new, stale = drift(baseline_paths((root / BASELINE).read_text()), narrowing_bodies(corpus)) diff --git a/tools/ci/hrx_f16_narrowing.txt b/tools/ci/hrx_f16_narrowing.txt index b7e67e613..2c6eba434 100644 --- a/tools/ci/hrx_f16_narrowing.txt +++ b/tools/ci/hrx_f16_narrowing.txt @@ -13,29 +13,52 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# Targets whose add_test() names them and which CI never builds (engine#1595). -# -# engine#1962: the .loom bodies that narrow an f32 value to f16, at llama.cpp pin 7a1f2b81c. +# engine#1962: the .loom bodies that narrow an f32 value to f16 (scalar or vector form), at llama.cpp pin 2b7960bae. # Paths are relative to third_party/llama.cpp/ggml/src/ggml-hrx/kernel-corpus/kernels. This is a # RATCHET, not a list of bugs: a body here may narrow a bounded value (a scale, a weight) and be # perfectly correct. What the list buys is that a NEW narrowing body cannot appear without someone # writing it down and saying what feeds it -- and that is the open question on engine#1962: which of # these are protected by a producer-side prescale cannot be answered by reading them. +loom-libs/motifs/bf16_f16.loom +loom-libs/motifs/dequant.loom +loom-libs/motifs/dequant_1bit.loom +loom-libs/motifs/dequant_prism.loom +loom-libs/motifs/f32_f16.loom +loom-libs/motifs/llm_attention_qkv_matmul_postops.loom +loom-libs/motifs/mul_mat_f32_f32_wmma_core.loom loom-libs/motifs/mul_mat_id_f16_f16_wmma_core.loom +loom-libs/motifs/mul_mat_id_f32_f32_wmma_core.loom loom-libs/motifs/mul_mat_id_q4k_f16_wmma_projection.loom loom-libs/motifs/mul_mat_id_q5k_f16_wmma_projection.loom +loom-libs/motifs/mul_mat_id_q6k_f16_wmma_projection.loom +loom-libs/motifs/mul_mat_id_swiglu_f16_f16_accumulate.loom +loom-libs/motifs/mul_mat_id_swiglu_f16_f16_wmma_body.loom +loom-libs/motifs/mul_mat_quantized_f16_prefill.loom +loom-libs/motifs/publish_f32.loom loom-libs/motifs/q4_k_f16.loom +loom-libs/motifs/q6_k_f16.loom +loom-libs/motifs/q8_0_f16.loom +loom-libs/motifs/q8_1_f16.loom loom-libs/motifs/quantize_q8_1_x4.loom -loom-libs/motifs/unary_f32_apply.loom +loom-libs/ops/copy_f32.loom loom-libs/ops/flash_attention_decode_split_f32_f16_wmma.loom loom-libs/ops/flash_attention_f32_f16_wmma.loom loom-libs/ops/gated_delta_net_f32_wmma.loom +loom-libs/ops/llm_attention_k_matmul_rope_set_rows_decode_f32_f32.loom loom-libs/ops/llm_attention_v_matmul_set_rows_decode_f32_f32.loom +loom-libs/ops/mul_mat_id_swiglu_f32_f32_wmma.loom +loom-libs/ops/mul_mat_q6_k_packed_f16_wmma.loom +loom-libs/ops/mul_mat_swiglu_f32_f32_wmma.loom loom-libs/ops/mul_mat_swiglu_symmetric_i4_wmma.loom +loom-libs/ops/mul_mat_symmetric_i4_wmma.loom loom-libs/ops/mul_mat_symmetric_i8_wmma.loom +loom-libs/ops/rmsnorm_binary_f32.loom +loom-libs/ops/rope_set_rows_f32.loom loom-libs/ops/set_rows.loom loom-libs/ops/small_rows_f32.loom +qwen_moe/ggml/linear_q6k_q8_1_x4.loom qwen_moe/ggml/quantize_q8_1_x4.loom +qwen_moe/qwen3_moe/attention_postprocess_f32_f16.loom qwen_moe/qwen3_moe/attention_prepare_quantized.loom qwen_moe/qwen3_moe/dense_linear_quantized_f16_wmma.loom qwen_moe/qwen3_moe/flash_attention_decode_f32_f16_wmma.loom @@ -43,6 +66,7 @@ qwen_moe/qwen3_moe/flash_attention_decode_q128_f32_f16_wmma.loom qwen_moe/qwen3_moe/flash_attention_decode_split_f32_f16_wmma.loom qwen_moe/qwen3_moe/flash_attention_decode_split_next_q8_test.loom qwen_moe/qwen3_moe/flash_attention_f32_f16_wmma.loom +qwen_moe/qwen3_moe/routed_down_quantized_f16_wmma.loom qwen_moe/qwen3_moe/routed_down_weighted_reduce_next_rmsnorm_q8_1_x4.loom qwen_moe/qwen3_moe/routed_gate_up_swiglu_q4k.loom qwen_moe/qwen3_moe/routed_linear_q4k_f16_wmma.loom