Archived
radiance 1.13.0
New models: - Tencent Hy-Memory (HYV3) from its BF16, FP8 and GGUF checkpoints, with its native tool calls - M2XFP4: metadata-augmented MXFP4 weights for Qwen3.8-27B and its DFlash2 drafter (top-1 89.1% -> 92.4% against plain MXFP4), its quantiser and recipe, and ParoQuant's M2XFP4 checkpoints - Gemma 4 E2B/E4B audio input; NVIDIA-Nemotron-3.5-Bolt-E4B in int8 and 4-bit, on two cards by default; Ornith-1.5-122B compose files Speed: - Weight reads beside the decode all-reduces (qwen35_fp8, gemma3; --weight-prefetch, on by default): the 27B MXFP4 with DFlash2 61.2 -> 67.9 tok/s at c1, TranslateGemma 27B +13% - Head-256 split-KV decode attention at 256 workgroups and without bank conflicts (27B: +3-5% a step at 25-41K context), and the prefill KV split for small launches (a short turn over a long context decodes the other sequences 27% faster in that step) - MXFP4 decode gate_up, activation and fp8 quantiser in one launch (27B, Gemma 3 up to 64 rows: TranslateGemma +2-5% at 2-8 streams) - ParoQuant int4/int5 tiled GEMMs 6-9% faster - Speculation: a step's tokens are sent before the drafter's passes end, no step waits behind one that ends its requests, and the MTP head's first draft no longer collapses after short chats in the prefix cache Features: - --kv-resident N: KV streaming for a context bigger than VRAM (qwen4exp_fp8, GLM-5.3) - MiniMax-H3: reference media and keyframe conditioning with the VAE encoders - Krea 2 rebalance presets balanced, detail and subtle - The dashboard's request history; a stall watch that reports a frozen step's thread and stack - rad-tune at the served configuration, collectives measured as the exchange; Gemma 4 E2B/E4B's per-layer embeddings read ahead off the step path - A GPTQ calibration tap and recipe for Gemma 4 31B's w4a8 container Fixes: - The two-rank all-reduce checks that a peer's push has landed before reducing it - z-lab's DFlash drafter on Gemma 4 31B keeps drafting after a prefix-cache hit - MiniMax-H3's modulation rows Changes: Plugin ABI 31: out-of-tree plugins must be rebuilt. RADIANCE_PREFETCH_BESIDE is --weight-prefetch now; RADIANCE_NO_HUGE_PAGES, RADIANCE_IDS_EARLY, RADIANCE_ATTN_BAND_SPLIT, R4D_PREFILL_SPLITS, RAD_PLE_TIMING and RAD_PLE_CACHE_ROWS are gone.
This commit is contained in:
commit
6645337f69
198 files changed
+20904
-3240
No files matched your search
+99
-183
@@ -77,25 +77,29 @@ struct ActFP8 {
|
||||
rad_buf cq() const { return q8_fed ? q8 : q; }
|
||||
rad_buf cs() const { return q8_fed ? s8 : s; }
|
||||
|
||||
/* Declares the codes (and the int8 pair with `i8`); `x` is set by the caller.
|
||||
/* Declares the codes: the E4M3 pair with `fp8_pair`, the int8 pair with `i8` (an int8 model's
|
||||
* activations need no E4M3 plane taking arena); `x` is set by the caller.
|
||||
*
|
||||
* `rows` defaults to g.max_tok and must be given for any buffer that is not one row per token
|
||||
* (a MoE expert plane is `rows * top_k`, an encoder tower's its patch count); too small a value
|
||||
* overruns the arena (core/runtime/issue.cpp refuses such an issue). */
|
||||
int declare_qs(RadBuilder* b, Names& nm, const Geom& g, const char* name, int64_t n,
|
||||
int64_t rows = 0, bool i8 = false) {
|
||||
int64_t rows = 0, bool i8 = false, bool fp8_pair = true) {
|
||||
/* bf16 linears read only `x` (Geom::w_bf16), so declare no codes; an unused plane would
|
||||
* still take arena space. */
|
||||
if (g.w_bf16) { q = s = q8 = s8 = 0; return RAD_OK; }
|
||||
const int64_t m = rows > 0 ? rows : g.max_tok;
|
||||
q = decl_b(b, nm.f("%s.q", name), RAD_F8E4M3, { m, n });
|
||||
s = decl_b(b, nm.f("%s.s", name), RAD_F32, { m, fp8_blocks(n) });
|
||||
if (fp8_pair) {
|
||||
q = decl_b(b, nm.f("%s.q", name), RAD_F8E4M3, { m, n });
|
||||
s = decl_b(b, nm.f("%s.s", name), RAD_F32, { m, fp8_blocks(n) });
|
||||
if (!q || !s) return RAD_E_INVAL;
|
||||
}
|
||||
if (i8) {
|
||||
q8 = decl_b(b, nm.f("%s.q8", name), RAD_I8, { m, n });
|
||||
s8 = decl_b(b, nm.f("%s.s8", name), RAD_F32, { m, fp8_blocks(n) });
|
||||
if (!q8 || !s8) return RAD_E_INVAL;
|
||||
}
|
||||
return (q && s) ? RAD_OK : RAD_E_INVAL;
|
||||
return (fp8_pair || i8) ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -525,6 +529,10 @@ struct GateQuantFP8 {
|
||||
* codes (`op_q`). */
|
||||
rad_op op_q = 0;
|
||||
|
||||
/* Set before declare: the product in bf16 alone, as for bf16 linears, for a consumer that
|
||||
* quantises its input itself (an int8 linear's op_q). */
|
||||
bool silu_only = false;
|
||||
|
||||
int declare(RadBuilder* b, const Geom& g, rad_buf gate_up, const ActFP8& act, int64_t n,
|
||||
const char* act_name = "silu", int64_t max_rows = 0, bool bf16_out = true,
|
||||
bool rot = false, double limit = 0);
|
||||
@@ -541,15 +549,16 @@ inline int GateQuantFP8::declare(RadBuilder* b, const Geom& g, rad_buf gate_up,
|
||||
want_x = bf16_out;
|
||||
op_quant = 0;
|
||||
this->n = n;
|
||||
/* bf16 linears read the product itself: `silu_mul` into the bf16 plane only, and no
|
||||
* rotation. */
|
||||
bf16 = g.w_bf16;
|
||||
/* bf16 linears and `silu_only` read the product itself: `silu_mul` into the bf16 plane only,
|
||||
* and no rotation. */
|
||||
bf16 = g.w_bf16 || silu_only;
|
||||
clipped = limit > 0;
|
||||
clamped_fused = false;
|
||||
if (!std::isfinite(limit) || limit < 0) return RAD_E_INVAL;
|
||||
if (clipped) {
|
||||
/* No rotated int8 form: its quantiser would be quant_act_i8g, unrotated. */
|
||||
if (!gate_up || !a.x || n <= 0 || rot || (a.q8_fed && a.q8_rot) ||
|
||||
/* No rotated int8 form: its quantiser would be quant_act_i8g, unrotated; and no clamp
|
||||
* with `silu_only`. */
|
||||
if (!gate_up || !a.x || n <= 0 || rot || silu_only || (a.q8_fed && a.q8_rot) ||
|
||||
std::strcmp(act_name, "silu"))
|
||||
return RAD_E_INVAL;
|
||||
const int64_t rows = max_rows > 0 ? max_rows : g.max_tok;
|
||||
@@ -725,10 +734,8 @@ struct LinearFP8 {
|
||||
* view whose layout hook reduces it). Read from the weight's encoding. */
|
||||
bool mx = false;
|
||||
rad_weight wref = 0;
|
||||
/* The M2XFP4 sibling (libquant `m2xfp4`): the MXFP4 weight plus one metadata byte a group,
|
||||
* read by `gemm_nt_q` at dtype m2xfp4 with the same E4M3 pair, `b_scale` and `b_ref` as MXFP4's
|
||||
* are, the meta plane riding the weight operand's stored blob. Read from the weight's
|
||||
* encoding. */
|
||||
/* Or M2XFP4 (libquant `m2xfp4`): the MXFP4 views, the meta plane riding the weight operand,
|
||||
* read by `gemm_nt_q` at dtype m2xfp4. */
|
||||
bool m2x = false;
|
||||
/* Rotated (ParoQuant) variant of `mx`: the same GEMM over an activation that
|
||||
* `givens_dense_quant` makes from the bf16 plane (channel scale, dense per-group rotation and
|
||||
@@ -898,10 +905,9 @@ inline bool linear_is_mxfp4(const RadEncoding& e) {
|
||||
sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == RAD_MX_GROUP;
|
||||
}
|
||||
|
||||
/* The M2XFP4 sibling (arXiv 2601.19213, the paper's weight-side Sg-EM): the MXFP4 weight plus one
|
||||
* metadata byte per group of 32, four 2-bit subgroup extra mantissas that refine the exponent to
|
||||
* (1 + k/4) . 2^E (libquant's `m2xfp4` quantiser; libr4d's m2xfp4 layout hook). The exponent byte
|
||||
* carries the adaptive bias; the GEMM applies nothing beyond the planes. */
|
||||
/* M2XFP4 (arXiv 2601.19213, weight-side Sg-EM): the MXFP4 weight plus one metadata byte per group
|
||||
* of 32, four 2-bit subgroup extra mantissas that refine the scale to (1 + k/4) . 2^E. The
|
||||
* exponent byte carries the adaptive bias; the GEMM applies nothing beyond the planes. */
|
||||
inline bool linear_is_m2xfp4(const RadEncoding& e) {
|
||||
if (!rad_enc_is(&e, "m2xfp4") || e.n_planes != 3 || e.transform[0]) return false;
|
||||
const RadEncPlane *sc = rad_enc_plane(&e, "scale"), *mt = rad_enc_plane(&e, "meta");
|
||||
@@ -1165,10 +1171,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
|
||||
rot8 ? "unrotated" : "rotated");
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
if (!a.x || !a.q8 || !a.s8) {
|
||||
fprintf(stderr, "radiance: '%s' is an int8 weight and its input lacks the bf16 plane "
|
||||
"or the int8 code pair (the model declared its input before learning "
|
||||
"the trunk is int8)\n", wn);
|
||||
/* The bf16 plane only when this linear quantises it itself. */
|
||||
if (!a.q8 || !a.s8 || (!a.q8_fed && !a.x)) {
|
||||
fprintf(stderr, "radiance: '%s' is an int8 weight and its input lacks the int8 code "
|
||||
"pair%s (the model declared its input before learning the trunk is "
|
||||
"int8)\n", wn, a.q8_fed ? "" : " or the bf16 plane it quantises");
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
if (N % 16 || K % RAD_FP8_BLOCK) {
|
||||
@@ -1358,12 +1365,18 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
|
||||
/* ---- OCP-MXFP4 weight over the E4M3 pair. Three views: codes, exponents as `b_scale`, and
|
||||
* the same plane as `b_ref`. Row parts apply as is (the exponent grid is one row tall); column
|
||||
* slices must be whole 128s, the activation's scale block. */
|
||||
paro = known && linear_is_paro_mxfp4(enc);
|
||||
mx = known && (linear_is_mxfp4(enc) || paro);
|
||||
if (mx) {
|
||||
/* ---- OCP-MXFP4 or M2XFP4 weight over an E4M3 pair, the producer's or, for a rotated
|
||||
* (ParoQuant) weight, its own. Three views: codes (M2XFP4's with its meta plane, which rides
|
||||
* the weight operand), exponents as `b_scale`, and the same plane as `b_ref`. Row parts apply
|
||||
* as is (the exponent grid is one row tall); column slices must be whole 128s, the
|
||||
* activation's scale block. */
|
||||
const bool paro_mx = known && linear_is_paro_mxfp4(enc);
|
||||
const bool paro_m2 = known && linear_is_paro_m2xfp4(enc);
|
||||
paro = paro_mx || paro_m2;
|
||||
mx = known && (linear_is_mxfp4(enc) || paro_mx);
|
||||
m2x = known && (linear_is_m2xfp4(enc) || paro_m2);
|
||||
if (mx || m2x) {
|
||||
const char* fmt = m2x ? "M2XFP4" : "MXFP4";
|
||||
if (stored) {
|
||||
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
|
||||
"may\n", wn, stored);
|
||||
@@ -1373,29 +1386,29 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
|
||||
/* The rotation is applied to the activation, so this linear needs the input's bf16
|
||||
* plane (probe() set reads_x) and quantises it itself. */
|
||||
if (!a.x) {
|
||||
fprintf(stderr, "radiance: '%s' is a rotated MXFP4 weight, rotated and quantised "
|
||||
"from its input's bf16 plane, and its input has none\n", wn);
|
||||
fprintf(stderr, "radiance: '%s' is a rotated %s weight, rotated and quantised "
|
||||
"from its input's bf16 plane, and its input has none\n", wn, fmt);
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
if (g.world > 1 || span_hi > 0) {
|
||||
fprintf(stderr, "radiance: '%s' is a rotated MXFP4 weight; its rotation is per "
|
||||
fprintf(stderr, "radiance: '%s' is a rotated %s weight; its rotation is per "
|
||||
"projection and is not cut by a tensor-parallel shard (world %d)\n",
|
||||
wn, g.world);
|
||||
wn, fmt, g.world);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
} else if (!a.q || !a.s) {
|
||||
fprintf(stderr, "radiance: '%s' is an MXFP4 weight read against its input's E4M3 "
|
||||
"pair, and its input has none\n", wn);
|
||||
fprintf(stderr, "radiance: '%s' is an %s weight read against its input's E4M3 "
|
||||
"pair, and its input has none\n", wn, fmt);
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
if (N % 16 || K % RAD_FP8_BLOCK) {
|
||||
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an MXFP4 weight takes rows in "
|
||||
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an %s weight takes rows in "
|
||||
"16s and K in whole %d-column activation scale blocks\n",
|
||||
base, (long long)N, (long long)K, RAD_FP8_BLOCK);
|
||||
base, (long long)N, (long long)K, fmt, RAD_FP8_BLOCK);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
RowParts rp;
|
||||
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, "MXFP4 tiles"));
|
||||
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, m2x ? "M2XFP4 tiles" : "MXFP4 tiles"));
|
||||
int64_t pb1 = 0, pb2 = 0;
|
||||
if (paro) {
|
||||
std::vector<int64_t> cut;
|
||||
@@ -1413,8 +1426,8 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
|
||||
}
|
||||
const char* sn = nm.f("%s.scale", base);
|
||||
const char* rn = nm.f("%s.ref", base);
|
||||
w = decl_view(b, wn, nullptr, "codes", RAD_FP4E2M1, { N, K }, access,
|
||||
shard, grp, rp.w, rp.n);
|
||||
w = decl_view(b, wn, nullptr, m2x ? "codes,meta" : "codes", RAD_FP4E2M1, { N, K },
|
||||
access, shard, grp, rp.w, rp.n);
|
||||
ws = decl_view(b, sn, wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
access, shard, grp, rp.w, rp.n);
|
||||
wref = decl_view(b, rn, wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
@@ -1435,100 +1448,12 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
|
||||
RAD_ARCH_TRY(rad_weight_shard_span(b, wref, lo, hi));
|
||||
}
|
||||
/* One GEMM: a rotated weight reads its own rotated pair, a plain one the producer's. */
|
||||
op = decl_gemm_q(b, 1, max_rows > 0 ? max_rows : g.max_tok, RAD_MX_GROUP, "mxfp4a8", pb1, pb2,
|
||||
op = decl_gemm_q(b, 1, max_rows > 0 ? max_rows : g.max_tok, RAD_MX_GROUP,
|
||||
m2x ? "m2xfp4" : "mxfp4a8", pb1, pb2,
|
||||
{ w, ws, wref }, { paro ? rot_q : a.q, paro ? rot_s : a.s }, y);
|
||||
return op ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* ---- M2XFP4 weight over the E4M3 pair: the MXFP4 weight's views plus the metadata plane,
|
||||
* which rides the weight operand -- the declared view takes codes and meta together, and the
|
||||
* stored blob carries the meta in its tail (libr4d's m2xfp4 layout). The exponent plane is
|
||||
* `b_scale` and `b_ref` as MXFP4's is; the row parts, the shape bounds and the column-shard
|
||||
* rule are the MXFP4 ones.
|
||||
*
|
||||
* A rotated weight (ParoQuant) carries the same planes in the basis its "givens" tables name
|
||||
* and is declared like the rotated MXFP4 one: `givens_dense_quant` makes the E4M3 pair from
|
||||
* the input's bf16 plane, once per rotated part, and the GEMM meets copy p with part p's rows
|
||||
* (`pb1`/`pb2`). `paro` makes step() issue that rotation first. */
|
||||
const bool m2rot = known && linear_is_paro_m2xfp4(enc);
|
||||
m2x = known && (linear_is_m2xfp4(enc) || m2rot);
|
||||
if (m2x) {
|
||||
paro = m2rot;
|
||||
if (stored) {
|
||||
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
|
||||
"may\n", wn, stored);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (m2rot) {
|
||||
if (!a.x) {
|
||||
fprintf(stderr, "radiance: '%s' is a rotated M2XFP4 weight, rotated and quantised "
|
||||
"from its input's bf16 plane, and its input has none\n", wn);
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
if (g.world > 1 || span_hi > 0) {
|
||||
fprintf(stderr, "radiance: '%s' is a rotated M2XFP4 weight; its rotation is per "
|
||||
"projection and is not cut by a tensor-parallel shard (world %d)\n",
|
||||
wn, g.world);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
} else if (!a.q || !a.s) {
|
||||
fprintf(stderr, "radiance: '%s' is an M2XFP4 weight read against its input's E4M3 "
|
||||
"pair, and its input has none\n", wn);
|
||||
return RAD_E_DTYPE;
|
||||
}
|
||||
if (N % 16 || K % RAD_FP8_BLOCK) {
|
||||
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an M2XFP4 weight takes rows in "
|
||||
"16s and K in whole %d-column activation scale blocks\n",
|
||||
base, (long long)N, (long long)K, RAD_FP8_BLOCK);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
RowParts rp;
|
||||
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, "M2XFP4 tiles"));
|
||||
int64_t pb1 = 0, pb2 = 0;
|
||||
if (m2rot) {
|
||||
std::vector<int64_t> cut;
|
||||
for (int64_t p : rot_part_rows) cut.push_back(p);
|
||||
if (cut.empty()) for (int64_t p : row_parts) cut.push_back(p);
|
||||
RAD_ARCH_TRY(declare_rotation(b, nm, base, wn, enc, N, K, grp, cut, &pb1, &pb2));
|
||||
const int64_t rows = max_rows > 0 ? max_rows : g.max_tok;
|
||||
rot_q = decl_b(b, nm.f("%s.rot_q", base), RAD_F8E4M3, { rows, rot_parts * K });
|
||||
rot_s = decl_b(b, nm.f("%s.rot_s", base), RAD_F32,
|
||||
{ rows, rot_parts * (K / RAD_FP8_BLOCK) });
|
||||
if (!rot_q || !rot_s) return RAD_E_INVAL;
|
||||
rot_dense = decl_dense(b, nm, base, wn, enc, K, grp);
|
||||
if (!rot_dense) return RAD_E_INVAL;
|
||||
op_rot = decl_dense_op(b, g, a, "fp8", 1, rows, { rot_q, rot_s });
|
||||
if (!op_rot) return RAD_E_INVAL;
|
||||
}
|
||||
const char* sn = nm.f("%s.scale", base);
|
||||
const char* rn = nm.f("%s.ref", base);
|
||||
w = decl_view(b, wn, nullptr, "codes,meta", RAD_FP4E2M1, { N, K }, access, shard, grp,
|
||||
rp.w, rp.n);
|
||||
ws = decl_view(b, sn, wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
access, shard, grp, rp.w, rp.n);
|
||||
wref = decl_view(b, rn, wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
access, shard, grp, rp.w, rp.n);
|
||||
if (!w || !ws || !wref) return RAD_E_INVAL;
|
||||
if (span_hi > 0) {
|
||||
const bool col = shard == RAD_SHARD_COL;
|
||||
if (col && (span_lo % RAD_FP8_BLOCK || span_hi % RAD_FP8_BLOCK)) {
|
||||
fprintf(stderr, "radiance: '%s' is sliced [%lld, %lld) on this rank, which cuts a "
|
||||
"%d-column activation scale block\n", base, (long long)span_lo,
|
||||
(long long)span_hi, RAD_FP8_BLOCK);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
const int64_t lo = col ? span_lo / RAD_MX_GROUP : span_lo;
|
||||
const int64_t hi = col ? span_hi / RAD_MX_GROUP : span_hi;
|
||||
RAD_ARCH_TRY(rad_weight_shard_span(b, w, span_lo, span_hi));
|
||||
RAD_ARCH_TRY(rad_weight_shard_span(b, ws, lo, hi));
|
||||
RAD_ARCH_TRY(rad_weight_shard_span(b, wref, lo, hi));
|
||||
}
|
||||
/* One GEMM: a rotated weight reads its own rotated pair, a plain one the producer's. */
|
||||
op = decl_gemm_q(b, 1, max_rows > 0 ? max_rows : g.max_tok, RAD_MX_GROUP, "m2xfp4", pb1,
|
||||
pb2, { w, ws, wref }, { m2rot ? rot_q : a.q, m2rot ? rot_s : a.s }, y);
|
||||
return op ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* Block fp8 is the last form; any other encoding is refused by name with the ones read. */
|
||||
if (known) {
|
||||
const RadEncPlane* sc = rad_enc_plane(&enc, "scale");
|
||||
@@ -1652,17 +1577,9 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
|
||||
RAD_W(w), RAD_W(ws), brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
|
||||
return;
|
||||
}
|
||||
if (m2x && !paro) {
|
||||
/* The MXFP4 operands over the producer's pair; the meta rides the weight's stored blob. */
|
||||
RAD_ISSUE_N(c, op, rows, brow_slice(a.q, r0, rows, K),
|
||||
brow_slice(a.s, r0, rows, K / RAD_FP8_BLOCK),
|
||||
RAD_W(w), RAD_W(ws), brow_slice(y, yr, rows, N), RAD_NONE, RAD_W(wref));
|
||||
return;
|
||||
}
|
||||
if (mx || m2x) {
|
||||
/* A rotated weight first rotates its input into its own codes, `rot_parts` copies side by
|
||||
* side; a plain weight reads the producer's. A rotated M2XFP4 weight is issued here as the
|
||||
* rotated MXFP4 one is, its meta riding the weight operand's stored blob. */
|
||||
/* A rotated weight (MXFP4 or M2XFP4) first rotates its input into its own codes,
|
||||
* `rot_parts` copies side by side; a plain weight reads the producer's. */
|
||||
const rad_buf aq = paro ? rot_q : a.q, as = paro ? rot_s : a.s;
|
||||
const int64_t ak = rot_parts * K;
|
||||
if (paro)
|
||||
@@ -1697,10 +1614,7 @@ struct DenseFP8 {
|
||||
* works against the same E4M3 activation pair. */
|
||||
rad_weight ref = 0;
|
||||
bool mx = false;
|
||||
/* Or M2XFP4 (libquant `m2xfp4`): the MXFP4 weight plus one metadata byte a group, read by
|
||||
* `gemm_nt_q` at dtype m2xfp4 with the same views and the same `b_ref`. The meta plane rides
|
||||
* the weight operand's stored blob (libr4d's m2xfp4 layout), so the declared weight takes the
|
||||
* codes and meta planes together. */
|
||||
/* Or M2XFP4 (libquant `m2xfp4`): the MXFP4 views, the meta plane riding the weight operand. */
|
||||
bool m2x = false;
|
||||
|
||||
/* `mapped`: the caller made the name map already, to ask the encoding before choosing this
|
||||
@@ -1739,16 +1653,11 @@ struct DenseFP8 {
|
||||
rad_op gemm(RadBuilder* b, int64_t hi, int64_t N, int64_t K,
|
||||
std::initializer_list<rad_buf> reads, std::initializer_list<rad_buf> writes,
|
||||
int64_t split_n = 0) const {
|
||||
if (m2x)
|
||||
if (mx || m2x)
|
||||
return rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
|
||||
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "m2xfp4")),
|
||||
RAD_WEIGHTS(w, s, ref)),
|
||||
reads, writes);
|
||||
if (mx)
|
||||
return rw(b, RAD_OP(b, "gemm_nt_q",
|
||||
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_INT("group", RAD_MX_GROUP),
|
||||
RAD_STR("dtype", m2x ? "m2xfp4" : "mxfp4a8")),
|
||||
RAD_WEIGHTS(w, s, ref)),
|
||||
reads, writes);
|
||||
if (split_n > 0)
|
||||
@@ -1775,44 +1684,27 @@ inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t
|
||||
std::initializer_list<int64_t> row_parts, bool mapped) {
|
||||
if (N <= 0 || K <= 0 || src.size() == 0) return RAD_E_INVAL;
|
||||
|
||||
/* ---- OCP-MXFP4, from the weight's encoding. The exponent grid is one row tall, so parts need
|
||||
* only whole 16-row tiles; K takes whole 128s for the activation's per-(row, 128) scale. */
|
||||
/* ---- OCP-MXFP4 or M2XFP4, from the weight's encoding. The exponent grid is one row tall, so
|
||||
* parts need only whole 16-row tiles; K takes whole 128s for the activation's per-(row, 128)
|
||||
* scale. */
|
||||
{
|
||||
const char* wn = nm.f("%s.weight", base);
|
||||
if (!mapped) RAD_ARCH_TRY(map_linear(b, wn, src));
|
||||
RadEncoding enc{};
|
||||
mx = weight_enc(b, wn, &enc) && linear_is_mxfp4(enc);
|
||||
m2x = !mx && linear_is_m2xfp4(enc); /* enc stays zero when there is no encoding */
|
||||
if (mx) {
|
||||
const bool known = weight_enc(b, wn, &enc);
|
||||
mx = known && linear_is_mxfp4(enc);
|
||||
m2x = known && linear_is_m2xfp4(enc);
|
||||
if (mx || m2x) {
|
||||
if (N % 16 || K % RAD_FP8_BLOCK) {
|
||||
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an MXFP4 weight takes rows "
|
||||
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an %s weight takes rows "
|
||||
"in 16s and K in whole %d-column activation scale blocks\n",
|
||||
base, (long long)N, (long long)K, RAD_FP8_BLOCK);
|
||||
base, (long long)N, (long long)K, m2x ? "M2XFP4" : "MXFP4", RAD_FP8_BLOCK);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
RowParts rp;
|
||||
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, "MXFP4 tiles"));
|
||||
w = decl_view(b, wn, nullptr, "codes", RAD_FP4E2M1, { N, K }, access, shard, grp,
|
||||
rp.w, rp.n);
|
||||
s = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, m2x ? "M2XFP4 tiles" : "MXFP4 tiles"));
|
||||
w = decl_view(b, wn, nullptr, m2x ? "codes,meta" : "codes", RAD_FP4E2M1, { N, K },
|
||||
access, shard, grp, rp.w, rp.n);
|
||||
ref = decl_view(b, nm.f("%s.ref", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
access, shard, grp, rp.w, rp.n);
|
||||
return w && s && ref ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
/* ---- M2XFP4: the MXFP4 views with the metadata plane riding the weight operand, the
|
||||
* same bounds. */
|
||||
if (m2x) {
|
||||
if (N % 16 || K % RAD_FP8_BLOCK) {
|
||||
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an M2XFP4 weight takes rows "
|
||||
"in 16s and K in whole %d-column activation scale blocks\n",
|
||||
base, (long long)N, (long long)K, RAD_FP8_BLOCK);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
RowParts rp;
|
||||
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, "M2XFP4 tiles"));
|
||||
w = decl_view(b, wn, nullptr, "codes,meta", RAD_FP4E2M1, { N, K }, access, shard, grp,
|
||||
rp.w, rp.n);
|
||||
s = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
access, shard, grp, rp.w, rp.n);
|
||||
ref = decl_view(b, nm.f("%s.ref", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
|
||||
@@ -1859,10 +1751,9 @@ inline rad_op decl_gated_fold(RadBuilder* b, int64_t rows, int64_t N, int64_t K,
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
* plane and that plane again as the per-row reference), up to min(rows, 8) rows, the band of
|
||||
* libr4d's one-to-eight-row GEMM. Declared only when a loaded library declares the op (libref
|
||||
* does not), so a CPU-only engine takes the pair. */
|
||||
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,
|
||||
@@ -1880,6 +1771,31 @@ inline rad_op decl_gated_fold_mx(RadBuilder* b, int64_t rows, int64_t N, int64_t
|
||||
return op;
|
||||
}
|
||||
|
||||
/* ================================================== a calibration tap */
|
||||
/* One tap of the GPTQ calibration (calib_dir): the Gram of a linear's bf16 input, summed over the
|
||||
* corpus in a PERSIST [K, K] f32 buffer that calib_write() saves. */
|
||||
struct CalibTap {
|
||||
rad_buf h = 0;
|
||||
rad_op op = 0;
|
||||
int64_t K = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, int64_t max_tok, const char* name, rad_buf a, int64_t k) {
|
||||
K = k;
|
||||
h = decl_b(b, nm.f("%s.gram", name), RAD_F32, { k, k }, RAD_BUF_PERSIST);
|
||||
if (!h) return RAD_E_INVAL;
|
||||
op = rw(b, RAD_OP(b, "gram_accum",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, max_tok), RAD_INT("n", k),
|
||||
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ a, h }, { h });
|
||||
return op ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
/* gram_accum takes a, a_scale?, h: no scale for a bf16 input. */
|
||||
void step(RadCtx* c, rad_buf a, int64_t T, bool live) const {
|
||||
if (op && live) RAD_ISSUE_N(c, op, T, brows(a, T), RAD_NONE, brows(h, K));
|
||||
}
|
||||
};
|
||||
|
||||
} /* namespace arch */
|
||||
} /* namespace rad */
|
||||
|
||||
|
||||
Reference in new issue
Block a user