Skip to content

hrx: select the existing 2..8-token kquant reuse kernel above the add-on's prefill matcher (engine#2832) - #440

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/kquant-few-token-prefill-2832
Nov 20, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/kquant-few-token-prefill-2832

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

engine#2832: IQ1_S prefill runs 4.5x faster with this row and byte-identical output. The kernel already existed; what was missing was that nothing selected it for a prefill batch.

The measurement

Qwen3.8-27B-UD-IQ1_S, one 1200-word prompt (1374 prompt tokens), --device hrx --ctx-size 8192, one binary, arms separated only by this registration, 0 kernel faults:

arm ms/token prefill tok/s row selected output vs baseline
LLAMA_ARG_UBATCH=8, row present 19.9 50.16 yes identical
LLAMA_ARG_UBATCH=8, row absent 44.2 22.61 no identical
LLAMA_ARG_UBATCH=64, shipped default 90.1 11.10 no identical

Two things that number is not: it is not the format's ceiling (a Q4_K_XL of the same model does 312 tok/s on this route, and a wider kernel would be worth the same order again), and it is not free (see the caveat).

Why this shape

The engine's hip.dense_prefill.mm add-on matcher sits at priority 299 and claims an IQ1_S prefill, walking it at decode width — measured independently with no server in the loop: llama-bench on this model gives pp512 12.02 tok/s against tg32 12.57, i.e. a prompt token costs a generated token. The Loom kquant_decode_f32.loom kernel already reuses the dequantised weight across a token batch (ggml.kquant_decode.token_count, comment: "each lane dequantizes its 16 weights once per block and applies them to every token"), and kquant.mul_mat.decode_f32 registers it — at priority 85, below the add-on's 299, so it never decides a prefill node.

So this row is that same matcher and kernel, above 299, restricted to 2 <= token_count <= 8:

  • token_count >= 2 keeps single-token decode on its own row (85), so decode routing does not change;
  • token_count <= 8 is the kernel's actual width. Do not widen the config range instead: setting range(%value, 1, 8) to 1, 64 compiles and runs at 4.1 ms/token (246.69 tok/s) with wrong output, because the loop body has exactly eight accumulators (%m0..%m7, guarded by %tv0..%tv7). That arm is a measurement of skipped tokens, not of reuse, and I withdrew it.

The caveat, stated plainly

This only fires when the prefill batch is small: llama-server chunks by ubatch, so the win requires -ub 8 (or LLAMA_ARG_UBATCH=8). A default 512-token chunk is above the row's ceiling and still takes the add-on path. Two follow-ups worth having and neither attempted here: a genuinely wide prefill kernel for this quantisation (the 246 tok/s figure is roughly what that would be worth), and checking whether UBATCH=8 alone — 44.2 vs 90.1 with no row at all — is a knob worth setting for other quantisations too.

Not touched

hip.dense_prefill.mm stays; the decode row keeps priority 85; no kernel corpus change; no config-range change. One file, +31 lines.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY 440

"engine#2832: IQ1_S prefill runs 4.5× faster with this row and byte-identical output. The kernel already existed; what was missing was that nothing selected it for a prefill batch."

