Repository navigation
CUDA: fuse tanh-mul-add/sub, router weight normalization and add + rms_norm chains - #412
danielhanchen wants to merge 2 commits into
Conversation
…s_norm chains - y = c -/+ tanh(a)*b (UNARY TANH, MUL, SUB/ADD), views of b between TANH and MUL are skipped - x / scale_bias(sum_rows(x)) or clamp, with an optional reshape + scale (router weights) - ADD followed by RMS_NORM of the sum (optionally through a reshape), the sum is still written; only rows that take the vector path of rms_norm_f32 with one float4 per thread (ncols <= 4096) Each fused kernel rounds like the separate kernels (same tanhf, no FMA contraction of the elementwise ops, the reduce_rows / rms_norm reductions and block sizes, the mean and the eps of the RMS norm rounded separately), so the results are bit-identical. The kernels run in place only when a source has exactly the layout of the output. Kill switches: GGML_CUDA_DISABLE_TANH_MUL_ADDSUB_FUSION, GGML_CUDA_DISABLE_ROW_NORM_DIV_FUSION, GGML_CUDA_DISABLE_ADD_RMS_NORM_FUSION. - CUDA: add + rms_norm fusion only on NVIDIA cc 10.0 and newer
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: 68ae3dfcd3
ℹ️ 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".
| // SCALE as scale_f32's FMA, the division as op_div's a / b | ||
|
|
||
| template <bool clamp, bool has_scale> | ||
| static __global__ void row_norm_div_f32(const float * __restrict__ x, float * __restrict__ dst, const int ncols, |
There was a problem hiding this comment.
Use the PDL-safe restrict qualifier in the fused kernels
On Hopper and newer NVIDIA GPUs, PDL is enabled by default, but this kernel unconditionally marks its input __restrict__. As documented beside GGML_CUDA_RESTRICT in common.cuh, that qualifier is incompatible with PDL: it permits non-coherent read-only loads that can observe stale data from an overlapping producer despite the dependency synchronization. Both this kernel and tanh_mul_addsub_f32 use the PDL launch helper, so these fusions can silently produce incorrect results on those GPUs. Use the existing GGML_CUDA_RESTRICT qualifier instead of unconditional __restrict__ in both kernels.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 874c88c. Both tanh_mul_addsub_f32 and row_norm_div_f32 now take plain pointers and copy them into GGML_CUDA_RESTRICT locals, the same pattern set-rows.cu uses. Putting the macro directly in the signature does not compile for templated kernels, because the host and device passes expand it differently. On sm_100 the SASS no longer has LDG.E.CONSTANT loads in either kernel. add_rms_norm_f32 never used restrict. Outputs are bit-identical to the previous head (PPL strings equal, KLD 0 at -ub 1/4/512) and the fusions still engage.
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
… + rms_norm chains (unslothai#412, commit 874c88c) 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
Pin for the prebuilt: upstream b12602 (418fe97) plus this one commit. Base branch base/upstream-418fe97b5.
What it changes
Three elementwise chains that Inkling 2 runs per layer visit become one kernel each (elem-fuse.cu, plus add_rms_norm_f32 in norm.cu):
Each fused kernel rounds like the separate kernels: the same tanhf, no FMA contraction of the elementwise ops (__fmul_rn / __fadd_rn), the reduce_rows and rms_norm reductions and block sizes, and the mean and the eps of the RMS norm rounded separately. The add + rms_norm fusion is dispatched only on NVIDIA cc 10.0 and newer: there (B200, RTX PRO 6000) it is bit-identical to the separate kernels, while on T4, L4, A100 and HIP it came out 1 ulp off (KLD 0.002-0.003), so it stays off there.
Kill switches: GGML_CUDA_DISABLE_TANH_MUL_ADDSUB_FUSION=1, GGML_CUDA_DISABLE_ROW_NORM_DIV_FUSION=1, GGML_CUDA_DISABLE_ADD_RMS_NORM_FUSION=1. test-backend-ops gets TANH_MUL_ADDSUB_FUSE (36), ROW_NORM_DIV_FUSE (25) and ADD_RMS_NORM_FUSE (15).
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)