Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
Merge branch 'dev' into feat/windows-xdna-shared-expert-lane
Kenneth-Javier Ortigozadb9bd4b7602f
diff --cc c/tests/test_backend_loader.pyindex 6adcc262,af222d62..ae103898--- a/c/tests/test_backend_loader.py+++ b/c/tests/test_backend_loader.py@@@ -2424,404 -2468,107 +2468,507 @@@ class LoaderBackendSelectionTest(unitteself.assertEqual(result.returncode, 2)+++# ---------------------------------------------------------------------------+# W2-N7-I1: the optional XDNA helper binding contract.+#+# c/backend_xdna.c is the C-side loader for an OPTIONAL native helper DLL. It+# must never require XRT: the helper owns XRT, the host owns only the binding.+# These tests compile the production loader in place from c/ (never a copy) and+# drive it against synthetic helpers built with the same MinGW gcc the loader+# tests already require. Nothing here needs XRT, an NPU, or coli_xdna.dll.+#+# The contract under test is the BINDING, not the device: a successful bind+# means HELPER_ABI_AVAILABLE and nothing more.+# ---------------------------------------------------------------------------++_XDNA_HELPER_BASENAME = "coli_xdna.dll"++# Synthetic helper. Never links XRT; -D switches select the defect under test.+_XDNA_FAKE_HELPER_C = r'''+#include <windows.h>++#ifndef FAKE_ABI_VERSION+#define FAKE_ABI_VERSION 2u+#endif++__declspec(dllexport) unsigned int coli_xdna_helper_abi_version(void){+ return (unsigned int)FAKE_ABI_VERSION;+}++/* ABI generation 2 requires the whole execution set, not just a handshake. None+ * of these does anything: the contract under test is the BINDING. */+__declspec(dllexport) int coli_xdna_helper_open(const char *x, const char *i,+ unsigned m, unsigned k, unsigned n){+ (void)x;(void)i;(void)m;(void)k;(void)n; return 0;+}+__declspec(dllexport) int coli_xdna_helper_wrap_weight(void *p, unsigned long long b){+ (void)p;(void)b; return 0;+}+#ifndef FAKE_OMIT_EXECUTE+__declspec(dllexport) int coli_xdna_helper_execute(const void *a, unsigned long long ab,+ void *c, unsigned long long cb){+ (void)a;(void)ab;(void)c;(void)cb; return 0;+}+#endif+__declspec(dllexport) int coli_xdna_helper_release_weight(void){ return 0; }+__declspec(dllexport) const char *coli_xdna_helper_last_error(void){ return ""; }++#ifndef FAKE_OMIT_SHUTDOWN+__declspec(dllexport) void coli_xdna_helper_shutdown(void){ }+#endif++#ifdef FAKE_NEEDS_MISSING_DEP+/* Pulls in a dependency the test deletes, so LoadLibrary fails with 126+ * (ERROR_MOD_NOT_FOUND) instead of the file being absent outright. */+__declspec(dllimport) int coli_xdna_fake_dep_probe(void);+__declspec(dllexport) int coli_xdna_helper_touch_dep(void){+ return coli_xdna_fake_dep_probe();+}+#endif++BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, LPVOID reserved){+ (void)h; (void)reason; (void)reserved; return TRUE;+}+'''++_XDNA_FAKE_DEP_C = r'''+#include <windows.h>+__declspec(dllexport) int coli_xdna_fake_dep_probe(void){ return 7; }+BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, LPVOID reserved){+ (void)h; (void)reason; (void)reserved; return TRUE;+}+'''++# Harness: compiles the REAL c/backend_xdna.c and drives its test seams.+_XDNA_HARNESS_C = r'''+#include <stdio.h>+#include <string.h>+#include <stdlib.h>+#include "backend_xdna.c"++static void report(const char *tag){+ ColiXdnaBinding s = coli_xdna_binding();+ printf("%s=%s attempts=%d bound=%d\n", tag, coli_xdna_binding_text(s),+ coli_xdna_test_load_attempts(), coli_xdna_test_entry_points_bound());+}++int main(int argc, char **argv){+ const char *mode = argc > 1 ? argv[1] : "";+ const char *path = argc > 2 ? argv[2] : NULL;++ if(path && *path) coli_xdna_test_set_helper_path(path);++ if(!strcmp(mode, "probe")){+ report("state");+ return 0;+ }+ if(!strcmp(mode, "repeat")){+ int n = argc > 3 ? atoi(argv[3]) : 5;+ for(int i = 0; i < n; i++) (void)coli_xdna_binding();+ report("state");+ return 0;+ }+ if(!strcmp(mode, "shutdown_after_probe")){+ report("before");+ coli_xdna_shutdown();+ printf("shutdown_ok=1\n");+ coli_xdna_shutdown(); /* repeated shutdown must be safe */+ printf("second_shutdown_ok=1\n");+ return 0;+ }+ if(!strcmp(mode, "shutdown_without_probe")){+ coli_xdna_shutdown();+ printf("shutdown_ok=1\n");+ printf("state=%s\n", coli_xdna_binding_text(coli_xdna_test_state()));+ return 0;+ }+ if(!strcmp(mode, "callable")){+ /* A rejected helper must leave NO callable entry point behind. */+ report("state");+ printf("callable=%d\n", coli_xdna_test_entry_points_bound());+ return 0;+ }+ fprintf(stderr, "unknown_mode=%s\n", mode);+ return 2;+}+'''+++class XdnaLoaderOwnerTest(unittest.TestCase):+ """The optional XDNA loader must exist and declare its binding contract.++ This owner needs no toolchain: it is the contract that c/backend_xdna.{c,h}+ are the C-side owners of the optional helper boundary, and that the boundary+ is versioned and all-or-nothing.+ """++ def test_loader_owner_files_exist(self):+ for name in ("backend_xdna.h", "backend_xdna.c"):+ with self.subTest(source=name):+ self.assertTrue((HERE / name).is_file(),+ "c/%s is the C-side XDNA owner and must exist" % name)++ def test_header_declares_the_binding_contract(self):+ hdr = (HERE / "backend_xdna.h").read_text(encoding="utf-8", errors="replace")+ for token in ("COLI_XDNA_ABI_VERSION", "ColiXdnaBinding",+ "COLI_XDNA_UNPROBED", "COLI_XDNA_AVAILABLE",+ "COLI_XDNA_ABSENT", "COLI_XDNA_LOAD_FAILED",+ "COLI_XDNA_ABI_INCOMPATIBLE", "COLI_XDNA_SYMBOL_INCOMPLETE",+ "coli_xdna_binding", "coli_xdna_shutdown"):+ with self.subTest(token=token):+ self.assertIn(token, hdr)+++class XdnaOptionalBindingTest(unittest.TestCase):+ """Does the optional XDNA helper bind all-or-nothing, and stay optional?++ Every case runs the production c/backend_xdna.c. The helper under test is a+ synthetic DLL with no XRT in it; the point is the loader's verdict, which is+ reached long before any real runtime would be.+ """++ tmp = None++ @classmethod+ def setUpClass(cls):+ reason = _fixture_toolchain_skip()+ if reason:+ raise unittest.SkipTest(reason)+ cls.tmp = tempfile.TemporaryDirectory(prefix="coli xdna bind ")+ root = Path(cls.tmp.name)+ cls.root = root++ def gcc(args, what):+ cmd = ["gcc"] + args+ proc = subprocess.run(cmd, text=True, errors="replace",+ capture_output=True, timeout=300)+ if proc.returncode != 0:+ raise FixtureBuildError(+ "%s failed (rc=%d)\ncommand: %s\nstdout:\n%s\nstderr:\n%s"+ % (what, proc.returncode, " ".join(cmd), proc.stdout, proc.stderr))+ return proc++ cls._gcc = staticmethod(gcc)++ helper_src = root / "fake_helper.c"+ helper_src.write_text(_XDNA_FAKE_HELPER_C, encoding="ascii")+ dep_src = root / "fake_dep.c"+ dep_src.write_text(_XDNA_FAKE_DEP_C, encoding="ascii")+ harness_src = root / "xdna_harness.c"+ harness_src.write_text(_XDNA_HARNESS_C, encoding="ascii")++ # Good helper: current ABI, complete entry points.+ cls.good = root / "good" / _XDNA_HELPER_BASENAME+ cls.good.parent.mkdir(parents=True, exist_ok=True)+ gcc(["-O0", "-shared", str(helper_src), "-o", str(cls.good)],+ "building the good synthetic helper")++ # Wrong ABI version.+ cls.bad_abi = root / "bad_abi" / _XDNA_HELPER_BASENAME+ cls.bad_abi.parent.mkdir(parents=True, exist_ok=True)+ gcc(["-O0", "-shared", "-DFAKE_ABI_VERSION=999u", str(helper_src),+ "-o", str(cls.bad_abi)], "building the wrong-ABI synthetic helper")++ # The previous ABI generation. W2-N7-I5 bumped the host to generation 2,+ # which is a deliberate compatibility break: a generation-1 helper+ # exports two of the seven entry points now required and cannot execute+ # anything, so it must be refused outright rather than partially bound.+ cls.abi1 = root / "abi1" / _XDNA_HELPER_BASENAME+ cls.abi1.parent.mkdir(parents=True, exist_ok=True)+ gcc(["-O0", "-shared", "-DFAKE_ABI_VERSION=1u", str(helper_src),+ "-o", str(cls.abi1)], "building the previous-generation synthetic helper")++ # Missing a required entry point.+ cls.incomplete = root / "incomplete" / _XDNA_HELPER_BASENAME+ cls.incomplete.parent.mkdir(parents=True, exist_ok=True)+ gcc(["-O0", "-shared", "-DFAKE_OMIT_SHUTDOWN", str(helper_src),+ "-o", str(cls.incomplete)], "building the incomplete synthetic helper")++ # Missing one of the entry points ABI 2 introduced. Binding is+ # all-or-nothing across the WHOLE set, not just the parts it inherited.+ cls.no_execute = root / "no_execute" / _XDNA_HELPER_BASENAME+ cls.no_execute.parent.mkdir(parents=True, exist_ok=True)+ gcc(["-O0", "-shared", "-DFAKE_OMIT_EXECUTE", str(helper_src),+ "-o", str(cls.no_execute)], "building the execute-less synthetic helper")++ # Present but unloadable: imports a dependency that is then deleted.+ cls.unloadable = root / "unloadable" / _XDNA_HELPER_BASENAME+ cls.unloadable.parent.mkdir(parents=True, exist_ok=True)+ dep_dll = cls.unloadable.parent / "coli_xdna_fake_dep.dll"+ gcc(["-O0", "-shared", str(dep_src), "-o", str(dep_dll)],+ "building the synthetic helper dependency")+ gcc(["-O0", "-shared", "-DFAKE_NEEDS_MISSING_DEP", str(helper_src),+ "-o", str(cls.unloadable), str(dep_dll)],+ "building the dependency-bound synthetic helper")+ dep_dll.unlink() # now the helper cannot load++ cls.absent = root / "empty" / _XDNA_HELPER_BASENAME # never created+ cls.absent.parent.mkdir(parents=True, exist_ok=True)++ cls.harness = root / "xdna_harness.exe"+ # c/compat.h:80 makes -D_FILE_OFFSET_BITS=64 mandatory on Windows, and the+ # project CFLAGS always carry it. Compiling the loader the way the build+ # actually compiles it keeps this owner honest rather than lenient.+ gcc(["-O0", "-D_FILE_OFFSET_BITS=64", "-I", str(HERE), str(harness_src),+ "-o", str(cls.harness)],+ "building the XDNA loader harness")++ @classmethod+ def tearDownClass(cls):+ if cls.tmp is not None:+ cls.tmp.cleanup()+ cls.tmp = None++ def _run(self, mode, path, *extra):+ cmd = [str(self.harness), mode, str(path)] + [str(a) for a in extra]+ proc = subprocess.run(cmd, text=True, errors="replace",+ capture_output=True, timeout=120)+ self.assertEqual(proc.returncode, 0,+ "harness failed: %s\n%s\n%s"+ % (" ".join(cmd), proc.stdout, proc.stderr))+ return proc.stdout++ def _state(self, out, key="state"):+ for line in out.splitlines():+ if line.startswith(key + "="):+ return line.split("=", 1)[1].split(" ")[0]+ self.fail("no %s= line in harness output:\n%s" % (key, out))++ # -- the load-bearing case: absence is not a failure --------------------+ def test_absent_helper_is_not_fatal(self):+ out = self._run("probe", self.absent)+ self.assertEqual(self._state(out), "ABSENT")++ def test_good_helper_binds(self):+ out = self._run("probe", self.good)+ self.assertEqual(self._state(out), "AVAILABLE")++ def test_wrong_abi_is_rejected(self):+ out = self._run("probe", self.bad_abi)+ self.assertEqual(self._state(out), "ABI_INCOMPATIBLE")++ def test_previous_abi_generation_is_rejected(self):+ # The I1 -> I5 generation bump is a compatibility break by design. A+ # stale generation-1 helper must never become callable under the+ # generation-2 contract, however many symbol names it happens to share.+ out = self._run("probe", self.abi1)+ self.assertEqual(self._state(out), "ABI_INCOMPATIBLE")+ self.assertIn("bound=0", out)++ def test_missing_execute_entry_point_is_rejected(self):+ # All-or-nothing applies to the entry points ABI 2 added, not only to+ # the ones it inherited from ABI 1.+ out = self._run("probe", self.no_execute)+ self.assertEqual(self._state(out), "SYMBOL_INCOMPLETE")+ self.assertIn("bound=0", out)++ def test_missing_required_entry_point_is_rejected(self):+ out = self._run("probe", self.incomplete)+ self.assertEqual(self._state(out), "SYMBOL_INCOMPLETE")++ def test_unloadable_helper_is_rejected_not_fatal(self):+ out = self._run("probe", self.unloadable)+ self.assertEqual(self._state(out), "LOAD_FAILED")++ # -- all-or-nothing ----------------------------------------------------+ def test_rejected_helper_leaves_nothing_callable(self):+ for name, path in (("bad_abi", self.bad_abi),+ ("incomplete", self.incomplete),+ ("unloadable", self.unloadable),+ ("absent", self.absent)):+ with self.subTest(helper=name):+ out = self._run("callable", path)+ self.assertEqual(self._state(out, "callable"), "0")++ def test_bound_helper_reports_its_entry_points(self):+ out = self._run("callable", self.good)+ self.assertEqual(self._state(out, "callable"), "1")++ # -- sticky verdict ----------------------------------------------------+ def test_repeated_probe_loads_at_most_once(self):+ for name, path in (("good", self.good), ("absent", self.absent),+ ("bad_abi", self.bad_abi)):+ with self.subTest(helper=name):+ out = self._run("repeat", path, 8)+ # attempts is reported on the same line as state=+ line = [l for l in out.splitlines() if l.startswith("state=")][0]+ attempts = int(line.split("attempts=")[1].split(" ")[0])+ self.assertLessEqual(attempts, 1,+ "loader re-probed %d times: %s" % (attempts, out))++ # -- lifetime ----------------------------------------------------------+ def test_shutdown_after_bind_is_safe_and_repeatable(self):+ out = self._run("shutdown_after_probe", self.good)+ self.assertEqual(self._state(out, "before"), "AVAILABLE")+ self.assertIn("shutdown_ok=1", out)+ self.assertIn("second_shutdown_ok=1", out)++ def test_shutdown_without_probe_is_safe(self):+ out = self._run("shutdown_without_probe", self.absent)+ self.assertIn("shutdown_ok=1", out)+ self.assertEqual(self._state(out), "UNPROBED")++ def test_shutdown_after_failed_bind_is_safe(self):+ out = self._run("shutdown_after_probe", self.bad_abi)+ self.assertEqual(self._state(out, "before"), "ABI_INCOMPATIBLE")+ self.assertIn("second_shutdown_ok=1", out)+++class XdnaDefaultBuildIndependenceTest(unittest.TestCase):+ """Does the ordinary host stay free of XRT and of the helper?++ c/backend_xdna.c is the only C-side XDNA owner and it must compile with no+ XRT header available. The default host must import neither XRT nor the+ optional helper.+ """++ @classmethod+ def setUpClass(cls):+ reason = _fixture_toolchain_skip()+ if reason:+ raise unittest.SkipTest(reason)++ def test_loader_source_names_no_xrt_header(self):+ src = (HERE / "backend_xdna.c").read_text(encoding="utf-8", errors="replace")+ hdr = (HERE / "backend_xdna.h").read_text(encoding="utf-8", errors="replace")+ for name, text in (("backend_xdna.c", src), ("backend_xdna.h", hdr)):+ with self.subTest(source=name):+ self.assertNotIn("xrt/", text,+ "%s must not include any XRT header" % name)+ self.assertNotIn("xrt_", text.replace("coli_xdna_", ""),+ "%s must not name an XRT symbol" % name)++ def test_loader_compiles_without_any_xrt_include_path(self):+ with tempfile.TemporaryDirectory(prefix="coli xdna nodep ") as tmp:+ obj = Path(tmp) / "backend_xdna.o"+ cmd = ["gcc", "-O0", "-D_FILE_OFFSET_BITS=64",+ "-c", str(HERE / "backend_xdna.c"), "-o", str(obj)]+ proc = subprocess.run(cmd, text=True, errors="replace",+ capture_output=True, timeout=300)+ self.assertEqual(proc.returncode, 0,+ "the C-side loader must compile with no XRT present:\n%s\n%s"+ % (proc.stdout, proc.stderr))+ self.assertTrue(obj.is_file())++ def test_default_host_imports_neither_xrt_nor_helper(self):+ exe = HERE / "colibri.exe"+ if not exe.is_file():+ raise unittest.SkipTest("c/colibri.exe not built here")+ proc = subprocess.run(["objdump", "-p", str(exe)], text=True,+ errors="replace", capture_output=True, timeout=300)+ self.assertEqual(proc.returncode, 0, proc.stderr)+ imports = [l.split()[-1].lower() for l in proc.stdout.splitlines()+ if "DLL Name:" in l]+ for bad in ("xrt_coreutil.dll", "xrt_core.dll", "xrt_coreutil_static.dll",+ _XDNA_HELPER_BASENAME):+ self.assertNotIn(bad, imports,+ "default host must not import %s (imports: %s)"+ % (bad, imports))++++ class LoaderPreInitAvailabilityTest(unittest.TestCase):+ """The device count qwen36's tier reads BEFORE it calls coli_cuda_init.++ qwen36_tier.c asks how many devices are usable so it knows which indices to+ hand to coli_cuda_init, so the answer cannot depend on that init having+ happened. Gating the wrapper on g_cuda.available answered 0 on every+ Windows host and the tier silently took the CPU path unless COLI_GPUS was+ set (#1577). Nothing here needs a GPU: the backend is the generated stub.+ """++ fixture = None++ @classmethod+ def setUpClass(cls):+ reason = _fixture_toolchain_skip()+ if reason:+ raise unittest.SkipTest(reason)+ cls.fixture = _StubFixture()++ @classmethod+ def tearDownClass(cls):+ if cls.fixture is not None:+ cls.fixture.cleanup()+ cls.fixture = None++ def setUp(self):+ self.cases = tempfile.TemporaryDirectory(prefix="coli loader probe ")+ self.addCleanup(self.cases.cleanup)++ def _probe(self, name, **kwargs):+ """Run the harness with the stub backend's own dependency present.++ The generated backend imports its marker from amdhip64_7.dll by+ basename, and a CUDA host loads the DLL from the engine's own directory,+ so the dependency has to sit beside it for the load to succeed at all+ (otherwise Windows error 126, which is a fixture setup mistake rather+ than a loader behaviour). The HIP control keeps its sandbox untouched:+ there the loader refuses on COLI_HIP_RUNTIME_DIR before any load.+ """+ if kwargs.get("vendor") == "cuda" and kwargs.get("with_backend", True):+ kwargs.setdefault("extra_files", (self.fixture.runtime_a,))+ return self.fixture.run_harness(Path(self.cases.name) / name, "NONE",+ **kwargs)++ @staticmethod+ def _detail(proc):+ return "\nstdout:\n%s\nstderr:\n%s" % (proc.stdout, proc.stderr)++ def test_the_probe_loads_the_dll_it_reports_on(self):+ """No init yet, backend present: the count comes from the DLL itself."""+ proc, out = self._probe("probe_present", vendor="cuda")+ detail = self._detail(proc)+ self.assertEqual(proc.returncode, 0, "harness failed" + detail)+ self.assertEqual(out.get("backend_loaded_before_probe"), "0",+ "the probe was measured with the DLL already loaded"+ + detail)+ self.assertEqual(out.get("available_before_init"),+ str(_AVAILABLE_PROBE),+ "the pre-init count did not come from the DLL" + detail)+ self.assertEqual(out.get("backend_loaded_after_probe"), "1",+ "the probe never loaded the backend DLL" + detail)+ # The harness still reaches the normal init afterwards: loading on+ # demand must not consume the one load the process is allowed.+ self.assertEqual(out.get("loader_init"), "1", detail)++ def test_the_probe_is_zero_when_no_backend_is_present(self):+ """The miss path stays a miss: 0 devices, nothing loaded, no crash."""+ proc, out = self._probe("probe_absent", vendor="cuda",+ with_backend=False)+ detail = self._detail(proc)+ self.assertEqual(proc.returncode, 0, "harness failed" + detail)+ self.assertEqual(out.get("available_before_init"), "0", detail)+ self.assertEqual(out.get("backend_loaded_after_probe"), "0", detail)+ self.assertEqual(out.get("backend_loaded"), "0", detail)+ self.assertEqual(out.get("loader_init"), "0", detail)++ def test_the_probe_falls_back_for_a_dll_without_the_export(self):+ """An older DLL answers through device_count(), not through a crash."""+ f = self.fixture+ proc, out = self._probe("probe_older_dll", vendor="cuda",+ backend_override=f.cuda_backend_old)+ detail = self._detail(proc)+ self.assertEqual(proc.returncode, 0, "harness failed" + detail)+ self.assertEqual(out.get("backend_loaded_after_probe"), "1", detail)+ self.assertEqual(out.get("available_before_init"),+ str(_DEVICE_COUNT_PROBE),+ "the fallback did not reach device_count()" + detail)++ def test_the_probe_keeps_the_hip_refusal_before_init(self):+ """A HIP host still refuses without a runtime directory, before init.++ The count is asked before init on every host, so the HIP control proves+ the earlier call neither bypasses COLI_HIP_RUNTIME_DIR validation nor+ leaves the backend mapped when that validation fails.+ """+ proc, out = self._probe("probe_hip_unconfigured", vendor="hip")+ detail = self._detail(proc)+ self.assertEqual(proc.returncode, 0, "harness failed" + detail)+ self.assertEqual(out.get("available_before_init"), "0", detail)+ self.assertEqual(out.get("backend_loaded_after_probe"), "0", detail)++if __name__ == "__main__":unittest.main()