libr4d: the norms' lane sum issues its reads eight at a time

1.15's kR4dSumLanes partition folds a row's sum of squares over 256 lanes
whatever the block width, so a 1024-thread decode block reads it back with a
quarter of its threads, one read at a time: a Gemma 3 sandwich norm re-reads
the 5376-wide row it has just written twice, 21 dependent loads a lane each.
TranslateGemma 27B lost 5 % of its decode (four streams 225.0 / 227.2 tok/s a
stream against 238.2 / 239.7 on 1.14; ar_rmsnorm_post_quant_fp8 3.1 -> 5.1 ms
a step under --profile-ops).

r4d_lanes_sumsq (r4d_common.h) keeps the partition and the order of the adds
and only batches the reads, eight before their adds; libr4d is built
-ffp-contract=off, so every bit stays the same (greedy Qwen 300/300 x2 and
TranslateGemma 6/6 identical to the merge without it). Used by both
lanes_sumsq copies and rmsnorm_body's two loops. TranslateGemma back to
238.9 / 239.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-01 20:57:42 +01:00
1 parent b34b1ac844
commit 55da38afbd
4 files changed
+25 -19

No files matched your search

+2 -11
View File
@@ -246,12 +246,7 @@ __device__ __forceinline__ void rmsnorm_body(
const TX* __restrict__ xr = x + (size_t)m * x_ld;
uint16_t* __restrict__ yr = y + (size_t)m * y_ld;
float ss = 0.0f;
if (threadIdx.x < kR4dSumLanes)
for (uint32_t i = threadIdx.x; i < n; i += kR4dSumLanes) {
const float v = act_f32(xr[i]);
ss += v * v;
}
float ss = r4d_lanes_sumsq(xr, n, [](TX h) { return act_f32(h); });
ss = block_sum<THR / kWave>(ss, lds);
const float s = rsqrtf(ss / (float)n + eps);
for (uint32_t i = threadIdx.x; i < n; i += THR) {
@@ -305,11 +300,7 @@ __device__ __forceinline__ void add_rmsnorm_body(
for (uint32_t i = threadIdx.x; i < n; i += THR)
xr[i] = f32_to_bf16(bf16_to_f32(xr[i]) + bf16_to_f32(ar[i]));
__syncthreads();
if (threadIdx.x < kR4dSumLanes)
for (uint32_t i = threadIdx.x; i < n; i += kR4dSumLanes) {
const float v = bf16_to_f32(xr[i]);
ss += v * v;
}
ss = r4d_lanes_sumsq(xr, n, [](uint16_t h) { return bf16_to_f32(h); });
}
ss = block_sum<THR / kWave>(ss, lds);
const float s = rsqrtf(ss / (float)n + eps);