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 tier: name QT_MAX_ROWS, pin replica-block placement, int8-container docs

mfethe13e16e2c5fca9
diff --git a/c/qwen36.c b/c/qwen36.c
index 3e41d399..2f877d2b 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -2793,8 +2793,17 @@ static void tier_warmstart(Model *m, int expert_is_int4) {
}
qt_fill_wait();
free(wpl); free(wpe); free(planned);
- fprintf(stderr, "[qtier] warmstart (parallel): all %d experts in RAM (int8 only for non-residents), %d in VRAM -- %.1f s\n",
- cap_total, wn, now_s()-t0);
+ /* The parenthesis used to read "int8 only for non-residents", which was
+ * true only while the int8 copy of every resident was freed. Since #1341
+ * that free is int4-only: on an int8 container every resident keeps its
+ * weights, so the RSS saving the old line implied does not exist there.
+ * Say which container this is instead of promising a saving the reader
+ * will not see. */
+ fprintf(stderr, "[qtier] warmstart (parallel): all %d experts in RAM (%s), %d in VRAM -- %.1f s\n",
+ cap_total,
+ expert_is_int4 ? "int8 copy dropped for residents, kept for non-residents"
+ : "int8 container: all experts keep their weights in RAM",
+ wn, now_s()-t0);
}
int main(int argc, char **argv) {
diff --git a/c/qwen36_tier.c b/c/qwen36_tier.c
index 0aae56bb..8fc89408 100644
--- a/c/qwen36_tier.c
+++ b/c/qwen36_tier.c
@@ -13,6 +13,11 @@
#include "tier.h"
#define QT_MAX_DEV 8
+/* Max rows in one issued group = max topk. The stride of a device's input
+ * replica block, the width of every per-device row array and the topk clamp
+ * must all agree: #1339 was three of these disagreeing, each spelling the
+ * same literal 32 separately. Name it once. */
+#define QT_MAX_ROWS 32
#define QT_QCAP 48 /* upload queue depth (staging ~1.6 MB/entry) */
typedef struct {
@@ -49,8 +54,8 @@ static struct {
uint64_t hits[QT_MAX_DEV], miss, uploads, q_full_skips;
/* issue state of the (single) decode thread */
int is_cnt[QT_MAX_DEV];
- int is_k[QT_MAX_DEV][32];
- float *is_x; size_t is_x_floats; /* 32*D input replicas per device */
+ int is_k[QT_MAX_DEV][QT_MAX_ROWS];
+ float *is_x; size_t is_x_floats; /* QT_MAX_ROWS*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 */
@@ -479,7 +484,7 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,
fprintf(stderr,"[qtier] cap=%d != n_experts=%d -> tier disabled (needs full RAM residency)\n",cap,ne);
return 0;
}
- if(topk>32){ fprintf(stderr,"[qtier] topk>32 unsupported\n"); return 0; }
+ if(topk>QT_MAX_ROWS){ fprintf(stderr,"[qtier] topk>%d unsupported\n",QT_MAX_ROWS); return 0; }
memset(&G,0,sizeof G);
G.nl=nl; G.ne=ne; G.D=D; G.Ih=Ih; G.topk=topk;
/* Placement state is re-derived per init: the device fold-in below reads
@@ -683,7 +688,11 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,
G.dev[i], G.budget[i]/1073741824.0, G.budget[i]/G.exp_bytes,
trunk ? " after trunk" : "");
}
- G.is_x_floats=(size_t)G.ndev*32*D;
+ /* qt_issue strides each device's block by QT_MAX_ROWS*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). 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);
@@ -977,11 +986,11 @@ static void qt_lfru_tick_locked(void){
}
uint32_t qt_issue(int layer,const int *eids,int K,const float *x){
- if(!G.on||K>32) return 0;
+ if(!G.on||K>QT_MAX_ROWS) return 0;
uint32_t mask=0;
- ColiCudaTensor *tg[QT_MAX_DEV][32],*tu[QT_MAX_DEV][32],*td[QT_MAX_DEV][32];
- static int rows[32]={0};
- if(!rows[0]) for(int i=0;i<32;i++) rows[i]=1;
+ ColiCudaTensor *tg[QT_MAX_DEV][QT_MAX_ROWS],*tu[QT_MAX_DEV][QT_MAX_ROWS],*td[QT_MAX_DEV][QT_MAX_ROWS];
+ static int rows[QT_MAX_ROWS]={0};
+ if(!rows[0]) for(int i=0;i<QT_MAX_ROWS;i++) rows[i]=1;
for(int i=0;i<G.ndev;i++) G.is_cnt[i]=0;
pthread_mutex_lock(&G.mx);
@@ -1001,7 +1010,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*32*G.D; /* per-device input block */
+ float *xr=G.is_x + (size_t)di*QT_MAX_ROWS*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/test_qwen36_tier_multidev.c b/c/tests/test_qwen36_tier_multidev.c
index a4478bb6..0cfd2964 100644
--- a/c/tests/test_qwen36_tier_multidev.c
+++ b/c/tests/test_qwen36_tier_multidev.c
@@ -105,12 +105,32 @@ int main(void) {
}
check(inside, "blocks_stay_inside_the_replica_buffer");
+ /* In-bounds and disjoint are both necessary and both insufficient: a
+ * stride SMALLER than the row capacity keeps every block inside the
+ * allocation while sliding device 1's window down onto device 0's, and a
+ * single-row issue on device 1 is disjoint from device 0's under any
+ * stride at all. The two checks below pin the stride itself. */
+ check(sizeof G.is_k[0] / sizeof G.is_k[0][0] == QT_MAX_ROWS &&
+ G.is_x_floats == (size_t)G.ndev * (sizeof G.is_k[0] / sizeof G.is_k[0][0]) * (size_t)G.D,
+ "replica_block_stride_equals_the_per_device_row_capacity");
+
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");
+ /* Where, not just whether: device di's block starts at
+ * G.is_x + di*QT_MAX_ROWS*D. This is the one assertion that fails on
+ * the old 8*D stride even when device 1 carries a single row. */
+ int dev0 = records[base2].device == 0 ? base2 : base2 + 1;
+ int dev1 = dev0 == base2 ? base2 + 1 : base2;
+ check(records[dev0].device == 0 && records[dev1].device == 1,
+ "the mixed batch should issue once on device 0 and once on device 1");
+ check(records[dev0].x == G.is_x,
+ "device_0_block_starts_at_the_buffer_base");
+ check(records[dev1].x == G.is_x + (size_t)QT_MAX_ROWS * (size_t)G.D,
+ "device_1_block_starts_one_full_row_capacity_in");
}
float val[32]; for (int k = 0; k < 32; k++) val[k] = 1.0f;
diff --git a/docs/qwen36-cuda-tier.md b/docs/qwen36-cuda-tier.md
index a0f05c7b..e2c7bd00 100644
--- a/docs/qwen36-cuda-tier.md
+++ b/docs/qwen36-cuda-tier.md
@@ -28,10 +28,14 @@ more GPUs and computed there through the existing shared CUDA backend
groups on all devices (`coli_cuda_expert_group_issue/take`); VRAM misses
fall back to the CPU int8 path and overlap with the in-flight groups, as
does the shared expert. Placement never changes routing or precision.
-- **Memory:** the warmstart frees the RAM int8 copies of VRAM-resident
- experts (rematerialized from the packed int4 copy on LFRU eviction; no
- container access). Peak RSS for the 35B int4 container: ~29 GB with two
- 8 GB GPUs.
+- **Memory:** on an **int4 container** the warmstart frees the RAM int8 copies
+ of VRAM-resident experts (rematerialized from the packed int4 copy on LFRU
+ eviction; no container access). Peak RSS for the 35B int4 container: ~29 GB
+ with two 8 GB GPUs. The RSS saving is a property of packed containers only:
+ on an **int8 container** there is no second copy to rematerialize from, so
+ since #1341 nothing is freed and every resident keeps its full RAM weights —
+ such a run gets the tier's VRAM speed at full residency cost, and the 29 GB
+ figure below does not apply to it.
## Usage
@@ -130,6 +134,9 @@ CPU fallback. Known, not yet addressed.
| peak RSS | 40 GB | **29 GB** |
| reference: Ollama q4_K_M, same box | 7.5 | 10.5 |
+All figures above are int4-container measurements; the peak-RSS row in
+particular has no int8 analogue (see **Memory**).
+
CPU-only baseline of this engine before the tier: 0.35 tok/s.
Numerics: logits cosine vs the f32 CPU reference 0.9992 (dense int8 on),
bit-identical GPU-vs-CPU on the same container (cosine 1.0000001).