Repository navigation
CUDA: fuse the SiTU gate of the MoE experts - #402
Open
danielhanchen wants to merge 1 commit into
Open
danielhanchen wants to merge 1 commit into
danielhanchen wants to merge 1 commit into
Conversation
SiTU, beta*tanh(g/beta)*sigmoid(g)*lb*tanh(u/lb) over the two halves of a gate_up product, is 9 graph nodes (7 kernels with the sigmoid*mul fusion) per MoE layer. Run it as one elementwise kernel with the rounding of the separate ops (the scales as FMAs like scale_f32, tanhf, 1/(1 + expf(-x)), products not contracted). GGML_CUDA_DISABLE_SITU_FUSION=1 turns it off.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
danielhanchen
changed the base branch from
master
to
base/upstream-418fe97b5
November 21, 2026 21:48
paleluna82-del
added a commit
to paleluna82-del/llama.cpp
that referenced
this pull request
Nov 22, 2026
…ry depth mix Both were n_tokens batched tiny GEMMs (rel table 1024x24x16, mix 2304x1xB), which hipBLASLt runs with the GPU mostly idle (pp2048 767 t/s by default, 1257 with ROCBLAS_USE_HIPBLASLT_BATCHED=0). - rel table: one GEMM over all heads and tokens (same bias matrix), Rcur flattened to 2D. - depth mix with one query: sum over blocks as a broadcast multiply and a row sum. R9700 with default hipBLASLt, with unslothai#400-unslothai#402 applied: pp512 1519 / pp2048 1460 / pp16384 1377 / tg128 41.8, i.e. the env variable is no longer needed. KLD vs the previous build 0.0023, top-1 97.8% (switching only the BLAS library on the previous build: 0.0021 / 97.9%). Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del
added a commit
to paleluna82-del/llama.cpp
that referenced
this pull request
Nov 22, 2026
Decode on this model is bound by the number of small kernels (~5600 per token), and each tiny elementwise kernel costs ~2.2 us inside a HIP graph. - ssm_conv: optionally fuse a residual add and a scale, (res + conv(x, w))*s, as SSM_CONV -> ADD -> SCALE. inkling2 builds the non-dynamic short convolutions in that order (192 per token). - new fused tanh gate mix, (x - tanh(f)*x) + tanh(g)*m, for the UNARY(TANH) -> MUL -> SUB -> UNARY(TANH) -> MUL -> ADD subgraph of the loop controllers. inkling2 expands the projections and the message first so that the elementwise tail is contiguous. - test-backend-ops cases for both. R9700, Q4_K_M, q8_0 KV, with unslothai#400-unslothai#402 applied: tg128 41.8 -> 45.8 t/s, pp512 1519 -> 1559. PPL unchanged at c512 (7.6923 -> 7.6899), KLD 0.001 vs the unfused path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del
added a commit
to paleluna82-del/llama.cpp
that referenced
this pull request
Nov 22, 2026
…conv on CUDA/HIP Each non-dynamic short convolution concatenates the past columns from the recurrent state with the new inputs, copies the last columns back to the state, convolves, adds the residual and scales: five kernels, 192 times per token. Fuse CONCAT -> VIEW -> VIEW -> CPY -> SSM_CONV -> ADD -> SCALE into one kernel that reads the past from the state, keeps the window in registers and writes the new past back (n_t <= 32, d_conv 4). inkling2 expands the conv output right after the state store so that the nodes are adjacent. Disable with GGML_CUDA_DISABLE_SSM_CONV_HIST_FUSION. R9700, Q4_K_M, q8_0 KV, with unslothai#400-unslothai#402 applied: tg128 45.8 -> 47.5 t/s. KLD vs unfused 0.000000 (max 5.6e-5) with ubatch 16 and with 4 sequences per ubatch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del
added a commit
to paleluna82-del/llama.cpp
that referenced
this pull request
Nov 22, 2026
…e same input Several projections of one input (Q, rel, sink, gate, ...) each quantized the same src1 to q8_1 again: 385 quantize kernels per token on Inkling-2-Mini decode for ~290 products. Keep the q8_1 copy of the last mul_mat_vec_q src1 in a per-context buffer and reuse it when the next mul_mat_vec_q has the same src1 tensor, data, shape and strides. The graph loop invalidates it when any node writes the src1 memory, at every graph evaluation and off the main stream. The buffer (4 MiB) is allocated outside stream capture and kept until the context is freed, so captured graphs stay valid. Disable with GGML_CUDA_DISABLE_Q8_1_REUSE. R9700, Inkling-2-Mini Q4_K_M, with unslothai#400-unslothai#402 applied: tg128 47.6 -> 48.4 t/s, bitwise identical (KLD 0 with ubatch 16, 4 sequences). New test-backend-ops case MUL_MAT_SHARED_SRC1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del
added a commit
to paleluna82-del/llama.cpp
that referenced
this pull request
Nov 22, 2026
The depth mix (attention over the RMS-normalized earlier block outputs with a learned query) was RMS_NORM -> RESHAPE -> CONCAT -> CONCAT -> MUL_MAT -> SOFT_MAX -> MUL -> SUM_ROWS, 88 times per token. Fuse it into one kernel per token that reads the stacked states and the current state once (squares, scores), does the softmax in registers and writes the weighted sum. The kernel reads the stack in its [E, n, T] layout only, so the fusion checks that the transposed stack is built from the same normalized states by the same concats. inkling2 expands the per-token query repeat first so that the nodes stay adjacent for ubatches > 1. Disable with GGML_CUDA_DISABLE_DEPTH_MIX_FUSION. R9700, Inkling-2-Mini Q4_K_M, with unslothai#400-unslothai#402 applied: tg128 48.3 -> 50.8 t/s, pp512 1550 -> 1640. KLD vs unfused 0.0024 at ubatch 16 (rounding from the reordered reductions; PPL c512 7.6899 -> 7.6851). New test-backend-ops case DEPTH_MIX. Co-Authored-By: Claude Opus 5.5 <[email protected]>
erokhins
pushed a commit
to JetBrains-Hardware/llama.cpp
that referenced
this pull request
Nov 24, 2026
abeloin
added a commit
to abeloin/llama.cpp-mix
that referenced
this pull request
Nov 24, 2026
0bfd204) Merge --no-ff du commit épinglé par unsloth (scripts/unsloth/pr-set.json). Couche 2, liste A (banc/notes/couche2-unsloth-liste-2026-11-24.md côté notes). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01QBjnvFeJ4P7Jp71oC6pzWf
This branch has not been deployed
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.
Pin for the prebuilt: upstream b12602 (418fe97) plus this one commit. Base branch base/upstream-418fe97b5.
What it changes
SiTU, beta * tanh(g / beta) * sigmoid(g) * lb * tanh(u / lb) over the two halves of a gate_up product, is 9 graph nodes (7 kernels with the existing sigmoid*mul fusion) per MoE layer. The matcher runs it as one elementwise kernel (situ.cu) with the rounding of the separate ops: the scales as FMAs like scale_f32, tanhf, 1 / (1 + expf(-x)) as op_sigmoid, products not contracted. The u view reads gate_up, so it is checked by use count and left out of the subgraph test. GGML_CUDA_DISABLE_SITU_FUSION=1 turns it off. test-backend-ops gets SITU_FUSE (3 cases).
Evidence (B200, one exclusive card)
Other backends
CUDA, HIP and MUSA share the .cu. On gfx1151 (Linux ROCm and Windows) the fused SITU_FUSE cases go from 0 to 3 OK, so the kernel runs and matches CPU there. Metal and Vulkan are untouched and never take the fused path.
Cross-platform base vs head (pin set with all four speed pins): HIP gfx1151 Linux ROCm and Windows, Vulkan lavapipe, Metal (macos-15), CPU and CUDA B200, no regression on any leg; test-llama-archs and the tiny Inkling 2 perplexity are identical. The GitHub macos-15 runner reports MUL_MAT unsupported, so Metal is untested for matmul (this change does not touch Metal code).