Skip to content

metal: fold SwiGLU into the signed FWHT, and narrow the last qwen35 layer to output rows - #287

Merged
bri-prism merged 2 commits into
prismfrom
perf/yukon-graph-fusions
Sep 28, 2026
Merged

bri-prism merged 2 commits into
prismfrom
perf/yukon-graph-fusions

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

Summary

Two graph-level changes for rotated (Hadamard) Qwen3.5 targets, taken from ideas in the Yukon MLX.fast promoted submissions (Apache-2.0) and rewritten for ggml.

  1. Last-layer row narrowing for plain decoding and DFlash targets (qwen35, qwen35moe). The last layer ran on every row of the batch whenever embeddings_nextn_masked was false, which is the default. Only unmasked nextn extraction (the MTP target) needs a hidden row per token. Plain AR and the DFlash target, which taps layer inputs through embeddings_layer_inp, now narrow to the output rows at the last layer, matching deepseek2, deepseek32 and glm-dsa. Capture taps keep their rows: a tap on the last layer reuses the already narrowed rows instead of slicing twice.

  2. Metal: SwiGLU folded into the signed FWHT for ffn_down. A rotated model's ffn_down input was GLU, then the sign-flip MUL, then the FWHT-hint matmul. The sign flip was already fused into the transform. This change evaluates the SwiGLU on load as well, using the same formula as kernel_swiglu, so the GLU output is no longer written and read back, and one dispatch per layer goes away. The idea comes from rube-de's promoted submission, which folds SwiGLU into the verify-window Hadamard input.

Also: test_fwht_signed never set run_whole_graph(), so the harness compared it node by node and never reached the signed-FWHT fusion it was written for. It now runs the whole graph and gains SwiGLU cases.

Measurements (M5 Pro, local)

Ternary Bonsai 2 27B PQ2_0, llama-bench -p 512 -n 128 -r 3 -ngl 99, two interleaved reps per arm, load under 2.5, nothing else on the GPU:

arm pp512 t/s tg128 t/s
prism (adfffbe) 403.3 27.89
narrowing only (fusion disabled) 399.3 27.81
this PR 408.7 (+1.3%) 28.13 (+0.9%)

