Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

qwen36: offer the rest of the dense trunk to the VRAM placer (dnout, attnproj, shexp)

JustVugg6203a14c05c0
diff --git a/c/Makefile b/c/Makefile
index edc703f6..96433f08 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1862,6 +1862,10 @@ tests/test_qwen36_tier_dense$(EXE): tests/test_qwen36_tier_dense.c tests/qwen36_
tests/test_qwen36_tier_int8_engine$(EXE): tests/test_qwen36_tier_int8_engine.c tests/qwen36_fake_cuda.h qwen36.c expert_ffn.h qwen36_tier.c qwen36_tier.h backend_cuda.h cli_args.h st.h json.h compat.h
$(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)
+# dnout / attnproj / shexp offered, placed and served from VRAM (fake backend).
+tests/test_qwen36_trunk_dense$(EXE): tests/test_qwen36_trunk_dense.c tests/qwen36_fake_cuda.h qwen36.c expert_ffn.h qwen36_tier.c qwen36_tier.h backend_cuda.h cli_args.h st.h json.h compat.h
+ $(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)
+
# #1391: il decode path deve offrire gli esperti int8 al tier, non solo il warmstart
tests/test_qwen36_tier_int8_decode$(EXE): tests/test_qwen36_tier_int8_decode.c tests/qwen36_fake_cuda.h qwen36.c expert_ffn.h qwen36_tier.c qwen36_tier.h backend_cuda.h cli_args.h st.h json.h compat.h
$(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)
diff --git a/c/qwen36.c b/c/qwen36.c
index bcf91996..656cb346 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -639,6 +639,11 @@ typedef struct {
float *dn_dtbias, *dn_alog; /* dt_bias[vh], A_log[vh] */
float *dn_norm; /* RMSNormGated weight [vdim] */
float *dn_out; /* out_proj [hidden, value_dim] */
+ /* VRAM copies the tier placed (qt_dense handle + 1, 0 = stays on the CPU):
+ * the DeltaNet out_proj, the attention q/k/v/o and the shared expert's
+ * three matrices. Offered per layer as "dnout", "attnproj", "shexp";
+ * see trunk_offer_dense / trunk_place_dense. */
+ int qth_dnout, qth_q, qth_k, qth_v, qth_o, qth_shg, qth_shu, qth_shd;
} Layer;
/* ---------- LRU expert cache (int8 weights + per-row float scales) ---------- */
@@ -1038,6 +1043,29 @@ static void matmul_d(float *y, const float *x, const float *W, int S, int I, int
}
matmul(y, x, W, S, I, O);
}
+/* A dense matrix the tier placed in VRAM (handle+1 kept in the Layer, 0 = CPU):
+ * one GEMV from the device, or 0 and the caller runs matmul_d as before. The
+ * tier turns a failing handle off itself, so the fallback is permanent. */
+static inline int qtd(int hp1, float *y, const float *x, int I, int O){
+ return hp1 > 0 && qt_dense_matmul(hp1 - 1, y, x, I, O);
+}
+/* Bytes of W's dense-i8 copy (int8 rows + per-row scales), 0 when there is
+ * none (COLI_DENSE_I8=0): nothing to offer, the CPU path stands. */
+static size_t qdw_bytes(const float *W){
+ for (int i = 0; i < g_qdw_n; i++)
+ if (g_qdw[i].w == W) return (size_t)g_qdw[i].I * g_qdw[i].O + (size_t)g_qdw[i].O * sizeof(float);
+ return 0;
+}
+/* Upload W's dense-i8 copy to `dev`; handle+1, or 0 when it stays on the CPU. */
+static int qdw_place(const float *W, int dev){
+ if (dev == QT_PLACE_CPU || !W) return 0;
+ for (int i = 0; i < g_qdw_n; i++)
+ if (g_qdw[i].w == W) {
+ int h = qt_dense_init(g_qdw[i].q, g_qdw[i].sc, g_qdw[i].I, g_qdw[i].O, dev);
+ return h >= 0 ? h + 1 : 0;
+ }
+ return 0;
+}
/* rmsnorm over a row of length D (in-place capable: out may == x).
* Qwen3_5MoeRMSNorm: out = (x * rsqrt(mean(x^2)+eps)) * (1.0 + weight). */
@@ -1796,9 +1824,11 @@ static void attention(Model *m, Layer *l, int layer, float *x, int S, int pos_ba
float *q = falloc((int64_t)S*q_out);
float *k = falloc((int64_t)S*kv_out);
float *vv= falloc((int64_t)S*kv_out);
- matmul_d(q, x, l->q, S, D, q_out);
- matmul_d(k, x, l->k, S, D, kv_out);
- matmul_d(vv, x, l->v, S, D, kv_out);
+ /* Decode (S == 1): the projections the tier placed answer from VRAM,
+ * one GEMV each; a prompt batch keeps the batched CPU matmul. */
+ if (!(S == 1 && qtd(l->qth_q, q, x, D, q_out))) matmul_d(q, x, l->q, S, D, q_out);
+ if (!(S == 1 && qtd(l->qth_k, k, x, D, kv_out))) matmul_d(k, x, l->k, S, D, kv_out);
+ if (!(S == 1 && qtd(l->qth_v, vv, x, D, kv_out))) matmul_d(vv, x, l->v, S, D, kv_out);
/* split q into query (first hd) and gate (next gate_dim), both per head */
float *query = falloc((int64_t)S*H*hd);
float *gate = falloc((int64_t)S*H*gate_dim);
@@ -1860,7 +1890,7 @@ static void attention(Model *m, Layer *l, int layer, float *x, int S, int pos_ba
float g = gate_dim ? gate[o] : 0.f;
ag[o] = ctx[o] * (1.f / (1.f + expf(-g)));
}
- matmul_d(out, ag, l->o, S, H*hd, D);
+ if (!(S == 1 && qtd(l->qth_o, out, ag, H*hd, D))) matmul_d(out, ag, l->o, S, H*hd, D);
free(q); free(k); free(vv); free(query); free(gate); free(ctx); free(ag);
}
@@ -1893,10 +1923,10 @@ static void qwen_shared_experts_cpu(Model *m, Layer *l, const float *x, int S,
if (B == 1) {
for (int s=0;s<S;s++) {
const float *xs=x+(int64_t)s*D;
- matmul_d(g,xs,l->sh_g,1,D,I);
- matmul_d(u,xs,l->sh_u,1,D,I);
+ if(!qtd(l->qth_shg,g,xs,D,I)) matmul_d(g,xs,l->sh_g,1,D,I);
+ if(!qtd(l->qth_shu,u,xs,D,I)) matmul_d(u,xs,l->sh_u,1,D,I);
for(int i=0;i<I;i++){float sv=g[i];g[i]=(sv/(1.f+expf(-sv)))*u[i];}
- matmul_d(hh,g,l->sh_d,1,I,D);
+ if(!qtd(l->qth_shd,hh,g,I,D)) matmul_d(hh,g,l->sh_d,1,I,D);
float sgate=1.f;
if(l->sh_gate){float sg=0.f;for(int i=0;i<D;i++)sg+=xs[i]*l->sh_gate[i];sgate=1.f/(1.f+expf(-sg));}
float *os=out+(int64_t)s*D;
@@ -2178,10 +2208,10 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out) {
{
double _ts2 = tm_now();
int Ish = c->shared_inter;
- matmul_d(sh, xs, l->sh_g, 1, D, Ish);
- matmul_d(shu, xs, l->sh_u, 1, D, Ish);
+ if (!qtd(l->qth_shg, sh, xs, D, Ish)) matmul_d(sh, xs, l->sh_g, 1, D, Ish);
+ if (!qtd(l->qth_shu, shu, xs, D, Ish)) matmul_d(shu, xs, l->sh_u, 1, D, Ish);
for (int i = 0; i < Ish; i++) { float sv = sh[i]; sh[i] = (sv / (1.f + expf(-sv))) * shu[i]; }
- matmul_d(shd, sh, l->sh_d, 1, Ish, D);
+ if (!qtd(l->qth_shd, shd, sh, Ish, D)) matmul_d(shd, sh, l->sh_d, 1, Ish, D);
float sgate = 1.f;
if (l->sh_gate) {
float sg = 0.f; const float *wg = l->sh_gate;
@@ -2370,7 +2400,8 @@ static void deltanet(Model *m, Layer *l, int layer, float *x, int S, int pos_bas
outr[(int64_t)h * vdim + d] = val * zr[d] / (1.f + expf(-zr[d]));
}
}
- matmul_d(out + (int64_t)s * H, outr, l->dn_out, 1, value_dim, H);
+ if (!qtd(l->qth_dnout, out + (int64_t)s * H, outr, value_dim, H))
+ matmul_d(out + (int64_t)s * H, outr, l->dn_out, 1, value_dim, H);
if (tm_on() && S==1){ g_dn_sub[3]+=tm_now()-_d0; }
if (layer == 0 && s == 0 && getenv("DN_DBG")) {
FILE *dbg = fopen(getenv("DN_DBG"), "wb");
@@ -2394,6 +2425,67 @@ static void deltanet(Model *m, Layer *l, int layer, float *x, int S, int pos_bas
free(conv_out); free(q); free(k); free(outv); free(outr); free(kv); free(delta);
}
+/* The rest of the dense trunk, offered to the placer by name and layer with
+ * the bytes of the dense-i8 copies (docs/qwen36-cuda-tier.md, "Placement"):
+ * dnout -- the DeltaNet out_proj, one matrix per DeltaNet layer
+ * attnproj -- q, k, v, o of every attention layer, offered as one item
+ * shexp -- gate, up, down of the shared expert, every layer
+ * Measured on ds (CPU, 8 threads): of the 37.5 ms a DeltaNet layer stack
+ * costs per decoded token, 23.4 are the input projections (already placeable
+ * as "dnproj"), 8.3 the out_proj and norm, 3.3 the convolution and 2.4 the
+ * recurrence -- the matmuls are the cost, not the recurrence, so this is
+ * where the trunk goes. Offer order after lmhead and dnproj: dnout, attnproj,
+ * shexp, each in layer order, so a partial placement is whole layers. A
+ * component is offered only when every matrix of it has a dense-i8 copy. */
+static void trunk_offer_dense(Model *m){
+ Cfg *c = &m->c;
+ for (int i = 0; i < c->n_layers; i++) {
+ if (c->is_attn[i]) continue;
+ size_t b = qdw_bytes(m->L[i].dn_out);
+ if (b) qt_trunk_offer("dnout", i, b);
+ }
+ for (int i = 0; i < c->n_layers; i++) {
+ if (!c->is_attn[i]) continue;
+ Layer *l = &m->L[i];
+ size_t bq = qdw_bytes(l->q), bk = qdw_bytes(l->k), bv = qdw_bytes(l->v), bo = qdw_bytes(l->o);
+ if (bq && bk && bv && bo) qt_trunk_offer("attnproj", i, bq + bk + bv + bo);
+ }
+ for (int i = 0; i < c->n_layers; i++) {
+ Layer *l = &m->L[i];
+ size_t bg = qdw_bytes(l->sh_g), bu = qdw_bytes(l->sh_u), bd = qdw_bytes(l->sh_d);
+ if (bg && bu && bd) qt_trunk_offer("shexp", i, bg + bu + bd);
+ }
+}
+/* After qt_init decided: upload what was placed, keep the handles in the
+ * Layer. Every matrix falls back on its own, so a failed upload costs one
+ * GEMV on the CPU, never the component. Returns the number of matrices
+ * placed; `vram_bytes` gets their size. */
+static int trunk_place_dense(Model *m, double *vram_bytes){
+ Cfg *c = &m->c; int placed = 0; double vram = 0;
+ for (int i = 0; i < c->n_layers; i++) {
+ Layer *l = &m->L[i];
+ if (!c->is_attn[i]) {
+ l->qth_dnout = qdw_place(l->dn_out, qt_place_of("dnout", i));
+ if (l->qth_dnout) { placed++; vram += (double)qdw_bytes(l->dn_out); }
+ } else {
+ int dev = qt_place_of("attnproj", i);
+ l->qth_q = qdw_place(l->q, dev); l->qth_k = qdw_place(l->k, dev);
+ l->qth_v = qdw_place(l->v, dev); l->qth_o = qdw_place(l->o, dev);
+ if (l->qth_q) { placed++; vram += (double)qdw_bytes(l->q); }
+ if (l->qth_k) { placed++; vram += (double)qdw_bytes(l->k); }
+ if (l->qth_v) { placed++; vram += (double)qdw_bytes(l->v); }
+ if (l->qth_o) { placed++; vram += (double)qdw_bytes(l->o); }
+ }
+ int dev = qt_place_of("shexp", i);
+ l->qth_shg = qdw_place(l->sh_g, dev); l->qth_shu = qdw_place(l->sh_u, dev); l->qth_shd = qdw_place(l->sh_d, dev);
+ if (l->qth_shg) { placed++; vram += (double)qdw_bytes(l->sh_g); }
+ if (l->qth_shu) { placed++; vram += (double)qdw_bytes(l->sh_u); }
+ if (l->qth_shd) { placed++; vram += (double)qdw_bytes(l->sh_d); }
+ }
+ if (vram_bytes) *vram_bytes = vram;
+ return placed;
+}
+
static void layers_forward_range(Model *m, float *x, int S, int pos_base,
int layer_begin, int layer_end,
int allow_prefetch, FILE *lf) {
@@ -3496,6 +3588,7 @@ int main(int argc, char **argv) {
if (have == 2)
qt_trunk_offer("dnproj", i, (size_t)(O_qkv + O_z) * m.c.hidden + (size_t)(O_qkv + O_z) * sizeof(float));
}
+ trunk_offer_dense(&m); /* dnout, attnproj, shexp: the rest of the per-token dense work */
}
if (qt_init(m.c.n_layers, m.c.n_experts, m.c.hidden, m.c.inter, cap, m.c.topk,
m.c.expert_gs, expert_is_int4)) {
@@ -3544,6 +3637,15 @@ int main(int argc, char **argv) {
fprintf(stderr, "[dnp] %d DeltaNet-Projektionen auf GPU (%.2f GB VRAM)\n",
placed, vram / 1073741824.0);
}
+ /* The rest of the trunk, wherever the placer put it: out_proj,
+ * attention projections, shared expert. Handles live in the Layer. */
+ {
+ double vram = 0;
+ int placed = trunk_place_dense(&m, &vram);
+ if (placed)
+ fprintf(stderr, "[dense] %d trunk matrices on GPU (dnout/attnproj/shexp, %.2f GB VRAM)\n",
+ placed, vram / 1073741824.0);
+ }
/* Warmstart: fill the VRAM budget BEFORE the first token (heat order
* when HEAT_FILE exists, natural order otherwise), loading all RAM
* slots along the way. */
diff --git a/c/qwen36_tier.c b/c/qwen36_tier.c
index e06c2de6..7322dae9 100644
--- a/c/qwen36_tier.c
+++ b/c/qwen36_tier.c
@@ -531,7 +531,7 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,
* the caller repeat every device in COLI_GPUS as well -- forgetting that
* would silently drop a component back to the CPU mid-A/B. */
{
- static const char *comps[] = {"lmhead","dnproj","dnout","attnproj"};
+ static const char *comps[] = {"lmhead","dnproj","dnout","attnproj","shexp"};
for(size_t ci=0; ci<sizeof comps/sizeof *comps; ci++)
for(int l=0; l<nl && G.ndev<QT_MAX_DEV; l++){
int d=qt_place_of(comps[ci],l);
@@ -650,7 +650,7 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,
else fprintf(stderr,"[qtier] lm_head-Device %d nicht verfuegbar -> CPU\n",ld);
}
/* every other component's devices, deduplicated */
- static const char *comps[] = {"dnproj","dnout","attnproj"};
+ static const char *comps[] = {"dnproj","dnout","attnproj","shexp"};
for(size_t ci=0; ci<sizeof comps/sizeof *comps; ci++)
for(int l=0; l<nl; l++){
int d=qt_place_of(comps[ci],l);
diff --git a/c/tests/test_qwen36_trunk_dense.c b/c/tests/test_qwen36_trunk_dense.c
new file mode 100644
index 00000000..907f19a5
--- /dev/null
+++ b/c/tests/test_qwen36_trunk_dense.c
@@ -0,0 +1,159 @@
+/* The rest of the dense trunk offered to the VRAM placer: "dnout" (DeltaNet
+ * out_proj), "attnproj" (q, k, v, o of an attention layer) and "shexp" (the
+ * shared expert's gate, up, down), placed by name and layer and served from
+ * VRAM through qt_dense handles kept in the Layer.
+ *
+ * What is pinned, on the fake CUDA backend (no GPU, no toolkit):
+ * - trunk_offer_dense offers exactly the components whose every matrix has
+ * a dense-i8 copy, with the bytes of those copies;
+ * - trunk_place_dense uploads what the placer took and keeps one handle per
+ * matrix, counting matrices and bytes;
+ * - a placed matrix answers the same GEMV from VRAM as matmul_d does on the
+ * CPU (same int8 rows, same per-row scales), and a component without a
+ * handle keeps running matmul_d;
+ * - the placer prices every one of them as a dense component: with room for
+ * all, all go; with COLI_PLACE=off nothing is offered at all.
+ * Include order as in tests/test_qwen36_tier_int8_engine.c: the engine, the
+ * fake backend, then the tier, so the tier's statics live in this TU. */
+#define main qwen36_main_unused
+#include "../qwen36.c"
+#undef main
+
+#include "../compat.h" /* setenv/unsetenv: MinGW has neither */
+
+#include "qwen36_fake_cuda.h"
+
+#include "../qwen36_tier.c"
+
+static int fails;
+static void ck(int ok, const char *what) {
+ if (ok) { printf(" ok %s\n", what); return; }
+ printf(" FAIL %s\n", what);
+ fails++;
+}
+
+/* Small but every dimension distinct, so a swapped I/O would show. */
+enum { NL = 2, D = 48, VH = 2, VD = 8, QH = 2, QD = 12, KVH = 1, KD = 8, SH = 20, NE = 4, IH = 16 };
+
+static unsigned g_seed = 12345;
+static float rnd(void) {
+ g_seed = g_seed * 1103515245u + 12345u;
+ return ((g_seed >> 8) & 0xFFFF) / 32768.f - 1.f;
+}
+static float *rnd_matrix(int rows, int cols) {
+ float *w = malloc((size_t)rows * cols * sizeof(float));
+ for (size_t i = 0; i < (size_t)rows * cols; i++) w[i] = rnd();
+ return w;
+}
+
+/* One GEMV both ways: from VRAM through the handle and on the CPU through
+ * matmul_d. The fake backend computes exactly what a real one does with these
+ * bytes (x . int8 row, times the row scale), so the two must agree to float
+ * accumulation order. */
+static double gemv_gap(int hp1, const float *W, int I, int O, int *served) {
+ float *x = rnd_matrix(1, I), *ya = calloc((size_t)O, sizeof(float)), *yb = calloc((size_t)O, sizeof(float));
+ *served = qtd(hp1, ya, x, I, O);
+ matmul_d(yb, x, W, 1, I, O);
+ double worst = 0, scale = 1e-6;
+ for (int o = 0; o < O; o++) {
+ double d = fabs((double)ya[o] - yb[o]);
+ if (d > worst) worst = d;
+ if (fabs((double)yb[o]) > scale) scale = fabs((double)yb[o]);
+ }
+ free(x); free(ya); free(yb);
+ return worst / scale;
+}
+
+static void build(Model *m) {
+ memset(m, 0, sizeof *m);
+ Cfg *c = &m->c;
+ c->n_layers = NL; c->hidden = D; c->n_experts = NE; c->inter = IH; c->topk = 1; c->expert_gs = 0;
+ c->q_heads = QH; c->q_head_dim = QD; c->kv_heads = KVH; c->k_head_dim = KD; c->head_dim = KD; c->o_in = QH * KD;
+ c->dn_vheads = VH; c->dn_vdim = VD; c->shared_inter = SH;
+ c->is_attn = calloc(NL, 1); c->is_attn[1] = 1;
+ m->L = calloc(NL, sizeof(Layer));
+ Layer *dn = &m->L[0], *at = &m->L[1];
+ dn->dn_out = rnd_matrix(D, VH * VD); qdw_register(dn->dn_out, VH * VD, D);
+ dn->sh_g = rnd_matrix(SH, D); qdw_register(dn->sh_g, D, SH);
+ dn->sh_u = rnd_matrix(SH, D); qdw_register(dn->sh_u, D, SH);
+ dn->sh_d = rnd_matrix(D, SH); qdw_register(dn->sh_d, SH, D);
+ at->q = rnd_matrix(QH * QD, D); qdw_register(at->q, D, QH * QD);
+ at->k = rnd_matrix(KVH * KD, D); qdw_register(at->k, D, KVH * KD);
+ at->v = rnd_matrix(KVH * KD, D); qdw_register(at->v, D, KVH * KD);
+ at->o = rnd_matrix(D, QH * KD); qdw_register(at->o, QH * KD, D);
+ /* the attention layer's shared expert is INCOMPLETE on purpose: gate and
+ * up have a dense-i8 copy, down does not (never registered), so "shexp"
+ * for layer 1 must not be offered and its three handles must stay 0 */
+ at->sh_g = rnd_matrix(SH, D); qdw_register(at->sh_g, D, SH);
+ at->sh_u = rnd_matrix(SH, D); qdw_register(at->sh_u, D, SH);
+ at->sh_d = rnd_matrix(D, SH); /* no qdw_register */
+}
+
+int main(void) {
+ setenv("COLI_CUDA", "1", 1); setenv("COLI_GPUS", "0", 1);
+ setenv("QT_NO_WARMSTART", "1", 1); setenv("HEAT_FILE", "", 1);
+ setenv("COLI_PLACE", "", 1); /* "" == unset == auto */
+ setenv("CUDA_EXPERT_GB", "1", 1); /* room for everything */
+ unsetenv("COLI_DENSE_I8");
+ fake_ndev = 1; fake_uploads = 0; fake_dense_compute = 1;
+
+ Model m; build(&m);
+ Layer *dn = &m.L[0], *at = &m.L[1];
+
+ printf("offers\n");
+ int before = G_offer_n;
+ trunk_offer_dense(&m);
+ /* dnout(layer 0) + attnproj(layer 1) + shexp(layer 0); NOT shexp(layer 1) */
+ ck(G_offer_n - before == 3, "three components offered: dnout, attnproj, shexp of the complete layer only");
+ size_t want_attn = qdw_bytes(at->q) + qdw_bytes(at->k) + qdw_bytes(at->v) + qdw_bytes(at->o);
+ int seen_attn = 0, seen_dnout = 0, seen_shexp1 = 0;
+ for (int o = before; o < G_offer_n; o++) {
+ if (!strcmp(G_offer[o].name, "attnproj") && G_offer[o].layer == 1 && G_offer[o].bytes == want_attn) seen_attn = 1;
+ if (!strcmp(G_offer[o].name, "dnout") && G_offer[o].layer == 0 && G_offer[o].bytes == qdw_bytes(dn->dn_out)) seen_dnout = 1;
+ if (!strcmp(G_offer[o].name, "shexp") && G_offer[o].layer == 1) seen_shexp1 = 1;
+ }
+ ck(seen_dnout, "dnout offered for the DeltaNet layer with the bytes of its dense-i8 copy");
+ ck(seen_attn, "attnproj offered for the attention layer as the sum of q, k, v, o");
+ ck(!seen_shexp1, "a shared expert missing one dense-i8 copy is not offered");
+ ck(qdw_bytes(at->sh_d) == 0, "a matrix without a dense-i8 copy reports zero bytes");
+
+ printf("placement\n");
+ ck(qt_init(NL, NE, D, IH, NE, 1, 0, 1), "tier starts (int4 mode, cap == n_experts)");
+ ck(qt_place_of("dnout", 0) == 0 && qt_place_of("attnproj", 1) == 0 && qt_place_of("shexp", 0) == 0,
+ "every offered component placed on the one device with room");
+ ck(qt_place_of("shexp", 1) == QT_PLACE_CPU, "the component never offered stays on the CPU");
+ double vram = 0;
+ int placed = trunk_place_dense(&m, &vram);
+ ck(placed == 8, "eight matrices placed: dnout, q k v o, and the complete shared expert");
+ ck(vram == (double)(qdw_bytes(dn->dn_out) + want_attn + qdw_bytes(dn->sh_g) + qdw_bytes(dn->sh_u) + qdw_bytes(dn->sh_d)),
+ "placed bytes are the sum of the dense-i8 copies uploaded");
+ ck(fake_uploads == 8, "one upload per placed matrix");
+ ck(dn->qth_dnout > 0 && at->qth_q > 0 && at->qth_k > 0 && at->qth_v > 0 && at->qth_o > 0, "handles kept in the Layer");
+ ck(dn->qth_shg > 0 && dn->qth_shu > 0 && dn->qth_shd > 0, "shared expert handles kept in the DeltaNet layer");
+ ck(at->qth_shg == 0 && at->qth_shu == 0 && at->qth_shd == 0, "no handle for the shared expert that was not offered");
+
+ printf("GEMV from VRAM == matmul_d on the CPU\n");
+ struct { const char *name; int hp1; const float *w; int I, O; } mats[] = {
+ {"dn_out", dn->qth_dnout, dn->dn_out, VH * VD, D},
+ {"q", at->qth_q, at->q, D, QH * QD}, {"k", at->qth_k, at->k, D, KVH * KD},
+ {"v", at->qth_v, at->v, D, KVH * KD}, {"o", at->qth_o, at->o, QH * KD, D},
+ {"sh_g", dn->qth_shg, dn->sh_g, D, SH}, {"sh_u", dn->qth_shu, dn->sh_u, D, SH}, {"sh_d", dn->qth_shd, dn->sh_d, SH, D},
+ };
+ for (size_t i = 0; i < sizeof mats / sizeof mats[0]; i++) {
+ int served = 0;
+ double gap = gemv_gap(mats[i].hp1, mats[i].w, mats[i].I, mats[i].O, &served);
+ char what[128];
+ snprintf(what, sizeof what, "%s: served from VRAM, relative gap %.2e", mats[i].name, gap);
+ ck(served && gap < 1e-4, what);
+ }
+ {
+ int served = 1; float x[D] = {0}, y[SH] = {0};
+ served = qtd(at->qth_shd, y, x, SH, D);
+ ck(!served, "a matrix with no handle is not served (the caller runs matmul_d)");
+ }
+
+ qt_shutdown();
+ if (fails) { printf("test_qwen36_trunk_dense: %d failure(s)\n", fails); return 1; }
+ printf("OK test_qwen36_trunk_dense: dnout, attnproj and shexp offered, placed and served from VRAM\n");
+ return 0;
+}
diff --git a/docs/qwen36-cuda-tier.md b/docs/qwen36-cuda-tier.md
index 57e6a195..1bc894c9 100644
--- a/docs/qwen36-cuda-tier.md
+++ b/docs/qwen36-cuda-tier.md
@@ -60,10 +60,29 @@ the trunk on the CPU is why a 6 GB card sees the hit rate stop mattering
(#1040): the GPU is doing the cheap job.
By default the engine now places the trunk itself. Before the tier decides its
-budget, the engine offers each trunk component with its size (lm_head once, the
-fused DeltaNet projection of every DeltaNet layer), and the tier prices them
-against the experts they would displace, in **bytes saved on the memory bus per
-token, per byte of VRAM**:
+budget, the engine offers each trunk component with its size, and the tier
+prices them against the experts they would displace, in **bytes saved on the
+memory bus per token, per byte of VRAM**. The components, with their int8 size
+on Qwen3.6-35B-A3B (hidden 2048, 30 DeltaNet and 10 attention layers):
+
+| component | what | per layer | total |
+|---|---|---|---|
+| `lmhead` | the output head, once | | 508 MB |
+| `dnproj` | DeltaNet in_proj qkv ++ z, fused | 25.2 MB | 755 MB |
+| `dnout` | DeltaNet out_proj | 8.4 MB | 252 MB |
+| `attnproj` | attention q, k, v, o (one item, four matrices) | 27.3 MB | 273 MB |
+| `shexp` | the shared expert's gate, up, down | 3.1 MB | 126 MB |
+
+Offer order is the placement priority once the budget runs short: `lmhead`,
+then `dnproj`, `dnout`, `attnproj`, `shexp`, each in layer order, so a partial
+placement is whole layers. `dnout`, `attnproj` and `shexp` were added after
+the M10 datapoint in #1652 showed the un-offloaded dense path as the ceiling
+on a CPU without AVX2; measured on a 16-core CPU, of the 37.5 ms a decoded
+token spends in the DeltaNet stack 23.4 are the input projections, 8.3 the
+out_proj and norm, 3.3 the convolution and 2.4 the recurrence -- the matmuls,
+not the recurrence, are what the trunk costs. They are served from VRAM on
+decode (one GEMV each); a prompt batch keeps the batched CPU matmul. The
+pricing rule:
- a dense component is read every token: 1.0 per byte;
- a routed expert is read with the probability a token routes to it -- its heat
@@ -82,6 +101,7 @@ of that device's expert budget, and each decision prints as a `[place]` line.
| unset or `auto` | automatic, as above |
| `off` | nothing placed: experts only (the behaviour before this) |
| `lmhead=0,dnproj=0:20+1:20,experts=0` | hand-written list (the measurement tool); obeyed as written, trunk bytes still charged to the budget |
+| `dnout=0,attnproj=0,shexp=0` | the same list form for the newer components; any of the five names, per layer or split with `+` |
First calibration, one Quadro RTX 4000 (8 GB), per-row int4 container, 200-token
decode, same prompt, output bit-identical in all four runs: