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: the Qwen-Image tiny oracle, the VAE, the serve protocol, under ASan too

JustVuggc330fbedc615
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 1b20f08b..fe6a33e3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1000,6 +1000,55 @@ jobs:
python3 tools/make_edge_tiny_tokenizer.py --vocab-size 64 qwen38_tiny_fp8
QWEN38_TINY=qwen38_tiny_fp8 python3 -m unittest -v tests.test_qwen38_brio
+ qwenimage-tiny-check:
+ name: Qwen-Image tiny oracle (text encoder, DiT, scheduler, VAE vs diffusers)
+ 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/requirements-qwenimage-tiny.txt
+ - name: Install the pinned diffusers reference
+ run: pip install -r c/tools/requirements-qwenimage-tiny.txt
+ - name: Every stage against diffusers, from the reference input of that stage
+ run: |
+ cd c
+ # A tiny random pipeline in the real checkpoint's layout (bf16 text
+ # encoder and DiT, f32 VAE, non-square 64x96, z_dim 16) and its f32
+ # reference. With COLI_IMG_BITS=16 the engine computes on exactly those
+ # weights, so each stage must match to summation order; the engine exits
+ # non-zero on a mismatch, and the RGBA must agree to 1 in 1000 bytes.
+ python3 tools/make_qwenimage_tiny.py qwenimage_tiny
+ make qwenimage test_qi_gemm tests/test_qwenimage_vae
+ ./test_qi_gemm
+ COLI_IMG_BITS=16 COLI_IMG_ACT8=0 ./qwenimage --model qwenimage_tiny --ref qwenimage_tiny/ref
+ - name: "The VAE alone: tiny decoder at two latent shapes"
+ run: |
+ cd c
+ python3 tools/make_qwenimage_vae_tiny.py qwenimage_vae_tiny
+ tests/test_qwenimage_vae qwenimage_vae_tiny
+ - name: The shipping defaults (int8 weights) end to end, and the serve protocol
+ run: |
+ cd c
+ ./qwenimage --model qwenimage_tiny --prompt "a red fox in the snow" \
+ --width 256 --height 256 --steps 2 --seed 1 --out tiny.png
+ python3 -c "import sys; sys.path.insert(0, '.'); import image_engine as e; w, h, c, px = e.decode_png(open('tiny.png', 'rb').read()); assert (w, h, c) == (256, 256, 4), (w, h, c)"
+ QWENIMAGE_TINY=qwenimage_tiny python3 -m unittest -v tests.test_qwenimage_engine_serve
+ - name: The oracle and the serve tests again under ASan and UBSan
+ run: |
+ cd c
+ make -B qwenimage EXTRA_CFLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer -g"
+ export ASAN_OPTIONS=detect_leaks=0 UBSAN_OPTIONS=print_stacktrace=1 OMP_NUM_THREADS=2
+ COLI_IMG_BITS=16 COLI_IMG_ACT8=0 ./qwenimage --model qwenimage_tiny --ref qwenimage_tiny/ref > qwenimage-san.log 2>&1 || true
+ QWENIMAGE_TINY=qwenimage_tiny python3 -m unittest tests.test_qwenimage_engine_serve >> qwenimage-san.log 2>&1 || true
+ if grep -qE "ERROR: AddressSanitizer|runtime error:" qwenimage-san.log; then
+ cat qwenimage-san.log
+ exit 1
+ fi
+ grep -q "all stages within tolerance" qwenimage-san.log
+
inkling-oracle:
name: Inkling oracle (token-exact vs transformers)
runs-on: ubuntu-latest
diff --git a/c/tests/test_qwenimage_engine_serve.py b/c/tests/test_qwenimage_engine_serve.py
new file mode 100644
index 00000000..93e39fb1
--- /dev/null
+++ b/c/tests/test_qwenimage_engine_serve.py
@@ -0,0 +1,80 @@
+"""The real qwenimage engine behind image_engine.ImageEngine, on the tiny fixture.
+
+The gateway and TUI tests run against tools/qwenimage_stub.py; this one holds
+the C engine to the same protocol: frames of the right size, the same seed giving
+the same bytes, a refused size or a CANCEL leaving the engine ready for the next
+request, and EOF as a clean exit.
+
+Needs QWENIMAGE_TINY (a directory written by tools/make_qwenimage_tiny.py) and
+the engine built (make qwenimage); skipped otherwise.
+ QWENIMAGE_TINY=/path/tiny python3 -m unittest -v tests.test_qwenimage_engine_serve"""
+import os
+import sys
+import unittest
+from pathlib import Path
+
+HERE = Path(__file__).resolve().parent.parent
+sys.path.insert(0, str(HERE))
+import image_engine # noqa: E402
+
+TINY = os.environ.get("QWENIMAGE_TINY")
+ENGINE = HERE / ("qwenimage.exe" if sys.platform == "win32" else "qwenimage")
+
+
[email protected](TINY and ENGINE.exists(), "needs QWENIMAGE_TINY and a built qwenimage")
+class RealEngineServe(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.logs = []
+ cls.engine = image_engine.ImageEngine(ENGINE, TINY, load_timeout=300,
+ on_log=cls.logs.append, on_stderr=cls.logs.append)
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.engine.close()
+
+ def generate(self, seed, width=256, height=256, **kw):
+ return self.engine.generate("a red fox in the snow", width, height, 2, seed, **kw)
+
+ def test_image_frame_and_determinism(self):
+ first = self.generate(7)
+ self.assertEqual((first["width"], first["height"], first["channels"]), (256, 256, 4))
+ self.assertEqual(len(first["rgba"]), 256 * 256 * 4)
+ self.assertEqual(first["seed"], 7)
+ self.assertEqual(self.generate(7)["rgba"], first["rgba"], "same seed, different bytes")
+ self.assertNotEqual(self.generate(8)["rgba"], first["rgba"], "the seed changed nothing")
+
+ def test_non_square_sizes(self):
+ wide = self.generate(3, width=320, height=256)
+ self.assertEqual((wide["width"], wide["height"]), (320, 256))
+ self.assertEqual(len(wide["rgba"]), 320 * 256 * 4)
+
+ def test_refused_size_keeps_the_engine(self):
+ with self.assertRaises(image_engine.ImageEngineError):
+ self.generate(1, width=300) # not a multiple of 32
+ self.assertEqual(len(self.generate(1)["rgba"]), 256 * 256 * 4)
+
+ def test_cancel_keeps_the_engine(self):
+ with self.assertRaises(image_engine.ImageCancelled):
+ self.generate(2, cancelled=lambda: True)
+ self.assertEqual(len(self.generate(2)["rgba"]), 256 * 256 * 4)
+
+ def test_progress_counts_completed_steps(self):
+ seen = []
+ self.generate(4, on_progress=lambda frame: seen.append((frame.get("stage"), frame.get("step"))))
+ self.assertIn(("encode", 0), seen)
+ self.assertIn(("denoise", 0), seen)
+ self.assertIn(("decode", 2), seen)
+
+
[email protected](TINY and ENGINE.exists(), "needs QWENIMAGE_TINY and a built qwenimage")
+class RealEngineExit(unittest.TestCase):
+ def test_eof_is_a_clean_exit(self):
+ engine = image_engine.ImageEngine(ENGINE, TINY, load_timeout=300, on_log=lambda m: None,
+ on_stderr=lambda m: None)
+ engine.close()
+ self.assertEqual(engine.process.returncode, 0)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/c/tools/requirements-qwenimage-tiny.txt b/c/tools/requirements-qwenimage-tiny.txt
new file mode 100644
index 00000000..2a788bf0
--- /dev/null
+++ b/c/tools/requirements-qwenimage-tiny.txt
@@ -0,0 +1,13 @@
+# Pinned dependencies for the Qwen-Image-2.1 tiny oracle (tools/make_qwenimage_tiny.py,
+# tools/make_qwenimage_vae_tiny.py). QwenImage21Pipeline is not in a diffusers
+# release yet, so diffusers is pinned to the commit the reference was built with.
+# No torchvision: the fixture builds the processor without the video part, which
+# text-to-image never calls.
+--extra-index-url https://download.pytorch.org/whl/cpu
+torch==2.14.0+cpu; sys_platform != "darwin"
+torch==2.14.0; sys_platform == "darwin"
+safetensors==0.8.0
+transformers==5.17.0
+numpy
+pillow
+diffusers @ git+https://github.com/huggingface/diffusers@80c7ed262aeffbeb43ef13ae04baeb9b84515a69