lfm2: the 8-bit trunk -- block-fp8 linears, the fused producers, and the d1-3B recipe

Every linear of the trunk is a LinearFP8, so the container's encoding decides the
GEMM: bf16 stays `gemm_nt` exactly as before, a recipe's block E4M3 is gemm_nt_q at
fp8a8 over an activation quantised once per producer, and int8 is gemm_nt_q at
i8a8. The format is read off layer 0's first linear; a trunk mixing formats is
refused by name. The convolution, the norms and attention stay bf16.

On a block-fp8 trunk the fused producers run: rmsnorm_quant_fp8 with a fold for
each mixer's norm (the previous block's residual add included) and MlpFP8 for the
feed-forward, whose gate|up GEMM applies SiLU(gate) * up and quantises in its
epilogue. The convolution's C * conv(B * x) and the codes its out projection reads
become one gate_quant_fp8 at act none. `gemm_nt_q_gated_wide` puts whole prompts
through the fold instead of falling back to the pair past 64 rows; k and v ask
for `gemm_nt_q_short`; the head norms and rope ask for `rmsnorm_rows` and
`rope_rows`. Each of those is the model's own declaration, so no other
architecture's path moves.

The lm_head is read at its stored codes (bf16, or E4M3/int8 where a recipe made
it so, logits_gemm_fp8/_i8). A tied head also declares the final-normed rows and
the embedding table to the core (rad_declare_scores), so a scored step needs no
head read at all.

data/recipes/d1-3b-fp8.recipe: all 30 layers' linears to E4M3 with a bf16 scale a
128x128 block; the tied head to int8 per row per 128 columns.

Co-Authored-By: Pi <pi@localhost>
This commit is contained in:
Code000andPi authored and StillDeadcode committed 2026-10-29 16:23:28 +01:00
1 parent b8cd1c950a
commit 8df39472b6
6 files changed
+404 -95

No files matched your search

