Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
test(serve): freeze OLMoE wire framing
Mario Gutierrez63b803677f9e
diff --git a/c/Makefile b/c/Makefileindex d14389cf..5d0660c0 100644--- a/c/Makefile+++ b/c/Makefile@@ -998,6 +998,9 @@ tests/test_decode_batch$(EXE): tests/test_decode_batch.c decode_batch.htests/test_olmoe_matmul_q$(EXE): tests/test_olmoe_matmul_q.c olmoe.c st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h route_trace.h$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)+tests/test_olmoe_serve_framing$(EXE): tests/test_olmoe_serve_framing.c olmoe.c st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h route_trace.h+ $(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)+tests/test_idot$(EXE): tests/test_idot.c colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h sample.h kv_persist.h telemetry.h route_trace.h$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)diff --git a/c/olmoe.c b/c/olmoe.cindex 0f59ea0c..dadd1fb0 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -1170,10 +1170,10 @@ typedef struct { char id[64]; int max_tok; float temp, top_p; char *payload; intstatic SReq g_q[SRV_QMAX]; static int g_qn = 0;/* read one control line (+ payload for SUBMIT). cur_id: request in flight;- * returns 1 if that request was cancelled, 0 otherwise, -1 on stdin EOF. */-static int serve_read_cmd(const char *cur_id) {+ * returns 1 if that request was cancelled, 0 otherwise, -1 on input EOF. */+static int serve_read_cmd(FILE *in, FILE *out, const char *cur_id) {char ln[512];- if (!fgets(ln, sizeof(ln), stdin)) return -1;+ if (!fgets(ln, sizeof(ln), in)) return -1;char cmd[16], id[64];if (sscanf(ln, "%15s %63s", cmd, id) < 2) return 0;if (!strcmp(cmd, "CANCEL")) return cur_id && !strcmp(id, cur_id);@@ -1181,18 +1181,18 @@ static int serve_read_cmd(const char *cur_id) {int slot, plen, max_tok; float temp, top_p;int nf = sscanf(ln, "%*s %*s %d %d %d %f %f", &slot, &plen, &max_tok, &temp, &top_p);if (nf < 5 || plen < 0 || plen > (1<<22) || max_tok < 1 || max_tok > (1<<20)) {- printf("ERROR %s bad submit header\n", id); fflush(stdout); return 0; }+ fprintf(out, "ERROR %s bad submit header\n", id); fflush(out); return 0; }(void)slot;char *pl = malloc((size_t)plen + 1);- if (fread(pl, 1, (size_t)plen, stdin) != (size_t)plen) { free(pl); return -1; }+ if (fread(pl, 1, (size_t)plen, in) != (size_t)plen) { free(pl); return -1; }pl[plen] = 0;- int nl = fgetc(stdin); (void)nl;+ int nl = fgetc(in); (void)nl;if (g_qn < SRV_QMAX) {SReq *q = &g_q[g_qn++];snprintf(q->id, sizeof(q->id), "%s", id);q->max_tok = max_tok; q->temp = temp; q->top_p = top_p;q->payload = pl; q->plen = plen;- } else { printf("ERROR %s queue full\n", id); fflush(stdout); free(pl); }+ } else { fprintf(out, "ERROR %s queue full\n", id); fflush(out); free(pl); }}return 0;}@@ -1223,7 +1223,7 @@ static void serve_one(Model *m, Tok *T, SReq *q, int ctx_cap) {fputc('\n', stdout); fflush(stdout);gen++; hist_len++;while (coli_stdin_readable()) {- int r = serve_read_cmd(q->id);+ int r = serve_read_cmd(stdin, stdout, q->id);if (r < 0) { free(ids); return; }if (r > 0) { cancelled = 1; limited = 0; }}@@ -1313,7 +1313,7 @@ static void serve_loop(Model *m, Tok *T, int ctx_cap) {serve_hwinfo(m);serve_tiers_emap(m);for (;;) {- while (!g_qn) if (serve_read_cmd(NULL) < 0) return;+ while (!g_qn) if (serve_read_cmd(stdin, stdout, NULL) < 0) return;SReq q = g_q[0];memmove(g_q, g_q + 1, (size_t)(--g_qn) * sizeof(SReq));serve_one(m, T, &q, ctx_cap);diff --git a/c/tests/test_olmoe_serve_framing.c b/c/tests/test_olmoe_serve_framing.cnew file mode 100644index 00000000..bcf2a4f6--- /dev/null+++ b/c/tests/test_olmoe_serve_framing.c@@ -0,0 +1,111 @@+/* Freeze OLMoE's existing request framing before moving it behind a shared+ * codec. This includes the production parser and queue, but no model weights. */+#define OLMOE_TESTING 1+#define main coli_olmoe_main_unused+#include "../olmoe.c"+#undef main++#include <assert.h>++static FILE *bytes_input(const void *data, size_t size)+{+ FILE *stream = tmpfile();+ assert(stream);+ assert(fwrite(data, 1, size, stream) == size);+ rewind(stream);+ return stream;+}++static size_t read_output(FILE *stream, char *output, size_t capacity)+{+ assert(fflush(stream) == 0);+ rewind(stream);+ return fread(output, 1, capacity, stream);+}++static void reset_queue(void)+{+ for (int i = 0; i < g_qn; i++) free(g_q[i].payload);+ memset(g_q, 0, sizeof(g_q));+ g_qn = 0;+}++static void test_submit_moves_exact_payload_into_queue(void)+{+ static const char frame[] =+ "SUBMIT req-7 0 5 4 0.7 0.95\nab\ncd\n";+ FILE *in = bytes_input(frame, sizeof(frame) - 1);+ FILE *out = tmpfile();+ assert(out);++ assert(serve_read_cmd(in, out, NULL) == 0);+ assert(g_qn == 1);+ assert(strcmp(g_q[0].id, "req-7") == 0);+ assert(g_q[0].plen == 5);+ assert(memcmp(g_q[0].payload, "ab\ncd", 5) == 0);+ assert(g_q[0].max_tok == 4);+ assert(g_q[0].temp > .69f && g_q[0].top_p > .94f);+ assert(fgetc(in) == EOF);++ char output[64];+ assert(read_output(out, output, sizeof(output)) == 0);+ fclose(in);+ fclose(out);+ reset_queue();+}++static void test_cancel_only_matches_active_request(void)+{+ static const char cancel[] = "CANCEL req-7\n";+ FILE *out = tmpfile();+ assert(out);++ FILE *match = bytes_input(cancel, sizeof(cancel) - 1);+ assert(serve_read_cmd(match, out, "req-7") == 1);+ fclose(match);++ FILE *other = bytes_input(cancel, sizeof(cancel) - 1);+ assert(serve_read_cmd(other, out, "req-8") == 0);+ fclose(other);+ fclose(out);+}++static void test_errors_are_byte_exact_and_frames_are_consumed(void)+{+ static const char bad[] = "SUBMIT bad 0 -1 4 0.7 0.95\n";+ FILE *bad_in = bytes_input(bad, sizeof(bad) - 1);+ FILE *bad_out = tmpfile();+ assert(bad_out);+ assert(serve_read_cmd(bad_in, bad_out, NULL) == 0);+ char output[128];+ size_t count = read_output(bad_out, output, sizeof(output));+ static const char bad_want[] = "ERROR bad bad submit header\n";+ assert(count == sizeof(bad_want) - 1);+ assert(memcmp(output, bad_want, count) == 0);+ fclose(bad_in);+ fclose(bad_out);++ g_qn = SRV_QMAX;+ static const char full[] = "SUBMIT full 0 1 4 0.7 0.95\nx\n";+ FILE *full_in = bytes_input(full, sizeof(full) - 1);+ FILE *full_out = tmpfile();+ assert(full_out);+ assert(serve_read_cmd(full_in, full_out, NULL) == 0);+ count = read_output(full_out, output, sizeof(output));+ static const char full_want[] = "ERROR full queue full\n";+ assert(count == sizeof(full_want) - 1);+ assert(memcmp(output, full_want, count) == 0);+ assert(fgetc(full_in) == EOF);+ fclose(full_in);+ fclose(full_out);+ g_qn = 0;+}++int main(void)+{+ test_submit_moves_exact_payload_into_queue();+ test_cancel_only_matches_active_request();+ test_errors_are_byte_exact_and_frames_are_consumed();+ puts("olmoe serve framing baseline: ok");+ return 0;+}diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.pyindex f3d174fe..09b807ff 100644--- a/c/tests/test_openai_server.py+++ b/c/tests/test_openai_server.py@@ -457,6 +457,30 @@ class FakeProcess:class DispatcherTest(unittest.TestCase):+ def test_olmoe_request_and_response_transcript_is_byte_exact(self):+ expected = b"SUBMIT 1 0 5 3 0.25 0.9\nH\xc3\xa9\nx\n"++ def respond(process, frame):+ self.assertEqual(frame, expected)+ process.stdout.feed(+ b"DATA 1 4\nA\n\xc3\xa9\n"+ b"DONE 1 STAT 1 2.5 50.0 1.25 5 0\n"+ )++ process = FakeProcess(respond)+ with patch("openai_server.ARCH", "olmoe"), \+ patch("openai_server.subprocess.Popen", return_value=process):+ engine = Engine("olmoe", "model")+ chunks = []+ stats = engine.generate("Hé\nx", 3, 0.25, 0.9, chunks.append)+ engine.close()++ self.assertEqual(process.writes, [expected])+ self.assertEqual(chunks, ["A\né"])+ self.assertEqual(stats["completion_tokens"], 1)+ self.assertEqual(stats["prompt_tokens"], 5)+ self.assertFalse(stats["length_limited"])+def test_dispatches_interleaved_requests_by_id(self):submitted = []