Skip to content

hrx: the fused SwiGLU prefill leaves a weight with a stored copy to a stored reader (engine#2392) - #354

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxmem/2392-swiglu-follows-decode
Nov 14, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxmem/2392-swiglu-follows-decode

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

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-kernels 13d92fb, 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 from GGML_HRX_HOST_WEIGHT_TRACE=2:

copies MiB weights in two layouts
add-on, before 470 3322.3 72 (70 read packed by the fused SwiGLU prefill, stored by hip:k_61d118a92b4c)
add-on, this PR 400 2387.2 2
add-on off (GGML_HRX_DISABLE_DISPATCH=hip.), reference 398 2375.9 0
  • Greedy 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:

    pp512 pp2048 tg128
    A1 (before) 3254 2924 78
    B1 (after) 3176 2810 78
    A2 (before) 3181 2791 79
    B2 (after) 3182 2846 78

    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.

    • Mutant: the new early-return removed (not committed). stored_copy fails at fused == 0, packed still 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.
  • The add-on's 22 host-tier failures stay as they are. The 12 weight-format cases are the guard for the order above, so they shouldn't be relaxed.
  • test-backend-ops was not rerun. Its weights are not in a file mapping, so the new condition never fires there.

🤖 Generated with Claude Code

… 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]>
@bong-water-water-bong

Copy link
Copy Markdown
Author

READY. The fix reuses the plain aligned prefill's own predicate (q4k_aligned_prefill_reads_stored) rather than re-deriving "does this weight already have a stored copy", which is the right call: the two dispatches share the constraint, so they should share the query. A second copy of that logic is what would drift the moment a third kernel needs the same answer.

Three things I checked in the change and agree with:

  • Both weights, joined with ||. gate || up then return false — one weight already stored means this kernel would hold that one twice, so declining is correct for either. A single-sided check would have left half the duplication in place, and the measured table (70 gate/up weights in both layouts) is what says both halves matter.
  • Declining rather than converting. Sending the pair back to the aligned matcher keeps the "read it the way it is stored" invariant that already exists, instead of adding a re-pack on the fused path. The direction matters: a re-pack would trade a memory duplication for a copy cost that shows up in prefill latency.
  • The GGML_HRX_Q4K_PACKED_TAIL early return was left in front of it. That keeps the experiment switch meaningful — with the variable set, the new rule does not quietly change what the switch is measuring.

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.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY at 30fee86 — a real +946 MiB regression fixed by declining rather than switching, with both branches of the new predicate tested and the speed claim stated honestly.

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 — schedule_swiglu_q4k_prefill_layout(bool stored_copy_exists) is driven both ways by the suite:

swiglu_q4k_prefill_layout.packed        ← false: the fused kernel keeps the node
swiglu_q4k_prefill_layout.stored_copy   ← true:  it declines

That is the negative arm by construction, and the env GGML_HRX_Q4K_PACKED_TAIL escape hatch prints "skipped: a packed-layout switch is set in the environment" rather than silently not running — the third-outcome discipline.

Two things I checked and one that is worth stating

  • 72 vs 70 in the diff is not an inconsistency: "72 (70 read packed by the fused SwiGLU prefill, stored by hip:k_61d118a92b4c)". Both numbers are real and the parenthetical says which set each is.
  • The speed claim is the honest kind: "The two A runs alone differ by 133 tok/s on pp2048, so this is within noise." Stating the noise size is what makes "within noise" a measurement instead of a shrug — and it is the right conclusion, since a memory fix that moved prefill by more than the run-to-run spread would be a different PR.
  • The behaviour is now order-dependent, and that should be written down. The rule keys on "a decode kernel read it stored: with the HIP add-on, every dense decode does" — so the same prompt dispatches differently depending on whether a decode happened earlier in the process. That is correct and cheap, but it means (a) the +946 MiB only appears in a long-prompt-after-decode sequence, which is exactly why it took the add-on to surface, and (b) a future test that builds a fresh context will not reproduce it. One sentence in the comment — "the condition is set by a prior decode, so a fresh-context prefill takes the fused path" — saves the next person a day.

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 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 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 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 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.

@bong-water-water-bong
bong-water-water-bong merged commit bf9329a into 1bit/hrx-vulkan-patched Nov 14, 2026
@bong-water-water-bong
bong-water-water-bong deleted the cl-hrxmem/2392-swiglu-follows-decode branch November 14, 2026 01:27
@bong-water-water-bong

Copy link
Copy Markdown
Author

cl-hrxmem, correction to this PR's speed claim (post-merge). "pp/tg within noise" was measured with llama-bench, which prefills before any decode. In that order no stored copy exists when prefill is planned, so this PR's rule never fired in the timed runs. Re-measured in the order where it does fire (Qwen3-4B Q4_K_M, add-on build at 0daeb800d, gpu-hold):

  • First prompt (llama-completion, 2105 tokens, warm-up decode first, rounds 2–4 of 4 interleaved):

    • before this PR: 1759 / 1516 / 1843 tok/s
    • this PR: 1803 / 1666 / 1836
    • follow-decode off (GGML_HRX_PREFILL_FOLLOWS_DECODE=0): 1242 / 1022 / 1522

    No cost here: the packed copies the other arms skip are made inside the first prompt, so holding both layouts doesn't speed it up. Greedy text is identical in all three.

  • Steady state (copies already made): serving now reads every dense Q4_K/Q6_K weight stored. That's the same layout set as GGML_HRX_Q4K_PACKED_TAIL=0 GGML_HRX_Q6K_PACKED_ROWS=0: 400 copies / 2387.2 MiB / 2 weights twice, identical to the serving census. llama-bench A B A B: pp512 3147, 3126 (packed) vs 2439, 2424 (stored), −22 %; pp2048 2849, 2813 vs 2350, 2351, −17 %; tg128 79/78 vs 78/78.

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.

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