Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
olmoe: the slot the forward pass reads is never the prefetcher's victim
JustVugg0e0f56940d78
diff --git a/c/olmoe.c b/c/olmoe.cindex 4b8a4732..71a5ebec 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -102,8 +102,12 @@ typedef struct {* meccanismo che fa stare GLM-5.2 nei 15 GB. dequant-on-use nel matmul. *//* pinned=1 means this slot is strongly preferred to keep (hot expert); it will* not be evicted during normal LRU eviction, but may be displaced under extreme- * cache pressure when all slots are pinned or in-flight. */-typedef struct { int eid; int pinned; int8_t *g, *u, *d; float *gs, *us, *ds; uint64_t used; } Slot;+ * cache pressure when all slots are pinned or in-flight.+ * busy counts the computations reading the slot, from expert_get to expert_put+ * (under g_pilot_mx): neither eviction takes a busy slot. Without it the PILOT+ * worker could pick the slot the forward pass was multiplying with as its LRU+ * victim and read another expert into it mid-matmul (at cap 1 the only slot). */+typedef struct { int eid; int pinned; int busy; int8_t *g, *u, *d; float *gs, *us, *ds; uint64_t used; } Slot;typedef struct {Slot *slots;int *slot_by_expert; /* expert id -> resident slot, -1 if absent */@@ -874,7 +878,7 @@ static void expert_get(Model *m, int layer, int eid, Slot **out) {hit = slot_indexed(m, layer, eid);}if (hit) {- m->hits++; hit->used = ++m->clock; *out = hit;+ m->hits++; hit->used = ++m->clock; hit->busy++; *out = hit;if (m->last_access) m->last_access[layer * m->c.n_experts + eid] = m->clock;pthread_mutex_unlock(&g_pilot_mx);return;@@ -886,17 +890,17 @@ static void expert_get(Model *m, int layer, int eid, Slot **out) {s = &lc->slots[lc->n++];slot_ensure_allocated(m, s);} else {- /* LRU eviction β skip pinned and in-flight (eid==-1) slots */+ /* LRU eviction β skip pinned, in-flight (eid==-1) and busy slots */int lru = -1;for (int i = 0; i < lc->n; i++) {- if (lc->slots[i].pinned || lc->slots[i].eid < 0) continue;+ if (lc->slots[i].pinned || lc->slots[i].eid < 0 || lc->slots[i].busy) 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 oldest non-in-flight slot- * (may be pinned, but never select one currently being loaded). */+ /* All slots are pinned, in-flight or busy; find oldest non-in-flight slot+ * (may be pinned, but never select one currently being loaded or read). */for (int i = 0; i < lc->n; i++) {- if (lc->slots[i].eid < 0) continue; /* never evict in-flight */+ if (lc->slots[i].eid < 0 || lc->slots[i].busy) continue; /* never evict in-flight */if (lru < 0 || lc->slots[i].used < lc->slots[lru].used) lru = i;}}@@ -912,7 +916,7 @@ static void expert_get(Model *m, int layer, int eid, Slot **out) {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 (lc->slots[i].eid < 0 || lc->slots[i].busy) continue;if (lru < 0 || lc->slots[i].used < lc->slots[lru].used) lru = i;}}@@ -929,11 +933,18 @@ static void expert_get(Model *m, int layer, int eid, Slot **out) {cache_publish(m, layer, s, eid);s->pinned = m->is_pinned[layer * c->n_experts + eid];s->used = ++m->clock;+ s->busy++;if (m->last_access) m->last_access[layer * c->n_experts + eid] = m->clock;*out = s;pthread_cond_broadcast(&g_pilot_cv);pthread_mutex_unlock(&g_pilot_mx);}+/* The computation that expert_get handed the slot to is done reading it. */+static void expert_put(Slot *s) {+ pthread_mutex_lock(&g_pilot_mx);+ s->busy--;+ pthread_mutex_unlock(&g_pilot_mx);+}/* ---------- IMPROVEMENT 2: pin top-N hot experts per layer ---------- */static void pin_hot_experts(Model *m) {@@ -1172,6 +1183,7 @@ static void moe_vk_cpu(Model *m, int layer, const float *x, int n, const int *ibmoe_expert_row(m, e, x + (int64_t)(i / K) * D, g, u, ctb + (int64_t)i * D);pthread_mutex_lock(&g_pilot_mx);if (e->eid == ib[i]) { VktExpertSrc vs = olmoe_vk_src(e); vkt_note(layer, ib[i], &vs); }+ e->busy--; /* expert_put, under the lock already held */pthread_mutex_unlock(&g_pilot_mx);}}@@ -1235,6 +1247,7 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out) {for (int kk = 0; kk < K; kk++) {Slot *e; expert_get(m, layer, idx[kk], &e);moe_expert_row(m, e, xs, g, u, hh);+ expert_put(e);float w = val[kk];float *os = out + (int64_t)s*D;for (int d = 0; d < D; d++) os[d] += w * hh[d];@@ -1386,16 +1399,16 @@ static void pilot_realload(Model *m, int layer, int eid) {s = &lc->slots[lc->n++];slot_ensure_allocated(m, s);} else {- /* LRU eviction β skip pinned and in-flight (eid==-1) slots */+ /* LRU eviction β skip pinned, in-flight (eid==-1) and busy slots */int lru = -1;for (int i = 0; i < lc->n; i++) {- if (lc->slots[i].pinned || lc->slots[i].eid < 0) continue;+ if (lc->slots[i].pinned || lc->slots[i].eid < 0 || lc->slots[i].busy) continue;if (lru < 0 || lc->slots[i].used < lc->slots[lru].used) lru = i;}if (lru < 0) {m->is_queued[layer * c->n_experts + eid] = 0;pthread_mutex_unlock(&g_pilot_mx);- return; /* all pinned/in-flight, skip */+ return; /* all pinned/in-flight/busy, skip */}/* LFRU eviction guard: don't displace a warm resident expert with a speculation */diff --git a/c/tests/test_olmoe_cache_index.c b/c/tests/test_olmoe_cache_index.cindex 8a227173..31067499 100644--- a/c/tests/test_olmoe_cache_index.c+++ b/c/tests/test_olmoe_cache_index.c@@ -184,6 +184,31 @@ static void check_demand_joins_demand(void) {free_loader(&m);}+/* The forward pass is multiplying with the only slot (cap 1) when the prefetcher+ * comes up with another expert for the layer: the prefetch must leave the slot+ * alone until expert_put, then may take it. Before the busy count it read the+ * other expert into the slot under the forward pass's matmul. */+static void check_prefetch_spares_busy(void) {+ Model m; init_cache(&m, 8, 1);+ m.c.inter = 4; m.c.hidden = 4;+ m.is_queued = calloc(8, 1); m.is_pinned = calloc(8, 1);+ g_io_loads = g_io_open = 0; g_io_release = 1; /* no read is held */+ g_test_expert_load = held_load;+ Slot *e = NULL;+ expert_get(&m, 0, 2, &e);+ m.is_queued[6] = 1;+ pilot_realload(&m, 0, 6);+ CHECK(io_get(&g_io_loads) == 1 && e && e->eid == 2 && slot_indexed(&m, 0, 2) == e,+ "the prefetch took the slot the forward pass was reading (%d reads)", io_get(&g_io_loads));+ CHECK(m.is_queued[6] == 0, "the skipped prefetch stayed queued");+ expert_put(e);+ m.is_queued[6] = 1;+ pilot_realload(&m, 0, 6);+ CHECK(io_get(&g_io_loads) == 2 && slot_indexed(&m, 0, 6) == e,+ "after expert_put the prefetch did not take the slot");+ free_loader(&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);@@ -209,6 +234,7 @@ int main(void) {check_demand_joins_prefetch();check_prefetch_skips_demand();check_demand_joins_demand();+ check_prefetch_spares_busy();if (failures) { fprintf(stderr,"olmoe cache index: %d failure(s)\n", failures); return 1; }puts("olmoe cache index: ok");return 0;