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

olmoe, mimo: a step the second device loses is redone from the rows as they came in

JustVugge9e7c4545e14
diff --git a/c/mimo_chain.h b/c/mimo_chain.h
index 60e16783..dda3afc3 100644
--- a/c/mimo_chain.h
+++ b/c/mimo_chain.h
@@ -81,6 +81,7 @@ typedef struct {
VkcBuf *x, *nrm, *tmp, *qkv, *kn, *vn, *wk, *wv, *ctx, *g, *u, *fin;
VkcBuf *h2d, *kvd, *outd, *routed, *cs;
float *host_out;
+ float *host_in; /* a chunk's rows before the primary's layers (the second device's chain) */
unsigned long long forwards, frames;
double wait_ms, host_ms;
} MimoChain;
@@ -591,13 +592,21 @@ static int mc_forward(Model *m, float *h, int S, int pos_base, float *logits, in
int ro = !full ? 0 : logits && all_rows ? n : logits && last ? 1 : 0;
float *hr = h + (size_t)c0 * H, *lc = all_rows ? logits + (size_t)c0 * V : logits;
at = ch;
+ /* the chunk's rows as they came in: the primary's residual replaces them, and if the
+ * second device is lost the CPU runs the chunk from these */
+ if (ch2) {
+ float *keep = (float *)realloc(ch2->host_in, (size_t)rows * H * sizeof(float));
+ if (!keep) goto lost;
+ ch2->host_in = keep;
+ memcpy(keep, hr, (size_t)n * H * sizeof(float));
+ }
if (!mc_chunk(ch, m, hr, n, pb, ch2 ? 0 : ro, lc)) goto lost;
if (ch2) {
at = ch2;
vkc_device(1);
int ok = mc_chunk(ch2, m, hr, n, pb, ro, lc);
vkc_device(0);
- if (!ok) goto lost;
+ if (!ok) { memcpy(hr, ch2->host_in, (size_t)n * H * sizeof(float)); goto lost; }
}
if (!full) { /* the CPU's layers and the head on the chunk's rows */
double t1 = now_s();
diff --git a/c/olmoe.c b/c/olmoe.c
index d60a74df..3f90862b 100644
--- a/c/olmoe.c
+++ b/c/olmoe.c
@@ -1762,6 +1762,9 @@ static float *step(Model *m, const int *ids, int S, int pos_base) {
if (!chain_n) {
free(chain_logit); chain_logit = NULL;
olc_cpu_step(m, pos_base);
+ /* the primary's layers may have run before the second device's were lost: their
+ * residual is in x, and the CPU redoes the whole step from the embedding */
+ for (int s = 0; s < S; s++) memcpy(x + (int64_t)s*D, m->embed + (int64_t)ids[s]*D, D*sizeof(float));
} else if (rows_only) { free(chain_logit); chain_logit = NULL; }
}
if (!chain_logit) layers_forward_range(m, x, S, pos_base, chain_n, c->n_layers, 1);