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

+24 -2
View File
@@ -20,6 +20,7 @@
#include "r4d.h"
#include "r4d_args.h"
#include "r4d_emit.h"
#include "r4d_chain.h"
#include "r4d_common.h"
#include "r4d_gdn_wmma.h"
@@ -242,7 +243,7 @@ constexpr long long kMxWavesTarget = 4096;
constexpr long long kMxMinTiles = 4;
template <int HEADS>
__global__ __launch_bounds__(kMxWaves * kWave) void qsa_score_mx_kernel(
__device__ __forceinline__ void qsa_score_mx_kernel_body(
const unsigned short* __restrict__ q, const unsigned short* __restrict__ bk,
const int32_t* __restrict__ bt, const int32_t* __restrict__ cu,
const int32_t* __restrict__ nc, float* __restrict__ out,
@@ -334,6 +335,26 @@ __global__ __launch_bounds__(kMxWaves * kWave) void qsa_score_mx_kernel(
}
}
/* The plain and chained forms (r4d_chain.h): every block leaves through the wrapper. */
template <int HEADS>
__global__ __launch_bounds__(kMxWaves * kWave) void qsa_score_mx_kernel(
const unsigned short* __restrict__ q, const unsigned short* __restrict__ bk,
const int32_t* __restrict__ bt, const int32_t* __restrict__ cu,
const int32_t* __restrict__ nc, float* __restrict__ out,
int M, int nb, int nseq, int tpw, int q_ld, int bk_ld, int bt_ld, int out_ld, int vec) {
qsa_score_mx_kernel_body<HEADS>(q, bk, bt, cu, nc, out, M, nb, nseq, tpw, q_ld, bk_ld, bt_ld, out_ld, vec);
}
template <int HEADS>
__global__ __launch_bounds__(kMxWaves * kWave) void r4dchain_qsa_score_mx(
const unsigned short* __restrict__ q, const unsigned short* __restrict__ bk,
const int32_t* __restrict__ bt, const int32_t* __restrict__ cu,
const int32_t* __restrict__ nc, float* __restrict__ out,
int M, int nb, int nseq, int tpw, int q_ld, int bk_ld, int bt_ld, int out_ld, int vec) {
r4d_chain_wait();
qsa_score_mx_kernel_body<HEADS>(q, bk, bt, cu, nc, out, M, nb, nseq, tpw, q_ld, bk_ld, bt_ld, out_ld, vec);
r4d_chain_done();
}
/* Form override for tests (r4d_selftest compares the two): 0 = automatic, 1 = scalar,
* 2 = matrix. Like r4d_gemm_fp8a8_pin_mw. */
int g_qsa_form = 0;
@@ -374,7 +395,7 @@ static int r4d_qsa_score_bf16_emit(long q, long bk, long bt, long cu, long nc, l
const unsigned gx = (unsigned)((tiles + tpw * kMxWaves - 1) / (tpw * kMxWaves));
const int vec = (out_ld % 4 == 0) && ((out & 15) == 0);
#define RAD_QSA_MX(H) \
emit_rc = R4D_EMIT(stream, (qsa_score_mx_kernel<H>), dim3(gx, (unsigned)qt), \
emit_rc = R4D_EMIT(stream, (g_r4d_chain_arg ? &r4dchain_qsa_score_mx<H> : &qsa_score_mx_kernel<H>), dim3(gx, (unsigned)qt), \
dim3(kMxWaves * kWave), 0, (const unsigned short*)q, \
(const unsigned short*)bk, \
(const int32_t*)bt, (const int32_t*)cu, (const int32_t*)nc, \
@@ -461,6 +482,7 @@ extern "C" int r4d_rad_qsa_score(const RadArgs* a, RadStream stream) {
const long long qp = r4d_row_pitch(q), kp = r4d_row_pitch(bk);
const long long tp = r4d_row_pitch(bt), op = r4d_row_pitch(o);
if (qp < 0 || kp < 0 || tp < 0 || op < 0) return R4D_NO(RAD_E_STRIDE);
const R4dChainArg chain_arg(r4d_geti_or(a, "chain", 0) != 0);
return r4d_qsa_score_bf16_emit(r4d_addr(q), r4d_addr(bk), r4d_addr(bt), r4d_addr(cu),
r4d_addr(nc), r4d_addr(o),
(int)M, (int)n, (int)heads, (int)nb, (int)nseq,