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

+43 -5
View File
@@ -28,11 +28,27 @@
#include <arch/rad_block_glm_vision.h>
#include <arch/rad_block_mlp_fp8.h>
#include <arch/rad_block_moe_fp8.h>
#include <cstdlib>
#include <cstring>
#include <memory>
namespace glm5next {
using namespace rad::arch;
/* The deployment switch of this plugin, read once a declare; docs/GUIDE.md lists it.
* RADIANCE_GLM_MLA_ROWS unset or 0 keeps the MLA's head form at every step size and declares none
* of the row form's full-width weights (1.29 GiB a card at eight ranks); 1 is the row form from
* GlmTp::kSplitRows; 2 is the row form with o summed as the head form sums it, a test of the two
* forms' difference (GlmMla::Form). The row form is opt-in: it is not bit-identical to the head
* form, and this model turns the rounding difference of a fraction of the attention outputs into
* a KL of 0.015 to 0.03 against it (docs/ARCHITECTURES.md). */
static GlmMla::Form mla_form() {
const char* e = std::getenv("RADIANCE_GLM_MLA_ROWS");
if (e && !std::strcmp(e, "1")) return GlmMla::Form::rows;
if (e && !std::strcmp(e, "2")) return GlmMla::Form::rows_sliced_o;
return GlmMla::Form::heads;
}
struct Layer {
bool full = false, draft = false;
ManifoldConn attn_hc{}, ffn_hc{};
@@ -46,6 +62,7 @@ struct Layer {
struct Model {
Names nm{};
Geom g{};
GlmMla::Form form = GlmMla::Form::rows;
std::vector<Layer> layers;
rad_kvgroup latent = 0, index = 0, tail = 0, state = 0, conv = 0;
/* The streams and the step's stream-wide buffers. */
@@ -102,7 +119,9 @@ static int stream_buffers(RadBuilder* b, Model& m) {
m.h0 = decl_b(b, nm.f("manifold"), RAD_BF16, { T, 16384 });
m.h1 = decl_b(b, nm.f("manifold_mid"), RAD_BF16, { T, 16384 });
m.x = decl_b(b, nm.f("collapsed"), RAD_BF16, { T, 4096 });
m.y = decl_b(b, nm.f("attn_delta"), RAD_BF16, { T, 4096 });
/* Rounded up to whole runs: the MLA row form gathers every rank's run at full length. */
m.y = decl_b(b, nm.f("attn_delta"), RAD_BF16,
{ g.world * GlmMla::rows_for(T, g.world), 4096 });
m.post = decl_b(b, nm.f("post"), RAD_F32, { T, 4 });
m.comb = decl_b(b, nm.f("mix"), RAD_F32, { T, 16 });
m.act.x = decl_b(b, nm.f("normalized"), RAD_BF16, { T, 4096 });
@@ -132,9 +151,9 @@ static int kda_buffers(RadBuilder* b, Model& m) {
kw.core.out = bf("recurrent_out", W);
return RAD_OK;
}
/* The head form's buffers, then the selection planes the indexer writes and the MLA reads. Under
* TP the selection planes hold every rank's run of rows, which can reach past the step
* (DsaCache::part_rows). */
/* The head form's buffers, then the selection planes the indexer writes and the MLA reads, then
* the row form's (Model::form). Under TP the selection planes hold every rank's run of rows,
* which can reach past the step (DsaCache::part_rows). */
static int mla_buffers(RadBuilder* b, Model& m) {
auto& g = m.g;
auto& nm = m.nm;
@@ -160,6 +179,21 @@ static int mla_buffers(RadBuilder* b, Model& m) {
const int64_t TQ = g.world > 1 ? std::max(T, g.world * part) : T;
aw.core.selected = decl_b(b, nm.f("selected"), RAD_I32, { TQ, 513 });
aw.core.length = decl_b(b, nm.f("length"), RAD_I32, { TQ });
if (g.world > 1 && m.form != GlmMla::Form::heads) {
auto& rows = aw.rows;
const int64_t R = GlmMla::rows_for(T, g.world);
rows.q = decl_b(b, nm.f("rows_query"), RAD_BF16, { R, 16384 });
rows.q_latent = decl_b(b, nm.f("rows_q_latent"), RAD_BF16, { R, 64, 512 });
rows.attn_latent = decl_b(b, nm.f("rows_attn_latent"), RAD_BF16, { R, 64, 512 });
rows.attn.x = decl_b(b, nm.f("rows_expanded"), RAD_BF16, { R, 16384 });
RAD_ARCH_TRY(rows.attn.declare_qs(b, nm, g, "rows_expanded", 16384, R));
rows.out = decl_b(b, nm.f("rows_out"), RAD_BF16, { R, 4096 });
if (m.form == GlmMla::Form::rows_sliced_o) {
rows.slices = decl_b(b, nm.f("rows_o_slices"), RAD_BF16, { g.world * R, 4096 });
rows.acc = decl_b(b, nm.f("rows_o_sum"), RAD_F32, { R, 4096 });
rows.widened = decl_b(b, nm.f("rows_o_slice_f32"), RAD_F32, { R, 4096 });
}
}
return RAD_OK;
}
static int indexer_buffers(RadBuilder* b, Model& m) {
@@ -292,7 +326,10 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra
decl_w(b, nn, RAD_F32, { 4096 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
RAD_ARCH_TRY(lay.norm.declare(b, g, gain, input, m.act, 4096));
if (lay.full) {
RAD_ARCH_TRY(lay.mla.declare(b, nm, g, l, m.aw, ap, m.latent, true, true));
// The MTP layer keeps the head form: its row form would cost another 126 MB a card.
auto aw = m.aw;
if (draft) aw.rows = {};
RAD_ARCH_TRY(lay.mla.declare(b, nm, g, l, aw, ap, m.latent, true, true));
DsaCache::Config c;
c.blocks = (g.max_ctx + 3) / 4 + 1;
c.work = (g.max_tok + 3) / 4 + g.max_seqs;
@@ -398,6 +435,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
auto& g = m.g;
m.nm = Names(ctx->scope);
auto& nm = m.nm;
m.form = mla_form();
RAD_ARCH_TRY(geometry(g, meta, ctx, world));
RAD_ARCH_TRY(stream_buffers(b, m));
RAD_ARCH_TRY(kda_buffers(b, m));