Repository navigation
CUDA: two RMS norms in a row in one kernel (Gemma 4 / LFM3 post-norm blocks, Nemotron tg +5%) - #150
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)
|
End-to-end check on the production DFlash preset (
+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 |
|
Review and independent check of this head ( The review found one gap in the guard. #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 |
CUDA: two RMS norms in a row in one kernel (#150 with a guard fix for broadcast weights, checked on the production presets)
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 withrms_norm(y)*w2of that result. These were two launches. Nowrms_norm2_f32does both in one launch: it writes the residualyand the normedz, andzalso as q8_1 blocks when a quantized mat-vec reads it next (asrms_norm_f32does).Change
norm.cu: kernelrms_norm2_f32andggml_cuda_op_rms_norm2. One block per row, the same block size / values per thread asrms_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_norm2matchesRMS_NORM, MUL, ADD [, MUL scalar], RMS_NORM, MULwith the residual and the second product as outputs. An output may take the exact place ofxorr(in-place add of the allocator), any other overlap is not fused.GGML_CUDA_NO_RMS_NORM2=1turns 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 withGGML_CUDA_NO_RMS_NORM2=1(off) and without (on), two rounds interleaved, median of 3 per cell: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