Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(qwen36): size the tier's input replicas per device (#1339)
Courtney Andrew Richardsonb5f6f1a8d1dd
diff --git a/c/Makefile b/c/Makefileindex f7dbd287..3e5d7bd3 100644--- a/c/Makefile+++ b/c/Makefile@@ -1641,7 +1641,13 @@ tests/test_cli_args$(EXE): tests/test_cli_args.c cli_args.h# coli_cuda_* e registra cosa riceve. E' il solo modo di provare in CI che un# esperto arriva davvero in VRAM e nel formato giusto (#1331) -- un test che si# fermasse a "qt_init ritorna 1" sarebbe passato anche col difetto.-tests/test_qwen36_tier_int8$(EXE): tests/test_qwen36_tier_int8.c qwen36_tier.c qwen36_tier.h backend_cuda.h+tests/test_qwen36_tier_int8$(EXE): tests/test_qwen36_tier_int8.c tests/qwen36_fake_cuda.h qwen36_tier.c qwen36_tier.h backend_cuda.h+ $(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)++# Same fake backend (tests/qwen36_fake_cuda.h), two devices: proves the tier's+# per-device input-replica block (G.is_x) is sized and strided so device 1's+# block cannot run past the end of the buffer or into device 0's (#1339).+tests/test_qwen36_tier_multidev$(EXE): tests/test_qwen36_tier_multidev.c tests/qwen36_fake_cuda.h qwen36_tier.c qwen36_tier.h backend_cuda.h$(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)tests/test_serve_poll$(EXE): tests/test_serve_poll.c serve_poll.hdiff --git a/c/qwen36_tier.c b/c/qwen36_tier.cindex 9d8e8daa..4ac3209f 100644--- a/c/qwen36_tier.c+++ b/c/qwen36_tier.c@@ -46,7 +46,7 @@ static struct {/* issue state of the (single) decode thread */int is_cnt[QT_MAX_DEV];int is_k[QT_MAX_DEV][32];- float *is_x; /* count*D input replicas per device */+ float *is_x; size_t is_x_floats; /* 32*D input replicas per device *//* M3 */int *fill_order; int fill_cur; /* warmstart order (heat desc) */int issue_open; /* guard: no tensor_free while a group is in flight */@@ -198,7 +198,11 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,G.dev[i], freeb/1073741824.0, b/1073741824.0, b/G.exp_bytes);}G.slot=calloc((size_t)nl*ne,sizeof(QSlot));- G.is_x=malloc((size_t)32*D*sizeof(float));+ /* qt_issue strides each device's block by 32*D floats (its max row+ * count), not 8*D: a device other than 0 with a full 32-row issue used+ * to run past its own slice and off the end of this allocation (#1339). */+ G.is_x_floats=(size_t)G.ndev*32*D;+ G.is_x=malloc(G.is_x_floats*sizeof(float));if(!G.slot||!G.is_x) return 0;/* load learned heat (HEAT_FILE): warmstart order + initial values */const char *hf=getenv("HEAT_FILE");@@ -426,7 +430,7 @@ uint32_t qt_issue(int layer,const int *eids,int K,const float *x){for(int di=0;di<G.ndev;di++){int c=G.is_cnt[di];if(!c) continue;- float *xr=G.is_x + (size_t)di*8*G.D; /* per-device input block */+ float *xr=G.is_x + (size_t)di*32*G.D; /* per-device input block */for(int j=0;j<c;j++) memcpy(xr+(size_t)j*G.D, x, (size_t)G.D*sizeof(float));if(!coli_cuda_expert_group_issue(tg[di],tu[di],td[di],rows,c,xr)){/* issue failed -> hand these k back to the CPU */diff --git a/c/tests/qwen36_fake_cuda.h b/c/tests/qwen36_fake_cuda.hnew file mode 100644index 00000000..525b6904--- /dev/null+++ b/c/tests/qwen36_fake_cuda.h@@ -0,0 +1,96 @@+/* qwen36_fake_cuda.h -- fake CUDA backend shared by the qwen36 tier tests.+ *+ * Defines every coli_cuda_* symbol qwen36_tier.c links against (its+ * signatures come from backend_cuda.h, which the tier includes on its own)+ * and RECORDS what it receives, so a test can assert on real upload/issue+ * traffic without a GPU or the CUDA toolkit. A test that only checked+ * "qt_init returns 1" would pass even with the tier fully broken.+ *+ * Two settable hooks beyond plain recording:+ * fake_ndev - device count returned by coli_cuda_available_device_count+ * and coli_cuda_device_count (default 1).+ * fake_issue_hook - called by coli_cuda_expert_group_issue with the issuing+ * device (taken from g[0]->device), the row count and the+ * input pointer; its return value is what issue returns.+ * NULL (the default) reproduces the old always-0 stub. */+#ifndef QWEN36_FAKE_CUDA_H+#define QWEN36_FAKE_CUDA_H++#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <stdint.h>+#include <time.h>++/* MinGW non ha setenv: il tier legge la sua configurazione dall'ambiente, e il+ * test deve poterla impostare anche su Windows. */+#if defined(_WIN32)+#include <stdlib.h>+static int test_setenv(const char *name, const char *value, int overwrite) {+ (void)overwrite; return _putenv_s(name, value);+}+#define setenv test_setenv+#endif+#include "../backend_cuda.h"++struct ColiCudaTensor { int fmt, I, O, device, gs; const void *w; };++static int fake_uploads;+static int last_fmt = -1;+static size_t last_bytes;+static unsigned char captured[4096];+static size_t captured_len;++static int fake_ndev = 1;+static int (*fake_issue_hook)(int device, int count, const float *x) = NULL;++static int upload_common(ColiCudaTensor **t, const void *w, int fmt,+ int I, int O, int device, int gs) {+ ColiCudaTensor *n = (ColiCudaTensor *)calloc(1, sizeof *n);+ n->fmt = fmt; n->I = I; n->O = O; n->device = device; n->gs = gs; n->w = w;+ *t = n;+ fake_uploads++;+ last_fmt = fmt;+ last_bytes = (size_t)I * O / (fmt == 1 ? 1 : 2);+ if (fake_uploads == 1) {+ captured_len = last_bytes < sizeof captured ? last_bytes : sizeof captured;+ memcpy(captured, w, captured_len);+ }+ return 1;+}+int coli_cuda_tensor_upload(ColiCudaTensor **t, const void *w, const float *s,+ int fmt, int I, int O, int device) {+ (void)s; return upload_common(t, w, fmt, I, O, device, 0);+}+int coli_cuda_tensor_upload_g(ColiCudaTensor **t, const void *w, const float *s,+ int fmt, int I, int O, int device, int gs) {+ (void)s; return upload_common(t, w, fmt, I, O, device, gs);+}+void coli_cuda_tensor_free(ColiCudaTensor *t) { free(t); }+int coli_cuda_available_device_count(void) { return fake_ndev; }+int coli_cuda_device_count(void) { return fake_ndev; }+int coli_cuda_init(const int *d, int n) { (void)d; (void)n; return 1; }+void coli_cuda_shutdown(void) {}+int coli_cuda_mem_info(int device, size_t *freeb, size_t *total) {+ (void)device;+ *freeb = 2ull << 30; *total = 4ull << 30; /* 2 GiB liberi */+ return 1;+}+int coli_cuda_expert_group_issue(ColiCudaTensor *const *g, ColiCudaTensor *const *u,+ ColiCudaTensor *const *d, const int *rows,+ int count, const float *x) {+ (void)u; (void)d; (void)rows;+ if (fake_issue_hook) return fake_issue_hook(count > 0 ? g[0]->device : -1, count, x);+ return 0;+}+const float *coli_cuda_expert_group_take(int device) { (void)device; return NULL; }+void coli_cuda_group_stats(uint64_t *calls, uint64_t *experts, uint64_t *rows,+ double *h2d, double *kernel, double *d2h) {+ if (calls) *calls = 0; if (experts) *experts = 0; if (rows) *rows = 0;+ if (h2d) *h2d = 0; if (kernel) *kernel = 0; if (d2h) *d2h = 0;+}+void coli_cuda_stats(int device, size_t *count, size_t *bytes) {+ (void)device; if (count) *count = 0; if (bytes) *bytes = 0;+}++#endif /* QWEN36_FAKE_CUDA_H */diff --git a/c/tests/test_qwen36_tier_int8.c b/c/tests/test_qwen36_tier_int8.cindex 61279f85..8fae78d7 100644--- a/c/tests/test_qwen36_tier_int8.c+++ b/c/tests/test_qwen36_tier_int8.c@@ -17,75 +17,9 @@#include <string.h>/* ---- backend CUDA finto: le firme vengono da backend_cuda.h, che il tier- include per conto suo; qui si definisce solo il corpo. ---- */-#include <stdint.h>-#include <time.h>--/* MinGW non ha setenv: il tier legge la sua configurazione dall'ambiente, e il- * test deve poterla impostare anche su Windows. */-#if defined(_WIN32)-#include <stdlib.h>-static int test_setenv(const char *name, const char *value, int overwrite) {- (void)overwrite; return _putenv_s(name, value);-}-#define setenv test_setenv-#endif-#include "../backend_cuda.h"--struct ColiCudaTensor { int fmt, I, O, device, gs; const void *w; };--static int fake_uploads;-static int last_fmt = -1;-static size_t last_bytes;-static unsigned char captured[4096];-static size_t captured_len;--static int upload_common(ColiCudaTensor **t, const void *w, int fmt,- int I, int O, int device, int gs) {- ColiCudaTensor *n = (ColiCudaTensor *)calloc(1, sizeof *n);- n->fmt = fmt; n->I = I; n->O = O; n->device = device; n->gs = gs; n->w = w;- *t = n;- fake_uploads++;- last_fmt = fmt;- last_bytes = (size_t)I * O / (fmt == 1 ? 1 : 2);- if (fake_uploads == 1) {- captured_len = last_bytes < sizeof captured ? last_bytes : sizeof captured;- memcpy(captured, w, captured_len);- }- return 1;-}-int coli_cuda_tensor_upload(ColiCudaTensor **t, const void *w, const float *s,- int fmt, int I, int O, int device) {- (void)s; return upload_common(t, w, fmt, I, O, device, 0);-}-int coli_cuda_tensor_upload_g(ColiCudaTensor **t, const void *w, const float *s,- int fmt, int I, int O, int device, int gs) {- (void)s; return upload_common(t, w, fmt, I, O, device, gs);-}-void coli_cuda_tensor_free(ColiCudaTensor *t) { free(t); }-int coli_cuda_available_device_count(void) { return 1; }-int coli_cuda_device_count(void) { return 1; }-int coli_cuda_init(const int *d, int n) { (void)d; (void)n; return 1; }-void coli_cuda_shutdown(void) {}-int coli_cuda_mem_info(int device, size_t *freeb, size_t *total) {- (void)device;- *freeb = 2ull << 30; *total = 4ull << 30; /* 2 GiB liberi */- return 1;-}-int coli_cuda_expert_group_issue(ColiCudaTensor *const *g, ColiCudaTensor *const *u,- ColiCudaTensor *const *d, const int *rows,- int count, const float *x) {- (void)g; (void)u; (void)d; (void)rows; (void)count; (void)x; return 0;-}-const float *coli_cuda_expert_group_take(int device) { (void)device; return NULL; }-void coli_cuda_group_stats(uint64_t *calls, uint64_t *experts, uint64_t *rows,- double *h2d, double *kernel, double *d2h) {- if (calls) *calls = 0; if (experts) *experts = 0; if (rows) *rows = 0;- if (h2d) *h2d = 0; if (kernel) *kernel = 0; if (d2h) *d2h = 0;-}-void coli_cuda_stats(int device, size_t *count, size_t *bytes) {- (void)device; if (count) *count = 0; if (bytes) *bytes = 0;-}+ include per conto suo; il corpo (condiviso con gli altri test del tier) e'+ in qwen36_fake_cuda.h. ---- */+#include "qwen36_fake_cuda.h"#include "../qwen36_tier.c"diff --git a/c/tests/test_qwen36_tier_multidev.c b/c/tests/test_qwen36_tier_multidev.cnew file mode 100644index 00000000..e9a7686d--- /dev/null+++ b/c/tests/test_qwen36_tier_multidev.c@@ -0,0 +1,112 @@+/* The tier's per-device input-replica block overruns G.is_x (#1339).+ *+ * The defect: qt_init allocated G.is_x as 32*D floats total (one device's+ * worth), but qt_issue strides each device's block by 8*D floats and then+ * writes up to 32 rows into it -- device di's block starts at 8*di*D but can+ * span 32*D floats, so any di>0 with a full 32-row issue writes past both its+ * own slice and the end of the allocation. With two devices and 32 resident+ * experts routed to device 1, device 1's block starts at 8*D and ends at+ * 8*D+32*D = 40*D, well past the 32*D-float buffer -- silent heap corruption+ * that only ASan (or a real GPU driver) would ever complain about.+ *+ * This uses the same fake CUDA backend as test_qwen36_tier_int8.c+ * (tests/qwen36_fake_cuda.h) with fake_ndev=2 and a fake_issue_hook that+ * records exactly which (device, x pointer, row count) each issue call used,+ * so the test can check the recorded blocks against the real allocation+ * bounds without a GPU or ASan. */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "qwen36_fake_cuda.h"++#include "../qwen36_tier.c"++static int fails;+static void check(int ok, const char *what) {+ if (!ok) { printf(" FAIL: %s\n", what); fails++; }+}++enum { MAX_REC = 8 };+static struct { int device, count; const float *x; } records[MAX_REC];+static int nrec;+static int record_issue(int device, int count, const float *x) {+ if (nrec < MAX_REC) {+ records[nrec].device = device; records[nrec].count = count; records[nrec].x = x;+ nrec++;+ }+ return 1;+}++int main(void) {+ enum { NL = 1, NE = 64, D = 64, IH = 32, TOPK = 32 };+ setenv("COLI_CUDA", "1", 1);+ setenv("COLI_GPUS", "0,1", 1);+ setenv("QT_NO_WARMSTART", "1", 1);+ fake_ndev = 2;++ if (!qt_init(NL, NE, D, IH, NE, TOPK, 0 /* per-row */, 1 /* int4 */)) {+ printf(" FAIL: il tier non parte con due device finti\n");+ return 1;+ }++ /* Make every expert resident: qt_note_block blocks on queue space, so a+ * single qt_fill_wait() after the loop is enough to drain the rest. */+ static unsigned char g4[NE][D * IH / 2], u4[NE][D * IH / 2], d4[NE][D * IH / 2];+ static float sc[NE][2 * IH + D]; /* gs=0 -> sc_gu=IH, sc_d=D */+ for (int eid = 0; eid < NE; eid++) {+ memset(g4[eid], (unsigned char)(eid + 1), sizeof g4[eid]);+ memset(u4[eid], (unsigned char)(eid + 2), sizeof u4[eid]);+ memset(d4[eid], (unsigned char)(eid + 3), sizeof d4[eid]);+ for (int i = 0; i < 2 * IH + D; i++) sc[eid][i] = 1.0f;+ qt_note_block(0, eid, g4[eid], u4[eid], d4[eid], sc[eid], sc[eid] + IH, sc[eid] + 2 * IH);+ }+ qt_fill_wait();+ for (int eid = 0; eid < NE; eid++)+ check(qt_is_resident(0, eid), "expert did not become resident during warmstart");++ fake_issue_hook = record_issue;+ float x[D];+ for (int i = 0; i < D; i++) x[i] = (float)i;++ /* All 32 issued experts are odd -> home(eid)=eid%2 routes every one of+ * them to device 1, the case that overruns today. */+ int eids[32];+ for (int k = 0; k < 32; k++) eids[k] = 2 * k + 1;+ uint32_t mask = qt_issue(0, eids, 32, x);+ check(mask == 0xFFFFFFFFu, "issuing 32 resident odd-numbered experts did not set all 32 mask bits");+ check(nrec == 1 && records[0].device == 1 && records[0].count == 32,+ "32 odd-numbered experts should issue as one 32-row group on device 1");++ int base2 = nrec;+ int eids2[32];+ for (int k = 0; k < 16; k++) { eids2[k] = 2 * k; eids2[16 + k] = 2 * k + 1; }+ uint32_t mask2 = qt_issue(0, eids2, 32, x);+ check(mask2 == 0xFFFFFFFFu, "issuing a mixed even/odd batch did not set all 32 mask bits");++ int inside = 1;+ for (int i = 0; i < nrec; i++) {+ const float *lo = G.is_x, *hi = G.is_x + G.is_x_floats;+ if (!(records[i].x >= lo && records[i].x + (size_t)records[i].count * G.D <= hi))+ inside = 0;+ }+ check(inside, "blocks_stay_inside_the_replica_buffer");++ check(nrec == base2 + 2, "the mixed batch should issue once per device (two calls)");+ if (nrec == base2 + 2) {+ const float *a0 = records[base2].x, *a1 = records[base2 + 1].x;+ size_t c0 = (size_t)records[base2].count, c1 = (size_t)records[base2 + 1].count;+ int disjoint = (a0 + c0 * G.D <= a1) || (a1 + c1 * G.D <= a0);+ check(disjoint, "device_blocks_do_not_overlap");+ }++ float val[32]; for (int k = 0; k < 32; k++) val[k] = 1.0f;+ float out[D]; memset(out, 0, sizeof out);+ qt_take(mask2, val, 32, out); /* take returns NULL in the fake backend; fine */++ qt_shutdown();++ if (fails) { printf("test_qwen36_tier_multidev: %d fallimenti\n", fails); return 1; }+ printf("test_qwen36_tier_multidev: ok\n");+ return 0;+}