The measured gain comes from the SwiGLU fusion. Narrowing alone reads flat to slightly lower here (399.3 vs 403.3 pp512, within the two-rep spread). It removes work in theory (the last layer's FFN runs on 1 row instead of 512 during prefill), but I could not show a speedup on this machine.

Op level (test-backend-ops perf, the ffn_down input at n_ff 17408): the fused and unfused SwiGLU paths are within 1% at 1 and 8 tokens. The end-to-end gain comes from the dispatch and memory round trip, not the transform.

Testing

  • test-backend-ops -o MUL_MAT_HADAMARD: Metal 29/29, CPU 33/33. The SwiGLU cases compile and run kernel_fwht_swiglu_f32_{128,1024,2048} (checked from the pipeline log). Before run_whole_graph() was added, the same cases passed on the unfused path.
  • Greedy output with llama-server, eight prompts of 256 tokens, AR and DFlash2 at depth 7: all 16 outputs byte-identical to prism, and DFlash2 acceptance counts identical on every prompt (for example 196/408 and 164/632).
  • KLD against prism logits (llama-perplexity --kl-divergence, 12 chunks of 512): mean KLD 0.000000, maximum 0.000054, same top token 100.000%, PPL ratio 1.000000 ± 0.000001, for this PR and for the fusion-disabled arm alike. The fused kernel keeps the same float order within each element.

Not in this PR

  • Stacking DFlash2 drafter q|k|v and gate|up. The BF16 drafter reads about 3.2 GB of weights per draft step, roughly 11 ms at this machine's bandwidth, and stacking removes about 15 dispatches per step without reducing bytes. The drafter's weight format is the lever there, not launch count.
  • CUDA and Vulkan keep the unfused GLU. See the CUDA plan below.

CUDA and Vulkan plan

CUDA already fuses MUL, RESHAPE and MUL_MAT(hadamard) in ggml_cuda_graph_evaluate_and_capture via ggml_cuda_op_fwht_signed. The matching change is a fourth pattern, GLU(swiglu, split), MUL, RESHAPE, MUL_MAT, that passes the gate and up pointers into the signed FWHT kernel's load, gated on F32 and contiguous inputs, with the same test cases. Vulkan has the sign fusion too and would take the same four-op pattern. Neither was built or measured for this PR; no NVIDIA or AMD hardware was used.

…sked or off

The last layer ran on every batch row unless embeddings_nextn_masked was set,
and qwen35 also skipped narrowing whenever capture layers were registered.
Only unmasked nextn extraction (the MTP target) needs a hidden row per token.
Plain decoding and DFlash targets, which read their taps through
embeddings_layer_inp, now slice to the output rows before the last layer's
residual, matching deepseek2, deepseek32 and glm-dsa. A capture tap on the
last layer reuses the already narrowed rows instead of slicing twice.

Idea from the last-row narrowing in the Yukon MLX.fast promoted submissions
(Apache-2.0).
A rotated model's ffn_down input is GLU, then the Hadamard sign-flip MUL, then
the FWHT-hint matmul. The sign flip was already applied while the transform
loads its row; the SwiGLU now is too, with kernel_swiglu's formula, so the GLU
output is never written and one dispatch per layer goes away. F32 only; other
types and non-split GLUs keep the unfused path.

test_fwht_signed never asked for whole-graph evaluation, so the harness checked
it node by node and never reached the fused kernels it was written for. It now
does, and gains SwiGLU cases plus ffn_down-shaped perf cases.

On an M5 Pro with Ternary Bonsai 2 27B PQ2_0: pp512 403.3 -> 408.7 t/s, tg128
27.89 -> 28.13 t/s (llama-bench, two interleaved reps); greedy output identical
for AR and DFlash2 depth 7 on eight prompts.

Adapted from rube-de's Yukon MLX.fast promoted submission, which folds SwiGLU
into the verify-window Hadamard input (Apache-2.0).
@bri-prism

Copy link
Copy Markdown
Collaborator Author

@khosravipasha could you review this when you get a chance? It folds SwiGLU into the signed FWHT on Metal (+1.3% prefill, +0.9% decode, KLD 0 with 100% top-1 agreement) and narrows the last qwen35 layer to output rows (flat on its own here).

@bri-prism
bri-prism merged commit 79971a4 into prism Sep 28, 2026
8 checks passed
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Add Brian (bri-prism)'s ffn_down-sized signed FWHT measurements, with and without fused SwiGLU, for decode, verification, and prefill. The fusion was adapted from rube-de's Yukon MLX.fast submission (Apache-2.0).

Source: PrismML-Eng#287

Source: PrismML-Eng@79971a4
Assisted-by: GitHub Copilot

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Add Brian (bri-prism)'s ffn_down-sized signed FWHT measurements, with and without fused SwiGLU, for decode, verification, and prefill. The fusion was adapted from rube-de's Yukon MLX.fast submission (Apache-2.0).

Source: PrismML-Eng#287

Source: PrismML-Eng@79971a4
Assisted-by: GitHub Copilot

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 2, 2026
Keep eligible sign-multiply and split SwiGLU chains together. Read their
inputs directly in FWHT and preserve intermediate rounding. Reject
observable intermediates, overlapping buffers and incompatible views.

The Vulkan implementation follows bri-prism's CUDA signed-FWHT and Metal
SwiGLU-FWHT fusion designs. The Metal contribution credits rube-de's
Yukon MLX implementation. Extend the shared signed-transform fixtures
with offset and aliasing guards. The SwiGLU test case is adapted from
bri-prism's PrismML-Eng#287.

Inspired-by: PrismML-Eng#121
Inspired-by: PrismML-Eng@79971a4
Assisted-by: Codex
Co-authored-by: bri-prism <[email protected]>
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 2, 2026
Add Brian (bri-prism)'s ffn_down-sized signed FWHT measurements, with and without fused SwiGLU, for decode, verification, and prefill. The fusion was adapted from rube-de's Yukon MLX.fast submission (Apache-2.0).

Source: PrismML-Eng#287

Source: PrismML-Eng@79971a4
Assisted-by: GitHub Copilot

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 24, 2026
Keep eligible sign-multiply and split SwiGLU chains together. Read their
inputs directly in FWHT and preserve intermediate rounding. Reject
observable intermediates, overlapping buffers and incompatible views.

The Vulkan implementation follows bri-prism's CUDA signed-FWHT and Metal
SwiGLU-FWHT fusion designs. The Metal contribution credits rube-de's
Yukon MLX implementation. Extend the shared signed-transform fixtures
with offset and aliasing guards. The SwiGLU test case is adapted from
bri-prism's PrismML-Eng#287.

Inspired-by: PrismML-Eng#121
Inspired-by: PrismML-Eng@79971a4
Assisted-by: Codex
Co-authored-by: bri-prism <[email protected]>
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 24, 2026
Add Brian (bri-prism)'s ffn_down-sized signed FWHT measurements, with and without fused SwiGLU, for decode, verification, and prefill. The fusion was adapted from rube-de's Yukon MLX.fast submission (Apache-2.0).

Source: PrismML-Eng#287

Source: PrismML-Eng@79971a4
Assisted-by: GitHub Copilot

Ported and rebased by Codex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant