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

qwen38: dense trunk matrices as int8 residents on the GPU (stage 1), generic tier dense API

kreuzzelg19ab9340534c
diff --git a/c/Makefile b/c/Makefile
index c0131883..c8b49b02 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1759,6 +1759,10 @@ tests/test_qwen36_tier_autoplace$(EXE): tests/test_qwen36_tier_autoplace.c tests
tests/test_qwen36_tier_fp8$(EXE): tests/test_qwen36_tier_fp8.c tests/qwen36_fake_cuda.h qwen36_tier.c qwen36_tier.h backend_cuda.h compat.h
$(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)
+# Generic resident dense matrices (the Qwen3.8 trunk) and per-offer placement.
+tests/test_qwen36_tier_dense$(EXE): tests/test_qwen36_tier_dense.c tests/qwen36_fake_cuda.h qwen36_tier.c qwen36_tier.h backend_cuda.h
+ $(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)
+
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)
diff --git a/c/qwen36_tier.c b/c/qwen36_tier.c
index 301ae73b..91b316e2 100644
--- a/c/qwen36_tier.c
+++ b/c/qwen36_tier.c
@@ -319,7 +319,7 @@ static struct { ColiCudaTensor *t; int dev, on; } G_dnp[QT_DN_MAX_LAYERS];
* which the hand-written list never did (the 0.7 GB above was the
* discovery). COLI_PLACE=off keeps today's behaviour: nothing placed. */
#define QT_OFFER_MAX 1024
-static struct { char name[16]; int layer; size_t bytes; } G_offer[QT_OFFER_MAX];
+static struct { char name[16]; int layer; size_t bytes; int dev; } G_offer[QT_OFFER_MAX];
static int G_offer_n;
static int G_auto_on; /* auto placement decided */
static int G_auto_lmh = QT_PLACE_CPU;
@@ -328,10 +328,12 @@ static size_t G_trunk_bytes[QT_MAX_DEV]; /* placed trunk per devic
int qt_place_of(const char *component, int layer){
if(G_auto_on){
- if(!strcmp(component, "lmhead")) return G_auto_lmh;
- if(!strcmp(component, "dnproj"))
- return (layer >= 0 && layer < QT_DN_MAX_LAYERS) ? G_auto_dnp[layer] : QT_PLACE_CPU;
- return QT_PLACE_CPU; /* experts follow COLI_GPUS; dnout/attnproj not yet placed */
+ /* the decision lives on the offer: any component name an engine
+ * offered can be asked for (lmhead/dnproj for qwen36, the trunk
+ * matrices of qwen38 by their own names) */
+ for(int o = 0; o < G_offer_n; o++)
+ if(G_offer[o].layer == layer && !strcmp(G_offer[o].name, component)) return G_offer[o].dev;
+ return QT_PLACE_CPU; /* experts follow COLI_GPUS */
}
if(!G_place_done) place_parse();
for(int i = 0; i < G_place_n; i++){
@@ -353,7 +355,7 @@ void qt_trunk_offer(const char *component, int layer, size_t bytes){
if(!component || !bytes || G_offer_n >= QT_OFFER_MAX) return;
if(layer < 0 || layer >= QT_DN_MAX_LAYERS) return;
snprintf(G_offer[G_offer_n].name, sizeof G_offer[0].name, "%s", component);
- G_offer[G_offer_n].layer = layer; G_offer[G_offer_n].bytes = bytes;
+ G_offer[G_offer_n].layer = layer; G_offer[G_offer_n].bytes = bytes; G_offer[G_offer_n].dev = QT_PLACE_CPU;
G_offer_n++;
}
@@ -414,11 +416,15 @@ static void auto_place(int nl, int ne, int topk, const size_t *capacity, const u
int placed = 0, kept = 0;
/* lmhead first (one call per token, latency-tolerant), then the
* projections in layer order */
+ for(int o = 0; o < G_offer_n; o++) G_offer[o].dev = QT_PLACE_CPU;
+ /* lmhead first (one call per token, latency-tolerant), then every other
+ * offered component in offer order -- the engine offers in layer order,
+ * so a partial placement is a prefix of the layers (#1361: whole
+ * layers, never a slice of the stream) */
for(int pass = 0; pass < 2; pass++)
for(int o = 0; o < G_offer_n; o++){
int is_lmh = !strcmp(G_offer[o].name, "lmhead");
if((pass == 0) != is_lmh) continue;
- if(!is_lmh && strcmp(G_offer[o].name, "dnproj")) continue; /* v1: these two */
size_t bytes = G_offer[o].bytes;
int di = 0;
for(int i = 1; i < G.ndev; i++) if(room[i] > room[di]) di = i;
@@ -432,7 +438,9 @@ static void auto_place(int nl, int ne, int topk, const size_t *capacity, const u
G_offer[o].name, G_offer[o].layer, bytes/1048576.0, k, lose/1048576.0, G.dev[di], pm);
kept++; continue;
}
- if(is_lmh) G_auto_lmh = G.dev[di]; else G_auto_dnp[G_offer[o].layer] = G.dev[di];
+ G_offer[o].dev = G.dev[di];
+ if(is_lmh) G_auto_lmh = G.dev[di];
+ else if(!strcmp(G_offer[o].name, "dnproj")) G_auto_dnp[G_offer[o].layer] = G.dev[di];
room[di] -= bytes; G_trunk_bytes[di] += bytes; placed++;
}
G_auto_on = 1;
@@ -744,6 +752,36 @@ int qt_dnproj_init(int layer, const int8_t *q, const float *sc,
return 1;
}
+/* ---- generic resident dense matrices (Qwen3.8 trunk) -----------------------
+ * Same mechanism as lmhead/dnproj -- an int8 per-row tensor resident on one
+ * device, one GEMV per call -- but addressed by a handle the engine keeps in
+ * its weight, so any matrix of any layer can live on the GPU without the
+ * tier learning its name. The engine offers sizes through qt_trunk_offer(),
+ * asks qt_place_of() where each went, and hands the quantized bytes here. */
+#define QT_DENSE_MAX 1024
+static struct { ColiCudaTensor *t; int dev, on; size_t bytes; } G_dense[QT_DENSE_MAX];
+static int G_dense_n;
+int qt_dense_init(const int8_t *q, const float *sc, int I, int O, int device){
+ if(device == QT_PLACE_CPU || !q || !sc || I <= 0 || O <= 0) return -1;
+ if(G_dense_n >= QT_DENSE_MAX) return -1;
+ int h = G_dense_n;
+ if(!coli_cuda_tensor_upload(&G_dense[h].t, q, sc, 1, I, O, device)){
+ fprintf(stderr,"[dense] upload [%d x %d] to dev %d failed -> stays on CPU\n", O, I, device);
+ return -1;
+ }
+ G_dense[h].dev = device; G_dense[h].on = 1; G_dense[h].bytes = (size_t)I*O + (size_t)O*sizeof(float);
+ G_dense_n++;
+ return h;
+}
+int qt_dense_matmul(int h, float *y, const float *x, int I, int O){
+ if(h < 0 || h >= G_dense_n || !G_dense[h].on) return 0;
+ if(coli_cuda_matmul(&G_dense[h].t, y, x, NULL, NULL, 1, 1, I, O, G_dense[h].dev, 0)) return 1;
+ fprintf(stderr,"[dense] handle %d GPU matmul failed; CPU from here on\n", h);
+ G_dense[h].on = 0;
+ return 0;
+}
+int qt_dense_count(void){ return G_dense_n; }
+
int qt_dnproj_matmul(int layer, float *y, const float *x, int I, int O){
if(layer < 0 || layer >= QT_DN_MAX_LAYERS || !G_dnp[layer].on) return 0;
if(coli_cuda_matmul(&G_dnp[layer].t,y,x,NULL,NULL,1,1,I,O,G_dnp[layer].dev,0))
@@ -1089,7 +1127,12 @@ void qt_stats(void){
(unsigned long long)calls,(unsigned long long)ex,h2d,kms,d2h); }
}
+static void dense_free_all(void){
+ for(int h = 0; h < G_dense_n; h++){ if(G_dense[h].t) coli_cuda_tensor_free(G_dense[h].t); G_dense[h].t = NULL; G_dense[h].on = 0; }
+ G_dense_n = 0;
+}
void qt_shutdown(void){
+ dense_free_all();
if(!G.on) return;
const char *hf=getenv("HEAT_FILE");
if(hf){
diff --git a/c/qwen36_tier.h b/c/qwen36_tier.h
index 603132f8..5caef9f6 100644
--- a/c/qwen36_tier.h
+++ b/c/qwen36_tier.h
@@ -72,6 +72,14 @@ void qt_trunk_offer(const char *component, int layer, size_t bytes);
int qt_dnproj_init(int layer, const int8_t *q, const float *sc,
int I, int O, int device);
int qt_dnproj_matmul(int layer, float *y, const float *x, int I, int O);
+/* Generic resident dense matrix (int8 per-row, one GEMV per call), addressed
+ * by a handle: the Qwen3.8 trunk uses this for every matrix it places. Offer
+ * the size with qt_trunk_offer(name, layer, bytes) before qt_init, ask
+ * qt_place_of(name, layer) after it, then hand the quantized bytes here.
+ * Returns the handle (>= 0) or -1 (stays on the CPU). */
+int qt_dense_init(const int8_t *q, const float *sc, int I, int O, int device);
+int qt_dense_matmul(int handle, float *y, const float *x, int I, int O);
+int qt_dense_count(void);
/* fp8 streaming mode (Qwen3.8): experts arrive as e4m3 bytes with 128x128
* block scales and do NOT all fit in RAM. cap may be smaller than n_experts;
@@ -129,6 +137,9 @@ static inline int qt_place_of(const char*a,int b){(void)a;(void)b;return QT_PLA
static inline void qt_trunk_offer(const char*a,int b,size_t c){(void)a;(void)b;(void)c;}
static inline int qt_dnproj_init(int a,const int8_t*b,const float*c,int d,int e,int f){(void)a;(void)b;(void)c;(void)d;(void)e;(void)f;return 0;}
static inline int qt_dnproj_matmul(int a,float*b,const float*c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return 0;}
+static inline int qt_dense_init(const int8_t*a,const float*b,int c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return -1;}
+static inline int qt_dense_matmul(int a,float*b,const float*c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return 0;}
+static inline int qt_dense_count(void){return 0;}
static inline int qt_ready(void){return 0;}
static inline int qt_is_resident(int a,int b){(void)a;(void)b;return 0;}
static inline void qt_shutdown(void){}
diff --git a/c/qwen38_core.h b/c/qwen38_core.h
index 97e6e596..9fca5b8b 100644
--- a/c/qwen38_core.h
+++ b/c/qwen38_core.h
@@ -49,6 +49,7 @@ typedef struct {
int64_t elements, scale_count;
Q38WeightKind kind;
unsigned owns_data:1, owns_scales:1;
+ int gpu; /* 0 = CPU; else 1 + tier handle of an int8 copy resident in VRAM (decode, S == 1) */
} Q38Weight;
typedef struct { float *norm; Q38Weight down, up, inject; } GatedResidual;
@@ -263,6 +264,11 @@ static void q38_matmul_bf16(float *y,const float *x,const uint16_t *W,
static void q38_weight_matmul(float *y,const float *x,const Q38Weight *weight,
int S,int I,int O) {
+ /* A matrix the tier placed in VRAM (q38_trunk_place) answers a decode
+ * GEMV from there; prefill rows and any failure take the CPU path below,
+ * so the BF16 copy stays the reference for everything but S == 1. */
+ if(S==1&&weight&&weight->gpu&&weight->rows==O&&weight->cols==I&&
+ qt_dense_matmul(weight->gpu-1,y,x,I,O))return;
if(!weight||weight->rows!=O||weight->cols!=I||!weight->data){
fprintf(stderr,"invalid matmul weight: have [%d,%d] kind=%d, need [%d,%d]\n",
weight?weight->rows:0,weight?weight->cols:0,
@@ -1694,6 +1700,87 @@ static void q38_tier_note(int layer,int eid,const Slot *ex) {
(const uint8_t*)ex->down.data,ex->gate.scales,ex->up.scales,ex->down.scales);
}
+/* ---- dense trunk on the GPU (R7b, stage 1) ---------------------------------
+ * The BF16 trunk is the largest fixed cost of a decode token here (about a
+ * third), and it is bandwidth-bound. Every matrix of at least 1 MiB is
+ * offered to the tier's placer by name and layer before qt_init; whatever
+ * the placer accepts is quantized to int8 per row at start (scale = max|w| /
+ * 127, the qwen36 dnproj/lmhead format) and uploaded once; the BF16 copy
+ * stays for prefill and as fallback. Q38_TRUNK_GPU=0 keeps the trunk on the
+ * CPU (parity runs against the BF16 reference). */
+typedef struct { Q38Weight *w; char name[16]; int layer; } Q38TrunkItem;
+static Q38TrunkItem *g_trunk; static int g_trunk_n, g_trunk_cap;
+static void q38_trunk_add(Q38Weight *w,const char *name,int layer) {
+ if(!w||!w->data||(w->kind!=Q38_WEIGHT_BF16&&w->kind!=Q38_WEIGHT_F32))return;
+ size_t bytes=(size_t)w->rows*w->cols+(size_t)w->rows*sizeof(float);
+ if(bytes<(1u<<20))return; /* a round trip costs more than a tiny GEMV saves */
+ if(g_trunk_n==g_trunk_cap){
+ g_trunk_cap=g_trunk_cap?2*g_trunk_cap:256;
+ g_trunk=(Q38TrunkItem*)realloc(g_trunk,(size_t)g_trunk_cap*sizeof(*g_trunk));
+ if(!g_trunk){fprintf(stderr,"OOM trunk table\n");exit(1);}
+ }
+ Q38TrunkItem *it=&g_trunk[g_trunk_n++]; it->w=w; it->layer=layer;
+ snprintf(it->name,sizeof it->name,"%s",name);
+ qt_trunk_offer(it->name,layer,bytes);
+}
+static int q38_trunk_enabled(void) {
+ const char *e=getenv("Q38_TRUNK_GPU"); return !(e&&e[0]=='0'&&!e[1]);
+}
+/* offers, before qt_init: lm_head first (the placer takes it first), then the
+ * layers in order so a partial placement is a prefix of the layers */
+static void q38_trunk_offer_all(Model *m) {
+ if(!q38_trunk_enabled())return;
+ Cfg *c=&m->c;
+ q38_trunk_add(&m->lm_head,"lmhead",0);
+ for(int l=0;l<c->layers;l++){
+ Layer *L=&m->L[l];
+ if(c->is_attn[l]){
+ q38_trunk_add(&L->q,"attnq",l); q38_trunk_add(&L->k,"attnk",l);
+ q38_trunk_add(&L->v,"attnv",l); q38_trunk_add(&L->o,"attno",l);
+ q38_trunk_add(&L->idx_qk,"qsaidx",l);
+ } else {
+ q38_trunk_add(&L->dn_qkv,"dnqkv",l); q38_trunk_add(&L->dn_z,"dnz",l);
+ q38_trunk_add(&L->dn_out,"dnout",l);
+ }
+ q38_trunk_add(&L->attn_gr.down,"hcad",l); q38_trunk_add(&L->attn_gr.up,"hcau",l);
+ q38_trunk_add(&L->attn_gr.inject,"hcai",l);
+ q38_trunk_add(&L->mlp_gr.down,"hcmd",l); q38_trunk_add(&L->mlp_gr.up,"hcmu",l);
+ q38_trunk_add(&L->mlp_gr.inject,"hcmi",l);
+ q38_trunk_add(&L->sh_g,"shg",l); q38_trunk_add(&L->sh_u,"shu",l); q38_trunk_add(&L->sh_d,"shd",l);
+ q38_trunk_add(&L->router,"router",l);
+ }
+}
+/* after qt_init: quantize and upload what the placer accepted */
+static void q38_trunk_place_all(Model *m) {
+ (void)m;
+ int placed=0; size_t placed_bytes=0; double t0=now_s();
+ for(int i=0;i<g_trunk_n;i++){
+ Q38TrunkItem *it=&g_trunk[i]; Q38Weight *w=it->w;
+ int dev=qt_place_of(it->name,it->layer);
+ if(dev==QT_PLACE_CPU)continue;
+ int O=w->rows,I=w->cols;
+ int8_t *q=(int8_t*)malloc((size_t)O*I); float *sc=(float*)malloc((size_t)O*sizeof(float));
+ if(!q||!sc){fprintf(stderr,"OOM trunk quantization\n");exit(1);}
+ #pragma omp parallel for schedule(static)
+ for(int r=0;r<O;r++){
+ float row[8192]; float *src=row; float *heap=NULL;
+ if(I>8192){heap=(float*)malloc((size_t)I*sizeof(float)); src=heap;}
+ q38_weight_row(w,r,src);
+ float mx=0.f; for(int k=0;k<I;k++){float a=fabsf(src[k]); if(a>mx)mx=a;}
+ float s=mx>0.f?mx/127.f:1.f, inv=1.f/s; sc[r]=s;
+ int8_t *dst=q+(size_t)r*I;
+ for(int k=0;k<I;k++){int v=(int)lrintf(src[k]*inv); if(v>127)v=127; if(v<-127)v=-127; dst[k]=(int8_t)v;}
+ free(heap);
+ }
+ int h=qt_dense_init(q,sc,I,O,dev);
+ free(q); free(sc);
+ if(h>=0){ w->gpu=h+1; placed++; placed_bytes+=(size_t)O*I; }
+ }
+ if(g_trunk_n)
+ fprintf(stderr,"[qtier] qwen38 trunk: %d of %d offered matrices resident as int8 (%.2f GiB) in %.1fs; the rest stays BF16 on the CPU\n",
+ placed,g_trunk_n,placed_bytes/1073741824.0,now_s()-t0);
+}
+
/* Start the tier after the model is loaded. COLI_CUDA=1 turns it on (the
* tier reads that itself); it needs every layer's experts in native FP8 with
* the block-scale bank resident, because the GPU kernels consume exactly the
@@ -1711,9 +1798,11 @@ static void q38_tier_start(Model *m,int cap) {
fprintf(stderr,"[qtier] qwen38: layer %d experts are not native FP8; staying on the CPU\n",layer);
return;
}
+ q38_trunk_offer_all(m); /* sizes only; the placer decides in qt_init */
if(qt_init_fp8(c->layers,c->experts,c->hidden,c->inter,cap,c->topk,E4M3_LUT)){
atexit(qt_shutdown);
fprintf(stderr,"[qtier] qwen38: fp8 expert tier on (RAM LRU %d/layer stays; experts stream to VRAM as they get hot)\n",cap);
+ q38_trunk_place_all(m);
}
}
diff --git a/c/tests/test_qwen36_tier_dense.c b/c/tests/test_qwen36_tier_dense.c
new file mode 100644
index 00000000..1a63c41d
--- /dev/null
+++ b/c/tests/test_qwen36_tier_dense.c
@@ -0,0 +1,76 @@
+/* Generic resident dense matrices and the placer that feeds them, on the
+ * fake backend: any component name an engine offers gets a decision per
+ * offer (not only lmhead/dnproj), qt_place_of answers by name and layer,
+ * qt_dense_init uploads int8 per-row (fmt 1) and hands back a handle, a
+ * failing matmul falls back to the CPU once and stays there, and shutdown
+ * releases the tensors. No GPU, no toolkit. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../compat.h" /* setenv: MinGW has none */
+#include "qwen36_fake_cuda.h"
+
+#include "../qwen36_tier.c"
+
+static int fails;
+static void check(int ok, const char *what) { if (!ok) { printf(" FAIL: %s\n", what); fails++; } }
+
+enum { NL = 2, NE = 8, D = 64, IH = 32, TOPK = 2 };
+
+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; unsetenv has no UCRT64 shim */
+ fake_ndev = 1; fake_uploads = 0;
+
+ /* room for 8 experts plus a little; each offer is 1 expert worth of bytes */
+ size_t exp_bytes = 3 * dev_alloc_footprint((size_t)D * IH / 2) + 3 * dev_alloc_footprint((2 * IH + D) / 3 * sizeof(float));
+ char gb[64]; snprintf(gb, sizeof gb, "%.15f", (double)(8 * exp_bytes + exp_bytes / 2) / 1073741824.0);
+ setenv("CUDA_EXPERT_GB", gb, 1);
+
+ printf(" 1. offers by name, decision per offer\n");
+ qt_trunk_offer("lmhead", 0, exp_bytes);
+ qt_trunk_offer("dnqkv", 0, exp_bytes); qt_trunk_offer("dnz", 0, exp_bytes);
+ qt_trunk_offer("dnqkv", 1, exp_bytes); qt_trunk_offer("dnz", 1, exp_bytes);
+ qt_trunk_offer("hcmu", 1, exp_bytes);
+ check(qt_init(NL, NE, D, IH, NE, TOPK, 0, 1), "tier starts (int4 mode, cap == n_experts)");
+ check(qt_place_of("lmhead", 0) == 0, "lmhead placed on the one device");
+ check(qt_place_of("dnqkv", 0) == 0 && qt_place_of("dnz", 1) == 0, "arbitrary names are placed by name and layer");
+ check(qt_place_of("hcmu", 1) == 0, "a name the tier never heard of before is placed like any other");
+ check(qt_place_of("dnqkv", 5) == QT_PLACE_CPU, "an unoffered layer stays on the CPU");
+ check(qt_place_of("nothing", 0) == QT_PLACE_CPU, "an unoffered name stays on the CPU");
+ pthread_mutex_lock(&G.mx);
+ check(G.budget[0] < 8 * exp_bytes, "placed trunk bytes come out of the expert budget");
+ pthread_mutex_unlock(&G.mx);
+
+ printf(" 2. dense init uploads int8 per row and returns a handle\n");
+ enum { O = 48, I = 96 };
+ static int8_t q[O * I]; static float sc[O];
+ for (int i = 0; i < O * I; i++) q[i] = (int8_t)(i % 251 - 125);
+ for (int r = 0; r < O; r++) sc[r] = 0.01f * (r + 1);
+ int before = fake_uploads;
+ int h = qt_dense_init(q, sc, I, O, qt_place_of("dnqkv", 0));
+ check(h >= 0, "handle returned for a placed matrix");
+ check(fake_uploads == before + 1, "one upload per matrix");
+ check(last_fmt == 1 && last_bytes == (size_t)I * O, "fmt 1 (int8 per row), one byte per element");
+ check(qt_dense_init(q, sc, I, O, QT_PLACE_CPU) == -1, "a CPU placement gets no handle");
+ check(qt_dense_init(NULL, sc, I, O, 0) == -1, "no bytes, no handle");
+ int h2 = qt_dense_init(q, sc, I, O, 0);
+ check(h2 == h + 1 && qt_dense_count() == 2, "handles count up");
+
+ printf(" 3. matmul goes to the backend by handle; unknown handles are refused\n");
+ float x[I], y[O]; for (int i = 0; i < I; i++) x[i] = 1.0f;
+ int mm = fake_matmuls;
+ check(qt_dense_matmul(h, y, x, I, O) == 1 && fake_matmuls == mm + 1, "a placed handle answers through coli_cuda_matmul");
+ check(qt_dense_matmul(h2, y, x, I, O) == 1 && fake_matmuls == mm + 2, "so does the second handle");
+ check(qt_dense_matmul(99, y, x, I, O) == 0 && qt_dense_matmul(-1, y, x, I, O) == 0 && fake_matmuls == mm + 2, "unknown handles are refused without a backend call");
+
+ printf(" 4. shutdown releases the matrices\n");
+ qt_shutdown();
+ check(qt_dense_count() == 0, "dense handles released at shutdown");
+
+ if (fails) { printf("test_qwen36_tier_dense: %d failure(s)\n", fails); return 1; }
+ printf("test_qwen36_tier_dense: ok\n");
+ return 0;
+}
diff --git a/c/tests/test_qwen38_tier_engine.c b/c/tests/test_qwen38_tier_engine.c
index b998f818..5455c198 100644
--- a/c/tests/test_qwen38_tier_engine.c
+++ b/c/tests/test_qwen38_tier_engine.c
@@ -51,6 +51,10 @@ int main(void) {
setenv("QT_UPLOAD_SYNC", "1", 1);
setenv("COLI_PLACE", "off", 1);
setenv("HEAT_FILE", "", 1);
+ /* the fake backend counts matmuls but computes nothing, so a trunk matrix
+ * placed on it would answer garbage; keep the trunk on the CPU here (the
+ * placement itself is pinned by test_qwen36_tier_dense) */
+ setenv("Q38_TRUNK_GPU", "0", 1);
/* room for six of the sixteen experts, so promotion has to swap */
size_t exp_bytes = 3 * dev_alloc_footprint(32 * 8) + 3 * dev_alloc_footprint(1 * sizeof(float));
char gb[64]; snprintf(gb, sizeof gb, "%.15f", (double)(6 * exp_bytes + exp_bytes / 2) / 1073741824.0);