Skip to content

CUDA: fuse the depth mix (stack concat, scores, softmax, mix) into one kernel - #414

Open
danielhanchen wants to merge 3 commits into
base/upstream-418fe97b5from
inkperf4-up-mix
Open

danielhanchen wants to merge 3 commits into
base/upstream-418fe97b5from
inkperf4-up-mix

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Pin for the prebuilt: upstream b12602 (418fe97) plus these two commits (the fusion, then its decode-size gate before Ada). Base branch base/upstream-418fe97b5.

What it changes

The attention-residual depth mix of Inkling 2 runs RMS_NORM of the current state, two CONCATs onto the normalized block stacks (or one CONCAT and a CONT of its permuted rows), MUL_MAT against the learned queries, SOFT_MAX_EXT and MUL_MAT against the transposed stack: six launches per mix, two to three mixes per layer. The matcher takes the chain after the RMS_NORM (which stays its own kernel) and runs it as one kernel per token (depth-mix.cu), split over up to 4 blocks for the output rows, with the rounding of the separate kernels:

  • the first product follows mul_mat_vec_f for its block size: the same per-thread accumulation, warp sums and final warp sum
  • the softmax follows soft_max_f32 with one warp per row
  • the second product (at most 32 columns) forms the same 32 lane partials as the one-warp mul_mat_vec_f launch and adds them in warp_reduce_sum order

The fusion is only taken where ggml_cuda_mul_mat would run both products as MMVF (and, on NVIDIA before Ada, only above 8 tokens; see below), for at most 32 stack rows and row lengths up to 8192; intermediates used outside the chain are still written. GGML_CUDA_DISABLE_DEPTH_MIX_FUSION=1 turns it off. test-backend-ops gets DEPTH_MIX_FUSE (113 cases, both graph forms, decode and prefill rows, 1/3/4 queries).

Evidence (B200, one exclusive card, load-gated runs)

  • Inkling-2-Mini decode: 94 depth_mix_f32 per token, -300 kernels.
  • Share of the decode set: tg128 -3.2% on UD-Q8_K_XL.
  • KLD against the pin set (4 files; prefill, -ub 1/4/8, dense 4K): PPL string equal, mean KLD 0, top-1 100%.

