Skip to content

CUDA: fuse the Inkling 2 short conv, depth mix, loop controller, L2 clip and situ chains - #33060

Merged
am17an merged 2 commits into
masterfrom
aman/cuda-inkling2-fusions
Nov 26, 2026
Merged

am17an merged 2 commits into
masterfrom
aman/cuda-inkling2-fusions

Conversation

@am17an

@am17an am17an commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Overview

Decoding Inkling 2 Mini on CUDA launches about 5.7k kernels per token, most of them small ops on rows of n_embd. This PR fuses five op chains of the Inkling 2 graph:

  • short conv (convolve(), 192 per token): CONCAT of the conv state and the input, the state store (CPY of the last columns, or SET_ROWS of the rollback windows when n_rs_seq > 0), SSM_CONV, the input-dependent taps of the full-attention K/V convs, the ADD of the input, SCALE, and the residual ADD after the attention and FFN output convs -> one kernel
  • depth mix (mix(), 120 per token): RMS_NORM of cur, CONCAT with the normalized stack, the scores against the queries, SOFT_MAX, the transposed copy of the states and the weighted sum -> one kernel
  • loop controller (control(), 96 per token): CONCAT, RMS_NORM, the rank-64 down projection, the gain and forget projections, TANH and the combine -> two kernels, for batches of up to 8 tokens
  • L2 clip (clip(), 144 per token): MUL by the per-channel scale, L2_NORM, SCALE and the MUL by tau -> one kernel (a variant of l2_norm_f32 in norm.cu)
  • situ activation of the experts (48 per token): the 9 element-wise ops on the halves of gate_up -> one kernel

The first commit only changes the order of the conv ops in the Inkling 2 graph (ggml_build_forward_expand of the taps input, the conv result, the residual adds and tau), so each chain is consecutive and can be matched. The values do not change.

The short conv kernel walks the tokens of a sequence with the last K columns in registers. The allocator usually runs the ADD and SCALE in place over the conv input, so the fused output often aliases its input; this is safe when one thread has the whole sequence (decode and verify batches), and other overlaps are rejected.

Additional information

Inkling 2 Mini IQ4_NL, RTX PRO 6000 Blackwell, llama-bench -fa 1 -r 3, 3 interleaved rounds of master (2d5b3dd) and this PR:

test master (t/s) PR (t/s) per round
pp4 222.8 / 227.6 / 224.8 357.8 / 359.8 / 355.1 +58 to +61%
pp512 4844 / 4994 / 4874 5828 / 5799 / 5930 +16 to +22%
tg128 100.4 / 102.9 / 102.5 131.6 / 131.4 / 132.4 +28 to +31%

On the previous base (244640b): pp2 +37 to +43%, pp8 +34 to +40%, pp4096 +11 to +13%.

  • Kernel launches per decode token: 5745 -> 2584. All convs, clips, situ and controllers fuse, and 118 of 120 mixes; the other two are the last reader of a stack, whose memory the allocator gives to the mix output, so the overlap check rejects them.
  • Accuracy, llama-perplexity -c 512 --chunks 16 on wikitext-2 against master:
    • the conv, clip and situ fusions keep the operation order of the unfused graph: mean KLD 0 (only the uint16 floor of the KLD base, as master against itself)
    • the depth mix and the loop controller sum the dot products in a different order: mean KLD 0.00205 with all fusions, PPL ratio 0.9996 +- 0.0013; for comparison master at -ub 256 against master at the default ubatch is 0.00225
    • one-token batches (-c 128 -ub 1 --chunks 4): mean KLD 0.0020, about half of it from the loop controller
  • test-backend-ops: new SITU, L2_CLIP, SHORT_CONV (CPY and rollback-slot stores, taps, residual, K = 2..8, one token per thread and the sequential path), DEPTH_MIX (1/3/4 queries, 1..12 stack rows, the norm computed before the mix) and LOOP_CTRL cases pass on CUDA, and each of them runs the fused kernel. test-llama-archs passes for inkling2 and inkling2_loop.
  • Not in this PR: the per-layer bias mask of the attention (the CUDA side of the indexed bias in model: use the indexed bias in the Inkling 2 graph #33053), the top-k weight norm of the router (CUDA: fuse the top-k MoE weight norm with an additive sum bias #32943), and the copies of the strided views of the mix output.
  • Tested on Blackwell (sm_120) only; HIP and MUSA are not built.

Requirements

@am17an
am17an marked this pull request as ready for review November 25, 2026 18:30
@am17an
am17an requested review from a team and ggerganov as code owners November 25, 2026 18:30
@am17an

am17an commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@ServeurpersoCom I guess you're going to do similar changes to metal. I think we should find a way to do this fusions out of the box for any new model. For CUDA, nvrtc is an option and for Metal you can just build a shader on the fly.

@github-actions github-actions Bot added model Model specific testing Everything test related ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Nov 25, 2026
@am17an

am17an commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@ggml-org/ggml-cuda would appreciate a speedy review of this one.

mbednarek360 pushed a commit to mbednarek360/llama.cpp that referenced this pull request Nov 26, 2026
Matches commit 1 of ggml-org#33060 so the short conv, taps, residual add and tau ops are consecutive for backend fusion.
@am17an
am17an force-pushed the aman/cuda-inkling2-fusions branch from 89478df to 5e8f72c Compare November 26, 2026 14:38
@am17an
am17an merged commit 0e4e925 into master Nov 26, 2026
32 checks passed
@ServeurpersoCom

Copy link
Copy Markdown
Contributor

Fix CI #33141

@pwilkin

pwilkin commented Nov 26, 2026

Copy link
Copy Markdown
Member

@am17an CI is failing after this.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

@am17an CI is failing after this.

@pwilkin fixed by #33141 (merged)

@anujj

anujj commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

The tau expand at inkling2.cpp:286 runs before build_inp_embd, so it lands in the CPU split. On GB10 this costs ~13% decode (42.0 -> 36.8 t/s); moving the line to the end of build_inputs fixes it (45.5 t/s, KLD 0 vs master at 16k). Can you move it there and re-check on GB10?

Decode speed (tg128, two rounds, 0 foreign processes):

Build Spark (GB10) RTX 5090
master 42.0 t/s 97.7 t/s
PR (as merged) 36.8 t/s (-12%) 128.2 t/s (+31%)
PR + tau fix 45.5 t/s (+8% vs master) 129.0 t/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning model Model specific testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants