Skip to content

CUDA: two RMS norms in a row in one kernel (#150 with a guard fix for broadcast weights, checked on the production presets) - #155

Merged
rafasmour merged 4 commits into
masterfrom
opt/rmsnorm2-merge
Dec 4, 2026
Merged

rafasmour merged 4 commits into
masterfrom
opt/rmsnorm2-merge

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

This PR is #150 (its two commits, unchanged) plus one commit from the review of #150. It also adds an independent check of the #150 head: speed against master in one lock block, the two production server presets that use the fused kernel, and KL divergence against master. Merge this PR instead of #150. If you merge #150 first, this PR then adds only the last commit.

What #150 does

Gemma 4 and LFM3 end every block with a post-norm, y = rms_norm(x)*w1 + r (Gemma 4 also has [*layer_scale]), and the next block starts with z = rms_norm(y)*w2. These were two launches. rms_norm2_f32 does both in one launch. It writes y and z, and also writes z as q8_1 blocks when a quantized mat-vec reads it next. Both sums are made in the same order as in the unfused kernels: perplexity and every generated text are the same with and without the fusion (below). GGML_CUDA_NO_RMS_NORM2=1 turns it off. On Nemotron 3.5 Bolt E4B this removes 82 of about 905 launches per token (rocprofv3 count in #150).

Review fix (bee224e)

  • The kernel reads w1 and w2 as one row of ncols values. CUDA: two RMS norms in a row in one kernel (Gemma 4 / LFM3 post-norm blocks, Nemotron tg +5%) #150 checked only the number of values and the 16-byte alignment of the rows. A weight that is broadcast over the rows and has ncols values in another shape, for example [4, ncols/4], was fused and gave wrong results. On the CUDA: two RMS norms in a row in one kernel (Gemma 4 / LFM3 post-norm blocks, Nemotron tg +5%) #150 kernels, the 12 new test cases with [4, k/4] weights fail (NMSE 0.5 to 1.8), and they pass with the fusion off. The check now also requires ne[0] == ncols. ([1, ncols] was already rejected, because its rows are not 16-byte aligned.)
  • The layer_scale factor was read as a float without a type check. The unfused RMS_NORM, MUL, ADD, MUL path checks it. Now it must be F32, and no output may overlap it.
  • New test cases: weights broadcast over the rows ([1, k], [4, k/4]), x as a view of longer rows, and rows in channels (3-D), with and without a Q8_0 mat-vec that reads both results. With these, RMS_NORM2 has 200 test cases.

No model has such weights: norm weights are F32 [n_embd], and layer_scale is F32 [1]. The fused kernel runs at the same places as in #150, and the two builds have the same speed (second table).

Results

RX 7900 XT (gfx1100), ROCm 10.1. Binaries: master 951890211 (build-bins/ink2/base), the #150 head 036e26e53, and this branch, all built with the same cmake options. llama-bench -fa on. Each cell is the median of all samples: two rounds in opposite order inside one lock block, 3 runs per round (LFM3: 5). "off" is the same binary with GGML_CUDA_NO_RMS_NORM2=1.

model test master #150 #150 off #150 vs master
Nemotron 3.5 Bolt E4B UD-Q5_K_XL, f16 K/V tg128 97.66 102.55 98.04 +5.0%
prompt 192 in ubatches of 4 (DFlash verify) 627.8 643.4 628.4 +2.5%
pp512 7665 7794 7708 +1.7%
pp1024 (ubatch 1024, as in production) 8001 8130 8151 +1.6%
Gemma 4 31B UD-IQ3_XXS, q8_0 K/V tg128 37.08 37.91 37.20 +2.3%
prompt 112 in ubatches of 7 (MTP verify) 144.78 146.72 145.02 +1.3%
pp512 806.6 808.8 807.3 +0.3%
LFM3 1.7B Q8_0, f16 K/V tg128 224.1 239.9 226.7 +7.1%
pp512 12258 12359 12294 +0.8%

Prompt processing does not change. Two launches less per layer are very small next to a 512-token ubatch, and "off" moves by as much as "on" (+0.1% to +1.9%). Depth was not tested: the work of the fused kernel per token does not depend on the context.

#150 head against this branch, in a second block with less load on the host (load 1.4 to 2.9, against 6 to 11 in the first block, where builds of other jobs were running):

model test off #150 this branch gain
Nemotron E4B tg128 98.62 103.70 103.74 +5.2%
prompt 192 in ubatches of 4 632.96 647.89 648.39 +2.4%
Gemma 4 31B tg128 37.29 38.08 38.08 +2.1%

Production presets: llama-server with LLAMA_SCHED_PER_SIZE=1, 6 chat completions of 256 tokens (2 code, 2 chat, 2 long answers), greedy and the preset sampler with seed 1, t/s of the generation:

preset binary sampler off on gain
nemotron-3.5-bolt-e4b-dflash (default) this branch, on / off alternating, 3 runs each greedy 181.7 (181.3-182.1) 188.2 (187.8-188.8) +3.6%
preset 179.3 (179.2-179.3) 186.1 (185.8-186.4) +3.8%
gemma-4-31b-it (MTP drafter, n-max 6) #150 head, 1 run each greedy 69.5 69.9 +0.6%
preset 64.1 65.0 +1.4%

In every run, each completion has the same text with and without the fusion (same hashes, same draft acceptance and mean accepted length: 2.58 / 2.55 for DFlash, 3.94 / 3.63 for MTP). For Nemotron, master gives the same text and the same speed as "off" (177.1 vs 176.6 t/s greedy in the first block). The gemma4-assistant MTP drafter has the same post-norm blocks, so the fusion also runs in the drafter.

Note on the first block: under its high load, each of its two DFlash "on" runs had one slow completion in the preset sampler pass (47 and 87 t/s; the same completion runs at 162 t/s off and 173 t/s on elsewhere), with the same text. The second block ran three on/off pairs at low load and had no slow completion: each completion was within 1% of the same completion in the other runs of the same setting.

Correctness

test-backend-ops -b ROCm0:

binary RMS_NORM2 on RMS_NORM2 off other RMS_NORM fusions
#150 head 152/152 152/152 950/950
#150 kernels + tests of this branch 188/200 (the 12 [4, k/4] cases fail) 200/200 -
this branch 200/200 200/200 950/950

The other RMS_NORM fusions are RMS_NORM 121, RMS_NORM_MUL_ADD 152, RMS_NORM_MUL_ADD_SCALE 12, RMS_NORM_MUL_MAT 146, RMS_NORM_MUL_ROPE 509 and RMS_NORM_SCALE 10 test cases.

KL divergence against master: llama-perplexity -c 512 --chunks 4 -fa on. The base logits come from the master binary. The test is the #150 head with the fusion on, and then with it off:

model ubatch mean KLD max KLD same top p
Nemotron E4B, f16 K/V 512 0.000001 0.000067 100%
4 0.000000 0.000058 100%
LFM3 1.7B, f16 K/V 512 0.000000 0.000053 100%
4 0.000001 0.000040 100%
Gemma 4 31B, q8_0 K/V 512 0.000000 0.000046 100%
7 0.000000 0.000057 100%

With the fusion off, the numbers are the same to the last digit (PPL, mean and max KLD). The small remaining KLD comes from the stored base logits, which are quantized. Ubatch 4 and 7 cover the mat-vec path, where the fused kernel can also write the q8_1 copy.

Limits

Assisted-by: Claude Opus 5.5

rms_norm*w1 + r [*scale] -> rms_norm*w2 runs as one launch: the residual
(add or scale node) and the second product are written, the second
product also as q8_1 blocks when a quantized mat-vec reads it next.
Gemma 4 has this chain twice per layer (post-attention norm -> FFN norm,
post-PLE norm + layer scale -> next attention norm), LFM3 as well.

The block sizes are those of rms_norm_f32, so both sums are made in the
same order as by the unfused kernels. GGML_CUDA_NO_RMS_NORM2 turns it off.

Tests: test-backend-ops RMS_NORM2 (with and without the scale, with a
Q8_0 mat-mul that reads both results).

Assisted-by: Claude Opus 5.5
Assisted-by: Claude Opus 5.5
(cherry picked from commit ddb427435ea0b11bc7ce7c71a1f7802e5d7b406a)
The fused kernel reads w1 and w2 as one row of ncols values. The check
compared only the number of values and the 16-byte alignment, so a
weight broadcast over the rows with ncols values in another shape
(e.g. [4, ncols/4]) was fused and the result was wrong. The scale was
read as float without a check of its type.

Check ne[0] of the weights and the type of the scale, and do not fuse
when an output overlaps the scale. New test cases: weights broadcast
over the rows, x as a view of longer rows, rows in channels.

Assisted-by: Claude Opus 5.5
@rafasmour

Copy link
Copy Markdown
Owner Author

Independent check of #155 (head bee224e6f)

Result: the speed and output claims hold. I found one more gap in the guard (an inner node of the chain that is also an operand of the chain). No model graph that reaches this fusion has it. Below are 20 failing test cases and a 6-line fix.

Build. I built bee224e6f again in my own worktree (build-bins/wt-rn2v, with tools/hipbuild.sh, the same cmake options as ink2/base). The only local change is new test cases in tests/test-backend-ops.cpp; the library code is the PR's. Master is build-bins/ink2/base (tree of 951890211). Binaries, scripts and logs are in build-bins/rn2v/.

test-backend-ops -b ROCm0

cases fusion on GGML_CUDA_NO_RMS_NORM2=1
RMS_NORM2 (the 200 cases of the PR) 200/200 200/200
RMS_NORM2_EDGE (new: k = 4, 132, 1028, 4100, 16384, 16388 with 1 and 5 rows; k 2560 x 512 rows; k 16384 + Q8_0 mat-vec; k 5376 x 8 rows + Q4_0 mat-vec; each with and without the scale) 30/30 30/30
RMS_NORM2_ALIAS (new, see below) 0/20 20/20
RMS_NORM, RMS_NORM_MUL_ADD, RMS_NORM_MUL_ADD_SCALE, RMS_NORM_MUL_MAT, RMS_NORM_MUL_ROPE, RMS_NORM_SCALE 950/950 -

Gap: an inner node of the chain is also an operand

ggml_can_fuse_subgraph checks only that every use of an inner node (norm1, mul1, norm2, and the add in front of a scale) is inside the chain. It allows more than one use. The fused kernel does not write these nodes. So when one of them is also r, w1 or w2, the kernel reads a buffer that nothing wrote. RMS_NORM2_ALIAS has 5 such graphs, each with and without the scale:

  • y = mul1 + mul1, y = mul1 + norm1 (4 rows)
  • mul1 = norm1*norm1, z = norm2*norm2, z = norm2*mul1 (1 row, so that the operand has ncols values and passes weights_ok)

All 20 fail with the fusion on (NMSE 0.70 to 8.1) and pass with it off. The RMS_NORM, MUL, ADD [, MUL] paths of master do not have this problem, because ggml_can_fuse requires one use for each inner node. None of the graphs that reach this fusion (Gemma 4, the Gemma 4 MTP assistant, LFM3, the Gemma 4 vision and audio encoders) has such a chain, so the speed and the outputs below do not change. Fix: require one use for each inner node.

     if (!ggml_can_fuse_subgraph(cgraph, i, n1 + 2, n1 == 4 ? ops4 : ops3, outputs, 2)) {
         return 0;
     }
+    // the kernel does not write norm1, mul1, norm2 (and the add before a scale): each must have one use, not be an operand of the chain too
+    for (int k = 0; k <= n1; k++) {
+        if (i + k != outputs[0] && ggml_node_get_use_count(cgraph, i + k) != 1) {
+            return 0;
+        }
+    }

With the fix: RMS_NORM2_ALIAS 20/20, RMS_NORM2 200/200, RMS_NORM2_EDGE 30/30, the other norm fusions 950/950. The fused kernel still runs in the models (same tg as the PR head, table below). The two new test structs are in build-bins/rn2v/verify-tests.diff and the fix is in build-bins/rn2v/alias-fix.diff.

Speed

RX 7900 XT, ROCm 10.1, llama-bench -fa on, depth 0. One lock block, rounds master / on / off, then off / on / master, 3 runs each (LFM3: 5). Each cell is the median of all 6 samples (LFM3: 10). The first round ran at load 2. During the second round, another agent's 6-job build started (load 4 to 8). This gave outliers in some samples, so for each test I also give the change in the first round only.

model test master #155 #155 off #155 vs master #155 vs off first round only, vs master / vs off
Nemotron 3.5 Bolt E4B UD-Q5_K_XL, f16 K/V tg128 98.04 103.00 97.89 +5.1% +5.2% +5.1% / +5.2%
prompt 192 in ubatches of 4 628.2 645.6 629.6 +2.8% +2.5% +2.7% / +2.6%
pp1024, ubatch 1024 8052 8297 8123 +3.0% +2.1% +0.5% / +2.0% (noise)
Gemma 4 31B UD-IQ3_XXS, q8_0 K/V tg128 37.21 38.06 37.24 +2.3% +2.2% +2.2% / +2.0%
pp512 809.1 813.3 807.5 +0.5% +0.7% +0.5% / +0.5%
LFM3 1.7B Q8_0, f16 K/V tg128 226.1 241.5 226.8 +6.8% +6.5% +7.0% / +7.2%
Ornith 1.5 9B Q4_K_M (qwen35, not in the claim; no post-norm pairs, so no fusion) pp512 3101.6 3107.3 3105.0 +0.2% +0.1%
tg128 102.27 102.49 102.15 +0.2% +0.3%

The fix keeps the fusion in the models. Second block (load 2 to 3), rounds #155 / #155 + fix / off, then off / #155 + fix / #155, 3 runs each (LFM3: 5), median of all samples:

model test #155 #155 + fix #155 off
Nemotron 3.5 Bolt E4B tg128 103.75 103.80 98.84
LFM3 1.7B tg128 244.67 244.80 229.38

Production presets that the PR did not test

The mmproj encoders of Gemma 4 have the same post-norm blocks (attn_post_norm -> add -> ln2, ffn_post_norm -> add -> next ln1 in build_vit; attn_post_norm -> add -> norm_conv in the audio conformer), with F32 norm weights. So the fused kernel also runs in the encoders. The server runs of the PR used --unset mmproj, so this path was not covered.

Kernel trace (rocprofv3, HIP graphs off) of the default preset with its mmproj (nemotron-3.5-bolt-e4b-dflash without the drafter): an image request has 30 rms_norm2_f32 launches with 256 threads over 1170 rows (the 768-wide vision encoder), and an audio request has 12 such launches over 436 rows (the 1024-wide audio encoder). These come in addition to the launches of the text layers (1024 threads).

Greedy, 160 tokens, LLAMA_SCHED_PER_SIZE=1, the full preset arguments (preset2args.py):

preset prompts master #155 #155 off
nemotron-3.5-bolt-e4b-dflash (default, with DFlash and its mmproj: vision 768 wide, audio 1024 wide) image (twice), text, audio 5556dd75 5556dd75 5cb155c9 571e8506 same same
gemma-4-31b-it-vision (mmproj ViT 1152 wide, 27 blocks) image (twice), text 100b4787 100b4787 5be7ea93 same same

The prompt time of the image requests (encoder included) does not change: Nemotron 160 / 162 / 156 ms (master / on / off, second image request), Gemma 31B 424 / 421 / 428 ms.

KL divergence against master

LFM3 1.7B Q8_0, f16 K/V, llama-perplexity -c 512 --chunks 4 -fa on, base logits from the master binary:

ubatch PPL mean KLD max KLD same top p
512 12.506849 0.000000 0.000053 100%
4 12.477966 0.000001 0.000040 100%
2048, 4 sequences of 512 in one ubatch 12.520034 0.000001 0.000051 100%

The PPL at ubatch 512 and 4 is the same to the last digit as in the PR.

Other checks

Assisted-by: Claude Opus 5.5

… operand

ggml_can_fuse_subgraph allows more than one use of an inner node as long
as every use is inside the chain. The fused kernel does not write norm1,
mul1, norm2 (and the add before a scale), so when one of them is also r,
w1 or w2 the kernel read a buffer that nothing wrote. Require one use for
each inner node.

New test cases: RMS_NORM2_ALIAS (y = mul1 + mul1, y = mul1 + norm1,
mul1 = norm1*norm1, z = norm2*norm2, z = norm2*mul1, with and without
the scale; 0/20 before, 20/20 with the fix) and RMS_NORM2_EDGE (shapes at
the edges of the block sizes, 30/30).

Assisted-by: Claude Opus 5.5
@rafasmour
rafasmour merged commit 49f4b8d into master Dec 4, 2026
11 of 27 checks passed
rafasmour added a commit that referenced this pull request Dec 4, 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.

1 participant