Repository navigation
CUDA: HIP: rms_norm: add variants that load float4 at a time and store x in registers - #29720
Conversation
am17an
left a comment
There was a problem hiding this comment.
Thanks, nice change. Tested on RTX Pro 6000, results are all positive or neutral.
ORippler
left a comment
There was a problem hiding this comment.
@IMbackK did you benchmark both wider loads and keeping elements in registers independently? Wider loads are not really expected to help unless you run into an IPC issue on AMD - the memory access pattern is fully coalesced. This kernel is by definition latency-limited (moving 43 kB inputs is not going to saturate any recent HW BW imo, and on CUDA we launch just 1/ at most very few CTAs meaning we cannot saturate memory BW).
The above were generated on gemma4-26b on master, not this PR.
If vpt/keeping things in registers alone is sufficient, it would simplify the PR and reduce templating.
Other observations:
rms_norm_mul_rope_f32/other kernels with the same pattern could benefit from this as well most likely.- For the invocation I opened, we actually could hide latency on CUDA a bit by emitting wider loads (2816 cols / 1024 threads = 2.5 loads per thread on master, and the compiler decided to do the classic 1/4/8/16 optimization based on provided
block_size, so we have a loop and pay 3x the latency of which would be needed). But based on Aman's numbers and previous PRs/attempts E2E performance gains are negligible for NVGPUs (#20520)
Nope i was actually after wider loads and the x in registers just came as an aside. anyhow looks like that x in register actually has the dominant effect but the wider loads help too, numbers from test-backend-ops perf cases:
using half the waves in the reduction should not matter here but the n4 < 32 case is indeed pretty bad. |
|
There are parallel efforts to fix issues with limited CUDA grid sizes: #28175 . The changes are largely orthogonal to each other though. |
|
@IMbackK are you planning to merge this PR? |
|
@am17an yes, i just have limited time atm, rebased and comments addressed current perf relative to master
|
ORippler
left a comment
There was a problem hiding this comment.
Some nits. Should CI times become too long/binary bloat too big we can make that templating conditional for HIP eventually
| float * dst, | ||
| const int ncols, |
There was a problem hiding this comment.
might want to consistify indentation
There was a problem hiding this comment.
I think aligning all of these that just adds diff noise for no gain.
…gml-org#29720 The float4 rms_norm variants (ggml-org#29720) also run on HIP and change the reduction order and the block size, so the outputs differ from r77 (dense Q6_K_M, c 4096: KLD 0.0015 at ub 2048, 0.0019 at ub 4; server text changes). GGML_HIP_RMS_NORM_VEC=0 takes the strided kernel instead: KLD 0 vs R77b and the r77 server text. Default stays on (llama-bench tg128 dense +0.25%).
…_VEC=1 for the float4 variants Keeps the outputs of r77 (bit-identical, KLD 0) so later rounds can still be validated by identity; the float4 kernels were only +0.25% on dense decode.
…e x in registers (ggml-org#29720) * CUDA: HIP: rms_norm: load float4 at a time and store x in registers * Add template paramter comments * use ggml_cuda_is_aligned for src * nits * also use ggml_cuda_is_aligned for src in fused variants * fix nits
|
Issue on an AI PRO R9700 (gfx1201, ROCm 10.0, Windows) with models Qwen3.8-27B UD-Q5_K_XL and draft-mtp n3.
I don't think this is a bug in the PR. The new summation order moves the logits a tiny bit, and that is enough to lower draft acceptance, so decode with speculative decoding gets 7-9% slower. On this GPU and model the prefill gain is only about 1% (same as the R9700 qwen35 rows above), so overall it is a clear loss for us. The direction depends on model/quant, more data in stew675/llama-cpp-rdna-boosts#229. One thing that may be worth fixing anyway: |
…e x in registers (ggml-org#29720) * CUDA: HIP: rms_norm: load float4 at a time and store x in registers * Add template paramter comments * use ggml_cuda_is_aligned for src * nits * also use ggml_cuda_is_aligned for src in fused variants * fix nits
Patch 19's pre-add prologue (x = pre_a + pre_b, stored to pre_dst, fed to the sum of squares) existed only in the scalar rms_norm path, so on b12212 the fused ADD + RMS_NORM + MUL bypassed rms_norm_f32_dispatch and kept the scalar summation order where stock runs the ADD and then ggml-org#29720's float4 RMS_NORM+MUL. The branch was no longer bit-identical to stock there (Q6_K q8_0 ppl 6.0782 vs 6.0940), and the fused scalar kernel took 9.11 us against 3.14 + 5.55 us for stock's pair. The float4 branch now has the prologue: each thread loads its float4 of pre_a and pre_b, adds them component by component (the ADD kernel's float add), writes the sum to pre_dst and keeps it in registers; upstream's squares, reduction, rsqrtf and multiply are unchanged. The pre-add branch of rms_norm_mul_f32_cuda goes through rms_norm_f32_dispatch, which also requires pre_a/pre_b/pre_dst to be 16-byte aligned, so a pre-add row gets stock's block size and vpt (5120/5376 -> <1024, 4, true>). Rows the float4 path refuses keep the scalar pre-add. The float4 1024-thread pre-add runs without the grid-stride loop unless grid y/z are clamped or GGML_CUDA_NORM_GRID_LOOP=1 (40 vs 48 registers). Qwen3.8-27B on 2x P100, -sm tensor: logits byte-identical to stock on Q4_K_M F16 and Q6_K q8_0 (ppl 6.0663 / 6.0940 in every chunk), greedy = stock 4/4; pre-add 6.75 vs 9.12 us per decode call; tg128 +1.27% (Q4_K_M), +1.23% at depth 16384, +0.75% (Q6_K q8_0), MTP production line +0.65%, pp and Gemma 4 unchanged. test-backend-ops gets ADD_RMS_NORM_MUL (29 cases: float4 widths, odd and unaligned rows, in-place, a clamped grid). GGML_CUDA_PRE_ADD_SCALAR=1: the scalar pre-add of b12212. Fold into 19 at the next rebase. Assisted-by: Claude Opus 5.5
…(RMS_NORM+MUL+MROPE/IMROPE fusion, block sizes) into our single-pass kernels The looped upstream norm kernels (ggml-org#28175) cost gemma4 serving 1.6 % in v5.12, so both are ported into the v5.11 single-pass kernels instead of taking upstream's files: - norm.cu: rms_norm_f32 gets a vec_vpt template path (float4 loads, row in registers, block size by ncols/4 as upstream); the launchers try it first when F32 rows are 16-byte aligned and ncols % 4 == 0. GGML_CUDA_RMS_NORM_VEC=0 = v5.43 kernels. - rope.cu: rms_norm_mul_rope_f32 gets MROPE / IMROPE (supports_mrope picked per launch from the mode, so NORMAL / NEOX compile to the old code) and ggml-org#32500's block sizes (GGML_CUDA_NORM_ROPE_TUNE=0 = old 256 / 1024). The fusion check accepts MROPE / IMROPE (GGML_CUDA_NORM_ROPE_MROPE=0 = off) except where the ggml-org#28175 guard kernel would run. - tests: ggml-org#32500's MROPE cases + our widths / head shapes in the eval list (timing under nvprof). Assisted-by: Claude Opus 5.5 (Claude Code)
…rms_norm_f32's vec path add_rms_norm_mul_f32 kept the scalar 1024-thread order, so with the ggml-org#29720 vec path on, fused add+norm no longer matched the unfused norm bit for bit and lost to it on prompt rows (2048 x 512: 79 vs 58 us incl. the separate tail). add_rms_norm_mul_vec uses the vec path's block size / float4-per-thread choice, sum expression and reduction. GGML_CUDA_RMS_NORM_VEC=0 restores the scalar kernels. Assisted-by: Claude Opus 5.5 (Claude Code)
…1d8297) SETTINGS gains the v5.44 bullet: float4 / fitted / fused norms (ggml-org#29720, ggml-org#32500 ports, GDN and h1 norms) change rounding, the shared-expert gate fusion and the conversion caches are bit-identical, ggml-org#32454 runs as a separate kernel; gate R44 numbers and every opt-out. Assisted-by: Claude Opus 5.5 (Claude Code)
Overview
While profiling gemma4 on mi100 at batch 1 decode i noticed that the rms_norm kernels where takeing a large amount of time, relative to mem bandwidth, so i gave float4 loads a try. On the way i noticed that x fits into registers here, which might help on cdna/gcn too since its caches are puny.
Additional information
Benchmarks (benefits mostly on CDNA, RDNA more neutral)
GPU-f980f88ef31631a2
GPU-86308d5dff4ce29e
GPU-575dfb9c7cb709fd
Requirements