Merge branch 'pr1-device' into pr2-engine

This commit is contained in:
C_Tamas committed 2026-10-25 17:19:16 +07:00
commit 06172e98de
49 files changed
+2100 -205

No files matched your search

+22 -7
View File
@@ -119,18 +119,27 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
down.reads_x && !(w.ffn.q8_fed && down.probed_i8), false,
swiglu_limit));
/* The fold (decl_gated_fold), an fp8a8 tile, so bf16, f32-scaled, MXFP4, rotated 5-bit, 4-bit
* and int8 gate_up weights take the pair, and so does a clamped (swiglu_limit) product.
* Declared over int8 codes, only a host kernel would take it, and that would pin the layer's
* weights to the host. */
if (swiglu_limit <= 0 && !gate_up.bf16 && !gate_up.f32_scale && !gate_up.mx &&
!gate_up.paro5 && !gate_up.w4 && !gate_up.i8)
/* The fold: decl_gated_fold, an fp8a8 tile, for block-fp8 weights; decl_gated_fold_mx for a
* plain MXFP4 one at one to eight rows. bf16, f32-scaled, rotated MXFP4, rotated 5-bit, 4-bit
* and int8 gate_up weights take the pair, and so does a clamped (swiglu_limit) product, which
* neither fold computes. Declared over int8 codes, only a host kernel would take it, and that
* would pin the layer's weights to the host. */
const bool fold_ok = swiglu_limit <= 0 && !gate_up.f32_scale;
if (fold_ok && !gate_up.bf16 && !gate_up.mx && !gate_up.paro5 && !gate_up.w4 && !gate_up.i8)
op_gated = decl_gated_fold(b, g.max_tok, gate_up.N, gate_up.K, gate_up.w, gate_up.ws,
{ w.h.q, w.h.s },
gq_ffn.want_x
? std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s, w.ffn.x }
: std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s },
&gated_rows);
else if (fold_ok && gate_up.mx && !gate_up.paro)
op_gated = decl_gated_fold_mx(b, g.max_tok, gate_up.N, gate_up.K, gate_up.w, gate_up.ws,
gate_up.wref, { w.h.q, w.h.s },
gq_ffn.want_x
? std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s,
w.ffn.x }
: std::initializer_list<rad_buf>{ w.ffn.q, w.ffn.s },
&gated_rows);
RAD_ARCH_TRY(down.declare(b, nm, g, down_base,
g.n_embd, g.n_ff, RAD_SHARD_COL, grp_layer(l),
{ src.down }, w.ffn, w.h.x));
@@ -179,7 +188,13 @@ inline void MlpFP8::slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const
}
inline void MlpFP8::body(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const {
if (op_gated && rows <= gated_rows) {
if (op_gated && rows <= gated_rows && gate_up.mx) {
RAD_ISSUE_N(c, op_gated, rows, brow_slice(w.h.q, r0, rows, g.n_embd),
brow_slice(w.h.s, r0, rows, g.n_embd / RAD_FP8_BLOCK), RAD_W(gate_up.w),
RAD_W(gate_up.ws), RAD_W(gate_up.wref), brow_slice(w.ffn.q, r0, rows, g.n_ff),
brow_slice(w.ffn.s, r0, rows, g.n_ff / RAD_FP8_BLOCK),
gq_ffn.want_x ? brow_slice(w.ffn.x, r0, rows, g.n_ff) : RAD_NONE);
} else if (op_gated && rows <= gated_rows) {
RAD_ISSUE_N(c, op_gated, rows, brow_slice(w.h.q, r0, rows, g.n_embd),
brow_slice(w.h.s, r0, rows, g.n_embd / RAD_FP8_BLOCK), RAD_W(gate_up.w),
RAD_W(gate_up.ws), brow_slice(w.ffn.q, r0, rows, g.n_ff),
+14 -4
View File
@@ -106,10 +106,18 @@ struct PleLayer {
rad_op op_gate = 0, op_conv = 0, op_add = 0;
int64_t hn() const { return c.hc * g.n_embd; }
/* The read-ahead queue's depth, in steps: the scheduler stages this many steps of the
* following prompt per step (batch.cpp, k_ahead_chunks) and the gather hands them to the
* reader thread, which prefetches their rows off the step path while the current chunk runs.
* Clamped to what `b_ids` was declared to hold, so a scheduler that offers more degrades the
* queue's depth, never an overflow. */
static constexpr int64_t kAheadChunks = 8;
/* The prompt that follows the step, bounded by what `b_ids` holds after the step's rows. */
int64_t ahead_rows(const RadBatch* batch) const {
const int64_t a = batch->n_ahead;
return a <= 0 ? 0 : (a < g.max_tok ? a : g.max_tok);
if (a <= 0) return 0;
const int64_t cap = kAheadChunks * g.max_tok;
return a < cap ? a : cap;
}
int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& cfg, int layer,
@@ -275,10 +283,12 @@ inline int PleLayer::declare(RadBuilder* b, Names& nm, const Geom& geom, const C
* follows declared order, where the two ops are adjacent, so a transient could be overwritten
* in between. */
{
/* Twice the step's rows: the step's ids, then those of the prompt that follows
* (RadBatch::n_ahead), which the gather's reader prefetches while the step runs. */
/* The step's rows, then the read-ahead queue's: up to kAheadChunks further steps of the
* following prompt (RadBatch::n_ahead), which the gather's reader prefetches while the
* step runs, so a prefill's table rows are read at disk speed off the step path. */
RadBufDecl d{};
d.dtype = RAD_I32; d.rank = 2; d.shape[0] = 2 * g.max_tok; d.shape[1] = c.heads;
d.dtype = RAD_I32; d.rank = 2;
d.shape[0] = (1 + kAheadChunks) * g.max_tok; d.shape[1] = c.heads;
d.kind = RAD_BUF_PERSIST; d.domain = RAD_DOMAIN_HOST;
b_ids = rad_decl_buffer(b, nm.f("ple_ids"), &d);
+22
View File
@@ -1692,6 +1692,28 @@ inline rad_op decl_gated_fold(RadBuilder* b, int64_t rows, int64_t N, int64_t K,
return op;
}
/* The same fold over an OCP-MXFP4 [2n, K] gate_up weight (`gemm_nt_q_gated_mx`: codes, the E8M0
* plane and that plane again as the per-row reference), declared for M up to min(rows, 8), the band
* of libr4d's one-to-eight-row GEMM it is built on. Optional like decl_gated_fold, and asked for
* only when a loaded library declares the op: libref does not, so a CPU-only engine takes the pair
* without ever naming it. */
inline rad_op decl_gated_fold_mx(RadBuilder* b, int64_t rows, int64_t N, int64_t K, rad_weight w,
rad_weight ws, rad_weight wref,
std::initializer_list<rad_buf> reads,
std::initializer_list<rad_buf> writes, int64_t* gated_rows) {
const int64_t hi = rows < 8 ? rows : 8;
if (hi <= 0 || N % 256 || K % RAD_FP8_BLOCK) return 0;
if (!rad_op_declared(b, "gemm_nt_q_gated_mx")) return 0;
const rad_op op = rw(b, RAD_OP(b, "gemm_nt_q_gated_mx",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8"),
RAD_STR("act", "silu")),
RAD_WEIGHTS(w, ws, wref)),
reads, writes);
if (op) *gated_rows = hi;
return op;
}
} /* namespace arch */
} /* namespace rad */
+70 -3
View File
@@ -655,6 +655,11 @@ struct Gemma3Mlp {
G3Linear ln_gu{}, ln_down{};
rad_op op_norm = 0, op_act = 0, op_post = 0, op_ar = 0, op_add = 0;
rad_op op_gq = 0;
/* gate|up, the GeGLU and down's E4M3 quantiser in one launch (libr4d gemm_nt_q_gated_mx at
* gelu_tanh) at up to `fold_rows` rows, when both linears are MXFP4: byte-identical to the
* GEMM, gelu_mul and quant_act_fp8 it replaces. */
rad_op op_fold = 0;
int64_t fold_rows = 0;
/* Calibration: gate|up's input and down's (this rank's columns). */
G3Tap tap_gu{}, tap_dn{};
const bool* tap_live = nullptr;
@@ -725,6 +730,22 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_ff), RAD_STR("dtype", g.dtype)),
RAD_NOWEIGHTS),
{w.gu}, {ln_down.q8, ln_down.s8});
// Not under calibration either: the down tap reads the bf16 product, which the fold never writes.
{
const int64_t hi = g.max_tok < 8 ? g.max_tok : 8;
// G3Linear's mx is linear_is_mxfp4, never a rotated (ParoQuant) weight, so one part.
if (ln_gu.mx && ln_down.mx && !ln_down.shared_in && !calib_dir() && hi > 0 &&
ln_gu.N % 256 == 0 && ln_gu.K % RAD_FP8_BLOCK == 0 &&
rad_op_declared(b, "gemm_nt_q_gated_mx")) {
op_fold = rw(b, RAD_OP(b, "gemm_nt_q_gated_mx",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", ln_gu.N), RAD_INT("K", ln_gu.K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8"),
RAD_STR("act", "gelu_tanh")),
RAD_WEIGHTS(ln_gu.w, ln_gu.ws, ln_gu.wref)),
{ln_gu.q8, ln_gu.s8}, {ln_down.q8, ln_down.s8});
if (op_fold) fold_rows = hi;
}
}
if (g.world > 1)
op_ar = rw(b, RAD_OP(b, "all_reduce",
RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd),
@@ -754,14 +775,23 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
void Gemma3Mlp::step(RadCtx* c, const RadBatch* batch, bool entry_done, bool tail_fused,
bool ar_fused) const {
const int64_t T = batch->n_tok;
const bool fold = op_fold && T <= fold_rows;
if (entry_done) {
ln_gu.gemm(c, w.gu, T);
if (!fold) ln_gu.gemm(c, w.gu, T);
} else {
RAD_ISSUE(c, op_norm, brows(w.x, T), RAD_W(w_norm), brows(w.h, T));
ln_gu.step(c, w.h, w.gu, T);
if (!fold)
ln_gu.step(c, w.h, w.gu, T);
else if (!ln_gu.shared_in)
RAD_ISSUE_N(c, ln_gu.op_q, T, brows(w.h, T), brows(ln_gu.q8, T), brows(ln_gu.s8, T));
tap_gu.step(c, w.h, T, tap_live);
}
if (op_gq) {
if (fold) {
RAD_ISSUE_N(c, op_fold, T, brows(ln_gu.q8, T), brows(ln_gu.s8, T), RAD_W(ln_gu.w),
RAD_W(ln_gu.ws), RAD_W(ln_gu.wref), brows(ln_down.q8, T), brows(ln_down.s8, T),
RAD_NONE);
ln_down.gemm(c, w.h, T);
} else if (op_gq) {
RAD_ISSUE_N(c, op_gq, T, brows(w.gu, T), brows(ln_down.q8, T), brows(ln_down.s8, T), RAD_NONE);
ln_down.gemm(c, w.h, T);
} else {
@@ -884,12 +914,28 @@ struct G3JunctionFP8 {
}
};
/* The weight reads beside the decode all-reduces (prefetch_beside, rad_arch.h): a weight's first
* kPrefetchBytes, on decode steps of up to kPrefetchStepRows rows (a verify of four sequences at
* speculation 7, as qwen35_fp8's). TranslateGemma-27B MXFP4 + DFlash2 at tp 2 on 2x R9700 (Gen5 x8),
* no speculation, two rounds each, x1 / x4 / x8 tok/s against none (56.8 / 182.0 / 325.0): 8 MB 63.3 /
* 203.1 / 354.9; 12 MB 63.0 / 202.6 / 354.9; 16 MB 63.5 / 203.9 / 356.6; 20 MB 64.1 / 204.0 / 359.4;
* 24 MB 64.4 / 206.4 / 359.3; 32 MB 63.1 / 201.4 / 351.9. Larger than qwen35_fp8's 12 MB: this
* model's hidden width (5376) and weights are larger, so its all-reduces take longer and its GEMMs
* read more; past ~24 MB the read outlasts the reduction and takes bandwidth from the GEMM it should
* feed. With speculation 7 (three rounds, greedy, medians) x1 / x4 / x8 / x16 158.1 / 409.0 / 592.0 / 710.8 -> 177.3 / 428.4 / 592.3 / 717.6. The drafter's reads (Dflash2::Config::prefetch) take the same size. */
constexpr int64_t kPrefetchBytes = (int64_t)24 << 20;
constexpr int64_t kPrefetchStepRows = 32;
struct Layer {
bool global = false;
Gemma3Attn attn{};
Gemma3Mlp mlp{};
G3Junction j_am{}, j_ma{};
G3JunctionFP8 jf_am{}, jf_ma{};
/* The reads beside the decode all-reduces: gate|up's leading bytes beside the attention block's
* reduction, the next layer's q's beside the feed-forward's. 0 where none is declared. */
rad_op op_pf_gu = 0, op_pf_next = 0;
rad_weight pf_gu_w = 0, pf_next_w = 0;
};
struct Model {
@@ -974,6 +1020,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
Dflash2::Config dc{};
dc.short_block = 1; /* rad_drafter_short_block below */
dc.prefetch = kPrefetchBytes; /* reads beside the drafter's all-reduces (at tp > 1) */
dc.n_taps = m.n_taps;
dc.block = ctx->max_spec + 1;
dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2);
@@ -1325,6 +1372,19 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
}
m.fuse_mx_ar = ar;
}
/* ---- the weight reads beside the decode all-reduces (prefetch_beside, rad_arch.h). Only at
* more than one rank; RADIANCE_PREFETCH_BESIDE=0 issues none (prefetch_beside). */
if (g.world > 1 && rad_op_declared(b, "prefetch")) {
for (int64_t l = 0; l < g.n_layer; ++l) {
Layer& lay = m.layers[(size_t)l];
lay.pf_gu_w = lay.mlp.ln_gu.w;
lay.op_pf_gu = decl_prefetch(b, lay.pf_gu_w, kPrefetchBytes);
if (l + 1 < g.n_layer) {
lay.pf_next_w = m.layers[(size_t)l + 1].attn.ln_q.w;
lay.op_pf_next = decl_prefetch(b, lay.pf_next_w, kPrefetchBytes);
}
}
}
/* The pipeline needs the fused fp8 junctions and every block's overlap all-reduce. */
if (m.pipe_on) {
bool ok = m.fuse_mx;
@@ -1642,6 +1702,9 @@ static void step(RadCtx* c, const RadBatch* batch) {
if (m.op_embed_ar)
RAD_ISSUE_N(c, m.op_embed_ar, T * m.g.n_embd, brows(m.b_x, T), RAD_NONE);
/* The reads beside the reductions go on decode steps of up to kPrefetchStepRows rows, none on a
* step that carries a prompt's rows (as qwen35_fp8's). */
const int64_t pf_h = batch->phase == RAD_PHASE_DECODE && T <= kPrefetchStepRows ? 1 : INT64_MAX;
if (m.fuse_mx) {
/* MXFP4 trunk: [post-norm, then add + next norm + quantiser as one op] at every junction;
* the last block's tail runs unfused into the final norm. */
@@ -1652,9 +1715,11 @@ static void step(RadCtx* c, const RadBatch* batch) {
/* Per step: the fused junction sends the exact wire only (G3JunctionFP8::op_ar). */
const bool far = m.fuse_mx_ar && ar_wire_is_exact(m.g, T);
l.attn.step(c, batch, true, true, far);
prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h);
if (!far) l.attn.post(c, T);
l.jf_am.step(c, T, far);
l.mlp.step(c, batch, true, !last, far);
prefetch_beside(c, l.op_pf_next, l.pf_next_w, pf_h);
if (!last) {
if (!far) l.mlp.post(c, T);
l.jf_ma.step(c, T, far);
@@ -1668,8 +1733,10 @@ static void step(RadCtx* c, const RadBatch* batch) {
const bool last = li + 1 == m.layers.size();
const bool ar = m.fuse_ar && ar_wire_is_exact(m.g, T); /* exact wire only, as above */
l.attn.step(c, batch, m.fuse, m.fuse, ar);
prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h);
if (m.fuse) l.j_am.step(c, T, ar);
l.mlp.step(c, batch, m.fuse, m.fuse && !last, ar);
prefetch_beside(c, l.op_pf_next, l.pf_next_w, pf_h);
if (m.fuse && !last) l.j_ma.step(c, T, ar);
tap(li);
}
+48
View File
@@ -106,11 +106,30 @@ using Ffn = MlpFP8;
using MtpHead = MtpFP8;
#endif
/* The weight reads beside the decode all-reduces (prefetch_beside, rad_arch.h): a weight's first
* kPrefetchBytes, on decode steps of up to kPrefetchStepRows rows. Qwen3.8-27B MXFP4 + DFlash2 at tp 2
* on 2x R9700 (Gen5 x8), one round each, no speculation, x1 / x4 / x8 / x16 tok/s against none
* (61.2 / 225.0 / 403.7 / 650.8): 8 MB 65.1 / 246.4 / 410.9 / 663.3; 10 MB 66.0 / 250.1 / 425.0 /
* 668.5; 12 MB 67.9 / 251.2 / 438.6 / 668.1; 14 MB 67.8 / 249.5 / 432.4 / 659.1; 16 MB 67.0 / 244.9 /
* 422.7 / 649.4; 20 MB 63.4 / 232.0 / 408.1 / 630.7; 32 MB 57.9 / 210.2 / 372.8 / 605.9 -- past
* ~12 MB the read outlasts the reduction and takes bandwidth from the GEMM it should feed. Reads up
* to 128 rows: x32 963.8 against 956.0, x64 1206.7 against 1216.6, so the ceiling is 32 rows (a
* verify of four sequences at speculation 7). With speculation 7 (three rounds) x1..x16 +0.1..+1.9 %.
* The drafter's reads (Dflash2::Config::prefetch) take the same size: speculation 7, eight rounds,
* median x1 +6 %, x16 unchanged. */
constexpr int64_t kPrefetchBytes = (int64_t)12 << 20;
constexpr int64_t kPrefetchStepRows = 32;
struct Layer {
int full = 0;
AttnGatedFP8 attn;
GdnFP8 gdn;
Ffn mlp;
/* The reads beside the decode all-reduces (prefetch_beside, rad_arch.h): gate_up's leading
* bytes beside the mixer's reduction, the next layer's first projection's beside the
* feed-forward's. 0 where none is declared. */
rad_op op_pf_gu = 0, op_pf_next = 0;
rad_weight pf_gu_w = 0, pf_next_w = 0;
};
struct Model {
@@ -283,6 +302,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
Dflash2::Config dc{};
dc.short_block = 1; /* rad_drafter_short_block below */
dc.prefetch = kPrefetchBytes; /* reads beside the drafter's all-reduces (at tp > 1) */
dc.n_taps = m.n_taps;
dc.block = ctx->max_spec + 1;
dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2);
@@ -1026,6 +1046,27 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
for (int64_t l = 0; l + 1 < m.n_main; ++l) m.layers[(size_t)l].mlp.xp_next = &m.layers[(size_t)l + 1].mlp;
#endif
#if !QWEN35_MOE
/* ---- the weight reads beside the decode all-reduces (prefetch_beside, rad_arch.h). At decode
* heights each block's reduction leaves the card's memory idle for its round trip over the link,
* so the GEMM behind it has its weight's leading bytes read into the on-die cache there: gate_up
* behind the mixer's reduction, the next layer's first projection (the delta net's in-projection,
* an attention layer's q|gate) behind the feed-forward's. Only at more than one rank;
* RADIANCE_PREFETCH_BESIDE=0 issues none (prefetch_beside). */
if (g.world > 1 && rad_op_declared(b, "prefetch")) {
for (int64_t l = 0; l < m.n_main; ++l) {
Layer& lay = m.layers[(size_t)l];
lay.pf_gu_w = lay.mlp.gate_up.w;
lay.op_pf_gu = decl_prefetch(b, lay.pf_gu_w, kPrefetchBytes);
if (l + 1 < m.n_main) {
const Layer& nx = m.layers[(size_t)l + 1];
lay.pf_next_w = nx.full ? nx.attn.qg.w : nx.gdn.in.w;
lay.op_pf_next = decl_prefetch(b, lay.pf_next_w, kPrefetchBytes);
}
}
}
#endif
m.op_final_norm = rw(b, RAD_OP(b, "rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_F64("eps", g.eps), RAD_F64("wadd", final_wadd), RAD_STR("dtype", g.dtype)),
@@ -1497,12 +1538,19 @@ static void step(RadCtx* c, const RadBatch* batch) {
RAD_ISSUE_N(c, m.op_rope_cs, batch->n_tok,
rope_pos1(batch, batch->n_tok), RAD_B(m.b_rope_cs));
/* The reads beside the reductions go on decode steps of up to kPrefetchStepRows rows, none on a
* step that carries a prompt's rows. This ceiling, not prefetch_beside's own (16), decides: it
* is passed a height of 1 where the step qualifies. */
const int64_t pf_h =
batch->phase == RAD_PHASE_DECODE && batch->n_tok <= kPrefetchStepRows ? 1 : INT64_MAX;
for (int64_t li = 0; li < (int64_t)m.layers.size(); ++li) {
const Layer& l = m.layers[(size_t)li];
if (l.full) l.attn.step(c, batch);
else l.gdn.step(c, batch);
prefetch_beside(c, l.op_pf_gu, l.pf_gu_w, pf_h);
dbg_resid(c, (int)li, "mix", m.g.n_embd, m.b_x, m.a_h.x);
l.mlp.step(c, batch);
prefetch_beside(c, l.op_pf_next, l.pf_next_w, pf_h);
dbg_resid(c, (int)li, "ffn", m.g.n_embd, m.b_x, m.a_h.x);
/* The tap, after the layer: the drafter conditions on this layer's output, which the next