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

Merge pull request #1769 into integ/release-batch-5

JustVugg34b4b63216bf
diff --cc c/backend_cuda.h
index 7ee39fe7,e4aca5d3..a5412196
--- a/c/backend_cuda.h
+++ b/c/backend_cuda.h
@@@ -159,12 -159,15 +159,19 @@@ COLI_CUDA_DLLEXPORT ColiCudaDn *coli_cu
COLI_CUDA_DLLEXPORT void coli_cuda_dn_free(ColiCudaDn *d);
COLI_CUDA_DLLEXPORT int coli_cuda_dn_set_state(ColiCudaDn *d, const float *ring, const float *rec); /* NULL = zero */
COLI_CUDA_DLLEXPORT int coli_cuda_dn_get_state(ColiCudaDn *d, float *ring, float *rec);
-COLI_CUDA_DLLEXPORT int coli_cuda_dn_step(ColiCudaDn *d, ColiCudaTensor *proj, ColiCudaTensor *outp,
+/* proj: the in_proj rows. Fused qkv ++ z in one matrix (projz NULL, qwen36's
+ * dnproj) or qkv alone with z in projz (qwen38's dnqkv / dnz). gate_sigmoid at
+ * create: the gated RMSNorm multiplies by silu(z) (0, Qwen3.6) or sigmoid(z)
+ * (1, Qwen3.8). */
+COLI_CUDA_DLLEXPORT int coli_cuda_dn_step(ColiCudaDn *d, ColiCudaTensor *proj, ColiCudaTensor *projz, ColiCudaTensor *outp,
const float *x, float *out, const float *egh, const float *beta);
+ /* Overwrite an uploaded tensor in place with new bytes of the same shape and
+ * format. The expert tier swaps residents, and every expert of a model has the
+ * same geometry, so the victim's device buffers can take the newcomer without
+ * the cudaFree + cudaMalloc pair, which synchronises the device under the
+ * async expert groups. Optional in the DLL loader: 0 from a backend that
+ * predates it (or on any failure), and the caller frees and uploads as before. */
+ COLI_CUDA_DLLEXPORT int coli_cuda_tensor_overwrite(ColiCudaTensor *tensor, const void *weights, const float *scales);
#ifdef COLI_ANS
/* Experimental Linux-only GPU-resident entropy tier. The archive remains in
* VRAM and is decoded into per-device scratch immediately before a grouped
diff --cc c/backend_loader.c
index 4cb1e4e9,e45979dc..435ff528
--- a/c/backend_loader.c
+++ b/c/backend_loader.c
@@@ -99,7 -99,8 +99,8 @@@ typedef ColiCudaDn *(*fn_dn_create)(
typedef void (*fn_dn_free)(ColiCudaDn *d);
typedef int (*fn_dn_set_state)(ColiCudaDn *d, const float *ring, const float *rec);
typedef int (*fn_dn_get_state)(ColiCudaDn *d, float *ring, float *rec);
-typedef int (*fn_dn_step)(ColiCudaDn *d, ColiCudaTensor *proj, ColiCudaTensor *outp, const float *x, float *out, const float *egh, const float *beta);
+typedef int (*fn_dn_step)(ColiCudaDn *d, ColiCudaTensor *proj, ColiCudaTensor *projz, ColiCudaTensor *outp, const float *x, float *out, const float *egh, const float *beta);
+ typedef int (*fn_tensor_overwrite)(ColiCudaTensor *tensor, const void *weights, const float *scales);
typedef int (*fn_e8_set_grid)(const void *grid);
typedef int (*fn_fp8_set_lut)(const float *lut);
typedef int (*fn_matmul)(ColiCudaTensor **tensor, float *y, const float *x,
@@@ -1660,10 -1663,15 +1663,15 @@@ ColiCudaDn *coli_cuda_dn_create(int dev
void coli_cuda_dn_free(ColiCudaDn *d){ if(d && g_cuda.dn_free) g_cuda.dn_free(d); }
int coli_cuda_dn_set_state(ColiCudaDn *d, const float *ring, const float *rec){ return d && g_cuda.dn_set_state ? g_cuda.dn_set_state(d, ring, rec) : 0; }
int coli_cuda_dn_get_state(ColiCudaDn *d, float *ring, float *rec){ return d && g_cuda.dn_get_state ? g_cuda.dn_get_state(d, ring, rec) : 0; }
-int coli_cuda_dn_step(ColiCudaDn *d, ColiCudaTensor *proj, ColiCudaTensor *outp, const float *x, float *out, const float *egh, const float *beta){
- return d && g_cuda.dn_step ? g_cuda.dn_step(d, proj, outp, x, out, egh, beta) : 0;
+int coli_cuda_dn_step(ColiCudaDn *d, ColiCudaTensor *proj, ColiCudaTensor *projz, ColiCudaTensor *outp, const float *x, float *out, const float *egh, const float *beta){
+ return d && g_cuda.dn_step ? g_cuda.dn_step(d, proj, projz, outp, x, out, egh, beta) : 0;
}
+ int coli_cuda_tensor_overwrite(ColiCudaTensor *tensor, const void *weights, const float *scales){
+ if(!g_cuda.available || !g_cuda.tensor_overwrite) return 0; /* older DLL: the tier frees and uploads instead */
+ return g_cuda.tensor_overwrite(tensor, weights, scales);
+ }
+
int coli_cuda_e8_set_grid(const void *grid){
if(!g_cuda.available || !g_cuda.e8_set_grid) return 0; /* fmt=6 stays CPU-side */
return g_cuda.e8_set_grid(grid);
diff --cc c/qwen36.c
index 2c072df6,7786dc4b..504aa6e7
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@@ -4078,16 -3721,19 +4119,17 @@@ static float *step_ex(Model *m, const i
ids[s], c->vocab - 1);
exit(1);
}
- int vrow = (m->vis_map && pos_base + s < m->vis_map_len) ? m->vis_map[pos_base + s] : -1;
- if (vrow >= 0 && vrow < m->vis_rows_n) /* an image placeholder: the tower's row */
- memcpy(x + (int64_t)s*D, m->vis_rows + (int64_t)vrow*D, D*sizeof(float));
- else
- memcpy(x + (int64_t)s*D, m->embed + (int64_t)ids[s]*D, D*sizeof(float));
+ q36_embed_row(m, ids[s], pos_base + s, x + (int64_t)s*D);
}
+ tier_rebuild_evicted(m);
+ int chain_n = 0; /* layers the chain ran (a partial chain: the CPU runs the rest) */
#ifdef COLI_VULKAN
/* COLI_VK_CHAIN: the layers, the final norm and lm_head on the device; x comes
- * back only when the prefill read-out below needs every row */
+ * back only when the prefill read-out below needs every row. A partial chain
+ * brings back every row's residual after its layers: the CPU continues from there. */
float *chain_logit = NULL;
if (g_vk_chain) {
- int echo = 0;
+ int echo = 0, rows_only = 0;
#ifndef QWEN36_NO_MAIN
echo = g_echo_k > 0 && g_echo_id && S > 1;
#endif
@@@ -4717,9 -4123,8 +4759,10 @@@ static void generate(Model *m, const in
m->kv_len = 0;
for (int i = 0; i < np; i++) out[i] = prompt[i];
float *logit = step(m, prompt, np, 0);
+ qt_stats_mark(); /* [qtier] stats also report the hit rate from here on */
int len = np;
+ Q36Spec *sp = &g_q36_run_spec;
+ q36_spec_begin(m, sp, prompt, np); /* COLI_LOOKUP=1: drafts, every token still the argmax below */
for (int s = 0; s < n_new; s++) {
int best = 0; float bv = logit[0];
for (int i = 1; i < c->vocab; i++) if (logit[i] > bv) { bv = logit[i]; best = i; }