Repository navigation
CUDA: two RMS norms in a row in one kernel (#150 with a guard fix for broadcast weights, checked on the production presets) - #155
Conversation
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
Independent check of #155 (head
|
| 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 hasncolsvalues and passesweights_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
git merge-tree: CUDA: two RMS norms in a row in one kernel (#150 with a guard fix for broadcast weights, checked on the production presets) #155 merges cleanly with opt/inkling2-all, CUDA: one warp per row in the float mat-vec for short rows of tall matrices on RDNA3 #148, server : skip the DFlash target features of prompt rows outside the drafter window (Gemma 4 prompts 4012 -> 6051 t/s) #149, gemma4 : no prune get_rows when every token is an output (Nemotron E4B tg +1.5%) #151, dflash : logits of the drafted rows only (Nemotron DFlash preset -1.96 GiB VRAM) #153 and llama : fill empty MoE cache slots from large batches (first prompt after a load +10% at 712 MiB, +24% at 1400 MiB) #154. It conflicts only with CUDA: store K/V in the cache from the kernel that makes the rows (rotated q8_0/q4_0 cache, Gemma 4 / Inkling 2 V norm) #152 (fusion.cu, norm.cu, test-backend-ops.cpp), as the PR says.- CI:
ubuntu-22-hip-quality-checklists the 24rms_norm2_f32kernels only as new kernels; the kernels that it reports with their VGPR counts areflash_attn_ext_*(and an ignoredrwkv_wkv7). Thecudajob stops at the error in mmvq.cu that was there before (mmvq_mixed_shared_expertdeclared but never referenced) before it compiles norm.cu, so no CI job compiles the new kernel with nvcc (nvcc compiled fusion.cu without an error). - KL tables of the PR: for the Gemma 4 models, PPL(base) is lower than PPL(Q) (44.75 against 50.75, 2246.9 against 5023.2). This is not a difference between the binaries. The master runs that wrote the base logits print the same PPL as PPL(Q) (50.7532 and 5023.2023 in
build-bins/rmsnorm2/results/a-nem-ub512-base.loganda-gem-ub512-base.log). llama-perplexity stores the base log-probs clipped atmax_logit - 16, so a very unlikely correct token gets a too small NLL in the base.
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
…n of #155 and the K/V store fusions)
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 withz = rms_norm(y)*w2. These were two launches.rms_norm2_f32does both in one launch. It writesyandz, and also writeszas 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=1turns it off. On Nemotron 3.5 Bolt E4B this removes 82 of about 905 launches per token (rocprofv3 count in #150).Review fix (bee224e)
w1andw2as one row ofncolsvalues. 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 hasncolsvalues 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 requiresne[0] == ncols. ([1, ncols]was already rejected, because its rows are not 16-byte aligned.)layer_scalefactor was read as a float without a type check. The unfusedRMS_NORM, MUL, ADD, MULpath checks it. Now it must be F32, and no output may overlap it.[1, k],[4, k/4]),xas 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], andlayer_scaleis 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 head036e26e53, 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 withGGML_CUDA_NO_RMS_NORM2=1.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):
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:nemotron-3.5-bolt-e4b-dflash(default)gemma-4-31b-it(MTP drafter, n-max 6)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:[4, k/4]cases fail)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: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
ncols % 4 == 0andncols <= 16384, and weights that are one row. Other shapes use the unfused kernels.norm.cu,fusion.cuandtest-backend-ops.cpp. Whichever of the two is merged second needs a keep-both resolution. The two fusions do not overlap: CUDA: store K/V in the cache from the kernel that makes the rows (rotated q8_0/q4_0 cache, Gemma 4 / Inkling 2 V norm) #152 starts at anRMS_NORMwithout a weight that is followed bySET_ROWS.ubuntu-22-hip-quality-checkflags the VGPRs of aflash_attn_ext_veckernel. The 24 newrms_norm2_f32kernels have no spills.Assisted-by: Claude Opus 5.5