libr4d: chained forms for Flash-Next's decode step, hc_read forms G, S and B3

Every kernel of Flash-Next's speculative decode step gets a chained form (r4d_chain.h), so the
step's small launches wait on their predecessor on the card instead of behind a queue barrier:
the router and the fp8 and four-bit expert GEMMs, the gated quantisers, the delta-net recurrence
and conv, the hyper-connection write and its all-reduce gather, the attention layer's indexer,
norm/rope/gate, quantiser and gqa6 decode rows, and the tiled and fused hc_read kernels. A
chained form is taken only at steps of up to 32 token rows (kR4dChainMaxRows); above that the
waiting blocks hold room a wide predecessor needs.

hc_read at one to sixteen decode rows (hc 4, n 2560, E4M3 mixing matrices, inject gains), the
shape Flash-Next calls 97 times a pass:
- form G (1..8 rows): kernel A' as (16-column tile, stream) blocks, so a block reads its W_down
  slice once for every row of the step; the slice and the norm gain load before the chain wait,
  the dots are bf16 WMMA, and the last of a tile's blocks sums the streams in ascending order.
- form S (9..16 rows): as G, but the norm's scale is applied after the dot, so N never sits in
  LDS and one block holds every row.
- kernel B3 (both forms, lr 320): the up product as WMMA over d with W_up's exact quarter
  scales, and the mix in each lane; W_up loads before the chain wait.
From 17 rows the tiled path (now chained) stays faster.

