libr4d: ar_rmsnorm_quant_i8 -- the two-rank exact all-reduce in front of rmsnorm_quant_i8

At two ranks the residual junction follows the row-parallel GEMM's
all-reduce. ar_rmsnorm_quant_i8 is that all-reduce (exact wire, the
ar_rmsnorm_quant_fp8 handshake) and rmsnorm_quant_i8 in one kernel, x
INOUT because it is the message. A deployment on the lossy wire keeps the
standalone all-reduce. Needs a peer rank, so the selftest skips it like
ar_rmsnorm_quant_fp8; byte identity is checked through a model run.
This commit is contained in:
keptn42 committed 2026-10-11 02:23:39 +00:00
1 parent 0a4cf9b893
commit d49d664381
7 files changed
+273 -25

No files matched your search

+167 -25
View File
@@ -34,6 +34,7 @@
#include "r4d_args.h"
#include "r4d_emit.h"
#include "r4d_common.h"
#include "r4d_ar_seq.h"
#include <cstdint>
namespace {
@@ -65,31 +66,17 @@ __device__ __forceinline__ float gain(const void* w, uint32_t i, bool f32) {
// the branch `x` itself or, with POST, rmsnorm(x; w_post). Without RES the norm is of `x`.
// SCL: the sum is multiplied by the [1] scalar before it is stored and normalised.
// `ybuf` holds the row's norm output for the fragment-order store, which reads across threads.
//
// THE ROW BODY, from the junction on: `s1` is the post-norm's factor, already reduced over `xr` by
// the caller (the plain kernel below takes it off the row; the all-reduce form takes it off the
// reduced sum it has just stored). l2 and lm are this function's own reduction planes.
template <bool WF32, bool RES, bool POST, bool SCL, uint32_t THR>
__global__ __launch_bounds__(THR) void rmsnorm_quant_i8_kernel(
const uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w,
const void* __restrict__ wp, const void* __restrict__ sc, uint32_t sc_f32,
signed char* __restrict__ q, float* __restrict__ S, uint16_t* __restrict__ yo,
uint32_t n, float eps, float wadd, uint32_t x_ld, uint32_t r_ld, uint32_t y_ld) {
extern __shared__ uint16_t ybuf[];
__shared__ float l1[THR / kWave], l2[THR / kWave], lm[THR / kWave];
const uint32_t row = blockIdx.x;
const uint16_t* __restrict__ xr = x + (size_t)row * x_ld;
uint16_t* __restrict__ rr = RES ? res + (size_t)row * r_ld : nullptr;
signed char* __restrict__ qr = q + (size_t)row * n;
// ---- the post-norm's sum of squares, over the branch output
float s1 = 0.0f;
if (POST) {
float ss = 0.0f;
for (uint32_t i = threadIdx.x; i < n; i += THR) { const float v = bf2f(xr[i]); ss += v * v; }
ss = block_sum<THR / kWave>(ss, l1);
s1 = rsqrtf(ss / (float)n + eps);
}
__device__ __forceinline__ void junction_row(
const uint16_t* __restrict__ xr, uint16_t* __restrict__ rr, const void* __restrict__ w,
const void* __restrict__ wp, float sv, signed char* __restrict__ qr, float* __restrict__ Sr,
uint16_t* __restrict__ yr, uint32_t n, float eps, float wadd, float s1,
uint16_t* ybuf, float* l2, float* lm) {
// ---- the junction (post-norm, add, scalar), stored, and the norm's sum of squares over it
float sv = 1.0f;
if (SCL) sv = sc_f32 ? ((const float*)sc)[0] : bf2f(((const uint16_t*)sc)[0]);
float ss2 = 0.0f;
for (uint32_t i = threadIdx.x; i < n; i += THR) {
uint16_t v = xr[i];
@@ -111,7 +98,7 @@ __global__ __launch_bounds__(THR) void rmsnorm_quant_i8_kernel(
for (uint32_t i = threadIdx.x; i < n; i += THR) {
const uint16_t y = f32_to_bf16(bf2f(nr[i]) * s2 * (gain(w, i, WF32) + wadd));
ybuf[i] = y;
if (yo) yo[(size_t)row * y_ld + i] = y;
if (yr) yr[i] = y;
m = fmaxf(m, fabsf(bf2f(y)));
}
m = r4d_wave_max(m);
@@ -122,7 +109,7 @@ __global__ __launch_bounds__(THR) void rmsnorm_quant_i8_kernel(
for (uint32_t i = 1; i < THR / kWave; i++) m = fmaxf(m, lm[i]);
const float scale = fmaxf(m, 1e-8f) / 127.f;
const float inv = 1.f / scale;
if (threadIdx.x == 0) S[row] = scale;
if (threadIdx.x == 0) *Sr = scale;
// ---- the codes, in r4d_quant_act_i8's fragment order: byte j of a k step comes from
// lane-half j>>3 and element j&7, i.e. source 8*((j&7)>>2) + 4*(j>>3) + (j&3) inside the step.
@@ -134,6 +121,110 @@ __global__ __launch_bounds__(THR) void rmsnorm_quant_i8_kernel(
}
}
__device__ __forceinline__ float scalar_of(const void* sc, uint32_t f32) {
return f32 ? ((const float*)sc)[0] : bf2f(((const uint16_t*)sc)[0]);
}
template <bool WF32, bool RES, bool POST, bool SCL, uint32_t THR>
__global__ __launch_bounds__(THR) void rmsnorm_quant_i8_kernel(
const uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w,
const void* __restrict__ wp, const void* __restrict__ sc, uint32_t sc_f32,
signed char* __restrict__ q, float* __restrict__ S, uint16_t* __restrict__ yo,
uint32_t n, float eps, float wadd, uint32_t x_ld, uint32_t r_ld, uint32_t y_ld) {
extern __shared__ uint16_t ybuf[];
__shared__ float l1[THR / kWave], l2[THR / kWave], lm[THR / kWave];
const uint32_t row = blockIdx.x;
const uint16_t* __restrict__ xr = x + (size_t)row * x_ld;
// ---- the post-norm's sum of squares, over the branch output
float s1 = 0.0f;
if (POST) {
float ss = 0.0f;
for (uint32_t i = threadIdx.x; i < n; i += THR) { const float v = bf2f(xr[i]); ss += v * v; }
ss = block_sum<THR / kWave>(ss, l1);
s1 = rsqrtf(ss / (float)n + eps);
}
junction_row<WF32, RES, POST, SCL, THR>(
xr, RES ? res + (size_t)row * r_ld : nullptr, w, wp, SCL ? scalar_of(sc, sc_f32) : 1.0f,
q + (size_t)row * n, S + row, yo ? yo + (size_t)row * y_ld : nullptr, n, eps, wadd, s1,
ybuf, l2, lm);
}
// ---------------------------------------------------------------------------------------------
// THE SAME JUNCTION WITH THE TWO-RANK ALL-REDUCE THAT ALWAYS PRECEDES IT: the branch output is a
// rank's partial sum of a column-sharded linear (o_proj, down_proj), and the post-norm is of the
// WHOLE sum. ar_rmsnorm_quant_fp8's handshake, exact wire only: each block pushes the rows it owns
// into the peer's scratch slot as 16B words, publishes its flag, waits for the peer's, and reduces
// `f2bf(mine + peer)` back into `x` -- the bytes the standalone exact all-reduce stores, fp32 add
// commuting exactly so both ranks agree -- on the post-norm's own sum-of-squares pass. A block owns
// whole rows, one a block at a decode batch.
#define R4D_ARQ8_SPIN_MAX 4000000000ULL
__device__ __forceinline__ void arq8_store_rel(unsigned int* p, unsigned int v) {
__hip_atomic_store(p, v, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_SYSTEM);
}
__device__ __forceinline__ unsigned int arq8_load_acq(const unsigned int* p) {
return __hip_atomic_load(p, __ATOMIC_ACQUIRE, __HIP_MEMORY_SCOPE_SYSTEM);
}
template <bool WF32, bool RES, bool POST, bool SCL, uint32_t THR>
__global__ __launch_bounds__(THR) void ar_rmsnorm_quant_i8_kernel(
uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w,
const void* __restrict__ wp, const void* __restrict__ sc, uint32_t sc_f32,
signed char* __restrict__ q, float* __restrict__ S, uint16_t* __restrict__ yo,
int4* peer_base, const int4* my_base, unsigned int* peer_flags, unsigned int* my_flags,
unsigned int* seq_ctrs, int M, uint32_t n, float eps, float wadd, uint32_t r_ld, uint32_t y_ld,
int slot_stride16, int drain, int acq, unsigned int nbw) {
extern __shared__ uint16_t ybuf[];
__shared__ float l1[THR / kWave], l2[THR / kWave], lm[THR / kWave];
__shared__ unsigned int s_seq, s_slot;
const int b = (int)blockIdx.x, nb = (int)gridDim.x;
const uint32_t tid = threadIdx.x;
const int r0 = (int)(((long)b * M) / nb), r1 = (int)(((long)(b + 1) * M) / nb);
r4d_ar_seq_take(seq_ctrs, nbw, s_seq, s_slot); /* device-resident, replay-safe */
const unsigned int sq = s_seq;
int4* const ps = peer_base + (size_t)s_slot * slot_stride16;
const int4* const ms = my_base + (size_t)s_slot * slot_stride16;
/* push my rows into the peer's slot; `x` is a tight [M][n] of bf16 */
const int wpr = (int)(n >> 3);
const int4* const in4 = (const int4*)x;
for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR) ps[k] = in4[k];
if (drain == 1) __threadfence_system();
else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory");
__syncthreads();
if (tid == 0) {
if (drain == 2) __threadfence_system();
arq8_store_rel(&peer_flags[b], sq);
unsigned long long z = 0;
while (r4d_ar_seq_behind(arq8_load_acq(&my_flags[b]), sq)) {
if (++z > R4D_ARQ8_SPIN_MAX) __builtin_trap();
}
}
__syncthreads();
if (acq) __threadfence_system();
const uint16_t* __restrict__ P = (const uint16_t*)ms; /* the peer's rows, in my scratch */
const float sv = SCL ? scalar_of(sc, sc_f32) : 1.0f;
for (int row = r0; row < r1; ++row) {
uint16_t* __restrict__ xr = x + (size_t)row * n;
const uint16_t* __restrict__ pr = P + (size_t)row * n;
float ss = 0.0f;
for (uint32_t i = tid; i < n; i += THR) {
const uint16_t a = f32_to_bf16(bf2f(xr[i]) + bf2f(pr[i]));
xr[i] = a;
if (POST) { const float v = bf2f(a); ss += v * v; }
}
float s1 = 0.0f;
if (POST) s1 = rsqrtf(block_sum<THR / kWave>(ss, l1) / (float)n + eps);
junction_row<WF32, RES, POST, SCL, THR>(
xr, RES ? res + (size_t)row * r_ld : nullptr, w, wp, sv, q + (size_t)row * n, S + row,
yo ? yo + (size_t)row * y_ld : nullptr, n, eps, wadd, s1, ybuf, l2, lm);
__syncthreads(); /* the planes and ybuf are re-used by the next row this block owns */
}
}
} // namespace
// ============================================================== the plugin ABI entry point
@@ -211,3 +302,54 @@ static int rad_rmsnorm_quant_i8_body(const RadArgs* a, const R4dSink& sk) {
return emit_rc;
}
R4D_ENTRY(r4d_rad_rmsnorm_quant_i8, rad_rmsnorm_quant_i8_body)
// The fused all-reduce form's launch. The IPC arguments are r4d_ar_oneshot_2rank_exact's -- the
// double-buffered scratch, the per-block flags and the device-resident sequence counters -- and the
// rest is the junction's. `x` is the message and takes no pitch; `nblocks` is clamped to M (a block
// owns whole rows). Returns 0, or a negative code for a shape it does not serve.
extern "C" int r4d_ar_rmsnorm_quant_i8(long peer_scratch, long my_scratch, long peer_flags,
long my_flags, long seq_ctrs, long slot_stride16,
long x, long res, long w, long wp, long sc, int sc_f32,
long y, long q, long s, int M, int n, float eps, float wadd,
int w_f32, int r_ld, int y_ld, long stream, int nblocks,
int drain, int acq) {
if (!x || !w || !q || !s) return -1;
if (!peer_scratch || !my_scratch || !peer_flags || !my_flags || !seq_ctrs) return -1;
if (M <= 0 || n <= 0) return -2;
if (n % 16) return -3;
if ((wp || sc) && !res) return -5;
if ((size_t)n * sizeof(uint16_t) > 60 * 1024) return -6;
if (!r_ld) r_ld = n;
if (!y_ld) y_ld = n;
int nb = nblocks < 1 ? 1 : nblocks;
if (nb > M) nb = M;
const int nbw = r4d_ar_max_blocks();
if (nb > nbw) nb = nbw;
const int thr = r4d_act_threads(M, n);
const size_t lds = (size_t)n * sizeof(uint16_t);
#define R4D_ARQ8_T(WF, RS, PO, SC, T) \
hipLaunchKernelGGL((ar_rmsnorm_quant_i8_kernel<WF, RS, PO, SC, T>), dim3((uint32_t)nb), \
dim3(T), lds, (hipStream_t)stream, (uint16_t*)x, (uint16_t*)res, \
(const void*)w, (const void*)wp, (const void*)sc, (uint32_t)sc_f32, \
(signed char*)q, (float*)s, (uint16_t*)y, (int4*)peer_scratch, \
(const int4*)my_scratch, (unsigned int*)peer_flags, \
(unsigned int*)my_flags, (unsigned int*)seq_ctrs, M, (uint32_t)n, eps, \
wadd, (uint32_t)r_ld, (uint32_t)y_ld, (int)slot_stride16, drain, acq, \
(unsigned)nbw)
#define R4D_ARQ8(WF, RS, PO, SC) \
do { if (thr == 1024) R4D_ARQ8_T(WF, RS, PO, SC, 1024); \
else if (thr == 512) R4D_ARQ8_T(WF, RS, PO, SC, 512); \
else R4D_ARQ8_T(WF, RS, PO, SC, 256); } while (0)
#define R4D_ARQ8_W(WF) \
do { if (!res) R4D_ARQ8(WF, false, false, false); \
else if (!wp && !sc) R4D_ARQ8(WF, true, false, false); \
else if (wp && !sc) R4D_ARQ8(WF, true, true, false); \
else if (wp && sc) R4D_ARQ8(WF, true, true, true); \
else R4D_ARQ8(WF, true, false, true); } while (0)
if (w_f32) R4D_ARQ8_W(true);
else R4D_ARQ8_W(false);
#undef R4D_ARQ8_W
#undef R4D_ARQ8
#undef R4D_ARQ8_T
return hipGetLastError() == hipSuccess ? 0 : -4;
}