Other NVIDIA GPUs (Colab T4, L4, A100-40GB, RTX PRO 6000 Blackwell Server; one card each, built from b12628 plus #399 plus the decode pins)

GPU (cc) file pp512 with it off tg128 with it off
T4 (7.5) UD-IQ1_S -4.8% +0.3%
L4 (8.9) UD-IQ1_S -8.7% +0.2%
L4 (8.9) UD-Q4_K_XL -8.2% +0.0%
A100 (8.0) UD-IQ1_S -7.1% +0.9%
A100 (8.0) UD-Q4_K_XL -5.7% +0.7%
A100 (8.0) UD-Q8_K_XL -6.0% +0.6%
RTX PRO 6000 (12.0) UD-IQ1_S -10.6% -3.1%
RTX PRO 6000 (12.0) UD-Q4_K_XL -12.2% -2.4%
RTX PRO 6000 (12.0) UD-Q8_K_XL -11.1% -2.2%
  • Prefill gains on every GPU (5-12%). Batch-1 decode gains on the RTX PRO 6000 (2-3%) and ties on L4, but A100 is 0.6-0.9% slower fused (outside its 0.1-0.3% spread) and T4 ties (+0.3%, inside its spread).
  • So the second commit keeps the separate kernels for decode-size mixes (up to 8 tokens) on NVIDIA before Ada (cc < 8.9): T4 and A100 then decode as before and keep the prefill gain. Ada and Blackwell take the fusion at every size (Hopper too, not measured); HIP is unchanged (a tie on gfx1151).

…e kernel

The attention-residual depth mix of Inkling 2 runs RMS_NORM of the current
state, two CONCATs onto the normalized block stacks, MUL_MAT against the
learned queries, SOFT_MAX_EXT and MUL_MAT against the transposed stack: six
launches per mix, two to three mixes per layer.

Match the chain after the RMS_NORM (which stays its own kernel) and run the
CONCATs, the [REPEAT,] MUL_MAT, the SOFT_MAX and the second MUL_MAT as one
kernel per token, split over up to 4 blocks for the output rows. The rounding
is the same as the separate kernels:
- the first product follows mul_mat_vec_f for its block size: the same per
  thread accumulation, warp sums and final warp sum
- the softmax follows soft_max_f32 with one warp per row
- the second product (at most 32 columns) forms the same 32 lane partials as
  the one-warp mul_mat_vec_f launch and adds them in the order of
  warp_reduce_sum, one thread per output row

The fusion is only taken where ggml_cuda_mul_mat would run both products as
MMVF (an odd stack or more than three queries keeps the second or both
products separate), for at most 32 stack rows and row lengths up to 8192.
Intermediates used outside the chain are written as well. When an output reuses
the memory of the RMS_NORM output, one block per token loads its row before
writing, and other overlaps are declined.

GGML_CUDA_DISABLE_DEPTH_MIX_FUSION=1 turns it off. GGML_CUDA_DEPTH_MIX_SHADOW=1
(debug, with GGML_CUDA_DISABLE_GRAPHS=1) runs the fused kernel into scratch
buffers next to the separate ops and prints a byte comparison of every output.

- CUDA: depth mix fusion for the mix whose transposed rows are a copy of the plain ones
…e kernels

Up to 8 tokens on NVIDIA cc < 8.9 the fused mix was about 1% slower than the separate kernels at batch-1 decode on A100 (cc 8.0) and within noise on T4 (cc 7.5); prefill (more tokens) still fuses there, +5-12% pp512. Ada, Hopper and Blackwell, and HIP, are unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-23T03:58:58.817248Z f766457 Manual request
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa85f1a6da

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +69 to +71
const float2 * x2 = (const float2 *) (k < n - 1 ? p.prev + k*p.prev_s1 + t*p.prev_s2 : normed);
for (int col2 = gt; col2 < ncols2; col2 += g_size) {
const float2 tmpx = x2[col2];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check previous-state alignment before issuing float2 loads

When prev is an F32 view with a four-byte offset or an odd-float row/token stride, the matcher accepts it because depth_mix_rows_ok() only checks the element stride. This cast then produces misaligned float2 loads, which can cause a CUDA misaligned-address error. The unfused path supports these inputs: CONCAT copies them into an aligned output before MMVF reads it, so checking MMVF eligibility on cat_s does not validate prev. Require eight-byte alignment of the previous-state pointer and relevant strides, or use scalar loads for unaligned rows.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and fixed in f766457. The matcher now declines when the earlier stack rows are not 8-byte aligned (pointer, row stride or token stride), so such stacks go through CONCAT, which copies them into an aligned stack. New test-backend-ops cases: DEPTH_MIX_FUSE with the earlier rows as a view at a 1-float offset with an odd row stride, in both graph forms. Before the fix this hit a CUDA misaligned address error; it passes now. The Inkling 2 stacks are aligned, so the fusion still engages (94 per token) and the output is bit-identical to before.

…nels

The scores read the earlier rows of the stack as float2. An F32 view at an
odd float offset, or with an odd row or token stride, passed the matcher and
gave a misaligned address. Such stacks now keep the separate kernels, where
CONCAT copies them into an aligned stack first. test-backend-ops: DEPTH_MIX_FUSE
with the earlier rows as a view at a 1-float offset with an odd row stride, in
both graph forms (misaligned address before, OK now).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: f766457c8a

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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
…to one kernel (unslothai#414, commit f766457)

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

Conflit limité à tests/test-backend-ops.cpp (structures de test ajoutées au même endroit par deux pins) : côté mix conservé, les cas de test de ce pin ne sont PAS repris — à réintégrer à la main.

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