Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(v4): rebuild the unit objects when the build flags change
Marcello Costagliola4e8ef204b0e1
diff --git a/.gitignore b/.gitignoreindex d1564447..0a3552cc 100644--- a/.gitignore+++ b/.gitignore@@ -30,6 +30,7 @@ c/deepseek_v4.exec/deepseek_v41c/deepseek_v41.exec/COLI_V4_UNIT_*.o+c/deepseek_v4.cflags# ...and the ownership-test objects, which the same Makefile puts in a build/# subdirectory (V4_OWN_DIR) rather than next to the sources. #868 caught the# twelve in c/, not the four in here, so `make check` still left `?? c/build/`.diff --git a/c/Makefile.deepseek-v4 b/c/Makefile.deepseek-v4index a7858363..7986c748 100644--- a/c/Makefile.deepseek-v4+++ b/c/Makefile.deepseek-v4@@ -223,8 +223,25 @@ REGISTRY_OBJ = expert_store_registry.oV4_SERVE_TEST := tests/test_v4_serve_framing$(if $(IS_WIN),.exe,)V4_SERVE_TEST_OBJS := $(filter-out COLI_V4_UNIT_GENERATE_STATS.o,$(V4_OBJS))+# The objects are named after the unit, not after the flags they were built+# with, and three builds share them: this engine (CUDA=1 adds+# -DCOLI_V4_GPU_TIER), the parent's test-c (no GPU tier) and its test-asan+# (EXTRA_CFLAGS). Timestamps cannot tell those apart, so make would report+# "is up to date" and link one build's objects into another (#1702). The+# compiler line is recorded here and rewritten only when it changes; every+# object compiled with $(CFLAGS) depends on it. Same scheme as git's GIT-CFLAGS.+V4_FLAGS_STAMP = deepseek_v4.cflags+V4_TRACK_FLAGS = $(subst ','\'',$(CC) $(CFLAGS))+$(V4_FLAGS_STAMP): FORCE+ @flags='$(V4_TRACK_FLAGS)'; \+ if test x"$$flags" != x"`cat $@ 2>/dev/null`"; then \+ test -f $@ && echo "deepseek-v4: build flags changed, rebuilding the units" >&2; \+ echo "$$flags" > $@; \+ fi+FORCE:+.PHONY: deepseek-v4 deepseek-v4-objs deepseek-v4-clean deepseek-v4-test-objs \- deepseek-v4-test-registry print-v4-objs+ deepseek-v4-test-registry print-v4-objs FORCEdeepseek-v4: $(V4_BINARY)# Build just the amalgamation unit + registry objects (no link). External@@ -242,24 +259,25 @@ $(V4_BINARY): $(V4_OBJS)# Pluggable expert-store backend registry (standalone; not a deepseek_v4.c unit).# Linked into the binary so the engine can dispatch COLI_EXPERT_STORE backends.-$(REGISTRY_OBJ): expert_store_registry.c expert_store_registry.h expert_store.h+$(REGISTRY_OBJ): expert_store_registry.c expert_store_registry.h expert_store.h \+ $(V4_FLAGS_STAMP)$(CC) $(CFLAGS) -c expert_store_registry.c -o $@$(TARGET_OBJS) $(TEST_UNIT_OBJS): %.o: deepseek_v4.c deepseek_v4.h \deepseek_v4_internal.h deepseek_v4_dspark.inc st.h json.h compat.h tensor.h quant.h \route_trace.h \native_quant.h native_quant_batch.h native_quant_dual.h \- native_quant_fp4_rows16.h expert_store_registry.h+ native_quant_fp4_rows16.h expert_store_registry.h $(V4_FLAGS_STAMP)$(CC) $(CFLAGS) -D$* -c deepseek_v4.c -o $@$(V4_HOT_TEST_OBJ): deepseek_v4.c deepseek_v4.h deepseek_v4_internal.h \st.h json.h compat.h tensor.h quant.h route_trace.h \- native_quant.h native_quant_fp4_rows16.h expert_store_registry.h+ native_quant.h native_quant_fp4_rows16.h expert_store_registry.h $(V4_FLAGS_STAMP)$(CC) $(CFLAGS) -DCOLI_V4_TEST_HOOKS \-DCOLI_V4_UNIT_EXPERT_STORE_HOT_ROWS16 -c deepseek_v4.c -o $@$(V4_BATCH_TEST_OBJ): deepseek_v4.c deepseek_v4.h deepseek_v4_internal.h \- tensor.h quant.h native_quant.h native_quant_batch.h+ tensor.h quant.h native_quant.h native_quant_batch.h $(V4_FLAGS_STAMP)$(CC) $(CFLAGS) -DCOLI_V4_TEST_HOOKS \-DCOLI_V4_UNIT_NATIVE_QUANT_BATCH -c deepseek_v4.c -o $@@@ -272,7 +290,7 @@ $(V4_SERVE_TEST): tests/test_v4_serve_framing.c deepseek_v4.c deepseek_v4.h \# The CUDA tier loader object. Compiled only on Windows, where it is appended# to V4_OBJS and resolves the MSVC-built coli_cuda_dsv4.dll at runtime. On# Linux it is empty so the engine links without it.-backend_loader_dsv4.o: backend_loader_dsv4.c backend_cuda_dsv4.h+backend_loader_dsv4.o: backend_loader_dsv4.c backend_cuda_dsv4.h $(V4_FLAGS_STAMP)$(CC) $(CFLAGS) -c backend_loader_dsv4.c -o $@# Linux/macOS CUDA=1: the tier compiled straight into the engine.@@ -297,4 +315,4 @@ test_expert_store_registry: test_expert_store_registry.c expert_store_registry.cdeepseek-v4-clean:rm -f $(V4_BINARY) $(V4_OBJS) $(TEST_UNIT_OBJS) $(V4_HOT_TEST_OBJ) \$(V4_BATCH_TEST_OBJ) \- $(V4_SERVE_TEST) test_expert_store_registry+ $(V4_SERVE_TEST) test_expert_store_registry $(V4_FLAGS_STAMP)diff --git a/c/tests/test_deepseek_v4_build_flags.py b/c/tests/test_deepseek_v4_build_flags.pynew file mode 100644index 00000000..850dc20d--- /dev/null+++ b/c/tests/test_deepseek_v4_build_flags.py@@ -0,0 +1,114 @@+"""Makefile.deepseek-v4 must rebuild its objects when the build flags change.++The unit objects are named after the unit (COLI_V4_UNIT_MATH.o), not after the+flags they were compiled with, and three builds share those names in c/: the+engine (`CUDA=1` adds -DCOLI_V4_GPU_TIER), the parent's `make test-c` (no GPU+tier) and `make test-asan` (EXTRA_CFLAGS). Timestamps cannot tell them apart,+so without a record of the flags make reports "is up to date" and links the+other build's objects (#1702):++ make -f Makefile.deepseek-v4 deepseek-v4 CUDA=1+ make test-c+ -> undefined reference to `coli_v4_gpu_matvec_grouped'++and, the silent direction, `make test-c` followed by the CUDA=1 engine build+links CPU-only units into an engine that was asked for the GPU tier.++The compiler here is a stand-in that only creates its -o target, so the test+needs make and sh but no toolchain, and checks exactly what make decides to+rebuild. The sources are copied, not linked, so their timestamps can be pinned+without touching the tree.+"""+import os+import shutil+import subprocess+import tempfile+import time+import unittest+from pathlib import Path++C_DIR = Path(__file__).resolve().parent.parent+MAKE = shutil.which("make")+UNIT = "COLI_V4_UNIT_MATH.o"++FAKE_CC = """#!/bin/sh+# Stand-in compiler: creates the -o target and compiles nothing.+while [ "$#" -gt 0 ]; do+ if [ "$1" = -o ]; then : > "$2"; exit 0; fi+ shift+done+exit 1+"""++[email protected](MAKE and os.name == "posix", "make and a POSIX shell are required")+class DeepseekV4BuildFlagsTest(unittest.TestCase):+ def setUp(self):+ self._tmp = tempfile.TemporaryDirectory()+ self.dir = Path(self._tmp.name)+ for pattern in ("Makefile.deepseek-v4*", "deepseek_v4.c", "*.h", "*.inc"):+ for src in C_DIR.glob(pattern):+ shutil.copy(src, self.dir / src.name)+ fake = self.dir / "fakecc.sh"+ fake.write_text(FAKE_CC, encoding="utf-8")+ self.cc = f"sh {fake}"+ # Sources well in the past: every object built below is newer than all+ # of them, so the only thing left that can make it stale is the flags.+ past = time.time() - 1000+ for f in self.dir.iterdir():+ os.utime(f, (past, past))+ probe = self.make("print-v4-objs")+ if probe.returncode != 0:+ self.skipTest("Makefile.deepseek-v4 does not support this host: "+ + probe.stderr.strip()[-200:])++ def tearDown(self):+ self._tmp.cleanup()++ def make(self, *args):+ return subprocess.run(+ [MAKE, "-f", "Makefile.deepseek-v4", f"CC={self.cc}", *args],+ cwd=self.dir, text=True, capture_output=True, check=False, timeout=120)++ def build(self, *variables):+ """Build the unit, then age everything it produced by the same amount.++ Leaves every file this build wrote at one timestamp, older than now+ and newer than the sources: a later build only recompiles if make+ finds a real reason to, never because two writes landed in the same+ clock tick.+ """+ before = {f: f.stat().st_mtime for f in self.dir.iterdir()}+ result = self.make(UNIT, *variables)+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+ aged = time.time() - 500+ for f in self.dir.iterdir():+ if before.get(f) != f.stat().st_mtime:+ os.utime(f, (aged, aged))+ return "-c deepseek_v4.c" in result.stdout++ def test_same_flags_do_not_rebuild(self):+ """The guard: a fix that always recompiles would pass the tests below."""+ self.assertTrue(self.build("EXTRA_CFLAGS=-DCOLI_TEST_FLAGS_A"))+ self.assertFalse(self.build("EXTRA_CFLAGS=-DCOLI_TEST_FLAGS_A"),+ f"{UNIT} was recompiled with unchanged flags")++ def test_changed_flags_rebuild(self):+ """EXTRA_CFLAGS is how the parent's test-asan reaches this Makefile."""+ self.assertTrue(self.build("EXTRA_CFLAGS=-DCOLI_TEST_FLAGS_A"))+ self.assertTrue(self.build("EXTRA_CFLAGS=-DCOLI_TEST_FLAGS_B"),+ f"{UNIT} was reused after EXTRA_CFLAGS changed: the "+ "objects of one build configuration leak into another")++ def test_cuda_tier_objects_are_not_reused_by_a_cpu_build(self):+ """The reported case, in both directions (#1702). Building one unit+ object needs no nvcc: only backend_cuda_dsv4.o does."""+ self.assertTrue(self.build("CUDA=1"))+ self.assertTrue(self.build(),+ f"{UNIT} built with CUDA=1 was reused by a CPU build")+ self.assertTrue(self.build("CUDA=1"),+ f"{UNIT} built without the GPU tier was reused by CUDA=1")+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tools/clean.py b/c/tools/clean.pyindex ce08c299..4258f1a5 100644--- a/c/tools/clean.py+++ b/c/tools/clean.py@@ -33,7 +33,7 @@ FILES = ["coli_cuda.dll", "coli_cuda.lib", "coli_cuda.exp",# hipcc emits an import library, export file and PDB alongside the DLL."coli_hip.dll", "coli_hip.lib", "coli_hip.exp", "coli_hip.pdb",- "deepseek_v4", "deepseek_v4.exe",+ "deepseek_v4", "deepseek_v4.exe", "deepseek_v4.cflags","deepseek_v41", "deepseek_v41.exe","native_quant.o", "native_quant_parallel.o", "native_quant_dual.o","native_quant_batch_avx512.o", "native_quant_fp4_rows16.o",