Skip to content

CUDA: two RMS norms in a row in one kernel (Gemma 4 / LFM3 post-norm blocks, Nemotron tg +5%) - #150

Merged
rafasmour merged 2 commits into
masterfrom
opt/rms-norm2-fusion
Dec 4, 2026
Merged

rafasmour merged 2 commits into
masterfrom
opt/rms-norm2-fusion

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

Gemma 4 and LFM3 end every attention and FFN block with a post-norm: rms_norm(x)*w1 + r (Gemma 4 E-models: [*layer_scale]), and the next block starts with rms_norm(y)*w2 of that result. These were two launches. Now rms_norm2_f32 does both in one launch: it writes the residual y and the normed z, and z also as q8_1 blocks when a quantized mat-vec reads it next (as rms_norm_f32 does).

Change

  • norm.cu: kernel rms_norm2_f32 and ggml_cuda_op_rms_norm2. One block per row, the same block size / values per thread as rms_norm_f32, so both sums are made in the same order as before. F32 rows, ncols % 4 == 0, up to 16384 values per row; other shapes are not fused.
  • fusion.cu: ggml_cuda_try_fuse_rms_norm2 matches RMS_NORM, MUL, ADD [, MUL scalar], RMS_NORM, MUL with the residual and the second product as outputs. An output may take the exact place of x or r (in-place add of the allocator), any other overlap is not fused. GGML_CUDA_NO_RMS_NORM2=1 turns it off.
  • test-backend-ops: RMS_NORM2 (k 30..20000, 1/4/7/32 rows, with/without the scale, with a Q8_0 mat-mul that reads both results).

Results

RX 7900 XT (gfx1100), ROCm 10.1, llama-bench -fa on, same binary with GGML_CUDA_NO_RMS_NORM2=1 (off) and without (on), two rounds interleaved, median of 3 per cell:

model test off on
Nemotron 3.5 Bolt E4B UD-Q5_K_XL, f16 K/V tg128 98.9 / 98.7 t/s 103.9 / 103.9 t/s (+5.1%)
same prompt 192 in batches of 4 (the DFlash verify shape) 6.383 / 6.375 ms per batch 6.203 / 6.212 ms (-2.7%)
LFM3 1.7B Q8_0, f16 K/V tg256 229.2 / 230.3 t/s 244.5 / 245.9 t/s (+6.7%)
Gemma 4 31B UD-IQ3_XXS, q8_0 K/V tg128 37.30 / 37.05 t/s 38.13 / 38.10 t/s (+2.5%)

Launches per token (rocprofv3, Nemotron tg): 905 -> 823.

Output: llama-perplexity --kl-divergence (c 512, 4 chunks) of on against off: Nemotron ub 512 and ub 4, LFM3 and Gemma 31B ub 512: mean KLD 0.000000, same top p 100%, PPL equal to 6 digits.

test-backend-ops -b ROCm0: RMS_NORM2 152/152, RMS_NORM 121/121, RMS_NORM_MUL_ADD 152/152, RMS_NORM_MUL_ADD_SCALE 12/12, RMS_NORM_MUL_MAT 146/146.

Note: the timings and tests were taken on a build that also had an env-gated timing experiment (off by default, not part of this branch) on top of these two commits; the experiment only adds a getenv check in the graph capture loop.

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)
@rafasmour

Copy link
Copy Markdown
Owner Author

End-to-end check on the production DFlash preset (nemotron-3.5-bolt-e4b-dflash: Nemotron 3.5 Bolt E4B target + DFlash drafter, LLAMA_SCHED_PER_SIZE=1), llama-server, 6 chat completions (2 code, 2 chat, 2 long answers), 256 tokens each. Same build of these kernels, GGML_CUDA_NO_RMS_NORM2=1 for the "off" run, runs in the order on, off, on:

run greedy t/s preset sampler (seed 1) t/s mean accepted length
on 188.4 186.6 2.58 / 2.55
off 181.7 180.0 2.58 / 2.55
on (again) 188.0 186.3 2.58 / 2.55

+3.6% for both samplers. The text of every completion is identical with and without the fusion (same hashes for greedy and for the seeded preset sampler).

Assisted-by: Claude Opus 5.5

@rafasmour

Copy link
Copy Markdown
Owner Author

Review and independent check of this head (036e26e53), built again with the same cmake options as master and measured against master in one lock block. The results match the description: Nemotron E4B tg128 +5.0 to +5.2%, the DFlash verify shape (4 tokens) +2.4 to +2.5%, LFM3 tg128 +7.1%, Gemma 4 31B tg128 +2.1 to +2.3%, prompt processing unchanged. On the nemotron-3.5-bolt-e4b-dflash preset, over three alternating on/off server runs, greedy is +3.6% and the preset sampler is +3.8%, with the same text in every completion. KL divergence against master is at the floor (mean KLD 0.000000 to 0.000001, same top p 100%), and the numbers are the same with GGML_CUDA_NO_RMS_NORM2=1.

The review found one gap in the guard. weights_ok compares only the number of values and the row alignment of w1 / w2, so a weight broadcast over the rows with ncols values in another shape, such as [4, ncols/4], is fused and gives wrong results: 12 new test cases fail on these kernels. The scale type is also not checked. No model has such weights, so the speed and the outputs above do not change.

#155 contains these two commits unchanged, plus the fix and the new test cases, with all the tables. Merging #155 also merges this PR.

Assisted-by: Claude Opus 5.5

rafasmour added a commit that referenced this pull request Dec 4, 2026
CUDA: two RMS norms in a row in one kernel (#150 with a guard fix for broadcast weights, checked on the production presets)
@rafasmour
rafasmour merged commit 036e26e into master Dec 4, 2026
11 of 27 checks passed
@rafasmour

Copy link
Copy Markdown
Owner Author

Superseded by #155. GitHub shows this PR as merged because #155 contains its two commits unchanged; #155 also adds the guard fix for broadcast weights and the F32 scale check, and the guard for inner chain nodes that are also operands (RMS_NORM2_ALIAS tests). This PR was not merged on its own.

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