The new forms are not byte-identical to the kernels they replace: the bf16 WMMA dots sum in
another order and S scales after the dot. Against an fp64 host reference, d is as close as the
per-row kernels' at 1..7 rows and closer than the tiled path's at 8..16 (mean error 0.13-0.14 %
of the row's RMS against 0.17 %, max 1.2-1.4 % against 1.3-2.0 %). The selftest's decode-forms
case now holds d to that reference (no further from it than the two-grid form) and x to the
two-grid form within rounding up to 16 rows, and stays byte for byte above.

Flash-Next on 2x R9700, sampled at temperature 1, 25k prompt, 500 tokens, two rounds of
1/2/4/8/16 streams: MTP depth 3 174/291/458/657/851 -> 207/339/493/647/843 tok/s; with depth 5
and the adaptive window 191/331/493/661/897 -> 218/355/494/662/893.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-05 09:48:37 +01:00
1 parent aeaf6a4580
commit b0a58b5dbf
16 files changed
+1563 -98

No files matched your search

+55 -10
View File
@@ -36,6 +36,7 @@
#include "r4d_ar_seq.h" // the all-reduce sequence step the fused all-reduce + norm takes
#include "r4d_fp8_args.h"
#include "r4d_emit.h"
#include "r4d_chain.h"
#include "r4d_common.h" // r4d_act_threads: the workgroup width, shared with the int8 path
#include "r4d_fwht.h" // the in-register rotation the `_had_` forms put before the absmax
#include "r4d_q8.h" // the 8-bit codes: E4M3, or int8 for an int8 trunk linear
@@ -215,7 +216,7 @@ __global__ __launch_bounds__(THR) void rmsnorm_quant_fp8_kernel(
// The launcher sizes the workgroup to the row's groups (capped at r4d_act_threads' width) and the
// kernel reads the width from blockDim, so a narrow row does not leave most waves idle.
template <int ACT, bool ROT = false, bool I8 = false>
__global__ __launch_bounds__(1024) void gated_quant_fp8_kernel(
__device__ __forceinline__ void gated_quant_fp8_kernel_body(
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y, uint8_t* __restrict__ q,
float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t gu_ld, uint32_t y_ld,
uint32_t q_ld, uint32_t s_ld) {
@@ -251,6 +252,24 @@ __global__ __launch_bounds__(1024) void gated_quant_fp8_kernel(
}
}
/* The plain and chained forms (r4d_chain.h): every block leaves through the wrapper. */
template <int ACT, bool ROT = false, bool I8 = false>
__global__ __launch_bounds__(1024) void gated_quant_fp8_kernel(
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y, uint8_t* __restrict__ q,
float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t gu_ld, uint32_t y_ld,
uint32_t q_ld, uint32_t s_ld) {
gated_quant_fp8_kernel_body<ACT, ROT, I8>(gu, y, q, s, n, nkb, gu_ld, y_ld, q_ld, s_ld);
}
template <int ACT, bool ROT = false, bool I8 = false>
__global__ __launch_bounds__(1024) void r4dchain_gated_quant_fp8(
const uint16_t* __restrict__ gu, uint16_t* __restrict__ y, uint8_t* __restrict__ q,
float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t gu_ld, uint32_t y_ld,
uint32_t q_ld, uint32_t s_ld) {
r4d_chain_wait();
gated_quant_fp8_kernel_body<ACT, ROT, I8>(gu, y, q, s, n, nkb, gu_ld, y_ld, q_ld, s_ld);
r4d_chain_done();
}
// -------------------------------------------------- gate . x -> quant, two source buffers
//
// The same epilogue with the gate in a separate buffer. For gated attention, `x` is the attention
@@ -267,7 +286,7 @@ __global__ __launch_bounds__(1024) void gated_quant_fp8_kernel(
// and the absmax, as gated_quant_fp8_kernel's ROT, for an out projection stored rotated: the codes
// are had_quant_act_i8g's over `y`.
template <int ACT, bool I8 = false, bool ROT = false>
__global__ __launch_bounds__(1024) void gate_quant_fp8_kernel(
__device__ __forceinline__ void gate_quant_fp8_kernel_body(
const uint16_t* __restrict__ g, const uint16_t* __restrict__ x, uint16_t* __restrict__ y,
uint8_t* __restrict__ q, float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t g_ld,
uint32_t x_ld, uint32_t y_ld, uint32_t q_ld, uint32_t s_ld) {
@@ -313,6 +332,24 @@ __global__ __launch_bounds__(1024) void gate_quant_fp8_kernel(
}
}
/* The plain and chained forms (r4d_chain.h): every block leaves through the wrapper. */
template <int ACT, bool I8 = false, bool ROT = false>
__global__ __launch_bounds__(1024) void gate_quant_fp8_kernel(
const uint16_t* __restrict__ g, const uint16_t* __restrict__ x, uint16_t* __restrict__ y,
uint8_t* __restrict__ q, float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t g_ld,
uint32_t x_ld, uint32_t y_ld, uint32_t q_ld, uint32_t s_ld) {
gate_quant_fp8_kernel_body<ACT, I8, ROT>(g, x, y, q, s, n, nkb, g_ld, x_ld, y_ld, q_ld, s_ld);
}
template <int ACT, bool I8 = false, bool ROT = false>
__global__ __launch_bounds__(1024) void r4dchain_gate_quant_fp8(
const uint16_t* __restrict__ g, const uint16_t* __restrict__ x, uint16_t* __restrict__ y,
uint8_t* __restrict__ q, float* __restrict__ s, uint32_t n, uint32_t nkb, uint32_t g_ld,
uint32_t x_ld, uint32_t y_ld, uint32_t q_ld, uint32_t s_ld) {
r4d_chain_wait();
gate_quant_fp8_kernel_body<ACT, I8, ROT>(g, x, y, q, s, n, nkb, g_ld, x_ld, y_ld, q_ld, s_ld);
r4d_chain_done();
}
// -------------------------------------------------------- all-reduce -> rmsnorm -> quant
//
// The same norm with the tensor-parallel all-reduce that always precedes it folded in (o_proj,
@@ -808,7 +845,7 @@ extern "C" int r4d_rmsnorm_had_quant_i8g(long x, long res, long w, long y, long
// 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) {
bool rot, bool i8 = false, bool chain = false) {
int emit_rc = RAD_OK;
if (!gu || !q || !s) return -1;
if (M <= 0 || n <= 0) return -2;
@@ -824,7 +861,8 @@ static int gated_quant_fp8_launch(long gu, long y, long q, long s, int M, int n,
// there is no cross-row reduction and the rotation is wave-local (r4d_fwht.h).
const int thr = std::min(nkb, r4d_act_threads(M, n) / (int)kWave) * (int)kWave;
#define R4D_GQ_R(a, R, I) \
emit_rc = R4D_EMIT(stream, (gated_quant_fp8_kernel<a, R, I>), dim3((uint32_t)M), dim3(thr), \
emit_rc = R4D_EMIT(stream, (chain ? &r4dchain_gated_quant_fp8<a, R, I> \
: &gated_quant_fp8_kernel<a, R, I>), dim3((uint32_t)M), dim3(thr), \
0, (const uint16_t*)gu, (uint16_t*)y, (uint8_t*)q, \
(float*)s, (uint32_t)n, (uint32_t)nkb, (uint32_t)gu_ld, (uint32_t)y_ld, \
(uint32_t)q_ld, (uint32_t)s_ld)
@@ -888,11 +926,15 @@ static int gate_quant_fp8_emit(long g, long x, long y, long q, long s, int M, in
// A wave a group, capped at r4d_act_threads' width, as gated_quant_fp8 above.
const int thr = std::min(nkb, r4d_act_threads(M, n) / (int)kWave) * (int)kWave;
#define R4D_GTQ_I(a, I) \
emit_rc = rot ? R4D_EMIT(stream, (gate_quant_fp8_kernel<a, true, true>), dim3((uint32_t)M), \
dim3(thr), 0, (const uint16_t*)g, (const uint16_t*)x, (uint16_t*)y, \
(uint8_t*)q, (float*)s, (uint32_t)n, (uint32_t)nkb, (uint32_t)g_ld, \
(uint32_t)x_ld, (uint32_t)y_ld, (uint32_t)q_ld, (uint32_t)s_ld) \
: R4D_EMIT(stream, (gate_quant_fp8_kernel<a, I>), dim3((uint32_t)M), dim3(thr), 0, \
emit_rc = rot ? R4D_EMIT(stream, (g_r4d_chain_arg ? &r4dchain_gate_quant_fp8<a, true, true> \
: &gate_quant_fp8_kernel<a, true, true>), \
dim3((uint32_t)M), dim3(thr), 0, (const uint16_t*)g, \
(const uint16_t*)x, (uint16_t*)y, (uint8_t*)q, (float*)s, \
(uint32_t)n, (uint32_t)nkb, (uint32_t)g_ld, (uint32_t)x_ld, \
(uint32_t)y_ld, (uint32_t)q_ld, (uint32_t)s_ld) \
: R4D_EMIT(stream, (g_r4d_chain_arg ? &r4dchain_gate_quant_fp8<a, I> \
: &gate_quant_fp8_kernel<a, I>), dim3((uint32_t)M), \
dim3(thr), 0, \
(const uint16_t*)g, (const uint16_t*)x, \
(uint16_t*)y, (uint8_t*)q, (float*)s, (uint32_t)n, (uint32_t)nkb, \
(uint32_t)g_ld, (uint32_t)x_ld, (uint32_t)y_ld, (uint32_t)q_ld, \
@@ -1124,7 +1166,9 @@ static int rad_gated_quant_fp8(const RadArgs* a, RadStream stream, bool rot, boo
if (mode < 0) return R4D_NO(RAD_E_UNSUPPORTED);
const int e = gated_quant_fp8_launch(r4d_addr(gu), y ? r4d_addr(y) : 0, r4d_addr(q),
r4d_addr(sc), (int)M, (int)K, mode, (int)gp, (int)yp,
(int)qp, (int)sp, stream, rot, q->dtype == RAD_I8);
(int)qp, (int)sp, stream, rot, q->dtype == RAD_I8,
/* decode heights: its rows are tokens or routed slots */
r4d_geti_or(a, "chain", 0) != 0 && M <= 1024);
return e == 0 ? RAD_OK : RAD_E_SHAPE;
}
@@ -1190,6 +1234,7 @@ static int rad_gate_quant(const RadArgs* a, RadStream stream, bool rot) {
if (gp < 0 || xp < 0 || qp < 0 || sp < 0 || yp < 0) return R4D_NO(RAD_E_STRIDE);
const int mode = act_mode(a, 1); /* sigmoid: gated attention */
if (mode < 0) return R4D_NO(RAD_E_UNSUPPORTED);
const R4dChainArg chain_arg(r4d_geti_or(a, "chain", 0) != 0 && M <= 1024);
const int e = gate_quant_fp8_emit(r4d_addr(g), r4d_addr(x), y ? r4d_addr(y) : 0,
r4d_addr(q), r4d_addr(sc), (int)M, (int)K, mode,
(int)gp, (int)xp, (int)yp, (int)qp, (int)sp, stream,