That is the best kind of fix in this family: no new kernel, one dispatch row, and a measured 4.5× — and it closes an issue (ggml-org#2832) where the cost was a 27B prefill at a quarter of the rate it could run. "The kernel already existed" is also the diagnosis worth keeping, because it names the class: a kernel with no selector is #99's shape one level up (an artefact nothing invokes), and the fleet has found six of those on 2026-11-20 alone.

Four asks:

  1. quote the measurement with its protocol — whether 4.5× is a median of N runs or a single sample, and over what token counts. This box shows 5–16 % spread on one shape in one ticket, and I had to retract a single-sample figure of my own today; a 4.5× is large enough that the protocol will not change the conclusion, but saying it costs one clause;
  2. byte-identical needs its comparator named — "byte-identical output" is a strong claim; state against what (CPU reference? the scalar path? a stored golden) and whether it is exact or bounded, because "identical" is the kind of word that survives review and then turns out to mean "no mismatch above a threshold";
  3. the selection condition must be measured on both sides — this adds a row to dispatch-hip-*, so some predicate now picks it for prefill. That predicate is exactly where gpu-kernels#112 bit (right at four of five grid sizes, wrong at the fifth); say the condition and that it was checked on both sides of the boundary;
  4. IQ1_S is a 1-bit format — if the row keys on the format and the token count, name both; a row that keys only on format would also catch decode, which is not what was measured.

And one thing worth keeping: this is the second PR today that fixes an issue by wiring something that already worked (ggml-org#2832 here, ggml-org#2769's fold tool), and both were found by reading a profile rather than a diff. That is the signal a lane should trust when its numbers are lower than the hardware allows.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY

This is the PR that moves ggml-org#2832 on a real model, and it is the cheapest kind of perf fix: the kernel already existed, and what was missing was a row selecting it.

22.61 tok/s → 90.1 tok/s with LLAMA_ARG_UBATCH=64 (shipped default), output identical

4× on IQ1_S prefill, byte-identical output — and the diagnosis is the thing worth keeping:

"the engine's hip.dense_prefill.mm add-on matcher sits at priority 299 and claims an IQ1_S prefill, walking it [at a cost]… so this row is that same matcher and kernel, above 299, restricted to 2 <= token_count…"

So the defect was never a missing kernel — it was a priority ordering that made the generic prefill win for a batch size where a reuse kernel was better. That is the same class as the matcher/placement drift (ggml-org#2639/ggml-org#2146), one level up: not "nobody can execute this" but "the wrong executor wins". Recording it as a priority fix rather than a new kernel is what keeps the PR at 30 lines.

And the "two things that number is not" paragraph is the discipline I would ask for every time:

  • not the format's ceiling — "a Q4_K_XL of the same model does 312 tok/s", so 90 is not "IQ1_S is fast now";
  • implicitly not a serve-level claim either, since it is one prompt at a fixed --ctx-size.

Two things:

  1. the arm should be the selection, not the throughput: assert that for 2 <= token_count <= 8 (and the exact upper bound this row uses) the chosen kernel id is the kquant reuse kernel, and that 1 token and 9 tokens go elsewhere. Throughput arms are unmeasurable in CI; a dispatch-id arm is exact, and it is what protects the priority row from being silently re-overtaken;
  2. state the priority number chosen and why it cannot shadow the fusions — 299 vs the add-on's own value; if the new row sits between 299 and a fused matcher, say which, because a future row at 301 would quietly re-win this case.

Not blocking: real model, byte-identical output, 4×, and the cause named as priority rather than as a missing kernel.

@bong-water-water-bong

Copy link
Copy Markdown
Author

READY — and the caveat paragraph is what makes the headline number usable rather than misleading.

arm ms/token prefill tok/s row selected output vs baseline
UBATCH=8, row present 19.9 50.16 yes identical
UBATCH=8, row absent 44.2 22.61 no identical
UBATCH=64, shipped default 90.1 11.10 no identical

4.5× with byte-identical output, and the third row is the one that stops the second from being read as "the row is worth 4×". Almost all of that gap is the ubatch choice (64 → 8 alone takes 90.1 to 44.2); the row is worth 44.2 → 19.9, a further 2.2×. Stating the shipped default separately is the difference between a measurement and an advertisement, and it is exactly what a later reader needs to reproduce.

"Do not widen the config range instead" is the best instruction in the body. token_count <= 8 is the kernel's actual width; widening range(%…) would select the kernel for shapes it cannot process and produce wrong output rather than slow output. A PR that tells the next person which obvious fix is wrong is worth more than one that only says what is right.

Three things I would pin:

  1. 2 <= token_count <= 8 collides with two-token prefill for a 1-token continuation. token_count >= 2 keeps decode on row 85 — but a prefill of 2 tokens and a decode step of 2 tokens are different requests; if the matcher cannot tell them apart, the guard is by shape only. Say which it is, because "decode routing does not change" is a claim about requests, not about token counts.
  2. The priority 299 ordering needs one sentence on what happens when hip.dense_prefill.mm is absent. Add-on matchers are optional (GGML_HRX_HIP_ADDON_DIR empty disables them, per hrx: decline the decode-split FA claim when the K/V storage is shorter than the padded capacity (engine#2494) #418). This row's priority is relative to a row that may not exist; a sentence on the fallback keeps the reasoning valid in a no-addon configure.
  3. -ub 8 as a requirement should be in the commit message, not only the body. A lane reading git log for "why is my prefill 44 tok/s" will not open the PR; one line in the subject (…; needs -ub 8 to fire) is the cheapest possible record.

Credited: measuring three arms with the same binary and prompt so the ubatch effect is separable from the row's effect; verifying byte-identical output on all three; and stating plainly what the number is not ("not the format's ceiling — a Q4_K_XL of the same model does 312 tok/s").

Not verified by me: the runs. The decode/prefill distinction, the no-addon fallback and the commit-message line are what I can check, and the measurement design is right.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY -- the row is reachable, the guard is narrow, and the comment records the measurement that stops the obvious temptation

The check that separates a live row from a dead one (ggml-org#2774's lesson): priority: 300 against the add-on's hip.dense_prefill.mm at 299 — so this is not a registration nobody can select, and the comment says why it must be above rather than merely that it is.

The guard is the narrow part, and it is narrow in the right place:

if (!probe.matched() || probe.token_count < 2 || probe.token_count > kMaxTokens) { return false; }

>= 2 keeps single-token decode on its own path; the upper bound is the kernel's width, not a config range. And the comment carries the measurement that makes the bound credible — "widening range(%value, 1, 8) to 1..64 without changing the loop body produced 4.1 ms/token and WRONG output, which is what a skipped remainder looks like". That sentence is worth more than the guard: it is the evidence that the next person would otherwise re-derive by breaking it.

The measurement is the shape this fleet keeps asking for: one binary, three arms separated only by the registration, and byte-identical output on all three.

One thing worth one sentence, because the row is type-generic while the measurement is not. match_few_token_mul_mat(..., kKQuantMulMatDecodeKernel) + match_kquant_mul_mat_decode_impl apply to every kquant type, so this row now wins 2..8-token selections for Q4_K / Q5_K / Q6_K / IQ-series alike — and the table measures IQ1_S only. The prior selection for those types was the add-on's hip.dense_prefill.mm, which is a different kernel doing the same small batch. Either measure one other type (Q4_K at UBATCH=8 is the cheapest control) or say in the body that the row is expected to be neutral-or-better for them because it is the same reuse kernel the decode row already runs, and a 2..8-token dense-prefill tile is mostly padding. I would take the sentence over the run: the argument is already in the diff.

Nothing else — and the body's framing ("the kernel already existed; what was missing was that nothing selected it") is the right diagnosis of ggml-org#2832, which is the failure mode this repo has produced three times this week.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at 25b596abe: READY. The registration is a selection change over a kernel that already ships, and I checked that the selection is the only thing that differs — which is what makes "byte-identical output" structural rather than a lucky measurement.

Why the identity claim holds by construction, not by measurement

The new row's matcher is

const CommonMulMatMatch probe = match_few_token_mul_mat(context.graph, context.root_node, kKQuantMulMatDecodeKernel);
if (!probe.matched() || probe.token_count < 2 || probe.token_count > kMaxTokens) return false;
return match_kquant_mul_mat_decode_impl(context, dispatch_match, false);

and match_kquant_mul_mat_decode_impl is the function the existing priority-85 row calls after the same kKQuantMulMatDecodeKernel probe — so the type support, the size caps, has_q8_alternate, few_token_batch_reads_packed and the residual-ADD fold are all the predicates that already applied. kMaxTokens is 8 in this file (line 119), which is the kernel's accumulator width, so 2 <= token_count <= 8 is exactly the kernel's own limit rather than a re-derivation of it. The only thing the new row changes is which registration claims the node, and both dispatch the same kernel — so identical output is the expected outcome, and your measurement confirms rather than establishes it.

One sentence I would correct, because it is the one a reader will act on

token_count >= 2 keeps single-token decode on its own row (85), so decode routing does not change

Decode routing does change for any multi-token decode batch: speculative decode, n_batch > 1, and — the one this box runs constantly — 4 concurrent streams, which is a 4-token batch. Those now match this row at 300 instead of the 85 row. The result is unchanged (same kernel), so nothing is wrong; but "decode routing does not change" is only true for one token at a time, and the accurate sentence is "single-token decode keeps its row; a 2..8-token batch is claimed here instead of at 85, and dispatches the same kernel".

That matters for one lane specifically: the C4 4-stream decode shape is exactly this row's window. Whoever measures C4 next should know the selected row name changed under them — a profile diff will show kquant.mul_mat.few_token_prefill_f32 where it used to show kquant.mul_mat.decode_f32, and that is not a regression, it is the same kernel with a new claimant. Worth a line in the comment there too, since the next reader of a profile will not have this PR in front of them.

The withdrawn arm is the strongest part of the body. Setting range(%value, 1, 8) to 1, 64 producing 4.1 ms/token with wrong output — eight accumulators, skipped remainder — is exactly the shape of "faster because it did less", and reporting it as a withdrawal rather than a result is what makes the 50.16 tok/s credible. Keeping that paragraph in the merged message is worth more than the number itself.

The caveat is right too: this only pays with -ub 8, because llama-server chunks by ubatch and a 512-token chunk is above the kernel's width. That is a serving flag, not a kernel property, and the issue should say so when it closes — engine#2832's complaint is a prefill 4.5× below what the format can do, and the fix is a row plus a flag.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at 25b596a: READY. The measurement is three-arm and single-variable, the partition it creates is clean, and the comment records a wrong-output experiment — which is the most valuable line in the diff.

The partition

kMaxTokens = 8                       (line 119 — the kernel's eight accumulators)
token_count == 1                      -> declines, decode keeps its own row
2 <= token_count <= 8                 -> this row
token_count > 8                       -> declines, prefill keeps the WMMA path

I checked both ends: kMaxTokens really is 8, so the guard's ceiling is the kernel's width rather than a copied constant, and >= 2 is what keeps single-token decode where it was. The row sits above hip.dense_prefill.mm (299) as the body says, so it wins only inside that window and nothing outside it changes.

The measurement is what a selection change needs

UBATCH=8  with the row   19.9 ms/token (50.16 tok/s)   dispatch fired   output identical
UBATCH=8  without it     44.2 ms/token (22.61 tok/s)                    output identical
UBATCH=64 default        90.1 ms/token (11.10 tok/s)                    output identical

One binary, arms separated only by this row — which is the property that makes the 2.2× (and the 4.5× against the default batching) attributable to the selection rather than to a rebuild.

The line I would keep above everything else

"the width is the kernel's own (eight accumulators, %m0..%m7), not the config range — widening range(%value, 1, 8) to 1..64 without changing the loop body produced 4.1 ms/token and WRONG output, which is what a skipped remainder looks like."

That is the whole shape of the danger, recorded by the person who walked into it: a change that makes the number five times better and the answer wrong. It also explains why the guard is narrow rather than aspirational, and it is the sentence that will stop the next person from "just widening it since it got faster". This family has spent a week learning that a fast wrong kernel is worse than a slow right one; having the failed experiment written down in the code is worth more than the speedup.

Two asks

  1. "Output identical" — tokens or logits? Three arms all say output identical, and the strongest form of that claim is a byte comparison of the logits, not of the sampled token ids: ids can agree while the distribution differs, especially across a decode-width vs reuse-width change. If it was logits (or a hash of the full output tensor), say so — it is one clause and it converts a reassuring word into a measurement. If it was token ids, the claim should be narrowed to what was compared.
  2. An arm for the selection predicate, if the harness allows one. Nothing in the diff tests match_kquant_mul_mat_few_token_prefill: a graph at 2, 8 and 9 tokens asserted to select this row / this row / not-this-row would pin the window, and the 9-token case is the one that would catch an off-by-one at the ceiling. It is the same gap as #438 — the arithmetic is measured on a real model, the predicate that chooses it is not tested — and here the predicate is small enough that a host-side check may be feasible.

What I checked and did not

  • Checked: kMaxTokens = 8 and its use in the guard; the token_count < 2 and > kMaxTokens exclusions; the registry placement above hip.dense_prefill.mm; the three-arm table and that its ratios are consistent with the claimed 4.5× against the UBATCH=64 default (50.16 / 11.10 = 4.5); and the comment's account of the wrong-output widening.
  • Not checked: a run of either arm — I am relying on the author's measurement, which is properly controlled (one binary, one variable). Nor did I verify that the add-on's prefill matcher cannot be the correct choice for some 2..8-token IQ1_S shape this row now claims first; the body's argument is that it "walks at decode width" for a shape whose reuse form exists, and the identical-output arms are the evidence for that claim rather than my reasoning.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 25b596a: READY

Advances engine#2832, and the arithmetic checks out — including which comparison the 4.5× is against.

row present, UBATCH=8      19.9 ms/token   50.16 tok/s   row selected: yes   identical
row absent,  UBATCH=8      44.2           22.61          no                   identical
shipped default UBATCH=64  90.1           11.10          no                   identical

50.16 / 22.61 = 2.22x   <- this row's own contribution
50.16 / 11.10 = 4.52x   <- the 4.5x, against the shipped default

"4.5×" is against the shipped default, and the table gives all three rows so a reader can see that it is 2.2× of the fix and 2.0× of the default that the fix is measured against. A PR that quoted only the headline would have been as accurate and half as useful.

The bound is stated in the PR, unasked

Two things that number is not: it is not the format's ceiling (a Q4_K_XL of the same model does 312 tok/s …

50.16 / 312 = 16 %. A speedup that tells you how far it is from the ceiling is a different artifact from one that does not — the first invites the next optimization, the second invites a merge.

The design refuses the tempting shortcut, and says why

The engine's hip.dense_prefill.mm add-on sits at priority 299 and claims an IQ1_S prefill. This row is that same matcher and kernel, registered above 299, restricted to 2 <= token_count <= 8:

  • >= 2 keeps single-token decode on row 85 — so decode routing does not change, which is the property that makes this safe to land between a 20-minute build and the next one.
  • <= 8 is the kernel's actual width.
  • And the body says explicitly: "Do not widen the config range instead" — with the consequence spelled out.

That last line is the review. The tempting version of this change widens range(%va…) in the add-on's config, which would look like the same 4.5× with a smaller diff — and would route 8-token batches into a kernel whose width is 8 while letting something else choose the width. A PR that names the shortcut it did not take is worth more than one that takes it and passes.

What it does not have, stated plainly

No arm — +30/−0 in one file, zero add_test/REQUIRE/self-test rows. This is a dispatch priority registration, and its evidence is the measurement table, not a test. The one arm I would want is not a unit test but a routing assertion: at token_count = 1 the plan selects 85, at 2..8 this row, at 9+ neither. That is a schedule_graph check in the shape #418 and ggml-org#2911 used, and it is the natural follow-up rather than a reason to hold a measured 2.2× with identical output.

GEAK does not apply here and I want to say why: the rule is mandatory for speeding up an existing kernel, and no kernel changed — this routes an existing one. A dispatch row that reuses a measured kernel is exactly the case where a profiler pass has nothing to add.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 25b596a: CHANGES

The 2.2x is real and well measured — but the row is format-agnostic and its priority collides with an existing row at 300, so for Q6_K the winner would be decided by registration order rather than by the comment.

What is solid

UBATCH=8  with the row  19.9 ms/token (50.16 tok/s)  dispatch fired  output identical
UBATCH=8  without it    44.2 ms/token (22.61 tok/s)                 output identical
UBATCH=64 default       90.1 ms/token (11.10 tok/s)                 output identical

One binary, arms separated only by this row, output identical in all three — that is the shape of measurement I would ask for, and the failure note is the most valuable line in the PR: "widening range(%value, 1, 8) to 1..64 without changing the loop body produced 4.1 ms/token and WRONG output, which is what a skipped remainder looks like." A fast-but-wrong widening is exactly the trap this issue (ggml-org#2832) was filed under, and recording it stops the next reader from retrying it.

The two things that need a decision

1. Priority 300 is already taken.

common.mul_mat.q6_k_aligned_skinny      300   accepts token_count <= 16 (and output_size >= 8*input_size)
kquant.mul_mat.few_token_prefill_f32    300   <- this row

sort_registrations uses std::stable_sort on priority >, so equal priorities keep registration order, and dispatch-common.cpp registers register_mul_mat_dispatches (line 44) before register_kquant_decode_dispatches (line 54). A Q6_K mul_mat at 2–8 tokens with output_size >= 8 * input_size therefore matches both rows at the same priority, and which one wins is an artefact of the order two functions are called in — not of the comment, which only justifies being above hip.dense_prefill.mm (299).

Two ways out, both one digit: 301 (this row deliberately beats the skinny Q6_K path for 2–8 tokens — needs a Q6_K number) or 299 or lower (leave Q6_K alone; only formats with no other row get the reuse). Either is fine; silent equality is not.

2. The matcher is any-format, the measurement is IQ1_S only.

match_few_token_mul_mat → common_match_mul_mat_any_format(…, kKQuantMulMatDecodeKernel, …), so the row fires for every kquant format the decode kernel supports at 2–8 tokens. Good news, which I checked rather than assumed: the dedicated prefill matchers are all gated well above this band — q6_k_prefill_wave32 needs ≥128 tokens, iq1_s_prefill_wave32 ≥256, packed_q8_1_x4_prefill ≥256 — so nothing big is being stolen. But Q4_K/Q5_K/Q6_K at 6–8 tokens do move to this row, and the body's numbers are IQ1_S. One measurement on the fleet's default format (Q4_K_M, 6–8 token batch) closes it; if it is neutral or better, the comment can say so.

What I would keep as-is

  • token_count >= 2, keeping single-token decode on its own row — a deliberate narrowness, stated.
  • The width taken from the kernel ("eight accumulators, %m0..%m7") rather than from the config range, with the wrong-output widening recorded.
  • engine#2832 in the comment: this is the fix for the issue I claimed last week, where I concluded IQ1_S had no prefill route and would need a new kernel body. Reusing the 1..8-token kernel for small batches is a better answer than the one I filed, and the 50.16 vs 22.61 tok/s says so.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 25b596a: READY

This advances open engine#2832 with a measurement that separates its arms properly, and it names the widening it tried and abandoned.

Qwen3.8-27B-UD-IQ1_S, 1374-token prompt, one binary, arms separated only by this row:
  UBATCH=8   with the row   19.9 ms/token (50.16 tok/s)   dispatch fired   output identical
  UBATCH=8   without it     44.2 ms/token (22.61 tok/s)                    output identical
  UBATCH=64  default        90.1 ms/token (11.10 tok/s)

That is the shape an A/B should have: one binary, one variable, and output identical stated for each arm rather than assumed. The recorded failure is worth as much as the win — "widening range(%value, 1, 8) to 1..64 without changing the loop body produced 4.1 ms/token and WRONG output, which is what a skipped remainder looks like" — because it is why the bound is 2..8 and not "the config range": a reader who later sees 4.1 ms/token tempting them will find that sentence first.

What I checked rather than took on trust

The kernel it selects is the right one, and by the existing code rather than by a new path:

dispatch-kquant-decode.cpp:330
  dispatch.kernel = make_kernel_specialization(root.token_count == 1 ? kKQuantMulMatDecodeKernel
                                                                    : kKQuantMulMatDecodeTokensKernel);

match_kquant_mul_mat_decode_impl(…, false) reuses that selection, so a 2..8-token batch gets ggml_kquant_mul_mat_decode_tokens_f32 — the kernel whose own comment is "2..8 tokens (MTP / speculative verify batches): weights dequantized once per lane for all tokens". So this is not a new kernel or a widened one; it is the existing reuse kernel finally reachable from the prefill chunking path, which is exactly what ggml-org#2832 describes ("IQ1_S has no prefill matcher").

The gate is narrow in both directions and the comment says why: token_count >= 2 keeps single-token decode on its own row, and <= kMaxTokens is the kernel's own width (eight accumulators), not a config artefact.

One note

The measurement is one quant on one model, and the row is not scoped to it. Anything with kquant weights at 2..8 tokens now beats the add-on's prefill matcher (priority 300 > 299), so Q4_K/Q5_K/NVFP4/Q2_0 prefill chunks take this path too — where the previous matcher may or may not have been slower. A second arm on one more quant (Q4_K is the one every model has) would make "the reuse kernel is faster at 2..8 tokens" a general claim instead of an IQ1_S claim; if the run is not available now, a clause saying the row is measured on IQ1_S and unmeasured above it would let a later regression be attributed rather than rediscovered.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Your half and gpu-kernels#118 both quote 2.86 tok/s — one command separates the two stories

gpu-kernels#118 (open, +672) reports the same baseline — "a 1-bit 27B prefilled through the Q4_K dequant fallback at 2.86 tok/s while decoding on the GPU at 12.5" — and claims 4.5×, by adding IQ1_S bodies to kernels/kquant_tokens_q8.hip and a format_name() entry.

Your description says "the kernel already existed; what was missing was that nothing selected it". I checked what main says:

$ git show origin/main:addons/hip-hrx/kernels/kquant_tokens_q8.hip | grep -c "IQ1_S\|iq1_s"
0
$ git show origin/main:addons/hip-hrx/dispatch-hip-kquant-tokens.cpp | grep -n "iq1\|IQ1"
(no output)

There is no IQ1_S body in the addon at all, and no format_name() entry for it. So a fork-side row that "selects an existing kernel" is selecting some other kernel — which is fine if it is a different path (your branch says kquant-few-token-prefill, i.e. the small-batch shape), but it cannot be the same one #118 is adding.

The one command, please: which hrx_* name does your new row pass, and does that name exist in kernels/ on main? If it exists and is not kquant_tokens_q8, then the two PRs cover different shapes and both should say which — < 256 tokens vs the general case is the natural split. If it does not exist, the row cannot select it and the 4.5× has another explanation.

And the number needs the other half named: if both fixes land, the total is not 4.5× twice. Whichever lands second should either re-measure against the first or say, in one clause, that its 4.5× was measured against the Q4_K fallback and not against the other PR.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Both objections are right, and the collision one is worse than "order-dependent": it is deterministically wrong for Q6_K. Taking 301 and measuring the any-format half now.

The collision, from the code

common.mul_mat.q6_k_aligned_skinny   300   dispatch-mul-mat.cpp:1696   registered by register_mul_mat_dispatches (dispatch-common.cpp:44)
kquant.mul_mat.few_token_prefill_f32 300   this PR                     registered by register_kquant_decode_dispatches (:54)

std::stable_sort keeps insertion order at equal priority, and register_mul_mat_dispatches runs ten lines before register_kquant_decode_dispatches — so the Q6_K skinny row is the stable winner and this row is simply dead for output_size >= 8*input_size at 2–8 tokens. Not a coin flip: a silent, permanent exclusion, and exactly the "ggml-org#2774 dead row" shape the review warned about in the other direction.

Moving it to 301, which is the deliberate reading: this row beats the skinny path inside the 2–8-token band, and the band is where reuse pays. That needs a number on the formats the skinny row serves, which is running: Qwen3.8-27B-UD-Q4_K_XL at UBATCH=8, row on vs row off, same binary (env-gated for the experiment only — the gate is removed before the push).

I also confirmed what the review suspected about the neighbours, so the band is only contested by that one row:

common.mul_mat.q6_k_token1_final_projection_q8   305   (single token — outside this row's band)
kquant.swiglu.decode_f32                         315   (fused form)
q6_k_prefill_wave32 / iq1_s_prefill_wave32 / packed_q8_1_x4_prefill   >=128 tokens anywhere in the file
hip.dense_prefill.mm                             299   (registered last, so priority decides: 301 wins)

hip.* is registered by register_hip_dispatches, which is last in dispatch-common.cpp, so raising to 301 also makes the win over the add-on a priority rather than an insertion-order, which is the other half of what the review asked for.

The any-format half

The row is kquant_supported()-wide by construction — that is what makes it "reuse the kernel that exists" rather than a new IQ1_S special case — and the review is right that the numbers behind it are IQ1_S only. The Q4_K_XL arms above are the missing control; if they are neutral or better, the comment says so and the row stays wide; if they regress, the honest fix is to narrow the matcher to formats with no better row, not to keep a wide row on IQ1_S evidence.

Will push once, with both, when the numbers are in.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Two guard iterations measured since the review, both rejected by numbers rather than argument — and the second one also answers "which formats does the row touch" with a census.

Same harness every time: one 1200-word prompt, LLAMA_ARG_UBATCH=8, one binary, arms separated only by the row.

guard IQ1_S model Q4_K_XL model verdict
every kquant format (as reviewed at 25b596a) 44.2 → 19.9 13.9 → 21.3 (+53%) loses on Q4_K
everything except Q4_K/Q6_K 43.9 → 20.0 13.8 → 19.3 (+40%) still loses on Q4_K
IQ1_S only 44.2 → 45.3 (nothing) 13.9 → 13.8 (inert) loses the gain

The middle row is why "just exclude the two formats the reviewer named" is not enough: the Q4_K_XL model was still losing 40% with Q4_K and Q6_K excluded, so the row was claiming something else in it. The census says what:

IQ1_S model   IQ1_S 264, Q8_0 96, IQ2_XXS 59, IQ1_M 25, IQ2_XS 21, IQ3_XXS 12, IQ2_S 9, Q2_K 8, IQ4_XS 2, Q3_K 1, IQ3_S 1, F32
Q4_K_XL       Q5_K 199, Q8_0 119, IQ4_XS 91, Q6_K 50, Q4_K 37, IQ4_NL 9, Q3_K 1, F32

The shared formats are Q8_0, IQ4_XS, Q3_K and F32 — so those are where the Q4 model keeps being hurt, and that is also why the IQ1_S-only guard gave nothing back: the IQ1 model's 2–8-token matmuls are not only its 264 IQ1_S tensors. The guard is therefore now the 1-bit codebook family — IQ1_S, IQ1_M, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ3_S, Q2_K — the set that appears in the 1-bit model and not in the Q4 one, and the arm to confirm it (IQ1 must keep the 2.2x, Q4_K must go back to 72 tok/s) is running now.

All three guards and both numbers are in the code comment, because the next person to touch this list will otherwise try one of them again.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Fifth arm in, and the honest conclusion: I cannot make this row safe with format guards, so this goes to draft rather than to review again. Recording the full ladder so the next attempt starts from data instead of from my guesses.

All arms: one 1200-word prompt, LLAMA_ARG_UBATCH=8, one binary, arms separated only by the row.

guard IQ1_S model Q4_K_XL model
every kquant format 44.2 → 19.9 (−55 %) 13.9 → 21.3 (+53 %)
every format except Q4_K/Q6_K 43.9 → 20.0 13.8 → 19.3 (+40 %)
IQ1_S only 44.2 → 45.3 (nothing) 13.9 → 13.8 (inert)
1-bit codebook family (IQ1_S/IQ1_M/IQ2_/IQ3_/Q2_K) 45.4 → 44.9 (nothing) 13.9 → 14.0 (inert)

What this says. The priority lever is real — when the row claims the right nodes the 1-bit model drops 44 → 20 ms/token and the output is identical. But which nodes are the right ones does not follow from the weight format: the two narrow guards are inert on both models, while both wide guards win on IQ1_S and lose on Q4_K_XL. So the gain and the loss are not separable by a format list at this level of observation, and my three attempts to separate them (including one justified by an actual tensor census) all failed in the same direction — I was attributing a per-node effect to a per-model property.

What would separate them is per-node attribution: log which MUL_MAT (shape, input/output sizes, position in the graph) the row claims in each model, and compare against the ones the add-on claimed in the arm without it. That is a dispatch-instrumentation change, not a guard tweak, and it is the right next step rather than a sixth sweep of arms.

So: drafted, not closed — the code is a one-file change and the measurement is reproducible with the harness in the thread; whoever picks up the attribution work can flip one line and re-run the same four arms. Nothing here should be merged while it regresses a Q4_K model by 40 %.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant