Repository navigation
CUDA: fuse the depth mix (stack concat, scores, softmax, mix) into one kernel - #414
danielhanchen wants to merge 3 commits into
Conversation
…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.
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. |
There was a problem hiding this comment.
💡 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".
| 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]; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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
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 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)
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)