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

fix(cuda): unwind failed Qwen tier initialization

ZacharyZcRfe29defe5373
diff --git a/c/Makefile b/c/Makefile
index 341ad971..ce42dc61 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -2180,3 +2180,7 @@ tests/test_kv_prefix$(EXE): tests/test_kv_prefix.c kv_prefix.h
tests/test_kimi_request_state$(EXE): tests/test_kimi_request_state.c kimi_k3.c kv_prefix.h serve_codec.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h idot.h omp_tune.h route_trace.h
$(CC) $(NOCUDA_CFLAGS) tests/test_kimi_request_state.c $(VK_OBJ) -o tests/test_kimi_request_state$(EXE) $(NOCUDA_LDFLAGS)
+
+# Failed tier startup must release host storage and initialized synchronization.
+tests/test_qwen36_tier_init_failure$(EXE): tests/test_qwen36_tier_init_failure.c tests/qwen36_fake_cuda.h qwen36_tier.c qwen36_tier.h backend_cuda.h compat.h
+ $(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)
diff --git a/c/qwen36_tier.c b/c/qwen36_tier.c
index 2b2e24e5..b7ac14ed 100644
--- a/c/qwen36_tier.c
+++ b/c/qwen36_tier.c
@@ -695,7 +695,7 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,
} else fprintf(stderr,"[place] experts=%d nicht verfuegbar -> COLI_GPUS bleibt\n",ed);
} else if(!G_auto_on && G_place_n && qt_place_named("experts")){
fprintf(stderr,"[place] experts=cpu -> VRAM-Tier aus\n");
- return 0;
+ goto fail_storage;
} else if(nres && !G_auto_on){
int w=0;
for(int i=0;i<G.ndev;i++){
@@ -734,12 +734,14 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,
* (#1339). The stride and G.is_k's row capacity are the same constant. */
G.is_x_floats=(size_t)G.ndev*QT_MAX_ROWS*D;
G.is_x=malloc(G.is_x_floats*sizeof(float));
- if(!G.is_x) return 0;
- pthread_mutex_init(&G.mx,NULL); pthread_cond_init(&G.cv,NULL); pthread_cond_init(&G.cv_take,NULL);
+ if(!G.is_x) goto fail_storage;
+ if(pthread_mutex_init(&G.mx,NULL)) goto fail_storage;
+ if(pthread_cond_init(&G.cv,NULL)) goto fail_mutex;
+ if(pthread_cond_init(&G.cv_take,NULL)) goto fail_cv;
qt_aff_get(&aff); qt_aff_widen(&aff); /* the uploader inherits this mask */
int th_ok=pthread_create(&G.th,NULL,uploader,NULL)==0;
qt_aff_restore(&aff);
- if(!th_ok) return 0;
+ if(!th_ok) goto fail_cv_take;
G.on=1;
fprintf(stderr,"[qtier] CUDA VRAM expert tier active: %d device(s), %.2f MB/expert\n",
G.ndev, G.exp_bytes/1048576.0);
@@ -749,6 +751,20 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,
* as CPU-only and --gpu was refused (#1533). */
fprintf(stderr,"[CUDA] mode: routed experts (qwen36 VRAM tier)\n");
return 1;
+
+fail_cv_take:
+ pthread_cond_destroy(&G.cv_take);
+fail_cv:
+ pthread_cond_destroy(&G.cv);
+fail_mutex:
+ pthread_mutex_destroy(&G.mx);
+fail_storage:
+ free(G.is_x); G.is_x=NULL; G.is_x_floats=0;
+ free(G.heat0); G.heat0=NULL;
+ free(G.slot); G.slot=NULL;
+ G_lmh.dev_ok=0;
+ /* CUDA contexts may also serve standalone dense projections. */
+ return 0;
}
int qt_ready(void){ return G.on; }
diff --git a/c/tests/test_qwen36_tier_init_failure.c b/c/tests/test_qwen36_tier_init_failure.c
new file mode 100644
index 00000000..0d63b5b2
--- /dev/null
+++ b/c/tests/test_qwen36_tier_init_failure.c
@@ -0,0 +1,74 @@
+/* Every failed initialization must unwind only the resources it acquired. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <errno.h>
+#include "../compat.h"
+#include "qwen36_fake_cuda.h"
+static void *owned[128];
+static int live, mutexes, conds, fail_stage, sync_step;
+static void *remember(void *p) {
+ if(p) { if(live==128) abort(); owned[live++]=p; }
+ return p;
+}
+static void *test_malloc(size_t n) {
+ if(fail_stage==1 && n==32*64*sizeof(float)) return NULL;
+ return remember(malloc(n));
+}
+static void *test_calloc(size_t n,size_t s) { return remember(calloc(n,s)); }
+static void test_free(void *p) {
+ if(p) for(int i=0;i<live;i++) if(owned[i]==p) { owned[i]=owned[--live]; break; }
+ free(p);
+}
+static int test_mutex_init(pthread_mutex_t *p,const pthread_mutexattr_t *a) {
+ if(++sync_step==fail_stage-1) return EAGAIN;
+ int rc=pthread_mutex_init(p,a);if(!rc) mutexes++;return rc;
+}
+static int test_cond_init(pthread_cond_t *p,const pthread_condattr_t *a) {
+ if(++sync_step==fail_stage-1) return EAGAIN;
+ int rc=pthread_cond_init(p,a);if(!rc) conds++;return rc;
+}
+static int test_mutex_destroy(pthread_mutex_t *p) {
+ int rc=pthread_mutex_destroy(p);if(!rc) mutexes--;return rc;
+}
+static int test_cond_destroy(pthread_cond_t *p) {
+ int rc=pthread_cond_destroy(p);if(!rc) conds--;return rc;
+}
+static int test_thread_create(pthread_t *t,const pthread_attr_t *a,
+ void *(*fn)(void *),void *arg) {
+ if(fail_stage==5) return EAGAIN;
+ return pthread_create(t,a,fn,arg);
+}
+#define malloc test_malloc
+#define calloc test_calloc
+#define free test_free
+#define pthread_mutex_init test_mutex_init
+#define pthread_cond_init test_cond_init
+#define pthread_mutex_destroy test_mutex_destroy
+#define pthread_cond_destroy test_cond_destroy
+#define pthread_create test_thread_create
+#include "../qwen36_tier.c"
+int main(void) {
+ char path[128];snprintf(path,sizeof path,"qt-init-heat-%ld.tmp",(long)getpid());
+ FILE *f=fopen(path,"wb");if(!f) return 1;
+ uint32_t data[]={0x51544831u,1,2,8,4};
+ if(fwrite(data,sizeof data,1,f)!=1){fclose(f);remove(path);return 1;}fclose(f);
+ setenv("COLI_CUDA","1",1);setenv("COLI_GPUS","0",1);
+ setenv("HEAT_FILE",path,1);setenv("CUDA_EXPERT_GB","0.0625",1);
+ int failed=0;
+ for(fail_stage=0;fail_stage<=5;fail_stage++) for(int retry=0;retry<2;retry++) {
+ setenv("COLI_PLACE",fail_stage==0?"experts=cpu,lmhead=0":"experts=0,lmhead=0",1);
+ sync_step=0;
+ int rc=qt_init(1,2,64,32,2,1,0,1);
+ if(rc || G.on || G.slot || G.heat0 || G.is_x || G.is_x_floats ||
+ G_lmh.dev_ok || live || mutexes || conds){
+ fprintf(stderr,"init failure stage=%d retry=%d rc=%d allocations=%d mutexes=%d conds=%d\n",
+ fail_stage,retry,rc,live,mutexes,conds);
+ failed=1;goto done;
+ }
+ }
+done:
+ remove(path);
+ if(!failed) puts("tier init failure: ok (12 cases)");
+ return failed;
+}