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(qwen36): hand qt_init the resolved cap, not the auto sentinel

Jaapjan Tinbergen5e02d2bd0eda
diff --git a/c/qwen36.c b/c/qwen36.c
index 1313c051..fa900b0a 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -902,6 +902,24 @@ static void pilot_prefetch(Model *m, int lnext, const float *x, int S);
static void *pilot_worker(void *arg);
static void ensure_pilot_worker_started(Model *m);
static void slot_ensure_allocated(Model *m, Slot *s);
+/* The cap main() must hand to qt_init.
+ *
+ * model_init_range resolves the cap<=0 "auto" sentinel into its own local copy
+ * and writes the result to every layer's cache; main()'s variable keeps the
+ * sentinel. qt_init refuses the VRAM expert tier for any cap != n_experts
+ * outside fp8-stream mode (qwen36_tier.c:536), so passing the unresolved 0
+ * switched the tier off under COLI_CUDA=1 even when auto-sizing had picked
+ * every expert. Found by review on #1747, not by a run: the guard sits behind
+ * COLI_CUDA and the CPU build links the inline stub.
+ *
+ * Pure on purpose, same convention as qwen36_cap_for_ram below: no Model
+ * pointer, no globals, no I/O, so test_qwen36_cap_precedence.c can pin it
+ * without a container. */
+static int qwen36_resolved_cap(int cap, const LCache *cache, int n_layers) {
+ if (cap > 0 || !cache || n_layers < 1) return cap;
+ return cache[0].cap;
+}
+
static int qwen36_cap_for_ram(double resident_gb, double avail_gb, double ram_gb_override,
int hidden, int inter, int n_experts, int n_active_layers,
int is_int4, double *slot_gb_out, double *budget_gb_out);
@@ -5516,6 +5534,8 @@ int main(int argc, char **argv) {
}
if (expert_mixed && getenv("COLI_CUDA") && getenv("COLI_CUDA")[0] == '1')
fprintf(stderr, "[qwen36] COLI_CUDA=1 ignored: the VRAM expert tier does not take the mixed layout yet (one format per expert)\n");
+ /* The sentinel never leaves main() unresolved: see qwen36_resolved_cap. */
+ cap = qwen36_resolved_cap(cap, m.cache, m.c.n_layers);
if (!qq_active() && !expert_mixed && m.c.n_experts > 0 &&
qt_init(m.c.n_layers, m.c.n_experts, m.c.hidden, m.c.inter, cap, m.c.topk,
m.c.expert_gs, expert_is_int4)) {
diff --git a/c/tests/test_qwen36_cap_precedence.c b/c/tests/test_qwen36_cap_precedence.c
index 338f461a..8d543ec7 100644
--- a/c/tests/test_qwen36_cap_precedence.c
+++ b/c/tests/test_qwen36_cap_precedence.c
@@ -100,6 +100,34 @@ int main(void) {
CHECK(qwen36_cap_for_ram(0.0, 0.0, 0.0, HIDDEN, INTER, N_EXPERTS, 40, 0, NULL, NULL) >= 1);
}
+ /* qwen36_resolved_cap: the sentinel must never leave main() unresolved.
+ *
+ * model_init_range resolves cap<=0 into its own local copy and writes the
+ * result to every layer's cache; main()'s variable keeps the sentinel.
+ * qt_init refuses the VRAM expert tier for any cap != n_experts outside
+ * fp8-stream mode, so handing it the unresolved 0 switched the tier off
+ * under COLI_CUDA=1 even when auto-sizing had picked every expert. Found by
+ * review on #1747, not by a run: the guard lives behind COLI_CUDA and the
+ * CPU build links the inline stub, so no CPU-only test can observe it. */
+ {
+ LCache cache[2];
+ cache[0].cap = N_EXPERTS; cache[1].cap = N_EXPERTS;
+
+ /* the sentinel resolves to what model_init_range stored */
+ CHECK(qwen36_resolved_cap(0, cache, 2) == N_EXPERTS);
+ CHECK(qwen36_resolved_cap(-1, cache, 2) == N_EXPERTS);
+
+ /* an explicit cap is passed through untouched -- the byte-identical
+ * guarantee for existing invocations (#1747's first fixed decision) */
+ CHECK(qwen36_resolved_cap(4, cache, 2) == 4);
+ CHECK(qwen36_resolved_cap(N_EXPERTS, cache, 2) == N_EXPERTS);
+
+ /* degenerate: nothing to read back, hand the sentinel on unchanged
+ * rather than invent a value or dereference a null cache */
+ CHECK(qwen36_resolved_cap(0, NULL, 2) == 0);
+ CHECK(qwen36_resolved_cap(0, cache, 0) == 0);
+ }
+
printf(fails ? "test_qwen36_cap_precedence: FAIL (%d)\n" : "test_qwen36_cap_precedence: PASS\n", fails);
return fails != 0;
}