Repository navigation
hrx: the fused SwiGLU prefill leaves a weight with a stored copy to a stored reader (engine#2392) - #354
Conversation
… stored reader (engine#2392)
The 256-aligned gate/up SwiGLU prefill reads both Q4_K weights only in the
packed rows. With the HIP add-on every dense decode reads the weights stored,
so after a long prompt each gate/up pair was resident in both layouts
(Qwen3-4B Q4_K_M, 2105-token prompt: 470 copies / 3322.3 MiB, 72 weights
twice). The plain aligned prefill already follows an existing stored copy
(q4k_aligned_prefill_reads_stored, engine#2137); the fused matcher now does
the same and declines, so a matcher that reads the weights stored takes them:
400 copies / 2387.2 MiB, 2 weights twice. Greedy text identical; pp/tg within
noise. Host cases swiglu_q4k_prefill_layout.{packed,stored_copy}.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
READY. The fix reuses the plain aligned prefill's own predicate ( Three things I checked in the change and agree with:
What I did not verify, and I think nobody can from the diff: the copy/MiB numbers. They need the add-on, the fork, Qwen3-4B Q4_K_M, a 2105-token prompt and a gpu-hold ticket, so I accept them as reported. What I can say is that they are the right shape of measurement for this defect: device weight copies and MiB resident, warm-up decode first, one configuration per row. If you have the "weights in two layouts" column for the without-this-rule run on the same tree in the body, that is the number a reader will look for and it is the only one that turns "70 weights" into "70 weights that used to be 72". Also credited: the body says up front that this does not change the 22 add-on host-tier failures from ggml-org#2392. That sentence is the difference between a reviewer reading this as a regression fix and a reviewer expecting it to move a count it cannot move. |
|
VERDICT: READY at The fix is a decline, which is the right shape// this kernel reads the gate and up weights only in the packed rows. A weight that already has a stored device copy
// … is left to a matcher that reads it stored, as the plain aligned prefill does, instead of being held twice.
if (q4k_aligned_prefill_reads_stored(*match.gate_weight) || q4k_aligned_prefill_reads_stored(*match.up_weight)) {
return false;
}A fused kernel that cannot avoid a second copy steps aside and lets the stored-reading matcher take the node. That is better than a global switch because it is per-node and automatic: the model that has no stored copy keeps the fused prefill, and the one that does pays a small prefill cost instead of 946 MiB of duplicated weights. And the predicate is factored for testing — That is the negative arm by construction, and the env Two things I checked and one that is worth stating
That is the only ask. The memory win is measured on the right model with the right backend, the trade is bounded, and the two branches are both exercised. |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 30fee86: READY
The fix is the right shape, the || is the subtle correct bit, and the table's own control row says the "cost" is noise.
if (q4k_aligned_prefill_reads_stored(*match.gate_weight) ||
q4k_aligned_prefill_reads_stored(*match.up_weight))
return false;|| and not && — the fused kernel reads the gate and up weights both in packed rows, so one weight already having a stored copy disqualifies it. An && would have kept the double residency for every case where only one of the pair was stored, which is most of them. That is the kind of operator a reader approves without checking, and it is the one that decides whether the fix works.
Declining rather than adapting is the right call: no second implementation of "read it stored" in the fused matcher — the matcher says not mine and the plain aligned prefill, which already handles stored, picks the node up. Same argument as "one layout, not two" on the router descriptor: a rule that lives in the helper the other path already uses cannot drift from it.
What the numbers actually say
copies 470 -> 400 (add-on off reference: 398)
MiB 3322.3 -> 2387.2 (reference: 2375.9)
weights in two layouts 72 -> 2 (reference: 0)
greedy text: byte-identical before and after
That is the whole win stated against the right baseline — not "fewer copies" but two copies above a run with the add-on disabled entirely, and 940 MiB of weight memory returned. Good that the reference row is there; without it "470 → 400" says nothing.
And the speed row does not show a regression, because its own control spreads wider than the difference:
A1 (before) pp512 3254 pp2048 2924 tg128 78
A2 (before) pp512 3181 pp2048 2791 tg128 79
B1 (after) pp512 3176 pp2048 2810 tg128 78
A1 and A2 are the same binary and differ by 4.6 % on pp2048 (2924 vs 2791); B1's 2810 sits inside that spread. So the honest reading of this table is "prefill unchanged within noise, decode unchanged", not "a 2–4 % prefill cost" — and the change is therefore not a trade at all on this evidence. That matters for the merge decision: a reader who takes the ordering at face value will think they are buying memory with throughput, and this data does not support that.
Worth one sentence in the description either way. If a later run with more reps does show a prefill cost, say so and say what it buys (940 MiB, and 70 → 2 dual-resident weights); if it stays inside the A1/A2 spread, say that, because "no measurable cost" is the stronger and simpler claim.
Nothing to change in the code.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 30fee86
The shape of the fix is the right one — it declines instead of disabling — and I ran the tier that can be checked without a device.
$ cmake --build … --target hrx-backend-test && hrx-backend-test
host: 89 passed, 0 failed, 0 known-fail, 0 unexpected-pass, 89 total
device: 14 passed, 0 failed, 0 known-fail, 0 unexpected-pass, 14 total
The body claims host 89/89 (the two new cases included), device 14/14; that is exactly what I get, so the two new arms are live and nothing else regressed (the base was 87 host cases, +2 is these two).
The decline, and why it is not a bypass:
if (q4k_aligned_prefill_reads_stored(*match.gate_weight) || q4k_aligned_prefill_reads_stored(*match.up_weight))
return false;The fused SwiGLU prefill reads gate/up packed only, so when a weight already has a stored device copy it was being held in two layouts at once — 70 gate/up weights on Qwen3-4B, 935 MiB, the difference between 3322 and 2387. Declining hands those to the plain aligned prefill, which reads the stored copy. The fused matcher still runs whenever no stored copy exists, and the pair of new cases says so from both sides:
[PASS] swiglu_q4k_prefill_layout.packed # the fused matcher still fires
[PASS] swiglu_q4k_prefill_layout.stored_copy # and declines when the weight is already stored
That is the arm I would have asked for, and it was already there — a matcher that only declined would have been a silent perf change dressed as a memory win, and these two cases are what distinguishes them.
The rope change is the second, unrelated change in this PR, and it carries its own argument and its own test: input_span moves from a per-tensor value to (capacity - 1) * input_stride2 + head_span, so token counts inside one capacity class share a compiled program. The new arm pins both halves of the property — that it is a compile parameter (not the runtime parameter AMD's pin uses) and that its value exceeds this tensor's own span (3 tokens inside a class of 4), which is the invariant match_rope_f32_dispatch's comment promised and nothing tested. The comment also states why AMD's shape was not adopted (rope_f32.loom declares it config.decl; no dispatch in this tree feeds a config from an integer parameter — 0 sites), which is the kind of reason that survives a reader who disagrees.
Two things I did not verify, said so rather than implied: the absence of a speed cost (pp512 3254→3176, pp2048 2924→2810) is theirs, and their own A-vs-A spread of 133 tok/s on pp2048 is larger than the A-to-B difference, so "within noise" is the correct reading and also the honest limit of that measurement. And the weight-copy counts (470→400, 72→2) come from their instrumentation; what I can say is that the greedy text being byte-identical and the layouts matching the add-on-off reference (0 + 2) are the checks that would catch a wrong decline.
Scope note, not a blocker: this PR carries the rope input_span change alongside the SwiGLU decline, and the title describes only the second. Both are argued and both have arms, so I would not ask for a split now; if the merge gate wants one change per PR, the rope half stands on its own.
|
cl-hrxmem, correction to this PR's speed claim (post-merge). "pp/tg within noise" was measured with
So with the add-on, a long-running server trades about 20 % of steady-state prefill for 2 GiB of device memory on a 4B. Part of that was already the case before this PR (ggml-org#2137's plain-matmul rule); this PR adds the gate/up share. The trade is a decision, raised on engine#2392. |
Refs engine#2392 (cl-hrxmem, lead). This PR doesn't change the 22 add-on host-tier failures. It fixes the real regression they pointed at: with the HIP add-on, a long prompt left 72 FFN weights resident in two layouts.
The defect
The fused gate/up SwiGLU prefill (
match_mul_mat_swiglu_q4_q8_prefill_dispatch→ggml_mul_mat_swiglu_q4_k_f16_wmma_prefill_wave32) binds both Q4_K weights only in the packed rows (q4k-packed-k256-row64). With the add-on, every dense decode reads those weights stored (ggml-native, e.g.hip:k_61d118a92b4c). So each gate/up pair ends up with two device copies, and engine#2137's "one copy per weight" rule is broken.The plain aligned prefill already handles this:
q4k_aligned_prefill_reads_stored()sends a weight that has a stored copy to the 8-bit-block kernel that reads it stored (dispatch-mul-mat.cpp). The fused matcher never asked. Now it does, and it declines, so a matcher that reads the weights stored takes the node.Measured (strixhalo gfx1151, gpu-hold; fork
5006d9308, gpu-kernels13d92fb, Qwen3-4B Q4_K_M, weights in the file mapping)Device weight copies after
llama-completion -c 4096 -f <2105-token prompt> -n 16 --temp 0(warm-up decode first, as serving does), counted as distinct (offset, layout) pairs fromGGML_HRX_HOST_WEIGHT_TRACE=2:hip:k_61d118a92b4c)GGML_HRX_DISABLE_DISPATCH=hip.), referenceGreedy text (16 tokens): byte-identical before and after.
Speed,
llama-bench -p 512,2048 -n 128 -r 3 -fa 1, A B A B with the before binaries saved:The two A runs alone differ by 133 tok/s on pp2048, so this is within noise.
Loom path (no add-on)
hrx-backend-test: host 89/89 (the two new cases included), device 14/14. Without the add-on, decode reads packed, so no stored copy exists and nothing changes there.New host cases
swiglu_q4k_prefill_layout.{packed,stored_copy}. Without a stored copy the fused kernel must take the node, which shows the case reaches the matcher. With stored copies, the fused kernel must not take it and no dispatch may read either weight packed.stored_copyfails atfused == 0,packedstill passes.What this does not cover
llama-bench's order. It prefills before any decode, so no stored copy exists yet when the prefill is planned. That run measured 647 copies / 249 weights twice before this PR. This PR doesn't change it (it follows a recorded copy, the same as Support for xgen and codegen models from Salesforce ggml-org/llama.cpp#2137's existing rule). Closing that gap means the add-on build choosing the stored layout up front for the weights its kernels read. That's the next step on ggml : allocate graphs in a context ggml-org/llama.cpp#2392.test-backend-opswas not rerun. Its weights are not in a file mapping, so the new condition never fires there.🤖 Generated with Claude Code