Archived
glm5next: the plugin over its blocks, one a file, and rad_glm_tp.h for the shards and the split threshold -- the same graph, read in order
The plugin carried the draft head's buffers, weights and ops in its declare and its draft pass
in its step, with the three ops that join the head to its layer declared inside the layer
function; each GLM block carried its core in a header of its own (rad_block_mla_latent.h,
rad_block_kda_state.h, rad_block_dsa_cache.h), the split threshold sat in rad_tp_split.h beside
the router's column split, and 64 / world, 8192 / world, 2048 / world were spelled across the
plugin and the blocks. tests/arch_test.cpp held the recording builder, the run-phase fakes, the
step lint, the batch kits and the fingerprints beside every architecture's cases, 9.5k lines in
one translation unit, GLM-5.3's among them.
Now one block a file in arch/common: rad_block_glm_{mhc,kda,mla,indexer,router,mtp}.h, the
cores joined to their blocks, and GlmMtp the draft head in three parts (inputs, join, head),
declared and stepped around the plugin's layer 45. rad_glm_tp.h (GlmTp) holds the widths a
rank's shard divides, the one threshold kSplitRows and the replicated all_reduce declared once;
SplitF32Proj moves over unchanged. The plugin reads as the stream's buffers, then each block's,
the KV groups, the layers, the head and the draft head.
tests/arch_fakes.h holds the fakes and the helpers every architecture test needs, included by
one translation unit a binary (neither binary links rad_core, as before); GLM-5.3's cases are
tests/glm5next_test.cpp, which includes only its plugin, with two cases new: every op the plugin
issues carries the operand count its schema declares (the kernel libraries' schemas under
$RADIANCE_HOME/kernels, counted as core/runtime/issue.cpp counts), and every declared weight
reads an entry the served container holds, at the dtype its stored encoding calls for (the
container's names and encodings as rad-info -v lists them).
Every declared name, declaration order and issue order is as it was: the pinned graph and step
hashes at one and eight ranks are the values arch_test pinned before the move (world 1
0xed3fc26c486ea1b5 / 0x98ba2952bb37b075, world 8 0x8204e073efdeee0c / 0x1279aecfe4e32d9a), and
arch_test's pins of the other architectures are unchanged.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
d2a165d56d
commit
fa4a7a75a4
17 files changed
+3768
-3105
No files matched your search
@@ -1,150 +0,0 @@
|
||||
/* GLM's stateful compressed-index core. The caller supplies projected queries,
|
||||
* signed F32 head gates, and [LayerNorm(key), pool_gate] BF16 rows. No RoPE.
|
||||
* Query projections are replicated across TP ranks; selected pages belong to MLA. */
|
||||
#ifndef RAD_BLOCK_DSA_CACHE_H
|
||||
#define RAD_BLOCK_DSA_CACHE_H
|
||||
#include "rad_arch.h"
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct DsaCache {
|
||||
struct Config {
|
||||
int64_t blocks = 0, work = 0, ring = 0, topk = 512;
|
||||
};
|
||||
struct Wire {
|
||||
rad_buf query = 0, codes = 0, scales = 0, gate = 0, kg = 0;
|
||||
rad_buf stage = 0, page = 0, bpos = 0, nc = 0, pooled = 0, pool_scale = 0;
|
||||
rad_buf score = 0, selected = 0, nsel = 0, length = 0;
|
||||
};
|
||||
Geom g{};
|
||||
Config c{};
|
||||
Wire w{};
|
||||
int layer = 0;
|
||||
rad_kvgroup index = 0, tail = 0, attention = 0;
|
||||
rad_weight ape = 0;
|
||||
rad_op op_query = 0, op_work = 0, op_pool = 0, op_tail = 0, op_score = 0, op_select = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Config& cfg,
|
||||
const Wire& wire, rad_kvgroup attn, rad_kvgroup tail_group,
|
||||
rad_kvgroup* index_group, const char* ape_source) {
|
||||
g = geom;
|
||||
c = cfg;
|
||||
w = wire;
|
||||
layer = l;
|
||||
attention = attn;
|
||||
tail = tail_group;
|
||||
if (!b || !index_group || !ape_source || !attention || !tail || c.blocks < 1 ||
|
||||
c.work < (g.max_tok + 3) / 4 + g.max_seqs || c.ring < 4 + g.max_spec || c.topk != 512 ||
|
||||
!w.query || !w.codes || !w.scales || !w.gate || !w.kg || !w.stage || !w.page ||
|
||||
!w.bpos || !w.nc || !w.pooled || !w.pool_scale || !w.score || !w.selected || !w.nsel ||
|
||||
!w.length)
|
||||
return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, tail));
|
||||
const char* name = nm.f("blk.%d.index_ape", l);
|
||||
RAD_ARCH_TRY(map_copy(b, name, ape_source));
|
||||
ape = decl_w(b, name, RAD_F32, { 4, 128 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE,
|
||||
grp_layer(l));
|
||||
if (!ape) return RAD_E_INVAL;
|
||||
// Query buffers are [tokens*32,128]; scoring uses their rank-three view.
|
||||
op_query = rw(b,
|
||||
RAD_OP(b, "dsa_query",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * 32), RAD_INT("n", 128),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.query }, { w.codes, w.scales });
|
||||
op_work = rw(
|
||||
b,
|
||||
RAD_OP(b, "qsa_work",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 256), RAD_INT("ratio", 4),
|
||||
RAD_INT("ring", c.ring), RAD_INT("seqs", g.max_seqs),
|
||||
RAD_INT("work", c.work), RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.kg }, { w.stage, w.page, w.bpos, w.nc });
|
||||
// Declare immediately before the cache consumer: page size comes from that op.
|
||||
if (!*index_group) {
|
||||
RadKVGroupDecl d{};
|
||||
d.kind = RAD_KV_FULL;
|
||||
d.dtype = RAD_U8;
|
||||
d.n_head_kv = 1;
|
||||
d.head_dim = 17;
|
||||
*index_group = rad_decl_kv_group(b, nm.f("kv_dsa_index"), &d);
|
||||
}
|
||||
index = *index_group;
|
||||
if (!index) return RAD_E_INVAL;
|
||||
op_pool =
|
||||
rw(b,
|
||||
RAD_OP(b, "dsa_pool",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, c.work), RAD_INT("n", 128), RAD_INT("ratio", 4),
|
||||
RAD_INT("block_size", 4), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(ape)),
|
||||
{ w.stage, w.page }, { w.pooled, w.pool_scale });
|
||||
if (!op_pool || rad_kv_block_size(b, index) != 4) return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, index));
|
||||
op_tail = rw(b,
|
||||
RAD_OP(b, "qsa_tail_store",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 256),
|
||||
RAD_INT("ratio", 4), RAD_INT("ring", c.ring),
|
||||
RAD_INT("seqs", g.max_seqs), RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.kg }, {});
|
||||
op_score = rw(b,
|
||||
RAD_OP(b, "dsa_score",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 128),
|
||||
RAD_INT("heads", 32), RAD_INT("blocks", c.blocks),
|
||||
RAD_INT("seqs", g.max_seqs), RAD_STR("dtype", "fp8_e4m3")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.codes, w.scales, w.gate, w.nc }, { w.score });
|
||||
op_select = rw(b,
|
||||
RAD_OP(b, "qsa_select",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("blocks", c.blocks),
|
||||
RAD_INT("topk", 512), RAD_INT("ratio", 4),
|
||||
RAD_INT("seqs", g.max_seqs), RAD_INT("signed", 1)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.score, w.nc }, { w.selected, w.nsel, w.length });
|
||||
return op_query && op_work && op_tail && op_score && op_select ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
void step(RadCtx* ctx, const RadBatch* batch) const {
|
||||
const auto* ib = kv_batch(batch, index);
|
||||
const auto* tb = kv_batch(batch, tail);
|
||||
const auto* ab = kv_batch(batch, attention);
|
||||
if (!ib || !tb || !ab || !ib->block_table || !ab->block_table || !tb->state_index) {
|
||||
rad_step_fail(ctx, "DSA requires index/attention block tables and tail state indices");
|
||||
return;
|
||||
}
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq;
|
||||
const int64_t work = (T + 3) / 4 + S,
|
||||
pitch = tb->state_index_pitch > 0 ? tb->state_index_pitch : 1;
|
||||
RAD_ISSUE_N(ctx, op_query, T * 32, brows(w.query, T * 32), brows(w.codes, T * 32),
|
||||
brows(w.scales, T * 32));
|
||||
RAD_ISSUE_N(ctx, op_work, T, brows(w.kg, T), praw(batch->positions, RAD_I32, T),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1), kv_cache(tail, layer),
|
||||
praw2(tb->state_index, RAD_I32, S, pitch),
|
||||
praw2(ib->block_table, RAD_I32, S, ib->block_table_pitch), RAD_NONE,
|
||||
brows(w.stage, work * 4), brows(w.page, work), brows(w.bpos, work),
|
||||
brows(w.nc, S));
|
||||
RAD_ISSUE_N(ctx, op_pool, work, brows(w.stage, work * 4), RAD_W(ape), brows(w.pooled, work),
|
||||
brows(w.pool_scale, work), brows(w.page, work), kv_cache(index, layer));
|
||||
// Gather before overwriting the tail; the wider ring preserves rejected-token rollback.
|
||||
RAD_ISSUE_N(ctx, op_tail, T, brows(w.kg, T), praw(batch->positions, RAD_I32, T),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1), kv_cache(tail, layer),
|
||||
praw2(tb->state_index, RAD_I32, S, pitch));
|
||||
int64_t live = ((int64_t)batch->max_ctx_len + T + batch->n_spec + 4) / 4 + 1;
|
||||
if (live > c.blocks) live = c.blocks;
|
||||
// T is the total across sequences, so the conservative bound can exceed the
|
||||
// per-sequence page table. Columns without a table entry cannot be scored.
|
||||
if (live > ib->block_table_pitch) live = ib->block_table_pitch;
|
||||
if (live < 1) live = 1;
|
||||
RAD_ISSUE_N(ctx, op_score, T, brows(w.codes, T * 32), brows(w.scales, T * 32),
|
||||
brows(w.gate, T), kv_cache(index, layer), RAD_NONE,
|
||||
praw2(ib->block_table, RAD_I32, S, ib->block_table_pitch),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1), brows(w.nc, S),
|
||||
bcol_at(w.score, 0, c.blocks, 0, live, T));
|
||||
RAD_ISSUE_N(ctx, op_select, T, bcol_at(w.score, 0, c.blocks, 0, live, T),
|
||||
praw2(ab->block_table, RAD_I32, S, ab->block_table_pitch), brows(w.nc, S),
|
||||
praw(batch->positions, RAD_I32, T), praw(batch->cu_seqlens, RAD_I32, S + 1),
|
||||
brows(w.selected, T), brows(w.nsel, T), brows(w.length, T));
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace rad::arch
|
||||
#endif
|
||||
@@ -1,13 +1,159 @@
|
||||
/* Replicated GLM indexer front end. Casts bridge ordinary linear rows to the
|
||||
* flattened per-head quantizer and join normalized keys with learned pool gates. */
|
||||
/* GLM-5.3's DSA indexer: the stateful compressed-index core over its paged key cache (DsaCache),
|
||||
* which scores every complete block of a sequence's history against each query and selects the
|
||||
* 512 the MLA attends to, and the front end around it (GlmIndexer), whose casts bridge ordinary
|
||||
* linear rows to the flattened per-head quantiser and join normalized keys with learned pool
|
||||
* gates. No RoPE; the only rotation is the Hadamard inside dsa_query. Selected pages belong to
|
||||
* the MLA.
|
||||
*
|
||||
* Under tensor parallelism the indexer is replicated whole: every rank pools into the whole index
|
||||
* cache, scores every query row against it and selects for every row. Only the head gates' f32
|
||||
* projection splits its columns from GlmTp::kSplitRows (SplitF32Proj). */
|
||||
#ifndef RAD_BLOCK_GLM_INDEXER_H
|
||||
#define RAD_BLOCK_GLM_INDEXER_H
|
||||
#include "rad_fp8.h"
|
||||
#include "rad_block_dsa_cache.h"
|
||||
#include "rad_tp_split.h"
|
||||
#include "rad_glm_tp.h"
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct DsaCache {
|
||||
struct Config {
|
||||
int64_t blocks = 0, work = 0, ring = 0, topk = 512;
|
||||
};
|
||||
struct Wire {
|
||||
rad_buf query = 0, codes = 0, scales = 0, gate = 0, kg = 0;
|
||||
rad_buf stage = 0, page = 0, bpos = 0, nc = 0, pooled = 0, pool_scale = 0;
|
||||
rad_buf score = 0, selected = 0, nsel = 0, length = 0;
|
||||
};
|
||||
Geom g{};
|
||||
Config c{};
|
||||
Wire w{};
|
||||
int layer = 0;
|
||||
rad_kvgroup index = 0, tail = 0, attention = 0;
|
||||
rad_weight ape = 0;
|
||||
rad_op op_query = 0, op_work = 0, op_pool = 0, op_tail = 0, op_score = 0, op_select = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Config& cfg,
|
||||
const Wire& wire, rad_kvgroup attn, rad_kvgroup tail_group,
|
||||
rad_kvgroup* index_group, const char* ape_source) {
|
||||
g = geom;
|
||||
c = cfg;
|
||||
w = wire;
|
||||
layer = l;
|
||||
attention = attn;
|
||||
tail = tail_group;
|
||||
if (!b || !index_group || !ape_source || !attention || !tail || c.blocks < 1 ||
|
||||
c.work < (g.max_tok + 3) / 4 + g.max_seqs || c.ring < 4 + g.max_spec || c.topk != 512 ||
|
||||
!w.query || !w.codes || !w.scales || !w.gate || !w.kg || !w.stage || !w.page ||
|
||||
!w.bpos || !w.nc || !w.pooled || !w.pool_scale || !w.score || !w.selected || !w.nsel ||
|
||||
!w.length)
|
||||
return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, tail));
|
||||
const char* name = nm.f("blk.%d.index_ape", l);
|
||||
RAD_ARCH_TRY(map_copy(b, name, ape_source));
|
||||
ape = decl_w(b, name, RAD_F32, { 4, 128 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE,
|
||||
grp_layer(l));
|
||||
if (!ape) return RAD_E_INVAL;
|
||||
// Query buffers are [tokens*32,128]; scoring uses their rank-three view.
|
||||
op_query = rw(b,
|
||||
RAD_OP(b, "dsa_query",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * 32), RAD_INT("n", 128),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.query }, { w.codes, w.scales });
|
||||
op_work = rw(
|
||||
b,
|
||||
RAD_OP(b, "qsa_work",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 256), RAD_INT("ratio", 4),
|
||||
RAD_INT("ring", c.ring), RAD_INT("seqs", g.max_seqs),
|
||||
RAD_INT("work", c.work), RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.kg }, { w.stage, w.page, w.bpos, w.nc });
|
||||
// Declare immediately before the cache consumer: page size comes from that op.
|
||||
if (!*index_group) {
|
||||
RadKVGroupDecl d{};
|
||||
d.kind = RAD_KV_FULL;
|
||||
d.dtype = RAD_U8;
|
||||
d.n_head_kv = 1;
|
||||
d.head_dim = 17;
|
||||
*index_group = rad_decl_kv_group(b, nm.f("kv_dsa_index"), &d);
|
||||
}
|
||||
index = *index_group;
|
||||
if (!index) return RAD_E_INVAL;
|
||||
op_pool =
|
||||
rw(b,
|
||||
RAD_OP(b, "dsa_pool",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, c.work), RAD_INT("n", 128), RAD_INT("ratio", 4),
|
||||
RAD_INT("block_size", 4), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(ape)),
|
||||
{ w.stage, w.page }, { w.pooled, w.pool_scale });
|
||||
if (!op_pool || rad_kv_block_size(b, index) != 4) return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, index));
|
||||
op_tail = rw(b,
|
||||
RAD_OP(b, "qsa_tail_store",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 256),
|
||||
RAD_INT("ratio", 4), RAD_INT("ring", c.ring),
|
||||
RAD_INT("seqs", g.max_seqs), RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.kg }, {});
|
||||
op_score = rw(b,
|
||||
RAD_OP(b, "dsa_score",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 128),
|
||||
RAD_INT("heads", 32), RAD_INT("blocks", c.blocks),
|
||||
RAD_INT("seqs", g.max_seqs), RAD_STR("dtype", "fp8_e4m3")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.codes, w.scales, w.gate, w.nc }, { w.score });
|
||||
op_select = rw(b,
|
||||
RAD_OP(b, "qsa_select",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("blocks", c.blocks),
|
||||
RAD_INT("topk", 512), RAD_INT("ratio", 4),
|
||||
RAD_INT("seqs", g.max_seqs), RAD_INT("signed", 1)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.score, w.nc }, { w.selected, w.nsel, w.length });
|
||||
return op_query && op_work && op_tail && op_score && op_select ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
void step(RadCtx* ctx, const RadBatch* batch) const {
|
||||
const auto* ib = kv_batch(batch, index);
|
||||
const auto* tb = kv_batch(batch, tail);
|
||||
const auto* ab = kv_batch(batch, attention);
|
||||
if (!ib || !tb || !ab || !ib->block_table || !ab->block_table || !tb->state_index) {
|
||||
rad_step_fail(ctx, "DSA requires index/attention block tables and tail state indices");
|
||||
return;
|
||||
}
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq;
|
||||
const int64_t work = (T + 3) / 4 + S,
|
||||
pitch = tb->state_index_pitch > 0 ? tb->state_index_pitch : 1;
|
||||
RAD_ISSUE_N(ctx, op_query, T * 32, brows(w.query, T * 32), brows(w.codes, T * 32),
|
||||
brows(w.scales, T * 32));
|
||||
RAD_ISSUE_N(ctx, op_work, T, brows(w.kg, T), praw(batch->positions, RAD_I32, T),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1), kv_cache(tail, layer),
|
||||
praw2(tb->state_index, RAD_I32, S, pitch),
|
||||
praw2(ib->block_table, RAD_I32, S, ib->block_table_pitch), RAD_NONE,
|
||||
brows(w.stage, work * 4), brows(w.page, work), brows(w.bpos, work),
|
||||
brows(w.nc, S));
|
||||
RAD_ISSUE_N(ctx, op_pool, work, brows(w.stage, work * 4), RAD_W(ape), brows(w.pooled, work),
|
||||
brows(w.pool_scale, work), brows(w.page, work), kv_cache(index, layer));
|
||||
// Gather before overwriting the tail; the wider ring preserves rejected-token rollback.
|
||||
RAD_ISSUE_N(ctx, op_tail, T, brows(w.kg, T), praw(batch->positions, RAD_I32, T),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1), kv_cache(tail, layer),
|
||||
praw2(tb->state_index, RAD_I32, S, pitch));
|
||||
int64_t live = ((int64_t)batch->max_ctx_len + T + batch->n_spec + 4) / 4 + 1;
|
||||
if (live > c.blocks) live = c.blocks;
|
||||
// T is the total across sequences, so the conservative bound can exceed the
|
||||
// per-sequence page table. Columns without a table entry cannot be scored.
|
||||
if (live > ib->block_table_pitch) live = ib->block_table_pitch;
|
||||
if (live < 1) live = 1;
|
||||
RAD_ISSUE_N(ctx, op_score, T, brows(w.codes, T * 32), brows(w.scales, T * 32),
|
||||
brows(w.gate, T), kv_cache(index, layer), RAD_NONE,
|
||||
praw2(ib->block_table, RAD_I32, S, ib->block_table_pitch),
|
||||
praw(batch->cu_seqlens, RAD_I32, S + 1), brows(w.nc, S),
|
||||
bcol_at(w.score, 0, c.blocks, 0, live, T));
|
||||
RAD_ISSUE_N(ctx, op_select, T, bcol_at(w.score, 0, c.blocks, 0, live, T),
|
||||
praw2(ab->block_table, RAD_I32, S, ab->block_table_pitch), brows(w.nc, S),
|
||||
praw(batch->positions, RAD_I32, T), praw(batch->cu_seqlens, RAD_I32, S + 1),
|
||||
brows(w.selected, T), brows(w.nsel, T), brows(w.length, T));
|
||||
}
|
||||
};
|
||||
|
||||
struct GlmIndexer {
|
||||
struct Wire {
|
||||
rad_buf input = 0, q_low = 0, q_wide = 0, kg_raw = 0;
|
||||
|
||||
+132
-16
@@ -1,13 +1,137 @@
|
||||
/* GLM KDA projections around the recurrent/cache core. These checkpoint weights
|
||||
* are BF16. The two low-rank gate paths have no intervening activation. The
|
||||
* caller supplies normalized input and owns the mHC residual connection. */
|
||||
/* GLM-5.3's KDA layer: the seven bf16 projections around the recurrent core (GlmKda), and the
|
||||
* core itself over already projected inputs (KdaState), whose ple_conv supplies exactly causal
|
||||
* depthwise convolution + SiLU, cold start and accepted-history rollback included. The two
|
||||
* low-rank gate paths have no intervening activation. The caller supplies the normalized input
|
||||
* and owns the mHC residual connection.
|
||||
*
|
||||
* Under tensor parallelism the heads split (GlmTp::heads), and with them the q/k/v rows, the
|
||||
* convolution taps, the state and the output projection's columns; the two rank-128 projections
|
||||
* into the gates are replicated. The block's all_reduce sums the output. */
|
||||
#ifndef RAD_BLOCK_GLM_KDA_H
|
||||
#define RAD_BLOCK_GLM_KDA_H
|
||||
#include "rad_fp8.h"
|
||||
#include "rad_block_kda_state.h"
|
||||
#include "rad_glm_tp.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct KdaState {
|
||||
struct Config {
|
||||
int64_t heads = 16, dim = 128, width = 4;
|
||||
double lower = -5;
|
||||
};
|
||||
struct Src {
|
||||
const char *q_conv = nullptr, *k_conv = nullptr, *v_conv = nullptr, *alog = nullptr,
|
||||
*bias = nullptr, *norm = nullptr;
|
||||
};
|
||||
struct Wire {
|
||||
rad_buf qkv = 0, conv = 0, a = 0, beta = 0, gate = 0, out = 0;
|
||||
};
|
||||
Geom g{};
|
||||
Config c{};
|
||||
Wire w{};
|
||||
int layer = 0;
|
||||
rad_kvgroup state = 0, history = 0;
|
||||
rad_weight taps = 0, alog = 0, bias = 0, norm = 0;
|
||||
rad_op op_conv = 0, op_verify = 0, op_prefill = 0, op_norm = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, rad_kvgroup st, rad_kvgroup cv,
|
||||
const Config& cfg, const Wire& wire, const Src& src) {
|
||||
g = geom;
|
||||
c = cfg;
|
||||
w = wire;
|
||||
layer = l;
|
||||
state = st;
|
||||
history = cv;
|
||||
if (!b || !st || !cv || c.heads < 1 || c.dim != 128 || c.width != 4 || g.max_spec > 31 ||
|
||||
!std::isfinite(c.lower) || c.lower >= 0 || !w.qkv || !w.conv || !w.a || !w.beta ||
|
||||
!w.gate || !w.out || w.qkv == w.conv || !src.q_conv || !src.k_conv || !src.v_conv ||
|
||||
!src.alog || !src.bias || !src.norm)
|
||||
return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, st));
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, cv));
|
||||
const int64_t width = c.heads * c.dim;
|
||||
const char *tn = nm.f("blk.%d.ssm_conv.weight", l), *an = nm.f("blk.%d.ssm_a", l),
|
||||
*bn = nm.f("blk.%d.ssm_dt_bias", l), *nn = nm.f("blk.%d.ssm_norm.weight", l);
|
||||
RAD_ARCH_TRY(map_concat(b, tn, { src.q_conv, src.k_conv, src.v_conv }));
|
||||
RAD_ARCH_TRY(map_copy(b, an, src.alog));
|
||||
RAD_ARCH_TRY(map_copy(b, bn, src.bias));
|
||||
RAD_ARCH_TRY(map_copy(b, nn, src.norm));
|
||||
taps = decl_w(b, tn, RAD_BF16, { 3 * width, c.width }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW,
|
||||
grp_layer(l), 0, { width, width, width });
|
||||
alog =
|
||||
decl_w(b, an, RAD_F32, { c.heads }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
|
||||
bias = decl_w(b, bn, RAD_F32, { width }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
|
||||
norm =
|
||||
decl_w(b, nn, RAD_F32, { c.dim }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
if (!taps || !alog || !bias || !norm) return RAD_E_INVAL;
|
||||
op_conv = rw(b,
|
||||
RAD_OP(b, "ple_conv",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 3 * width),
|
||||
RAD_INT("width", c.width), RAD_INT("dilation", 1),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(taps)),
|
||||
{ w.qkv }, { w.conv });
|
||||
for (int verify = 0; verify < 2; ++verify) {
|
||||
const rad_op op = rw(
|
||||
b,
|
||||
RAD_OP(b, "kda_update",
|
||||
RAD_PARAMS(
|
||||
RAD_RANGE("M", 1, g.max_tok), RAD_INT("heads", c.heads),
|
||||
RAD_INT("head_k", c.dim), RAD_INT("head_v", c.dim),
|
||||
RAD_F64("lower_bound", c.lower),
|
||||
RAD_F64("scale", 1.0 / std::sqrt((double)c.dim)),
|
||||
RAD_F64("l2_eps", 1e-6), RAD_STR("mode", verify ? "verify" : "prefill"),
|
||||
RAD_STR("dtype", "bf16"),
|
||||
RAD_STR("state_form", g.max_spec > 0 ? "anchor" : "per_candidate")),
|
||||
RAD_WEIGHTS(alog, bias)),
|
||||
{ w.conv, w.a, w.beta }, { w.out });
|
||||
if (verify)
|
||||
op_verify = op;
|
||||
else
|
||||
op_prefill = op;
|
||||
}
|
||||
op_norm = rw(b,
|
||||
RAD_OP(b, "gdn_gated_rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("channels", c.dim),
|
||||
RAD_F64("eps", g.eps), RAD_STR("act", "sigmoid")),
|
||||
RAD_WEIGHTS(norm)),
|
||||
{ w.out, w.gate }, { w.out });
|
||||
return op_conv && op_verify && op_prefill && op_norm ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
void step(RadCtx* ctx, const RadBatch* batch) const {
|
||||
const auto *st = kv_batch(batch, state), *cv = kv_batch(batch, history);
|
||||
if (!st || !cv || !st->state_index || !cv->state_index) {
|
||||
rad_step_fail(ctx, "KDA requires recurrent and convolution state indices");
|
||||
return;
|
||||
}
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq, width = c.heads * c.dim;
|
||||
const int64_t pitch = st->state_index_pitch > 0 ? st->state_index_pitch : 1;
|
||||
int64_t D = 0, DT = 0;
|
||||
batch_split(batch, &D, &DT);
|
||||
for (int verify = 1; verify >= 0; --verify) {
|
||||
const int64_t seq0 = verify ? 0 : D, seqs = verify ? D : S - D, rows = verify ? DT : T;
|
||||
if (!seqs) continue;
|
||||
const RadOperand cu = praw(batch->cu_seqlens + seq0, RAD_I32, seqs + 1);
|
||||
const RadOperand init = praw(batch->ctx_lens + seq0, RAD_I32, seqs);
|
||||
const RadOperand accepted =
|
||||
verify ? praw(batch->num_accepted, RAD_I32, seqs) : RAD_NONE;
|
||||
RAD_ISSUE_N(ctx, op_conv, rows, brows(w.qkv, rows), RAD_W(taps),
|
||||
kv_cache(history, layer), cu, RAD_NONE,
|
||||
praw(cv->state_index + seq0, RAD_I32, seqs), init, accepted,
|
||||
brows(w.conv, rows));
|
||||
RAD_ISSUE_N(ctx, verify ? op_verify : op_prefill, rows, bcol(w.conv, 0, width, rows),
|
||||
bcol(w.conv, width, width, rows), bcol(w.conv, 2 * width, width, rows),
|
||||
brows(w.a, rows), brows(w.beta, rows), RAD_W(alog), RAD_W(bias),
|
||||
kv_cache(state, layer),
|
||||
praw2(st->state_index + seq0 * pitch, RAD_I32, seqs, pitch), cu, accepted,
|
||||
init, brows(w.out, rows));
|
||||
}
|
||||
RAD_ISSUE_N(ctx, op_norm, T, brows(w.out, T), brows(w.gate, T), RAD_W(norm),
|
||||
brows(w.out, T));
|
||||
}
|
||||
};
|
||||
|
||||
struct GlmKda {
|
||||
struct Wire {
|
||||
rad_buf input = 0, forget_low = 0, gate_low = 0, out = 0;
|
||||
@@ -23,14 +147,14 @@ struct GlmKda {
|
||||
rad_kvgroup history, bool reduce = true) {
|
||||
g = geom;
|
||||
w = wire;
|
||||
if (!b || !prefix || g.n_embd != 4096 || g.world < 1 || 64 % g.world || !w.input ||
|
||||
!w.forget_low || !w.gate_low || !w.out || w.input == w.out ||
|
||||
if (!b || !prefix || g.n_embd != 4096 || g.world < 1 || GlmTp::kHeads % g.world ||
|
||||
!w.input || !w.forget_low || !w.gate_low || !w.out || w.input == w.out ||
|
||||
w.forget_low == w.gate_low)
|
||||
return RAD_E_INVAL;
|
||||
// All seven projections are plain BF16 in the pinned checkpoint.
|
||||
Geom bg = g;
|
||||
bg.w_bf16 = true;
|
||||
const int64_t width = 8192 / g.world, heads = 64 / g.world;
|
||||
const int64_t width = GlmTp::kda_width(g.world), heads = GlmTp::heads(g.world);
|
||||
const auto group = grp_layer(layer);
|
||||
auto act = [](rad_buf x) {
|
||||
ActFP8 a{};
|
||||
@@ -67,15 +191,7 @@ struct GlmKda {
|
||||
if (!qkv.bf16 || !beta.bf16 || !forget_a.bf16 || !forget_b.bf16 || !gate_a.bf16 ||
|
||||
!gate_b.bf16 || !output.bf16)
|
||||
return RAD_E_UNSUPPORTED;
|
||||
if (reduce && g.world > 1)
|
||||
op_ar = rw(b,
|
||||
RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_RANGE("numel", 4096, g.max_tok * 4096),
|
||||
RAD_STR("dtype", "bf16"), RAD_INT("exact", g.wire_exact),
|
||||
RAD_INT("min_bytes", g.wire_min_bytes)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.out }, { w.out });
|
||||
if (reduce) op_ar = GlmTp::reduce(b, g, w.out);
|
||||
return reduce && g.world > 1 && !op_ar ? RAD_E_INVAL : RAD_OK;
|
||||
}
|
||||
void step(RadCtx* ctx, const RadBatch* batch) const {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
/* GLM manifold-constrained residual connections. The mixed residual must have a
|
||||
* separate destination: each output stream reads every original input stream. */
|
||||
#ifndef RAD_BLOCK_MHC_H
|
||||
#define RAD_BLOCK_MHC_H
|
||||
#include "rad_tp_split.h"
|
||||
/* GLM-5.3's manifold-constrained residual connections (mHC): four residual streams in one
|
||||
* 16384-wide row. read() mixes them down to the block's 4096-wide input and saves the mixing
|
||||
* coefficients; write() adds the block's output back into every stream. The mixed residual must
|
||||
* have a separate destination: each output stream reads every original input stream. */
|
||||
#ifndef RAD_BLOCK_GLM_MHC_H
|
||||
#define RAD_BLOCK_GLM_MHC_H
|
||||
#include "rad_glm_tp.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace rad {
|
||||
@@ -27,7 +29,7 @@ struct ManifoldConn {
|
||||
rad_op op_pre = 0, op_post = 0;
|
||||
/* Above the split threshold under TP, GLM's projection is split by residual chunks: each rank
|
||||
* computes the partials of its chunks with mhc_pre's own kernels, an all_gather joins them,
|
||||
* and mhc_pre_finish completes the read on every rank (rad_tp_split.h). */
|
||||
* and mhc_pre_finish completes the read on every rank (rad_glm_tp.h). */
|
||||
static constexpr int64_t kChunk = 2048, kPartStride = 28;
|
||||
rad_buf part = 0, parts = 0;
|
||||
rad_op op_partial = 0, op_gather = 0, op_finish = 0;
|
||||
@@ -72,7 +74,7 @@ struct ManifoldConn {
|
||||
}
|
||||
int declare_split(RadBuilder* b, Names& nm, const char* name) {
|
||||
const int64_t total = c.hc * g.n_embd / kChunk;
|
||||
split_rows = kGlmTpSplitRows;
|
||||
split_rows = GlmTp::kSplitRows;
|
||||
if (g.world < 2 || g.n_embd != 4096 || c.hc != 4 || total % g.world) {
|
||||
split_rows = 0;
|
||||
return RAD_OK;
|
||||
@@ -1,12 +1,92 @@
|
||||
/* No-RoPE GLM MLA. prepare() exposes normalized Q-lora for the replicated
|
||||
* indexer; finish() consumes its selection. The caller owns the mHC residual. */
|
||||
/* GLM-5.3's sparse latent attention (MLA, no RoPE): the projections and the latent cache store
|
||||
* around the absorbed core (GlmMla), and the core itself over the latent cache (MlaLatent), whose
|
||||
* 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. */
|
||||
#ifndef RAD_BLOCK_GLM_MLA_H
|
||||
#define RAD_BLOCK_GLM_MLA_H
|
||||
#include "rad_fp8.h"
|
||||
#include "rad_block_mla_latent.h"
|
||||
#include "rad_glm_tp.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct MlaLatent {
|
||||
struct Config {
|
||||
int64_t heads = 16, head_dim = 256, latent_dim = 512, blocks = 513;
|
||||
};
|
||||
struct Wire {
|
||||
rad_buf q = 0, q_latent = 0, attn_latent = 0, out = 0, selected = 0, length = 0;
|
||||
};
|
||||
Geom g{};
|
||||
Config c{};
|
||||
Wire w{};
|
||||
int layer = 0;
|
||||
rad_kvgroup kv = 0;
|
||||
rad_weight projection = 0;
|
||||
rad_op op_absorb = 0, op_attn = 0, op_expand = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, rad_kvgroup group,
|
||||
const Config& cfg, const Wire& wire, const char* source) {
|
||||
g = geom;
|
||||
c = cfg;
|
||||
w = wire;
|
||||
layer = l;
|
||||
kv = group;
|
||||
if (!b || !source || !group || c.heads < 1 || c.blocks < 1 || !w.q || !w.q_latent ||
|
||||
!w.attn_latent || !w.out || !w.selected || !w.length || w.q == w.q_latent ||
|
||||
w.q_latent == w.attn_latent || w.attn_latent == w.out || c.latent_dim != 512 ||
|
||||
c.head_dim != 256)
|
||||
return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, group));
|
||||
const char* name = nm.f("blk.%d.attn_kv_b.weight", l);
|
||||
RAD_ARCH_TRY(map_copy(b, name, source));
|
||||
// The conversion recipe decodes this small projection only.
|
||||
projection = decl_w(b, name, RAD_BF16, { c.heads * 2 * c.head_dim, c.latent_dim },
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
|
||||
if (!projection) return RAD_E_INVAL;
|
||||
op_absorb = rw(
|
||||
b,
|
||||
RAD_OP(b, "mla_project",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("heads", c.heads),
|
||||
RAD_INT("head_dim", c.head_dim), RAD_INT("latent_dim", c.latent_dim),
|
||||
RAD_STR("mode", "absorb"), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(projection)),
|
||||
{ w.q }, { w.q_latent });
|
||||
op_attn =
|
||||
rw(b,
|
||||
RAD_OP(b, "mla_attn",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("latent_dim", c.latent_dim),
|
||||
RAD_INT("heads", c.heads), RAD_INT("blocks", c.blocks),
|
||||
RAD_INT("block_size", 4),
|
||||
RAD_F64("scale", 1.0 / std::sqrt((double)c.head_dim)),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.q_latent, w.selected, w.length }, { w.attn_latent });
|
||||
if (!op_attn || rad_kv_block_size(b, group) != 4) return RAD_E_INVAL;
|
||||
op_expand = rw(
|
||||
b,
|
||||
RAD_OP(b, "mla_project",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("heads", c.heads),
|
||||
RAD_INT("head_dim", c.head_dim), RAD_INT("latent_dim", c.latent_dim),
|
||||
RAD_STR("mode", "expand"), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(projection)),
|
||||
{ w.attn_latent }, { w.out });
|
||||
return op_absorb && op_attn && op_expand ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
void step(RadCtx* ctx, int64_t rows) const {
|
||||
RAD_ISSUE_N(ctx, op_absorb, rows, brows(w.q, rows), RAD_W(projection),
|
||||
brows(w.q_latent, rows));
|
||||
RAD_ISSUE_N(ctx, op_attn, rows, brows(w.q_latent, rows), kv_cache(kv, layer),
|
||||
brows(w.selected, rows), brows(w.length, rows), brows(w.attn_latent, rows));
|
||||
RAD_ISSUE_N(ctx, op_expand, rows, brows(w.attn_latent, rows), RAD_W(projection),
|
||||
brows(w.out, rows));
|
||||
}
|
||||
};
|
||||
|
||||
struct GlmMla {
|
||||
struct Wire {
|
||||
ActFP8 input{}, q_low{}, attn{};
|
||||
@@ -30,8 +110,8 @@ struct GlmMla {
|
||||
w = wire;
|
||||
layer = l;
|
||||
kv = group;
|
||||
if (!b || !prefix || !kv || g.n_embd != 4096 || g.world < 1 || 64 % g.world || !w.input.x ||
|
||||
!w.q_raw || !w.kv_raw || !w.latent || !w.out || !w.q_low.x || !w.attn.x ||
|
||||
if (!b || !prefix || !kv || g.n_embd != 4096 || g.world < 1 || GlmTp::kHeads % g.world ||
|
||||
!w.input.x || !w.q_raw || !w.kv_raw || !w.latent || !w.out || !w.q_low.x || !w.attn.x ||
|
||||
w.attn.x != w.core.out)
|
||||
return RAD_E_INVAL;
|
||||
const auto wg = grp_layer(l);
|
||||
@@ -56,8 +136,8 @@ struct GlmMla {
|
||||
RAD_WEIGHTS(kv_gain)),
|
||||
{ w.kv_raw }, { w.latent });
|
||||
if (!op_kv_norm) return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(q_b.declare(b, nm, g, nm.f("blk.%d.attn_q_b", l), 16384 / g.world, 1536,
|
||||
RAD_SHARD_ROW, wg, { nm.ckpt("%s.q_b_proj.weight", prefix) },
|
||||
RAD_ARCH_TRY(q_b.declare(b, nm, g, nm.f("blk.%d.attn_q_b", l), GlmTp::mla_width(g.world),
|
||||
1536, RAD_SHARD_ROW, wg, { nm.ckpt("%s.q_b_proj.weight", prefix) },
|
||||
w.q_low, w.core.q));
|
||||
// Cache stores contiguous halves of the normalized latent, with no duplicated K/V.
|
||||
op_store = rw(b,
|
||||
@@ -72,22 +152,14 @@ struct GlmMla {
|
||||
}
|
||||
int declare_finish(RadBuilder* b, Names& nm, const char* prefix, bool reduce = true) {
|
||||
MlaLatent::Config cfg;
|
||||
cfg.heads = 64 / g.world;
|
||||
cfg.heads = GlmTp::heads(g.world);
|
||||
RAD_ARCH_TRY(
|
||||
core.declare(b, nm, g, layer, kv, cfg, w.core, nm.ckpt("%s.kv_b_proj.weight", prefix)));
|
||||
RAD_ARCH_TRY(attn_quant.declare(b, g, w.attn, 16384 / g.world));
|
||||
RAD_ARCH_TRY(attn_quant.declare(b, g, w.attn, GlmTp::mla_width(g.world)));
|
||||
RAD_ARCH_TRY(output.declare(b, nm, g, nm.f("blk.%d.attn_output", layer), 4096,
|
||||
16384 / g.world, RAD_SHARD_COL, grp_layer(layer),
|
||||
GlmTp::mla_width(g.world), RAD_SHARD_COL, grp_layer(layer),
|
||||
{ nm.ckpt("%s.o_proj.weight", prefix) }, w.attn, w.out));
|
||||
if (reduce && g.world > 1)
|
||||
op_ar = rw(b,
|
||||
RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_RANGE("numel", 4096, g.max_tok * 4096),
|
||||
RAD_STR("dtype", "bf16"), RAD_INT("exact", g.wire_exact),
|
||||
RAD_INT("min_bytes", g.wire_min_bytes)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.out }, { 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;
|
||||
}
|
||||
void prepare(RadCtx* ctx, const RadBatch* batch) const {
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
/* GLM-5.3's draft head: checkpoint layer 45 as a serial drafter (RAD_DRAFT_SERIAL), around the
|
||||
* one MLA + indexer + MoE layer the plugin declares and steps itself.
|
||||
*
|
||||
* h = gather(the trunk's final hidden states, at the rows this pass continues from)
|
||||
* e = embed(the token one position on)
|
||||
* x = eh_proj( concat( enorm(e), hnorm(h) ) ) the normalized embedding first
|
||||
* x += attention(norm(x)); the proposal rows: x += moe(norm(x)) <- the plugin's layer
|
||||
* h' = shared_head.norm(x); logits = the trunk's lm_head over h'
|
||||
*
|
||||
* h' is both the logits input and the next round's `h`. No mHC: the layer's residual is a plain
|
||||
* add. By the sign of RadBatch::draft_pass a history pass (< 0) covers every row the trunk step
|
||||
* had, fills the draft layer's latent, index and tail for all of them and runs the MoE and the
|
||||
* head on the proposal rows only (draft_out_ids); a draft round (> 1) is one row a sequence.
|
||||
*
|
||||
* The block is declared and stepped in three parts around the layer, in this order: `inputs`
|
||||
* before the layer's attention, `join` (the residual add and the proposal rows' selection) between
|
||||
* its attention and its feed-forward, `head` after it. The embedding and lm_head are the trunk's.
|
||||
* Its names live under the `mtp.` scope (Names), its weights in layer 45's group. eh_proj
|
||||
* (4096 x 8192 bf16, 64 MiB) is replicated and read whole in every draft pass. */
|
||||
#ifndef RAD_BLOCK_GLM_MTP_H
|
||||
#define RAD_BLOCK_GLM_MTP_H
|
||||
#include "rad_glm_tp.h"
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct GlmMtp {
|
||||
/* The trunk's: its final hidden states, its logits, its embedding and head. */
|
||||
struct Wire {
|
||||
rad_buf final_hidden = 0, logits = 0;
|
||||
rad_weight token = 0, head = 0;
|
||||
};
|
||||
static constexpr int kDepth = 3; /* --num-speculative-tokens auto */
|
||||
Names nm{ "mtp" };
|
||||
Geom g{};
|
||||
Wire w{};
|
||||
rad_buf x = 0, hidden = 0, embedding = 0, concat = 0, out = 0;
|
||||
rad_weight en = 0, hn = 0, eh = 0, norm = 0;
|
||||
rad_op op_prev = 0, op_embed = 0, op_embed_ar = 0, op_en = 0, op_hn = 0, op_eh = 0;
|
||||
rad_op op_residual = 0, op_select = 0, op_back = 0;
|
||||
rad_op op_norm = 0, op_copy = 0, op_head = 0;
|
||||
|
||||
/* The buffers, the weights, and the ops up to the layer's input. */
|
||||
int declare_inputs(RadBuilder* b, const Geom& geom, const Wire& wire) {
|
||||
g = geom;
|
||||
w = wire;
|
||||
if (!b || !w.final_hidden || !w.logits || !w.token || !w.head || g.max_spec < 1)
|
||||
return RAD_E_INVAL;
|
||||
x = decl_b(b, nm.f("x"), RAD_BF16, { g.max_tok, 4096 });
|
||||
hidden = decl_b(b, nm.f("hidden"), RAD_BF16, { g.max_tok, 4096 }, RAD_BUF_PERSIST);
|
||||
embedding = decl_b(b, nm.f("embedding"), RAD_BF16, { g.max_tok, 4096 });
|
||||
concat = decl_b(b, nm.f("concat"), RAD_BF16, { g.max_tok, 8192 });
|
||||
out = decl_b(b, nm.f("final"), RAD_BF16, { g.max_tok, 4096 });
|
||||
const char* pre = "model.language_model.layers.45";
|
||||
auto gain = [&](const char* n, const char* src) {
|
||||
const char* name = nm.f("%s", n);
|
||||
if (map_copy(b, name, nm.ckpt("%s.%s", pre, src)) < 0) return (rad_weight)0;
|
||||
return decl_w(b, name, RAD_F32, { 4096 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE,
|
||||
grp_layer(45));
|
||||
};
|
||||
en = gain("enorm.weight", "enorm.weight");
|
||||
hn = gain("hnorm.weight", "hnorm.weight");
|
||||
norm = gain("output_norm.weight", "shared_head.norm.weight");
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("eh_proj.weight"), nm.ckpt("%s.eh_proj.weight", pre)));
|
||||
eh = decl_w(b, nm.f("eh_proj.weight"), RAD_BF16, { 4096, 8192 }, RAD_ACCESS_PER_TOKEN,
|
||||
RAD_SHARD_NONE, grp_layer(45));
|
||||
if (!x || !hidden || !embedding || !concat || !out || !en || !hn || !norm || !eh)
|
||||
return RAD_E_INVAL;
|
||||
op_prev = rw(b,
|
||||
RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 4096),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.final_hidden }, { hidden });
|
||||
op_embed = rw(b,
|
||||
RAD_OP(b, "embed_lookup",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n_embd", 4096),
|
||||
RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", "bf16"),
|
||||
RAD_INT("vocab_offset", g.vocab_off)),
|
||||
RAD_WEIGHTS(w.token)),
|
||||
{}, { embedding });
|
||||
op_embed_ar = GlmTp::reduce(b, g, embedding, true);
|
||||
op_en = rmsnorm(b, en, embedding, concat);
|
||||
op_hn = rmsnorm(b, hn, hidden, concat);
|
||||
op_eh = rw(b,
|
||||
RAD_OP(b, "gemm_nt",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", 4096),
|
||||
RAD_INT("K", 8192), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(eh)),
|
||||
{ concat }, { x });
|
||||
return op_prev && op_embed && op_en && op_hn && op_eh && (g.world == 1 || op_embed_ar)
|
||||
? RAD_OK
|
||||
: RAD_E_INVAL;
|
||||
}
|
||||
/* Between the layer's attention and its feed-forward: the residual add of the attention's
|
||||
* output `y`, and on a history pass the proposal rows gathered to the head of `x`. */
|
||||
int declare_join(RadBuilder* b, rad_buf y) {
|
||||
op_residual = rw(b,
|
||||
RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 4096),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ x, y }, { x });
|
||||
// All history rows fill the draft cache; only proposal rows need its FFN.
|
||||
op_select = rw(b,
|
||||
RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", 4096),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ x }, { hidden });
|
||||
op_back = cast(b, hidden, x);
|
||||
return op_residual && op_select && op_back ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
/* After the layer: the head's norm, its output kept as the next round's hidden, the logits,
|
||||
* and the drafter's declaration. */
|
||||
int declare_head(RadBuilder* b) {
|
||||
op_norm = rmsnorm(b, norm, x, out);
|
||||
op_copy = cast(b, out, hidden);
|
||||
op_head = rw(b,
|
||||
RAD_OP(b, "logits_gemm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows),
|
||||
RAD_INT("n_vocab", g.n_vocab), RAD_INT("n_embd", 4096),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(w.head)),
|
||||
{ hidden }, { w.logits });
|
||||
if (!op_norm || !op_copy || !op_head) return RAD_E_INVAL;
|
||||
RadDrafterDecl dd{};
|
||||
dd.name = "mtp";
|
||||
dd.kind = RAD_DRAFT_SERIAL;
|
||||
dd.depth = g.max_spec;
|
||||
dd.mask_token = -1;
|
||||
return rad_declare_drafter(b, &dd);
|
||||
}
|
||||
|
||||
/* The rows a pass runs its feed-forward and head on: the proposal rows of a history pass,
|
||||
* every row of a draft round. */
|
||||
static int64_t head_rows(const RadBatch* batch) {
|
||||
return batch->draft_pass < 0 ? batch->n_draft_out : batch->n_tok;
|
||||
}
|
||||
void step_inputs(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
if (batch->draft_pass < 0 && batch->n_out > 0)
|
||||
RAD_ISSUE_N(c, op_prev, batch->n_out, RAD_B(w.final_hidden),
|
||||
praw(batch->out_ids, RAD_I32, batch->n_out), brows(hidden, batch->n_out));
|
||||
RAD_ISSUE_N(c, op_embed, T, praw(batch->token_ids, RAD_I32, T), RAD_W(w.token),
|
||||
brows(embedding, T));
|
||||
if (op_embed_ar) RAD_ISSUE_N(c, op_embed_ar, T * 4096, brows(embedding, T), RAD_NONE);
|
||||
RAD_ISSUE_N(c, op_en, T, brows(embedding, T), RAD_W(en), bcol(concat, 0, 4096, T));
|
||||
RAD_ISSUE_N(c, op_hn, T, brows(hidden, T), RAD_W(hn), bcol(concat, 4096, 4096, T));
|
||||
RAD_ISSUE_N(c, op_eh, T, brows(concat, T), RAD_W(eh), brows(x, T));
|
||||
}
|
||||
void step_join(RadCtx* c, const RadBatch* batch, rad_buf y) const {
|
||||
const int64_t T = batch->n_tok, L = head_rows(batch);
|
||||
RAD_ISSUE_N(c, op_residual, T, brows(x, T), brows(y, T), brows(x, T));
|
||||
if (batch->draft_pass < 0) {
|
||||
RAD_ISSUE_N(c, op_select, L, RAD_B(x), praw(batch->draft_out_ids, RAD_I32, L),
|
||||
brows(hidden, L));
|
||||
RAD_ISSUE_N(c, op_back, L, brows(hidden, L), brows(x, L));
|
||||
}
|
||||
}
|
||||
void step_head(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t L = head_rows(batch);
|
||||
RAD_ISSUE_N(c, op_norm, L, brows(x, L), RAD_W(norm), brows(out, L));
|
||||
RAD_ISSUE_N(c, op_copy, L, brows(out, L), brows(hidden, L));
|
||||
RAD_ISSUE_N(c, op_head, L, brows(hidden, L), RAD_W(w.head), brows(w.logits, L));
|
||||
}
|
||||
|
||||
private:
|
||||
rad_op rmsnorm(RadBuilder* b, rad_weight gain, rad_buf in, rad_buf out) const {
|
||||
return rw(b,
|
||||
RAD_OP(b, "rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 4096),
|
||||
RAD_F64("eps", g.eps), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(gain)),
|
||||
{ in }, { out });
|
||||
}
|
||||
rad_op cast(RadBuilder* b, rad_buf in, rad_buf out) const {
|
||||
return rw(b,
|
||||
RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 4096),
|
||||
RAD_STR("from", "bf16"), RAD_STR("to", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ in }, { out });
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace rad::arch
|
||||
#endif
|
||||
@@ -2,7 +2,7 @@
|
||||
* come from the uncorrected sigmoid probabilities. All routing arithmetic is F32. */
|
||||
#ifndef RAD_BLOCK_GLM_ROUTER_H
|
||||
#define RAD_BLOCK_GLM_ROUTER_H
|
||||
#include "rad_tp_split.h"
|
||||
#include "rad_glm_tp.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace rad {
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
/* GLM's recurrent core over already projected inputs. ple_conv supplies exactly
|
||||
* causal depthwise convolution + SiLU, including cold start and accepted-history rollback. */
|
||||
#ifndef RAD_BLOCK_KDA_STATE_H
|
||||
#define RAD_BLOCK_KDA_STATE_H
|
||||
#include "rad_arch.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct KdaState {
|
||||
struct Config {
|
||||
int64_t heads = 16, dim = 128, width = 4;
|
||||
double lower = -5;
|
||||
};
|
||||
struct Src {
|
||||
const char *q_conv = nullptr, *k_conv = nullptr, *v_conv = nullptr, *alog = nullptr,
|
||||
*bias = nullptr, *norm = nullptr;
|
||||
};
|
||||
struct Wire {
|
||||
rad_buf qkv = 0, conv = 0, a = 0, beta = 0, gate = 0, out = 0;
|
||||
};
|
||||
Geom g{};
|
||||
Config c{};
|
||||
Wire w{};
|
||||
int layer = 0;
|
||||
rad_kvgroup state = 0, history = 0;
|
||||
rad_weight taps = 0, alog = 0, bias = 0, norm = 0;
|
||||
rad_op op_conv = 0, op_verify = 0, op_prefill = 0, op_norm = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, rad_kvgroup st, rad_kvgroup cv,
|
||||
const Config& cfg, const Wire& wire, const Src& src) {
|
||||
g = geom;
|
||||
c = cfg;
|
||||
w = wire;
|
||||
layer = l;
|
||||
state = st;
|
||||
history = cv;
|
||||
if (!b || !st || !cv || c.heads < 1 || c.dim != 128 || c.width != 4 || g.max_spec > 31 ||
|
||||
!std::isfinite(c.lower) || c.lower >= 0 || !w.qkv || !w.conv || !w.a || !w.beta ||
|
||||
!w.gate || !w.out || w.qkv == w.conv || !src.q_conv || !src.k_conv || !src.v_conv ||
|
||||
!src.alog || !src.bias || !src.norm)
|
||||
return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, st));
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, cv));
|
||||
const int64_t width = c.heads * c.dim;
|
||||
const char *tn = nm.f("blk.%d.ssm_conv.weight", l), *an = nm.f("blk.%d.ssm_a", l),
|
||||
*bn = nm.f("blk.%d.ssm_dt_bias", l), *nn = nm.f("blk.%d.ssm_norm.weight", l);
|
||||
RAD_ARCH_TRY(map_concat(b, tn, { src.q_conv, src.k_conv, src.v_conv }));
|
||||
RAD_ARCH_TRY(map_copy(b, an, src.alog));
|
||||
RAD_ARCH_TRY(map_copy(b, bn, src.bias));
|
||||
RAD_ARCH_TRY(map_copy(b, nn, src.norm));
|
||||
taps = decl_w(b, tn, RAD_BF16, { 3 * width, c.width }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW,
|
||||
grp_layer(l), 0, { width, width, width });
|
||||
alog =
|
||||
decl_w(b, an, RAD_F32, { c.heads }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
|
||||
bias = decl_w(b, bn, RAD_F32, { width }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
|
||||
norm =
|
||||
decl_w(b, nn, RAD_F32, { c.dim }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
if (!taps || !alog || !bias || !norm) return RAD_E_INVAL;
|
||||
op_conv = rw(b,
|
||||
RAD_OP(b, "ple_conv",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 3 * width),
|
||||
RAD_INT("width", c.width), RAD_INT("dilation", 1),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(taps)),
|
||||
{ w.qkv }, { w.conv });
|
||||
for (int verify = 0; verify < 2; ++verify) {
|
||||
const rad_op op = rw(
|
||||
b,
|
||||
RAD_OP(b, "kda_update",
|
||||
RAD_PARAMS(
|
||||
RAD_RANGE("M", 1, g.max_tok), RAD_INT("heads", c.heads),
|
||||
RAD_INT("head_k", c.dim), RAD_INT("head_v", c.dim),
|
||||
RAD_F64("lower_bound", c.lower),
|
||||
RAD_F64("scale", 1.0 / std::sqrt((double)c.dim)),
|
||||
RAD_F64("l2_eps", 1e-6), RAD_STR("mode", verify ? "verify" : "prefill"),
|
||||
RAD_STR("dtype", "bf16"),
|
||||
RAD_STR("state_form", g.max_spec > 0 ? "anchor" : "per_candidate")),
|
||||
RAD_WEIGHTS(alog, bias)),
|
||||
{ w.conv, w.a, w.beta }, { w.out });
|
||||
if (verify)
|
||||
op_verify = op;
|
||||
else
|
||||
op_prefill = op;
|
||||
}
|
||||
op_norm = rw(b,
|
||||
RAD_OP(b, "gdn_gated_rmsnorm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("channels", c.dim),
|
||||
RAD_F64("eps", g.eps), RAD_STR("act", "sigmoid")),
|
||||
RAD_WEIGHTS(norm)),
|
||||
{ w.out, w.gate }, { w.out });
|
||||
return op_conv && op_verify && op_prefill && op_norm ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
void step(RadCtx* ctx, const RadBatch* batch) const {
|
||||
const auto *st = kv_batch(batch, state), *cv = kv_batch(batch, history);
|
||||
if (!st || !cv || !st->state_index || !cv->state_index) {
|
||||
rad_step_fail(ctx, "KDA requires recurrent and convolution state indices");
|
||||
return;
|
||||
}
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq, width = c.heads * c.dim;
|
||||
const int64_t pitch = st->state_index_pitch > 0 ? st->state_index_pitch : 1;
|
||||
int64_t D = 0, DT = 0;
|
||||
batch_split(batch, &D, &DT);
|
||||
for (int verify = 1; verify >= 0; --verify) {
|
||||
const int64_t seq0 = verify ? 0 : D, seqs = verify ? D : S - D, rows = verify ? DT : T;
|
||||
if (!seqs) continue;
|
||||
const RadOperand cu = praw(batch->cu_seqlens + seq0, RAD_I32, seqs + 1);
|
||||
const RadOperand init = praw(batch->ctx_lens + seq0, RAD_I32, seqs);
|
||||
const RadOperand accepted =
|
||||
verify ? praw(batch->num_accepted, RAD_I32, seqs) : RAD_NONE;
|
||||
RAD_ISSUE_N(ctx, op_conv, rows, brows(w.qkv, rows), RAD_W(taps),
|
||||
kv_cache(history, layer), cu, RAD_NONE,
|
||||
praw(cv->state_index + seq0, RAD_I32, seqs), init, accepted,
|
||||
brows(w.conv, rows));
|
||||
RAD_ISSUE_N(ctx, verify ? op_verify : op_prefill, rows, bcol(w.conv, 0, width, rows),
|
||||
bcol(w.conv, width, width, rows), bcol(w.conv, 2 * width, width, rows),
|
||||
brows(w.a, rows), brows(w.beta, rows), RAD_W(alog), RAD_W(bias),
|
||||
kv_cache(state, layer),
|
||||
praw2(st->state_index + seq0 * pitch, RAD_I32, seqs, pitch), cu, accepted,
|
||||
init, brows(w.out, rows));
|
||||
}
|
||||
RAD_ISSUE_N(ctx, op_norm, T, brows(w.out, T), brows(w.gate, T), RAD_W(norm),
|
||||
brows(w.out, T));
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace rad::arch
|
||||
#endif
|
||||
@@ -1,84 +0,0 @@
|
||||
/* Absorbed GLM MLA core. The caller projects/norms q and stores the current
|
||||
* latent before step(). Complete heads shard as rows of the shared K/V weight. */
|
||||
#ifndef RAD_BLOCK_MLA_LATENT_H
|
||||
#define RAD_BLOCK_MLA_LATENT_H
|
||||
#include "rad_arch.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct MlaLatent {
|
||||
struct Config {
|
||||
int64_t heads = 16, head_dim = 256, latent_dim = 512, blocks = 513;
|
||||
};
|
||||
struct Wire {
|
||||
rad_buf q = 0, q_latent = 0, attn_latent = 0, out = 0, selected = 0, length = 0;
|
||||
};
|
||||
Geom g{};
|
||||
Config c{};
|
||||
Wire w{};
|
||||
int layer = 0;
|
||||
rad_kvgroup kv = 0;
|
||||
rad_weight projection = 0;
|
||||
rad_op op_absorb = 0, op_attn = 0, op_expand = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, rad_kvgroup group,
|
||||
const Config& cfg, const Wire& wire, const char* source) {
|
||||
g = geom;
|
||||
c = cfg;
|
||||
w = wire;
|
||||
layer = l;
|
||||
kv = group;
|
||||
if (!b || !source || !group || c.heads < 1 || c.blocks < 1 || !w.q || !w.q_latent ||
|
||||
!w.attn_latent || !w.out || !w.selected || !w.length || w.q == w.q_latent ||
|
||||
w.q_latent == w.attn_latent || w.attn_latent == w.out || c.latent_dim != 512 ||
|
||||
c.head_dim != 256)
|
||||
return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, group));
|
||||
const char* name = nm.f("blk.%d.attn_kv_b.weight", l);
|
||||
RAD_ARCH_TRY(map_copy(b, name, source));
|
||||
// The conversion recipe decodes this small projection only.
|
||||
projection = decl_w(b, name, RAD_BF16, { c.heads * 2 * c.head_dim, c.latent_dim },
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_ROW, grp_layer(l));
|
||||
if (!projection) return RAD_E_INVAL;
|
||||
op_absorb = rw(
|
||||
b,
|
||||
RAD_OP(b, "mla_project",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("heads", c.heads),
|
||||
RAD_INT("head_dim", c.head_dim), RAD_INT("latent_dim", c.latent_dim),
|
||||
RAD_STR("mode", "absorb"), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(projection)),
|
||||
{ w.q }, { w.q_latent });
|
||||
op_attn =
|
||||
rw(b,
|
||||
RAD_OP(b, "mla_attn",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("latent_dim", c.latent_dim),
|
||||
RAD_INT("heads", c.heads), RAD_INT("blocks", c.blocks),
|
||||
RAD_INT("block_size", 4),
|
||||
RAD_F64("scale", 1.0 / std::sqrt((double)c.head_dim)),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ w.q_latent, w.selected, w.length }, { w.attn_latent });
|
||||
if (!op_attn || rad_kv_block_size(b, group) != 4) return RAD_E_INVAL;
|
||||
op_expand = rw(
|
||||
b,
|
||||
RAD_OP(b, "mla_project",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("heads", c.heads),
|
||||
RAD_INT("head_dim", c.head_dim), RAD_INT("latent_dim", c.latent_dim),
|
||||
RAD_STR("mode", "expand"), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(projection)),
|
||||
{ w.attn_latent }, { w.out });
|
||||
return op_absorb && op_attn && op_expand ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
void step(RadCtx* ctx, int64_t rows) const {
|
||||
RAD_ISSUE_N(ctx, op_absorb, rows, brows(w.q, rows), RAD_W(projection),
|
||||
brows(w.q_latent, rows));
|
||||
RAD_ISSUE_N(ctx, op_attn, rows, brows(w.q_latent, rows), kv_cache(kv, layer),
|
||||
brows(w.selected, rows), brows(w.length, rows), brows(w.attn_latent, rows));
|
||||
RAD_ISSUE_N(ctx, op_expand, rows, brows(w.attn_latent, rows), RAD_W(projection),
|
||||
brows(w.out, rows));
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace rad::arch
|
||||
#endif
|
||||
@@ -0,0 +1,103 @@
|
||||
/* GLM-5.3's tensor parallelism in one place: what a rank's shard of the model is, from which step
|
||||
* size a replicated piece of work splits over the ranks instead, and the column split of a
|
||||
* replicated F32 projection.
|
||||
*
|
||||
* The residual stream is replicated (spec.md §9). Two kinds of split share the ranks' work:
|
||||
*
|
||||
* heads the MLA and KDA heads, with them the rows of their projections, the KDA state and
|
||||
* the convolution history; the experts' and the dense feed-forward's width
|
||||
* (`heads`, `kda_width`, `expert_width`, `dense_width`); joined by an all_reduce
|
||||
* columns a replicated F32 projection's output columns (SplitF32Proj), or the mHC read's
|
||||
* input chunks (ManifoldConn); joined by an all_gather, bit for bit the replicated
|
||||
* computation */
|
||||
#ifndef RAD_GLM_TP_H
|
||||
#define RAD_GLM_TP_H
|
||||
#include "rad_arch.h"
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
struct GlmTp {
|
||||
/* The model's widths a rank's shard divides: 64 heads of 128 (KDA) and 256 (MLA), experts of
|
||||
* 2048 and a dense feed-forward of 12288. */
|
||||
static constexpr int64_t kHeads = 64, kKdaDim = 128, kMlaDim = 256, kExpertFF = 2048,
|
||||
kDenseFF = 12288;
|
||||
static bool world_ok(int world) { return world == 1 || world == 2 || world == 4 || world == 8; }
|
||||
static int64_t heads(int world) { return kHeads / world; }
|
||||
static int64_t kda_width(int world) { return heads(world) * kKdaDim; }
|
||||
static int64_t mla_width(int world) { return heads(world) * kMlaDim; }
|
||||
static int64_t expert_width(int world) { return kExpertFF / world; }
|
||||
static int64_t dense_width(int world) { return kDenseFF / world; }
|
||||
|
||||
/* Rows from which a replicated piece of work splits over the ranks. Below it, as in decode,
|
||||
* the collective's latency outweighs the saved arithmetic and every rank runs the replicated
|
||||
* form. */
|
||||
static constexpr int64_t kSplitRows = 256;
|
||||
|
||||
/* The all_reduce of a replicated 4096-wide bf16 row over the ranks: the embedding's (exact)
|
||||
* and each head-split block's (the deployment's wire). */
|
||||
static rad_op reduce(RadBuilder* b, const Geom& g, rad_buf x, bool exact = false) {
|
||||
if (g.world == 1) return 0;
|
||||
return rw(b,
|
||||
RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
RAD_RANGE("numel", 4096, g.max_tok * 4096),
|
||||
RAD_STR("dtype", "bf16"),
|
||||
RAD_INT("exact", exact ? 1 : g.wire_exact),
|
||||
RAD_INT("min_bytes", g.wire_min_bytes)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ x }, { x });
|
||||
}
|
||||
};
|
||||
|
||||
/* The split's ops are declared whatever the step size, and each gather's message as a range of
|
||||
* rows: the engine's arena levels declare again at a smaller max_tok and must get the same graph
|
||||
* with the same fixed parameters.
|
||||
*
|
||||
* A replicated [n, k] F32 projection computed column-split: rank r runs the same gemm_nt_f32 on
|
||||
* rows [r*n/world, (r+1)*n/world) of the weight and a row-interleaved all_gather lays the slices
|
||||
* out as the full [M, n]. The kernel computes every output column independently of n and the
|
||||
* gather moves bytes, so each value is bit-identical to the replicated projection's. */
|
||||
struct SplitF32Proj {
|
||||
rad_buf part = 0;
|
||||
rad_op op_part = 0, op_gather = 0;
|
||||
int64_t cols = 0, k = 0, min_rows = 0, first_col = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& g, const char* name, int64_t rows, int64_t n,
|
||||
int64_t width, rad_weight weight, rad_buf input, rad_buf output) {
|
||||
min_rows = GlmTp::kSplitRows;
|
||||
if (g.world < 2 || n % g.world || (n / g.world * 4) % 16) {
|
||||
min_rows = 0;
|
||||
return RAD_OK;
|
||||
}
|
||||
cols = n / g.world;
|
||||
k = width;
|
||||
first_col = g.rank * cols;
|
||||
part = decl_b(b, nm.f("%s.part", name), RAD_F32, { rows, cols });
|
||||
op_part = rw(b,
|
||||
RAD_OP(b, "gemm_nt_f32",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("N", cols), RAD_INT("K", k),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(weight)),
|
||||
{ input }, { part });
|
||||
op_gather = rw(
|
||||
b,
|
||||
RAD_OP(b, "all_gather",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", cols, rows * cols),
|
||||
RAD_STR("dtype", "f32"), RAD_INT("row", cols)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ part }, { output });
|
||||
return part && op_part && op_gather ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
bool takes(int64_t rows) const { return min_rows && rows >= min_rows; }
|
||||
void step(RadCtx* ctx, int64_t rows, RadOperand input, rad_weight weight,
|
||||
RadOperand output) const {
|
||||
RadOperand slice = RAD_W(weight);
|
||||
slice.offset = first_col * k;
|
||||
slice.rows = cols;
|
||||
RAD_ISSUE_N(ctx, op_part, rows, input, slice, brows(part, rows));
|
||||
RAD_ISSUE_N(ctx, op_gather, rows * cols, brows(part, rows), output);
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace rad::arch
|
||||
#endif
|
||||
@@ -1,65 +0,0 @@
|
||||
/* GLM's replicated F32 work split over the tensor-parallel ranks: each rank computes a disjoint
|
||||
* part with the replicated kernel's own arithmetic and an all_gather moves the parts to every rank,
|
||||
* so every value is bit-identical to the replicated computation's. */
|
||||
#ifndef RAD_TP_SPLIT_H
|
||||
#define RAD_TP_SPLIT_H
|
||||
#include "rad_arch.h"
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
/* Rows from which a replicated F32 projection splits its columns over the ranks. Below it the
|
||||
* collective's latency outweighs the saved arithmetic, so decode keeps the replicated form. */
|
||||
constexpr int64_t kGlmTpSplitRows = 256;
|
||||
|
||||
/* The split's ops are declared whatever the step size, and each gather's message as a range of
|
||||
* rows: the engine's arena levels declare again at a smaller max_tok and must get the same graph
|
||||
* with the same fixed parameters.
|
||||
*
|
||||
* A replicated [n, k] F32 projection computed column-split: rank r runs the same gemm_nt_f32 on
|
||||
* rows [r*n/world, (r+1)*n/world) of the weight and a row-interleaved all_gather lays the slices
|
||||
* out as the full [M, n]. The kernel computes every output column independently of n and the
|
||||
* gather moves bytes, so each value is bit-identical to the replicated projection's. */
|
||||
struct SplitF32Proj {
|
||||
rad_buf part = 0;
|
||||
rad_op op_part = 0, op_gather = 0;
|
||||
int64_t cols = 0, k = 0, min_rows = 0, first_col = 0;
|
||||
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& g, const char* name, int64_t rows, int64_t n,
|
||||
int64_t width, rad_weight weight, rad_buf input, rad_buf output) {
|
||||
min_rows = kGlmTpSplitRows;
|
||||
if (g.world < 2 || n % g.world || (n / g.world * 4) % 16) {
|
||||
min_rows = 0;
|
||||
return RAD_OK;
|
||||
}
|
||||
cols = n / g.world;
|
||||
k = width;
|
||||
first_col = g.rank * cols;
|
||||
part = decl_b(b, nm.f("%s.part", name), RAD_F32, { rows, cols });
|
||||
op_part = rw(b,
|
||||
RAD_OP(b, "gemm_nt_f32",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("N", cols), RAD_INT("K", k),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(weight)),
|
||||
{ input }, { part });
|
||||
op_gather = rw(
|
||||
b,
|
||||
RAD_OP(b, "all_gather",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world), RAD_RANGE("numel", cols, rows * cols),
|
||||
RAD_STR("dtype", "f32"), RAD_INT("row", cols)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ part }, { output });
|
||||
return part && op_part && op_gather ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
bool takes(int64_t rows) const { return min_rows && rows >= min_rows; }
|
||||
void step(RadCtx* ctx, int64_t rows, RadOperand input, rad_weight weight,
|
||||
RadOperand output) const {
|
||||
RadOperand slice = RAD_W(weight);
|
||||
slice.offset = first_col * k;
|
||||
slice.rows = cols;
|
||||
RAD_ISSUE_N(ctx, op_part, rows, input, slice, brows(part, rows));
|
||||
RAD_ISSUE_N(ctx, op_gather, rows * cols, brows(part, rows), output);
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace rad::arch
|
||||
#endif
|
||||
+255
-272
@@ -1,16 +1,35 @@
|
||||
/* GLM-5.3-Flash: four mHC streams, KDA/latent MLA, mixed Quark weights,
|
||||
* width-sharded routed experts and a native serial next-token decoder.
|
||||
* This plugin uses only the public ABI and the installed common headers. */
|
||||
#include <arch/rad_block_mhc.h>
|
||||
/* glm5next -- GLM-5.3-Flash (`model_type: glm5_next`) from AMD's Quark MXFP4 export: four mHC
|
||||
* residual streams over 34 KDA and 11 sparse latent-attention (MLA) layers behind a DSA indexer,
|
||||
* 288 MXFP4 experts at top-8 under sigmoid routing, block-FP8 attention and dense layers at f32
|
||||
* scales, each weight served as the container stores it; its MTP layer is a serial drafter.
|
||||
* docs/ARCHITECTURES.md describes the model and its traps.
|
||||
*
|
||||
* The blocks live in arch/common (installed as <arch/...>), one a file:
|
||||
*
|
||||
* rad_block_glm_mhc.h ManifoldConn the four-stream residual read and write
|
||||
* rad_block_glm_kda.h GlmKda the 34 linear layers, KdaState their recurrence
|
||||
* rad_block_glm_mla.h GlmMla the 11 sparse MLA layers, MlaLatent their core
|
||||
* rad_block_glm_indexer.h GlmIndexer the DSA indexer, DsaCache its key cache and selection
|
||||
* rad_block_glm_router.h GlmRouter the correction-bias router (through MoeFP8)
|
||||
* rad_block_glm_mtp.h GlmMtp the draft head around the plugin's layer 45
|
||||
* rad_glm_tp.h GlmTp the shards and the split threshold
|
||||
*
|
||||
* This file declares the buffers the blocks share, places the blocks and issues the step's
|
||||
* order. Declaration order is liveness (the buffer plan takes lifetimes from it) and the issue
|
||||
* order is the recorded pass, so both are kept as they read here. This plugin uses only the
|
||||
* public ABI and the installed common headers. */
|
||||
#include <arch/rad_block_glm_mhc.h>
|
||||
#include <arch/rad_block_glm_kda.h>
|
||||
#include <arch/rad_block_glm_mla.h>
|
||||
#include <arch/rad_block_glm_indexer.h>
|
||||
#include <arch/rad_block_glm_mtp.h>
|
||||
#include <arch/rad_block_mlp_fp8.h>
|
||||
#include <arch/rad_block_moe_fp8.h>
|
||||
#include <memory>
|
||||
|
||||
namespace glm5next {
|
||||
using namespace rad::arch;
|
||||
|
||||
struct Layer {
|
||||
bool full = false, draft = false;
|
||||
ManifoldConn attn_hc{}, ffn_hc{};
|
||||
@@ -20,17 +39,17 @@ struct Layer {
|
||||
GlmIndexer index{};
|
||||
MlpFP8 dense{};
|
||||
MoeFP8 moe{};
|
||||
rad_op residual = 0;
|
||||
};
|
||||
struct Model {
|
||||
Names nm{}, dnm{ "mtp" };
|
||||
Names nm{};
|
||||
Geom g{};
|
||||
std::vector<Layer> layers;
|
||||
Layer draft{};
|
||||
rad_kvgroup latent = 0, index = 0, tail = 0, state = 0, conv = 0;
|
||||
/* The streams and the step's stream-wide buffers. */
|
||||
rad_buf h0 = 0, h1 = 0, x = 0, y = 0, post = 0, comb = 0, final_hidden = 0, logit_input = 0,
|
||||
logits = 0;
|
||||
ActFP8 act{}, ff{};
|
||||
ActFP8 act{};
|
||||
/* The blocks' wires: one set of activations, shared by every layer of a kind. */
|
||||
GlmKda::Wire kw{};
|
||||
GlmMla::Wire aw{};
|
||||
GlmIndexer::Wire iw{};
|
||||
@@ -38,11 +57,10 @@ struct Model {
|
||||
MoeFP8::Wire mw{};
|
||||
rad_weight token = 0, head = 0, final_gain = 0;
|
||||
rad_op embed = 0, embed_ar = 0, expand = 0, contract = 0, norm = 0, gather = 0, head_op = 0;
|
||||
rad_buf mtp_x = 0, mtp_h = 0, mtp_e = 0, mtp_concat = 0, mtp_final = 0;
|
||||
rad_weight en = 0, hn = 0, eh = 0, mnorm = 0;
|
||||
rad_op mprev = 0, membed = 0, membed_ar = 0, men = 0, mhn = 0, meh = 0, mnorm_op = 0,
|
||||
mselect = 0, mback = 0, mcopy = 0, mhead = 0;
|
||||
/* The draft head and its layer, declared only when the deployment speculates. */
|
||||
bool have_mtp = false;
|
||||
GlmMtp mtp{};
|
||||
Layer draft{};
|
||||
};
|
||||
static std::unique_ptr<Model> models[MAX_RANKS];
|
||||
static thread_local std::unique_ptr<Model> probe_model;
|
||||
@@ -52,21 +70,10 @@ static int64_t cfg(const RadModelMeta* m, const char* key, int64_t fallback) {
|
||||
std::snprintf(nested, sizeof nested, "text_config.%s", key);
|
||||
return rad_meta_geti(m, key, rad_meta_geti(m, nested, fallback));
|
||||
}
|
||||
static rad_op reduce(RadBuilder* b, const Geom& g, rad_buf x, bool exact = false) {
|
||||
if (g.world == 1) return 0;
|
||||
return rw(b,
|
||||
RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(
|
||||
RAD_INT("world_size", g.world), RAD_RANGE("numel", 4096, g.max_tok * 4096),
|
||||
RAD_STR("dtype", "bf16"), RAD_INT("exact", exact ? 1 : g.wire_exact),
|
||||
RAD_INT("min_bytes", g.wire_min_bytes)),
|
||||
RAD_NOWEIGHTS),
|
||||
{ x }, { x });
|
||||
}
|
||||
static rad_op copy(RadBuilder* b, const Geom& g, rad_buf x, rad_buf y, int64_t n = 4096) {
|
||||
static rad_op copy(RadBuilder* b, const Geom& g, rad_buf x, rad_buf y) {
|
||||
return rw(b,
|
||||
RAD_OP(b, "cast",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n),
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 4096),
|
||||
RAD_STR("from", "bf16"), RAD_STR("to", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ x }, { y });
|
||||
@@ -79,116 +86,184 @@ static rad_op norm(RadBuilder* b, const Geom& g, rad_weight w, rad_buf x, rad_bu
|
||||
RAD_WEIGHTS(w)),
|
||||
{ x }, { y });
|
||||
}
|
||||
static int buffers(RadBuilder* b, Model& m) {
|
||||
|
||||
/* ------------------------------------------------------------------ the shared activations */
|
||||
static int stream_buffers(RadBuilder* b, Model& m) {
|
||||
auto& g = m.g;
|
||||
auto& nm = m.nm;
|
||||
const int64_t T = g.max_tok, W = 8192 / g.world, F = 2048 / g.world;
|
||||
auto bf = [&](const char* n, int64_t width) {
|
||||
return decl_b(b, nm.f("%s", n), RAD_BF16, { T, width });
|
||||
};
|
||||
auto a = [&](ActFP8& v, const char* n, int64_t width) {
|
||||
v.x = bf(n, width);
|
||||
return v.declare_qs(b, nm, g, n, width);
|
||||
};
|
||||
m.h0 = bf("manifold", 16384);
|
||||
m.h1 = bf("manifold_mid", 16384);
|
||||
m.x = bf("collapsed", 4096);
|
||||
m.y = bf("attn_delta", 4096);
|
||||
const int64_t T = g.max_tok;
|
||||
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 });
|
||||
m.post = decl_b(b, nm.f("post"), RAD_F32, { T, 4 });
|
||||
m.comb = decl_b(b, nm.f("mix"), RAD_F32, { T, 16 });
|
||||
RAD_ARCH_TRY(a(m.act, "normalized", 4096));
|
||||
m.act.x = decl_b(b, nm.f("normalized"), RAD_BF16, { T, 4096 });
|
||||
RAD_ARCH_TRY(m.act.declare_qs(b, nm, g, "normalized", 4096));
|
||||
m.final_hidden = decl_b(b, nm.f("final_hidden"), RAD_BF16, { T, 4096 }, RAD_BUF_PERSIST);
|
||||
m.logit_input = decl_b(b, nm.f("logit_input"), RAD_BF16, { g.max_logit_rows, 4096 });
|
||||
m.logits = decl_b(b, nm.f("logits"), RAD_F32, { g.max_logit_rows, g.n_vocab });
|
||||
RAD_ARCH_TRY(rad_declare_logits(b, m.logits));
|
||||
m.kw.input = m.act.x;
|
||||
m.kw.out = m.y;
|
||||
m.kw.forget_low = bf("forget_low", 128);
|
||||
m.kw.gate_low = bf("gate_low", 128);
|
||||
m.kw.core.qkv = bf("qkv", 3 * W);
|
||||
m.kw.core.conv = bf("convolved", 3 * W);
|
||||
m.kw.core.a = bf("forget", W);
|
||||
m.kw.core.beta = bf("beta", 64 / g.world);
|
||||
m.kw.core.gate = bf("gate", W);
|
||||
m.kw.core.out = bf("recurrent_out", W);
|
||||
m.aw.input = m.act;
|
||||
m.aw.q_raw = bf("q_raw", 1536);
|
||||
m.aw.kv_raw = bf("kv_raw", 512);
|
||||
RAD_ARCH_TRY(a(m.aw.q_low, "q_low", 1536));
|
||||
m.aw.latent = bf("latent", 512);
|
||||
m.aw.out = m.y;
|
||||
m.aw.core.q = bf("query", 16384 / g.world);
|
||||
m.aw.core.q_latent = decl_b(b, nm.f("q_latent"), RAD_BF16, { T, 64 / g.world, 512 });
|
||||
m.aw.core.attn_latent = decl_b(b, nm.f("attn_latent"), RAD_BF16, { T, 64 / g.world, 512 });
|
||||
RAD_ARCH_TRY(a(m.aw.attn, "expanded", 16384 / g.world));
|
||||
m.aw.core.out = m.aw.attn.x;
|
||||
m.aw.core.selected = decl_b(b, nm.f("selected"), RAD_I32, { T, 513 });
|
||||
m.aw.core.length = decl_b(b, nm.f("length"), RAD_I32, { T });
|
||||
const int64_t work = (T + 3) / 4 + g.max_seqs, blocks = (g.max_ctx + 3) / 4 + 1;
|
||||
m.iw.input = m.act.x;
|
||||
m.iw.q_low = m.aw.q_low.x;
|
||||
m.iw.q_wide = bf("index_query", 4096);
|
||||
m.iw.kg_raw = bf("index_key_gate", 256);
|
||||
auto& iw = m.iw.cache;
|
||||
iw.query = decl_b(b, nm.f("index_heads"), RAD_BF16, { T * 32, 128 });
|
||||
iw.codes = decl_b(b, nm.f("index_codes"), RAD_F8E4M3, { T * 32, 128 });
|
||||
iw.scales = decl_b(b, nm.f("index_scales"), RAD_F32, { T * 32, 1 });
|
||||
iw.gate = decl_b(b, nm.f("index_head_weights"), RAD_F32, { T, 32 });
|
||||
iw.kg = bf("index_normalized_key_gate", 256);
|
||||
iw.stage = decl_b(b, nm.f("pool_stage"), RAD_BF16, { work * 4, 256 });
|
||||
iw.page = decl_b(b, nm.f("pool_page"), RAD_I32, { work });
|
||||
iw.bpos = decl_b(b, nm.f("pool_pos"), RAD_I32, { work });
|
||||
iw.nc = decl_b(b, nm.f("pool_count"), RAD_I32, { g.max_seqs });
|
||||
iw.pooled = decl_b(b, nm.f("pool_codes"), RAD_F8E4M3, { work, 128 });
|
||||
iw.pool_scale = decl_b(b, nm.f("pool_scales"), RAD_F32, { work, 1 });
|
||||
iw.score = decl_b(b, nm.f("index_scores"), RAD_F32, { T, blocks });
|
||||
iw.selected = m.aw.core.selected;
|
||||
iw.length = m.aw.core.length;
|
||||
iw.nsel = decl_b(b, nm.f("selected_count"), RAD_I32, { T });
|
||||
m.dw.x = m.x;
|
||||
m.dw.h = m.act;
|
||||
m.dw.gate_up = bf("dense_gate_up", 2 * g.n_ff);
|
||||
RAD_ARCH_TRY(a(m.dw.ffn, "dense_ff", g.n_ff));
|
||||
const int64_t R = T;
|
||||
m.mw.x = m.x;
|
||||
m.mw.h = m.act;
|
||||
m.mw.logits = decl_b(b, nm.f("router_logits"), RAD_F32, { T, 288 });
|
||||
m.mw.ids = decl_b(b, nm.f("expert_ids"), RAD_I32, { T, 8 });
|
||||
m.mw.ew = decl_b(b, nm.f("expert_weights"), RAD_F32, { T, 8 });
|
||||
m.mw.eoff = decl_b(b, nm.f("expert_offsets"), RAD_I32, { 289 });
|
||||
m.mw.ecnt = decl_b(b, nm.f("expert_counts"), RAD_I32, { 288 });
|
||||
m.mw.sorted = decl_b(b, nm.f("expert_sorted"), RAD_I32, { R * 8 });
|
||||
m.mw.inv = decl_b(b, nm.f("expert_inverse"), RAD_I32, { R * 8 });
|
||||
m.mw.egu = decl_b(b, nm.f("expert_gate_up"), RAD_BF16, { R * 8, 2 * F });
|
||||
m.mw.eff.x = decl_b(b, nm.f("expert_ff"), RAD_BF16, { R * 8, F });
|
||||
RAD_ARCH_TRY(m.mw.eff.declare_qs(b, nm, g, "expert_ff", F, R * 8));
|
||||
m.mw.edn = decl_b(b, nm.f("expert_down"), RAD_BF16, { R * 8, 4096 });
|
||||
m.mw.sgu = bf("shared_gate_up", 2 * F);
|
||||
RAD_ARCH_TRY(a(m.mw.sff, "shared_ff", F));
|
||||
m.mw.sout = bf("shared_out", 4096);
|
||||
m.mw.sgate = bf("shared_one", 1);
|
||||
return rad_declare_logits(b, m.logits);
|
||||
}
|
||||
static int kda_buffers(RadBuilder* b, Model& m) {
|
||||
auto& g = m.g;
|
||||
auto& nm = m.nm;
|
||||
const int64_t T = g.max_tok, W = GlmTp::kda_width(g.world);
|
||||
auto bf = [&](const char* n, int64_t width) {
|
||||
return decl_b(b, nm.f("%s", n), RAD_BF16, { T, width });
|
||||
};
|
||||
auto& kw = m.kw;
|
||||
kw.input = m.act.x;
|
||||
kw.out = m.y;
|
||||
kw.forget_low = bf("forget_low", 128);
|
||||
kw.gate_low = bf("gate_low", 128);
|
||||
kw.core.qkv = bf("qkv", 3 * W);
|
||||
kw.core.conv = bf("convolved", 3 * W);
|
||||
kw.core.a = bf("forget", W);
|
||||
kw.core.beta = bf("beta", GlmTp::heads(g.world));
|
||||
kw.core.gate = bf("gate", W);
|
||||
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. */
|
||||
static int mla_buffers(RadBuilder* b, Model& m) {
|
||||
auto& g = m.g;
|
||||
auto& nm = m.nm;
|
||||
const int64_t T = g.max_tok, H = GlmTp::heads(g.world), Q = GlmTp::mla_width(g.world);
|
||||
auto bf = [&](const char* n, int64_t width) {
|
||||
return decl_b(b, nm.f("%s", n), RAD_BF16, { T, width });
|
||||
};
|
||||
auto& aw = m.aw;
|
||||
aw.input = m.act;
|
||||
aw.q_raw = bf("q_raw", 1536);
|
||||
aw.kv_raw = bf("kv_raw", 512);
|
||||
aw.q_low.x = bf("q_low", 1536);
|
||||
RAD_ARCH_TRY(aw.q_low.declare_qs(b, nm, g, "q_low", 1536));
|
||||
aw.latent = bf("latent", 512);
|
||||
aw.out = m.y;
|
||||
aw.core.q = bf("query", Q);
|
||||
aw.core.q_latent = decl_b(b, nm.f("q_latent"), RAD_BF16, { T, H, 512 });
|
||||
aw.core.attn_latent = decl_b(b, nm.f("attn_latent"), RAD_BF16, { T, H, 512 });
|
||||
aw.attn.x = bf("expanded", Q);
|
||||
RAD_ARCH_TRY(aw.attn.declare_qs(b, nm, g, "expanded", Q));
|
||||
aw.core.out = aw.attn.x;
|
||||
aw.core.selected = decl_b(b, nm.f("selected"), RAD_I32, { T, 513 });
|
||||
aw.core.length = decl_b(b, nm.f("length"), RAD_I32, { T });
|
||||
return RAD_OK;
|
||||
}
|
||||
static int indexer_buffers(RadBuilder* b, Model& m) {
|
||||
auto& g = m.g;
|
||||
auto& nm = m.nm;
|
||||
const int64_t T = g.max_tok;
|
||||
const int64_t work = (T + 3) / 4 + g.max_seqs, blocks = (g.max_ctx + 3) / 4 + 1;
|
||||
auto bf = [&](const char* n, int64_t width) {
|
||||
return decl_b(b, nm.f("%s", n), RAD_BF16, { T, width });
|
||||
};
|
||||
auto& iw = m.iw;
|
||||
iw.input = m.act.x;
|
||||
iw.q_low = m.aw.q_low.x;
|
||||
iw.q_wide = bf("index_query", 4096);
|
||||
iw.kg_raw = bf("index_key_gate", 256);
|
||||
auto& c = iw.cache;
|
||||
c.query = decl_b(b, nm.f("index_heads"), RAD_BF16, { T * 32, 128 });
|
||||
c.codes = decl_b(b, nm.f("index_codes"), RAD_F8E4M3, { T * 32, 128 });
|
||||
c.scales = decl_b(b, nm.f("index_scales"), RAD_F32, { T * 32, 1 });
|
||||
c.gate = decl_b(b, nm.f("index_head_weights"), RAD_F32, { T, 32 });
|
||||
c.kg = bf("index_normalized_key_gate", 256);
|
||||
c.stage = decl_b(b, nm.f("pool_stage"), RAD_BF16, { work * 4, 256 });
|
||||
c.page = decl_b(b, nm.f("pool_page"), RAD_I32, { work });
|
||||
c.bpos = decl_b(b, nm.f("pool_pos"), RAD_I32, { work });
|
||||
c.nc = decl_b(b, nm.f("pool_count"), RAD_I32, { g.max_seqs });
|
||||
c.pooled = decl_b(b, nm.f("pool_codes"), RAD_F8E4M3, { work, 128 });
|
||||
c.pool_scale = decl_b(b, nm.f("pool_scales"), RAD_F32, { work, 1 });
|
||||
c.score = decl_b(b, nm.f("index_scores"), RAD_F32, { T, blocks });
|
||||
c.selected = m.aw.core.selected;
|
||||
c.length = m.aw.core.length;
|
||||
c.nsel = decl_b(b, nm.f("selected_count"), RAD_I32, { T });
|
||||
return RAD_OK;
|
||||
}
|
||||
static int ffn_buffers(RadBuilder* b, Model& m) {
|
||||
auto& g = m.g;
|
||||
auto& nm = m.nm;
|
||||
const int64_t T = g.max_tok, F = GlmTp::expert_width(g.world);
|
||||
auto bf = [&](const char* n, int64_t width) {
|
||||
return decl_b(b, nm.f("%s", n), RAD_BF16, { T, width });
|
||||
};
|
||||
auto& dw = m.dw;
|
||||
dw.x = m.x;
|
||||
dw.h = m.act;
|
||||
dw.gate_up = bf("dense_gate_up", 2 * g.n_ff);
|
||||
dw.ffn.x = bf("dense_ff", g.n_ff);
|
||||
RAD_ARCH_TRY(dw.ffn.declare_qs(b, nm, g, "dense_ff", g.n_ff));
|
||||
auto& mw = m.mw;
|
||||
mw.x = m.x;
|
||||
mw.h = m.act;
|
||||
mw.logits = decl_b(b, nm.f("router_logits"), RAD_F32, { T, 288 });
|
||||
mw.ids = decl_b(b, nm.f("expert_ids"), RAD_I32, { T, 8 });
|
||||
mw.ew = decl_b(b, nm.f("expert_weights"), RAD_F32, { T, 8 });
|
||||
mw.eoff = decl_b(b, nm.f("expert_offsets"), RAD_I32, { 289 });
|
||||
mw.ecnt = decl_b(b, nm.f("expert_counts"), RAD_I32, { 288 });
|
||||
mw.sorted = decl_b(b, nm.f("expert_sorted"), RAD_I32, { T * 8 });
|
||||
mw.inv = decl_b(b, nm.f("expert_inverse"), RAD_I32, { T * 8 });
|
||||
mw.egu = decl_b(b, nm.f("expert_gate_up"), RAD_BF16, { T * 8, 2 * F });
|
||||
mw.eff.x = decl_b(b, nm.f("expert_ff"), RAD_BF16, { T * 8, F });
|
||||
RAD_ARCH_TRY(mw.eff.declare_qs(b, nm, g, "expert_ff", F, T * 8));
|
||||
mw.edn = decl_b(b, nm.f("expert_down"), RAD_BF16, { T * 8, 4096 });
|
||||
mw.sgu = bf("shared_gate_up", 2 * F);
|
||||
mw.sff.x = bf("shared_ff", F);
|
||||
RAD_ARCH_TRY(mw.sff.declare_qs(b, nm, g, "shared_ff", F));
|
||||
mw.sout = bf("shared_out", 4096);
|
||||
mw.sgate = bf("shared_one", 1);
|
||||
return RAD_OK;
|
||||
}
|
||||
static int kv_groups(RadBuilder* b, Model& m) {
|
||||
auto& g = m.g;
|
||||
auto& nm = m.nm;
|
||||
RadKVGroupDecl d{};
|
||||
d.kind = RAD_KV_FULL;
|
||||
d.dtype = RAD_BF16;
|
||||
d.n_head_kv = 1;
|
||||
d.head_dim = 256;
|
||||
m.latent = rad_decl_kv_group(b, nm.f("kv_latent"), &d);
|
||||
d = {};
|
||||
d.kind = RAD_KV_LINEAR;
|
||||
d.dtype = RAD_BF16;
|
||||
d.n_head_kv = 1;
|
||||
d.state_dim[0] = 4 + g.max_spec;
|
||||
d.state_dim[1] = 256;
|
||||
m.tail = rad_decl_kv_group(b, nm.f("kv_index_tail"), &d);
|
||||
d = {};
|
||||
d.kind = RAD_KV_LINEAR;
|
||||
d.dtype = RAD_F32;
|
||||
d.n_head_kv = GlmTp::heads(g.world);
|
||||
d.state_dim[0] = d.state_dim[1] = 128;
|
||||
m.state = rad_decl_kv_group(b, nm.f("kv_kda"), &d);
|
||||
d = {};
|
||||
d.kind = RAD_KV_CONV;
|
||||
d.dtype = RAD_BF16;
|
||||
d.n_head_kv = 1;
|
||||
d.head_dim = 3 * GlmTp::kda_width(g.world);
|
||||
d.conv_width = 4;
|
||||
m.conv = rad_decl_kv_group(b, nm.f("kv_kda_conv"), &d);
|
||||
// The index group is declared by the first indexer, right before its cache consumer.
|
||||
return m.latent && m.tail && m.state && m.conv ? RAD_OK : RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ one layer */
|
||||
static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool draft) {
|
||||
const auto& g = m.g;
|
||||
lay.draft = draft;
|
||||
lay.full = draft || l % 4 == 3;
|
||||
const char* pre = nm.ckpt("model.language_model.layers.%d", l);
|
||||
const char* ap = nm.ckpt("%s.self_attn", pre);
|
||||
rad_buf input = draft ? m.mtp_x : m.x;
|
||||
rad_buf input = draft ? m.mtp.x : m.x;
|
||||
ManifoldConn::Config hc;
|
||||
hc.eps = g.eps;
|
||||
if (!draft) {
|
||||
ManifoldConn::Config hc;
|
||||
hc.eps = g.eps;
|
||||
ManifoldConn::Wire a{ m.h0, m.x, m.y, m.h1, m.post, m.comb };
|
||||
ManifoldConn::Wire f{ m.h1, m.x, m.act.x, m.h0, m.post, m.comb };
|
||||
ManifoldConn::Src as{ nm.ckpt("%s.hc_attn_fn", pre), nm.ckpt("%s.hc_attn_scale", pre),
|
||||
nm.ckpt("%s.hc_attn_base", pre) };
|
||||
ManifoldConn::Src fs{ nm.ckpt("%s.hc_ffn_fn", pre), nm.ckpt("%s.hc_ffn_scale", pre),
|
||||
nm.ckpt("%s.hc_ffn_base", pre) };
|
||||
RAD_ARCH_TRY(lay.attn_hc.declare(b, nm, g, l, nm.f("blk.%d.hc_attn", l), hc, a, as, true));
|
||||
// Declare ffn mHC after the attention below: declaration order is liveness.
|
||||
(void)f;
|
||||
(void)fs;
|
||||
}
|
||||
const char* nn = nm.f("blk.%d.attn_norm.weight", l);
|
||||
RAD_ARCH_TRY(map_copy(b, nn, nm.ckpt("%s.input_layernorm.weight", pre)));
|
||||
@@ -210,29 +285,15 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra
|
||||
nm.ckpt("%s.dt_bias", ap), nm.ckpt("%s.o_norm.weight", ap) };
|
||||
RAD_ARCH_TRY(lay.kda.declare(b, nm, g, l, m.kw, ap, s, m.state, m.conv));
|
||||
}
|
||||
// The ffn connection is declared after the attention's post: declaration order is liveness.
|
||||
if (!draft) {
|
||||
RAD_ARCH_TRY(lay.attn_hc.declare_post(b));
|
||||
ManifoldConn::Config hc;
|
||||
hc.eps = g.eps;
|
||||
ManifoldConn::Wire f{ m.h1, m.x, m.act.x, m.h0, m.post, m.comb };
|
||||
ManifoldConn::Src fs{ nm.ckpt("%s.hc_ffn_fn", pre), nm.ckpt("%s.hc_ffn_scale", pre),
|
||||
nm.ckpt("%s.hc_ffn_base", pre) };
|
||||
RAD_ARCH_TRY(lay.ffn_hc.declare(b, nm, g, l, nm.f("blk.%d.hc_ffn", l), hc, f, fs, true));
|
||||
} else {
|
||||
lay.residual = rw(b,
|
||||
RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 4096),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.mtp_x, m.y }, { m.mtp_x });
|
||||
// All history rows fill the draft cache; only proposal rows need its FFN.
|
||||
m.mselect = rw(b,
|
||||
RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", 4096),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.mtp_x }, { m.mtp_h });
|
||||
m.mback = copy(b, g, m.mtp_h, m.mtp_x);
|
||||
RAD_ARCH_TRY(m.mtp.declare_join(b, m.y));
|
||||
}
|
||||
if (l < 3) {
|
||||
auto w = m.dw;
|
||||
@@ -254,7 +315,7 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra
|
||||
s.shared = nm.ckpt("%s.mlp.shared_experts", pre);
|
||||
s.cfg.n_expert = s.cfg.n_expert_all = 288;
|
||||
s.cfg.top_k = 8;
|
||||
s.cfg.n_ff_exp = s.cfg.n_ff_shared = 2048 / g.world;
|
||||
s.cfg.n_ff_exp = s.cfg.n_ff_shared = GlmTp::expert_width(g.world);
|
||||
s.cfg.rows = g.max_tok;
|
||||
s.cfg.sigmoid_router = 1;
|
||||
s.cfg.route_scale = 2.5;
|
||||
@@ -263,8 +324,8 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra
|
||||
s.cfg.router_fuse = 1;
|
||||
s.cfg.swiglu_quant = 1;
|
||||
for (int q = 0; q < 2; ++q) {
|
||||
s.cfg.ff_lo[q] = g.rank * (2048 / g.world);
|
||||
s.cfg.ff_hi[q] = (g.rank + 1) * (2048 / g.world);
|
||||
s.cfg.ff_lo[q] = g.rank * GlmTp::expert_width(g.world);
|
||||
s.cfg.ff_hi[q] = (g.rank + 1) * GlmTp::expert_width(g.world);
|
||||
}
|
||||
RAD_ARCH_TRY(MoeFP8::probe(b, nm, l, s, &s.cfg));
|
||||
RAD_ARCH_TRY(lay.moe.declare(b, nm, g, l, w, s, false, draft));
|
||||
@@ -272,29 +333,16 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra
|
||||
if (!draft) RAD_ARCH_TRY(lay.ffn_hc.declare_post(b));
|
||||
return RAD_OK;
|
||||
}
|
||||
static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) {
|
||||
if (!b || !meta || !ctx || ctx->is_draft || ctx->rank < 0 || ctx->rank >= MAX_RANKS)
|
||||
return RAD_E_INVAL;
|
||||
const int world = ctx->world_size ? ctx->world_size : 1;
|
||||
if ((world != 1 && world != 2 && world != 4 && world != 8) || meta->n_embd != 4096 ||
|
||||
meta->n_layers != 45 || meta->n_vocab != 154880 ||
|
||||
cfg(meta, "num_nextn_predict_layers", 0) != 1 || cfg(meta, "hc_mult", 0) != 4 ||
|
||||
cfg(meta, "n_routed_experts", 0) != 288 || cfg(meta, "index_topk", 0) != 2048 ||
|
||||
ctx->max_spec > 31 || (ctx->kv_dtype && ctx->kv_dtype != RAD_BF16))
|
||||
return RAD_E_UNSUPPORTED;
|
||||
auto& slot = ctx->shape_probe ? probe_model : models[ctx->rank];
|
||||
slot = std::make_unique<Model>();
|
||||
auto& m = *slot;
|
||||
auto& g = m.g;
|
||||
m.nm = Names(ctx->scope);
|
||||
auto& nm = m.nm;
|
||||
|
||||
/* ------------------------------------------------------------------ declare */
|
||||
static int geometry(Geom& g, const RadModelMeta* meta, const RadBuildCtx* ctx, int world) {
|
||||
g.n_layer = 45;
|
||||
g.n_embd = 4096;
|
||||
g.head_dim = 256;
|
||||
g.n_head_all = g.n_head_kv_all = 64;
|
||||
g.n_head = g.n_head_kv = 64 / world;
|
||||
g.n_ff_all = 12288;
|
||||
g.n_ff = 12288 / world;
|
||||
g.head_dim = GlmTp::kMlaDim;
|
||||
g.n_head_all = g.n_head_kv_all = GlmTp::kHeads;
|
||||
g.n_head = g.n_head_kv = GlmTp::heads(world);
|
||||
g.n_ff_all = GlmTp::kDenseFF;
|
||||
g.n_ff = GlmTp::dense_width(world);
|
||||
g.n_vocab_all = 154880;
|
||||
g.n_vocab = shard_extent(154880, world, ctx->rank);
|
||||
g.vocab_off = shard_begin(154880, world, ctx->rank);
|
||||
@@ -311,34 +359,31 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
g.eps = meta->rms_eps;
|
||||
g.wire_exact = !ctx->tp_wire_lossy;
|
||||
g.wire_min_bytes = ctx->tp_wire_min_bytes;
|
||||
if (g.max_tok < 1 || g.max_ctx < 1 || g.max_seqs < 1 || g.eps <= 0) return RAD_E_INVAL;
|
||||
RAD_ARCH_TRY(buffers(b, m));
|
||||
RadKVGroupDecl d{};
|
||||
d.kind = RAD_KV_FULL;
|
||||
d.dtype = RAD_BF16;
|
||||
d.n_head_kv = 1;
|
||||
d.head_dim = 256;
|
||||
m.latent = rad_decl_kv_group(b, nm.f("kv_latent"), &d);
|
||||
d = {};
|
||||
d.kind = RAD_KV_LINEAR;
|
||||
d.dtype = RAD_BF16;
|
||||
d.n_head_kv = 1;
|
||||
d.state_dim[0] = 4 + g.max_spec;
|
||||
d.state_dim[1] = 256;
|
||||
m.tail = rad_decl_kv_group(b, nm.f("kv_index_tail"), &d);
|
||||
d = {};
|
||||
d.kind = RAD_KV_LINEAR;
|
||||
d.dtype = RAD_F32;
|
||||
d.n_head_kv = 64 / world;
|
||||
d.state_dim[0] = d.state_dim[1] = 128;
|
||||
m.state = rad_decl_kv_group(b, nm.f("kv_kda"), &d);
|
||||
d = {};
|
||||
d.kind = RAD_KV_CONV;
|
||||
d.dtype = RAD_BF16;
|
||||
d.n_head_kv = 1;
|
||||
d.head_dim = 3 * 8192 / world;
|
||||
d.conv_width = 4;
|
||||
m.conv = rad_decl_kv_group(b, nm.f("kv_kda_conv"), &d);
|
||||
return g.max_tok < 1 || g.max_ctx < 1 || g.max_seqs < 1 || g.eps <= 0 ? RAD_E_INVAL : RAD_OK;
|
||||
}
|
||||
static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) {
|
||||
if (!b || !meta || !ctx || ctx->is_draft || ctx->rank < 0 || ctx->rank >= MAX_RANKS)
|
||||
return RAD_E_INVAL;
|
||||
const int world = ctx->world_size ? ctx->world_size : 1;
|
||||
if (!GlmTp::world_ok(world) || meta->n_embd != 4096 || meta->n_layers != 45 ||
|
||||
meta->n_vocab != 154880 || cfg(meta, "num_nextn_predict_layers", 0) != 1 ||
|
||||
cfg(meta, "hc_mult", 0) != 4 || cfg(meta, "n_routed_experts", 0) != 288 ||
|
||||
cfg(meta, "index_topk", 0) != 2048 || ctx->max_spec > 31 ||
|
||||
(ctx->kv_dtype && ctx->kv_dtype != RAD_BF16))
|
||||
return RAD_E_UNSUPPORTED;
|
||||
auto& slot = ctx->shape_probe ? probe_model : models[ctx->rank];
|
||||
slot = std::make_unique<Model>();
|
||||
auto& m = *slot;
|
||||
auto& g = m.g;
|
||||
m.nm = Names(ctx->scope);
|
||||
auto& nm = m.nm;
|
||||
RAD_ARCH_TRY(geometry(g, meta, ctx, world));
|
||||
RAD_ARCH_TRY(stream_buffers(b, m));
|
||||
RAD_ARCH_TRY(kda_buffers(b, m));
|
||||
RAD_ARCH_TRY(mla_buffers(b, m));
|
||||
RAD_ARCH_TRY(indexer_buffers(b, m));
|
||||
RAD_ARCH_TRY(ffn_buffers(b, m));
|
||||
RAD_ARCH_TRY(kv_groups(b, m));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("token_embd.weight"),
|
||||
nm.ckpt("model.language_model.embed_tokens.weight")));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("output.weight"), nm.ckpt("lm_head.weight")));
|
||||
@@ -357,7 +402,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
RAD_INT("vocab_offset", g.vocab_off)),
|
||||
RAD_WEIGHTS(m.token)),
|
||||
{}, { m.x });
|
||||
m.embed_ar = reduce(b, g, m.x, true);
|
||||
m.embed_ar = GlmTp::reduce(b, g, m.x, true);
|
||||
m.expand = copy(b, g, m.x, m.h0);
|
||||
m.layers.resize(45);
|
||||
for (int l = 0; l < 45; ++l) RAD_ARCH_TRY(layer(b, m, m.layers[l], nm, l, false));
|
||||
@@ -382,63 +427,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
RAD_WEIGHTS(m.head)),
|
||||
{ m.logit_input }, { m.logits });
|
||||
if (g.max_spec > 0) {
|
||||
auto& dn = m.dnm;
|
||||
m.mtp_x = decl_b(b, dn.f("x"), RAD_BF16, { g.max_tok, 4096 });
|
||||
m.mtp_h = decl_b(b, dn.f("hidden"), RAD_BF16, { g.max_tok, 4096 }, RAD_BUF_PERSIST);
|
||||
m.mtp_e = decl_b(b, dn.f("embedding"), RAD_BF16, { g.max_tok, 4096 });
|
||||
m.mtp_concat = decl_b(b, dn.f("concat"), RAD_BF16, { g.max_tok, 8192 });
|
||||
m.mtp_final = decl_b(b, dn.f("final"), RAD_BF16, { g.max_tok, 4096 });
|
||||
const char* pre = "model.language_model.layers.45";
|
||||
auto weight = [&](const char* n, const char* src) {
|
||||
const char* name = dn.f("%s", n);
|
||||
if (map_copy(b, name, dn.ckpt("%s.%s", pre, src)) < 0) return (rad_weight)0;
|
||||
return decl_w(b, name, RAD_F32, { 4096 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE,
|
||||
grp_layer(45));
|
||||
};
|
||||
m.en = weight("enorm.weight", "enorm.weight");
|
||||
m.hn = weight("hnorm.weight", "hnorm.weight");
|
||||
m.mnorm = weight("output_norm.weight", "shared_head.norm.weight");
|
||||
RAD_ARCH_TRY(map_copy(b, dn.f("eh_proj.weight"), dn.ckpt("%s.eh_proj.weight", pre)));
|
||||
m.eh = decl_w(b, dn.f("eh_proj.weight"), RAD_BF16, { 4096, 8192 }, RAD_ACCESS_PER_TOKEN,
|
||||
RAD_SHARD_NONE, grp_layer(45));
|
||||
m.mprev = rw(b,
|
||||
RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", 4096),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ m.final_hidden }, { m.mtp_h });
|
||||
m.membed = rw(b,
|
||||
RAD_OP(b, "embed_lookup",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n_embd", 4096),
|
||||
RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", "bf16"),
|
||||
RAD_INT("vocab_offset", g.vocab_off)),
|
||||
RAD_WEIGHTS(m.token)),
|
||||
{}, { m.mtp_e });
|
||||
m.membed_ar = reduce(b, g, m.mtp_e, true);
|
||||
m.men = norm(b, g, m.en, m.mtp_e, m.mtp_concat);
|
||||
m.mhn = norm(b, g, m.hn, m.mtp_h, m.mtp_concat);
|
||||
m.meh = rw(b,
|
||||
RAD_OP(b, "gemm_nt",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", 4096),
|
||||
RAD_INT("K", 8192), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(m.eh)),
|
||||
{ m.mtp_concat }, { m.mtp_x });
|
||||
RAD_ARCH_TRY(layer(b, m, m.draft, dn, 45, true));
|
||||
m.mnorm_op = norm(b, g, m.mnorm, m.mtp_x, m.mtp_final);
|
||||
m.mcopy = copy(b, g, m.mtp_final, m.mtp_h);
|
||||
m.mhead =
|
||||
rw(b,
|
||||
RAD_OP(b, "logits_gemm",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n_vocab", g.n_vocab),
|
||||
RAD_INT("n_embd", 4096), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(m.head)),
|
||||
{ m.mtp_h }, { m.logits });
|
||||
RadDrafterDecl dd{};
|
||||
dd.name = "mtp";
|
||||
dd.kind = RAD_DRAFT_SERIAL;
|
||||
dd.depth = g.max_spec;
|
||||
dd.mask_token = -1;
|
||||
RAD_ARCH_TRY(rad_declare_drafter(b, &dd));
|
||||
GlmMtp::Wire tw{ m.final_hidden, m.logits, m.token, m.head };
|
||||
RAD_ARCH_TRY(m.mtp.declare_inputs(b, g, tw));
|
||||
RAD_ARCH_TRY(layer(b, m, m.draft, m.mtp.nm, 45, true));
|
||||
RAD_ARCH_TRY(m.mtp.declare_head(b));
|
||||
m.have_mtp = true;
|
||||
}
|
||||
rad_note(b,
|
||||
@@ -446,6 +438,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
world, m.have_mtp ? "enabled" : "disabled");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ step */
|
||||
static void attention(RadCtx* c, Layer& l, const RadBatch* batch) {
|
||||
l.norm.step(c, batch->n_tok);
|
||||
if (l.full) {
|
||||
@@ -455,6 +449,24 @@ static void attention(RadCtx* c, Layer& l, const RadBatch* batch) {
|
||||
} else
|
||||
l.kda.step(c, batch);
|
||||
}
|
||||
static void draft_pass(RadCtx* c, Model& m, const RadBatch* batch) {
|
||||
if (!m.have_mtp || batch->draft_pass == 1) {
|
||||
rad_step_fail(c, "GLM MTP pass is not declared or has invalid round 1");
|
||||
return;
|
||||
}
|
||||
const int64_t T = batch->n_tok;
|
||||
m.mtp.step_inputs(c, batch);
|
||||
auto& l = m.draft;
|
||||
l.norm.step(c, T);
|
||||
l.mla.prepare(c, batch);
|
||||
l.index.step(c, batch);
|
||||
// A history pass with no proposal row fills the draft caches and ends there.
|
||||
if (batch->draft_pass < 0 && batch->n_draft_out <= 0) return;
|
||||
l.mla.finish(c, batch);
|
||||
m.mtp.step_join(c, batch, m.y);
|
||||
l.moe.step_rows(c, GlmMtp::head_rows(batch));
|
||||
m.mtp.step_head(c, batch);
|
||||
}
|
||||
static void step(RadCtx* c, const RadBatch* batch) {
|
||||
auto& m = *models[rad_rank(c)];
|
||||
const int64_t T = batch->n_tok;
|
||||
@@ -463,36 +475,7 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
return;
|
||||
}
|
||||
if (batch->draft_pass) {
|
||||
if (!m.have_mtp || batch->draft_pass == 1) {
|
||||
rad_step_fail(c, "GLM MTP pass is not declared or has invalid round 1");
|
||||
return;
|
||||
}
|
||||
if (batch->draft_pass < 0 && batch->n_out > 0)
|
||||
RAD_ISSUE_N(c, m.mprev, batch->n_out, RAD_B(m.final_hidden),
|
||||
praw(batch->out_ids, RAD_I32, batch->n_out), brows(m.mtp_h, batch->n_out));
|
||||
RAD_ISSUE_N(c, m.membed, T, praw(batch->token_ids, RAD_I32, T), RAD_W(m.token),
|
||||
brows(m.mtp_e, T));
|
||||
if (m.membed_ar) RAD_ISSUE_N(c, m.membed_ar, T * 4096, brows(m.mtp_e, T), RAD_NONE);
|
||||
RAD_ISSUE_N(c, m.men, T, brows(m.mtp_e, T), RAD_W(m.en), bcol(m.mtp_concat, 0, 4096, T));
|
||||
RAD_ISSUE_N(c, m.mhn, T, brows(m.mtp_h, T), RAD_W(m.hn), bcol(m.mtp_concat, 4096, 4096, T));
|
||||
RAD_ISSUE_N(c, m.meh, T, brows(m.mtp_concat, T), RAD_W(m.eh), brows(m.mtp_x, T));
|
||||
auto& l = m.draft;
|
||||
l.norm.step(c, T);
|
||||
l.mla.prepare(c, batch);
|
||||
l.index.step(c, batch);
|
||||
if (batch->draft_pass < 0 && batch->n_draft_out <= 0) return;
|
||||
l.mla.finish(c, batch);
|
||||
RAD_ISSUE_N(c, l.residual, T, brows(m.mtp_x, T), brows(m.y, T), brows(m.mtp_x, T));
|
||||
const int64_t L = batch->draft_pass < 0 ? batch->n_draft_out : T;
|
||||
if (batch->draft_pass < 0) {
|
||||
RAD_ISSUE_N(c, m.mselect, L, RAD_B(m.mtp_x), praw(batch->draft_out_ids, RAD_I32, L),
|
||||
brows(m.mtp_h, L));
|
||||
RAD_ISSUE_N(c, m.mback, L, brows(m.mtp_h, L), brows(m.mtp_x, L));
|
||||
}
|
||||
l.moe.step_rows(c, L);
|
||||
RAD_ISSUE_N(c, m.mnorm_op, L, brows(m.mtp_x, L), RAD_W(m.mnorm), brows(m.mtp_final, L));
|
||||
RAD_ISSUE_N(c, m.mcopy, L, brows(m.mtp_final, L), brows(m.mtp_h, L));
|
||||
RAD_ISSUE_N(c, m.mhead, L, brows(m.mtp_h, L), RAD_W(m.head), brows(m.logits, L));
|
||||
draft_pass(c, m, batch);
|
||||
return;
|
||||
}
|
||||
RAD_ISSUE_N(c, m.embed, T, praw(batch->token_ids, RAD_I32, T), RAD_W(m.token), brows(m.x, T));
|
||||
@@ -523,7 +506,7 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
}
|
||||
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
if (!meta || !out) return RAD_E_INVAL;
|
||||
if (cfg(meta, "num_nextn_predict_layers", 0) == 1) out->draft_depth = 3;
|
||||
if (cfg(meta, "num_nextn_predict_layers", 0) == 1) out->draft_depth = GlmMtp::kDepth;
|
||||
out->shape_probe_ok = 1;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
+20
-5
@@ -415,6 +415,19 @@ the plugin claims the empty descriptor and reads each weight's encoding from the
|
||||
mapped, so never converted, and an image request fails its step. The reference is SGLang's
|
||||
`glm5_next.py` and `glm5_next_nextn.py` at AMD's pinned commit 654df43.
|
||||
|
||||
The plugin is one file over blocks in `arch/common`, one block a file; it declares the buffers
|
||||
the blocks share, places them and issues the step's order.
|
||||
|
||||
| file | block | what |
|
||||
|---|---|---|
|
||||
| `rad_block_glm_mhc.h` | `ManifoldConn` | the four-stream residual's read and write |
|
||||
| `rad_block_glm_kda.h` | `GlmKda`, `KdaState` | the 34 linear layers and their recurrence |
|
||||
| `rad_block_glm_mla.h` | `GlmMla`, `MlaLatent` | the 11 sparse MLA layers and their absorbed core |
|
||||
| `rad_block_glm_indexer.h` | `GlmIndexer`, `DsaCache` | the DSA indexer, its key cache and selection |
|
||||
| `rad_block_glm_router.h` | `GlmRouter` | the correction-bias router, through `MoeFP8` |
|
||||
| `rad_block_glm_mtp.h` | `GlmMtp` | the draft head around the plugin's layer 45 |
|
||||
| `rad_glm_tp.h` | `GlmTp`, `SplitF32Proj` | the shards, the split threshold, the column split of a replicated f32 projection |
|
||||
|
||||
```
|
||||
45 layers, hidden 4096, vocab 154880 (embedding and lm_head separate, bf16)
|
||||
mHC: 4 residual streams, a 16384-wide row; 20 Sinkhorn passes, hc_eps 1e-6; the embedding is
|
||||
@@ -446,7 +459,7 @@ Traps:
|
||||
- **`comb` is indexed `[source, destination]`.** Each connection's write is
|
||||
`out[dst] = post[dst] * y + sum_src comb[src, dst] * h[src]`; the transpose has every shape
|
||||
valid and is another model. `out` must be a separate buffer: each output stream reads every
|
||||
input stream (`rad_block_mhc.h`).
|
||||
input stream (`rad_block_glm_mhc.h`).
|
||||
- **The router and the index head gates stay f32.** Both projections are f32 weights against a
|
||||
bf16 activation with f32 results (`gemm_nt_f32`), and the routing weights reach the gather
|
||||
as f32. Rounding any of them to bf16 changes which experts and which blocks are picked, and a
|
||||
@@ -504,9 +517,11 @@ The container holds 25845 weights, 171.32 GiB; at eight ranks each card holds 22
|
||||
3 unless `--num-speculative-tokens` says otherwise (at most 31). Its history pass writes the draft
|
||||
layer's latent, index and tail for every row and runs its MoE on the proposal rows only.
|
||||
|
||||
**Tensor parallelism.** At 1, 2, 4 or 8 ranks; declare refuses any other. `arch_test` declares and
|
||||
steps the whole plugin at 1, 4 and 8, and the connection, router and indexer splits at 1, 2, 4
|
||||
and 8.
|
||||
**Tensor parallelism.** At 1, 2, 4 or 8 ranks; declare refuses any other. `tests/glm5next_test.cpp`
|
||||
declares and steps the whole plugin at 1, 4 and 8, against pinned graph and step hashes at 1 and
|
||||
8, the connection, router and index-gate splits at 1, 2, 4 and 8, each issue's operand count
|
||||
against the loaded kernel libraries' schemas, and every declared weight against the served
|
||||
container's names and encodings.
|
||||
|
||||
- The routed and the shared experts split their 2048 width into equal contiguous slices, 256 a
|
||||
rank at eight; each rank computes its slice of every expert, and the block's all-reduce sums
|
||||
@@ -519,7 +534,7 @@ and 8.
|
||||
- The vocabulary is row-sharded, 19360 rows a rank at eight; the embedding's all-reduce is exact.
|
||||
- The f32 projections (the router's 288 x 4096, the index head gates' 32 x 4096, each
|
||||
connection's mHC projection of 24 x 16384) are replicated. From a pass of 256 rows
|
||||
(`kGlmTpSplitRows`, `arch/common/rad_tp_split.h`), each rank computes a disjoint part
|
||||
(`GlmTp::kSplitRows`; `SplitF32Proj` in `arch/common/rad_glm_tp.h`), each rank computes a disjoint part
|
||||
with the replicated kernel and an `all_gather` joins the parts, so every value is
|
||||
bit-identical: the router and the head gates split their output columns, the mHC projection
|
||||
its 16384-wide input into eight 2048-column chunks, whose partials `mhc_pre_finish` completes
|
||||
|
||||
+13
-9
@@ -10,11 +10,12 @@
|
||||
# fast every test with none of slow, gpu, oot and perf, set at the end of this file: the
|
||||
# inner loop (`ctest --preset fast`, or `ctest -L fast`)
|
||||
#
|
||||
# arch_test does not link rad_core: it supplies its own recording fake of the declare-phase C ABI,
|
||||
# so plugins are tested without depending on core/build/'s builder. With the archive linked, a new
|
||||
# ABI call missing from the fake would pull in rad_builder.cpp.o and collide on every rad_decl_*
|
||||
# symbol; without it the error is a single undefined reference.
|
||||
set(RAD_TESTS_NO_CORE arch_test)
|
||||
# arch_test and glm5next_test do not link rad_core: they supply their own recording fake of the
|
||||
# declare-phase C ABI (arch_fakes.h), so plugins are tested without depending on core/build/'s
|
||||
# builder. With the archive linked, a new ABI call missing from the fake would pull in
|
||||
# rad_builder.cpp.o and collide on every rad_decl_* symbol; without it the error is a single
|
||||
# undefined reference.
|
||||
set(RAD_TESTS_NO_CORE arch_test glm5next_test)
|
||||
|
||||
# fixture_test tests tools/kfixture.cpp and tools/opshapes.cpp (the seeded draw, the reduction and
|
||||
# the on-disk recording) directly, kbench_test tools/kbench_core.cpp (rad-kbench's rules), and
|
||||
@@ -273,10 +274,13 @@ if(TARGET abi_layout_test)
|
||||
endif()
|
||||
|
||||
# arch_test reads libref's vocabulary from $RADIANCE_HOME/kernels to check that each architecture
|
||||
# declares on it, so libref is built before it.
|
||||
if(TARGET libref AND TARGET arch_test)
|
||||
add_dependencies(arch_test libref)
|
||||
endif()
|
||||
# declares on it, and glm5next_test the loaded libraries' op schemas, so libref is built before
|
||||
# them.
|
||||
foreach(t arch_test glm5next_test)
|
||||
if(TARGET libref AND TARGET ${t})
|
||||
add_dependencies(${t} libref)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# kbench_mutant_test runs rad-kbench against the deliberately defective kernel libraries
|
||||
# (core/plugin/testplugin/kbmutant.cc) on the host backend, so it needs the tool, libref and the
|
||||
|
||||
+1177
File diff suppressed because it is too large.
Load diff
+2
-2345
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user