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

feat(vulkan): MiMo's partial dense chain, the first N layers on the device

JustVuggb3210a6960fc
diff --git a/c/mimo.c b/c/mimo.c
index a3f6d773..0c568617 100644
--- a/c/mimo.c
+++ b/c/mimo.c
@@ -1191,6 +1191,32 @@ static void embed_rows(Model *m, const int *ids, int n, float *h) {
* image-pad position of the WHOLE prompt (the first at `first_pad`). */
typedef struct { const float *rows; int n_rows; } ImageRows;
+/* Layers [l0, L) on the CPU for the nc rows hc at position pc (in place; xn and tmp hold
+ * nc rows each). trace: MIMO_TRACE's file, the residual after every block. */
+static void layers_cpu(Model *m, float *hc, int nc, int pc, int l0, float *xn, float *tmp, FILE *trace) {
+ Cfg *c = &m->c;
+ int H = c->hidden;
+ for (int li = l0; li < c->n_layers && nc > 0; li++) {
+ Layer *l = &m->L[li];
+ for (int t = 0; t < nc; t++) rmsnorm(xn + (size_t)t * H, hc + (size_t)t * H, l->ln1, H, c->eps);
+ attention(m, li, xn, nc, pc, tmp);
+ for (size_t i = 0; i < (size_t)nc * H; i++) hc[i] += tmp[i];
+ if (trace) fwrite(hc, sizeof(float), (size_t)nc * H, trace);
+ for (int t = 0; t < nc; t++) rmsnorm(xn + (size_t)t * H, hc + (size_t)t * H, l->ln2, H, c->eps);
+ if (c->moe[li]) moe(m, li, xn, nc, tmp);
+ else dense_mlp(m, l, xn, nc, tmp);
+ for (size_t i = 0; i < (size_t)nc * H; i++) hc[i] += tmp[i];
+ if (trace) fwrite(hc, sizeof(float), (size_t)nc * H, trace);
+ }
+}
+/* The final norm and lm_head on the CPU: every row of hc into lc (all_rows), or its last. */
+static void head_cpu(Model *m, const float *hc, int nc, float *lc, int all_rows, float *xn) {
+ Cfg *c = &m->c;
+ int H = c->hidden, from = all_rows ? 0 : nc - 1, rows = nc - from;
+ for (int t = from; t < nc; t++) rmsnorm(xn + (size_t)(t - from) * H, hc + (size_t)t * H, m->norm, H, c->eps);
+ dw_matmul(lc, xn, rows, &m->head);
+}
+
#ifdef COLI_VULKAN
#include "mimo_chain.h" /* COLI_VK_CHAIN: every layer's dense chain on the device */
#endif
@@ -1224,31 +1250,16 @@ static void forward(Model *m, const int *ids, int n, float *logits, int all_rows
int done = 0;
#ifdef COLI_VULKAN
if (g_vk_chain && !trace) {
- done = mc_forward(m, h, n, m->pos, logits, all_rows);
+ done = mc_forward(m, h, n, m->pos, logits, all_rows, xn, tmp);
if (done < n) mc_cpu_step(m, m->pos + done);
}
#endif
float *hc = h + (size_t)done * H, *lc = logits && all_rows ? logits + (size_t)done * c->vocab : logits;
int nc = n - done, pc = m->pos + done;
- for (int li = 0; li < c->n_layers && nc > 0; li++) {
- Layer *l = &m->L[li];
- for (int t = 0; t < nc; t++) rmsnorm(xn + (size_t)t * H, hc + (size_t)t * H, l->ln1, H, c->eps);
- attention(m, li, xn, nc, pc, tmp);
- for (size_t i = 0; i < (size_t)nc * H; i++) hc[i] += tmp[i];
- if (trace) fwrite(hc, sizeof(float), (size_t)nc * H, trace);
- for (int t = 0; t < nc; t++) rmsnorm(xn + (size_t)t * H, hc + (size_t)t * H, l->ln2, H, c->eps);
- if (c->moe[li]) moe(m, li, xn, nc, tmp);
- else dense_mlp(m, l, xn, nc, tmp);
- for (size_t i = 0; i < (size_t)nc * H; i++) hc[i] += tmp[i];
- if (trace) fwrite(hc, sizeof(float), (size_t)nc * H, trace);
- }
+ layers_cpu(m, hc, nc, pc, 0, xn, tmp, trace);
if (trace) { fclose(trace); trace = NULL; }
traced = 1;
- if (lc && nc > 0) {
- int from = all_rows ? 0 : nc - 1, rows = nc - from;
- for (int t = from; t < nc; t++) rmsnorm(xn + (size_t)(t - from) * H, hc + (size_t)t * H, m->norm, H, c->eps);
- dw_matmul(lc, xn, rows, &m->head);
- }
+ if (lc && nc > 0) head_cpu(m, hc, nc, lc, all_rows, xn);
kv_prefix_record(&m->kvp, ids, m->pos, n);
m->pos += n;
m->forwards++;
@@ -1342,8 +1353,10 @@ static Vision *g_vision;
* the experts take only what they leave; one that does not fit stays on the
* CPU. Returns how many went up. The dense chain alone (the per-matrix path off)
* takes the trunk and leaves the tower on the CPU (vision 0). */
+static void mc_place(Model *m, int *dropped); /* below: a fit's layers, layer by layer */
static int vk_dense_upload(Model *m, int vision) {
int n = 0;
+ if (g_mc_fit.L && !g_mc_placed) mc_place(m, NULL); /* the chain's fit: its layers, each whole or none */
for (int li = 0; li < m->c.n_layers; li++) {
Layer *l = &m->L[li];
n += dw_upload(&l->qkv) + dw_upload(&l->o);
@@ -1389,6 +1402,104 @@ static size_t vk_dense_bytes(const Model *m, int vision) {
return b;
}
+/* ---- a partial chain (vk_chain.h, vkc_fit): the first n layers on the device --------
+ * mc_fit_start decides n at start-up, before any upload and before the tier sizes its
+ * budget. A layer's bytes: its matrices in their dense form (MIMO_DENSE_BITS: the fused
+ * qkv and o_proj, the dense layer's MLP too), its K/V mirror at the size the chain
+ * allocates (a sliding layer's ring; a full layer's context, or COLI_VK_KV_DEVICE_ROWS
+ * when that asks for fewer: the split covers the rest) and its norms and sink logits.
+ * Fixed: the final norm and the scratch of one block of vkc_fit_rows(MIMO_CHUNK) rows
+ * (the residual's handoff reuses the normed rows' read-back). Tail: the head, and the
+ * vision tower when the per-matrix path takes it (COLI_VK_DENSE). The CPU's layers, the
+ * head and the tower of a partial chain stay on the CPU with their host copies (vk_off:
+ * the per-matrix path leaves them there). */
+static void mc_fit_start(Model *m) {
+ Cfg *c = &m->c; int L = c->n_layers, H = c->hidden;
+ for (int k = 0; k < 2; k++) if (c->head_dim[k] > 256 || c->v_dim[k] > 256) return; /* the chain declines */
+ size_t *per = calloc((size_t)L, sizeof *per), *mat = calloc((size_t)L, sizeof *mat);
+ int ok = per && mat;
+ long forced = vkc_kv_env("COLI_VK_KV_DEVICE_ROWS", 0);
+#define MC_FITW(d) do { const DW *d_ = (d); int g_; const float *s_; int f_ = d_->w ? dw_vk_fmt(d_, &g_, &s_) : -1; \
+ if (f_ < 0) ok = 0; else { per[i] += vkc_fit_tensor(f_, d_->I, d_->O, g_); mat[i] += coli_vk_tensor_payload(f_, d_->I, d_->O, g_); } } while (0)
+ for (int i = 0; i < L && ok; i++) {
+ const Layer *l = &m->L[i];
+ MC_FITW(&l->qkv); MC_FITW(&l->o);
+ if (!c->moe[i]) { MC_FITW(&l->gate); MC_FITW(&l->up); MC_FITW(&l->down); }
+ McGeo g = mc_geo(m, i);
+ size_t rows = !g.swa && forced > 0 && forced < g.rows ? (size_t)forced : (size_t)g.rows;
+ per[i] += vkc_fit_buf(rows * g.kd * sizeof(float)) + vkc_fit_buf(rows * g.vdd * sizeof(float)) +
+ ((size_t)2 * H + (l->sink ? (size_t)c->heads[c->swa[i]] : 0)) * sizeof(float);
+ }
+#undef MC_FITW
+ int hg; const float *hs;
+ int hf = m->head.w ? dw_vk_fmt(&m->head, &hg, &hs) : -1;
+ if (!ok || hf < 0) { free(per); free(mat); return; } /* a matrix the device does not take: the chain declines as before */
+ size_t tail = vkc_fit_tensor(hf, m->head.I, m->head.O, hg);
+ if (g_vision && coli_vk_dense()) { /* the tower the per-matrix path puts on the device */
+ Vision *v = g_vision;
+ const DW *t[3] = {&v->embed, &v->fc1, &v->fc2};
+ for (int k = 0; k < 3; k++) if (t[k]->w && dw_vk_fmt(t[k], &hg, &hs) >= 0) tail += vkc_fit_tensor(dw_vk_fmt(t[k], &hg, &hs), t[k]->I, t[k]->O, hg);
+ for (int b = 0; b < v->depth; b++) {
+ const DW *d[5] = {&v->b[b].qkv, &v->b[b].proj, &v->b[b].gate, &v->b[b].up, &v->b[b].down};
+ for (int k = 0; k < 5; k++) if (d[k]->w && dw_vk_fmt(d[k], &hg, &hs) >= 0) tail += vkc_fit_tensor(dw_vk_fmt(d[k], &hg, &hs), d[k]->I, d[k]->O, hg);
+ }
+ }
+ MimoChain g; memset(&g, 0, sizeof g);
+ g.o_kvd = calloc((size_t)L, sizeof(size_t));
+ const char *mc = getenv("MIMO_CHUNK");
+ int R = vkc_fit_rows(mc && *mc && atoi(mc) > 0 ? atoi(mc) : 64);
+ g_mc_count = 0; g_mc_fitcount = 1;
+ mc_scratch(&g, m, R, 1);
+ size_t fixed = (size_t)g_mc_count + vkc_fit_buf((size_t)H * sizeof(float));
+ g_mc_count = -1; g_mc_fitcount = 0;
+ free(g.o_kvd);
+ vkc_fit("mimo", L, per, mat, fixed, tail, &g_mc_fit);
+ free(per); free(mat);
+}
+/* Layers from..L-1, the head and the tower refused to the device: what of them is there
+ * is freed, and the per-matrix path leaves them on the CPU. */
+static void mc_refuse(DW *d) {
+ if (d->vk && !d->vk_gone) { coli_vk_tensor_free((ColiVkTensor *)d->vk); d->vk = NULL; }
+ if (!d->vk_gone) d->vk_off = 1;
+}
+static void mc_refuse_from(Model *m, int from) {
+ if (vkc_ready()) vkc_finish();
+ for (int li = from; li < m->c.n_layers; li++) {
+ Layer *l = &m->L[li];
+ mc_refuse(&l->qkv); mc_refuse(&l->o); mc_refuse(&l->gate); mc_refuse(&l->up); mc_refuse(&l->down);
+ }
+ mc_refuse(&m->head);
+ if (g_vision) {
+ Vision *v = g_vision;
+ mc_refuse(&v->embed); mc_refuse(&v->fc1); mc_refuse(&v->fc2);
+ for (int b = 0; b < v->depth; b++) {
+ mc_refuse(&v->b[b].qkv); mc_refuse(&v->b[b].proj); mc_refuse(&v->b[b].gate); mc_refuse(&v->b[b].up); mc_refuse(&v->b[b].down);
+ }
+ }
+}
+static void dw_drop(DW *d, int *n); /* below */
+/* The fit's layers up, layer by layer: each whole or not at all (a layer that fails is
+ * freed, it and the rest refused, vkc_fit_shrink); dropped: with COLI_VK_DENSE_HOST, each
+ * layer's host copies given back once all of it is on the device (counted there). */
+static void mc_place(Model *m, int *dropped) {
+ for (int i = 0; i < g_mc_fit.n; i++) {
+ Layer *l = &m->L[i];
+ DW *d[5] = {&l->qkv, &l->o, &l->gate, &l->up, &l->down};
+ int nd = m->c.moe[i] ? 2 : 5, ok = 1;
+ for (int k = 0; k < nd && ok; k++) ok = dw_upload(d[k]);
+ if (!ok) {
+ mc_refuse_from(m, i);
+ vkc_fit_shrink("mimo", &g_mc_fit, i, "a dense matrix did not go up");
+ break;
+ }
+ if (dropped) for (int k = 0; k < nd; k++) dw_drop(d[k], dropped);
+ vkc_fit_mark(&g_mc_fit, i);
+ }
+ if (vkc_fit_partial(&g_mc_fit)) mc_refuse_from(m, g_mc_fit.n); /* the CPU's layers, the head, the tower */
+ vkc_fit_placed("mimo", &g_mc_fit);
+ g_mc_placed = 1;
+}
+
/* ---- the dense matrices on the device only (COLI_VK_DENSE_HOST) ----------------
* With the trunk on the device (the chain, or COLI_VK_DENSE), every trunk matrix goes
* up now, before the expert tier sizes its budget, and its host copy is dropped; the
@@ -1409,17 +1520,35 @@ static void dw_drop(DW *d, int *n) {
static void mimo_dho_start(Model *m) {
if (!g_vk_ready) return;
int vision = coli_vk_dense();
- if (!coli_vk_dense_host_decide("mimo", coli_vk_dense() || g_vk_chain, vk_dense_bytes(m, vision))) return;
+ /* a partial chain's fit: the first n layers' bytes only (the head and the tower with
+ * the tail); n = 0 leaves the dense part on the CPU */
+ size_t bytes = vk_dense_bytes(m, vision);
+ int fit = g_mc_fit.L > 0, tail = !fit || (g_mc_fit.n == g_mc_fit.L && g_mc_fit.tail);
+ if (fit) {
+ bytes = 0;
+ for (int li = 0; li < g_mc_fit.n; li++) {
+ const Layer *l = &m->L[li];
+ bytes += dw_bytes(&l->qkv) + dw_bytes(&l->o);
+ if (!m->c.moe[li]) bytes += dw_bytes(&l->gate) + dw_bytes(&l->up) + dw_bytes(&l->down);
+ }
+ if (tail) bytes += vk_dense_bytes(m, vision) - vk_dense_bytes(m, 0) + dw_bytes(&m->head);
+ }
+ if (!coli_vk_dense_host_decide("mimo", (coli_vk_dense() || g_vk_chain) && !(fit && !g_mc_fit.n), bytes)) return;
g_mimo_dho = 1;
g_dw_dho_S = &m->S; g_dw_dho_c = &m->c;
int n = 0;
- for (int li = 0; li < m->c.n_layers; li++) {
- Layer *l = &m->L[li];
- dw_drop(&l->qkv, &n); dw_drop(&l->o, &n);
- if (!m->c.moe[li]) { dw_drop(&l->gate, &n); dw_drop(&l->up, &n); dw_drop(&l->down, &n); }
- }
- dw_drop(&m->head, &n);
- if (g_vision) {
+ if (fit) {
+ coli_vk_dense_host_layers(g_mc_fit.n, g_mc_fit.L);
+ mc_place(m, &n); /* each layer whole, then its host copies */
+ tail = g_mc_fit.n == g_mc_fit.L && g_mc_fit.tail;
+ } else
+ for (int li = 0; li < m->c.n_layers; li++) {
+ Layer *l = &m->L[li];
+ dw_drop(&l->qkv, &n); dw_drop(&l->o, &n);
+ if (!m->c.moe[li]) { dw_drop(&l->gate, &n); dw_drop(&l->up, &n); dw_drop(&l->down, &n); }
+ }
+ if (tail) dw_drop(&m->head, &n);
+ if (g_vision && tail) {
Vision *v = g_vision;
DW *t[3] = {&v->embed, &v->fc1, &v->fc2};
for (int k = 0; k < 3; k++) { if (vision) dw_drop(t[k], &n); else t[k]->vk_off = 1; }
@@ -1465,7 +1594,8 @@ static void vk_tier_start(Model *m) {
.gate_up = f, .down = f, .act = VKT_ACT_SWIGLU,
.max_rows = MIMO_VK_ROWS * c->topk,
.ram_reserve = (size_t)(m->e_bytes + 8192) * (size_t)cap * (size_t)nmoe,
- .dense_bytes = (coli_vk_dense_device_only() ? 0 : /* placed already (mimo_dho_start) */
+ .dense_bytes = g_mc_fit.L && vkc_fit_partial(&g_mc_fit) ? mc_kv_bytes_n(m, g_mc_fit.n) : /* a partial chain: placed already, its caches to come */
+ (coli_vk_dense_device_only() ? 0 : /* placed already (mimo_dho_start) */
coli_vk_dense() ? vk_dense_bytes(m, 1) : g_vk_chain ? vk_dense_bytes(m, 0) : 0) +
(g_vk_chain ? mc_kv_bytes(m) : 0), /* the chain's KV caches */
.in_ram = vk_in_ram, .ram_ctx = m,
@@ -1984,9 +2114,19 @@ int main(int argc, char **argv) {
* MiMo checkpoint: an integrated GPU keeps it opt-in (docs/vulkan.md). */
if (g_vk_ready) {
g_vk_chain = coli_vk_chain_decide("mimo", tier, COLI_VK_CHAIN_UNMEASURED);
+ /* the chain's fit before any upload: its first n layers on the device, the
+ * others, the head and the tower on the CPU when they do not all fit */
+ if (g_vk_chain) mc_fit_start(m);
+ if (g_mc_fit.L && !g_mc_fit.n) { /* its line said the chain stays off: nothing goes up */
+ g_vk_chain = 0;
+ mc_refuse_from(m, 0);
+ vkc_fit_placed("mimo", &g_mc_fit);
+ g_mc_placed = 1;
+ }
if (g_vk_chain && !vkc_init()) g_vk_chain = 0;
}
mimo_dho_start(m); /* COLI_VK_DENSE_HOST: the dense matrices on the device only, before the tier sizes its budget */
+ if (g_vk_chain && vkc_fit_partial(&g_mc_fit) && !g_mc_placed) mc_place(m, NULL); /* before the tier, too */
if (g_vk_ready && tier) vk_tier_start(m);
if (g_vk_ready && !vkt_ready() && !coli_vk_dense()) coli_vk_dense_decide("mimo", 0, 1); /* no tier after all */
if (g_vk_chain) { /* the chain's teardown before the device's (the tier registered the device's) */
@@ -1994,7 +2134,7 @@ int main(int argc, char **argv) {
atexit(vkc_shutdown);
}
}
- if (g_vk_ready && (coli_vk_dense() || g_vk_chain)) { /* COLI_VK_DENSE=0: the trunk stays on the CPU */
+ if (g_vk_ready && (coli_vk_dense() || g_vk_chain) && !(g_mc_fit.L && !g_mc_fit.n)) { /* COLI_VK_DENSE=0: the trunk stays on the CPU */
int up = vk_dense_upload(m, coli_vk_dense());
size_t used = 0, count = 0;
coli_vk_mem_info(&used, &count);
diff --git a/c/mimo_chain.h b/c/mimo_chain.h
index 7c1ef687..d69b3c30 100644
--- a/c/mimo_chain.h
+++ b/c/mimo_chain.h
@@ -60,6 +60,7 @@
typedef struct {
int ok, failed;
+ int nl, full; /* layers on the device (the first nl); the head there too */
int rows; /* scratch capacity in rows */
int rd_off[2]; /* the RoPE table of each layer kind in cs */
VkcBuf *prm; /* the norms and the sink logits */
@@ -77,6 +78,11 @@ typedef struct {
} MimoChain;
static int g_vk_chain = 0; /* COLI_VK_CHAIN decided on, and the chain's pipelines are up */
+/* The partial chain's fit (mc_fit_start, mimo.c): the first n of L layers on the device;
+ * L = 0 while there is none (the chain off, or one that declines as before). */
+static VkcFit g_mc_fit;
+static int g_mc_placed; /* vkc_fit_placed said it (mc_place, mimo.c) */
+static int mc_layers(const Model *m) { return g_mc_fit.L ? g_mc_fit.n : m->c.n_layers; }
/* the geometry of layer li's attention */
@@ -96,45 +102,58 @@ static int mc_ctensor(DW *d) { return dw_upload(d) && d->vk; }
/* the device's KV caches, in bytes (the tier's budget leaves them room); a full layer
* holds COLI_VK_KV_DEVICE_ROWS rows when that asks for fewer than the context */
-static size_t mc_kv_bytes(const Model *m) {
+static size_t mc_kv_bytes_n(const Model *m, int nl) {
size_t b = 0;
long forced = vkc_kv_env("COLI_VK_KV_DEVICE_ROWS", 0);
- for (int i = 0; i < m->c.n_layers; i++) {
+ for (int i = 0; i < nl; i++) {
McGeo g = mc_geo(m, i);
size_t rows = !g.swa && forced > 0 && forced < g.rows ? (size_t)forced : (size_t)g.rows;
b += rows * (g.kd + g.vdd) * sizeof(float);
}
return b;
}
+static size_t mc_kv_bytes(const Model *m) { return mc_kv_bytes_n(m, m->c.n_layers); }
/* The model's parameters on the device, its tensors resolved, its caches allocated;
- * NULL = the chain cannot run. */
+ * NULL = the chain cannot run. With a fit, the first n layers only (their matrices placed
+ * at start-up, mc_place), and the head when the tail went up too. */
static MimoChain *mc_setup(Model *m) {
MimoChain *ch = (MimoChain *)m->vkchain;
if (ch) return ch->ok ? ch : NULL;
ch = (MimoChain *)calloc(1, sizeof *ch);
if (!ch) return NULL;
m->vkchain = ch;
- Cfg *c = &m->c; int L = c->n_layers, H = c->hidden;
+ Cfg *c = &m->c; int L = c->n_layers, H = c->hidden, nl = mc_layers(m);
for (int k = 0; k < 2; k++)
if (c->head_dim[k] > 256 || c->v_dim[k] > 256) {
fprintf(stderr, "[VK] mimo chain: head dims %d/%d are past its attention shader's 256; per-matrix path\n",
c->head_dim[k], c->v_dim[k]);
return NULL;
}
+ if (nl < 1) return NULL;
ch->o_ln1 = calloc(L, sizeof(size_t)); ch->o_ln2 = calloc(L, sizeof(size_t)); ch->o_sink = calloc(L, sizeof(size_t));
ch->o_kvd = calloc(L, sizeof(size_t)); ch->kv_valid = calloc(L, sizeof(int)); ch->kvi = calloc(L, sizeof(int));
ch->kc = calloc(L, sizeof(void *)); ch->vc = calloc(L, sizeof(void *));
if (!ch->o_ln1 || !ch->o_ln2 || !ch->o_sink || !ch->o_kvd || !ch->kv_valid || !ch->kvi || !ch->kc || !ch->vc) return NULL;
+ /* the matrices: the device copies the per-matrix path uploads (vk_dense_upload) */
+ int ok = 1;
+ for (int i = 0; i < nl && ok; i++) {
+ Layer *l = &m->L[i];
+ ok = mc_ctensor(&l->qkv) && mc_ctensor(&l->o) &&
+ (c->moe[i] || (mc_ctensor(&l->gate) && mc_ctensor(&l->up) && mc_ctensor(&l->down)));
+ }
+ int full = nl == L && (!g_mc_fit.L || g_mc_fit.tail);
+ ok = ok && (!full || mc_ctensor(&m->head));
+ if (!ok) { fprintf(stderr, "[VK] mimo chain: a dense matrix did not reach the device; per-matrix path\n"); return NULL; }
size_t n = 0;
- for (int i = 0; i < L; i++) {
+ for (int i = 0; i < nl; i++) {
ch->o_ln1[i] = n; n += H; ch->o_ln2[i] = n; n += H;
if (m->L[i].sink) { ch->o_sink[i] = n; n += c->heads[c->swa[i]]; }
}
ch->o_final = n; n += H;
float *arena = calloc(n, sizeof(float));
if (!arena) return NULL;
- for (int i = 0; i < L; i++) {
+ for (int i = 0; i < nl; i++) {
Layer *l = &m->L[i];
memcpy(arena + ch->o_ln1[i], l->ln1, H * sizeof(float));
memcpy(arena + ch->o_ln2[i], l->ln2, H * sizeof(float));
@@ -142,25 +161,17 @@ static MimoChain *mc_setup(Model *m) {
}
memcpy(arena + ch->o_final, m->norm, H * sizeof(float));
ch->prm = vkc_buf(n * sizeof(float), VKC_DEV);
- int ok = ch->prm && vkc_begin() && vkc_write(ch->prm, 0, arena, n * sizeof(float)) && vkc_submit(1);
+ ok = ch->prm && vkc_begin() && vkc_write(ch->prm, 0, arena, n * sizeof(float)) && vkc_submit(1);
free(arena);
if (!ok) return NULL;
- /* the matrices: the device copies the per-matrix path uploads (vk_dense_upload) */
- for (int i = 0; i < L && ok; i++) {
- Layer *l = &m->L[i];
- ok = mc_ctensor(&l->qkv) && mc_ctensor(&l->o) &&
- (c->moe[i] || (mc_ctensor(&l->gate) && mc_ctensor(&l->up) && mc_ctensor(&l->down)));
- }
- ok = ok && mc_ctensor(&m->head);
- if (!ok) { fprintf(stderr, "[VK] mimo chain: a dense matrix did not reach the device; per-matrix path\n"); return NULL; }
/* the caches, in the host's layout and at its size; the full layers' split past the
* device's budget (a window of blocks a layer, the rest in the host's cache) */
int nfull = 0, frows = 0;
size_t frow = 0;
for (int i = 0; i < L; i++) {
McGeo g = mc_geo(m, i);
- ch->kvi[i] = g.swa ? -1 : nfull++;
- if (!g.swa) { frows = g.rows; frow = (size_t)(g.kd + g.vdd) * sizeof(float); }
+ ch->kvi[i] = g.swa || i >= nl ? -1 : nfull++;
+ if (!g.swa && i < nl) { frows = g.rows; frow = (size_t)(g.kd + g.vdd) * sizeof(float); }
}
/* KV placement is row-anchored; its legacy chunk argument is not used. The
* actual prompt chunk is sized after setup and clamped to ks.chunk. */
@@ -169,7 +180,7 @@ static MimoChain *mc_setup(Model *m) {
return NULL;
}
size_t kv = 0;
- for (int i = 0; i < L; i++) {
+ for (int i = 0; i < nl; i++) {
McGeo g = mc_geo(m, i);
if (!g.swa && ch->ks.on) g.rows = ch->ks.rows;
ch->kc[i] = vkc_buf((size_t)g.rows * g.kd * sizeof(float), VKC_DEV);
@@ -180,18 +191,25 @@ static MimoChain *mc_setup(Model *m) {
}
kv += (size_t)g.rows * (g.kd + g.vdd);
}
+ ch->nl = nl; ch->full = full;
ch->ok = 1;
int swa = 0;
- for (int i = 0; i < L; i++) swa += c->swa[i];
+ for (int i = 0; i < nl; i++) swa += c->swa[i];
fprintf(stderr, "[VK] mimo chain: %d layers on the device (%d sliding window), %.1f MiB of KV mirrors\n",
- L, swa, kv * 4 / 1048576.0);
+ nl, swa, kv * 4 / 1048576.0);
return ch;
}
-/* mc_res counts instead of reserving while g_mc_count >= 0 (the chunk's sizing) */
+/* mc_res counts instead of reserving while g_mc_count >= 0 (the chunk's sizing; the fit's,
+ * g_mc_fitcount, each buffer as the pools round it) */
static long long g_mc_count = -1;
+static int g_mc_fitcount;
static int mc_res(VkcBuf **b, size_t floats, int kind) {
- if (g_mc_count >= 0) { g_mc_count += (long long)(floats ? floats : 1) * (long long)sizeof(float); return 1; }
+ if (g_mc_count >= 0) {
+ size_t by = (floats ? floats : 1) * sizeof(float);
+ g_mc_count += (long long)(g_mc_fitcount ? vkc_fit_buf(by) : by);
+ return 1;
+ }
return vkc_reserve(b, (floats ? floats : 1) * sizeof(float), kind);
}
static int mc_scratch(MimoChain *ch, Model *m, int rows, int out);
@@ -259,7 +277,7 @@ static int mc_scratch(MimoChain *ch, Model *m, int rows, int out) {
static void mc_cpu_step(Model *m, int pos_base) {
MimoChain *ch = (MimoChain *)m->vkchain;
if (!ch || !ch->ok) return;
- for (int i = 0; i < m->c.n_layers; i++) {
+ for (int i = 0; i < ch->nl; i++) { /* the device's layers: the CPU's keep their caches on the host */
if (ch->kv_valid[i] > pos_base) ch->kv_valid[i] = pos_base;
if (ch->kvi[i] >= 0) vkc_kv_lower(&ch->ks, ch->kvi[i], pos_base);
}
@@ -268,7 +286,7 @@ static void mc_cpu_step(Model *m, int pos_base) {
static void mc_rings_rewritten(Model *m) {
MimoChain *ch = (MimoChain *)m->vkchain;
if (!ch || !ch->ok) return;
- for (int i = 0; i < m->c.n_layers; i++) if (m->c.swa[i]) ch->kv_valid[i] = 0;
+ for (int i = 0; i < ch->nl; i++) if (m->c.swa[i]) ch->kv_valid[i] = 0;
}
/* host rows [t0, t1) of a ring into the device's ring (at most two runs of slots) */
static int mc_ring_up(VkcBuf *dst, const float *src, int R, int t0, int t1, int w) {
@@ -296,7 +314,7 @@ static int mc_ring_copy(VkcBuf *ring, VkcBuf *lin, int R, int base, int t0, int
* pos_base reads them. */
static int mc_push_state(MimoChain *ch, Model *m, int pos_base, int n_rows) {
Cfg *c = &m->c; int ok = 1;
- for (int i = 0; i < c->n_layers && ok; i++) {
+ for (int i = 0; i < ch->nl && ok; i++) {
McGeo g = mc_geo(m, i);
Layer *l = &m->L[i];
if (ch->kv_valid[i] > pos_base) ch->kv_valid[i] = pos_base;
@@ -416,7 +434,7 @@ static int mc_dense_mlp(MimoChain *ch, Model *m, int i, int n) {
/* The rows the step at pb wrote, into the host's caches (the CPU's own placement). */
static void mc_commit(MimoChain *ch, Model *m, int n, int pb) {
const float *kv = (const float *)vkc_ptr(ch->kvd);
- for (int i = 0; i < m->c.n_layers; i++) {
+ for (int i = 0; i < ch->nl; i++) {
McGeo g = mc_geo(m, i);
Layer *l = &m->L[i];
int from = g.swa && n > g.rows ? n - g.rows : 0, cap = g.swa && g.rows < ch->rows ? g.rows : ch->rows;
@@ -436,13 +454,17 @@ static void mc_commit(MimoChain *ch, Model *m, int n, int pb) {
* logits as forward() wants them (NULL: none; all_rows: [S][V], else the last row's).
* Returns how many rows it computed: S, or fewer when the device was lost or the chain
* declined -- the host's caches then hold every position before pos_base + that many,
- * and the CPU runs the rest. */
-static int mc_forward(Model *m, const float *h, int S, int pos_base, float *logits, int all_rows) {
+ * and the CPU runs the rest. A partial chain (ch->full 0) runs its first ch->nl layers
+ * here for each chunk, brings the chunk's residual rows down with the frame that ends it,
+ * and runs the CPU's layers (layers_cpu, from nl) and the head (head_cpu) on them before
+ * the next chunk, in xn and tmp (forward()'s rows): a chunk counts as computed once both
+ * sides ran it, so a device lost in a later chunk loses nothing either side holds. */
+static int mc_forward(Model *m, float *h, int S, int pos_base, float *logits, int all_rows, float *xn, float *tmp) {
if (!g_vk_chain) return 0;
if (g_vk_chain == COLI_VK_CHAIN_PREFILL && S <= 2) return 0; /* prompts only: decode on the CPU */
MimoChain *ch = mc_setup(m);
if (!ch || ch->failed) return 0;
- Cfg *c = &m->c; int H = c->hidden, L = c->n_layers, V = c->vocab;
+ Cfg *c = &m->c; int H = c->hidden, L = ch->nl, V = c->vocab, full = ch->full;
int CH = mc_chunk_rows(ch, m), rows = S < CH ? S : CH;
if (ch->ks.on && rows > ch->ks.chunk) rows = ch->ks.chunk; /* a step's rows fit the split's window */
int out = logits && all_rows ? rows : 1;
@@ -494,15 +516,25 @@ static int mc_forward(Model *m, const float *h, int S, int pos_base, float *logi
}
}
if (ok && pending) ok = mc_add(ch->x, ch->routed, n * H);
- int ro = logits && all_rows ? n : logits && last ? 1 : 0;
+ int ro = !full ? 0 : logits && all_rows ? n : logits && last ? 1 : 0;
if (ok && ro) ok = mc_norm(ch->x, (size_t)(n - ro) * H, ch->prm, ch->o_final, ch->fin, 0, ro, H, c->eps) &&
vkc_matmul((ColiVkTensor *)m->head.vk, ch->fin, 0, ch->outd, 0, ro);
+ if (ok && !full) ok = vkc_copy(ch->h2d, 0, ch->x, 0, (size_t)n * H); /* the handoff to the CPU's layers */
double t0 = now_s();
ok = ok && vkc_submit(1);
ch->frames++; ch->wait_ms += (now_s() - t0) * 1e3;
if (!ok) goto lost;
mc_commit(ch, m, n, pb);
if (ro) memcpy(all_rows ? logits + (size_t)c0 * V : logits, vkc_ptr(ch->outd), (size_t)ro * V * sizeof(float));
+ if (!full) { /* the CPU's layers and the head on the chunk's rows */
+ float *hr = h + (size_t)c0 * H;
+ memcpy(hr, vkc_ptr(ch->h2d), (size_t)n * H * sizeof(float));
+ double t1 = now_s();
+ layers_cpu(m, hr, n, pb, L, xn, tmp, NULL);
+ float *lc = logits && all_rows ? logits + (size_t)c0 * V : logits && last ? logits : NULL;
+ if (lc) head_cpu(m, hr, n, lc, all_rows, xn);
+ ch->host_ms += (now_s() - t1) * 1e3;
+ }
done = c0 + n;
}
ch->forwards++;