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

fix(qwen36): don't evict an expert a moe run is still reading

Marcello Costagliolac57e52714e68
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 886d0182..6836332b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -599,6 +599,49 @@ jobs:
# COLI_CUDA=1 on a mixed container is refused with a line, the CPU path stands
COLI_CUDA=1 COLI_DENSE_I8=0 SNAP=qwen36_tiny64_d8 ./qwen36 8 4 qwen36_tiny64/ref_full.json > mixed_cuda.log 2>&1 || true
grep -q "COLI_CUDA=1 ignored: the VRAM expert tier does not take the mixed layout" mixed_cuda.log || { echo "FAIL: tier refusal line missing"; cat mixed_cuda.log; exit 1; }
+ - name: Pinned experts and PILOT prefetch never change the logits (int4 gs64 fixture)
+ run: |
+ cd c
+ # HOT and COLIBRI_RESIDENT pin experts, PILOT loads the next layers' from a
+ # worker thread: they decide which experts stay in RAM, never what is
+ # computed, so the last logits must be the bytes of cap=16 without them.
+ # At a small cap they were not: a moe run read its experts from slots that
+ # nothing marked busy, so the run's own next load (fewer unpinned slots than
+ # topk) or the worker could reload one under the kernel. cap starts at 2 =
+ # topk: this step is about eviction, the cap=1 cut is the A/B step's.
+ B=./qwen36
+ run() { # tag kernel cap [VAR=value...]
+ local tag=$1 k=$2 cap=$3; shift 3
+ env "$@" QWEN_EXPERT_KERNEL=$k COLI_DENSE_I8=0 SNAP=qwen36_tiny64_c DUMP=pin_$tag.f32 \
+ $B "$cap" 4 qwen36_tiny64/ref_full.json > pin_$tag.log 2>&1 || true
+ test -s pin_$tag.f32 || { echo "FAIL: no logits dump ($tag)"; cat pin_$tag.log; exit 1; }
+ }
+ fail=0
+ for k in 0 1; do
+ run ref_$k $k 16
+ for cap in 2 3 4; do
+ for hot in 1 2 3; do
+ run k${k}_cap${cap}_hot$hot $k $cap HOT=$hot
+ grep -q "^\[HOT\] Pinned [1-9]" pin_k${k}_cap${cap}_hot$hot.log || { echo "FAIL: HOT pinned nothing"; exit 1; }
+ done
+ for r in 1 2; do
+ run k${k}_cap${cap}_res$r $k $cap COLIBRI_RESIDENT=$r
+ grep -q "^\[RESIDENT\] Pinned [1-9]" pin_k${k}_cap${cap}_res$r.log || { echo "FAIL: RESIDENT pinned nothing"; exit 1; }
+ done
+ done
+ # PILOT races a loader thread against the moe run: repeat it
+ for i in $(seq 10); do
+ run k${k}_pilot1_$i $k 2 PILOT=1
+ run k${k}_pilot3_$i $k 2 PILOT=3 WIDE=4
+ done
+ n=0 bad=0
+ for f in pin_k${k}_*.f32; do
+ n=$((n+1)); cmp -s "$f" pin_ref_$k.f32 || { echo "kernel=$k: ${f#pin_k${k}_} differs from cap=16"; bad=$((bad+1)); }
+ done
+ echo "kernel=$k: $((n-bad))/$n runs give the cap=16 logits"
+ [ $bad -eq 0 ] || fail=1
+ done
+ exit $fail
- name: A malformed container is refused, not read
run: |
cd c
diff --git a/c/qwen36.c b/c/qwen36.c
index e7515014..077c5106 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -731,7 +731,8 @@ typedef struct {
/* pw: the expert as expert_ffn.h wants it (planar int4, gate|up|down), the
* only weight copy a slot holds when the shared kernel is active; g/u/d and
* g4/u4/d4 are then NULL. */
-typedef struct { int eid; int pinned; int is_int4; int8_t *g, *u, *d; uint8_t *g4, *u4, *d4; uint8_t *pw; float *gs, *us, *ds; uint64_t used; } Slot;
+typedef struct { int eid; int pinned; int is_int4; int8_t *g, *u, *d; uint8_t *g4, *u4, *d4; uint8_t *pw; float *gs, *us, *ds; uint64_t used;
+ unsigned busy; /* callers computing from this slot (expert_hold): never evicted */ } Slot;
typedef struct {
Slot *slots;
int *slot_by_expert; /* expert id -> resident slot, -1 if absent */
@@ -843,6 +844,33 @@ static void cache_publish(Model *m, int layer, Slot *s, int eid) {
lc->slot_by_expert[eid] = (int)(s - lc->slots);
}
+/* A slot someone is computing from, as colibri.c's ESlot.in_flight: taken
+ * under g_pilot_mx at lookup (expert_hold), dropped without it when the
+ * compute is done. A release without its hold would wrap the count and
+ * leave the slot unevictable for good, so it stops here instead. */
+static void slot_hold(Slot *s) { __atomic_add_fetch(&s->busy, 1, __ATOMIC_ACQ_REL); }
+static void slot_release(Slot *s) {
+ if (__atomic_fetch_sub(&s->busy, 1, __ATOMIC_ACQ_REL) == 0) {
+ fprintf(stderr, "qwen36: expert slot released more often than held\n"); abort();
+ }
+}
+static int slot_busy(const Slot *s) { return __atomic_load_n(&s->busy, __ATOMIC_ACQUIRE) != 0; }
+
+/* The slot a full layer cache gives up, caller holds g_pilot_mx: the least
+ * recently used one that is neither being loaded (eid < 0) nor computed from
+ * (busy), unpinned unless allow_pinned. -1 when there is none. The demand
+ * path and the PILOT worker both pick here, so neither can evict an expert a
+ * moe run is still reading. */
+static int slot_victim(const LCache *lc, int allow_pinned) {
+ int lru = -1;
+ for (int i = 0; i < lc->n; i++) {
+ const Slot *s = &lc->slots[i];
+ if (s->eid < 0 || slot_busy(s) || (s->pinned && !allow_pinned)) continue;
+ if (lru < 0 || s->used < lc->slots[lru].used) lru = i;
+ }
+ return lru;
+}
+
static void ensure_pilot_worker_started(Model *m) {
if (!pilot_m) {
pilot_m = m;
@@ -1913,31 +1941,33 @@ static void ehit_mark(Model *m, int layer, int eid){
}
if(layer>=0&&layer<c->n_layers&&eid>=0&&eid<c->n_experts) ehit[layer][eid]=1;
}
-static void expert_get(Model *m, int layer, int eid, Slot **out) {
+/* hold=1 marks the slot busy before the lock drops, so nothing can evict it
+ * between this lookup and the caller's slot_release. */
+static void expert_fetch(Model *m, int layer, int eid, Slot **out, int hold) {
ehit_mark(m, layer, eid); /* tocca solo m->ehit[layer][eid] */
LCache *lc = &m->cache[layer];
pthread_mutex_lock(&g_pilot_mx);
Slot *hit = slot_indexed(m, layer, eid);
if (hit) {
m->hits++; hit->used = ++m->clock; *out = hit;
+ if (hold) slot_hold(hit);
pthread_mutex_unlock(&g_pilot_mx); return;
}
m->miss++;
Cfg *c = &m->c; Slot *s;
if (lc->n < lc->cap) { s = &lc->slots[lc->n++]; slot_ensure_allocated(m, s); }
else {
- /* LRU eviction — skip pinned and in-flight (eid==-1) slots */
- int lru = -1;
- for (int i = 0; i < lc->n; i++) {
- if (lc->slots[i].pinned || lc->slots[i].eid < 0) continue;
- if (lru < 0 || lc->slots[i].used < lc->slots[lru].used) lru = i;
- }
- if (lru < 0) {
- /* All slots are pinned or in-flight; find the oldest non-in-flight
- * slot (may be pinned, but never one currently being loaded). */
- for (int i = 0; i < lc->n; i++) { if (lc->slots[i].eid < 0) continue; if (lru < 0 || lc->slots[i].used < lc->slots[lru].used) lru = i; }
- }
+ /* LRU eviction: an unpinned slot first, else the oldest pinned one;
+ * never one being loaded or computed from. */
+ int lru = slot_victim(lc, 0);
+ if (lru < 0) lru = slot_victim(lc, 1);
while (lru < 0) {
+ /* Every slot is held or being loaded. Only this thread holds (the
+ * PILOT worker never does) and a moe run holds fewer slots than
+ * cap, so with nothing loading a hold leaked: say so, don't hang. */
+ int loading = 0;
+ for (int i = 0; i < lc->n; i++) if (lc->slots[i].eid < 0) loading = 1;
+ if (!loading) { fprintf(stderr, "qwen36: layer %d: all %d expert slots held, none loading\n", layer, lc->n); exit(1); }
/* EVERY slot is in flight: each buffer is owned by an unlocked pread
* in the pilot worker (or a demand load) that will publish into it.
* The old last resort (lru=0) stole such a slot mid-load — two writers
@@ -1954,10 +1984,7 @@ static void expert_get(Model *m, int layer, int eid, Slot **out) {
pthread_mutex_unlock(&g_pilot_mx);
sleep_ms(1);
pthread_mutex_lock(&g_pilot_mx);
- for (int i = 0; i < lc->n; i++) {
- if (lc->slots[i].eid < 0) continue;
- if (lru < 0 || lc->slots[i].used < lc->slots[lru].used) lru = i;
- }
+ lru = slot_victim(lc, 1);
}
s = &lc->slots[lru]; s->pinned = 0;
}
@@ -1969,8 +1996,13 @@ static void expert_get(Model *m, int layer, int eid, Slot **out) {
pthread_mutex_lock(&g_pilot_mx);
m->t_disk += t_read; /* sotto lock: qui arrivano anche i thread del PILOT */
cache_publish(m, layer, s, eid); s->pinned = m->is_pinned[layer * c->n_experts + eid]; s->used = ++m->clock;
+ if (hold) slot_hold(s);
*out = s; pthread_mutex_unlock(&g_pilot_mx);
}
+static void expert_get(Model *m, int layer, int eid, Slot **out) { expert_fetch(m, layer, eid, out, 0); }
+/* expert_get for a caller that computes from the slot after the lock drops:
+ * it stays resident until slot_release. */
+static Slot *expert_hold(Model *m, int layer, int eid) { Slot *s; expert_fetch(m, layer, eid, &s, 1); return s; }
static void pin_hot_experts(Model *m) {
Cfg *c = &m->c;
@@ -2229,7 +2261,9 @@ static void qwen_shared_experts_cpu(Model *m, Layer *l, const float *x, int S,
* Mirrors HF Qwen3 MoE: softmax(gate), optional group-limited top-k, normalized
* weights, sum routed experts, then add the un-gated shared expert. */
/* One MoE layer through expert_ffn.h. The experts a run holds must all be
- * resident at once, so the batch is cut to what the layer cache can hold:
+ * resident at once: each is held (expert_hold) until the kernel is done with
+ * it, so neither the run's next load nor the PILOT worker can evict it, and
+ * the batch is cut to what the layer cache can hold:
* the whole prompt chunk when cap covers S*K slots, one token when it covers
* K, one (token, expert) pair otherwise (cap=1 in CI evicts on every routed
* expert). A pair run adds val*expert into out exactly as the per-token loop
@@ -2245,18 +2279,19 @@ static void moe_xf_run(Model *m, int layer, const float *x, int S, float *out, c
XfExpert *ex = malloc(sizeof(XfExpert) * (size_t)n);
const XfExpert **exp = malloc(sizeof(XfExpert *) * (size_t)n);
int *ridx = malloc(sizeof(int) * (size_t)n); float *rval = falloc(n);
+ Slot **held = malloc(sizeof(Slot *) * (size_t)n);
float *tmp = kper < K ? falloc(D) : NULL;
void *scratch = malloc(xf_moe_scratch_bytes(per, kper, D, F));
- if (!ex || !exp || !ridx || !scratch) { fprintf(stderr, "OOM moe_xf_run\n"); exit(1); }
+ if (!ex || !exp || !ridx || !held || !scratch) { fprintf(stderr, "OOM moe_xf_run\n"); exit(1); }
int timed = tm_on() && S == 1;
for (int s0 = 0; s0 < S; s0 += per) {
for (int k0 = 0; k0 < K; k0 += kper) {
double t0 = timed ? tm_now() : 0;
for (int s = 0; s < per; s++) for (int k = 0; k < kper; k++) {
int src = (s0 + s) * K + (k0 + k), dst = s * kper + k;
- ridx[dst] = idx[src]; rval[dst] = val[src]; exp[dst] = NULL;
+ ridx[dst] = idx[src]; rval[dst] = val[src]; exp[dst] = NULL; held[dst] = NULL;
if (idx[src] < 0) continue;
- Slot *e; expert_get(m, layer, idx[src], &e);
+ Slot *e = held[dst] = expert_hold(m, layer, idx[src]);
ex[dst].g4 = e->pw; ex[dst].u4 = e->pw + gp; ex[dst].d4 = e->pw + 2 * gp;
ex[dst].gs = e->gs; ex[dst].us = e->us; ex[dst].ds = e->ds;
exp[dst] = &ex[dst];
@@ -2268,9 +2303,10 @@ static void moe_xf_run(Model *m, int layer, const float *x, int S, float *out, c
float *os = out + (int64_t)s0 * D; for (int d = 0; d < D; d++) os[d] += tmp[d];
}
if (timed) { double t2 = tm_now(); g_xf_load += t1 - t0; g_xf_run += t2 - t1; }
+ for (int i = 0; i < n; i++) if (held[i]) slot_release(held[i]);
}
}
- free(ex); free(exp); free(ridx); free(rval); free(tmp); free(scratch);
+ free(ex); free(exp); free(ridx); free(rval); free(held); free(tmp); free(scratch);
}
/* ---------- CACHE_ROUTE: residency-aware top-K fill (docs/CACHE_ROUTE.md) ----------
@@ -2465,12 +2501,13 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out) {
double _q1 = tm_now();
for (int kk = 0; kk < K; kk++) {
if (qmask & (1u<<kk)) continue;
- Slot *e; expert_get(m, layer, idx[kk], &e);
+ Slot *e = expert_hold(m, layer, idx[kk]);
slot_ensure_int8(m, e);
matmul_qe(g, xs, e->g, e->gs, D, I);
matmul_qe(u, xs, e->u, e->us, D, I);
for (int i = 0; i < I; i++) { float gv = g[i]; g[i] = (gv / (1.f + expf(-gv))) * u[i]; }
matmul_qd(hh, g, e->d, e->ds, I, D);
+ slot_release(e);
float w = val[kk]; float *os = out + (int64_t)s*D;
for (int d = 0; d < D; d++) os[d] += w * hh[d];
}
@@ -2504,12 +2541,13 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out) {
}
} else {
for (int kk = 0; kk < K; kk++) {
- Slot *e; expert_get(m, layer, idx[kk], &e);
+ Slot *e = expert_hold(m, layer, idx[kk]); /* the PILOT worker may not evict it mid-matmul */
slot_ensure_int8(m, e);
matmul_qe(g, xs, e->g, e->gs, D, I);
matmul_qe(u, xs, e->u, e->us, D, I);
for (int i = 0; i < I; i++) { float gv = g[i]; g[i] = (gv / (1.f + expf(-gv))) * u[i]; }
matmul_qd(hh, g, e->d, e->ds, I, D);
+ slot_release(e);
float w = val[kk];
float *os = out + (int64_t)s*D;
for (int d = 0; d < D; d++) os[d] += w * hh[d];
@@ -2982,8 +3020,7 @@ static void pilot_realload(Model *m, int layer, int eid) {
Slot *s;
if (lc->n < lc->cap) { s = &lc->slots[lc->n++]; slot_ensure_allocated(m, s); }
else {
- int lru = -1;
- for (int i = 0; i < lc->n; i++) { if (lc->slots[i].pinned || lc->slots[i].eid < 0) continue; if (lru < 0 || lc->slots[i].used < lc->slots[lru].used) lru = i; }
+ int lru = slot_victim(lc, 0);
if (lru < 0) { m->is_queued[layer*c->n_experts+eid]=0; pthread_mutex_unlock(&g_pilot_mx); return; }
s = &lc->slots[lru]; s->pinned = 0;
}
diff --git a/c/tests/test_qwen36_cache_index.c b/c/tests/test_qwen36_cache_index.c
index df1e90f8..f8757c10 100644
--- a/c/tests/test_qwen36_cache_index.c
+++ b/c/tests/test_qwen36_cache_index.c
@@ -39,6 +39,33 @@ static void check_lookup_scaling(int cap) {
free_cache(&m);
}
+/* The eviction rule the demand path and the PILOT worker share: a slot a moe
+ * run is computing from (held) or one being loaded is never given up, and a
+ * pinned one only when nothing unpinned is left. */
+static void check_victim(void) {
+ Model m; init_cache(&m, 6, 3); LCache *lc = &m.cache[0]; lc->n = 3;
+ for (int i = 0; i < 3; i++) { cache_publish(&m, 0, &lc->slots[i], i); lc->slots[i].used = (uint64_t)(i + 1); }
+ CHECK(slot_victim(lc, 0) == 0, "LRU victim is not the oldest slot");
+
+ Slot *h = expert_hold(&m, 0, 0); /* a resident expert: the hit path holds too */
+ CHECK(h == &lc->slots[0] && slot_busy(h), "expert_hold did not hold the resident slot");
+ CHECK(slot_victim(lc, 0) == 1 && slot_victim(lc, 1) == 1, "a held slot was chosen for eviction");
+
+ lc->slots[1].pinned = 1; slot_hold(&lc->slots[2]); slot_hold(&lc->slots[2]); /* routed twice in one run */
+ CHECK(slot_victim(lc, 0) == -1, "an unpinned victim came from pinned and held slots");
+ CHECK(slot_victim(lc, 1) == 1, "the pinned fallback missed the one pinned free slot");
+ cache_hide(&m, 0, &lc->slots[1]); /* now being loaded */
+ CHECK(slot_victim(lc, 1) == -1, "a slot being loaded was chosen for eviction");
+
+ slot_release(&lc->slots[2]);
+ CHECK(slot_victim(lc, 1) == -1, "one release freed a slot held twice");
+ slot_release(&lc->slots[2]);
+ CHECK(slot_victim(lc, 1) == 2, "a fully released slot stayed unevictable");
+ slot_release(h);
+ CHECK(slot_victim(lc, 1) == 0, "the released oldest slot is not the victim");
+ free_cache(&m);
+}
+
int main(void) {
Model m; init_cache(&m, 6, 2); LCache *lc = &m.cache[0]; lc->n = 2;
cache_publish(&m, 0, &lc->slots[0], 1);
@@ -59,6 +86,7 @@ int main(void) {
"out-of-range lookup was accepted");
free_cache(&m);
+ check_victim();
check_lookup_scaling(44);
check_lookup_scaling(219);
if (failures) { fprintf(stderr,"qwen36 cache index: %d failure(s)\n", failures); return 1; }