glm5next: --kv-cache-dtype fp8 narrows the latent attention cache to E4M3

The plugin refused fp8. It now takes bf16 or fp8 and applies it to kv_latent alone: Geom::kv_dtype
is the configured cache dtype, kv_latent is declared in it, and GlmMla's kv_store and
kv_store_streamed name it, as does every mla_attn over the latent (head form, row form and the
MTP layer's), which then resolves to the fp8kv rows. The store keeps kv_store's default descale of
1, which the attention reads, as vLLM's fp8 cache does. kv_index_tail, kv_dsa_index and the KDA
groups keep their dtypes; the indexer reads only the latent group's tables. Host-tier, streamed
and replicated sizing follow the group dtype already. Under bf16 the attention states no
kv_dtype and the graph is the one declared before.

At 4 x 262144 tokens the latent cache is 12 layers of 512 bytes a token instead of 1024.

glm5next_test: fp8 declares the latent group E4M3 and the other four unchanged, every latent store
and attention names fp8_e4m3, and bf16 (stated or default) leaves the attention without kv_dtype;
the refusal test now refuses f16.
This commit is contained in:
AnimusNull authored and AnimusNull committed 2026-11-05 14:13:11 +00:00
1 parent f776ecbf2e
commit d858aa722b
3 files changed
+107 -30

No files matched your search

+30 -22
View File
@@ -19,6 +19,7 @@
#include "rad_glm_tp.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
namespace rad {
@@ -27,6 +28,25 @@ struct MlaLatent {
struct Config {
int64_t heads = 16, head_dim = 256, latent_dim = 512, blocks = 513;
};
/* The attention over the latent cache, `rows` rows of `heads` heads. The cache is Geom::kv_dtype
* (bf16, or fp8_e4m3 under --kv-cache-dtype fp8); `kv_dtype` is stated only for a cache that is
* not bf16, so a bf16 build declares the op it always did. */
static rad_op attn_op(RadBuilder* b, const Geom& g, int64_t rows, int64_t heads,
int64_t blocks, double scale) {
if (g.kv_dtype && std::strcmp(g.kv_dtype, "bf16"))
return RAD_OP(b, "mla_attn",
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("latent_dim", 512),
RAD_INT("heads", heads), RAD_INT("blocks", blocks),
RAD_INT("block_size", 4), RAD_F64("scale", scale),
RAD_STR("dtype", "bf16"), RAD_STR("kv_dtype", g.kv_dtype)),
RAD_NOWEIGHTS);
return RAD_OP(b, "mla_attn",
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("latent_dim", 512),
RAD_INT("heads", heads), RAD_INT("blocks", blocks),
RAD_INT("block_size", 4), RAD_F64("scale", scale),
RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS);
}
struct Wire {
rad_buf q = 0, q_latent = 0, attn_latent = 0, out = 0, selected = 0, length = 0;
/* qsa_select's entry count (selected_count), one int a row: the streamed resolve's `nsel`.
@@ -68,16 +88,10 @@ struct MlaLatent {
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 });
op_attn = rw(b,
attn_op(b, g, g.max_tok, c.heads, c.blocks,
1.0 / std::sqrt((double)c.head_dim)),
{ 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,
@@ -172,12 +186,13 @@ struct GlmMla {
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.
/* Cache stores contiguous halves of the normalized latent, with no duplicated K/V. An E4M3
* cache (Geom::kv_dtype) stores at kv_store's default descale of 1, which mla_attn reads. */
op_store = rw(b,
RAD_OP(b, "kv_store",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", 256),
RAD_INT("n_head_kv", 1), RAD_INT("block_size", 4),
RAD_STR("kv_dtype", "bf16")),
RAD_STR("kv_dtype", g.kv_dtype)),
RAD_NOWEIGHTS),
{ w.latent }, {});
if (!op_store) return RAD_E_INVAL;
@@ -200,7 +215,7 @@ struct GlmMla {
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok),
RAD_INT("head_dim", 256), RAD_INT("n_head_kv", 1),
RAD_INT("block_size", 4),
RAD_STR("kv_dtype", "bf16")),
RAD_STR("kv_dtype", g.kv_dtype)),
RAD_NOWEIGHTS),
{ w.latent }, {});
/* Issued in finish, after this layer's indexer selects and before the attention reads
@@ -239,15 +254,8 @@ struct GlmMla {
{ in }, { out });
};
op_rows_absorb = project("absorb", r.q, r.q_latent);
op_rows_attn =
rw(b,
RAD_OP(b, "mla_attn",
RAD_PARAMS(RAD_RANGE("M", 1, R), RAD_INT("latent_dim", 512),
RAD_INT("heads", 64), RAD_INT("blocks", core.c.blocks),
RAD_INT("block_size", 4), RAD_F64("scale", 1.0 / std::sqrt(256.0)),
RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS),
{ r.q_latent, w.core.selected, w.core.length }, { r.attn_latent });
op_rows_attn = rw(b, MlaLatent::attn_op(b, g, R, 64, core.c.blocks, 1.0 / std::sqrt(256.0)),
{ r.q_latent, w.core.selected, w.core.length }, { r.attn_latent });
op_rows_expand = project("expand", r.attn_latent, r.attn.x);
RAD_ARCH_TRY(rows_quant.declare(b, g, r.attn, 16384));
rows_output.stored = nm.f("blk.%d.attn_output.weight", layer);
+11 -6
View File
@@ -271,7 +271,8 @@ static int kv_groups(RadBuilder* b, Model& m) {
auto& nm = m.nm;
RadKVGroupDecl d{};
d.kind = RAD_KV_FULL;
d.dtype = RAD_BF16;
// bf16, or E4M3 under --kv-cache-dtype fp8 (geometry); the other groups keep their own.
d.dtype = rad_dtype_parse(g.kv_dtype);
d.n_head_kv = 1;
d.head_dim = 256;
m.latent = rad_decl_kv_group(b, nm.f("kv_latent"), &d);
@@ -435,6 +436,9 @@ static int geometry(Geom& g, const RadModelMeta* meta, const RadBuildCtx* ctx, i
g.max_logit_rows = std::min<int64_t>(g.max_tok, g.max_seqs * (g.max_spec + 1));
if (ctx->max_out_rows > g.max_logit_rows) g.max_logit_rows = ctx->max_out_rows;
g.eps = meta->rms_eps;
/* --kv-cache-dtype names the latent cache's dtype only (kv_groups); declare refuses all but
* bf16 and fp8. */
g.kv_dtype = rad_dtype_name(rad_kv_cache_dtype(ctx, RAD_BF16));
g.wire_exact = !ctx->tp_wire_lossy;
g.wire_min_bytes = ctx->tp_wire_min_bytes;
return g.max_tok < 1 || g.max_ctx < 1 || g.max_seqs < 1 || g.eps <= 0 ? RAD_E_INVAL : RAD_OK;
@@ -473,11 +477,12 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
ctx->max_spec);
return RAD_E_UNSUPPORTED;
}
if (ctx->kv_dtype && ctx->kv_dtype != RAD_BF16) {
fprintf(stderr, "radiance: glm5next: --kv-cache-dtype %s; this plugin keeps its latent "
"attention, index tail and KDA caches in bf16 and has no fp8 cache, so "
"run it with --kv-cache-dtype bf16\n",
ctx->kv_dtype == RAD_F8E4M3 ? "fp8" : rad_dtype_name(ctx->kv_dtype));
/* fp8 narrows the latent attention cache to E4M3 at a descale of 1 (kv_groups); the index
* tail, the DSA index and the KDA caches keep their dtypes. */
if (ctx->kv_dtype && ctx->kv_dtype != RAD_BF16 && ctx->kv_dtype != RAD_F8E4M3) {
fprintf(stderr, "radiance: glm5next: --kv-cache-dtype %s; this plugin serves its latent "
"attention cache in bf16 or fp8 only\n",
rad_dtype_name(ctx->kv_dtype));
return RAD_E_UNSUPPORTED;
}
auto& slot = ctx->shape_probe ? probe_model : models[ctx->rank];
+66 -2
View File
@@ -1024,8 +1024,8 @@ TEST(glm53_refusals_name_what_was_refused) {
return err;
};
RadBuildCtx c = ok;
c.kv_dtype = RAD_F8E4M3;
CHECK(has(refusal(meta, c), "--kv-cache-dtype fp8"));
c.kv_dtype = RAD_F16;
CHECK(has(refusal(meta, c), "--kv-cache-dtype f16"));
c = ok;
c.max_spec = 32;
CHECK(has(refusal(meta, c), "--num-speculative-tokens 32"));
@@ -1055,6 +1055,70 @@ TEST(glm53_refusals_name_what_was_refused) {
}
}
/* --kv-cache-dtype fp8 narrows the latent attention cache alone to E4M3: its group, every latent
* store (plain and streamed) and every latent attention (head form, row form, the MTP layer's)
* name fp8_e4m3, and the index tail, DSA index and KDA groups keep their dtypes. Under bf16 the
* attention states no kv_dtype, the op it always declared. */
TEST(glm53_fp8_kv_cache_narrows_the_latent_group_only) {
const char* keys[] = { "num_nextn_predict_layers", "hc_mult", "n_routed_experts",
"index_topk" };
const char* vals[] = { "1", "4", "288", "2048" };
RadModelMeta meta{};
meta.n_layers = 45;
meta.n_embd = 4096;
meta.n_vocab = 154880;
meta.rms_eps = 1e-5f;
meta.n_kv = 4;
meta.kv_key = keys;
meta.kv_val = vals;
const auto mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
const auto fp8 = rad_enc_affine(RAD_F8E4M3, RAD_F32, 128, 128);
for (int kv_dtype : { (int)RAD_DT_INVALID, (int)RAD_BF16, (int)RAD_F8E4M3 }) {
const bool narrow = kv_dtype == RAD_F8E4M3;
RadBuilder b;
b.kv_block = 4;
b.encs = { { "mtp.blk.45.ffn_gate_up_exps", fp8 },
{ "mtp.blk.45.ffn_down_exps", fp8 },
{ "ffn_gate_up_exps", mx },
{ "ffn_down_exps", mx },
{ "ffn_gate_up_shexp", mx },
{ "ffn_down_shexp", mx },
{ "ffn_gate_up.weight", fp8 },
{ "ffn_down.weight", fp8 } };
RadBuildCtx build{};
build.rank = 0;
build.world_size = 4;
build.max_tok = 512;
build.max_seqs = 4;
build.max_ctx = 4096;
build.max_spec = 3;
build.tp_attn_rows = 1;
build.kv_dtype = kv_dtype;
REQUIRE_EQ(glm5next::declare(&b, &meta, &build), RAD_OK);
std::map<std::string, uint32_t> dtypes;
for (const auto& g : b.kv_groups) dtypes[g.name] = g.d.dtype;
CHECK_EQ(dtypes["kv_latent"], narrow ? (uint32_t)RAD_F8E4M3 : (uint32_t)RAD_BF16);
CHECK_EQ(dtypes["kv_index_tail"], (uint32_t)RAD_BF16);
CHECK_EQ(dtypes["kv_dsa_index"], (uint32_t)RAD_U8);
CHECK_EQ(dtypes["kv_kda"], (uint32_t)RAD_F32);
CHECK_EQ(dtypes["kv_kda_conv"], (uint32_t)RAD_BF16);
int stores = 0, attns = 0;
for (const auto& o : b.ops) {
if (o.op == "kv_store" || o.op == "kv_store_streamed") {
++stores;
CHECK_EQ(pstr(&o, "kv_dtype"), narrow ? "fp8_e4m3" : "bf16");
}
if (o.op == "mla_attn") {
++attns;
CHECK_EQ(pstr(&o, "kv_dtype"), narrow ? "fp8_e4m3" : "<absent>");
}
}
// 11 MLA layers and the MTP layer, each a store and a head-form attention; 11 row forms.
CHECK(stores >= 12);
CHECK_EQ(attns, 12 + 11);
}
}
/* The MoE block's decode fusions: over a two-row trunk step, its draft pass and a 512-row prefill
* (past the router fusion's one-workgroup sort), each fused launch stands where it fits and the
* launches it replaces are gone, with the step lint clean throughout. The sort writes each slot's