Merge branch 'main' into fix-stage-drain

This commit is contained in:
C_Tamas committed 2026-10-25 14:19:31 +01:00
commit bcdc89eded
43 files changed
+3197 -157

No files matched your search

+647 -13
View File
@@ -50,6 +50,11 @@
#undef QWEN35_NS
#undef QWEN35_ARCH_ID
#include "../arch/qwen35_w8a8/qwen35_w8a8.cpp"
/* Tencent Hy-Memory: FP8 full attention with Q/K norm and sigmoid-correction MoE. */
#include "../arch/hyv3_fp8/hyv3_fp8.cpp"
#include "../arch/hyv3_bf16/hyv3_bf16.cpp"
#include "../arch/hyv3_gguf/hyv3_gguf.cpp"
#include "hyv3_bf16_fixture.h"
/* The LLaMA family from a dense checkpoint: llama_fp8 under its other descriptor. */
#undef LLAMA_DENSE_SRC
#undef LLAMA_NS
@@ -348,6 +353,484 @@ TEST(llama_reads_its_rotary_scaling_by_type) {
}
}
TEST(fp8_qk_attention_normalizes_before_neox_rope) {
using namespace rad::arch;
RadModelMeta meta{};
meta.arch_id = "hyv3"; meta.name = "test-hyv3"; meta.quant = "fp8";
meta.n_layers = 1; meta.n_embd = 2048; meta.n_head = 32; meta.n_head_kv = 4;
meta.head_dim = 128; meta.n_ff = 6912; meta.n_vocab = 120832;
meta.n_ctx_train = 262144; meta.rms_eps = 1e-5f; meta.rope_theta = 11158840.0f;
RadBuildCtx ctx = build_ctx();
Geom g{};
CHECK_OK(geom_from(g, &meta, &ctx, "bf16", RAD_BF16));
RadBuilder b;
Names nm("");
RadKVGroupDecl kd{};
kd.kind = RAD_KV_FULL; kd.dtype = RAD_BF16; kd.n_head_kv = g.n_head_kv;
kd.head_dim = g.head_dim;
const rad_kvgroup kv = rad_decl_kv_group(&b, "kv_attn", &kd);
CHECK_OK(rad_bind_layer_kv(&b, 0, kv));
AttnQKFP8::Wire w{};
w.x = decl_b(&b, nm.f("x"), RAD_BF16, {g.max_tok, g.n_embd});
w.h.x = decl_b(&b, nm.f("h"), RAD_BF16, {g.max_tok, g.n_embd});
CHECK_OK(w.h.declare_qs(&b, nm, g, "h", g.n_embd));
w.q = decl_b(&b, nm.f("q"), RAD_BF16, {g.max_tok, g.q_dim()});
w.k = decl_b(&b, nm.f("k"), RAD_BF16, {g.max_tok, g.kv_dim()});
w.v = decl_b(&b, nm.f("v"), RAD_BF16, {g.max_tok, g.kv_dim()});
w.attn.x = decl_b(&b, nm.f("attn"), RAD_BF16, {g.max_tok, g.q_dim()});
CHECK_OK(w.attn.declare_qs(&b, nm, g, "attn", g.q_dim()));
const rad_op init = rw(&b, RAD_OP(&b, "fill",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_STR("dtype", g.dtype), RAD_F64("value", 0.0)),
RAD_NOWEIGHTS),
{}, {w.x});
AttnQKFP8::Src src{};
src.norm = nm.ckpt("model.layers.0.input_layernorm.weight");
src.q = nm.ckpt("model.layers.0.self_attn.q_proj.weight");
src.k = nm.ckpt("model.layers.0.self_attn.k_proj.weight");
src.v = nm.ckpt("model.layers.0.self_attn.v_proj.weight");
src.q_norm = nm.ckpt("model.layers.0.self_attn.q_norm.weight");
src.k_norm = nm.ckpt("model.layers.0.self_attn.k_norm.weight");
src.o = nm.ckpt("model.layers.0.self_attn.o_proj.weight");
AttnQKFP8 attn;
CHECK_OK(attn.declare(&b, nm, g, 0, kv, w, src));
const RecWeight* qw = find_w(b, "blk.0.attn_q.weight");
const RecWeight* kw = find_w(b, "blk.0.attn_k.weight");
const RecWeight* qn = find_w(b, "blk.0.attn_q_norm.weight");
CHECK(qw && kw && qn);
CHECK_EQ(qw->d.shape[0], 4096); CHECK_EQ(qw->d.shape[1], 2048);
CHECK_EQ(kw->d.shape[0], 512); CHECK_EQ(kw->d.shape[1], 2048);
CHECK_EQ(qn->d.shape[0], 128); CHECK_EQ(qn->d.dtype, (uint32_t)RAD_F32);
CHECK_EQ(qn->d.shard, RAD_SHARD_NONE);
int q_norms = 0, k_norms = 0;
for (const auto& op : b.ops) if (op.op == "rmsnorm" && pint(&op, "n") == 128) {
if (op.w.size() == 1 && op.w[0] == attn.w_q_norm) ++q_norms;
if (op.w.size() == 1 && op.w[0] == attn.w_k_norm) ++k_norms;
}
CHECK_EQ(q_norms, 1); CHECK_EQ(k_norms, 1);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
RAD_ISSUE(&run, init, brows(w.x, batch.n_tok));
attn.step(&run, &batch);
auto issue_of = [&](rad_op h) {
for (size_t i = 0; i < run.issues.size(); ++i)
if (run.issues[i].op == h) return (int)i;
return -1;
};
const int q_norm_issue = issue_of(attn.op_q_norm), k_norm_issue = issue_of(attn.op_k_norm);
const int q_rope_issue = issue_of(attn.op_rope_q), k_rope_issue = issue_of(attn.op_rope_k);
const int attn_issue = issue_of(attn.op_attn);
CHECK(q_norm_issue >= 0); CHECK(k_norm_issue >= 0); CHECK(attn_issue >= 0);
CHECK(q_rope_issue >= 0); CHECK(k_rope_issue >= 0);
CHECK(q_norm_issue < q_rope_issue); CHECK(k_norm_issue < k_rope_issue);
CHECK(q_rope_issue < attn_issue); CHECK(k_rope_issue < attn_issue);
const auto lint = lint_step(b, {&run});
CHECK(lint.empty());
}
static RadModelMeta hyv3_fp8_test_meta() {
static const char* keys[] = {
"qk_norm", "moe_router_use_sigmoid", "moe_router_enable_expert_bias", "route_norm",
"enable_moe_fp32_combine", "attention_bias", "mlp_bias", "first_k_dense_replace",
"num_experts", "num_experts_per_tok", "num_shared_experts", "expert_hidden_dim",
"moe_intermediate_size", "router_scaling_factor", "mlp_layer_types",
"hyv3.kv_cache.0.k_scale", "hyv3.kv_cache.0.v_scale",
"hyv3.kv_cache.1.k_scale", "hyv3.kv_cache.1.v_scale",
};
static const char* vals[] = {
"1", "1", "1", "1", "1", "0", "0", "1", "128", "8", "1", "768", "1536",
"2.826", "dense sparse", "0.0376674123", "0.0004359654",
"0.0224609375", "0.0005318778",
};
RadModelMeta meta{};
meta.arch_id = "hyv3"; meta.name = "test-hyv3"; meta.quant = "fp8";
meta.n_layers = 2; meta.n_embd = 2048; meta.n_head = 32; meta.n_head_kv = 4;
meta.head_dim = 128; meta.n_ff = 6912; meta.n_vocab = 120832;
meta.n_ctx_train = 262144; meta.rms_eps = 1e-5f; meta.rope_theta = 11158840.0f;
meta.n_expert = 128; meta.n_expert_used = 8;
meta.n_kv = (int)(sizeof(keys) / sizeof(keys[0])); meta.kv_key = keys; meta.kv_val = vals;
return meta;
}
TEST(hyv3_bf16_declares_checkpoint_geometry_and_plain_moe) {
RadModelMeta meta = hyv3_fp8_test_meta();
meta.quant = "";
RadArchProbe probe{};
CHECK_OK(hyv3_bf16::probe(&meta, &probe));
RadBuilder b;
const RadEncoding plain = rad_enc_plain(RAD_BF16);
b.encs = {
{ "attn_q.weight", plain }, { "attn_k.weight", plain },
{ "attn_v.weight", plain }, { "attn_output.weight", plain },
{ "ffn_gate_up.weight", plain }, { "ffn_down.weight", plain },
{ "ffn_gate_up_exps", plain }, { "ffn_down_exps", plain },
{ "ffn_gate_up_shexp.weight", plain }, { "ffn_down_shexp.weight", plain },
{ "output.weight", plain },
};
RadBuildCtx ctx = build_ctx();
const int rc = hyv3_bf16::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) return;
const RecWeight* q_norm = find_w(b, "blk.0.attn_q_norm.weight");
const RecWeight* k_norm = find_w(b, "blk.0.attn_k_norm.weight");
const RecWeight* q = find_w(b, "blk.0.attn_q.weight");
const RecWeight* k = find_w(b, "blk.0.attn_k.weight");
const RecWeight* v = find_w(b, "blk.0.attn_v.weight");
const RecWeight* o = find_w(b, "blk.0.attn_output.weight");
const RecWeight* dense = find_w(b, "blk.0.ffn_gate_up.weight");
const RecWeight* dense_down = find_w(b, "blk.0.ffn_down.weight");
const RecWeight* expert = find_w(b, "blk.1.ffn_gate_up_exps.0.weight");
const RecWeight* expert_down = find_w(b, "blk.1.ffn_down_exps.0.weight");
const RecWeight* shared = find_w(b, "blk.1.ffn_gate_up_shexp.weight");
const RecWeight* shared_down = find_w(b, "blk.1.ffn_down_shexp.weight");
const RecWeight* router = find_w(b, "blk.1.glm_router.weight");
const RecWeight* correction = find_w(b, "blk.1.glm_router.correction");
REQUIRE(q_norm && k_norm && q && k && v && o && dense && dense_down && expert &&
expert_down && shared && shared_down && router && correction);
CHECK_EQ(q_norm->d.shape[0], hyv3_bf16_fixture::q_norm[0]);
CHECK_EQ(k_norm->d.shape[0], hyv3_bf16_fixture::k_norm[0]);
CHECK_EQ(router->d.shape[0], hyv3_bf16_fixture::router[0]);
CHECK_EQ(router->d.shape[1], hyv3_bf16_fixture::router[1]);
CHECK_EQ(correction->d.shape[0], hyv3_bf16_fixture::correction[0]);
CHECK_EQ(q->d.shape[0], hyv3_bf16_fixture::q_proj[0]);
CHECK_EQ(q->d.shape[1], hyv3_bf16_fixture::q_proj[1]);
CHECK_EQ(k->d.shape[0], hyv3_bf16_fixture::k_proj[0]);
CHECK_EQ(k->d.shape[1], hyv3_bf16_fixture::k_proj[1]);
CHECK_EQ(v->d.shape[0], hyv3_bf16_fixture::v_proj[0]);
CHECK_EQ(v->d.shape[1], hyv3_bf16_fixture::v_proj[1]);
CHECK_EQ(o->d.shape[0], hyv3_bf16_fixture::o_proj[0]);
CHECK_EQ(o->d.shape[1], hyv3_bf16_fixture::o_proj[1]);
CHECK_EQ(dense->d.shape[0], hyv3_bf16_fixture::dense_gate[0] +
hyv3_bf16_fixture::dense_up[0]);
CHECK_EQ(dense->d.shape[1], hyv3_bf16_fixture::dense_gate[1]);
CHECK_EQ(dense_down->d.shape[0], hyv3_bf16_fixture::dense_down[0]);
CHECK_EQ(dense_down->d.shape[1], hyv3_bf16_fixture::dense_down[1]);
CHECK_EQ(expert->d.shape[0], hyv3_bf16_fixture::expert_gate[0] +
hyv3_bf16_fixture::expert_up[0]);
CHECK_EQ(expert->d.shape[1], hyv3_bf16_fixture::expert_gate[1]);
CHECK_EQ(expert_down->d.shape[0], hyv3_bf16_fixture::expert_down[0]);
CHECK_EQ(expert_down->d.shape[1], hyv3_bf16_fixture::expert_down[1]);
CHECK_EQ(shared->d.shape[0], hyv3_bf16_fixture::shared_gate[0] +
hyv3_bf16_fixture::shared_up[0]);
CHECK_EQ(shared->d.shape[1], hyv3_bf16_fixture::shared_gate[1]);
CHECK_EQ(shared_down->d.shape[0], hyv3_bf16_fixture::shared_down[0]);
CHECK_EQ(shared_down->d.shape[1], hyv3_bf16_fixture::shared_down[1]);
const auto& moe = hyv3_bf16::models[0].layers[1].moe;
CHECK_EQ(moe.c.n_ff_exp, 768);
CHECK_EQ(moe.c.n_ff_shared, 768);
CHECK_EQ(moe.c.expert_bf16, 1);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
hyv3_bf16::step(&run, &batch);
CHECK(lint_step(b, {&run}).empty());
}
TEST(hyv3_gguf_maps_llamacpp_moe_tensors_and_lints_tp2) {
RadModelMeta meta = hyv3_fp8_test_meta();
meta.quant = "gguf_ftype_15";
RadArchProbe probe{};
CHECK_OK(hyv3_gguf::probe(&meta, &probe));
const RadEncoding plain = rad_enc_plain(RAD_BF16);
for (int rank = 0; rank < 2; ++rank) {
RadBuilder b;
b.encs = {
{ "attn_q.weight", plain }, { "attn_k.weight", plain },
{ "attn_v.weight", plain }, { "attn_output.weight", plain },
{ "ffn_gate_up.weight", plain }, { "ffn_down.weight", plain },
{ "ffn_gate_up_exps", plain }, { "ffn_down_exps", plain },
{ "ffn_gate_up_shexp.weight", plain }, { "ffn_down_shexp.weight", plain },
{ "output.weight", plain },
};
RadBuildCtx ctx = build_ctx(rank, 2);
const int rc = hyv3_gguf::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) continue;
auto mapped = [&](const char* target, const char* source) {
for (const RecMap& m : b.maps)
if (m.declared == target && m.src.size() == 1 && m.src[0] == source) return true;
return false;
};
CHECK(mapped("blk.0.attn_q.weight", "blk.0.attn_q.weight"));
CHECK(mapped("blk.1.glm_router.weight", "blk.1.ffn_gate_inp.weight"));
CHECK(mapped("blk.1.glm_router.correction", "blk.1.exp_probs_b"));
CHECK(mapped("token_embd.weight", "token_embd.weight"));
const std::string expert_name =
"blk.1.ffn_gate_up_exps." + std::to_string(rank * 64) + ".weight";
const std::string down_name =
"blk.1.ffn_down_exps." + std::to_string(rank * 64) + ".weight";
bool expert_gate_up = false, expert_down = false, shared_gate_up = false;
for (const RecMap& m : b.maps) {
if (m.declared == expert_name && m.src.size() == 2 &&
m.src[0] == "blk.1.ffn_gate_exps.weight" &&
m.src[1] == "blk.1.ffn_up_exps.weight") expert_gate_up = true;
if (m.declared == down_name && m.src.size() == 1 &&
m.src[0] == "blk.1.ffn_down_exps.weight") expert_down = true;
if (m.declared == "blk.1.ffn_gate_up_shexp.weight" && m.src.size() == 2 &&
m.src[0] == "blk.1.ffn_gate_shexp.weight" &&
m.src[1] == "blk.1.ffn_up_shexp.weight") shared_gate_up = true;
}
CHECK(expert_gate_up); CHECK(expert_down); CHECK(shared_gate_up);
const auto& moe = hyv3_gguf::models[rank].layers[1].moe;
CHECK_EQ(moe.c.expert_bf16, 1);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
hyv3_gguf::step(&run, &batch);
CHECK(lint_step(b, {&run}).empty());
}
}
TEST(hyv3_bf16_selects_native_int4_expert_kernels_from_recipe_encodings) {
RadModelMeta meta = hyv3_fp8_test_meta();
meta.quant = "";
const RadEncoding plain = rad_enc_plain(RAD_BF16);
const RadEncoding w4 = rad_enc_affine(RAD_I4, RAD_BF16, 1, 128);
RadEncoding w4a = rad_enc_affine(RAD_U4, RAD_F16, 1, 128);
rad_enc_add_plane(&w4a, "zero", RAD_U8, 1, 128);
for (int rank = 0; rank < 2; ++rank) {
RadBuilder b;
b.encs = {
{ "attn_q.weight", w4a }, { "attn_k.weight", w4a },
{ "attn_v.weight", w4a }, { "attn_output.weight", w4a },
{ "ffn_gate_up.weight", w4a }, { "ffn_down.weight", w4a },
{ "ffn_gate_up_exps", w4 }, { "ffn_down_exps", w4 },
{ "ffn_gate_up_shexp.weight", plain }, { "ffn_down_shexp.weight", plain },
{ "output.weight", plain },
};
RadBuildCtx ctx = build_ctx(rank, 2);
const int rc = hyv3_bf16::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) continue;
const auto& moe = hyv3_bf16::models[rank].layers[1].moe;
CHECK_EQ(moe.c.expert_w4, 1);
CHECK_EQ(moe.c.expert_bf16, 0);
CHECK_EQ(moe.c.n_ff_exp, 768);
CHECK_EQ(moe.c.expert_base, rank * 64);
int asym_w4_gemms = 0;
for (const RecOp& op : b.ops) {
const RecParam* dtype = op.op == "gemm_nt_q" ? param(&op, "dtype") : nullptr;
if (dtype && dtype->sval == "w4a8i8_asym") ++asym_w4_gemms;
}
CHECK(asym_w4_gemms > 0);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
hyv3_bf16::step(&run, &batch);
CHECK(lint_step(b, {&run}).empty());
}
}
TEST(hyv3_bf16_supports_int8_linear_recipes_at_tp2) {
RadModelMeta meta = hyv3_fp8_test_meta();
meta.quant = "";
const RadEncoding plain = rad_enc_plain(RAD_BF16);
const RadEncoding w8 = rad_enc_affine(RAD_I8, RAD_BF16, 1, 128);
for (int rank = 0; rank < 2; ++rank) {
RadBuilder b;
b.encs = {
{ "attn_q.weight", w8 }, { "attn_k.weight", w8 },
{ "attn_v.weight", w8 }, { "attn_output.weight", w8 },
{ "ffn_gate_up.weight", w8 }, { "ffn_down.weight", w8 },
{ "ffn_gate_up_exps", plain }, { "ffn_down_exps", plain },
{ "ffn_gate_up_shexp.weight", w8 }, { "ffn_down_shexp.weight", w8 },
{ "output.weight", w8 },
};
RadBuildCtx ctx = build_ctx(rank, 2);
const int rc = hyv3_bf16::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) continue;
int i8_gemms = 0;
for (const RecOp& op : b.ops) {
const RecParam* dtype = op.op == "gemm_nt_q" ? param(&op, "dtype") : nullptr;
if (dtype && dtype->sval == "i8a8") ++i8_gemms;
}
CHECK(i8_gemms > 0);
CHECK_EQ(find_w(b, "output.weight")->d.dtype, (uint32_t)RAD_I8);
const auto& moe = hyv3_bf16::models[rank].layers[1].moe;
CHECK_EQ(moe.c.expert_bf16, 1);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
hyv3_bf16::step(&run, &batch);
CHECK(lint_step(b, {&run}).empty());
}
}
TEST(hyv3_bf16_selects_block_fp8_and_mxfp4_expert_formats) {
RadModelMeta meta = hyv3_fp8_test_meta();
meta.quant = "";
const RadEncoding plain = rad_enc_plain(RAD_BF16);
const RadEncoding fp8 = rad_enc_affine(RAD_F8E4M3, RAD_F32, 128, 128);
const RadEncoding mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
for (int format = 0; format < 2; ++format) {
const RadEncoding encoded = format == 0 ? fp8 : mx;
for (int rank = 0; rank < 2; ++rank) {
RadBuilder b;
b.encs = {
{ "attn_q.weight", encoded }, { "attn_k.weight", encoded },
{ "attn_v.weight", encoded }, { "attn_output.weight", encoded },
{ "ffn_gate_up.weight", encoded }, { "ffn_down.weight", encoded },
{ "ffn_gate_up_exps", encoded }, { "ffn_down_exps", encoded },
{ "ffn_gate_up_shexp.weight", encoded }, { "ffn_down_shexp.weight", encoded },
{ "output.weight", plain },
};
RadBuildCtx ctx = build_ctx(rank, 2);
const int rc = hyv3_bf16::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) continue;
const auto& moe = hyv3_bf16::models[rank].layers[1].moe;
CHECK_EQ(moe.c.expert_f32_scale, format == 0 ? 1 : 0);
CHECK_EQ(moe.c.expert_mx, format == 0 ? 0 : 1);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
hyv3_bf16::step(&run, &batch);
CHECK(lint_step(b, {&run}).empty());
}
}
}
TEST(hyv3_fp8_declares_dense_then_sigmoid_moe_with_fp32_combine) {
RadModelMeta meta = hyv3_fp8_test_meta();
RadBuildCtx ctx = build_ctx();
RadBuilder b;
RadArchProbe probe{};
CHECK_OK(hyv3_fp8::probe(&meta, &probe));
const int rc = hyv3_fp8::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) return;
const RecWeight* q_norm = find_w(b, "blk.0.attn_q_norm.weight");
const RecWeight* dense = find_w(b, "blk.0.ffn_gate_up.weight");
const RecWeight* expert = find_w(b, "blk.1.ffn_gate_up_exps.0.weight");
const RecMap* expert_map = find_map(b, "blk.1.ffn_gate_up_exps.0.weight");
const RecWeight* shared = find_w(b, "blk.1.ffn_gate_up_shexp.weight");
const RecWeight* router = find_w(b, "blk.1.glm_router.weight");
const RecWeight* bias = find_w(b, "blk.1.glm_router.correction");
CHECK(q_norm && dense && expert && expert_map && shared && router && bias);
CHECK_EQ(expert_map->src.size(), 2u);
CHECK_EQ(expert_map->src[0], std::string("model.layers.1.mlp.experts.0.gate_proj.weight"));
CHECK_EQ(expert_map->src[1], std::string("model.layers.1.mlp.experts.0.up_proj.weight"));
CHECK_EQ(q_norm->d.shape[0], 128);
CHECK_EQ(dense->d.shape[0], 13824); CHECK_EQ(dense->d.shape[1], 2048);
CHECK_EQ(expert->d.shape[0], 1536); CHECK_EQ(expert->d.shape[1], 2048);
CHECK_EQ(shared->d.shape[0], 1536); CHECK_EQ(shared->d.shape[1], 2048);
CHECK_EQ(router->d.shape[0], 128); CHECK_EQ(router->d.shape[1], 2048);
CHECK_EQ(bias->d.shape[0], 128); CHECK_EQ(bias->d.dtype, (uint32_t)RAD_F32);
auto& model = hyv3_fp8::models[0];
auto& moe = model.layers[1].moe;
CHECK_EQ(moe.c.n_ff_exp, 768); CHECK_EQ(moe.c.n_ff_shared, 768);
CHECK_EQ(moe.c.expert_f32_scale, 1);
CHECK_EQ(moe.c.route_scale, 2.826); CHECK_EQ(moe.c.sigmoid_router, 1);
CHECK_EQ(moe.c.shared_fp32_combine, 1); CHECK_EQ(moe.gfold, false);
const RecOp* gather = b.op_of(moe.op_gather);
CHECK(gather != nullptr);
CHECK_EQ(gather->writes.size(), 1u);
CHECK_EQ(gather->writes[0], moe.w.moe);
CHECK(moe.op_moe_to_f32 && moe.op_shared_to_f32 && moe.op_combine_f32 &&
moe.op_combined_to_bf16);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
hyv3_fp8::step(&run, &batch);
auto issue_of = [&](rad_op h) {
for (size_t i = 0; i < run.issues.size(); ++i)
if (run.issues[i].op == h) return (int)i;
return -1;
};
const int gather_i = issue_of(moe.op_gather), routed_i = issue_of(moe.op_moe_to_f32);
const int shared_i = issue_of(moe.op_shared_to_f32), add_i = issue_of(moe.op_combine_f32);
const int cast_i = issue_of(moe.op_combined_to_bf16);
CHECK(gather_i >= 0 && routed_i >= 0 && shared_i >= 0 && add_i >= 0 && cast_i >= 0);
CHECK(gather_i < routed_i && routed_i < add_i && shared_i < add_i && add_i < cast_i);
const auto lint = lint_step(b, {&run});
CHECK(lint.empty());
}
TEST(hyv3_fp8_tensor_parallel_partitions_experts_and_shared_width) {
RadModelMeta meta = hyv3_fp8_test_meta();
for (int rank = 0; rank < 2; ++rank) {
RadBuildCtx ctx = build_ctx(rank, 2);
ctx.kv_dtype = RAD_F8E4M3;
RadBuilder b;
const int rc = hyv3_fp8::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) continue;
auto& model = hyv3_fp8::models[rank];
const auto& moe = model.layers[1].moe;
CHECK_EQ(moe.c.n_expert, 64);
CHECK_EQ(moe.c.expert_base, rank * 64);
CHECK_EQ(moe.c.n_ff_shared, 384);
CHECK_EQ(moe.c.shared_lo, rank * 384);
CHECK_EQ(moe.c.shared_hi, (rank + 1) * 384);
CHECK(moe.op_moe_ar && moe.op_shared_ar && !moe.op_ar);
CHECK(find_w(b, rank == 0 ? "blk.1.ffn_gate_up_exps.0.weight"
: "blk.1.ffn_gate_up_exps.64.weight") != nullptr);
CHECK_EQ(model.g.n_ff, 3456);
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch; run.rank = rank;
hyv3_fp8::step(&run, &batch);
auto issue_of = [&](rad_op h) {
for (size_t i = 0; i < run.issues.size(); ++i)
if (run.issues[i].op == h) return (int)i;
return -1;
};
CHECK(issue_of(moe.op_gather) < issue_of(moe.op_moe_ar));
CHECK(issue_of(moe.op_moe_ar) < issue_of(moe.op_moe_to_f32));
CHECK(issue_of(moe.op_shared_ar) < issue_of(moe.op_shared_to_f32));
CHECK(issue_of(moe.op_combine_f32) < issue_of(moe.op_combined_to_bf16));
CHECK(lint_step(b, {&run}).empty());
}
}
TEST(hyv3_fp8_uses_static_layerwise_fp8_kv_descales) {
RadModelMeta meta = hyv3_fp8_test_meta();
RadBuildCtx ctx = build_ctx();
ctx.kv_dtype = RAD_F8E4M3;
RadBuilder b;
const int rc = hyv3_fp8::declare(&b, &meta, &ctx);
CHECK_EQ(rc, RAD_OK);
if (rc < 0) return;
CHECK_EQ(b.kv_groups[0].d.dtype, (uint32_t)RAD_F8E4M3);
auto& attn = hyv3_fp8::models[0].layers[0].attn;
CHECK(attn.op_k_descale && attn.op_v_descale);
CHECK(attn.k_descale && attn.v_descale);
const RecWeight* q = find_w(b, "blk.0.attn_q.weight");
CHECK(q != nullptr);
if (q) CHECK_EQ(q->d.shape[0], 4096);
const RecOp* k_fill = b.op_of(attn.op_k_descale);
const RecOp* v_fill = b.op_of(attn.op_v_descale);
CHECK(k_fill != nullptr && v_fill != nullptr);
if (k_fill) CHECK(std::fabs(pflt(k_fill, "value") - 0.0376674123) < 1e-9);
if (v_fill) CHECK(std::fabs(pflt(v_fill, "value") - 0.0004359654) < 1e-10);
const RecOp* store = b.op_of(attn.op_kv_store);
CHECK(store != nullptr);
if (store) {
CHECK(std::fabs(pflt(store, "k_scale") - 0.0376674123) < 1e-9);
CHECK(std::fabs(pflt(store, "v_scale") - 0.0004359654) < 1e-10);
}
const auto& attn1 = hyv3_fp8::models[0].layers[1].attn;
const RecOp* store1 = b.op_of(attn1.op_kv_store);
CHECK(store1 != nullptr);
if (store1) {
CHECK(std::fabs(pflt(store1, "k_scale") - 0.0224609375) < 1e-10);
CHECK(std::fabs(pflt(store1, "v_scale") - 0.0005318778) < 1e-10);
}
RadBatch batch = decode_batch();
RadCtx run{}; run.b = &b; run.batch = &batch;
hyv3_fp8::step(&run, &batch);
auto issue_of = [&](rad_op h) {
for (size_t i = 0; i < run.issues.size(); ++i)
if (run.issues[i].op == h) return (int)i;
return -1;
};
CHECK(issue_of(attn.op_k_descale) < issue_of(attn.op_attn));
CHECK(issue_of(attn.op_v_descale) < issue_of(attn.op_attn));
CHECK(lint_step(b, {&run}).empty());
}
TEST(the_alternation_puts_full_attention_at_3_and_7) {
RadBuilder b;
RadModelMeta meta = qwen35_meta();
@@ -3823,6 +4306,47 @@ TEST(gemma4_declares_two_kv_groups_and_binds_the_schedule) {
}
}
/* A step with an image: the rows go into the token lookup before its all-reduce, since
* RadBatch::mm_embd holds them at rank 0 and zeros at every other rank. */
TEST(gemma4_scatters_image_rows_into_the_lookup_before_its_all_reduce) {
std::vector<const char*> keys(std::begin(k_gem4_keys), std::end(k_gem4_keys));
std::vector<const char*> vals(std::begin(k_gem4_vals), std::end(k_gem4_vals));
const char* vk[] = { "vision_config.model_type", "vision_config.hidden_size",
"vision_config.num_attention_heads", "vision_config.head_dim",
"vision_config.intermediate_size", "vision_config.pooling_kernel_size",
"vision_config.position_embedding_size", "vision_config.num_hidden_layers",
"vision_config.patch_size", "vision_config.rope_parameters.rope_theta",
"vision_config.rope_parameters.rope_type", "vision_config.hidden_activation" };
const char* vv[] = { "gemma4_vision", "768", "12", "64", "3072", "3", "10240", "2", "16", "100",
"default", "gelu_pytorch_tanh" };
keys.insert(keys.end(), std::begin(vk), std::end(vk));
vals.insert(vals.end(), std::begin(vv), std::end(vv));
static const int32_t rows[] = { 4, 5, 6, 7, 8, 9, 10, 11 };
static uint16_t embd[8 * 5376];
for (int world : { 1, 2 }) {
for (int rank = 0; rank < world; ++rank) {
RadBuilder b;
RadModelMeta meta = gemma4_meta();
meta.n_kv = (int)keys.size(); meta.kv_key = keys.data(); meta.kv_val = vals.data();
RadBuildCtx ctx = build_ctx(rank, world);
ctx.max_enc_patches = 9 * 64;
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
RadBatch batch = prefill_batch();
batch.n_mm_rows = 8; batch.mm_rows = rows; batch.mm_embd = embd;
RadCtx c; c.b = &b; c.rank = rank; c.world = world; c.batch = &batch;
gemma4_bf16::step(&c, &batch);
CHECK_LINT(b, &c);
CHECK(c.step_fail.empty());
const std::vector<std::string> ops = issued_ops(b, c);
REQUIRE(ops.size() > 3);
CHECK_EQ(ops[0], std::string("embed_lookup"));
CHECK_EQ(ops[1], std::string("scatter_rows"));
CHECK(c.issues[1].opd[0].raw == embd);
if (world == 2) CHECK_EQ(ops[2], std::string("all_reduce"));
}
}
}
/* The E2B/E4B form on the same twelve layers: the last four share KV (no cache of their own),
* per-layer embeddings 64 wide, a global class with its own v (attention_k_eq_v 0) and no
* num_global_key_value_heads, so the global layers take num_key_value_heads. */
@@ -3885,13 +4409,103 @@ TEST(a_gemma4_e4b_form_shares_kv_and_runs_the_per_layer_branch) {
}
}
/* The per-layer branch is not split across ranks, and a model whose every layer shares KV has no
* cache to read. */
/* At two ranks the per-layer table is vocab-sharded like the token table and its lookup is reduced
* like the token lookup, before anything reads it; the rest of the branch is replicated. A step
* reduces the two lookups and, a layer, attention's and the feed-forward's partial sums -- folded
* into the attention junction where it resolves -- and never the branch's output, which every rank
* holds whole. The prefill overlap, whose slices skip the branch, stays off. */
TEST(the_gemma4_e4b_form_runs_at_two_ranks) {
const int64_t L = 12, P = 12 * 64;
for (bool w4 : { false, true }) {
for (int rank : { 0, 1 }) {
RadBuilder b;
if (w4) {
const RadEncoding e = rad_enc_affine(RAD_I4, RAD_F16, 1, 128);
b.encs = { { "attn_q.", e }, { "attn_k.", e }, { "attn_v.", e }, { "attn_o.", e },
{ "ffn_gate_up.", e }, { "ffn_down.", e } };
}
RadModelMeta meta = gemma4_e4b_meta();
RadBuildCtx ctx = build_ctx(rank, 2);
ctx.max_tok = 4096; /* past two slices of the overlap */
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
CHECK(!gemma4_bf16::g_model[rank].overlap);
CHECK_EQ(gemma4_bf16::g_model[rank].fuse, w4);
/* the per-layer table takes the token table's slice of the vocabulary */
const RecOp* tok = find_op(b, "embed_lookup", 0);
const RecOp* ple = find_op(b, "embed_lookup", 1);
REQUIRE(tok != nullptr && ple != nullptr);
CHECK_EQ(pint(ple, "n_embd"), P);
CHECK_EQ(pint(ple, "n_vocab"), pint(tok, "n_vocab"));
CHECK_EQ(pint(ple, "vocab_offset"), pint(tok, "vocab_offset"));
RadBatch batch = decode_batch();
RadCtx c; c.b = &b; c.rank = rank; c.world = 2; c.batch = &batch;
gemma4_bf16::step(&c, &batch);
CHECK_LINT(b, &c);
CHECK(c.step_fail.empty());
const std::vector<std::string> ops = issued_ops(b, c);
REQUIRE(ops.size() > 4);
CHECK_EQ(ops[0], std::string("embed_lookup"));
CHECK_EQ(ops[1], std::string("all_reduce"));
CHECK_EQ(ops[2], std::string("embed_lookup"));
CHECK_EQ(ops[3], std::string("all_reduce"));
CHECK_EQ(c.issues[3].n, batch.n_tok * P);
const int folded = count_issued(ops, "ar_rmsnorm_quant_i8");
CHECK_EQ(count_issued(ops, "all_reduce") + folded, (int)(2 + 2 * L));
CHECK_EQ(folded, w4 ? (int)L : 0);
}
}
}
/* With an image at two ranks, the pad rows of the image positions go into the per-layer inputs
* after their lookup's all-reduce: every rank writes them whole, so before it they would count
* twice. */
TEST(the_gemma4_e4b_form_writes_image_pad_rows_after_the_per_layer_all_reduce) {
std::vector<const char*> keys(std::begin(k_gem4e_keys), std::end(k_gem4e_keys));
std::vector<const char*> vals(std::begin(k_gem4e_vals), std::end(k_gem4e_vals));
const char* vk[] = { "vision_config.model_type", "vision_config.hidden_size",
"vision_config.num_attention_heads", "vision_config.head_dim",
"vision_config.intermediate_size", "vision_config.pooling_kernel_size",
"vision_config.position_embedding_size", "vision_config.num_hidden_layers",
"vision_config.patch_size", "vision_config.rope_parameters.rope_theta",
"vision_config.rope_parameters.rope_type", "vision_config.hidden_activation" };
const char* vv[] = { "gemma4_vision", "768", "12", "64", "3072", "3", "10240", "2", "16", "100",
"default", "gelu_pytorch_tanh" };
keys.insert(keys.end(), std::begin(vk), std::end(vk));
vals.insert(vals.end(), std::begin(vv), std::end(vv));
static const int32_t rows[] = { 4, 5, 6, 7, 8, 9, 10, 11 };
static uint16_t embd[8 * 5376];
for (int rank : { 0, 1 }) {
RadBuilder b;
RadModelMeta meta = gemma4_e4b_meta();
meta.n_kv = (int)keys.size(); meta.kv_key = keys.data(); meta.kv_val = vals.data();
RadBuildCtx ctx = build_ctx(rank, 2);
ctx.max_enc_patches = 9 * 64;
CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx));
RadBatch batch = prefill_batch();
batch.n_mm_rows = 8; batch.mm_rows = rows; batch.mm_embd = embd;
RadCtx c; c.b = &b; c.rank = rank; c.world = 2; c.batch = &batch;
gemma4_bf16::step(&c, &batch);
CHECK_LINT(b, &c);
CHECK(c.step_fail.empty());
const std::vector<std::string> ops = issued_ops(b, c);
size_t i = 0; /* the per-layer lookup: the second embed_lookup */
for (int seen = 0; i < ops.size(); ++i)
if (ops[i] == "embed_lookup" && ++seen == 2) break;
REQUIRE(i + 4 < ops.size());
const std::vector<std::string> want = { "all_reduce", "fill", "mul_weight", "scatter_rows" };
CHECK(std::vector<std::string>(ops.begin() + (long)i + 1, ops.begin() + (long)i + 5) == want);
}
}
/* The per-layer branch is served at one or two ranks, and a model whose every layer shares KV has
* no cache to read. */
TEST(the_gemma4_e4b_form_is_refused_where_it_cannot_run) {
{
RadBuilder b;
RadModelMeta meta = gemma4_e4b_meta();
RadBuildCtx ctx = build_ctx(0, 2);
RadBuildCtx ctx = build_ctx(0, 4);
CHECK_EQ(gemma4_bf16::declare(&b, &meta, &ctx), RAD_E_UNSUPPORTED);
}
{
@@ -10129,7 +10743,7 @@ TEST(flashnext_a_spanned_prefill_runs_a_pass_at_a_time_with_its_all_reduces_besi
ctx.max_mixed_tok = 2048;
REQUIRE_EQ(stderr_of_rc([&] { return qwen4exp_fp8::declare(&b, &meta, &ctx); }),
RAD_OK);
const qwen4exp_fp8::Model& m = qwen4exp_fp8::g_model[rank];
qwen4exp_fp8::Model& m = qwen4exp_fp8::g_model[rank];
CHECK(m.span_on);
CHECK_EQ(m.pipe_on, world > 2);
CHECK_EQ(m.qsacfg.plane_rows, 2048);
@@ -10165,6 +10779,10 @@ TEST(flashnext_a_spanned_prefill_runs_a_pass_at_a_time_with_its_all_reduces_besi
CHECK(score > 0);
CHECK_EQ(wide_score, 0);
CHECK(conv > 0);
/* This fake runtime has no engine teardown between model redeclarations. */
rad_dev_free(m.pipe_cu_h, RAD_MEM_HOST_PINNED);
m.pipe_cu_h = nullptr;
m.pipe_cu_tried = false;
if (world < 4) {
CHECK_EQ(ar, world > 1 ? 1 : 0);
CHECK_EQ(ar_lane0, ar);
@@ -10360,13 +10978,24 @@ MetaKV krea2_meta() {
} /* namespace */
/* `rebalance` (RAD_GEN_COND_REBALANCE, declared in the generator's cond_opts): each of the twelve
* taps is scaled into te.h2 by its gain (ComfyUI's ConditioningKrea2Rebalance at its defaults,
* multiplier 4 times 1, 1, 1, 1, 1, 1, 1, 2.5, 5, 1.1, 4, 1), f32 and rounded once, and the
* tap's scatter reads te.h2. Without it the pass issues no scale and scatters te.x. */
/* `rebalance` (RAD_GEN_COND_REBALANCE, declared in the generator's cond_opts with its presets):
* each of the twelve taps is scaled into te.h2 by its preset's gain (row 0 ComfyUI's
* ConditioningKrea2Rebalance at its defaults, multiplier 4 times 1, 1, 1, 1, 1, 1, 1, 2.5, 5, 1.1,
* 4, 1; rows 1 to 3 balanced, detail, subtle), f32 and rounded once, and the tap's scatter reads
* te.h2. Without it, or with a preset row the plugin does not know, the pass issues no scale and
* scatters te.x. */
TEST(krea2_rebalance_scales_each_tap_and_is_off_without_the_option) {
MetaKV mk = krea2_meta();
const double gains[12] = { 4, 4, 4, 4, 4, 4, 4, 10, 20, 4.4, 16, 4 };
/* RAD_GEN_COND_REBALANCE_PRESET rows 0 (the default), 1 balanced, 2 detail, 3 subtle */
const double gains[4][12] = {
{ 4, 4, 4, 4, 4, 4, 4, 10, 20, 4.4, 16, 4 },
{ 0.44109, 0.44109, 0.44109, 0.44109, 0.44109, 0.44109, 0.44109, 1.1027, 2.2055, 0.4852,
1.7644, 0.44109 },
{ 0.28562, 0.28562, 0.32132, 0.32132, 0.35702, 0.35702, 0.42842, 1.0711, 2.1421, 0.53553,
1.7851, 0.42842 },
{ 0.84759, 0.84759, 0.84759, 0.84759, 0.84759, 0.84759, 0.84759, 1.2714, 1.6952, 0.84759,
1.2714, 0.84759 },
};
RadArchProbe probe{};
REQUIRE_EQ(krea2::probe(mk.meta(), &probe), RAD_OK);
CHECK_EQ(probe.gen_rows, (int64_t)2 * (16384 + 512));
@@ -10375,7 +11004,8 @@ TEST(krea2_rebalance_scales_each_tap_and_is_off_without_the_option) {
RadBuilder b;
RadBuildCtx ctx = qimg_ctx(rank, world);
REQUIRE_EQ(stderr_of_rc([&] { return krea2::declare(&b, mk.meta(), &ctx); }), RAD_OK);
CHECK_EQ(b.generator.cond_opts, (uint32_t)RAD_GEN_COND_REBALANCE);
CHECK_EQ(b.generator.cond_opts,
(uint32_t)(RAD_GEN_COND_REBALANCE | RAD_GEN_COND_REBALANCE_PRESET));
const RecBuf* bx = find_b(b, "te.x");
const RecBuf* bh = find_b(b, "te.h2");
const RecBuf* bt = find_b(b, "te.taps");
@@ -10383,9 +11013,13 @@ TEST(krea2_rebalance_scales_each_tap_and_is_off_without_the_option) {
const rad_buf x = (rad_buf)(bx - b.bufs.data() + 1);
const rad_buf h2 = (rad_buf)(bh - b.bufs.data() + 1);
const rad_buf taps = (rad_buf)(bt - b.bufs.data() + 1);
for (bool rebalance : { false, true }) {
/* -1: no option; 0 to 3 the presets; 4 a row the plugin does not know */
for (int preset : { -1, 0, 1, 2, 3, 4 }) {
const bool rebalance = preset >= 0 && preset < 4;
auto k = gen_pass(b, RAD_GEN_PASS_COND, 12);
k->g.cond_opts = rebalance ? RAD_GEN_COND_REBALANCE : 0;
k->g.cond_opts = preset >= 0 ? RAD_GEN_COND_REBALANCE |
(preset << RAD_GEN_COND_REBALANCE_PRESET_SHIFT)
: 0;
RadCtx c{ .b = &b, .batch = &k->b, .rank = rank, .world = world };
krea2::step(&c, &k->b);
CHECK(c.step_fail.empty());
@@ -10418,7 +11052,7 @@ TEST(krea2_rebalance_scales_each_tap_and_is_off_without_the_option) {
const std::vector<double> mul = list(pstr(g, "mul")), add = list(pstr(g, "add"));
CHECK_EQ(mul.size(), (size_t)64);
CHECK_EQ(add.size(), (size_t)64);
for (double v : mul) CHECK_EQ(v, gains[tap]);
for (double v : mul) CHECK_EQ(v, gains[preset][tap]);
for (double v : add) CHECK_EQ(v, 0.0);
}
++tap;