Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
Merge pull request #1865 into integ/release-batch-5
JustVuggd8d8290ee243
diff --cc c/olmoe.cindex e0382cb8,1d8f2b0d..ed4b896f--- a/c/olmoe.c+++ b/c/olmoe.c@@@ -588,15 -535,29 +588,34 @@@ static void load_cfg(Cfg *c, const chafree(buf); free(arena);}+/* The parameters of an automatic cache size (cap <= 0), for olm_dho_grow_cap: with the+ * dense weights on the device only, the RAM they held goes to the experts. */- typedef struct { int on; double ram_arg, resident, kv_gb, slot_gb; int layers; } OlmAutoCap;++typedef struct { int on; double ram_arg, resident, slot_gb; int layers; } OlmAutoCap;+static OlmAutoCap g_olm_auto_cap;+- static float *load_t(Model *m, const char *name) {- int64_t n = st_numel(&m->S, name);- if (n < 0) { fprintf(stderr, "missing %s\n", name); exit(1); }- float *p = falloc(n);+ /* rows x columns is the shape the forward uses the tensor with, counted from+ * the config: q/k/v/o are [hidden, hidden], the router [num_experts, hidden],+ * embed_tokens and lm_head [vocab_size, hidden], a norm [hidden] (columns 0).+ * Allocated from the header's element count instead, a tensor shorter than+ * that was read past its end, and one of another shape was used as if it had+ * this one. Exact, like the experts in load_expert_merged(). */+ static float *load_t(Model *m, const char *name, int64_t rows, int64_t columns) {+ st_tensor *t = st_find(&m->S, name);+ if (!t) { fprintf(stderr, "missing %s\n", name); exit(1); }+ if (t->rank != (columns ? 2 : 1) || t->shape[0] != rows || (columns && t->shape[1] != columns)) {+ char got[192] = "[";+ for (int k = 0; k < t->rank; k++)+ snprintf(got + strlen(got), sizeof(got) - strlen(got), "%s%lld", k ? ", " : "",+ (long long)t->shape[k]);+ strncat(got, "]", sizeof(got) - strlen(got) - 1);+ char want[64];+ if (columns) snprintf(want, sizeof(want), "[%lld, %lld]", (long long)rows, (long long)columns);+ else snprintf(want, sizeof(want), "[%lld]", (long long)rows);+ fprintf(stderr, "%s: shape %s, expected %s from config.json, refusing (untrusted container)\n",+ name, got, want);+ exit(1);+ }+ float *p = falloc(t->numel);st_read_f32(&m->S, name, p, 0); /* densa: niente DONTNEED, resta residente */return p;}@@@ -685,15 -645,12 +706,15 @@@ static void model_init_range(Model *m,derived = 1;}if (derived > c->n_experts) derived = c->n_experts;+ if (load_boundaries) { /* the standalone engine: kept for olm_dho_grow_cap */- g_olm_auto_cap = (OlmAutoCap){1, ram_arg, resident, kv_gb, slot_gb, layers};++ g_olm_auto_cap = (OlmAutoCap){1, ram_arg, resident, slot_gb, layers};+ }fprintf(stderr, "[cache] %d slots/layer of %d experts: %.1f GB budget "- "(%s), %.1f GB dense resident, %.1f GB projected KV, "- "%.0f MB per expert\n",+ "(%s), %.1f GB dense resident, %.0f MB per expert; "+ "the KV takes slots back as the context grows\n",derived, c->n_experts, budget,ram_arg > 0.0 ? "RAM_GB" : "88% of what the OS still offers",- resident, kv_gb, slot_gb * 1000.0);+ resident, slot_gb * 1000.0);cap = derived;}m->cache = calloc(c->n_layers, sizeof(LCache));@@@ -1585,126 -1360,6 +1606,129 @@@ static void olmoe_echo(const char *id,#ifdef COLI_VULKAN#include "olmoe_chain.h" /* COLI_VK_CHAIN: every layer's dense chain on the device */++/* ---- dense weights on the device only (COLI_VK_DENSE_HOST) ------------------------+ * With the dense part on the device (the chain, or COLI_VK_DENSE), the attention's+ * q/k/v/o, the router and lm_head go up at start and their f32 host copies are given+ * back; matmul_res then runs them on the device whatever COLI_VK_DENSE says. The CPU+ * multiplies by one only after a lost device: matmul_res first reads it back here+ * (load_t: the same f32 bytes) and keeps it from there on. What keeps its host copy:+ * the embedding (its rows are gathered on the CPU) and the norms. The chain is decided+ * after the tier (olc_start); its decision is taken here silently first, from the same+ * inputs, so the matrices are placed before the tier sizes its budget. */- typedef struct { void **vk; float **field; int64_t n; char name[96]; } OlmDho;++typedef struct { void **vk; float **field; int64_t n, rows, columns; char name[96]; } OlmDho;+static OlmDho *g_olm_dho; static int g_olm_dho_n, g_olm_dho_cap;+static Model *g_olm_dho_model;+static pthread_mutex_t g_olm_dho_mx = PTHREAD_MUTEX_INITIALIZER;+static float *olm_dho_reload(void **vk) {+ pthread_mutex_lock(&g_olm_dho_mx);+ OlmDho *e = NULL;+ for (int i = 0; i < g_olm_dho_n && !e; i++) if (g_olm_dho[i].vk == vk) e = &g_olm_dho[i];+ if (!e || !g_olm_dho_model) { fprintf(stderr, "[VK] olmoe: a dense matrix the device held alone cannot be read back\n"); exit(1); }+ if (!*e->field) {- *e->field = load_t(g_olm_dho_model, e->name);++ *e->field = load_t(g_olm_dho_model, e->name, e->rows, e->columns);+ coli_vk_dense_host_reloaded((size_t)e->n * sizeof(float));+ }+ float *w = *e->field;+ pthread_mutex_unlock(&g_olm_dho_mx);+ return w;+}+static void olm_dho_drop(Model *m, float **field, void **vk, const char *name, int I, int O, size_t *bytes) {+ if (!*field || *vk == (void *)&g_vk_refused) return;+ if (!*vk && !coli_vk_tensor_ensure((ColiVkTensor **)vk, *field, NULL, 10, I, O, 0)) { *vk = &g_vk_refused; return; }+ if (g_olm_dho_n == g_olm_dho_cap) {+ g_olm_dho_cap = g_olm_dho_cap ? 2 * g_olm_dho_cap : 64;+ g_olm_dho = realloc(g_olm_dho, (size_t)g_olm_dho_cap * sizeof *g_olm_dho);+ if (!g_olm_dho) { fprintf(stderr, "OOM dense matrix table\n"); exit(1); }+ }+ OlmDho *e = &g_olm_dho[g_olm_dho_n++];- e->vk = vk; e->field = field; e->n = (int64_t)I * O;++ e->vk = vk; e->field = field; e->n = (int64_t)I * O; e->rows = O; e->columns = I;+ snprintf(e->name, sizeof e->name, "%s", name);+ free(*field); *field = NULL;+ size_t b = (size_t)I * O * sizeof(float);+ coli_vk_dense_host_dropped(b);+ *bytes += b;+ (void)m;+}+/* An automatic cache (cap <= 0) sized with the dense weights in RAM: give the experts+ * what they held. With RAM_GB the process's own resident set shrinks by what was+ * dropped; without it, what the OS offers now is read again (on a discrete GPU it grew+ * by that much; on a device sharing the RAM the device copy took it back). Slots fill+ * lazily, so a layer's cache grows by zeroed slots. */+static void olm_dho_grow_cap(Model *m, size_t dropped) {+ OlmAutoCap *a = &g_olm_auto_cap;+ Cfg *c = &m->c;+ if (!a->on || !m->cache || a->slot_gb <= 0.0) return;+ double resident = a->resident - dropped / 1e9;+ double budget = a->ram_arg > 0.0 ? a->ram_arg : resident + mem_available_gb() * 0.88;- double room = budget - resident - a->kv_gb - 0.5;++ double room = budget - resident - 0.5;++ /* the room the cache shares with the KV grows with it: kv_room_fit takes slots++ * back from the new size as positions are written */++ if (room > 0.0 && (int64_t)(room * 1e9) > m->room_bytes) m->room_bytes = (int64_t)(room * 1e9);+ int derived = room > 0.0 ? (int)(room / a->slot_gb / (double)a->layers) : 1;+ if (derived < 1) derived = 1;+ if (derived > c->n_experts) derived = c->n_experts;+ int was = m->cache[0].cap;+ if (derived <= was) return;+ pthread_mutex_lock(&g_pilot_mx);+ for (int i = 0; i < c->n_layers; i++) {+ LCache *lc = &m->cache[i];+ if (!lc->slots) continue;+ Slot *s = realloc(lc->slots, (size_t)derived * sizeof(Slot));+ if (!s) { fprintf(stderr, "OOM growing the expert cache\n"); exit(1); }+ memset(s + lc->cap, 0, (size_t)(derived - lc->cap) * sizeof(Slot));+ lc->slots = s; lc->cap = derived;+ }+ pthread_mutex_unlock(&g_pilot_mx);+ fprintf(stderr, "[cache] %d slots/layer of %d experts (was %d): the dense weights on the device only gave "+ "%.2f GB back, %.2f GB dense resident now\n", derived, c->n_experts, was, dropped / 1e9, resident);+}+/* Layer i's five matrices (i = -1: lm_head) uploaded if they are not yet, their host+ * copies given back; the bytes given back. */+static size_t g_olm_dho_dropped;+static size_t olm_dho_layer(Model *m, int i) {+ Cfg *c = &m->c; int D = c->hidden, E = c->n_experts;+ size_t dropped = 0;+ char nm[96];+ if (i < 0) olm_dho_drop(m, &m->lm_head, &m->vk_lm_head, "lm_head.weight", D, c->vocab, &dropped);+ else {+ Layer *l = &m->L[i];+ snprintf(nm, sizeof nm, "model.layers.%d.self_attn.q_proj.weight", i); olm_dho_drop(m, &l->q, &l->vk_q, nm, D, D, &dropped);+ snprintf(nm, sizeof nm, "model.layers.%d.self_attn.k_proj.weight", i); olm_dho_drop(m, &l->k, &l->vk_k, nm, D, D, &dropped);+ snprintf(nm, sizeof nm, "model.layers.%d.self_attn.v_proj.weight", i); olm_dho_drop(m, &l->v, &l->vk_v, nm, D, D, &dropped);+ snprintf(nm, sizeof nm, "model.layers.%d.self_attn.o_proj.weight", i); olm_dho_drop(m, &l->o, &l->vk_o, nm, D, D, &dropped);+ snprintf(nm, sizeof nm, "model.layers.%d.mlp.gate.weight", i); olm_dho_drop(m, &l->gate, &l->vk_gate, nm, D, E, &dropped);+ }+ g_olm_dho_dropped += dropped;+ return dropped;+}+/* With the fit of a partial chain (olc_fit_start) only the N layers' matrices (and the+ * head's when it goes up) count: the CPU's are refused. The chain's setup then drops each+ * layer's host copies once the layer is on the device (olc_place), and olm_dho_finish+ * says what was given back and grows an automatic cache by it. */+static void olm_dho_start(Model *m) {+ Cfg *c = &m->c;+ int D = c->hidden, E = c->n_experts;+ size_t bytes = 0;+ for (int i = 0; i < c->n_layers; i++)+ if (m->L[i].vk_q != (void *)&g_vk_refused) bytes += (size_t)(4 * (int64_t)D * D + (int64_t)E * D) * sizeof(float);+ if (m->vk_lm_head != (void *)&g_vk_refused) bytes += (size_t)c->vocab * (size_t)D * sizeof(float);+ int chain = coli_vk_chain_decide(NULL, vkt_wanted() && c->n_experts > 0, OLMOE_CHAIN_IGPU);+ if (g_olc_fit_on) chain = chain && g_olc_fit.n > 0;+ if (!coli_vk_dense_host_decide("olmoe", (chain || coli_vk_dense()) && m->lm_head && bytes, bytes)) return;+ g_olm_dho_model = m;+ if (g_olc_fit_on && chain) return; /* olc_place drops them layer by layer, olm_dho_finish reports */+ for (int i = 0; i < c->n_layers; i++) olm_dho_layer(m, i);+ olm_dho_layer(m, -1);+ coli_vk_dense_host_placed("olmoe", "the embedding (its rows are gathered on the CPU), norms");+ olm_dho_grow_cap(m, g_olm_dho_dropped);+}+static void olm_dho_finish(Model *m) {+ if (!g_olc_fit_on || !g_olc_fit.n || !coli_vk_dense_device_only()) return;+ coli_vk_dense_host_layers(g_olc_fit.n, g_olc_fit.L);+ coli_vk_dense_host_placed("olmoe", "the embedding (its rows are gathered on the CPU), norms");+ olm_dho_grow_cap(m, g_olm_dho_dropped);+}#endifstatic float *step(Model *m, const int *ids, int S, int pos_base) {