Archived
libxe2, arch/common: the 28 Oct pending set, the bf16 cast fused, the vectorized norm, a draft-vocab prefix
- gdn_kkt_solve on DPAS (~0.4 ms at 2048 tokens against 7.7), the int8 head's DPAS forms at 9-16 rows (verify past depth 7), the plain block norm (RadBuildCtx::plain_norm, set on the SYCL backend: `add` + `rmsnorm` where no reader takes the E4M3 codes) with the fast rmsnorm rows. - rmsnorm's bf16 rows up to 8192 wide: a work-item eight elements by one 16-byte load (the generic form walked 20 dependent loads a work-item): 24.7 -> 3.3 us a decode norm, ~3.5 ms a round. - w4a16: a bf16 activation narrowed to fp16 at the kernel's own load (the separate cast launch before every GEMM is gone), bit for bit the same. - RADIANCE_MTP_DRAFT_VOCAB=N: the 2-bit draft head over the vocabulary's first N ids (a prefix shortlist, GRIMOIRE's idea); the u2 layout hook takes an op N below the weight's rows. 131072: greedy acceptance identical on the decode proxy, draft head 650 -> 351 us a pass. KL against the exact-kernel reference (2002 positions): 0.002386, top-1 97.30% (was 0.002376 / 96.80%). Decode proxy (greedy, depth 5, mtp4): 72.0 -> 74.3 tok/s with the prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7580ec3d17
commit
726e532114
18 files changed
+472
-54
No files matched your search
@@ -705,6 +705,9 @@ struct Geom {
|
||||
/* RAD_ATTN_FP8_* bits: the attention products accepted in e4m3 (RadBuildCtx describes it).
|
||||
* decl_attn passes it on as `attn_paged`'s `prefill_fp8`. */
|
||||
int attn_prefill_fp8 = 0;
|
||||
/* RadBuildCtx::plain_norm: a block norm none of whose readers takes codes may run as `add` +
|
||||
* `rmsnorm` (rad_fp8.h, NormQuantFP8::x_only). */
|
||||
int plain_norm = 0;
|
||||
float eps = 1e-6f;
|
||||
/* GemmaRMSNorm's `1 +`. Qwen3.5, Qwen3-Next and Gemma normalise with `x_hat * (1 + w)` and
|
||||
* store `w` near 0.1, so reading it as a plain gain shrinks every normed activation 5-10x. An
|
||||
@@ -977,6 +980,7 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c,
|
||||
g.wire_exact= c->tp_wire_lossy ? 0 : 1;
|
||||
g.wire_min_bytes = c->tp_wire_min_bytes;
|
||||
g.attn_prefill_fp8 = c->attn_prefill_fp8;
|
||||
g.plain_norm = c->plain_norm;
|
||||
g.eps = m->rms_eps;
|
||||
g.theta = m->rope_theta;
|
||||
g.rope_scale= 1.0f; /* yarn_from reads the model's rotary scaling */
|
||||
@@ -1447,6 +1451,22 @@ inline const RadKVGroupBatch* kv_batch(const RadBatch* b, rad_kvgroup group) {
|
||||
}
|
||||
|
||||
/* ================================================================== calibration in slices */
|
||||
|
||||
/* The draft head's rows: the vocabulary's first RADIANCE_MTP_DRAFT_VOCAB ids (a prefix shortlist,
|
||||
* rounded down to whole 16-row tiles) or all of it. Byte-level BPE vocabularies number their
|
||||
* merges by frequency, so a prefix keeps the likely drafts and the head reads that much less of
|
||||
* its weight a draft round; a draft outside it is never proposed, which costs acceptance only --
|
||||
* the verify still runs the full head. */
|
||||
inline int64_t mtp_draft_rows(int64_t n_vocab_all) {
|
||||
static const long long want = [] {
|
||||
const char* s = std::getenv("RADIANCE_MTP_DRAFT_VOCAB");
|
||||
return s ? std::atoll(s) : 0LL;
|
||||
}();
|
||||
if (want <= 0 || want >= n_vocab_all) return n_vocab_all;
|
||||
const int64_t r = (int64_t)want / 16 * 16;
|
||||
return r > 0 ? r : n_vocab_all;
|
||||
}
|
||||
|
||||
/* The layers [lo, hi) of n a calibration taps: the environment variable `var` as "lo:hi", else
|
||||
* all of them. A large model's Grams do not fit beside its weights at once, so it is calibrated
|
||||
* in slices of layers, the corpus pushed through once a slice. */
|
||||
|
||||
@@ -260,7 +260,15 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
|
||||
w_k_norm = decl_w(b, nm.f("blk.%d.attn_k_norm.weight", l), RAD_F32, {g.head_dim},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
|
||||
/* One norm+quantise op feeds all three input projections (rad_fp8.h: NormQuantFP8). */
|
||||
/* One norm+quantise op feeds all three input projections (rad_fp8.h: NormQuantFP8). Where
|
||||
* none of them reads the code pair, the plain form (NormQuantFP8::x_only): the projections are
|
||||
* probed for it first, and their declare() does not map them again. */
|
||||
if (!ext_in && g.plain_norm && ar_in == kArNone && !g.attn_zero) {
|
||||
RAD_ARCH_TRY(qg.probe(b, nm, g, nm.f("blk.%d.attn_qg", l), { src.qg }));
|
||||
RAD_ARCH_TRY(kp.probe(b, nm, g, nm.f("blk.%d.attn_k", l), { src.k }));
|
||||
RAD_ARCH_TRY(vp.probe(b, nm, g, nm.f("blk.%d.attn_v", l), { src.v }));
|
||||
nq_h.x_only = qg.codes_free && kp.codes_free && vp.codes_free;
|
||||
}
|
||||
if (!ext_in)
|
||||
RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd,
|
||||
fold ? w.x : 0, 0, ar_in));
|
||||
|
||||
@@ -233,7 +233,12 @@ inline int GdnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
|
||||
/* Norm and quantise in one op (rad_fp8.h: NormQuantFP8); the qkv|z and a|b projections read
|
||||
* it. */
|
||||
* it. a|b is a plain bf16 GEMM over the bf16 plane, so where qkv|z does not read the code pair
|
||||
* the norm takes its plain form (NormQuantFP8::x_only; qkv|z probed first). */
|
||||
if (!ext_in && g.plain_norm && ar_in == kArNone) {
|
||||
RAD_ARCH_TRY(in.probe(b, nm, g, nm.f("blk.%d.ssm_inz", l), { s.in_qkv, s.in_z }));
|
||||
nq_h.x_only = in.codes_free;
|
||||
}
|
||||
if (!ext_in)
|
||||
RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd,
|
||||
fold ? w.x : 0, 0, ar_in));
|
||||
|
||||
@@ -106,7 +106,12 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
|
||||
/* Norm, quantise and residual add in one op (rad_fp8.h: NormQuantFP8). Folded, it adds the
|
||||
* previous block's delta from `h` into the stream; unfolded it reads the stream directly. */
|
||||
* previous block's delta from `h` into the stream; unfolded it reads the stream directly. The
|
||||
* plain form where gate|up does not read the code pair (NormQuantFP8::x_only; probed first). */
|
||||
if (g.plain_norm && ar_in == kArNone) {
|
||||
RAD_ARCH_TRY(gate_up.probe(b, nm, g, nm.f("blk.%d.ffn_gate_up", l), { src.gate, src.up }));
|
||||
nq_h.x_only = gate_up.codes_free;
|
||||
}
|
||||
RAD_ARCH_TRY(nq_h.declare(b, g, w_norm, fold ? w.h.x : w.x, w.h, g.n_embd, fold ? w.x : 0,
|
||||
0, ar_in));
|
||||
RAD_ARCH_TRY(gate_up.declare(b, nm, g, nm.f("blk.%d.ffn_gate_up", l),
|
||||
|
||||
@@ -329,14 +329,14 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
|
||||
op_dlogits = rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows),
|
||||
RAD_INT("N", g.n_vocab_all), RAD_INT("K", g.n_embd),
|
||||
RAD_INT("N", mtp_draft_rows(g.n_vocab_all)), RAD_INT("K", g.n_embd),
|
||||
RAD_INT("group", MTP_W2_GROUP), RAD_STR("dtype", "w2a8")),
|
||||
RAD_WEIGHTS(w_dh, w_dhs)),
|
||||
{w.dq, w.ds, w.dsum}, {w.dlog});
|
||||
|
||||
op_dtopk = rw(b, RAD_OP(b, "row_topk",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows),
|
||||
RAD_INT("N", g.n_vocab_all), RAD_INT("R", 1),
|
||||
RAD_INT("N", mtp_draft_rows(g.n_vocab_all)), RAD_INT("R", 1),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{w.dlog}, {w.didx, w.dval});
|
||||
|
||||
@@ -382,6 +382,13 @@ struct NormQuantFP8 {
|
||||
rad_op op_arn6 = 0;
|
||||
/* The ops write had_quant_act_i8g's rotated int8 codes into the int8 pair (ActFP8::q8_rot). */
|
||||
bool hi8 = false;
|
||||
/* Set by the block before declare when Geom::plain_norm is on, no all-reduce is fused into
|
||||
* this norm, and no reader takes `a`'s code pair (LinearFP8::codes_free): the norm runs as
|
||||
* `add` (`op_add`, the residual stored before the variance reads it, as rmsnorm_quant_fp8
|
||||
* stores it) then `rmsnorm` into `a.x` -- rmsnorm_quant_fp8's residual and bf16 output,
|
||||
* without the codes. */
|
||||
bool x_only = false;
|
||||
rad_op op_add = 0;
|
||||
Geom gm{};
|
||||
/* The row width, for turning a row slice into element offsets. */
|
||||
int64_t n = 0;
|
||||
@@ -418,6 +425,24 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain,
|
||||
"linears are bf16; its blocks take a prepared input instead\n");
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (x_only) {
|
||||
if (fuse_ar != kArNone || !src || !a.x || !w || n <= 0) return RAD_E_INVAL;
|
||||
const int64_t rows = max_rows > 0 ? max_rows : g.max_tok;
|
||||
if (res) {
|
||||
op_add = rw(b, RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ src, res }, { res });
|
||||
if (!op_add) return RAD_E_INVAL;
|
||||
}
|
||||
op = rw(b, RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n), RAD_F64("eps", g.eps),
|
||||
RAD_F64("wadd", g.wadd), RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w)),
|
||||
{ res ? res : src }, { a.x });
|
||||
return op ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
/* Rotated int8 codes (ActFP8::q8_rot) come from the `_had_quant_i8g` twins, into the int8
|
||||
* pair; the model checked they are declared before feeding. Otherwise E4M3, as always. */
|
||||
hi8 = a.q8_fed && a.q8_rot;
|
||||
@@ -493,6 +518,14 @@ inline void NormQuantFP8::step_plain(RadCtx* c, int64_t T, int64_t r0, int64_t r
|
||||
* different summation order would run. */
|
||||
inline void NormQuantFP8::step(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const {
|
||||
if (rows <= 0) rows = T - r0;
|
||||
if (x_only) {
|
||||
if (op_add)
|
||||
RAD_ISSUE_N(c, op_add, rows, brow_slice(src, r0, rows, n), brow_slice(res, r0, rows, n),
|
||||
brow_slice(res, r0, rows, n));
|
||||
RAD_ISSUE_N(c, op, rows, brow_slice(res ? res : src, r0, rows, n), RAD_W(w),
|
||||
brow_slice(a.x, r0, rows, n));
|
||||
return;
|
||||
}
|
||||
RAD_ISSUE_N(c, pick(rows), rows, brow_slice(src, r0, rows, n),
|
||||
res ? brow_slice(res, r0, rows, n) : RAD_NONE, RAD_W(w),
|
||||
brow_slice(hi8 ? a.q8 : a.q, r0, rows, n),
|
||||
@@ -777,6 +810,11 @@ struct LinearFP8 {
|
||||
* 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. */
|
||||
bool reads_x = false;
|
||||
/* Whether it reads the bf16 plane alone and never the producer's code pair: a 4-bit weight
|
||||
* (cast or quantised from the plane by its own op) or a plain bf16 one. Known from probe();
|
||||
* a block norm all of whose readers are code-free may skip the quantiser
|
||||
* (NormQuantFP8::x_only). */
|
||||
bool codes_free = false;
|
||||
bool mapped = false; /* probe() made the name map; declare() does not make it again */
|
||||
/* The stored weight this linear's views take when it is not `<base>.weight`: a second linear
|
||||
* over one container entry, e.g. a whole projection beside its tensor-parallel slice. Set
|
||||
@@ -1082,6 +1120,7 @@ inline int LinearFP8::probe(RadBuilder* b, Names& nm, const Geom& g, const char*
|
||||
const bool rotated = known && (linear_is_paro_mxfp4(enc) || linear_is_paro_w5(enc) ||
|
||||
linear_is_paro_m2xfp4(enc));
|
||||
reads_x = g.w_bf16 || plain || probed_i8 || rotated || (known && linear_is_w4(enc));
|
||||
codes_free = plain || (known && linear_is_w4(enc));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -1166,7 +1166,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
m.b_mtp_dq = decl_b(b, nm.f("mtp_dq"), RAD_I8, {g.max_logit_rows, g.n_embd});
|
||||
m.b_mtp_ds = decl_b(b, nm.f("mtp_ds"), RAD_F32, {g.max_logit_rows, 1});
|
||||
m.b_mtp_dsum = decl_b(b, nm.f("mtp_dsum"), RAD_F32, {g.n_embd / 128, dmpad});
|
||||
m.b_mtp_dlog = decl_b(b, nm.f("mtp_dlog"), g.act_dtype, {g.max_logit_rows, g.n_vocab_all});
|
||||
m.b_mtp_dlog = decl_b(b, nm.f("mtp_dlog"), g.act_dtype, {g.max_logit_rows, mtp_draft_rows(g.n_vocab_all)});
|
||||
/* A draft round's proposal comes out of row_topk here, never through the sampler's
|
||||
* token buffer. */
|
||||
m.b_mtp_didx = decl_b(b, nm.f("mtp_didx"), RAD_I32, {g.max_logit_rows, 1});
|
||||
|
||||
Reference in new issue
Block a user