Skip to content

CUDA: fuse the SiTU gate of the MoE experts - #402

Open
danielhanchen wants to merge 1 commit into
base/upstream-418fe97b5from
inkperf-cuda-situ
Open

danielhanchen wants to merge 1 commit into
base/upstream-418fe97b5from
inkperf-cuda-situ

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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)

  • Inkling-2-Mini decode: 5723 -> 5350 kernels per token. On top of the mmvf, clip and graph changes: UD-Q4_K_XL pp512 +57%, pp4096 +45%, tg128 +27% over the pin set; UD-IQ1_S +60% / +46% / +28%.
  • test-backend-ops CUDA0: SITU_FUSE 3/3, TANH, SIGMOID, MUL, SCALE pass.
  • llama-perplexity KLD against the pin set (UD-IQ1_S, UD-Q4_K_XL, Mainline_Support UD-IQ1_S, decode path, two GPUs): PPL string equal, mean KLD 0, top-1 100%.

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

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T21:50:37.705938Z 0bfd204 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen
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

No deployments
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