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

ci: build and ship qwen36, and gate it against a tiny oracle (B5 + B5b)

kreuzzelg9ae0c8a8c50b
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 321ab856..805bd5b4 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -190,7 +190,7 @@ jobs:
run: |
cd c
rc=0
- ENGINES="colibri inkling kimi_k3 olmoe"
+ ENGINES="colibri inkling kimi_k3 olmoe qwen36"
if [ "${{ matrix.v4 }}" = "1" ]; then ENGINES="$ENGINES deepseek-v4"; fi
for t in $ENGINES; do
echo "::group::$t"
@@ -317,6 +317,71 @@ jobs:
-Xcompiler=-Wall,-Wextra
echo "inkling CUDA syntax check passed"
+ qwen36-tiny-check:
+ name: Qwen3.6 tiny oracle (token-exact + ASan/UBSan)
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
+ with:
+ python-version: '3.12'
+ cache: pip
+ cache-dependency-path: c/tools/oracle-requirements.txt
+ - name: Install torch (CPU) + transformers
+ run: pip install -r c/tools/oracle-requirements.txt
+ - name: Tiny Qwen3.6-shaped fixture + full-hybrid oracle
+ run: |
+ cd c
+ # Same layout as the 35B model (10 x (3 x DeltaNet -> MoE, 1 x Attention
+ # -> MoE)) at toy dimensions, so the converter and the engine treat it
+ # exactly like the real one. --mode full exercises BOTH layer kinds;
+ # attention_only would leave the DeltaNet path untested.
+ # The reference comes from make_qwen36_tiny.py, not make_qwen36_oracle.py:
+ # the oracle encodes a text prompt through AutoTokenizer.from_pretrained(),
+ # and this fixture is synthetic -- weights, no tokenizer. The tiny script
+ # already has the model in memory and generates greedily from fixed ids.
+ # --ref-mode full leaves BOTH layer kinds active; the default
+ # attention_only replaces the 30 DeltaNet layers with identity, which is
+ # what Phase 1 computed and would leave three quarters of the engine
+ # untested.
+ python3 tools/make_qwen36_tiny.py --out qwen36_tiny --ref-mode full \
+ --emit-ref qwen36_tiny/ref_full.json
+ # ebits=8 keeps the expert quantization error far below anything that
+ # could flip a greedy argmax, so a mismatch means the engine changed,
+ # not the container.
+ python3 tools/convert_qwen36.py --model qwen36_tiny --out qwen36_tiny_c --ebits 8
+ - name: Token-exact against the oracle, at several cache capacities
+ run: |
+ cd c
+ make qwen36
+ # cap=1 evicts on every routed expert, which is where slot bookkeeping
+ # breaks; cap=8 never would. That is not hypothetical here: the
+ # in-flight eviction fallback this engine inherited was exactly such a
+ # bug, and it only shows when cap is smaller than the number of loads
+ # in flight. The engine exits non-zero on any token mismatch.
+ for cap in 1 2 8; do
+ echo "::group::cap=$cap"
+ SNAP=qwen36_tiny_c ./qwen36 "$cap" 8 qwen36_tiny/ref_full.json
+ echo "::endgroup::"
+ done
+ - name: Same run under ASan + UBSan
+ run: |
+ cd c
+ # Token-exactness alone would not have caught the config-driven heap
+ # overflows this engine shipped with: they do not necessarily change
+ # the output. The sanitizers are the half of this gate that watches
+ # memory, and PILOT=1 puts concurrent expert loads against the cache
+ # so the slot paths are exercised, not just walked past.
+ make clean >/dev/null 2>&1 || true
+ make qwen36 EXTRA_CFLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer -g"
+ ASAN_OPTIONS=detect_leaks=0 UBSAN_OPTIONS=halt_on_error=1 \
+ SNAP=qwen36_tiny_c PILOT=1 WIDE=2 \
+ ./qwen36 1 8 qwen36_tiny/ref_full.json
+ # TODO once A1/A2 land: add a malformed-container case here (config.json
+ # and qwen36_meta.json disagreeing on the layer count) and assert the
+ # engine REFUSES it. That is the case ASan caught, and a well-formed
+ # fixture cannot reproduce it -- this step guards the happy path only.
+
inkling-oracle:
name: Inkling oracle (token-exact vs transformers)
runs-on: ubuntu-latest
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index b7036e93..0c1579ed 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -62,10 +62,10 @@ jobs:
- name: Build engines
run: |
cd c
- for t in colibri inkling kimi_k3 olmoe; do
+ for t in colibri inkling kimi_k3 olmoe qwen36; do
make $t ${{ matrix.make_args }}
done
- ls -lh colibri${{ matrix.ext }} inkling${{ matrix.ext }} kimi_k3${{ matrix.ext }} olmoe${{ matrix.ext }}
+ ls -lh colibri${{ matrix.ext }} inkling${{ matrix.ext }} kimi_k3${{ matrix.ext }} olmoe${{ matrix.ext }} qwen36${{ matrix.ext }}
# DeepSeek V4 has its own Makefile and its own target name (`deepseek-v4`,
# producing `deepseek_v4`), so it never joined the loop above -- and #858 is
@@ -120,6 +120,10 @@ jobs:
cp c/inkling${{ matrix.ext }} dist/inkling${{ matrix.ext }}
cp c/kimi_k3${{ matrix.ext }} dist/kimi_k3${{ matrix.ext }}
cp c/olmoe${{ matrix.ext }} dist/olmoe${{ matrix.ext }}
+ # qwen36 -- plain name, like the others: engine_for() resolves "qwen" to
+ # a "qwen36" binary next to coli. Unconditional: the engine is portable C
+ # with no platform gate, so every archive can carry it.
+ cp c/qwen36${{ matrix.ext }} dist/qwen36${{ matrix.ext }}
# deepseek_v4 -- underscore, not the hyphen of the make target: engine_for()
# looks for "deepseek_v4" next to coli. Guarded, not unconditional, because
# the macos-arm64 archive has no such engine to ship (#858).
@@ -168,7 +172,7 @@ jobs:
# a green build, green tests, and an archive with no engine for the model the
# user actually had. Presence is not enough -- assert they are executable, and
# that the launcher's own resolver finds them where it looks (next to coli).
- SIBLINGS="inkling kimi_k3 olmoe"
+ SIBLINGS="inkling kimi_k3 olmoe qwen36"
# deepseek_v4 only where the engine builds (COLI_V4_SUPPORTED); asserting it
# unconditionally would fail the macos-arm64 archive for shipping an engine
# that platform cannot have.
diff --git a/c/qwen36.c b/c/qwen36.c
index 5d2bd738..3f228e1a 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -2267,12 +2267,14 @@ int main(int argc, char **argv) {
else fprintf(stderr, "[dump] cannot open %s\n", dp ? dp : "qwen36_logits.f32");
}
+ int ref_match = 0;
if (is_ref) {
int match = 0;
printf("\nReference: "); for (int i=np;i<nfull;i++) printf("%d ", full[i]);
printf("\nC engine : "); for (int i=np;i<nfull;i++) { printf("%d ", out[i]); if (out[i]==full[i]) match++; }
if (g_tok) { printf("Text : "); print_decoded(out, np, nfull); printf("\n"); }
printf("\nMatching tokens: %d/%d\n", match, n_new);
+ ref_match = match;
} else {
if (g_openai) {
emit_openai_result(out, np, n_new, g_stream);
@@ -2292,6 +2294,11 @@ int main(int argc, char **argv) {
(unsigned long long)m.hits, (unsigned long long)m.miss);
fprintf(stderr, "Speed: %.2f tok/s (%.1fs for %d tokens)\n", n_new/dt, dt, n_new);
free(buf); free(arena);
+ /* Oracle mode is a gate, not a report: a mismatch must fail the caller.
+ * inkling.c does the same (`return (match == ngen) ? 0 : 1;`) and its CI
+ * job relies on it — without this, tools/make_qwen36_oracle.py could be
+ * wired into a workflow that stays green through any regression. */
+ if (is_ref) return ref_match == n_new ? 0 : 1;
return 0;
}
#endif /* QWEN36_NO_MAIN */
diff --git a/c/tools/make_qwen36_tiny.py b/c/tools/make_qwen36_tiny.py
index 5336df02..6167fca9 100644
--- a/c/tools/make_qwen36_tiny.py
+++ b/c/tools/make_qwen36_tiny.py
@@ -70,7 +70,8 @@ class Zero(nn.Module):
def build(out: Path, hidden=64, n_layers=8, q_heads=4, kv_heads=2,
head_dim=16, rope_dim=8, n_experts=8, topk=2, inter=32,
- vocab=320, max_new=16, prompt_ids=None, emit_ref=None):
+ vocab=320, max_new=16, prompt_ids=None, emit_ref=None,
+ ref_mode="attention_only"):
ModelCls, ConfigCls = get_classes()
layer_types = ["full_attention" if i % 4 == 3 else "linear_attention"
for i in range(n_layers)]
@@ -113,14 +114,26 @@ def build(out: Path, hidden=64, n_layers=8, q_heads=4, kv_heads=2,
print(f"Tiny model saved at {out} (params ~ {sum(p.numel() for p in model.parameters())/1e6:.1f}M)")
if emit_ref is not None:
- # attention_only: replace DeltaNet layers (i%4!=3) with identity
- replaced = 0
- for i in range(n_layers):
- if i % 4 != 3:
- model.model.layers[i].self_attn = Zero()
- model.model.layers[i].mlp = Zero()
- replaced += 1
- print(f"attention_only: replaced {replaced} DeltaNet layers with identity")
+ # attention_only replaces the DeltaNet layers (i%4!=3) with identity,
+ # which is what the Phase-1 engine computed. The Phase-2 engine runs
+ # BOTH layer kinds, so a gate built on that reference would pass while
+ # never touching 30 of 40 layers -- hence ref_mode="full", which leaves
+ # the model whole.
+ #
+ # This lives here rather than in make_qwen36_oracle.py because that
+ # script encodes a text prompt through AutoTokenizer.from_pretrained(),
+ # and this fixture is synthetic: it has weights and no tokenizer. The
+ # oracle script stays the tool for real checkpoints.
+ if ref_mode == "attention_only":
+ replaced = 0
+ for i in range(n_layers):
+ if i % 4 != 3:
+ model.model.layers[i].self_attn = Zero()
+ model.model.layers[i].mlp = Zero()
+ replaced += 1
+ print(f"attention_only: replaced {replaced} DeltaNet layers with identity")
+ else:
+ print("full: both layer kinds active (Gated Attention + Gated DeltaNet)")
if prompt_ids is None:
prompt_ids = [1, 2, 3, 4, 5]
input_ids = torch.tensor([prompt_ids])
@@ -129,7 +142,7 @@ def build(out: Path, hidden=64, n_layers=8, q_heads=4, kv_heads=2,
use_cache=True)
full = out_ids[0].tolist()
payload = {"prompt_ids": prompt_ids, "full_ids": full,
- "mode": "attention_only", "model": "qwen36_tiny"}
+ "mode": ref_mode, "model": "qwen36_tiny"}
Path(emit_ref).write_text(json.dumps(payload, indent=2))
print(f"ref.json -> {emit_ref}")
print(f" prompt_ids={prompt_ids}")
@@ -141,6 +154,10 @@ def main():
ap.add_argument("--out", required=True, help="Output model dir")
ap.add_argument("--emit-ref", default="ref_qwen36.json",
help="Also emit this ref.json (attention_only). Set '' to skip.")
+ ap.add_argument("--ref-mode", choices=["attention_only", "full"],
+ default="attention_only",
+ help="full keeps both layer kinds (Phase-2 engine); "
+ "attention_only replaces DeltaNet with identity (Phase 1)")
ap.add_argument("--max-new", type=int, default=16)
ap.add_argument("--prompt-ids", default=None,
help="Comma-separated token ids for the prompt (default 1,2,3,4,5)")
@@ -151,7 +168,8 @@ def main():
prompt_ids = [int(x) for x in args.prompt_ids.split(",") if x.strip() != ""]
emit = args.emit_ref if args.emit_ref else None
- build(Path(args.out), max_new=args.max_new, prompt_ids=prompt_ids, emit_ref=emit)
+ build(Path(args.out), max_new=args.max_new, prompt_ids=prompt_ids, emit_ref=emit,
+ ref_mode=args.ref_mode)
if __name__ == "__main__":