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): release Qwen tier resources on shutdown

ZacharyZcR09776503ebd9
diff --git a/c/Makefile b/c/Makefile
index 341ad971..fc32daaa 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)
+
+# Release owned projections and experts before CUDA teardown, then reopen safely.
+tests/test_qwen36_tier_release$(EXE): tests/test_qwen36_tier_release.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..dffa5ed8 100644
--- a/c/qwen36_tier.c
+++ b/c/qwen36_tier.c
@@ -45,7 +45,7 @@ static struct {
QSlot *slot; /* [nl*ne] */
pthread_mutex_t mx;
pthread_t th;
- int th_stop;
+ int th_stop, waiters;
/* upload ring with staging copies */
struct { int layer, eid; uint8_t *w; float *s; int v_layer, v_eid; } q[QT_QCAP];
int qh, qt_, qn;
@@ -68,6 +68,13 @@ static struct {
uint32_t *heat0; /* heat table loaded from HEAT_FILE */
} G;
+/* Count parked callers so shutdown can reclaim their shared storage safely. */
+static void wait_take_locked(void){
+ G.waiters++;
+ pthread_cond_wait(&G.cv_take,&G.mx);
+ if(--G.waiters==0 && G.th_stop) pthread_cond_broadcast(&G.cv_take);
+}
+
static QSlot *qs(int layer, int eid){ return &G.slot[(size_t)layer*G.ne + eid]; }
static int home(int eid){ return eid % G.ndev; }
@@ -156,7 +163,7 @@ static void *uploader(void *arg){
pthread_cond_broadcast(&G.cv_take); /* queue space available */
if(ve>=0){
/* LFRU swap: free the victim only when no group is in flight */
- while(G.issue_open && !G.th_stop) pthread_cond_wait(&G.cv_take,&G.mx);
+ while(G.issue_open && !G.th_stop) wait_take_locked();
QSlot *v=qs(vl,ve);
if(G.th_stop && G.issue_open){
/* Shutting down with a group still open: qt_take() -- the only
@@ -922,7 +929,7 @@ void qt_note_block(int layer,int eid,
pthread_mutex_lock(&G.mx);
if(G_fp8_stream) stream_point(s,g4,u4,d4,gs,us,ds);
else if(!s->g4){ s->g4=g4; s->u4=u4; s->d4=d4; s->gs=gs; s->us=us; s->ds=ds; }
- while(G.qn>=QT_QCAP && !G.th_stop) pthread_cond_wait(&G.cv_take,&G.mx);
+ while(G.qn>=QT_QCAP && !G.th_stop) wait_take_locked();
enqueue_locked(layer,eid,-1,-1,0);
if(G_fp8_stream) stream_forget(s);
pthread_mutex_unlock(&G.mx);
@@ -1015,7 +1022,7 @@ void qt_note_planned(int layer,int eid,
}
if(G_fp8_stream) stream_point(s,g4,u4,d4,gs,us,ds);
else if(!s->g4){ s->g4=g4; s->u4=u4; s->d4=d4; s->gs=gs; s->us=us; s->ds=ds; }
- while(G.qn>=QT_QCAP && !G.th_stop) pthread_cond_wait(&G.cv_take,&G.mx);
+ while(G.qn>=QT_QCAP && !G.th_stop) wait_take_locked();
if(!enqueue_locked(layer,eid,-1,-1,1)){
/* not enqueueable (e.g. already resident): return the reservation */
if(s->planned) G.used[home(eid)]-=G.exp_bytes;
@@ -1034,7 +1041,7 @@ void qt_note_planned(int layer,int eid,
void qt_fill_wait(void){
if(!G.on) return;
pthread_mutex_lock(&G.mx);
- while(G.inflight>0 && !G.th_stop) pthread_cond_wait(&G.cv_take,&G.mx);
+ while(G.inflight>0 && !G.th_stop) wait_take_locked();
pthread_mutex_unlock(&G.mx);
}
@@ -1081,7 +1088,7 @@ uint32_t qt_issue(int layer,const int *eids,int K,const float *x){
* did not get scheduled once before the run was over (0 uploads, 0 hits,
* six entries still queued). No group is open here, so the wait cannot
* meet a swap parked on issue_open. */
- if(G_upload_sync) while(G.inflight>0 && !G.th_stop) pthread_cond_wait(&G.cv_take,&G.mx);
+ if(G_upload_sync) while(G.inflight>0 && !G.th_stop) wait_take_locked();
if(layer==0) qt_lfru_tick_locked();
G.issue_open=1;
for(int k=0;k<K;k++){
@@ -1158,6 +1165,11 @@ void qt_stats(void){
static void dense_free_all(void){
for(int h = 0; h < G_dense_n; h++){ if(G_dense[h].t) coli_cuda_tensor_free(G_dense[h].t); G_dense[h].t = NULL; G_dense[h].on = 0; }
G_dense_n = 0;
+ if(G_lmh.t) coli_cuda_tensor_free(G_lmh.t);
+ memset(&G_lmh,0,sizeof G_lmh);
+ for(int l=0;l<QT_DN_MAX_LAYERS;l++)
+ if(G_dnp[l].t) coli_cuda_tensor_free(G_dnp[l].t);
+ memset(G_dnp,0,sizeof G_dnp);
}
void qt_shutdown(void){
dense_free_all();
@@ -1178,6 +1190,27 @@ void qt_shutdown(void){
* otherwise never notice th_stop and pthread_join below would hang (#1340). */
pthread_mutex_lock(&G.mx); G.th_stop=1; pthread_cond_signal(&G.cv); pthread_cond_broadcast(&G.cv_take); pthread_mutex_unlock(&G.mx);
pthread_join(G.th,NULL);
+ pthread_mutex_lock(&G.mx);
+ while(G.waiters) pthread_cond_wait(&G.cv_take,&G.mx);
+ pthread_mutex_unlock(&G.mx);
+ /* The uploader is stopped, but a decode group may still own the tensors. */
+ for(int di=0;di<G.ndev;di++)
+ if(G.is_cnt[di]) coli_cuda_expert_group_take(G.dev[di]);
+ for(size_t i=0;i<(size_t)G.nl*G.ne;i++){
+ QSlot *s=&G.slot[i];
+ if(s->tg) coli_cuda_tensor_free(s->tg);
+ if(s->tu) coli_cuda_tensor_free(s->tu);
+ if(s->td) coli_cuda_tensor_free(s->td);
+ }
+ free(G.slot); G.slot=NULL;
+ free(G.is_x); G.is_x=NULL; G.is_x_floats=0;
+ free(G.fill_order); G.fill_order=NULL;
+ free(G.heat0); G.heat0=NULL;
+ pthread_cond_destroy(&G.cv_take);
+ pthread_cond_destroy(&G.cv);
+ pthread_mutex_destroy(&G.mx);
+ G.issue_open=0;
+ memset(G.is_cnt,0,sizeof G.is_cnt);
G.on=0;
G_fp8_stream=0;
coli_cuda_shutdown();
diff --git a/c/tests/test_qwen36_tier_invariants.c b/c/tests/test_qwen36_tier_invariants.c
index daa0e4e9..621610f5 100644
--- a/c/tests/test_qwen36_tier_invariants.c
+++ b/c/tests/test_qwen36_tier_invariants.c
@@ -322,14 +322,8 @@ static void test_shutdown_wakes_everyone(void) {
check(!G.on, "shutdown_returns_with_four_waiters_parked");
check(t_note_block_done && t_note_planned_done && t_fill_wait_done,
"every cv_take waiter must return once shutdown has been requested");
- /* the abandoned swaps left the books consistent: the victim still holds
- * its tensor and says so, no queued flag survives */
- pthread_mutex_lock(&G.mx);
- int stale_queued = 0;
- for (int l = 0; l < NL; l++) for (int e = 0; e < NE; e++) stale_queued += qs(l, e)->queued;
- pthread_mutex_unlock(&G.mx);
- check(qs(0, 0)->resident == 1 && qs(0, 0)->tg != NULL, "the victim of an abandoned swap keeps its tensor and its resident flag");
- check(stale_queued == 0, "no expert may still read as queued after shutdown drained or abandoned the queue");
+ check(!G.slot && !G.is_x && !G.waiters,
+ "shutdown releases storage only after every parked caller has resumed");
}
/* ======================================================================== */
diff --git a/c/tests/test_qwen36_tier_release.c b/c/tests/test_qwen36_tier_release.c
new file mode 100644
index 00000000..27873845
--- /dev/null
+++ b/c/tests/test_qwen36_tier_release.c
@@ -0,0 +1,68 @@
+/* Owned tensors must be released before backend shutdown, including standalone
+ * dense projections. A pending expert group must finish before its weights go. */
+#include <math.h>
+#include <stdio.h>
+#include "../compat.h"
+#define coli_cuda_tensor_free fixture_free
+#define coli_cuda_shutdown fixture_shutdown
+#define coli_cuda_expert_group_take fixture_take
+#include "qwen36_fake_cuda.h"
+#undef coli_cuda_tensor_free
+#undef coli_cuda_shutdown
+#undef coli_cuda_expert_group_take
+static int freed, stops, pending, failures;
+static ColiCudaTensor *borrowed[3];
+#define CHECK(c) do { if(!(c)){fprintf(stderr,"FAIL %d: %s\n",__LINE__,#c);failures++;} } while(0)
+void coli_cuda_tensor_free(ColiCudaTensor *t) {
+ if(!t) return;
+ for(int i=0;i<3;i++) CHECK(!pending || t!=borrowed[i]);
+ freed++; fixture_free(t);
+}
+void coli_cuda_shutdown(void) {
+ CHECK(!pending); CHECK(freed==fake_uploads); stops++;
+}
+const float *coli_cuda_expert_group_take(int device) { pending=0; return NULL; }
+static int issue(int device,int count,const float *x) { pending=1; return 1; }
+#include "../qwen36_tier.c"
+int main(void) {
+ enum { D=64, I=32 };
+ int8_t q[D*D]; float sc[D], x[D], y[D];
+ memset(q,1,sizeof q);
+ for(int i=0;i<D;i++){sc[i]=1.f;x[i]=1.f;}
+ fake_dense_compute=1;
+ for(int round=1;round<=2;round++) {
+ memset(q,round,sizeof q);
+ CHECK(qt_dnproj_init(0,q,sc,D,D,0));
+ CHECK(qt_dense_init(q,sc,D,D,0)>=0);
+ CHECK(qt_dnproj_matmul(0,y,x,D,D));
+ CHECK(y[0]==D*round);
+ qt_shutdown();
+ CHECK(freed==fake_uploads); CHECK(!G_dnp[0].t && !G_dnp[0].on);
+ CHECK(!qt_dnproj_matmul(0,y,x,D,D)); CHECK(qt_dense_count()==0);
+ int before=freed; qt_shutdown(); CHECK(freed==before); CHECK(stops==0);
+ }
+ setenv("COLI_CUDA","1",1); setenv("COLI_GPUS","0",1);
+ setenv("COLI_PLACE","experts=0,lmhead=0,dnproj=0",1);
+ setenv("QT_NO_WARMSTART","1",1); setenv("HEAT_FILE","",1);
+ setenv("CUDA_EXPERT_GB","0.0625",1);
+ for(int round=0;round<2;round++) {
+ CHECK(qt_init(1,2,D,I,2,1,0,1));
+ CHECK(qt_dnproj_init(0,q,sc,D,D,0));
+ CHECK(qt_lmhead_init(q,sc,D,D));
+ CHECK(qt_dense_init(q,sc,D,D,0)>=0);
+ uint8_t g[D*I/2]={0}, u[D*I/2]={0}, d[D*I/2]={0};
+ qt_note(0,0,g,u,d,sc,sc,sc); qt_fill_wait();
+ CHECK(qt_is_resident(0,0));
+ borrowed[0]=qs(0,0)->tg; borrowed[1]=qs(0,0)->tu; borrowed[2]=qs(0,0)->td;
+ fake_issue_hook=issue;
+ int eid=0; CHECK(qt_issue(0,&eid,1,x)==1);
+ qt_shutdown();
+ CHECK(!pending); CHECK(freed==fake_uploads); CHECK(!G.on);
+ CHECK(!G.slot && !G.is_x && !G.fill_order && !G.heat0);
+ CHECK(!G_lmh.t && !G_lmh.on && !G_lmh.dev_ok);
+ CHECK(!G_dnp[0].t && !G_dnp[0].on);
+ int before=freed; qt_shutdown(); CHECK(freed==before);
+ }
+ printf("tier release: %s (%d uploads, %d frees)\n",failures?"FAIL":"ok",fake_uploads,freed);
+ return failures!=0;
+}
diff --git a/c/tests/test_qwen36_tier_shutdown.c b/c/tests/test_qwen36_tier_shutdown.c
index 5e46330d..16a3370c 100644
--- a/c/tests/test_qwen36_tier_shutdown.c
+++ b/c/tests/test_qwen36_tier_shutdown.c
@@ -135,14 +135,9 @@ int main(void) {
qt_shutdown();
shutdown_done = 1;
check(!G.on, "shutdown_returns_while_a_group_is_open");
- /* The abandoned swap must leave the victim exactly as the open group left
- * it, and must not have driven the incoming expert's upload after
- * shutdown began. The uploader is already joined here, so reading G
- * needs no lock. */
- check(qs(0, resident_eid)->resident && qs(0, resident_eid)->tg,
- "shutdown_abandons_the_swap_instead_of_freeing_the_victim");
- check(!qs(0, 1)->queued && !qs(0, 1)->resident && G.uploads == 1,
- "shutdown_abandons_the_swap_instead_of_uploading_the_incoming_expert");
+ check(!G.slot && !G.is_x, "shutdown_releases_tier_storage");
+ check(G.uploads == 1 && fake_uploads == 3,
+ "shutdown_abandons_the_swap_without_uploading_the_incoming_expert");
if (fails) { printf("test_qwen36_tier_shutdown: %d fallimenti\n", fails); return 1; }
printf("test_qwen36_tier_shutdown: ok\n");