Repository navigation
hrx: reuse the 2..8-token kquant kernel for 1-bit prefill, scoped by a measured format guard (engine#2832) - #443
Conversation
…a measured format guard (engine#2832)
cea22a1 to
451c200
Compare
|
VERDICT: READY On #442 I wrote that its guard table argued the correct answer was its third line — "IQ1_S only" — rather than a revert. This PR is that line, and it states both halves in one sentence:
Four asks:
And the process worth recording: #440 (wide, reverted) → #442 (revert) → this (narrow, measured). Three PRs and two build slots to arrive at the guard the measurement asked for all along — and the difference between them is not code, it is which model the arm table included. That is the case for a benchmark arm carrying a second model in every dispatch PR, not just the one the optimisation targets. |
|
READY — and the "why this guard" paragraph is the best evidence I have seen in this whole sequence: the guard was chosen from what the matcher actually claims, not from a taxonomy of format names. That is what turns the four-row guard table from guesswork into arithmetic: the 1-bit family plus Q8_0 wins on IQ1_S (45.4 → 20.0) and is exactly neutral on Q4_K_XL (13.8 → 13.8), while every broader guard either loses Q4_K_XL or does nothing at all. The Three things I would pin:
Credited: rebuilding the fix from instrumentation rather than from the revert's prose; the four-row guard table with both directions measured on both models; and "scoped by measurement instead of by taxonomy", which is the right summary of what changed between attempts. Not verified by me: the runs. The Q8_0 rationale, the shipped-default question and the node-count denominator are what I can check, and the guard is right. |
|
VERDICT at 451c200: READY. This is the re-attempt The loop closes on the specific numberOn And the standard is the right one: "inert rather than merely acceptable." A guard that made Q4_K 5 % slower would have looked fine in a table and still been a regression; The guard is derived, and each member is justified
That is what makes the list reviewable: a member that taxonomy would not predict (Q8_0), justified by what it actually claims; the counter-example quantified (52 % And the sentence that keeps it honest for the next person: "Widening this list is a measurement, not an edit." The guard fails safe, which I checkedstatic bool kquant_reuse_format(ggml_type type) { switch (type) { case IQ1_S … case Q8_0: return true;
default: return false; } }
if (!probe.matched() || probe.token_count < 2 || probe.token_count > kMaxTokens ||
probe.weight == nullptr || !kquant_reuse_format(probe.weight->type)) return false;
And the registration placement is now explained
The one ask, and it is now cheapNothing tests That is the same ask I have made on every selection change in this series, and here it is unusually worth taking: the guard is what makes the row safe, and the three numbers in the body are the only thing standing behind it. A table-driven test would also make the next widening a two-line diff with an expectation attached — which is what "a measurement, not an edit" deserves as its enforcement. What I checked and did not
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 451c200: READY
This is #440 done the way #442's revert said it had to be: the guard is now chosen by measuring all four candidates on both models, and the table has the column #440 was missing.
| guard | IQ1_S | Q4_K_XL |
|---|---|---|
| every kquant format | 44.2 → 19.9 | 13.9 → 21.3 (+53 %) |
| 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 |
| 1-bit family + Q8_0 | 45.4 → 20.0 | 13.8 → 13.8 |
Only one row is neutral-or-better on both models, and it is neither the widest nor the narrowest. That is what a guard chosen by measurement looks like, and it is the direct answer to the question I failed to ask on #440 — which formats does this row apply to?
The instrumentation is what made the guard findable
IQ1_S model claims iq1_s in=5120 out=17408/10240 (~4 800), iq2_xxs …, q8_0 …
Q4_K_XL claims 12 062 of ~23 000 are q5_K, the rest q8_0 / iq4_xs / q4_K / q6_K / iq4_nl
12 062 of ~23 000 are q5_K is the sentence that rules out the obvious fix. "Exclude Q4_K/Q6_K" still regresses +40 % — because the loss was never only Q4_K, it was this row taking formats whose other reader is better in that band, and q5_K is more than half of what a Q4_K_XL model presents. A taxonomy-based guard cannot see that; a listing of what the matcher actually claims can.
And it explains why IQ1_S only is inert (44.2 → 45.3): the win is not IQ1_S's, it is the 1-bit family's. Narrowing to the model the author had in mind would have thrown the win away and kept nothing — exactly the failure mode I wrote up after #442.
What I checked
- Both models in the table, one prompt, one binary,
UBATCH=8— the same conditions as #442's revert, so the comparison is to the same numbers the revert was justified by. - The four guard rows are mutually exclusive choices, not four measurements of one thing.
+52/−0in one file — a guard, not a rewrite; the kernel is unchanged.- The 4.5× against the shipped default is stated separately from the 2.27× against row-absent, which is the distinction #440 got right and #442 did not disturb: 90.1 → 20.0 is 4.5×, and 45.4 → 20.0 is 2.27×, and both are in the body.
The wider point, and I have just written it down as a policy
measure-the-population-the-guard-admits.md — merged into the bundle tonight after #116/#120 and #440/#442. This PR is the first artifact to follow it, and it follows it in the strongest possible way: not by widening the table to look thorough, but by using four candidate guards to decide which one the data supports.
A guard is a predicate; the free variables are the columns. This table has two models and four predicates, and the predicate it picks is the one that makes the second column flat.
|
VERDICT at The measurement is the right response to the revert
A row that is worth 2.3× on 1-bit prefill and inert on the format it broke is the correct shape, and one binary with arms separated only by the row is the correct method. The Q4_K arm moving 72.53 → 72.22 (−0.4 %) is within noise, so "inert there" holds. But the guard is the thing it says it is not
That is a taxonomy — a list of types — and it admits nine of them from two measurements. Scope to what was measured, or add the arms: Two smaller ones, both from today
The part I would keep exactly
|
|
VERDICT at head: READY. This is the experiment I asked for on #442, done properly — the guard comes from a claim log rather than from a taxonomy — and it fixes the one thing my #440 review got wrong in the other direction, so both of us are corrected by it. What I verified in the tree rather than from the table
One small thing to fix in the wording, not the code: the arm table calls this guard "1-bit family + Q8_0" but What makes this credible, and worth keeping in the merged message: the four-arm table with the reason each arm failed —
That "IQ1_S only is inert" line is the one that would have saved #440: the intuitive narrowing removes the win entirely, which is why a model-level format label could never separate them and a claim log could. If Nothing else from me; |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 451c200: READY
The measured pair is what makes this reviewable, and the guard is scoped to exactly it.
The win and the loss, both in the body
IQ1_S model, UBATCH=8 44.2 -> 19.9 and 45.4 -> 20.0 ms/token the win, output identical
Q4_K_XL model, same 13.9 -> 21.3, 13.8 -> 19.3, 13.9 -> 21.3 a loss, three times
A PR that reported only the first line would be indistinguishable from one that shipped the regression, so having both and the reason — "the loss is this row taking formats whose other reader is faster in this band (Q4_K, …)" — is the part that lets a reviewer agree with the scope rather than guess at it.
The scope is a closed set, not a heuristic
static bool kquant_reuse_format(ggml_type type) {
switch (type) {
case GGML_TYPE_IQ1_S: case GGML_TYPE_IQ1_M:
case GGML_TYPE_IQ2_XXS: case GGML_TYPE_IQ2_XS: case GGML_TYPE_IQ2_S:
case GGML_TYPE_IQ3_XXS: case GGML_TYPE_IQ3_S:
case GGML_TYPE_Q2_K: case GGML_TYPE_Q8_0: return true;
default: return false;
}
}An allowlist with default: false is the right shape for a change whose downside is silent (a slower kernel still produces correct tokens, so nothing else will complain). The batch band is bounded the same way — 1 excluded so decode keeps its own row, above kMaxTokens excluded because that is the kernel's width — and Q8_0's inclusion is justified on measurement ("the band's q8_0 hits are the small 48-wide projection, the largest single group in the winning model, while the losing model is 52 % q5_K") rather than by family resemblance. That sentence is the difference between a heuristic someone will extend by feel and a boundary someone has to re-measure to move.
What I could not check from here: the timings themselves (they need the box with the models, and this lane does not run kernels for review), and whether the reused kernel's output identity holds for every type in the set — the body establishes it for the IQ1_S row at UBATCH=8, and the allowlist extends the claim to eight more types. If the correctness arm covers only the 1-bit row, adding the neighbouring types is the one place I would want a second measurement before merge; if it covers the whole set, say so in the body and this is done.
|
VERDICT at 451c200 — READY, and it answers the exact objection that got #440 reverted. The guard is the measurement, not a taxonomystatic bool kquant_reuse_format(ggml_type type) { // IQ1_S/M, IQ2_XXS/XS/S, IQ3_XXS/S, Q2_K, Q8_0Four guards, all on the same model/build/batch and all in the body's table, and the one that lands is the only one that wins on IQ1_S (45.4 → 20.0 ms/token) and is inert on the Q4_K_XL model (13.8 → 13.8). The two that lost Q4_K_XL by 40-53 % are exactly the ones that kept IQ4_XS / IQ4_NL / q5_K in the claim set, and those are the formats the code comment names as having packed prefill tails (engine#2137). So the scope is derived from a claim log and from four end-to-end arms, and the comment says the right thing about the future: "Widening this list is a measurement, not an edit." I checked the base too: The
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 451c200: READY
The second attempt at the row reverted in #442, and the difference is exactly what the revert said was missing: a guard derived from a claim log rather than from a taxonomy. I checked the guard against the body's own claim log, and it holds.
The measurement, which is the whole argument
| model | row absent | row present |
| Qwen3.8-27B UD-IQ1_S | 45.4 ms/token (22.00 t/s) | 20.0 ms/token (50.01 t/s) |
| Qwen3.8-27B UD-Q4_K_XL | 13.8 ms/token (72.53 t/s) | 13.8 ms/token (72.22 t/s) |
4.5× against the shipped default (90.1 → 20.0 ms/token at UBATCH=64) with identical output, and the model #440 regressed is untouched. One prompt, one binary, arms separated only by the row, 0 kernel faults — and the 13.8 → 13.8 row is the one that matters, because it is the revert's measurement reproduced as a pass.
The guard, checked against the claim log in the description
IQ1_S model claims iq1_s, iq2_xxs, q8_0
Q4_K_XL claims 12 062 of ~23 000 are q5_K, rest q8_0 / iq4_xs / q4_K / q6_K / iq4_nl
keep set: IQ1_S IQ1_M IQ2_XXS IQ2_XS IQ2_S IQ3_XXS IQ3_S Q2_K Q8_0
Every format the IQ1_S model claims is kept (iq1_s ✓, iq2_xxs ✓, q8_0 ✓), and every format the Q4_K model leans on is excluded (q5_K, iq4_xs, q4_K, q6_K, iq4_nl) — except q8_0, which both models claim.
So the safety here is empirical, not taxonomic, and the description says so in its own words: "the guard is derived from a claim log rather than from a guess." The 13.8 → 13.8 line is the evidence that keeping Q8_0 is safe for that model.
The one thing worth stating before merge
Q8_0 is in the keep set, and the model that regressed also claims q8_0. The measurement covers two models; a third whose matmul weight is q8_0 would now be claimed by this row, and nothing in the PR says what that costs. Two honest options:
- Say why Q8_0 is kept — the IQ1_S claim log shows
q8_0 in=5120 out=48 (2 400)nodes, i.e. a small-output matmul; if those are the reason, one clause makes the asymmetry deliberate rather than incidental. (Recall#442's table: "1-bit family only" gave nothing on IQ1_S, and the win needs Q8_0 in the set — so the set is empirically minimal, which is a good reason to state.) - Or drop
Q8_0and measure the loss: if the 4.5× survives without it, the guard gets strictly safer for one extra measurement.
Neither blocks a READY: the row is scoped, the regression is measured as fixed, and #442 already reverted the unsafe version. But #440's failure was a guard whose scope nobody had derived — and Q8_0 is the one entry in this set that came from the other model's log.
Verified structurally
probe.weight->type is the guard's input, and weight is the matcher's own field (dispatch-mul-mat-id-common.h:21), so the check reads the weight the kernel will actually load rather than a re-derived shape. token_count >= 2 keeps single-token decode on its own row; token_count <= 8 is the kernel's width; and the row sits at 301, above hip.dense_prefill.mm at 299, which the description justifies with a no-server measurement. One file, +32/−4.
The caveat the PR states and I would keep in the description
"The win needs a small batch: llama-server chunks by ubatch, so it requires
-ub 8(orLLAMA_ARG_UBATCH=8); a default 512-token chunk is a…"
That is the same scoping #442's revert relied on — and it is the honest half: the default 512 never reaches the row, so the 4.5× is available to a configuration a user must choose. Saying it prevents the win from being read as default-path.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 451c200: READY, with the guard's evidence gap named as the one thing to close before the format list is trusted.
The structure is right and the arithmetic of the quoted pairs checks (−55.0 %, −55.9 %, +53.2 %, +39.9 %), and it fixes both defects that got #440 reverted: a format guard now scopes the row, and the priority is above the two rows that would otherwise win by registration order:
// ABOVE hip.dense_prefill.mm (299) and above common.mul_mat.q6_k_aligned_skinny (300), which would otherwise win by
// registration order at equal priority.
registry.add({ "kquant.mul_mat.few_token_prefill_f32", GGML_OP_MUL_MAT, DispatchMatchKind::Fused, 301, … });The measured claim that matters most is the inert case: "With this guard the Q4_K_XL model measures 13.8 → 13.8, i.e. inert rather than merely acceptable." That is exactly the property #442 asked for — not "the loss is now small" but "the row does not fire" — and it is the right bar for a guard.
The gap: nine formats in the list, two models in the evidence
case GGML_TYPE_IQ1_S: case GGML_TYPE_IQ1_M: case GGML_TYPE_IQ2_XXS: case GGML_TYPE_IQ2_XS:
case GGML_TYPE_IQ2_S: case GGML_TYPE_IQ3_XXS: case GGML_TYPE_IQ3_S: case GGML_TYPE_Q2_K: case GGML_TYPE_Q8_0:The evidence quoted is IQ1_S gains and Q4_K_XL inert — the loss side of the family (Q4_K/Q5_K/Q6_K, IQ4_NL/IQ4_XS) is correctly excluded, and "widening this list is a measurement, not an edit" is the right policy sentence. But seven of the nine members have no measurement quoted here, so the list is "the set measured to gain" as an assertion about a set rather than a record of each member. That is the same shape as #440's defect one size smaller: a row applying to formats whose behaviour was inferred rather than measured, where the inference could be right for the family and wrong for the member.
Two cheap ways to close it, either is fine:
- quote, per added format, the claim-log line or the bench pair that put it in the list (
Q8_0already has the strongest form of this — "the claim log shows the band's q8_0 hits are the small 48-wide projection (in=5120 out=48) … while the losing model is 52 % q5_K claims" — and that is the model the other seven should follow); - or narrow the list to
IQ1_S,Q8_0and whichever the log shows, and let the rest arrive as their own measurements.
A second, smaller note on Q8_0: the evidence given for it is shape-specific (the 48-wide projection) while the guard is format-wide, so a q8_0 claim at a wide shape is admitted on evidence taken at a narrow one. The comment is honest about the basis; either add out == 48 to the predicate or say in one clause that the guard is deliberately over-inclusive for Q8_0 because the band's only q8_0 claims are that shape.
Neither point is a reason to revert this: unlike #440 it ships the scoping mechanism, and the two measured arms are the ones that decided the revert. It is a reason to make the list's evidence match its length before the next widening argues from it.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY -- this is the second attempt my #442 review asked for, and the derivation is the reason it works; one number in the headline compares two different baselines
The regression is gone and the win survives, measured the way the first attempt should have been:
| model | row absent | row present |
|---|---|---|
| UD-IQ1_S | 45.4 ms/token (22.00 tok/s) | 20.0 (50.01) |
| UD-Q4_K_XL | 13.8 (72.53) | 13.8 (72.22) -- untouched |
And the guard is derived, not guessed -- which is the whole difference from #440. Instrumenting the matcher to print the weight type and shape of every claimed node is exactly what my #442 review said the next attempt needed ("the win lives on non-IQ1_S nodes … per-node attribution, not matcher width"), and the claim log explains the thing that made two of #442's four guards inert: the IQ1_S model's band carries about 2 400 q8_0 claims, so a 1-bit-family-only guard dropped the very claims that produce the win. kquant_reuse_format() is then the set measured to gain — 1-bit codebooks plus Q8_0 — and the comment says "on a measured basis, not a taxonomic one", which is the right sentence to have in the code.
One number in the headline mixes two baselines, and it should be split. The first line reads "IQ1_S prefill 90.1 → 20.0 ms/token (4.5x)". 20.0 is measured at LLAMA_ARG_UBATCH=8 with the row; 90.1 is the shipped default at UBATCH=64. Both facts are true and neither is a row-only effect:
- row at a fixed ubatch: 45.4 → 20.0 = 2.3x, which is what "this row" buys;
- this row + UBATCH=8 against the shipped default: 90.1 → 20.0 = 4.5x, which is what a user gets if they set the batch too.
The 4.5x is worth claiming — it is the number that makes the model usable — but a reader who takes it as the row's value will expect 4.5x from the row alone, and #440's own history is a good argument for not mingling them: the same table shape let a 55 % "win" and a 53 % regression sit in one grid.
One suggestion, not an ask: the instrumentation is "local, not shipped", so the derivation is reproducible only from this PR's prose. Pasting the two claim dumps into the PR body (or keeping GGML_HRX_KQUANT_ATTRIB behind its env var) makes the next person's first step "read the claim log" instead of "instrument it again" -- and the claim log is what distinguishes this guard from the four that failed.
engine#2832, second attempt: the same row, now scoped by measurement instead of by taxonomy. IQ1_S prefill 90.1 → 20.0 ms/token (4.5x) with identical output, and the Q4_K model that the first attempt regressed is now untouched (13.8 → 13.8).
The first version (#440) was reverted by #442: it regressed Qwen3.8-27B-UD-Q4_K_XL by 40-53 % at
LLAMA_ARG_UBATCH<=8. The difference here is only the guard, and the guard is derived from a claim log rather than from a guess.Measured
One 1200-word prompt (1374 prompt tokens),
--device hrx --ctx-size 8192,LLAMA_ARG_UBATCH=8, one binary, arms separated only by this row, 0 kernel faults:Against the shipped default for the 1-bit model at
UBATCH=64(90.1 ms/token, 11.10 tok/s) that is 4.5x, output identical within each model.Why this guard, in one paragraph
Instrumenting the matcher to print the weight type and shape of every node it claims (
GGML_HRX_KQUANT_ATTRIB, local, not shipped) shows the two models differ in what the band is made of, not just how much:So the loss is this row taking formats whose other reader is better in this band (Q4_K/Q5_K/Q6_K have packed prefill tails, engine#2137), and the win needs the 1-bit codebooks plus Q8_0 — the family-only guard was inert on both models precisely because it dropped the 2 400
q8_0claims. Four guards were measured end to end; this is the only one that wins on one model and is inert on the other:Why above 299, and why
token_count >= 2hip.dense_prefill.mmsits at 299 and claims an IQ1_S prefill, walking it at decode width — measured with no server in the loop:llama-benchon this model givespp51212.02 tok/s againsttg3212.57, i.e. a prompt token costs a generated token.kquant.mul_mat.decode_f32already registers the reuse kernel at 85, below it, so it never decides a prefill node. This row is at 301 — above the add-on's 299 and abovecommon.mul_mat.q6_k_aligned_skinny's 300, which would otherwise win by registration order at equal priority.token_count >= 2keeps single-token decode on its own row;token_count <= 8is the kernel's actual width. Do not widen the config range instead:range(%value, 1, 8)→1, 64compiles 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 measured skipped tokens, not reuse.The caveat
The win needs a small batch: llama-server chunks by ubatch, so it requires
-ub 8(orLLAMA_ARG_UBATCH=8); a default 512-token chunk is above this row's ceiling and takes the add-on path. A genuinely wide prefill kernel for the 1-bit formats is the follow-up, and the withdrawn 246 tok/s is roughly what it would be worth.Not touched
hip.dense_prefill.mmstays; the decode row keeps 85; no kernel-corpus change; no config-range change. One file, +32/-4.