diff --git a/docs/OPS.md b/docs/OPS.md index e432d00a..cf02fa96 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -635,6 +635,8 @@ copy. |---|---|---| | `had_quant_act_i8g` | `M` `n` `group` `dtype` | `x` → `q`, `scale` | | `gated_had_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate_up` → `q`, `scale`, `out_bf16`? | +| `gated_had_quant_i8g` | `M` `n` `group` `dtype` `act`:str/opt | `gate_up` → `q`, `scale`, `out_bf16`? | +| `rmsnorm_had_quant_i8g` | `M` `n` `eps`:f64 `group` `dtype` `wadd`:f64/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`? | | `gate_quant_fp8` | `M` `n` `group` `dtype` `act`:str/opt | `gate`, `x` → `q`, `scale`, `out_bf16`? | | `rmsnorm_quant_i8` | `M` `n` `eps`:f64 `dtype` `wadd`:f64/opt | `x`, `residual`:inout?, `w`w → `q`, `scale`, `out_bf16`?; `w_post`w?, `scalar`w? | | `gelu_mul_quant_i8` | `M` `n` `dtype` | `gate_up` → `q`, `scale`, `out_bf16`? | @@ -642,6 +644,15 @@ copy. `gelu_mul_quant_i8` is `gelu_mul` with `rmsnorm_quant_i8`'s quantiser on its pass, byte-identical to the pair. +`gated_had_quant_i8g` and `rmsnorm_had_quant_i8g` are `gated_quant_fp8` and `rmsnorm_quant_fp8` +with `had_quant_act_i8g`'s quantiser on their pass: the `group`-wide rotation of the bf16-rounded +value, then int8 codes per (row, `group`), byte-identical to the producer followed by +`had_quant_act_i8g`. They feed a weight at `i8a8h` without the activation quantiser of its own: +one launch for a norm read by three projections (q, k and v), and none between the gated product and +the down projection. `out_bf16` receives the unrotated value, for a bf16 consumer. `n` is a +multiple of `group`. A rotated code read as a plain one, or the reverse, gives a model that runs +and is wrong, so these are separate ops, as `gated_had_quant_fp8` is. + `rmsnorm_quant_i8` is `rmsnorm` with `quant_act_i8`'s quantiser on its pass: step-order int8 codes and one f32 scale per row, what `gemm_nt_q` reads at `dtype=w4a8` and `w8a8`. With `residual` the norm is of `residual + x`, stored back; `w_post` normalises `x` before that add and `scalar` (`[1]`) @@ -2097,6 +2108,7 @@ device code. |---|---|---| | `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_post_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`?; `w_post`w | +| `ar_rmsnorm_had_quant_i8g` | `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 | @@ -2106,6 +2118,8 @@ device code. `wire` is the payload width on the link: 0 is exact bf16, 6 the rotated six-bit form. It is a parameter for the same reason as `min_bytes`. `x` is inout on `ar_rmsnorm_quant_fp8`: it is both the message and the input, and the reduced sum lands in it. +`ar_rmsnorm_had_quant_i8g` is `ar_rmsnorm_quant_fp8` with `rmsnorm_had_quant_i8g`'s quantiser: +`x` and the residual are the same bytes, and the codes are int8 for a weight at `i8a8h`. The norm-carrying forms are two-rank only. `ar_hc_write` and `ar_gather_hc_write` also have an exact-wire four-rank form. At other widths the arch blocks issue the plain ops. diff --git a/libr4d/r4d.h b/libr4d/r4d.h index adc78ab3..e62ee672 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -713,6 +713,15 @@ int r4d_rmsnorm_quant_fp8(long x, long res, long w, long y, long q, long s, int int q_ld, int s_ld, long stream); int r4d_gated_quant_fp8(long gu, long y, long q, long s, int M, int n, int mode, int gu_ld, int y_ld, int q_ld, int s_ld, long stream); +// The two above with had_quant_act_i8g's quantiser on the pass instead (the `_had_quant_i8g` +// forms): the width-128 Hadamard, then int8 codes at amax / 127 per (row, 128), for a weight at +// i8a8h. `q` is int8, `n` a multiple of 128, and `y` keeps the unrotated bf16 value. The codes +// are the producer + had_quant_act_i8g pair's bytes. +int r4d_rmsnorm_had_quant_i8g(long x, long res, long w, long y, long q, long s, int M, int n, + float eps, float wadd, int w_f32, int x_ld, int r_ld, int y_ld, + int q_ld, int s_ld, long stream); +int r4d_gated_had_quant_i8g(long gu, long y, long q, long s, int M, int n, int mode, + int gu_ld, int y_ld, int q_ld, int s_ld, long stream); // The activation is rounded to bf16 before the product, matching the unfused `sigmoid` then `mul` // pair with a bf16 buffer between them. // The same epilogue with the gate in a different buffer from what it scales, as in gated @@ -738,6 +747,15 @@ int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long peer_flag int nblocks, int nthreads, int drain, int acq, int wire, long loc_pack, long loc_scale_off, long wp = 0); int r4d_ar_rmsnorm_quant_fp8_chunk_elems(void); +// Its rotated int8 form, as r4d_rmsnorm_had_quant_i8g: the reduction and the norm unchanged, the +// norm's pass quantised by had_quant_act_i8g's rule. No post-norm. +int r4d_ar_rmsnorm_had_quant_i8g(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 y, long q, long s, + int M, int n, float eps, float wadd, int w_f32, + int r_ld, int y_ld, int q_ld, int s_ld, long stream, + int nblocks, int nthreads, int drain, int acq, int wire, + long loc_pack, long loc_scale_off); // 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 diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index 4c532109..f735b5b9 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -1240,7 +1240,9 @@ extern "C" int r4d_rad_ar_ln_had_quant_i8(const RadArgs* a, RadStream s) { * chooses it as the standalone `all_reduce` row would (lossy wire allowed, group tiles the message, * size above --tp-wire-min-kb), and both ranks decide on the same T. A wire-6 chunk that would * straddle a row is refused as a shape. */ -extern "C" int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { +/* `hi8`: ar_rmsnorm_had_quant_i8g, the same with had_quant_act_i8g's quantiser on the norm's pass + * (int8 codes, K whole 128s, no post-norm). */ +static int rad_ar_rmsnorm_quant(const RadArgs* a, RadStream s, bool hi8) { const ArInst* in = (const ArInst*)(a ? a->instance : nullptr); if (!in) return RAD_E_STATE; if (in->world != 2) return RAD_E_SHAPE; @@ -1252,9 +1254,11 @@ extern "C" int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { const RadTensor* sc = r4d_opt(a, AL_SCALE); const RadTensor* ob = r4d_opt(a, AL_OUTBF16); /* ar_rmsnorm_quant_fp8's optional post-norm (Gemma 3's sandwich); ar_ln_had_quant_i8 has no - * seventh operand, and this function serves only the fp8 op. */ + * seventh operand, and this function serves only the fp8 op and its rotated int8 twin. */ const RadTensor* wpo = r4d_opt(a, AL_WPOST); if (!x || !w || !q || !sc) return RAD_E_INVAL; + if (hi8 && q->dtype != RAD_I8) return RAD_E_DTYPE; + if (hi8 && wpo) return RAD_E_UNSUPPORTED; if (!r4d_contig(w)) return RAD_E_STRIDE; if (wpo && (!rs || !r4d_contig(wpo) || r4d_numel(wpo) != r4d_numel(w) || wpo->dtype != w->dtype)) return RAD_E_SHAPE; @@ -1272,6 +1276,7 @@ extern "C" int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { const long long grp = r4d_quant_act_fp8_group(); const long long nkb = (K + grp - 1) / grp; if (r4d_geti_or(a, "group", grp) != grp) return RAD_E_SHAPE; + if (hi8 && K % grp) return RAD_E_SHAPE; if (r4d_numel(w) != K) return RAD_E_SHAPE; if (w->dtype != RAD_F32 && w->dtype != RAD_BF16) return RAD_E_DTYPE; if (q->shape[0] != M || q->shape[1] != K) return RAD_E_SHAPE; @@ -1327,17 +1332,34 @@ extern "C" int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { if (nblocks > in->max_blocks) nblocks = in->max_blocks; if (nblocks < 1) nblocks = 1; - const int e = r4d_ar_rmsnorm_quant_fp8( - 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(ob), r4d_addr(q), - r4d_addr(sc), (int)M, (int)K, eps, wadd, w_f32, - (int)rp, (int)yp, (int)qp, (int)sp, - (long)(uintptr_t)s, nblocks, nthreads, cfgv.drain, cfgv.acq, wire, - in->locpack, in->slot_bytes / 2, wpo ? r4d_addr(wpo) : 0); + const int e = hi8 + ? r4d_ar_rmsnorm_had_quant_i8g( + 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(ob), r4d_addr(q), + r4d_addr(sc), (int)M, (int)K, eps, wadd, w_f32, + (int)rp, (int)yp, (int)qp, (int)sp, + (long)(uintptr_t)s, nblocks, nthreads, cfgv.drain, cfgv.acq, wire, + in->locpack, in->slot_bytes / 2) + : r4d_ar_rmsnorm_quant_fp8( + 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(ob), r4d_addr(q), + r4d_addr(sc), (int)M, (int)K, eps, wadd, w_f32, + (int)rp, (int)yp, (int)qp, (int)sp, + (long)(uintptr_t)s, nblocks, nthreads, cfgv.drain, cfgv.acq, wire, + in->locpack, in->slot_bytes / 2, wpo ? r4d_addr(wpo) : 0); return e == 0 ? RAD_OK : RAD_E_SHAPE; } +extern "C" int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { + return rad_ar_rmsnorm_quant(a, s, false); +} + +extern "C" int r4d_rad_ar_rmsnorm_had_quant_i8g(const RadArgs* a, RadStream s) { + return rad_ar_rmsnorm_quant(a, s, true); +} + /* The all-reduce and the sandwich-norm junction after it, int8 out: exact `all_reduce` of `x`, then * `rmsnorm_quant_i8` (post-norm of the reduced x, residual add, scalar, norm and the per-row int8 * quantiser the w4a8 skinny GEMM reads). Operands are rmsnorm_quant_i8's with `x` inout, since the diff --git a/libr4d/r4d_fused_quant_fp8.hip b/libr4d/r4d_fused_quant_fp8.hip index ea31d0dd..4f9853f0 100644 --- a/libr4d/r4d_fused_quant_fp8.hip +++ b/libr4d/r4d_fused_quant_fp8.hip @@ -101,7 +101,13 @@ __device__ __forceinline__ void quant_group(const float v[4], float* __restrict_ // consecutive columns, so the group's amax is a wave reduction and the store a single dword. `nr` // is the row the norm is taken of, already in memory (see the bit-identity note above). Shared so // the fused all-reduce cannot drift from the standalone norm. -template +// +// HI8 is had_quant_act_i8g's form (the `_had_quant_i8g` ops): the width-128 Hadamard between the +// bf16-rounded norm output and the absmax, then int8 codes, as gated_quant_fp8_kernel's ROT and I8 +// together. r4d_quant_act_fp8's kernel has the same mapping, rotation and rounding, so the codes +// are the norm + had_quant_act_i8g pair's bytes. `y` still receives the unrotated norm output. +// The launchers require n % 128 == 0, so no rotated group contains padding. +template __device__ __forceinline__ void norm_quant_row( const uint16_t* __restrict__ nr, const void* __restrict__ w, uint16_t* __restrict__ yr, uint8_t* __restrict__ qr, float* __restrict__ s, size_t s_row, uint32_t n, uint32_t nkb, @@ -128,13 +134,14 @@ __device__ __forceinline__ void norm_quant_row( v[j] = bf2f(h); } } - quant_group(v, s, qr, s_row + kb, c0, n, lane); + if constexpr (HI8) r4d_fwht_wave<(int)kGroup>(v, (int)lane); + quant_group(v, s, qr, s_row + kb, c0, n, lane); } } // ---------------------------------------------------------------------------- rmsnorm -> quant // One block per row. Pass one is the block-wide sum of squares; pass two is norm_quant_row. -template +template __global__ __launch_bounds__(THR) void rmsnorm_quant_fp8_kernel( const uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w, uint16_t* __restrict__ y, uint8_t* __restrict__ q, float* __restrict__ s, uint32_t n, @@ -178,8 +185,8 @@ __global__ __launch_bounds__(THR) void rmsnorm_quant_fp8_kernel( } const float rs = rsqrtf(block_sum(ss, lds) / (float)n + eps); - norm_quant_row(nr, w, y ? y + (size_t)row * y_ld : nullptr, q + (size_t)row * q_ld, - s, (size_t)row * s_ld, n, nkb, rs, wadd); + norm_quant_row(nr, w, y ? y + (size_t)row * y_ld : nullptr, + q + (size_t)row * q_ld, s, (size_t)row * s_ld, n, nkb, rs, wadd); } // ---------------------------------------------------------------------------- gate -> quant @@ -366,7 +373,10 @@ __device__ __forceinline__ unsigned int arq_load_acq(const unsigned int* p) { // POST: a sandwich-norm junction (Gemma 3): the reduced sum is post-normed against `wp` before the // residual add, `v = bf16(sum * s1 * (wp + wadd))` with s1 the sum's own rms, as rmsnorm_quant_i8's // POST computes it. `x` still receives the reduced sum. Exact wire and a residual only. -template +// +// HI8: ar_rmsnorm_had_quant_i8g, pass two in norm_quant_row's rotated int8 form. The reduction and +// the norm are untouched, so `x`, the residual and `y` are ar_rmsnorm_quant_fp8's bytes. +template __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w, const void* __restrict__ wp, @@ -584,8 +594,9 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( } /* block_sum's barrier is also what publishes pass one's stores to pass two's reads. */ const float rs = rsqrtf(block_sum(ss, lds) / (float)n + eps); - norm_quant_row(nr, w, y ? y + (size_t)row * y_ld : nullptr, - q + (size_t)row * q_ld, s, (size_t)row * s_ld, n, nkb, rs, wadd); + norm_quant_row(nr, w, y ? y + (size_t)row * y_ld : nullptr, + q + (size_t)row * q_ld, s, (size_t)row * s_ld, n, nkb, rs, + wadd); /* lds is reused by this block's next row; without the barrier a thread still reading it * would race the next row's write. */ __syncthreads(); @@ -606,14 +617,18 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( // res [M][n] bf16, `r_ld` elements a row (0 = n), optional, read and written // w [n] f32 or bf16 // y [M][n] bf16, `y_ld` elements a row (0 = n), optional -// q [M][n] E4M3, `q_ld` bytes a row (0 = n), rounded up to 4 +// q [M][n] E4M3 (rotated int8 when `hi8`), `q_ld` bytes a row (0 = n), rounded +// up to 4 // s [M][ceil(n/128)] f32, `s_ld` elements a row (0 = ceil(n/128)) -extern "C" int r4d_rmsnorm_quant_fp8(long x, long res, long w, long y, long q, long s, int M, int n, - float eps, float wadd, int w_f32, int x_ld, int r_ld, - int y_ld, int q_ld, int s_ld, long stream) { +// +// `hi8` additionally requires n % 128 == 0, as gated_quant_fp8_launch's `rot`. +static int rmsnorm_quant_launch(long x, long res, long w, long y, long q, long s, int M, int n, + float eps, float wadd, int w_f32, int x_ld, int r_ld, int y_ld, + int q_ld, int s_ld, long stream, bool hi8) { if (!x || !w || !q || !s) return -1; if (M <= 0 || n <= 0) return -2; if (n % 4) return -3; // a lane owns four columns; see the kernel + if (hi8 && (n % (int)kGroup)) return -6; if (!x_ld) x_ld = n; if (!r_ld) r_ld = n; if (!y_ld) y_ld = n; @@ -627,12 +642,14 @@ extern "C" int r4d_rmsnorm_quant_fp8(long x, long res, long w, long y, long q, l // rmsnorm_bf16 must pick the same width from the same function: the width decides which // columns a thread sums, so a different width changes the last bits and breaks bit identity. const int thr = r4d_act_threads(M, n); -#define R4D_RQ_T(wf, rs, T) \ - hipLaunchKernelGGL((rmsnorm_quant_fp8_kernel), dim3((uint32_t)M), dim3(T), 0, \ +#define R4D_RQ_H(wf, rs, T, H) \ + hipLaunchKernelGGL((rmsnorm_quant_fp8_kernel), dim3((uint32_t)M), dim3(T), 0, \ (hipStream_t)stream, (const uint16_t*)x, (uint16_t*)res, (const void*)w, \ (uint16_t*)y, (uint8_t*)q, (float*)s, (uint32_t)n, (uint32_t)nkb, eps, \ wadd, (uint32_t)x_ld, (uint32_t)r_ld, (uint32_t)y_ld, (uint32_t)q_ld, \ (uint32_t)s_ld) +#define R4D_RQ_T(wf, rs, T) \ + do { if (hi8) R4D_RQ_H(wf, rs, T, true); else R4D_RQ_H(wf, rs, T, false); } while (0) #define R4D_RQ(wf, rs) \ do { if (thr == 1024) R4D_RQ_T(wf, rs, 1024); \ else if (thr == 512) R4D_RQ_T(wf, rs, 512); \ @@ -641,9 +658,26 @@ extern "C" int r4d_rmsnorm_quant_fp8(long x, long res, long w, long y, long q, l else { if (res) R4D_RQ(false, true); else R4D_RQ(false, false); } #undef R4D_RQ #undef R4D_RQ_T +#undef R4D_RQ_H return hipGetLastError() == hipSuccess ? 0 : -4; } +extern "C" int r4d_rmsnorm_quant_fp8(long x, long res, long w, long y, long q, long s, int M, int n, + float eps, float wadd, int w_f32, int x_ld, int r_ld, + int y_ld, int q_ld, int s_ld, long stream) { + return rmsnorm_quant_launch(x, res, w, y, q, s, M, n, eps, wadd, w_f32, x_ld, r_ld, y_ld, + q_ld, s_ld, stream, false); +} + +// The same norm with had_quant_act_i8g's quantiser on its pass: the width-128 Hadamard, then int8 +// codes at amax / 127 per (row, 128). For a rotated int8 linear (dtype i8a8h); `q` is int8. +extern "C" int r4d_rmsnorm_had_quant_i8g(long x, long res, long w, long y, long q, long s, int M, + int n, float eps, float wadd, int w_f32, int x_ld, + int r_ld, int y_ld, int q_ld, int s_ld, long stream) { + return rmsnorm_quant_launch(x, res, w, y, q, s, M, n, eps, wadd, w_f32, x_ld, r_ld, y_ld, + q_ld, s_ld, stream, true); +} + // `mode` is 0 for silu (down_proj's input) and 1 for sigmoid (a gated attention o_proj's), as in // r4d_gated_had_quant_i8. // @@ -654,6 +688,8 @@ extern "C" int r4d_rmsnorm_quant_fp8(long x, long res, long w, long y, long q, l // // `rot` additionally requires n % 128 == 0: a ragged last group is fine unrotated (zero-padded, // masked by the GEMM) but a butterfly mixing real values with padding has no matching weight block. +// `rot` with `i8` is had_quant_act_i8g's form (gated_had_quant_i8g), for a rotated int8 down +// projection; `rot` alone pairs with a rotated four-bit expert, which reads E4M3. static int gated_quant_fp8_launch(long gu, long y, long q, long s, int M, int n, int mode, int gu_ld, int y_ld, int q_ld, int s_ld, RadStream stream, bool rot, bool i8 = false) { @@ -663,8 +699,6 @@ static int gated_quant_fp8_launch(long gu, long y, long q, long s, int M, int n, if (n % 4) return -3; if (mode != 0 && mode != 1) return -5; if (rot && (n % (int)kGroup)) return -6; - // The rotated codes pair with a rotated four-bit expert, which reads E4M3 only. - if (rot && i8) return -7; if (!gu_ld) gu_ld = 2 * n; if (!y_ld) y_ld = n; if (!q_ld) q_ld = n; @@ -679,7 +713,8 @@ static int gated_quant_fp8_launch(long gu, long y, long q, long s, int M, int n, (float*)s, (uint32_t)n, (uint32_t)nkb, (uint32_t)gu_ld, (uint32_t)y_ld, \ (uint32_t)q_ld, (uint32_t)s_ld) #define R4D_GQ(a) \ - do { if (rot) R4D_GQ_R(a, true, false); \ + do { if (rot && i8) R4D_GQ_R(a, true, true); \ + else if (rot) R4D_GQ_R(a, true, false); \ else if (i8) R4D_GQ_R(a, false, true); \ else R4D_GQ_R(a, false, false); } while (0) if (mode == 0) R4D_GQ(0); @@ -702,6 +737,13 @@ extern "C" int r4d_gated_had_quant_fp8(long gu, long y, long q, long s, int M, i (RadStream)(uintptr_t)stream, true); } +// The same product with had_quant_act_i8g's quantiser: `q` is int8. +extern "C" int r4d_gated_had_quant_i8g(long gu, long y, long q, long s, int M, int n, int mode, + int gu_ld, int y_ld, int q_ld, int s_ld, long stream) { + return gated_quant_fp8_launch(gu, y, q, s, M, n, mode, gu_ld, y_ld, q_ld, s_ld, + (RadStream)(uintptr_t)stream, true, true); +} + // act . x with the gate and x in separate buffers; see the kernel for why a row is a (token, head) // pair and why the activation is rounded to bf16 before the product. `mode` as in // r4d_gated_quant_fp8. @@ -760,15 +802,19 @@ extern "C" int r4d_gate_quant_fp8(long g, long x, long y, long q, long s, int M, // `res` behaves as in r4d_rmsnorm_quant_fp8. Returns 0, or a negative code for an unserved shape. extern "C" int r4d_ar_rmsnorm_quant_fp8_chunk_elems(void) { return ARQ_CE; } -extern "C" int r4d_ar_rmsnorm_quant_fp8(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 y, long q, long s, - int M, int n, float eps, float wadd, int w_f32, - int r_ld, int y_ld, int q_ld, int s_ld, long stream, - int nblocks, int nthreads, int drain, int acq, int wire, - long loc_pack, long loc_scale_off, long wp) { +// `hi8` is the rotated int8 form (ar_rmsnorm_had_quant_i8g): `q` is int8, n % 128 == 0, and there +// is no post-norm. +static int ar_rmsnorm_quant_launch(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 y, long q, long s, + int M, int n, float eps, float wadd, int w_f32, + int r_ld, int y_ld, int q_ld, int s_ld, long stream, + int nblocks, int nthreads, int drain, int acq, int wire, + long loc_pack, long loc_scale_off, long wp, bool hi8) { if (!x || !w || !q || !s) return -1; if (wp && (!res || wire != 0)) return -10; // a post-norm needs the stream and the exact wire + if (hi8 && wp) return -11; + if (hi8 && (n % (int)kGroup)) return -12; if (!peer_scratch || !my_scratch || !peer_flags || !my_flags || !seq_ctrs) return -1; if (M <= 0 || n <= 0) return -2; // n % 8: n bf16 is a whole number of 16B words (see the push), which also gives every lane its @@ -791,9 +837,9 @@ extern "C" int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long // The width must be r4d_act_threads', as for the standalone norm (it fixes the summation order). const int thr = nthreads ? nthreads : r4d_act_threads(M, n); if (thr != 256 && thr != 512 && thr != 1024) return -6; -#define R4D_ARQ_P(wf, rs, T, W, PO) \ - hipLaunchKernelGGL((ar_rmsnorm_quant_fp8_kernel), dim3((uint32_t)nb), dim3(T), \ - 0, \ +#define R4D_ARQ_H(wf, rs, T, W, PO, H) \ + hipLaunchKernelGGL((ar_rmsnorm_quant_fp8_kernel), dim3((uint32_t)nb), \ + dim3(T), 0, \ (hipStream_t)stream, (uint16_t*)x, (uint16_t*)res, (const void*)w, \ (const void*)wp, \ (uint16_t*)y, (uint8_t*)q, (float*)s, (int4*)peer_scratch, \ @@ -802,7 +848,10 @@ extern "C" int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long (long)loc_scale_off, M, (uint32_t)n, \ (uint32_t)nkb, eps, wadd, (uint32_t)r_ld, (uint32_t)y_ld, (uint32_t)q_ld, \ (uint32_t)s_ld, (int)slot_stride16, drain, acq, (unsigned)nbw) -#define R4D_ARQ_T(wf, rs, T, W) R4D_ARQ_P(wf, rs, T, W, false) +#define R4D_ARQ_P(wf, rs, T, W, PO) R4D_ARQ_H(wf, rs, T, W, PO, false) +#define R4D_ARQ_T(wf, rs, T, W) \ + do { if (hi8) R4D_ARQ_H(wf, rs, T, W, false, true); \ + else R4D_ARQ_P(wf, rs, T, W, false); } while (0) #define R4D_ARQ_W(wf, rs, T) \ do { if (wire == 6) R4D_ARQ_T(wf, rs, T, 6); \ else if (rs && wp) R4D_ARQ_P(wf, rs, T, 0, true); \ @@ -817,9 +866,37 @@ extern "C" int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long #undef R4D_ARQ_W #undef R4D_ARQ_T #undef R4D_ARQ_P +#undef R4D_ARQ_H return hipGetLastError() == hipSuccess ? 0 : -4; } +extern "C" int r4d_ar_rmsnorm_quant_fp8(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 y, long q, long s, + int M, int n, float eps, float wadd, int w_f32, + int r_ld, int y_ld, int q_ld, int s_ld, long stream, + int nblocks, int nthreads, int drain, int acq, int wire, + long loc_pack, long loc_scale_off, long wp) { + return ar_rmsnorm_quant_launch(peer_scratch, my_scratch, peer_flags, my_flags, seq_ctrs, + slot_stride16, x, res, w, y, q, s, M, n, eps, wadd, w_f32, + r_ld, y_ld, q_ld, s_ld, stream, nblocks, nthreads, drain, acq, + wire, loc_pack, loc_scale_off, wp, false); +} + +// The same with had_quant_act_i8g's quantiser on the norm's pass (no post-norm). +extern "C" int r4d_ar_rmsnorm_had_quant_i8g(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 y, long q, long s, + int M, int n, float eps, float wadd, int w_f32, + int r_ld, int y_ld, int q_ld, int s_ld, long stream, + int nblocks, int nthreads, int drain, int acq, + int wire, long loc_pack, long loc_scale_off) { + return ar_rmsnorm_quant_launch(peer_scratch, my_scratch, peer_flags, my_flags, seq_ctrs, + slot_stride16, x, res, w, y, q, s, M, n, eps, wadd, w_f32, + r_ld, y_ld, q_ld, s_ld, stream, nblocks, nthreads, drain, acq, + wire, loc_pack, loc_scale_off, 0, true); +} + // ============================================================== the plugin ABI entry points // // The shared operand reader is r4d_fp8_args.h; see libr4d/r4d_args.h. @@ -828,7 +905,9 @@ extern "C" int r4d_ar_rmsnorm_quant_fp8(long peer_scratch, long my_scratch, long * The standalone quantiser's shape rules, applied to the operand that produces the row. The bf16 * output is optional so a layer without a bf16 consumer skips the store. */ -extern "C" int r4d_rad_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { +/* `hi8`: rmsnorm_had_quant_i8g, whose codes are int8 and whose row is whole 128s (the rotation's + * width). Separate ops for the reason given at r4d_rad_gated_had_quant_fp8. */ +static int rad_rmsnorm_quant(const RadArgs* a, RadStream s, bool hi8) { const RadTensor* x = r4d_opt(a, RQ_X); const RadTensor* rs = r4d_opt(a, RQ_RES); const RadTensor* w = r4d_opt(a, RQ_W); @@ -836,7 +915,7 @@ extern "C" int r4d_rad_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { const RadTensor* sc = r4d_opt(a, RQ_S); const RadTensor* y = r4d_opt(a, RQ_Y); if (!x || !w || !q || !sc) return R4D_NO(RAD_E_INVAL); - if (q->dtype != RAD_F8E4M3) return R4D_NO(RAD_E_DTYPE); + if (q->dtype != (hi8 ? RAD_I8 : RAD_F8E4M3)) return R4D_NO(RAD_E_DTYPE); /* Rank 2 on the input too, unlike the standalone quantiser: a norm has one gain per channel, * and folding trailing dims would spread a gain across heads. */ if (x->rank != 2 || q->rank != 2 || sc->rank != 2) return R4D_NO(RAD_E_SHAPE); @@ -850,21 +929,30 @@ extern "C" int r4d_rad_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { if (q->shape[0] != M || q->shape[1] != K) return R4D_NO(RAD_E_SHAPE); if (sc->shape[0] != M || sc->shape[1] != nkb) return R4D_NO(RAD_E_SHAPE); if (y && (y->shape[0] != M || y->shape[1] != K)) return R4D_NO(RAD_E_SHAPE); + if (hi8 && K % grp) return R4D_NO(RAD_E_SHAPE); /* The residual has the input's extent and is read and written. It may alias `out_bf16` (see * the note at the top of the file). */ if (rs && (rs->rank != 2 || rs->shape[0] != M || rs->shape[1] != K)) return R4D_NO(RAD_E_SHAPE); const long long xp = r4d_row_pitch(x), qp = r4d_row_pitch(q), sp = r4d_row_pitch(sc); const long long yp = y ? r4d_row_pitch(y) : 0, rp = rs ? r4d_row_pitch(rs) : 0; if (xp < 0 || qp < 0 || sp < 0 || yp < 0 || rp < 0) return R4D_NO(RAD_E_STRIDE); - const int e = r4d_rmsnorm_quant_fp8(r4d_addr(x), rs ? r4d_addr(rs) : 0, r4d_addr(w), - y ? r4d_addr(y) : 0, 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)xp, (int)rp, (int)yp, (int)qp, - (int)sp, (long)(uintptr_t)s); + const int e = rmsnorm_quant_launch(r4d_addr(x), rs ? r4d_addr(rs) : 0, r4d_addr(w), + y ? r4d_addr(y) : 0, 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)xp, (int)rp, (int)yp, (int)qp, + (int)sp, (long)(uintptr_t)s, hi8); return e == 0 ? RAD_OK : RAD_E_SHAPE; } +extern "C" int r4d_rad_rmsnorm_quant_fp8(const RadArgs* a, RadStream s) { + return rad_rmsnorm_quant(a, s, false); +} + +extern "C" int r4d_rad_rmsnorm_had_quant_i8g(const RadArgs* a, RadStream s) { + return rad_rmsnorm_quant(a, s, true); +} + /* The gate's activation: `act` by name overrides `mode` (libr4d's integer: 0 silu, 1 sigmoid) when * both are given, as for r4d_gated_had_quant_i8. Returns -1 for an unknown name. */ static int act_mode(const RadArgs* a, int dflt) { @@ -875,15 +963,17 @@ static int act_mode(const RadArgs* a, int dflt) { return -1; } -static int rad_gated_quant_fp8(const RadArgs* a, RadStream stream, bool rot) { +/* `hi8` (with `rot`): gated_had_quant_i8g, int8 codes only. */ +static int rad_gated_quant_fp8(const RadArgs* a, RadStream stream, bool rot, bool hi8 = false) { const RadTensor* gu = r4d_opt(a, GQ_GU); const RadTensor* q = r4d_opt(a, GQ_Q); const RadTensor* sc = r4d_opt(a, GQ_S); const RadTensor* y = r4d_opt(a, GQ_Y); if (!gu || !q || !sc) return R4D_NO(RAD_E_INVAL); - /* The codes' dtype picks the quantiser: E4M3, or int8 for an int8 down projection. */ + /* The codes' dtype picks the quantiser: E4M3, or int8 for an int8 down projection. The two + * rotated ops each take one: E4M3 for a four-bit expert, int8 for a rotated int8 weight. */ if (q->dtype != RAD_F8E4M3 && q->dtype != RAD_I8) return R4D_NO(RAD_E_DTYPE); - if (rot && q->dtype == RAD_I8) return R4D_NO(RAD_E_DTYPE); + if (rot && q->dtype != (hi8 ? RAD_I8 : RAD_F8E4M3)) return R4D_NO(RAD_E_DTYPE); if (gu->rank != 2 || q->rank != 2 || sc->rank != 2) return R4D_NO(RAD_E_SHAPE); if (y && y->rank != 2) return R4D_NO(RAD_E_SHAPE); const long long M = gu->shape[0], K = q->shape[1]; @@ -916,6 +1006,11 @@ extern "C" int r4d_rad_gated_had_quant_fp8(const RadArgs* a, RadStream stream) { return rad_gated_quant_fp8(a, stream, true); } +/* Its int8 twin, had_quant_act_i8g's codes: pairs only with a weight stored at dtype i8a8h. */ +extern "C" int r4d_rad_gated_had_quant_i8g(const RadArgs* a, RadStream stream) { + return rad_gated_quant_fp8(a, stream, true, true); +} + /* Elements between consecutive `width`-wide rows, as r4d_gdn.cpp's gn_row_pitch: a * [.., heads, width] operand steps by the stride of the second-to-last axis; a flattened one is * contiguous and steps by `width`. This reads a column view of an interleaved projection (a gated diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 9868aab9..689f39cf 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -381,6 +381,7 @@ int r4d_rad_ar_twoshot_4rank_ti8 (const RadArgs*, RadStream); int r4d_rad_ar_ln_init(const RadParam*, int, int rank, int world, void** out); int r4d_rad_ar_ln_had_quant_i8(const RadArgs*, RadStream); int r4d_rad_ar_rmsnorm_quant_fp8(const RadArgs*, RadStream); +int r4d_rad_ar_rmsnorm_had_quant_i8g(const RadArgs*, RadStream); int r4d_rad_ar_hc_write(const RadArgs*, RadStream); /* ar_hc_write at four ranks, exact wire only, on an instance of the wide family keyed by its own * M bound. */ @@ -493,8 +494,10 @@ int r4d_rad_quant_act_fp8 (const RadArgs*, RadStream); int r4d_rad_had_quant_act_fp8 (const RadArgs*, RadStream); int r4d_rad_givens_rotate (const RadArgs*, RadStream); int r4d_rad_rmsnorm_quant_fp8(const RadArgs*, RadStream); +int r4d_rad_rmsnorm_had_quant_i8g(const RadArgs*, RadStream); int r4d_rad_gated_quant_fp8 (const RadArgs*, RadStream); int r4d_rad_gated_had_quant_fp8(const RadArgs*, RadStream); +int r4d_rad_gated_had_quant_i8g(const RadArgs*, RadStream); int r4d_rad_gate_quant_fp8 (const RadArgs*, RadStream); int r4d_rad_gram_accum (const RadArgs*, RadStream); /* The same accumulator over a plain bf16 activation, no scale operand. */ @@ -783,6 +786,7 @@ int r4d_shape_all_reduce (const RadParam*, int, int operand, RadOpdDes int r4d_shape_all_gather (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_ar_ln_had_quant_i8 (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_ar_rmsnorm_quant_fp8 (const RadParam*, int, int operand, RadOpdDesc*); +int r4d_shape_ar_rmsnorm_had_quant_i8g (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_ar_hc_write (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_ar_gather_hc_write (const RadParam*, int, int operand, RadOpdDesc*); @@ -813,6 +817,8 @@ int r4d_shape_gdn_gated_norm_had_quant_i8(const RadParam*, int, int operand, int r4d_shape_quant_act_fp8 (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_rmsnorm_quant_fp8 (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_gated_quant_fp8 (const RadParam*, int, int operand, RadOpdDesc*); +int r4d_shape_rmsnorm_had_quant_i8g (const RadParam*, int, int operand, RadOpdDesc*); +int r4d_shape_gated_had_quant_i8g (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_gate_quant_fp8 (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_gram_accum (const RadParam*, int, int operand, RadOpdDesc*); int r4d_shape_qsa_block_key (const RadParam*, int, int operand, RadOpdDesc*); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index e27aa13e..bf59cad7 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -3070,6 +3070,11 @@ static const RadOpSchema kSchemas[] = { SCHEMA("rmsnorm_quant_fp8", pRmsNormQuantFp8, oRmsNormQuantFp8, "RMS norm and the fp8 activation quantiser in one pass over the row. The bf16 norm " "output the unfused pair wrote is still written, and the codes are taken over it."), + SCHEMA("rmsnorm_had_quant_i8g", pRmsNormQuantFp8, oRmsNormQuantFp8, + "rmsnorm_quant_fp8 with had_quant_act_i8g's quantiser on its pass: a `group`-wide " + "Hadamard of the bf16 norm output, then int8 codes per (row, `group`). `out_bf16` " + "receives the UNROTATED norm output. Pairs only with an i8 weight stored rotated " + "(dtype i8a8h); byte-identical to the norm and had_quant_act_i8g."), SCHEMA("rmsnorm_quant_i8", pRmsNormQuantI8, oRmsNormQuantI8, "RMS norm and the per-row int8 quantiser the w4a8 skinny GEMM reads, in one pass; with " "a residual the norm is of residual + x (+ a post-norm of x, x a scalar), stored back."), @@ -3089,6 +3094,11 @@ static const RadOpSchema kSchemas[] = { "the rotation is a property of the quantised pair alone. Pairs with a weight stored at " "dtype w4a8h; substituting it for gated_quant_fp8, or the reverse, produces a model " "that runs and is wrong."), + SCHEMA("gated_had_quant_i8g", pGatedQuantFp8, oGatedQuantFp8, + "gated_had_quant_fp8 at int8 codes: silu(a)*b or sigmoid(a)*b, a `group`-wide Hadamard " + "and had_quant_act_i8g's quantiser, in one pass. `out_bf16` receives the UNROTATED " + "product. Pairs only with an i8 weight stored rotated (dtype i8a8h); byte-identical " + "to the product and had_quant_act_i8g."), SCHEMA("gemm_nt_q_gated", pGemmNtQGated, oGemmNtQGated, "gemm_nt_q over a [2n, K] gate_up plane with gated_quant_fp8's pass folded into the " "epilogue: the outputs are the quantiser's E4M3 codes, its scale per (row, `group` " @@ -3206,6 +3216,9 @@ static const RadOpSchema kSchemas[] = { SCHEMA("ar_rmsnorm_quant_fp8", pArRmsNormQuantFp8, oArRmsNormQuantFp8, "the two-rank all-reduce and the residual add, RMS norm and block-scaled fp8 " "quantisation that always follow it, in one kernel. Both wires."), + SCHEMA("ar_rmsnorm_had_quant_i8g", pArRmsNormQuantFp8, oArRmsNormQuantFp8, + "the two-rank all-reduce of x and rmsnorm_had_quant_i8g after it, in one kernel; " + "either wire, as ar_rmsnorm_quant_fp8."), SCHEMA("ar_hc_write", pArHcWrite, oArHcWrite, "the all-reduce and the gated residual's write that follows it, in one kernel. Both " "wires at two ranks, the exact one at four."), @@ -4368,6 +4381,16 @@ static const RadConstraint cArRmsNormQuantFp8[] = { C_EQ("world_size", 2), C_P2P, }; +/* The rotated int8 twins: `n DIV 128`, since a rotation block cannot straddle the end of a row + * (which also gives the all-reduce's 16B words). */ +static const RadConstraint cRmsNormHadQuantI8g[] = { + C_DIV("n", R4D_FP8_BLOCK), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"), +}; +static const RadConstraint cArRmsNormHadQuantI8g[] = { + C_DIV("n", R4D_FP8_BLOCK), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"), + C_EQ("world_size", 2), + C_P2P, +}; /* hc_write's own bounds plus the all-reduce's: `n DIV 8` because `y` is pushed as 16B words, and * world_size 2 because the handshake is one peer flag per block. */ static const RadConstraint cArHcWrite[] = { @@ -7472,6 +7495,22 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, /* `add` + `rmsnorm` + `quant_act_fp8`, not `rmsnorm_add` (r4d_fuse.cpp). */ r4d_fuse_rmsnorm_quant_fp8 }, +/* The rotated int8 twin, for a trunk of weights stored at i8a8h: the same kernel with + * had_quant_act_i8g's quantiser on its second pass. No fusion hook: the architecture declares it. */ +{ "rmsnorm_had_quant_i8g", "rmsnorm_had_quant_i8g", "quant", + "RMS norm, a 128-wide Hadamard and the per-(row, 128) int8 quantiser in one pass over the row, " + "plus the bf16 norm output", + "n a multiple of 128 -- a rotation block cannot straddle the end of a row; one workgroup per row " + "and two passes over it, as rmsnorm_quant_fp8; the gain is f32 or bf16; group 128, both the " + "rotation width and the scale's block width; an optional residual may ALIAS out_bf16", + "bf16 in, int8 out with an f32 scale per (row, 128 columns) and an optional bf16 out that " + "receives the UNROTATED norm output; the codes are taken over the rotated BF16-ROUNDED norm " + "output, byte-identical to rmsnorm + had_quant_act_i8g", + RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + ROW(cRmsNormHadQuantI8g), NOTUNE, + nullptr, nullptr, r4d_rad_rmsnorm_had_quant_i8g, nullptr, nullptr, nullptr, + r4d_shape_rmsnorm_had_quant_i8g }, + { "gated_quant_fp8", "gated_quant_fp8", "quant", "an elementwise gated product and the fp8 activation quantiser in one pass; `act` silu is " "down_proj's input and sigmoid is a gated o_proj's", @@ -7502,6 +7541,21 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, nullptr, nullptr, r4d_rad_gated_had_quant_fp8, nullptr, nullptr, nullptr, r4d_shape_gated_quant_fp8 }, +/* Its int8 twin, for a down projection whose weight is stored at i8a8h. */ +{ "gated_had_quant_i8g", "gated_had_quant_i8g", "quant", + "an elementwise gated product, a 128-wide Hadamard along n, and the per-(row, 128) int8 " + "quantiser, in one pass", + "n a multiple of 128 -- a rotation block cannot straddle the end of a row; the gated operand is " + "[M][2n], gate then up; no reduction across the row, so no barrier; group 128, both the " + "rotation width and the scale's block width; strides in elements", + "bf16 in, int8 out with an f32 scale per (row, 128 columns) and an optional bf16 out that " + "receives the UNROTATED product; the codes are taken over the rotated BF16-ROUNDED product, " + "byte-identical to the product + had_quant_act_i8g", + RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + ROW(cGatedHadQuantFp8), NOTUNE, + nullptr, nullptr, r4d_rad_gated_had_quant_i8g, nullptr, nullptr, nullptr, + r4d_shape_gated_had_quant_i8g }, + { "gate_quant_fp8", "gate_quant_fp8", "quant", "an elementwise gated product and the fp8 activation quantiser in one pass, with the gate in " "its own operand: a gated attention's gate is a column slice of the [q|gate] projection and " @@ -7671,6 +7725,22 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, /* `all_reduce` + `rmsnorm_quant_fp8`, both wires (r4d_fuse.cpp). */ r4d_fuse_ar_rmsnorm_quant_fp8 }, +{ "ar_rmsnorm_had_quant_i8g", "ar_rmsnorm_had_quant_i8g", "quant", + "the two-rank all-reduce and the residual add, RMS norm, 128-wide Hadamard and per-(row, 128) " + "int8 quantisation that always follow it, in one kernel; the all-reduce's output is never read " + "back", + "exactly 2 ranks; n a multiple of 128 (a rotation block cannot straddle a row) and x unpitched; " + "group 128; a block owns whole ROWS, so the block count is clamped to M; either wire, chosen by " + "`wire` -- the rotated 6-bit payload additionally needs n to be a multiple of 512; residual and " + "out_bf16 may be null", + "bf16 in, int8 out with an f32 scale per (row, 128 columns) and an optional bf16 out of the " + "UNROTATED norm output; x and the residual are ar_rmsnorm_quant_fp8's bytes, and the codes " + "rmsnorm_had_quant_i8g's over the same norm output", + RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + ROW(cArRmsNormHadQuantI8g), ROW(tAr), nullptr, + r4d_rad_ar_ln_init, r4d_rad_ar_fini, r4d_rad_ar_rmsnorm_had_quant_i8g, nullptr, nullptr, nullptr, + r4d_shape_ar_rmsnorm_had_quant_i8g }, + { "ar_rmsnorm_post_quant_fp8", "ar_rmsnorm_post_quant_fp8", "quant", "the two-rank all-reduce, a post-norm of the reduced sum, the residual add, RMS norm and " "block-scaled fp8 quantisation in one kernel: a sandwich-norm junction (Gemma 3)", diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index ecbe6124..227b8914 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -10764,6 +10764,62 @@ static void fused_quant_fp8_numeric(const Plugin& pl) { } } + /* --- the rotated int8 twins, against their producer and had_quant_act_i8g --- + * + * For a trunk stored at i8a8h: the bf16 output must not move (it is unrotated), and codes and + * scales must equal had_quant_act_i8g's over it. K is whole 128s, as the rotation needs. */ + if (const RadKernelInfo* qh = find(pl, "had_quant_act_i8g")) { + if (const RadKernelInfo* rh = find(pl, "rmsnorm_had_quant_i8g")) { + rc = launch_row(nrm, { T(d_x, RAD_BF16, { M, K }), T(d_w, RAD_F32, { K }), + T(d_y[0], RAD_BF16, { M, K }) }, + { PI("M", M), PI("n", K), PF("eps", eps), PS("dtype", "bf16"), + PF("wadd", wadd) }); + if (rc == RAD_OK) + rc = launch_row(qh, { T(d_y[0], RAD_BF16, { M, K }), T(d_q[0], RAD_I8, { M, K }), + T(d_s[0], RAD_F32, { M, nkb }) }, + { PI("M", M), PI("n", K), PI("group", 128), PS("dtype", "bf16") }); + if (rc == RAD_OK) + rc = launch_row(rh, { T(d_x, RAD_BF16, { M, K }), T(nullptr, RAD_BF16, {}), + T(d_w, RAD_F32, { K }), + T(d_q[1], RAD_I8, { M, K }), T(d_s[1], RAD_F32, { M, nkb }), + T(d_y[1], RAD_BF16, { M, K }) }, + { PI("M", M), PI("n", K), PF("eps", eps), PI("group", 128), + PS("dtype", "bf16"), PF("wadd", wadd) }); + if (rc != RAD_OK) ok("rmsnorm_had_quant_i8g vs rmsnorm + had_quant_act_i8g", false, + "rc=" + std::to_string(rc)); + else same("rmsnorm_had_quant_i8g is byte-identical to rmsnorm + had_quant_act_i8g"); + + rc = launch_row(rh, { T(d_x, RAD_BF16, { M, K }), T(nullptr, RAD_BF16, {}), + T(d_w, RAD_F32, { K }), + T(d_q[1], RAD_F8E4M3, { M, K }), T(d_s[1], RAD_F32, { M, nkb }) }, + { PI("M", M), PI("n", K), PF("eps", eps), PI("group", 128), + PS("dtype", "bf16"), PF("wadd", wadd) }); + ok("rmsnorm_had_quant_i8g refuses E4M3 codes", rc != RAD_OK, "rc=" + std::to_string(rc)); + } + if (const RadKernelInfo* gh = find(pl, "gated_had_quant_i8g")) { + rc = launch_row(smu, { T(d_gu, RAD_BF16, { M, 2 * K }), T(d_y[0], RAD_BF16, { M, K }) }, + { PI("M", M), PI("n", K), PS("dtype", "bf16") }); + if (rc == RAD_OK) + rc = launch_row(qh, { T(d_y[0], RAD_BF16, { M, K }), T(d_q[0], RAD_I8, { M, K }), + T(d_s[0], RAD_F32, { M, nkb }) }, + { PI("M", M), PI("n", K), PI("group", 128), PS("dtype", "bf16") }); + if (rc == RAD_OK) + rc = launch_row(gh, { T(d_gu, RAD_BF16, { M, 2 * K }), T(d_q[1], RAD_I8, { M, K }), + T(d_s[1], RAD_F32, { M, nkb }), T(d_y[1], RAD_BF16, { M, K }) }, + { PI("M", M), PI("n", K), PI("group", 128), PS("dtype", "bf16"), + PS("act", "silu") }); + if (rc != RAD_OK) ok("gated_had_quant_i8g vs silu_mul + had_quant_act_i8g", false, + "rc=" + std::to_string(rc)); + else same("gated_had_quant_i8g is byte-identical to silu_mul + had_quant_act_i8g"); + + rc = launch_row(gh, { T(d_gu, RAD_BF16, { M, 2 * K }), T(d_q[1], RAD_F8E4M3, { M, K }), + T(d_s[1], RAD_F32, { M, nkb }) }, + { PI("M", M), PI("n", K), PI("group", 128), PS("dtype", "bf16"), + PS("act", "silu") }); + ok("gated_had_quant_i8g refuses E4M3 codes", rc != RAD_OK, "rc=" + std::to_string(rc)); + } + } + /* --- add + rmsnorm + quant_act_fp8, against the same three fused --- * * The residual sum is f32, and the replaced pair rounds it to bf16 in `add`'s store before @@ -10814,6 +10870,47 @@ static void fused_quant_fp8_numeric(const Plugin& pl) { ok("rmsnorm_quant_fp8 leaves the same residual behind", nd == 0, "differs " + std::to_string(nd)); } + + /* --- add + rmsnorm + had_quant_act_i8g, against rmsnorm_had_quant_i8g's residual form --- */ + const RadKernelInfo* qh = find(pl, "had_quant_act_i8g"); + const RadKernelInfo* rh = find(pl, "rmsnorm_had_quant_i8g"); + if (!qh || !rh) return; + st_memcpy(d_r[0], hr.data(), hr.size() * 2, hipMemcpyHostToDevice); + st_memcpy(d_r[1], hr.data(), hr.size() * 2, hipMemcpyHostToDevice); + rc = launch_row(ad, { T(d_r[0], RAD_BF16, { M, K }), T(d_x, RAD_BF16, { M, K }), + T(d_r[0], RAD_BF16, { M, K }) }, + { PI("M", M), PI("n", K), PS("dtype", "bf16") }); + if (rc == RAD_OK) + rc = launch_row(nrm, { T(d_r[0], RAD_BF16, { M, K }), T(d_w, RAD_F32, { K }), + T(d_y[0], RAD_BF16, { M, K }) }, + { PI("M", M), PI("n", K), PF("eps", eps), PS("dtype", "bf16"), + PF("wadd", wadd) }); + if (rc == RAD_OK) + rc = launch_row(qh, { T(d_y[0], RAD_BF16, { M, K }), T(d_q[0], RAD_I8, { M, K }), + T(d_s[0], RAD_F32, { M, nkb }) }, + { PI("M", M), PI("n", K), PI("group", 128), PS("dtype", "bf16") }); + if (rc == RAD_OK) + rc = launch_row(rh, { T(d_x, RAD_BF16, { M, K }), T(d_r[1], RAD_BF16, { M, K }), + T(d_w, RAD_F32, { K }), + T(d_q[1], RAD_I8, { M, K }), T(d_s[1], RAD_F32, { M, nkb }), + T(d_y[1], RAD_BF16, { M, K }) }, + { PI("M", M), PI("n", K), PF("eps", eps), PI("group", 128), + PS("dtype", "bf16"), PF("wadd", wadd) }); + if (rc != RAD_OK) { + ok("rmsnorm_had_quant_i8g residual vs add + rmsnorm + had_quant_act_i8g", false, + "rc=" + std::to_string(rc)); + return; + } + same("rmsnorm_had_quant_i8g residual is byte-identical to add + rmsnorm + had_quant_act_i8g"); + { + std::vector r0((size_t)M * K), r1((size_t)M * K); + st_memcpy(r0.data(), d_r[0], r0.size() * 2, hipMemcpyDeviceToHost); + st_memcpy(r1.data(), d_r[1], r1.size() * 2, hipMemcpyDeviceToHost); + int nd = 0; + for (size_t i = 0; i < r0.size(); ++i) nd += r0[i] != r1[i]; + ok("rmsnorm_had_quant_i8g leaves the same residual behind", nd == 0, + "differs " + std::to_string(nd)); + } } /* ---- gelu_mul_quant_i8, against gelu_mul + quant_act_i8 ------------------------------------- @@ -25849,7 +25946,8 @@ static void device_phase(const Plugin& pl) { /* The fused quantisers are checked against the kernels they replace, gated on their own * rows. */ - if (sel("rmsnorm_quant_fp8") || sel("gated_quant_fp8") || sel("gate_quant_fp8")) { + if (sel("rmsnorm_quant_fp8") || sel("gated_quant_fp8") || sel("gate_quant_fp8") || + sel("rmsnorm_had_quant_i8g") || sel("gated_had_quant_i8g")) { if (!g_case) std::printf("\nthe fp8 fusions, against the pair each one replaces\n"); fused_quant_fp8_numeric(pl); } @@ -25965,6 +26063,8 @@ static void device_phase(const Plugin& pl) { /* Also skipped: the norm half is rmsnorm_quant_fp8's device function, checked there, and the * handshake and reduced sum need a peer rank. */ skip_pair("ar_rmsnorm_quant_fp8", "two-rank fusion: needs a peer rank"); + /* The same for its rotated int8 twin, whose norm half is rmsnorm_had_quant_i8g's. */ + skip_pair("ar_rmsnorm_had_quant_i8g", "two-rank fusion: needs a peer rank"); skip_pair("ar_rmsnorm_quant_i8", "two-rank fusion: needs a peer rank"); dfree_all(); diff --git a/libr4d/r4d_shapes.cpp b/libr4d/r4d_shapes.cpp index 880482b3..7bc5a417 100644 --- a/libr4d/r4d_shapes.cpp +++ b/libr4d/r4d_shapes.cpp @@ -1178,6 +1178,24 @@ extern "C" int r4d_shape_ar_rmsnorm_quant_fp8(const RadParam* p, int n_p, int op R4D_PICK(l); } +extern "C" int r4d_shape_ar_rmsnorm_had_quant_i8g(const RadParam* p, int n_p, int operand, + RadOpdDesc* out) { + + const int64_t M = pi(p, n_p, "M"); + const int64_t n = pi(p, n_p, "n"); + const int64_t g = pi(p, n_p, "group", R4D_FP8_BLOCK); + if (g != R4D_FP8_BLOCK) return RAD_E_UNSUPPORTED; + const RadOpdDesc l[] = { + opd((uint32_t)RAD_BF16, M, n), /* inout: the message and the input */ + opd((uint32_t)RAD_BF16, M, n), + opd((uint32_t)RAD_F32, n), /* f32 or bf16; the shim reads the operand's own */ + opd((uint32_t)RAD_I8, M, n), + opd((uint32_t)RAD_F32, M, (n + g - 1) / g), + opd((uint32_t)RAD_BF16, M, n), + }; + R4D_PICK(l); +} + /* ========================================================================================== * GEMM, dequant and the row top-R (shims in the r4d_gemm_*.hip units, r4d_dequant_w4_bf16.hip and * r4d_rowtopk_bf16.hip). @@ -1891,6 +1909,43 @@ extern "C" int r4d_shape_gated_quant_fp8(const RadParam* p, int n_p, int operand R4D_PICK(l); } +/* The `_had_quant_i8g` twins: the same operands with int8 codes (had_quant_act_i8g's). */ +extern "C" int r4d_shape_rmsnorm_had_quant_i8g(const RadParam* p, int n_p, int operand, + RadOpdDesc* out) { + + const int64_t M = pi(p, n_p, "M"); + const int64_t n = pi(p, n_p, "n"); + const int64_t g = pi(p, n_p, "group", R4D_FP8_BLOCK); + const uint32_t D = dt_act(p, n_p, "dtype"); + if (g != R4D_FP8_BLOCK) return RAD_E_UNSUPPORTED; + const RadOpdDesc l[] = { + opd(D, M, n), + opd(D, M, n), + opd((uint32_t)RAD_F32, n), + opd((uint32_t)RAD_I8, M, n), + opd((uint32_t)RAD_F32, M, (n + g - 1) / g), + opd(D, M, n), + }; + R4D_PICK(l); +} + +extern "C" int r4d_shape_gated_had_quant_i8g(const RadParam* p, int n_p, int operand, + RadOpdDesc* out) { + + const int64_t M = pi(p, n_p, "M"); + const int64_t n = pi(p, n_p, "n"); + const int64_t g = pi(p, n_p, "group", R4D_FP8_BLOCK); + const uint32_t D = dt_act(p, n_p, "dtype"); + if (g != R4D_FP8_BLOCK) return RAD_E_UNSUPPORTED; + const RadOpdDesc l[] = { + opd(D, M, 2 * n), /* gate then up in one buffer */ + opd((uint32_t)RAD_I8, M, n), + opd((uint32_t)RAD_F32, M, (n + g - 1) / g), + opd(D, M, n), + }; + R4D_PICK(l); +} + extern "C" int r4d_shape_gate_quant_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {