Skip to content

CUDA: GGML_OP_ATTN_RES_MIX + Inkling 2 single-token decode fusions - #409

Closed
danbunnell wants to merge 1 commit into
unslothai:inkling2-attn-bandedfrom
danbunnell:pr-inkling2-cuda-fusions
Closed

danbunnell wants to merge 1 commit into
unslothai:inkling2-attn-bandedfrom
danbunnell:pr-inkling2-cuda-fusions

Conversation

@danbunnell

Copy link
Copy Markdown

Summary

Complementary to #400 (mmvf), #401 (clip-rows), and #402 (SiTU) with zero overlap, this PR reduces per-token kernel launch and memory traffic overhead in Inkling 2 (inkling2 / inkling2_loop):

  1. GGML_OP_ATTN_RES_MIX (CPU + CUDA, attn-res-mix.{cu,cuh}):
    • Fuses Inkling 2's block attention residual mix (RMS-norm over block stack + query dot-product + softmax + weighted sum) into a single op, eliminating intermediate RMS_NORM, CONCAT, REPEAT, MUL_MAT, SOFT_MAX, and CONT(TRANSPOSE) nodes while preserving Unsloth's stack()/block_stack fallback when types/counts don't qualify.
  2. Inkling 2 single-token CUDA fusions (inkling2-fuse.{cu,cuh}, ggml-cuda.cu, inkling2.cpp):
    • Shortconv K=4 step: fuses single-token recurrent state shift/copy + SSM_CONV + dynamic 4-lag taps (26 ops -> 1 kernel) or static conv + residual + scale [+ outer residual add] (8/9 ops -> 1 kernel), keeping Unsloth's xt prefill path intact.
    • Loop controller: fuses CONCAT(cur_norm, msg_norm) + MUL_MAT(w_down) (2 ops -> 1 kernel), MUL_MAT(w_gain) + MUL_MAT(w_forget) + tanh gate blend (8 ops -> 1 kernel), plus a 6-op post-gate fallback.
    • Dense relative kq_b builder (9 ops -> 1 kernel for n_rel == 16, single token) and general 2-op ADD + SCALE.
    • Adds matching add_alloc_deps entries in ggml_backend_cuda_graph_optimize so gallocr never aliases fused inputs with outputs.

Verification

  • Merges cleanly onto b12606 + scripts/unsloth/pr-set.json@cfc9ea3709ca (#386, #400, #401, #402) with zero conflicts.
  • Compiled with CUDA 13.3 (sm_86); GPU KLD parity and benchmark results will be posted shortly.

Adds a fused GGML_OP_ATTN_RES_MIX op (CPU + CUDA) for Inkling 2 block attention
residuals and single-token CUDA graph fusions for Inkling 2 decode:
- Dynamic (26-op) and static (8/9-op) K=4 shortconv state update + conv + residual
- Loop controller CONCAT+MUL_MAT down-projection, 8-op up-projection + tanh gate
  blend, and 6-op post-gate fallback
- Single-token 9-op dense kq_b relative bias builder (n_rel=16)
- General 2-op ADD+SCALE residual tail fusion

Complementary to unslothai/llama.cpp unslothai#400 (mmvf), unslothai#401 (clip-rows), and unslothai#402
(SiTU), with zero overlap.
@danbunnell

Copy link
Copy Markdown
Author

GPU Verification Results (Linux x86_64, RTX 3090 24 GB sm_86, CUDA 13.3)

Tested the combined mix (b12606 + pr-set.json@cfc9ea37 + #407 + #408 + #409, zero merge conflicts, merged tree b301feeb6) on unsloth/Inkling-2-Mini-GGUF (UD-IQ4_XS + CPU mmproj-F16.gguf):

  • test-backend-ops test -b CUDA0 -o FLASH_ATTN_EXT_BANDED: 71/71 PASS
  • KLD parity (llama-perplexity, 16 chunks, -ub 128):
    • q8_0 KV: Mean KLD = 0.003023, Mean PPL(Q) = 3.048365
    • q4_0 KV: Mean KLD = 0.007744, Mean PPL(Q) = 3.054044
  • 256K (-ctk q8_0 -ctv q8_0 -ub 128 -fa on, 100% GPU): 6/6 end-to-end gates (load, chat, reasoning, tool-calling, needle-in-haystack context, speed) PASS — 92.1 tok/s decode, 772 tok/s prefill (vs 57.5 tok/s decode / 638 tok/s prefill on #386@7db4a706), peak VRAM 21,161 MiB, 0 NVRM Xids.
  • 512K (-ctk q4_0 -ctv q4_0 -ub 128 -fa on, 100% GPU, no --n-cpu-moe): 6/6 gates PASS — 93.0 tok/s decode, 627 tok/s prefill (vs 54.5 tok/s / 383 tok/s with --n-cpu-moe 2 -ub 64 on #386@7db4a706), peak VRAM 21,445 MiB, 0 NVRM Xids.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this. I tested it on a B200 stacked on our pin set (the published Mini GGUFs do not load on #386 alone), base = pin set, head = pin set plus this PR.

Numerics change on every decode step:

mean KLD top-1
UD-IQ1_S -c 512 / -ub 1 / -ub 4 0.0031 / 0.0030 / 0.0024 97.9% / 97.8% / 98.6%
UD-Q4_K_XL -c 512 / -ub 1 / -ub 4 0.0024 / 0.0020 / 0.0018 98.0% / 97.7% / 98.8%

The A/A floor is 0, and greedy text diverges on both models. Splitting it with local switches: GGML_OP_ATTN_RES_MIX accounts for all of the prefill drift (it computes dot(raw, q)*inv_rms instead of RMS_NORM then MUL_MAT), and the single-token fusions add decode drift on their own (FMA contraction in the control gate, the conv taps summed in a different order, and the projection and kq_b dots not keeping MUL_MAT's reduction order). The node reordering in inkling2.cpp is exact. A local test of the new op (540 shapes) shows CUDA matches its CPU reference, but that does not tie either to the old graph.

Other issues:

We have exact versions of several of these in #411 (short conv chain), #412 (tanh mul add/sub) and #414 (depth mix), each with a kill switch, and they give about +30% tg128 on B200 together with the rest of that series. If you want to push this further, a CUDA-backend fusion that keeps the original rounding, rather than a new graph op, would fit with those and not affect the other backends.

@danbunnell

Copy link
Copy Markdown
Author

Thanks for the detailed review and for pointing out the Metal/Vulkan fallback, b12635 GGML_OP_COUNT conflict, and rounding drift. Closing this since #411, #412, and #414 cover these cleanly as bit-identical CUDA-backend fusions with kill switches. Will benchmark #419 first and only follow up with a small bit-exact CUDA-backend fusion if anything meaningful remains.

@danbunnell danbunnell closed this Nov 23, 2026
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.

2 participants