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

qwen36: offload batched attention projections to CUDA

ZacharyZcR8e8447cc7d9f
diff --git a/c/qwen36.c b/c/qwen36.c
index 49ced244..41a8dbe5 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -1253,8 +1253,11 @@ static void matmul_d(float *y, const float *x, const float *W, int S, int I, int
matmul(y, x, W, S, I, O);
}
/* A dense matrix the tier placed in VRAM (handle+1 kept in the Layer, 0 = CPU):
- * one GEMV from the device, or 0 and the caller runs matmul_d as before. The
+ * a device matmul, or 0 and the caller runs matmul_d as before. The
* tier turns a failing handle off itself, so the fallback is permanent. */
+static inline int qtd_batch(int hp1, float *y, const float *x, int S, int I, int O){
+ return hp1 > 0 && qt_dense_matmul_batch(hp1 - 1, y, x, S, I, O);
+}
static inline int qtd(int hp1, float *y, const float *x, int I, int O){
return hp1 > 0 && qt_dense_matmul(hp1 - 1, y, x, I, O);
}
@@ -2033,11 +2036,11 @@ static void attention(Model *m, Layer *l, int layer, float *x, int S, int pos_ba
float *q = falloc((int64_t)S*q_out);
float *k = falloc((int64_t)S*kv_out);
float *vv= falloc((int64_t)S*kv_out);
- /* Decode (S == 1): the projections the tier placed answer from VRAM,
- * one GEMV each; a prompt batch keeps the batched CPU matmul. */
- if (!(S == 1 && qtd(l->qth_q, q, x, D, q_out))) matmul_d(q, x, l->q, S, D, q_out);
- if (!(S == 1 && qtd(l->qth_k, k, x, D, kv_out))) matmul_d(k, x, l->k, S, D, kv_out);
- if (!(S == 1 && qtd(l->qth_v, vv, x, D, kv_out))) matmul_d(vv, x, l->v, S, D, kv_out);
+ /* The projections the tier placed answer from VRAM for the whole batch,
+ * with one backend call per matrix; unavailable handles use CPU matmul. */
+ if (!qtd_batch(l->qth_q, q, x, S, D, q_out)) matmul_d(q, x, l->q, S, D, q_out);
+ if (!qtd_batch(l->qth_k, k, x, S, D, kv_out)) matmul_d(k, x, l->k, S, D, kv_out);
+ if (!qtd_batch(l->qth_v, vv, x, S, D, kv_out)) matmul_d(vv, x, l->v, S, D, kv_out);
/* split q into query (first hd) and gate (next gate_dim), both per head */
float *query = falloc((int64_t)S*H*hd);
float *gate = falloc((int64_t)S*H*gate_dim);
@@ -2099,7 +2102,7 @@ static void attention(Model *m, Layer *l, int layer, float *x, int S, int pos_ba
float g = gate_dim ? gate[o] : 0.f;
ag[o] = ctx[o] * (1.f / (1.f + expf(-g)));
}
- if (!(S == 1 && qtd(l->qth_o, out, ag, H*hd, D))) matmul_d(out, ag, l->o, S, H*hd, D);
+ if (!qtd_batch(l->qth_o, out, ag, S, H*hd, D)) matmul_d(out, ag, l->o, S, H*hd, D);
free(q); free(k); free(vv); free(query); free(gate); free(ctx); free(ag);
}
diff --git a/c/qwen36_tier.c b/c/qwen36_tier.c
index 2b2e24e5..78cd5d62 100644
--- a/c/qwen36_tier.c
+++ b/c/qwen36_tier.c
@@ -801,13 +801,16 @@ int qt_dense_init(const int8_t *q, const float *sc, int I, int O, int device){
G_dense_n++;
return h;
}
-int qt_dense_matmul(int h, float *y, const float *x, int I, int O){
- if(h < 0 || h >= G_dense_n || !G_dense[h].on) return 0;
- if(coli_cuda_matmul(&G_dense[h].t, y, x, NULL, NULL, 1, 1, I, O, G_dense[h].dev, 0)) return 1;
+int qt_dense_matmul_batch(int h, float *y, const float *x, int S, int I, int O){
+ if(h < 0 || h >= G_dense_n || !G_dense[h].on || S <= 0) return 0;
+ if(coli_cuda_matmul(&G_dense[h].t, y, x, NULL, NULL, 1, S, I, O, G_dense[h].dev, 0)) return 1;
fprintf(stderr,"[dense] handle %d GPU matmul failed; CPU from here on\n", h);
G_dense[h].on = 0;
return 0;
}
+int qt_dense_matmul(int h, float *y, const float *x, int I, int O){
+ return qt_dense_matmul_batch(h, y, x, 1, I, O);
+}
int qt_dense_count(void){ return G_dense_n; }
int qt_dnproj_matmul(int layer, float *y, const float *x, int I, int O){
diff --git a/c/qwen36_tier.h b/c/qwen36_tier.h
index 228288d6..d8b319d7 100644
--- a/c/qwen36_tier.h
+++ b/c/qwen36_tier.h
@@ -85,6 +85,8 @@ int qt_dnproj_matmul(int layer, float *y, const float *x, int I, int O);
* Returns the handle (>= 0) or -1 (stays on the CPU). */
int qt_dense_init(const int8_t *q, const float *sc, int I, int O, int device);
int qt_dense_matmul(int handle, float *y, const float *x, int I, int O);
+/* Row-major x[S,I] -> y[S,O], using the same resident int8 tensor. */
+int qt_dense_matmul_batch(int handle, float *y, const float *x, int S, int I, int O);
int qt_dense_count(void);
/* fp8 streaming mode (Qwen3.8): experts arrive as e4m3 bytes with 128x128
@@ -147,6 +149,7 @@ static inline int qt_dnproj_init(int a,const int8_t*b,const float*c,int d,int e
static inline int qt_dnproj_matmul(int a,float*b,const float*c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return 0;}
static inline int qt_dense_init(const int8_t*a,const float*b,int c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return -1;}
static inline int qt_dense_matmul(int a,float*b,const float*c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return 0;}
+static inline int qt_dense_matmul_batch(int a,float*b,const float*c,int d,int e,int f){(void)a;(void)b;(void)c;(void)d;(void)e;(void)f;return 0;}
static inline int qt_dense_count(void){return 0;}
static inline int qt_ready(void){return 0;}
static inline int qt_is_resident(int a,int b){(void)a;(void)b;return 0;}
diff --git a/c/tests/qwen36_fake_cuda.h b/c/tests/qwen36_fake_cuda.h
index 46d8f7a3..03e9b01d 100644
--- a/c/tests/qwen36_fake_cuda.h
+++ b/c/tests/qwen36_fake_cuda.h
@@ -110,9 +110,11 @@ void coli_cuda_stats(int device, size_t *count, size_t *bytes) {
* device). Counted, never computed: the placement tests check WHERE work
* went; the arithmetic has its own oracle in the CUDA build. Parameters are
* unused on purpose (CFLAGS carry -Wno-unused-parameter). */
-static int fake_matmuls;
+static int fake_matmuls, fake_matmul_fail, fake_matmul_rows;
int coli_cuda_matmul(ColiCudaTensor **tensor, float *y, const float *x, const void *weights, const float *scales, int fmt, int S, int I, int O, int device, int gs) {
fake_matmuls++;
+ fake_matmul_rows = S;
+ if (fake_matmul_fail) return 0;
ColiCudaTensor *t = tensor ? *tensor : NULL;
if (fake_dense_compute && t && t->fmt == 1 && t->w && t->sc && t->I == I && t->O == O) {
const int8_t *q = (const int8_t *)t->w;
diff --git a/c/tests/test_qwen36_tier_dense.c b/c/tests/test_qwen36_tier_dense.c
index 1a63c41d..72712664 100644
--- a/c/tests/test_qwen36_tier_dense.c
+++ b/c/tests/test_qwen36_tier_dense.c
@@ -22,7 +22,7 @@ int main(void) {
setenv("COLI_CUDA", "1", 1); setenv("COLI_GPUS", "0", 1);
setenv("QT_NO_WARMSTART", "1", 1); setenv("HEAT_FILE", "", 1);
setenv("COLI_PLACE", "", 1); /* "" == unset == auto; unsetenv has no UCRT64 shim */
- fake_ndev = 1; fake_uploads = 0;
+ fake_ndev = 1; fake_uploads = 0; fake_dense_compute = 1;
/* room for 8 experts plus a little; each offer is 1 expert worth of bytes */
size_t exp_bytes = 3 * dev_alloc_footprint((size_t)D * IH / 2) + 3 * dev_alloc_footprint((2 * IH + D) / 3 * sizeof(float));
@@ -66,6 +66,29 @@ int main(void) {
check(qt_dense_matmul(h2, y, x, I, O) == 1 && fake_matmuls == mm + 2, "so does the second handle");
check(qt_dense_matmul(99, y, x, I, O) == 0 && qt_dense_matmul(-1, y, x, I, O) == 0 && fake_matmuls == mm + 2, "unknown handles are refused without a backend call");
+ {
+ float xb[3 * I], yb[3 * O];
+ for (int i = 0; i < 3 * I; i++) xb[i] = (float)(i % 7 - 3);
+ check(qt_dense_matmul_batch(h, yb, xb, 3, I, O), "batch accepted");
+ check(fake_matmul_rows == 3, "all rows sent in one backend call");
+ int equal = 1;
+ for (int row = 0; row < 3; row++) for (int o = 0; o < O; o++) {
+ float want = 0;
+ for (int i = 0; i < I; i++) want += xb[row * I + i] * q[o * I + i];
+ if (yb[row * O + o] != want * sc[o]) equal = 0;
+ }
+ check(equal, "every batch row uses the resident weights and row scales");
+ int calls = fake_matmuls;
+ check(!qt_dense_matmul_batch(h, yb, xb, 0, I, O) && fake_matmuls == calls,
+ "empty batch declined without a backend call");
+ fake_matmul_fail = 1;
+ check(!qt_dense_matmul_batch(h, yb, xb, 3, I, O), "batch failure requests CPU fallback");
+ fake_matmul_fail = 0;
+ calls = fake_matmuls;
+ check(!qt_dense_matmul(h, yb, xb, I, O) && fake_matmuls == calls,
+ "failed handle remains disabled for decode too");
+ }
+
printf(" 4. shutdown releases the matrices\n");
qt_shutdown();
check(qt_dense_count() == 0, "dense handles released at shutdown");
diff --git a/c/tests/test_qwen36_trunk_dense.c b/c/tests/test_qwen36_trunk_dense.c
index 02a9e1a3..ef969576 100644
--- a/c/tests/test_qwen36_trunk_dense.c
+++ b/c/tests/test_qwen36_trunk_dense.c
@@ -33,7 +33,7 @@ static void ck(int ok, const char *what) {
}
/* Small but every dimension distinct, so a swapped I/O would show. */
-enum { NL = 2, D = 48, VH = 2, VD = 8, QH = 2, QD = 12, KVH = 1, KD = 8, SH = 20, NE = 4, IH = 16 };
+enum { NL = 2, D = 48, VH = 2, VD = 8, QH = 2, QD = 16, KVH = 1, KD = 8, SH = 20, NE = 4, IH = 16 };
static unsigned g_seed = 12345;
static float rnd(void) {
@@ -156,6 +156,39 @@ int main(void) {
ck(!served, "a matrix with no handle is not served (the caller runs matmul_d)");
}
+ /* Drive attention itself: this catches an S==1 gate left at any of the
+ * four projection call sites, which a tier-only test cannot detect. */
+ {
+ enum { S = 3 };
+#ifdef _OPENMP
+ omp_set_num_threads(1);
+#endif
+ m.max_t = m.kv_cap = S;
+ m.K = calloc(NL, sizeof(float *)); m.V = calloc(NL, sizeof(float *));
+ m.K[1] = calloc(S * KVH * KD, sizeof(float));
+ m.V[1] = calloc(S * KVH * KD, sizeof(float));
+ m.attn_sc = calloc(S, sizeof(float));
+ float *x = rnd_matrix(S, D), gpu[S * D], cpu[S * D], fallback[S * D];
+ Layer host = *at;
+ host.qth_q = host.qth_k = host.qth_v = host.qth_o = 0;
+ attention(&m, &host, 1, x, S, 0, cpu);
+ int calls = fake_matmuls;
+ attention(&m, at, 1, x, S, 0, gpu);
+ ck(fake_matmuls == calls + 4 && fake_matmul_rows == S,
+ "prefill dispatches all four projections as batches");
+ fake_matmul_fail = 1;
+ attention(&m, at, 1, x, S, 0, fallback);
+ fake_matmul_fail = 0;
+ double gap = 0, scale = 1e-6;
+ for (int i = 0; i < S * D; i++) {
+ gap = fmax(gap, fabs((double)gpu[i] - cpu[i]));
+ gap = fmax(gap, fabs((double)fallback[i] - cpu[i]));
+ scale = fmax(scale, fabs(cpu[i]));
+ }
+ ck(gap / scale < 1e-4, "batch attention and failed-backend fallback agree with CPU");
+ free(x); free(m.K[1]); free(m.V[1]); free(m.K); free(m.V); free(m.attn_sc);
+ }
+
qt_shutdown();
if (fails) { printf("test_qwen36_trunk_dense: %d failure(s)\n", fails); return 1; }
printf("OK test_qwen36_trunk_dense: dnout, attnproj and shexp offered, placed and served from VRAM\n");