Skip to content

CUDA: fuse tanh-mul-add/sub, router weight normalization and add + rms_norm chains - #412

Open
danielhanchen wants to merge 2 commits into
base/upstream-418fe97b5from
inkperf4-up-elem
Open

danielhanchen wants to merge 2 commits into
base/upstream-418fe97b5from
inkperf4-up-elem

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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

  • y = c -/+ tanh(a)*b (UNARY TANH, MUL, SUB/ADD): the loop controllers, 192 per decode token
  • x / scale_bias(sum_rows(x)) or clamp, with an optional reshape + scale: the router weight normalization, 48 per token
  • ADD followed by RMS_NORM of the sum (through an optional reshape): the residual add before the depth mix, 88 per token. The sum is still written.

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)

  • Inkling-2-Mini decode: -606 kernels per token.
  • Share of the decode set (the set with only these switched off): tg128 -7.3% on UD-Q8_K_XL.
  • KLD against the pin set (UD-IQ1_S, UD-Q4_K_XL, UD-Q8_K_XL, Mainline_Support UD-IQ1_S; 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 -1.3% -2.9%
L4 (8.9) UD-IQ1_S -0.8% -3.0%
L4 (8.9) UD-Q4_K_XL -1.3% -2.3%
A100 (8.0) UD-IQ1_S -1.1% -6.3%
A100 (8.0) UD-Q4_K_XL -0.8% -5.9%
A100 (8.0) UD-Q8_K_XL -1.1% -5.3%
RTX PRO 6000 (12.0) UD-IQ1_S -2.6% -6.3%
RTX PRO 6000 (12.0) UD-Q4_K_XL -3.2% -5.5%
RTX PRO 6000 (12.0) UD-Q8_K_XL -2.8% -5.0%
  • add + rms_norm runs only on cc 10.0 and newer, so of these four GPUs only the RTX PRO 6000 takes it (and stays bit-identical there); T4, L4 and A100 run the other two fusions.

…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
@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-23T04:18:29.338146Z 874c88c 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: 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".

Comment thread ggml/src/ggml-cuda/elem-fuse.cu Outdated
// 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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

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.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 874c88cadd

ℹ️ 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
… + 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

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