glm5next: the MLA row form, opt-in with RADIANCE_GLM_MLA_ROWS=1 -- a prefill-sized step by rows over the ranks, each rank with all 64 heads

From GlmTp::kSplitRows each rank runs its run of the step's rows (GlmTp::run_of: ceil(T / world)
rows, the last run shorter) through q_b, absorption, attention with all 64 heads
(mla_attn_rows_bf16), expansion and o, and an all_gather of whole runs takes the all-reduce's
place. The latent cache and the DSA selection are whole on every rank, so nothing is exchanged
before the attention; each rank sends its run to seven peers, half the bytes of the two-shot
all-reduce, through the same exchange instance. In a 4096-row chunk at 16k of context a rank's
attention drops from 14.0 to 3.9 ms a layer on the kernel bench. The price is q_b, kv_b and o at
full width on every rank: 120 MiB a layer, 1.29 GiB a card over the 11 trunk layers, and about
0.1 GiB of activations; the MTP layer keeps its heads. The full-width weights are whole views
(RadWeightDecl::source) of blk.N.attn_q_b, attn_kv_b and attn_output, the head form's container
entries, so they take the stored encoding (fp8a8f32, bf16) and need no name map; LinearFP8 gets
`stored` for that, served by its block-fp8 branch only, and LinearFP8::step writes a run of rows
from a given output row.

The head form stays the default, unset or RADIANCE_GLM_MLA_ROWS=0, and declares none of it: that
graph is the one before this change, byte for byte (the pinned hashes). The row form is not
bit-identical to the head form, and GLM-5.3 turns that into a measurable divergence: over a
43k-position KL corpus at TP8 (English prose, German prose, code) the row form scores a mean KL
of 0.033 against the head form (median 0.004, top-1 agreement 93.5 %), with greedy text
unchanged. The projections are not the cause: q_b and o over a rank's rows give the head form's
bytes (r4d_emu), and o summed as eight rank partials in the all-reduce's order
(RADIANCE_GLM_MLA_ROWS=2, a test form, GlmMla::Form::rows_sliced_o) scores 0.032. The
attention's rounding is: the rows kernel parts from the paired union tile in about 0.1 % of
every row's outputs by one bf16 step (the union pairing sums a pair's second row in another
order), and the 16-head tile, which parts in the same share of every second row only, halves the
KL to 0.015. A bit-identical row form would run the union tile eight times a row over its own
group of eight heads, without this kernel's gain, or need a rows kernel with the union's
arithmetic; neither is here, so the form is opt-in.

Measured on eight R9700 at Gen5 links, the same build: a 20k-token prefill 5074 tok/s in the row
form against 4270 in the head form, BetterBench prefill at 22k 4984 against 4151, single stream
181.8 against 180.2 tok/s, eight streams 468 against 454.

glm5next_test declares the block at one to eight ranks and checks the full-width views and their
sources, the fp8a8f32 GEMMs, the gather's bound against the all-reduce's, each rank's operands
at 300 rows (runs of 38, the last 34 at eight ranks) and the head form at 255; the o-slices
form's eight GEMMs and seven adds; the model's counts and the TP8 pin under
RADIANCE_GLM_MLA_ROWS=1 (0x1d378152c40f8834 / 0x82ed1094dbf4b637), with the default pins
unchanged; and the row rule against both formulas it replaced. plugin_test resolves every band
of every op the row form adds at eight ranks against libr4d, with no card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 committed 2026-10-22 09:58:07 +00:00
1 parent 7d7c66da55
commit eb14e4f642
9 files changed
+717 -76

No files matched your search

