diff --git a/libr4d/r4d_common.h b/libr4d/r4d_common.h index 54598bce..221ac160 100644 --- a/libr4d/r4d_common.h +++ b/libr4d/r4d_common.h @@ -299,6 +299,27 @@ __host__ __device__ __forceinline__ int r4d_act_threads(int M, int K) { // width, so a launch at it keeps the order it always had. constexpr uint32_t kR4dSumLanes = 256; +// A row's sum of squares in kR4dSumLanes' partition: lane v < kR4dSumLanes adds f(row[v])^2, +// f(row[v + 256])^2, ... in column order; the lanes past it return 0. The reads go out eight at a +// time before their adds: one at a time, a row the block has just written and now reads back costs +// a memory latency a column (21 in a row for a 5376-wide Gemma 3 hidden state, twice a sandwich +// norm). The adds keep their order, so the bits are the one-at-a-time loop's. +template +__device__ __forceinline__ float r4d_lanes_sumsq(const T* row, uint32_t n, F f) { + float ss = 0.0f; + if (threadIdx.x >= kR4dSumLanes) return ss; + uint32_t i = threadIdx.x; + for (; i + 7 * kR4dSumLanes < n; i += 8 * kR4dSumLanes) { + float v[8]; +#pragma unroll + for (int k = 0; k < 8; ++k) v[k] = f(row[i + k * kR4dSumLanes]); +#pragma unroll + for (int k = 0; k < 8; ++k) ss += v[k] * v[k]; + } + for (; i < n; i += kR4dSumLanes) { const float v = f(row[i]); ss += v * v; } + return ss; +} + // The full-card cliff of the R9700 (gfx1201, 64 CUs). A launch whose wavefront count falls in // the last 32 before a multiple of 2048 runs about five times slower than the same work one // workgroup wider, whatever the kernel does: 256-thread workgroups take 27.6 us at 253 to 256 of diff --git a/libr4d/r4d_fused_quant_fp8.hip b/libr4d/r4d_fused_quant_fp8.hip index 8d0417f5..0fde9e8f 100644 --- a/libr4d/r4d_fused_quant_fp8.hip +++ b/libr4d/r4d_fused_quant_fp8.hip @@ -82,10 +82,7 @@ __device__ __forceinline__ float block_sum(float v, float* lds) { // row's bits do not depend on the block's width. When the block's threads stored `row`, the // caller issues the barrier first, as add_rmsnorm does. __device__ __forceinline__ float lanes_sumsq(const uint16_t* row, uint32_t n) { - float ss = 0.0f; - if (threadIdx.x < kR4dSumLanes) - for (uint32_t i = threadIdx.x; i < n; i += kR4dSumLanes) { const float v = bf2f(row[i]); ss += v * v; } - return ss; + return r4d_lanes_sumsq(row, n, [](uint16_t h) { return bf2f(h); }); } // Quantise one 128-column group from the four bf16-rounded values a lane owns. The clamp in diff --git a/libr4d/r4d_model_bf16.hip b/libr4d/r4d_model_bf16.hip index 05b79cbe..3d6a694f 100644 --- a/libr4d/r4d_model_bf16.hip +++ b/libr4d/r4d_model_bf16.hip @@ -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(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(ss, lds); const float s = rsqrtf(ss / (float)n + eps); diff --git a/libr4d/r4d_rmsnorm_quant_i8.hip b/libr4d/r4d_rmsnorm_quant_i8.hip index a2e57a7a..0ab4af16 100644 --- a/libr4d/r4d_rmsnorm_quant_i8.hip +++ b/libr4d/r4d_rmsnorm_quant_i8.hip @@ -64,10 +64,7 @@ __device__ __forceinline__ float block_sum(float v, float* lds) { // depend on the block's width. `row` holds the row's bf16 values; when the block's threads wrote // it, the caller issues the barrier first. __device__ __forceinline__ float lanes_sumsq(const uint16_t* row, uint32_t n) { - float ss = 0.0f; - if (threadIdx.x < kR4dSumLanes) - for (uint32_t i = threadIdx.x; i < n; i += kR4dSumLanes) { const float f = bf2f(row[i]); ss += f * f; } - return ss; + return r4d_lanes_sumsq(row, n, [](uint16_t h) { return bf2f(h); }); } __device__ __forceinline__ float gain(const void* w, uint32_t i, bool f32) {