Archived
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:
1 parent
b34b1ac844
commit
55da38afbd
4 files changed
+25
-19
No files matched your search
@@ -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 <typename T, typename F>
|
||||
__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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user