Archived
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:
1 parent
aeaf6a4580
commit
b0a58b5dbf
16 files changed
+1563
-98
No files matched your search
@@ -19,6 +19,7 @@
|
||||
#include "r4d.h"
|
||||
#include "r4d_args.h"
|
||||
#include "r4d_emit.h"
|
||||
#include "r4d_chain.h"
|
||||
#include "r4d_common.h"
|
||||
#include "rad_yarn.h"
|
||||
#include <hip/hip_bf16.h>
|
||||
@@ -67,7 +68,7 @@ enum { QK_KV_NONE = 0, QK_KV_FP8 = 1, QK_KV_BF16 = 2, QK_KV_F8V4 = 3, QK_KV_K6V4
|
||||
* writes that element and the head's v element into the cache at the token's slot, as E4M3 with
|
||||
* kv_store_fp8's arithmetic or as bf16 like kv_store_bf16 (see the store at the end). */
|
||||
template <int HD, int KV = QK_KV_NONE>
|
||||
__global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
|
||||
__device__ __forceinline__ void r4d_qk_norm_rope_gate_kernel_body(
|
||||
const __hip_bfloat16* __restrict__ QG, const __hip_bfloat16* __restrict__ K,
|
||||
__hip_bfloat16* __restrict__ QO, __hip_bfloat16* __restrict__ KO,
|
||||
__hip_bfloat16* __restrict__ GO,
|
||||
@@ -274,6 +275,32 @@ __global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
/* The plain and chained forms (r4d_chain.h): every block leaves through the wrapper. */
|
||||
template <int HD, int KV = QK_KV_NONE>
|
||||
__global__ __launch_bounds__(256) void r4d_qk_norm_rope_gate_kernel(
|
||||
const __hip_bfloat16* __restrict__ QG, const __hip_bfloat16* __restrict__ K,
|
||||
__hip_bfloat16* __restrict__ QO, __hip_bfloat16* __restrict__ KO,
|
||||
__hip_bfloat16* __restrict__ GO,
|
||||
const void* __restrict__ WQ, const void* __restrict__ WK,
|
||||
const void* __restrict__ CS, const void* __restrict__ POS, long cs_rows,
|
||||
int nq, int nkv, int rot, float eps, float wadd,
|
||||
int qg_pitch, int k_pitch, int cs_f32, int pos_i64, int w_f32, QkKvStore kvs) {
|
||||
r4d_qk_norm_rope_gate_kernel_body<HD, KV>(QG, K, QO, KO, GO, WQ, WK, CS, POS, cs_rows, nq, nkv, rot, eps, wadd, qg_pitch, k_pitch, cs_f32, pos_i64, w_f32, kvs);
|
||||
}
|
||||
template <int HD, int KV = QK_KV_NONE>
|
||||
__global__ __launch_bounds__(256) void r4dchain_qk_norm_rope_gate(
|
||||
const __hip_bfloat16* __restrict__ QG, const __hip_bfloat16* __restrict__ K,
|
||||
__hip_bfloat16* __restrict__ QO, __hip_bfloat16* __restrict__ KO,
|
||||
__hip_bfloat16* __restrict__ GO,
|
||||
const void* __restrict__ WQ, const void* __restrict__ WK,
|
||||
const void* __restrict__ CS, const void* __restrict__ POS, long cs_rows,
|
||||
int nq, int nkv, int rot, float eps, float wadd,
|
||||
int qg_pitch, int k_pitch, int cs_f32, int pos_i64, int w_f32, QkKvStore kvs) {
|
||||
r4d_chain_wait();
|
||||
r4d_qk_norm_rope_gate_kernel_body<HD, KV>(QG, K, QO, KO, GO, WQ, WK, CS, POS, cs_rows, nq, nkv, rot, eps, wadd, qg_pitch, k_pitch, cs_f32, pos_i64, w_f32, kvs);
|
||||
r4d_chain_done();
|
||||
}
|
||||
|
||||
// ============================================================ the cos/sin table
|
||||
//
|
||||
// The fused kernel reads a precomputed table instead of theta, and this fills it: one launch per
|
||||
@@ -376,7 +403,10 @@ static int qk_body(const RadArgs* args, RadStream stream, const QkKvStore& kvs)
|
||||
const int nh = (int)(nq + nkv);
|
||||
// One block per (token, head), 256 threads, one element per lane. The width is fixed at 256, not
|
||||
// fitted to the head count, so the sum of squares matches r4d_rmsnorm_bf16 (see the kernel).
|
||||
#define R4D_QKG_L(H) emit_rc = R4D_EMIT(stream, (r4d_qk_norm_rope_gate_kernel<H, KVF>), \
|
||||
/* The chained form (r4d_chain.h) where the row resolved with `chain`. */
|
||||
const bool ch = r4d_geti_or(args, "chain", 0) != 0 && r4d_chain_rows_ok(M);
|
||||
#define R4D_QKG_L(H) emit_rc = R4D_EMIT(stream, (ch ? &r4dchain_qk_norm_rope_gate<H, KVF> \
|
||||
: &r4d_qk_norm_rope_gate_kernel<H, KVF>), \
|
||||
dim3((int)M, nh), dim3(256), 0, (const __hip_bfloat16*)r4d_p(qg), \
|
||||
(const __hip_bfloat16*)r4d_p(k), (__hip_bfloat16*)r4d_wp(qo), (__hip_bfloat16*)r4d_wp(ko), \
|
||||
(__hip_bfloat16*)r4d_wp(go), r4d_p(wq), r4d_p(wk), r4d_p(cs), r4d_p(po), (long)cs_rows, \
|
||||
|
||||
Reference in new issue
Block a user