Repository navigation
metal: fold SwiGLU into the signed FWHT, and narrow the last qwen35 layer to output rows - #287
Merged
Merged
Conversation
…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).
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). |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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_maskedwas 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 throughembeddings_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.Metal: SwiGLU folded into the signed FWHT for
ffn_down. A rotated model'sffn_downinput 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 askernel_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_signednever setrun_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: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 runkernel_fwht_swiglu_f32_{128,1024,2048}(checked from the pipeline log). Beforerun_whole_graph()was added, the same cases passed on the unfused path.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).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
CUDA and Vulkan plan
CUDA already fuses MUL, RESHAPE and MUL_MAT(hadamard) in
ggml_cuda_graph_evaluate_and_captureviaggml_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.