+5 -3
View File
@@ -1399,16 +1399,18 @@ inline int yarn_from(Geom& g, const RadModelMeta* meta, const char* prefix, int6
}
/* `rope` declared for the geometry's rotary: M-RoPE's mode and sections when the model has them,
* `mode` otherwise. */
* `mode` otherwise. `op` names the row: "rope" (every caller), or "rope_rows" for a model that
* wants the per-(token, pair) angles form at prefill row counts. */
inline rad_op decl_rope(RadBuilder* b, const Geom& g, int64_t rows, int64_t head_dim,
int64_t n_head, int64_t n_head_kv, int64_t rot, const char* mode) {
int64_t n_head, int64_t n_head_kv, int64_t rot, const char* mode,
const char* op = "rope") {
const RadParam ps[] = { RAD_RANGE("M", 1, rows), RAD_INT("head_dim", head_dim),
RAD_INT("n_head", n_head), RAD_INT("n_head_kv", n_head_kv),
RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale),
RAD_STR("mode", g.rope_mc ? g.rope_mode : mode),
RAD_INT("rotary_dim", rot), RAD_STR("sections", g.rope_sections) };
const int n = (int)(sizeof ps / sizeof ps[0]) - (g.rope_mc ? 0 : 1);
return decl_rotary(b, g, "rope", ps, n, nullptr, 0);
return decl_rotary(b, g, op, ps, n, nullptr, 0);
}
/* How many of this step's sequences are decode rows, and where their tokens end.
+10 -6
View File
@@ -52,9 +52,9 @@ struct MlpFP8 {
rad_op op_ar = 0, op_add = 0;
/* gate_up with the gate and quantiser in its epilogue, so the [M, 2*n_ff] bf16 product is
* never written (unfused, the extra launch and second pass are a sizeable decode cost).
* Declared beside the pair, not instead of it: the fold is the narrow tile's epilogue and
* serves M <= 64, so prefill chunks take the pair. Byte-identical, as r4d_selftest and
* ref_test check. */
* Declared beside the pair, not instead of it: the narrow fold serves M <= 64, so prefill
* chunks take the pair -- unless the model asked for `wide_fold`, whose row serves every M.
* Byte-identical, as r4d_selftest and ref_test check. */
rad_op op_gated = 0;
int64_t gated_rows = 0;
/* The next block absorbs this block's closing all-reduce, so this one must not issue it.
@@ -67,9 +67,13 @@ struct MlpFP8 {
/* `fold`: a predecessor's delta is still in `w.h.x` and this norm does its residual add
* (false for the first block of a chain). `add_out`: issue the trailing add here. See
* rad_fp8.h. */
/* `wide_fold`: declare the gated fold over the whole prompt (gemm_nt_q_gated_wide, whose row
* serves every M) rather than the narrow row's 64-row band. Set by a model that runs whole
* prompts through the fold; every other caller keeps the band and the pair past it. */
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire,
const Src& src, bool fold = false, bool add_out = true,
int ar_in = kArNone, bool ar_out_ = false, double swiglu_limit = 0);
int ar_in = kArNone, bool ar_out_ = false, double swiglu_limit = 0,
bool wide_fold = false);
void step(RadCtx* c, const RadBatch* batch) const;
/* The block over the first `T` rows of its buffers (see MoeFP8::step_rows). */
void step_rows(RadCtx* c, int64_t T) const { slice(c, T, 0, T); }
@@ -93,7 +97,7 @@ struct MlpFP8 {
inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire,
const Src& src, bool fold, bool add_out,
int ar_in, bool ar_out_, double swiglu_limit) {
int ar_in, bool ar_out_, double swiglu_limit, bool wide_fold) {
g = geom; w = wire; layer = l;
ar_out = ar_out_;
@@ -131,7 +135,7 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
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);
&gated_rows, wide_fold);
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 },
+12 -5
View File
@@ -768,6 +768,11 @@ struct LinearFP8 {
/* probe() found an int8 weight; lets a model learn its trunk's format from one linear before
* declaring activation buffers (ActFP8::declare_qs's `i8`). */
bool probed_i8 = false;
/* Whether this linear's block-fp8 GEMM takes the short-prompt split rule
* (`gemm_nt_q_short`, narrow_ksplit's `short_unsplit`): at N <= 512, K <= 5120 and 41..64 rows
* the split's per-slice floors dominate, so the row runs unsplit. Opt-in, so every other
* caller keeps the split and its bits. Set before declare(). */
bool short_split = false;
/* Whether this linear reads the input's bf16 plane, known from probe() before the producer is
* declared. A producer writes the bf16 plane only when asked, so the block must ask for it when
* this is set; otherwise the linear reads unwritten arena bytes. */
@@ -1509,7 +1514,7 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* caller with a tighter bound (the lm_head, over sampled rows only) passes `max_rows`, so the
* narrow kernel's split-K scratch is not sized for max_tok. */
const int64_t hi = max_rows > 0 ? max_rows : g.max_tok;
op = rw(b, RAD_OP(b, "gemm_nt_q",
op = rw(b, RAD_OP(b, short_split && !f32_scale ? "gemm_nt_q_short" : "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", f32_scale?"fp8a8f32":"fp8a8")),
RAD_WEIGHTS(w, ws)),
@@ -1724,14 +1729,16 @@ inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t
/* gate_up, silu_mul and the E4M3 quantiser in one GEMM epilogue (`gemm_nt_q_gated`, an fp8a8
* tile over a block-fp8 [2n, K] weight), declared for M up to min(rows, 64): the kernel refuses M
* past that band. Optional: when it does not resolve the caller's unfused ops run, and the issue
* past that band, unless the caller asks for the wide row (`wide`), whose gated prefill tile
* serves every M. Optional: when it does not resolve the caller's unfused ops run, and the issue
* site checks `*gated_rows`, set to the band only when the op exists. */
inline rad_op decl_gated_fold(RadBuilder* b, int64_t rows, int64_t N, int64_t K, rad_weight w,
rad_weight ws, std::initializer_list<rad_buf> reads,
std::initializer_list<rad_buf> writes, int64_t* gated_rows) {
const int64_t hi = rows < 64 ? rows : 64;
std::initializer_list<rad_buf> writes, int64_t* gated_rows,
bool wide = false) {
const int64_t hi = wide ? rows : (rows < 64 ? rows : 64);
if (hi <= 0) return 0;
const rad_op op = rw(b, RAD_OP(b, "gemm_nt_q_gated",
const rad_op op = rw(b, RAD_OP(b, wide ? "gemm_nt_q_gated_wide" : "gemm_nt_q_gated",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "fp8a8"),
RAD_STR("act", "silu")),