diff --git a/docs/OPS.md b/docs/OPS.md index 0d4dc263..c3154694 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -916,6 +916,7 @@ by ~1 ULP, which breaks the replicated-state invariant. The build sets it global | op | params | operands | |---|---|---| | `ar_rmsnorm_quant_fp8` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`:inout, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? | +| `ar_rmsnorm_quant_i8` | `M` `n` `eps`:f64 `world_size` `dtype` `wadd`:f64/opt | `x`:inout, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`?; `w_post`w?, `scalar`w? | | `ar_hc_write` | `M`(range) `n` `hc` `world_size` `dtype` `wire`/opt | `y`:inout, `inj`, `h`:inout | | `ar_gather_hc_write` | `M`(range) `n` `hc` `top_k` `world_size` `dtype` `act`/opt `wire`/opt | `y_expert`, `expert_w`, `sorted_tok`, `shared`?, `shared_gate`?, `y`:inout, `inj`, `h`:inout | | `ar_ln_had_quant_i8` | `M` `n` `eps`:f64 `group` `world_size` `dtype` `wadd`:f64/opt `wire`/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? | diff --git a/libr4d/r4d.h b/libr4d/r4d.h index 72ef2813..94f9b398 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -798,6 +798,15 @@ int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long peer_flag long loc_pack, long loc_scale_off); int r4d_ar_rmsnorm_quant_fp8_chunk_elems(void); +// The same IPC with the sandwich-norm junction after it (r4d_rmsnorm_quant_i8.hip): post-norm of the +// reduced `x` (`wp`), the residual add, the [1] scalar (`sc`), the norm (`w`) and the w4a8 GEMM's +// per-row int8 quantiser. Exact wire only; `x` is the message and takes no pitch. +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); + // ---- sampling ---------------------------------------------------------------------------------- // // One workgroup a position: temperature, softmax, top_k, top_p, min_p, renormalise and the draw, in diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index 2e52c93c..bad16845 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -1231,6 +1231,62 @@ extern "C" int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { return e == 0 ? RAD_OK : RAD_E_SHAPE; } +/* THE ALL-REDUCE AND THE SANDWICH-NORM JUNCTION AFTER IT, int8 out: `all_reduce` (exact) of `x`, then + * `rmsnorm_quant_i8` -- post-norm of the reduced x, the residual add, the scalar, the norm and the + * per-row int8 quantiser the w4a8 skinny GEMM reads. Operands are rmsnorm_quant_i8's with `x` + * INOUT, because it is the message and the reduced sum lands back in it. The exact wire only: a + * deployment that allows the lossy one keeps the standalone all-reduce. Width and block count as + * ar_rmsnorm_quant_fp8 resolves them, for the same reasons: the norm's width from M, one block a + * row. */ +enum { AQ8_X = 0, AQ8_RES, AQ8_W, AQ8_Q, AQ8_S, AQ8_Y, AQ8_WP, AQ8_SC }; + +extern "C" int r4d_rad_ar_rmsnorm_quant_i8(const RadArgs* a, RadStream s) { + const ArInst* in = (const ArInst*)(a ? a->instance : nullptr); + if (!in) return RAD_E_STATE; + if (in->world != 2) return RAD_E_SHAPE; + const RadTensor* x = r4d_opt(a, AQ8_X); + const RadTensor* rs = r4d_opt(a, AQ8_RES); + const RadTensor* w = r4d_opt(a, AQ8_W); + const RadTensor* q = r4d_opt(a, AQ8_Q); + const RadTensor* sc = r4d_opt(a, AQ8_S); + const RadTensor* ob = r4d_opt(a, AQ8_Y); + const RadTensor* wp = r4d_opt(a, AQ8_WP); + const RadTensor* sl = r4d_opt(a, AQ8_SC); + if (!x || !w || !q || !sc) return RAD_E_INVAL; + if ((wp || sl) && !rs) return RAD_E_INVAL; + if (!r4d_contig(w) || (wp && !r4d_contig(wp))) return RAD_E_STRIDE; + if (x->dtype != RAD_BF16 || q->dtype != RAD_I8 || sc->dtype != RAD_F32) return RAD_E_DTYPE; + long long M = 0, K = 0; + if (r4d_rows_of(x, &M, &K) != RAD_OK) return RAD_E_SHAPE; + if (M * K > in->max_numel) return RAD_E_SHAPE; + if (K % 16) return RAD_E_SHAPE; + if (r4d_numel(w) != K || (w->dtype != RAD_F32 && w->dtype != RAD_BF16)) return RAD_E_DTYPE; + if (wp && (r4d_numel(wp) != K || wp->dtype != w->dtype)) return RAD_E_SHAPE; + if (sl && (r4d_numel(sl) != 1 || (sl->dtype != RAD_F32 && sl->dtype != RAD_BF16))) return RAD_E_SHAPE; + if (r4d_numel(q) != M * K || !r4d_contig(q)) return RAD_E_SHAPE; + if (r4d_numel(sc) != M || !r4d_contig(sc)) return RAD_E_SHAPE; + if (ob && r4d_numel(ob) != M * K) return RAD_E_SHAPE; + if (rs && r4d_numel(rs) != M * K) return RAD_E_SHAPE; + const long long xp = r4d_row_pitch(x), yp = ob ? r4d_row_pitch(ob) : 0, rp = rs ? r4d_row_pitch(rs) : 0; + if (xp != K || yp < 0 || rp < 0) return RAD_E_STRIDE; /* x is the message: tight */ + if (r4d_geti_or(a, "wire", 0) != 0) return RAD_E_INVAL; + + R4dArCfg cfgv; + r4d_cfg_ar(a, &cfgv); + int nblocks = cfgv.nblocks > 0 ? cfgv.nblocks : (int)M; + if (nblocks > (int)M) nblocks = (int)M; + if (nblocks > 256) nblocks = 256; + if (nblocks > in->max_blocks) nblocks = in->max_blocks; + if (nblocks < 1) nblocks = 1; + const int e = r4d_ar_rmsnorm_quant_i8( + in->peer_scratch[0], in->my_scratch, in->peer_flags[0], in->my_flags, in->my_seq, + in->slot16, r4d_addr(x), r4d_addr(rs), r4d_addr(w), r4d_addr(wp), r4d_addr(sl), + sl ? (sl->dtype == RAD_F32) : 0, r4d_addr(ob), r4d_addr(q), r4d_addr(sc), (int)M, (int)K, + (float)r4d_getf(a, "eps", 1e-6), (float)r4d_getf(a, "wadd", 0.0), w->dtype == RAD_F32, + (int)rp, (int)yp, (long)(uintptr_t)s, nblocks, cfgv.drain, cfgv.acq); + return e == 0 ? RAD_OK : RAD_E_SHAPE; +} + /* THE ALL-REDUCE WITH THE GATED RESIDUAL'S WRITE IN IT: `all_reduce` of `y` and then `hc_write` * of the reduced `y` into `h`, one kernel. A block whose output feeds a hyper-connection write * skips its own collective when this runs, and the write carries it. diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 7492c192..44ef9f3b 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -305,6 +305,7 @@ int r4d_rad_qkv_norm_rope (const RadArgs*, RadStream); int r4d_rad_qkv_norm_rope_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*); int r4d_rad_gelu_mul_quant_i8 (const RadArgs*, RadStream); int r4d_rad_gelu_mul_quant_i8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*); +int r4d_rad_ar_rmsnorm_quant_i8 (const RadArgs*, RadStream); int r4d_rad_rmsnorm_quant_i8 (const RadArgs*, RadStream); int r4d_rad_rmsnorm_quant_i8_describe(const RadArgs*, RadStream, RadLaunchDesc*, int, int*); int r4d_rad_had_quant_act_i8 (const RadArgs*, RadStream); diff --git a/libr4d/r4d_rmsnorm_quant_i8.hip b/libr4d/r4d_rmsnorm_quant_i8.hip index 0baa5f05..6ceb0ba7 100644 --- a/libr4d/r4d_rmsnorm_quant_i8.hip +++ b/libr4d/r4d_rmsnorm_quant_i8.hip @@ -34,6 +34,7 @@ #include "r4d_args.h" #include "r4d_emit.h" #include "r4d_common.h" +#include "r4d_ar_seq.h" #include 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 -__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(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 +__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(ss, l1); + s1 = rsqrtf(ss / (float)n + eps); + } + junction_row( + 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 +__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(ss, l1) / (float)n + eps); + junction_row( + 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), 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; +} diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 592f8bb9..da325c7b 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -1384,6 +1384,27 @@ static const RadOperandSpec oGeluMulQuantI8[] = { { "out_bf16", RAD_OPD_OUT, 1 }, }; +/* `ar_rmsnorm_quant_i8` is rmsnorm_quant_i8 with the two-rank exact all-reduce of `x` in front: + * same operands, `x` INOUT because the reduced sum lands back in it, plus `world_size`. */ +static const RadParamSpec pArRmsNormQuantI8[] = { + { "M", RAD_P_INT, RAD_REQUIRED }, + { "n", RAD_P_INT, RAD_REQUIRED }, + P_F64("eps"), + { "world_size", RAD_P_INT, RAD_REQUIRED }, + { "dtype", RAD_P_STR, RAD_REQUIRED }, + P_F64O("wadd"), +}; +static const RadOperandSpec oArRmsNormQuantI8[] = { + { "x", RAD_OPD_INOUT, 0 }, + { "residual", RAD_OPD_INOUT, 1 }, + { "w", RAD_OPD_WEIGHT, 0 }, + { "q", RAD_OPD_OUT, 0 }, + { "scale", RAD_OPD_OUT, 0 }, + { "out_bf16", RAD_OPD_OUT, 1 }, + { "w_post", RAD_OPD_WEIGHT, 1 }, + { "scalar", RAD_OPD_WEIGHT, 1 }, +}; + static const RadParamSpec pGatedQuantFp8[] = { { "M", RAD_P_INT, RAD_REQUIRED }, { "n", RAD_P_INT, RAD_REQUIRED }, @@ -2383,6 +2404,8 @@ static const RadOpSchema kSchemas[] = { SCHEMA("gelu_mul_quant_i8", pGeluMulQuantI8, oGeluMulQuantI8, "gelu(gate) * up (tanh form) and the per-row int8 quantiser the w4a8 skinny GEMM reads, in " "one pass."), + SCHEMA("ar_rmsnorm_quant_i8", pArRmsNormQuantI8, oArRmsNormQuantI8, + "the two-rank exact all-reduce of x and rmsnorm_quant_i8 after it, in one kernel."), SCHEMA("gated_quant_fp8", pGatedQuantFp8, oGatedQuantFp8, "silu(a)*b or sigmoid(a)*b, then the same quantiser, in one pass."), SCHEMA("gated_had_quant_fp8", pGatedQuantFp8, oGatedQuantFp8, @@ -3249,6 +3272,10 @@ static const RadConstraint cQkvNormRope[] = { static const RadConstraint cGeluMulQuantI8[] = { C_DIV("n", 16), C_IN("dtype", "bf16"), }; +/* x is the message, pushed as 16B words, and the row is staged in LDS for the fragment store. */ +static const RadConstraint cArRmsNormQuantI8[] = { + C_DIV("n", 16), C_IN("dtype", "bf16"), C_EQ("world_size", 2), C_P2P, +}; static const RadConstraint cHadQuantActI8[] = { C_DIV("n", 16), C_DIV("n", 128), C_LE("group", 512), C_IN("dtype", "bf16"), }; @@ -5667,6 +5694,17 @@ static const RadKernelInfo kKernels[] = { nullptr, nullptr, r4d_rad_gelu_mul_quant_i8, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, r4d_rad_gelu_mul_quant_i8_describe }, +{ "ar_rmsnorm_quant_i8", "ar_rmsnorm_quant_i8", "quant", + "the two-rank exact all-reduce and the junction after it -- post-norm of the reduced sum, residual " + "add, scalar, RMS norm and quant_act_i8's quantiser -- in one kernel", + "exactly 2 ranks; n a multiple of 16, x unpitched (it is the message); one block a row; exact wire", + "bf16 in, int8 out in the w4a8 GEMM's fragment order with an f32 per-row scale; the reduced sum " + "rounded to bf16 as the standalone exact all-reduce stores it", + RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + ROW(cArRmsNormQuantI8), ROW(tAr), nullptr, + r4d_rad_ar_ln_init, r4d_rad_ar_fini, r4d_rad_ar_rmsnorm_quant_i8, nullptr, nullptr, nullptr, + nullptr }, + { "had_quant_act_i8", "had_quant_act_i8", "quant", "block Hadamard along K then per-row symmetric int8 quantisation; the rotation is what makes " "int8 activations usable at all: unrotated, the per-row absmax is set by a few outlier channels " diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 05321fac..a80c69b9 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -13700,6 +13700,7 @@ static void device_phase(const Plugin& pl) { * at every one of the 128 sites, and the generated text is compared byte for byte against a * build that issues the pair instead. */ skip_pair("ar_rmsnorm_quant_fp8", "two-rank fusion: needs a peer rank"); + skip_pair("ar_rmsnorm_quant_i8", "two-rank fusion: needs a peer rank"); dfree_all(); }