+186 -5
View File
@@ -3,14 +3,23 @@
* complete heads shard as rows of the shared K/V weight. prepare() exposes the normalized q latent
* for the indexer; finish() consumes the indexer's selection. The caller owns the mHC residual.
*
* Under tensor parallelism the 64 heads split over the ranks (GlmTp::heads): the rows of q_b and
* kv_b, the columns of o, an all_reduce of the output; q_a, kv_a and the latent cache are
* replicated. */
* Under tensor parallelism the head form splits the 64 heads over the ranks (GlmTp::heads): the
* rows of q_b and kv_b, the columns of o, an all_reduce of the output; q_a, kv_a and the latent
* cache are replicated. From GlmTp::kSplitRows a prefill-sized step takes the row form instead
* (Wire::rows, Form::rows): each rank runs its run of the step's rows (GlmTp::run_of) through q_b,
* absorption, attention, expansion and the output projection with all 64 heads, then an
* all_gather lays the runs out as the whole step. The latent cache and the selection are whole on
* every rank already, so the split needs no other exchange. The gather moves half the bytes of
* the head form's all-reduce, and the attention loads each key once for all 64 heads of a row.
* The price is the three projections at full width on every rank (126 MB a layer); RowsWire::q
* unset declares neither them nor the form. */
#ifndef RAD_BLOCK_GLM_MLA_H
#define RAD_BLOCK_GLM_MLA_H
#include "rad_fp8.h"
#include "rad_glm_tp.h"
#include <algorithm>
#include <cmath>
#include <vector>
namespace rad {
namespace arch {
@@ -88,11 +97,33 @@ struct MlaLatent {
};
struct GlmMla {
/* How a prefill-sized step under tensor parallelism runs: `heads` keeps the head form at every
* step size and spares the full-width weights' VRAM; `rows` is the row form; `rows_sliced_o`
* the row form with o summed as the head form sums it (a test of how much of the two forms'
* difference is o's rounding). The plugin chooses; the block takes the choice through which
* RowsWire buffers it is given. */
enum class Form { heads, rows, rows_sliced_o };
/* The row form's buffers, `rows_for(max_tok)` rows each. `out` (Wire::out) must hold
* world * rows_for(T) rows, since every rank's run is gathered at full length. */
struct RowsWire {
rad_buf q = 0, q_latent = 0, attn_latent = 0, out = 0;
ActFP8 attn{};
/* Form::rows_sliced_o only: o in the head form's world K slices, each rounded to bf16
* ([world * rows, 4096]), summed in f32 in ascending slice order as the exact all-reduce
* folds ranks (two [rows, 4096] f32 planes), rounded once. */
rad_buf slices = 0, acc = 0, widened = 0;
};
struct Wire {
ActFP8 input{}, q_low{}, attn{};
rad_buf q_raw = 0, kv_raw = 0, latent = 0, out = 0;
MlaLatent::Wire core{};
RowsWire rows{};
};
/* The gathered row of the row form: 4096 bf16, so a run is ceil(T / world) rows. */
static constexpr int64_t kOutRowBytes = 4096 * 2;
static int64_t rows_for(int64_t T, int world) {
return GlmTp::run_rows(T, world, kOutRowBytes);
}
Geom g{};
Wire w{};
int layer = 0;
@@ -103,6 +134,12 @@ struct GlmMla {
MlaLatent core{};
rad_weight q_gain = 0, kv_gain = 0;
rad_op op_kv_norm = 0, op_store = 0, op_ar = 0;
LinearFP8 rows_q_b{}, rows_output{};
QuantFP8 rows_quant{};
rad_weight rows_kv_b = 0;
rad_op op_rows_absorb = 0, op_rows_attn = 0, op_rows_expand = 0, op_rows_gather = 0;
std::vector<LinearFP8> rows_o_slices;
rad_op op_slice_first = 0, op_slice_widen = 0, op_slice_add = 0, op_slice_narrow = 0;
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire,
const char* prefix, rad_kvgroup group, bool reduce = true,
bool defer_finish = false) {
@@ -160,7 +197,126 @@ struct GlmMla {
GlmTp::mla_width(g.world), RAD_SHARD_COL, grp_layer(layer),
{ nm.ckpt("%s.o_proj.weight", prefix) }, w.attn, w.out));
if (reduce) op_ar = GlmTp::reduce(b, g, w.out);
return op_store && (!(reduce && g.world > 1) || op_ar) ? RAD_OK : RAD_E_INVAL;
if (!op_store || ((reduce && g.world > 1) && !op_ar)) return RAD_E_INVAL;
return reduce ? declare_rows(b, nm, prefix) : RAD_OK;
}
/* Declared whatever the step size, as every TP split here is: arena levels declare again at a
* smaller max_tok and must get the same graph. The gather's message bound is the all-reduce's,
* so both share one exchange instance and its scratch. */
int declare_rows(RadBuilder* b, Names& nm, const char* prefix) {
const auto& r = w.rows;
if (!r.q || g.world < 2) return RAD_OK;
if (!r.q_latent || !r.attn_latent || !r.out || !r.attn.x || r.attn.x == r.q) return RAD_E_INVAL;
const int64_t R = rows_for(g.max_tok, g.world);
const auto wg = grp_layer(layer);
/* Whole views of the head form's container entries: one stored weight each, whose
* encoding (block fp8 at f32 scales, the decoded bf16 kv_b) the views inherit. */
rows_q_b.stored = nm.f("blk.%d.attn_q_b.weight", layer);
RAD_ARCH_TRY(rows_q_b.declare(b, nm, g, nm.f("blk.%d.attn_q_b_rows", layer), 16384, 1536,
RAD_SHARD_NONE, wg, { nm.ckpt("%s.q_b_proj.weight", prefix) },
w.q_low, r.q, R));
rows_kv_b = decl_view(b, nm.f("blk.%d.attn_kv_b_rows.weight", layer),
nm.f("blk.%d.attn_kv_b.weight", layer), nullptr, RAD_BF16,
{ 64 * 2 * 256, 512 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, wg);
if (!rows_kv_b) return RAD_E_INVAL;
auto project = [&](const char* mode, rad_buf in, rad_buf out) {
return rw(b,
RAD_OP(b, "mla_project",
RAD_PARAMS(RAD_RANGE("M", 1, R), RAD_INT("heads", 64),
RAD_INT("head_dim", 256), RAD_INT("latent_dim", 512),
RAD_STR("mode", mode), RAD_STR("dtype", "bf16")),
RAD_WEIGHTS(rows_kv_b)),
{ in }, { out });
};
op_rows_absorb = project("absorb", r.q, r.q_latent);
op_rows_attn =
rw(b,
RAD_OP(b, "mla_attn",
RAD_PARAMS(RAD_RANGE("M", 1, R), RAD_INT("latent_dim", 512),
RAD_INT("heads", 64), RAD_INT("blocks", core.c.blocks),
RAD_INT("block_size", 4), RAD_F64("scale", 1.0 / std::sqrt(256.0)),
RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS),
{ r.q_latent, w.core.selected, w.core.length }, { r.attn_latent });
op_rows_expand = project("expand", r.attn_latent, r.attn.x);
RAD_ARCH_TRY(rows_quant.declare(b, g, r.attn, 16384));
if (r.slices) {
RAD_ARCH_TRY(declare_o_slices(b, nm, prefix, R));
} else {
rows_output.stored = nm.f("blk.%d.attn_output.weight", layer);
RAD_ARCH_TRY(rows_output.declare(b, nm, g, nm.f("blk.%d.attn_output_rows", layer),
4096, 16384, RAD_SHARD_NONE, wg,
{ nm.ckpt("%s.o_proj.weight", prefix) }, r.attn,
r.out, R));
}
op_rows_gather = rw(b,
RAD_OP(b, "all_gather",
RAD_PARAMS(RAD_INT("world_size", g.world),
RAD_RANGE("numel", 4096, g.max_tok * 4096),
RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS),
{ r.out }, { w.out });
return op_rows_absorb && op_rows_attn && op_rows_expand && op_rows_gather ? RAD_OK
: RAD_E_INVAL;
}
/* The test form of o: slice k is the head form's rank k slice of the stored weight's columns,
* a GEMM over the matching columns of the quantised input. */
int declare_o_slices(RadBuilder* b, Names& nm, const char* prefix, int64_t R) {
const auto& r = w.rows;
if (!r.acc || !r.widened) return RAD_E_INVAL;
const int64_t K = 16384 / g.world;
rows_o_slices.assign((size_t)g.world, LinearFP8{});
for (int k = 0; k < g.world; ++k) {
auto& lin = rows_o_slices[(size_t)k];
lin.stored = nm.f("blk.%d.attn_output.weight", layer);
RAD_ARCH_TRY(lin.declare(b, nm, g, nm.f("blk.%d.attn_output_rows%d", layer, k), 4096,
K, RAD_SHARD_COL, grp_layer(layer),
{ nm.ckpt("%s.o_proj.weight", prefix) }, r.attn, r.slices, R,
{}, k * K, (k + 1) * K));
}
auto cast = [&](const char* from, const char* to, rad_buf x, rad_buf y) {
return rw(b,
RAD_OP(b, "cast",
RAD_PARAMS(RAD_RANGE("M", 1, R), RAD_INT("n", 4096),
RAD_STR("from", from), RAD_STR("to", to)),
RAD_NOWEIGHTS),
{ x }, { y });
};
op_slice_first = cast("bf16", "f32", r.slices, r.acc);
op_slice_widen = cast("bf16", "f32", r.slices, r.widened);
op_slice_add = rw(b,
RAD_OP(b, "add",
RAD_PARAMS(RAD_RANGE("M", 1, R), RAD_INT("n", 4096),
RAD_STR("dtype", "f32")),
RAD_NOWEIGHTS),
{ r.acc, r.widened }, { r.acc });
op_slice_narrow = cast("f32", "bf16", r.acc, r.out);
return op_slice_first && op_slice_widen && op_slice_add && op_slice_narrow ? RAD_OK
: RAD_E_INVAL;
}
void step_o_slices(RadCtx* ctx, int64_t run, int64_t n) const {
const auto& r = w.rows;
const int64_t K = 16384 / g.world, G = K / RAD_FP8_BLOCK;
for (int k = 0; k < g.world; ++k) {
const auto& lin = rows_o_slices[(size_t)k];
RAD_ISSUE_N(ctx, lin.op, n, bcol(r.attn.q, k * K, K, n), bcol(r.attn.s, k * G, G, n),
RAD_W(lin.w), RAD_W(lin.ws), brow_slice(r.slices, k * run, n, 4096),
RAD_NONE, RAD_NONE);
}
RAD_ISSUE_N(ctx, op_slice_first, n, brow_slice(r.slices, 0, n, 4096), brows(r.acc, n));
for (int k = 1; k < g.world; ++k) {
RAD_ISSUE_N(ctx, op_slice_widen, n, brow_slice(r.slices, k * run, n, 4096),
brows(r.widened, n));
RAD_ISSUE_N(ctx, op_slice_add, n, brows(r.acc, n), brows(r.widened, n),
brows(r.acc, n));
}
RAD_ISSUE_N(ctx, op_slice_narrow, n, brows(r.acc, n), brows(r.out, n));
}
/* From the TP split threshold, and while the last rank's run is not empty (always at that
* threshold for up to 8 ranks); the same answer on every rank, as the gather needs. */
bool rows_take(int64_t T) const {
return op_rows_gather && GlmTp::splits(T, g.world) &&
(g.world - 1) * rows_for(T, g.world) < T;
}
void prepare(RadCtx* ctx, const RadBatch* batch) const {
const auto* kb = kv_batch(batch, kv);
@@ -173,17 +329,42 @@ struct GlmMla {
kv_a.step(ctx, w.input, w.kv_raw, T);
q_norm.step(ctx, T);
RAD_ISSUE_N(ctx, op_kv_norm, T, brows(w.kv_raw, T), RAD_W(kv_gain), brows(w.latent, T));
q_b.step(ctx, w.q_low, w.core.q, T);
if (!rows_take(T)) q_b.step(ctx, w.q_low, w.core.q, T);
RAD_ISSUE_N(ctx, op_store, T, bcol(w.latent, 0, 256, T), bcol(w.latent, 256, 256, T),
praw(kb->slot_mapping, RAD_I32, T), kv_cache(kv, layer));
}
void finish(RadCtx* ctx, const RadBatch* batch) const {
const int64_t T = batch->n_tok;
if (rows_take(T)) {
finish_rows(ctx, T);
return;
}
core.step(ctx, T);
attn_quant.step(ctx, T);
output.step(ctx, w.attn, w.out, T);
if (op_ar) RAD_ISSUE_N(ctx, op_ar, T * 4096, brows(w.out, T), RAD_NONE);
}
/* This rank's rows [first, first + n) end to end; the last rank's run may be short, and the
* gather sends its unwritten tail rows, which land past T. */
void finish_rows(RadCtx* ctx, int64_t T) const {
const auto& r = w.rows;
const GlmTp::Run run = GlmTp::run_of(T, g.world, g.rank, kOutRowBytes);
const int64_t first = run.first, n = run.rows;
rows_q_b.step(ctx, w.q_low, r.q, T, first, n, 0);
RAD_ISSUE_N(ctx, op_rows_absorb, n, brows(r.q, n), RAD_W(rows_kv_b), brows(r.q_latent, n));
RAD_ISSUE_N(ctx, op_rows_attn, n, brows(r.q_latent, n), kv_cache(kv, layer),
brow_slice(w.core.selected, first, n, core.c.blocks),
brow_slice(w.core.length, first, n, 1), brows(r.attn_latent, n));
RAD_ISSUE_N(ctx, op_rows_expand, n, brows(r.attn_latent, n), RAD_W(rows_kv_b),
brows(r.attn.x, n));
rows_quant.step(ctx, n);
if (r.slices)
step_o_slices(ctx, run.run, n);
else
rows_output.step(ctx, r.attn, r.out, n);
RAD_ISSUE_N(ctx, op_rows_gather, run.run * 4096, brows(r.out, run.run),
brows(w.out, g.world * run.run));
}
};
}
} // namespace rad::arch
+34 -4
View File
@@ -745,6 +745,10 @@ struct LinearFP8 {
* this is set; otherwise the linear reads unwritten arena bytes. */
bool reads_x = 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
* before declare(); no name map is made, and only block fp8 is served this way. */
const char* stored = nullptr;
/* The weight's access class (rad_builder.h): read every pass by default; a linear run once a
* request (an encoder's) says so, which lets placement keep it off the card. Set before
* declare(). */
@@ -1039,9 +1043,10 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
/* Name map first: the weight's encoding, looked up through it, picks the branch below. */
const char* wn = nm.f("%s.weight", base);
if (!mapped) RAD_ARCH_TRY(map_linear(b, wn, src));
if (!mapped && !stored) RAD_ARCH_TRY(map_linear(b, wn, src));
const char* sw = stored ? stored : wn;
RadEncoding enc{};
const bool known = weight_enc(b, wn, &enc);
const bool known = weight_enc(b, sw, &enc);
const bool plain = known && rad_enc_is(&enc, "plain") && enc.n_planes == 1 &&
enc.plane[0].dtype == RAD_BF16;
@@ -1051,6 +1056,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* constraints. */
bf16 = g.w_bf16 || plain;
if (bf16) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (known && !plain) {
char en[256];
rad_enc_format(&enc, en, sizeof en);
@@ -1089,6 +1099,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
const bool rot8 = known && linear_is_w8h(enc);
i8 = known && (linear_is_w8(enc) || rot8);
if (i8) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (a.q8_fed && rot8 != a.q8_rot) {
fprintf(stderr, "radiance: '%s' is a%s int8 weight and its input is fed codes its "
"producer wrote %s\n", wn, rot8 ? " rotated" : " plain",
@@ -1148,6 +1163,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* codes, and (scale, zero) as `b_scale`. */
paro5 = known && linear_is_paro_w5(enc);
if (paro5) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
const int bits = enc.plane[0].dtype == RAD_U4 ? 4 : 5;
if (!a.x) {
fprintf(stderr, "radiance: '%s' is a rotated %d-bit weight, rotated from its input's "
@@ -1214,6 +1234,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* whole 128s, the scale group. */
w4 = known && linear_is_w4(enc);
if (w4) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (!a.x) {
fprintf(stderr, "radiance: '%s' is a 4-bit weight, read from its input's bf16 plane, "
"and its input has none\n", wn);
@@ -1284,6 +1309,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
paro = known && linear_is_paro_mxfp4(enc);
mx = known && (linear_is_mxfp4(enc) || paro);
if (mx) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (paro) {
/* The rotation is applied to the activation, so this linear needs the input's bf16
* plane (probe() set reads_x) and quantises it itself. */
@@ -1395,10 +1425,10 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
f32_scale = fp8_scale && fp8_scale->dtype == RAD_F32;
/* One stored weight, two operands: the codes and scale plane of `<base>.weight`, block fp8 in
* the checkpoint or made so by rad-convert's recipe. */
w = decl_view(b, wn, nullptr, "codes", RAD_F8E4M3, { N, K },
w = decl_view(b, wn, stored, "codes", RAD_F8E4M3, { N, K },
access, shard, grp, rp.w, rp.n);
ws_row_div = RAD_FP8_BLOCK;
ws = decl_view(b, sn, wn, "scale", f32_scale ? RAD_F32 : RAD_BF16,
ws = decl_view(b, sn, sw, "scale", f32_scale ? RAD_F32 : RAD_BF16,
{ fp8_blocks(N), fp8_blocks(K) }, access, shard, grp, rp.s, rp.n);
if (!w || !ws) return RAD_E_INVAL;
/* An uneven slice; the scale plane takes the same slice in blocks. */
+4 -3
View File
@@ -13,9 +13,10 @@
* row-interleaved all_gather, bit for bit the replicated projection
*
* The one rule for a run: ceil(T / world) rows, rounded up to whole 16-byte words of the gathered
* row, because an all_gather moves whole words. The indexer gathers one i32 length a row beside
* its page tables, so its runs are multiples of four rows. The last rank's run may be short, or
* lie wholly past a small step (`run_of`). */
* row, because an all_gather moves whole words. The MLA gathers 4096-wide bf16 rows, so its runs
* are ceil(T / world); the indexer gathers one i32 length a row beside its page tables, so its
* runs are multiples of four rows. The last rank's run may be short, or lie wholly past a small
* step (`run_of`). */
#ifndef RAD_GLM_TP_H
#define RAD_GLM_TP_H
#include "rad_arch.h"