This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/tests/host_experts_test.cpp
T
Deadcode ddcb9c69bb radiance 2.0.0
Radiance becomes a multi-model engine: one process serves several models behind one port, loads
each when a request names it, parks it in host memory or restores it from its container, and
shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the
command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP).

Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an
int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block
FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such
weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5,
Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B,
the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0
serves all of them.

Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new
Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill
reworked, and a reworked adaptive draft window.

Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its
configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool
in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server,
each accelerator's memory by model, model states over time.

Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by
AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests
keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410);
eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d
large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi
(#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon
(#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and
multi-model serving as asked for in #367 and #109.

Plugin ABI 39: out-of-tree plugins need to be rebuilt.

Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
2026-11-14 22:32:28 +01:00

262 lines
10 KiB
C++

/* host_experts_test.cpp -- Qwen3.8-Flash-Next's routed feed-forward, declared for a CPU-only engine
* against libavx alone (`--kernels libavx` on the host backend).
*
* The block is arch/common/rad_block_moe_fp8.h with the connection around it
* (arch/common/rad_block_hc.h), declared as qwen4exp_fp8 declares them, at a small geometry with
* the served container's structure and encodings (data/recipes/qwen4exp-w4nl64-i8-hc8m.recipe):
* the connection's read writing the rotated E4M3 codes and the int8 plain codes, the router, its
* top-k and the sort with two protected experts, the codebook experts' grouped GEMMs, the rotated
* gated quantiser between them, the protected experts' bf16 pair, the int8 shared expert and its
* gate, the gather with the shared arm folded in, and the connection's write. Every op must resolve
* to a libavx host row, and every codebook view of several planes must be stored in the joined
* form both host libraries read (abi/rad_planes.h). The mixing matrices are plain bf16 here; their
* E4M3 form is a separate weight's concern.
*/
#include "rad_test.h"
#include "rad_planes.h"
#include "build/rad_build.h"
#include "plugin/registry.h"
#include "rad_internal.h"
#include <arch/rad_block_hc.h>
#include <arch/rad_block_moe_fp8.h>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <string>
using namespace rad;
using namespace rad::arch;
namespace {
/* One layer at Flash-Next's structure: n_embd and the expert width whole 128-wide groups, eight
* experts at top-k 2 with a shared expert as wide, four streams. */
constexpr int64_t kEmbd = 256, kFF = 128, kExperts = 8, kTopK = 2, kHC = 4, kLowRank = 32,
kTok = 64;
/* The protected experts, kept bf16 by the recipe: a pair, so the rest split by parity. */
bool protected_expert(long long e) { return e == 3 || e == 5; }
RadEncoding codebook(const char* dtype) {
const bool w5 = !std::strcmp(dtype, "w5nl64a8h");
const int64_t group = !std::strcmp(dtype, "w4nl32a8h") ? 32 : 64;
RadEncoding e;
rad_enc_clear(&e);
rad_enc_copy_str(e.scheme, "affine");
rad_enc_copy_str(e.transform, "fwht128");
rad_enc_add_plane(&e, "codes", w5 ? RAD_U5 : RAD_U4, 1, 1);
rad_enc_add_plane(&e, "scale", RAD_F8E4M3, 1, group);
rad_enc_add_plane(&e, "scale.1", RAD_F32, 0, 0);
rad_enc_add_table(&e, "table", RAD_F32, 1, w5 ? 32 : 16);
return e;
}
/* The container's encodings by declared name; anything else is read as plain in its declared
* dtype. */
int sources(const char* dtype, const std::string& name, WeightSource* out) {
auto expert = [&](const char* prefix, bool gate_up) {
const size_t n = std::strlen(prefix);
if (name.compare(0, n, prefix) != 0) return false;
const long long e = std::atoll(name.c_str() + n);
out->rank = 2;
out->shape[0] = gate_up ? 2 * kFF : kEmbd;
out->shape[1] = gate_up ? kEmbd : kFF;
out->enc = protected_expert(e) ? rad_enc_plain(RAD_BF16) : codebook(dtype);
return true;
};
if (expert("blk.0.ffn_gate_up_exps.", true) || expert("blk.0.ffn_down_exps.", false))
return RAD_OK;
const bool gu = name == "blk.0.ffn_gate_up_shexp.weight";
if (gu || name == "blk.0.ffn_down_shexp.weight") {
out->rank = 2;
out->shape[0] = gu ? 2 * kFF : kEmbd;
out->shape[1] = gu ? kEmbd : kFF;
out->enc = rad_enc_affine(RAD_I8, RAD_BF16, 1, RAD_FP8_BLOCK);
return RAD_OK;
}
return RAD_E_NOTFOUND;
}
RadModelMeta meta() {
RadModelMeta m{};
m.arch_id = "qwen4exp";
m.name = "host-experts";
m.quant = "";
m.n_layers = 1;
m.n_embd = kEmbd;
return m;
}
RadBuildCtx ctx() {
RadBuildCtx c{};
c.rank = 0;
c.world_size = 1;
c.max_tok = kTok;
c.max_seqs = 8;
c.max_ctx = 4096;
c.scope = "";
return c;
}
/* The connection and the block on `b`, as qwen4exp_fp8 declares one layer's feed-forward. */
int declare_layer(RadBuilder* b, MoeFP8* mlp, HyperConn* hc) {
Names nm;
Geom g;
g.n_layer = 1;
g.n_embd = kEmbd;
g.n_ff = g.n_ff_all = kFF;
g.max_tok = kTok;
g.max_seqs = 8;
g.max_ctx = 4096;
g.max_logit_rows = 8;
g.dtype = "bf16";
g.act_dtype = RAD_BF16;
MoeFP8::Config mc;
mc.n_expert = mc.n_expert_all = kExperts;
mc.top_k = kTopK;
mc.n_ff_exp = kFF;
mc.n_ff_shared = kFF;
mc.rows = kTok;
MoeFP8::Src ms{};
RAD_ARCH_TRY(moe_expert_sources(b, nm, "format", "model.layers.0.mlp.experts", &ms));
RAD_ARCH_TRY(MoeFP8::probe(b, nm, 0, ms, &mc));
if (!mc.expert_rot || !mc.expert_nl) return RAD_E_DTYPE;
const int64_t T = kTok, ER = mc.rows * mc.top_k;
ActFP8 x, hr, eff, sff;
x.x = decl_b(b, nm.f("x"), RAD_BF16, { T, kEmbd });
RAD_ARCH_TRY(x.declare_qs(b, nm, g, "x", kEmbd, 0, true));
x.q8_fed = true;
RAD_ARCH_TRY(hr.declare_qs(b, nm, g, "moe_hr", kEmbd, 0, true, false, RAD_FP8_BLOCK));
hr.q8_fed = hr.q8_rot = true;
eff.x = decl_b(b, nm.f("moe_eff"), RAD_BF16, { ER, kFF });
RAD_ARCH_TRY(eff.declare_qs(b, nm, g, "moe_eff", kFF, ER, true, false, RAD_FP8_BLOCK));
eff.q8_fed = eff.q8_rot = true;
sff.x = decl_b(b, nm.f("moe_sff"), RAD_BF16, { T, kFF });
RAD_ARCH_TRY(sff.declare_qs(b, nm, g, "moe_sff", kFF, 0, true));
sff.q8_fed = true;
HyperConn::Wire hw{};
hw.h = decl_b(b, nm.f("h_wide"), RAD_BF16, { T, kHC * kEmbd });
hw.x = x;
hw.inj = decl_b(b, nm.f("hc_inj"), RAD_BF16, { T, kHC });
hw.rq = hr.cq();
hw.rs = hr.cs();
HyperConn::Config hcfg;
hcfg.hc = kHC;
hcfg.lowrank = kLowRank;
hcfg.rotate = 1;
hcfg.codes_i8 = 1;
HyperConn::Src hs{};
hs.norm = nm.ckpt("model.layers.0.mlp_hyper_connection.hc_norm.weight");
hs.mix_down = nm.ckpt("model.layers.0.mlp_hyper_connection.input_mix_weight_down.weight");
hs.mix_up = nm.ckpt("model.layers.0.mlp_hyper_connection.input_mix_weight_up.weight");
hs.inject_w = nm.ckpt("model.layers.0.mlp_hyper_connection.block_inject_weight.weight");
RAD_ARCH_TRY(hc->declare(b, nm, g, "blk.0.ffn_hc", hcfg, hw, hs));
MoeFP8::Wire mw{};
mw.x = hw.h;
mw.h = x;
mw.hr = hr;
mw.logits = decl_b(b, nm.f("moe_logits"), RAD_BF16, { T, kExperts });
mw.ids = decl_b(b, nm.f("moe_ids"), RAD_I32, { T, kTopK });
mw.ew = decl_b(b, nm.f("moe_ew"), RAD_BF16, { T, kTopK });
mw.eoff = decl_b(b, nm.f("moe_eoff"), RAD_I32, { kExperts + 1 });
mw.ecnt = decl_b(b, nm.f("moe_ecnt"), RAD_I32, { kExperts }, RAD_BUF_PERSIST);
mw.sorted = decl_b(b, nm.f("moe_sorted"), RAD_I32, { ER });
mw.egu = decl_b(b, nm.f("moe_egu"), RAD_BF16, { ER, 2 * kFF });
mw.eff = eff;
mw.edn = decl_b(b, nm.f("moe_edn"), RAD_BF16, { ER, kEmbd });
mw.moe = decl_b(b, nm.f("moe_out"), RAD_BF16, { T, kEmbd });
mw.sgu = decl_b(b, nm.f("moe_sgu"), RAD_BF16, { T, 2 * kFF });
mw.sff = sff;
mw.sout = decl_b(b, nm.f("moe_sout"), RAD_BF16, { T, kEmbd });
mw.sgate = decl_b(b, nm.f("moe_sgate"), RAD_BF16, { T, 1 });
ms.cfg = mc;
ms.router = nm.ckpt("model.layers.0.mlp.gate.weight");
ms.shared = nm.ckpt("model.layers.0.mlp.shared_expert");
ms.shared_gate = nm.ckpt("model.layers.0.mlp.shared_expert_gate.weight");
mlp->hr_in = true;
return mlp->declare(b, nm, g, 0, mw, ms, false, false, kArNone, false);
}
} /* namespace */
/* Every op of the layer resolves to libavx with no other kernel library loaded, at each codebook
* dtype, and each codebook view of several planes is stored joined. */
TEST(flash_next_experts_declare_on_libavx_alone) {
Registry reg;
const int st = reg.load_dir(home() + "/kernels", { "libavx" });
if (st == RAD_E_IO || !reg.plugin("libavx")) ARTIFACT_MISSING(home() + "/kernels/libavx.so");
/* The routed arm at int8 activations writes its gated product with gated_had_quant_i8g, which
* libavx has no row for. */
if (!reg.schema("gated_had_quant_i8g"))
SKIP("libavx declares no gated_had_quant_i8g, the int8 routed arm's gated quantiser");
for (const char* dtype : { "w4nl64a8h", "w4nl32a8h" }) {
Builder b(reg, meta(), ctx());
b.set_sources([dtype](const std::string& name, const Program&, WeightSource* out) {
return sources(dtype, name, out);
});
MoeFP8 mlp;
HyperConn hc;
std::string log;
int rc = RAD_OK;
log = stderr_of([&] {
rc = declare_layer(&b, &mlp, &hc);
if (rc == RAD_OK) rc = b.finish();
});
if (rc != RAD_OK || !b.errors().empty()) {
fprintf(stderr, " %s: declare %d\n%s", dtype, rc, log.c_str());
for (const auto& e : b.errors()) fprintf(stderr, " %s\n", e.c_str());
}
REQUIRE_EQ(rc, RAD_OK);
CHECK(b.errors().empty());
/* Every band of every op on a libavx host row. */
std::set<std::string> ops;
const Program& P = b.program();
for (size_t i = 1; i < P.ops.size(); ++i) {
const OpInfo& o = P.ops[i];
ops.insert(o.op);
for (const Band& band : o.bands) {
const Resolved& r = band.dom[RAD_DOMAIN_HOST];
if (!r.row || r.row->plugin != "libavx") {
fprintf(stderr, " %s: op '%s' has no libavx host row\n", dtype,
o.op.c_str());
CHECK(false);
}
}
}
for (const char* want : { "hc_read", "hc_write", "gemm_nt", "router_topk", "moe_scatter",
"moe_gemm_q", "silu_mul", "moe_gemm",
"gemm_nt_q", "moe_gather" })
if (!ops.count(want)) {
fprintf(stderr, " %s: the layer declares no '%s'\n", dtype, want);
CHECK(false);
}
/* Each codebook view of several planes joined; the protected experts' and a single
* plane's stored as they are. */
int joined = 0;
for (size_t h = 1; h < P.weights.size(); ++h) {
const WeightInfo& w = P.weights[h];
if (w.name.find("_exps.") == std::string::npos) continue;
if (w.n_sel > 1) {
CHECK_EQ(w.layout_tag, std::string(RAD_PLANES_TAG));
CHECK_EQ(w.lay_dom, (int)RAD_DOMAIN_HOST);
++joined;
} else {
CHECK(w.layout_tag.empty());
}
}
/* Six codebook experts, codes view and scale view, both projections. */
CHECK_EQ(joined, 4 * 6);
}
}
RAD_TEST_MAIN()