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 backend_cuda_dsv4.o when the nvcc command changes
bokiko9ba9ed895f09
diff --git a/.gitignore b/.gitignoreindex 0a3552cc..eeba7b07 100644--- a/.gitignore+++ b/.gitignore@@ -31,6 +31,7 @@ c/deepseek_v41c/deepseek_v41.exec/COLI_V4_UNIT_*.oc/deepseek_v4.cflags+c/deepseek_v4.cudaflags# ...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 130e8100..fccd0bda 100644--- a/c/Makefile.deepseek-v4+++ b/c/Makefile.deepseek-v4@@ -240,6 +240,29 @@ $(V4_FLAGS_STAMP): FORCEfiFORCE:+# The same defect one level down. backend_cuda_dsv4.o is named after its source,+# but what it contains comes from $(NVCC) $(V4_NVCCFLAGS): -arch=$(CUDA_ARCH)+# (or the -gencode preset), the -DCOLI_DSV4_NO_TC guard and the DeepGEMM defines+# all arrive through it. $(CFLAGS) above does not cover it, so without this:+#+# make -f Makefile.deepseek-v4 deepseek-v4 CUDA=1 CUDA_ARCH=sm_86+# make -f Makefile.deepseek-v4 deepseek-v4 CUDA=1 CUDA_ARCH=sm_80+# -> no output at all, exit 0+#+# and the engine that asked for sm_80 links the sm_86 object, in silence. The+# parent Makefile keeps the same class of state for the same reason, CUDA_ARCH+# included, in .build-config (#306). Same scheme as $(V4_FLAGS_STAMP) above, so+# a dry run (make -n) or a clean writes nothing: the file is only touched by the+# recipe, and only when the command really changed.+V4_CUDA_FLAGS_STAMP = deepseek_v4.cudaflags+V4_TRACK_CUDA_FLAGS = $(subst ','\'',$(NVCC) $(V4_NVCCFLAGS))+$(V4_CUDA_FLAGS_STAMP): FORCE+ @flags='$(V4_TRACK_CUDA_FLAGS)'; \+ if test x"$$flags" != x"`cat $@ 2>/dev/null`"; then \+ test -f $@ && echo "deepseek-v4: CUDA build flags changed, rebuilding backend_cuda_dsv4.o" >&2; \+ echo "$$flags" > $@; \+ fi+.PHONY: deepseek-v4 deepseek-v4-objs deepseek-v4-clean deepseek-v4-test-objs \deepseek-v4-test-registry print-v4-objs FORCEdeepseek-v4: $(V4_BINARY)@@ -294,7 +317,8 @@ backend_loader_dsv4.o: backend_loader_dsv4.c backend_cuda_dsv4.h $(V4_FLAGS_STAM$(CC) $(CFLAGS) -c backend_loader_dsv4.c -o $@# Linux/macOS CUDA=1: the tier compiled straight into the engine.-backend_cuda_dsv4.o: backend_cuda_dsv4.cu backend_cuda_dsv4.h $(V4_DEEPGEMM_DEP)+backend_cuda_dsv4.o: backend_cuda_dsv4.cu backend_cuda_dsv4.h $(V4_DEEPGEMM_DEP) \+ $(V4_CUDA_FLAGS_STAMP)"$(NVCC)" $(V4_NVCCFLAGS) -c backend_cuda_dsv4.cu -o $@ifneq ($(DEEPGEMM_STAMP),)@@ -315,4 +339,5 @@ 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_FLAGS_STAMP)+ $(V4_SERVE_TEST) test_expert_store_registry $(V4_FLAGS_STAMP) \+ $(V4_CUDA_FLAGS_STAMP)diff --git a/c/tests/test_deepseek_v4_cuda_arch_flags.py b/c/tests/test_deepseek_v4_cuda_arch_flags.pynew file mode 100644index 00000000..32595123--- /dev/null+++ b/c/tests/test_deepseek_v4_cuda_arch_flags.py@@ -0,0 +1,154 @@+"""backend_cuda_dsv4.o must be rebuilt when the nvcc command line changes.++The object is named after its source, but its contents come from+$(NVCC) $(V4_NVCCFLAGS): ``-arch=$(CUDA_ARCH)`` (or the -gencode preset), the+-DCOLI_DSV4_NO_TC guard and the DeepGEMM defines. Timestamps cannot tell those+apart, so without a record of the command make reports "is up to date" and+reuses the previous object (#1702 is the C-side case; #1707 fixed the units,+this is the nvcc side):++ make -f Makefile.deepseek-v4 deepseek-v4 CUDA=1 CUDA_ARCH=sm_86+ make -f Makefile.deepseek-v4 deepseek-v4 CUDA=1 CUDA_ARCH=sm_80+ -> no output at all, still the sm_86 engine++The parent Makefile records CUDA_ARCH in .build-config for this reason (#306);+the standalone build keeps its own stamp, deepseek_v4.cudaflags.++The nvcc here is a stand-in that only creates its -o target and appends its own+command line to a log, so the test needs make and sh but no CUDA toolkit and no+GPU, and it checks exactly what make decides to recompile. 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")+OBJ = "backend_cuda_dsv4.o"+STAMP = "deepseek_v4.cudaflags"++FAKE_NVCC = """#!/bin/sh+# Stand-in nvcc: records the command line, creates the -o target, compiles nothing.+printf '%s\\n' "$*" >> "$FAKE_NVCC_LOG"+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 DeepseekV4CudaArchFlagsTest(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", "*.cu"):+ for src in C_DIR.glob(pattern):+ shutil.copy(src, self.dir / src.name)+ self.nvcc = self.dir / "fake-nvcc.sh"+ self.nvcc.write_text(FAKE_NVCC, encoding="utf-8")+ self.nvcc.chmod(0o755)+ self.log = self.dir / "nvcc.log"+ # Sources well in the past: every object built below is newer than all of+ # them, so the only thing left that can make the object stale is a flag.+ 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):+ env = dict(os.environ, FAKE_NVCC_LOG=str(self.log))+ return subprocess.run(+ [MAKE, "-f", "Makefile.deepseek-v4", f"NVCC={self.nvcc}", *args],+ cwd=self.dir, text=True, capture_output=True, check=False,+ timeout=120, env=env)++ def nvcc_calls(self):+ return len(self.log.read_text(encoding="utf-8").split("\n")) - 1 if self.log.exists() else 0++ def build(self, *variables):+ """Build the object, then age it so only a flags change can rebuild it.++ Leaves the object and the stamp older than now but newer than the+ sources, so a later build recompiles only if make finds a real reason,+ never because two writes landed in the same clock tick.+ """+ self.log.write_text("", encoding="utf-8")+ result = self.make(OBJ, *variables)+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+ calls = self.nvcc_calls()+ aged = time.time() - 500+ for f in self.dir.iterdir():+ if f.stat().st_mtime > aged + 400:+ os.utime(f, (aged, aged))+ return calls++ def test_same_arch_does_not_rebuild(self):+ """The guard: a fix that always recompiles would pass the tests below."""+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=sm_86"), 1)+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=sm_86"), 0,+ f"{OBJ} was recompiled with an unchanged nvcc command line")++ def test_changed_arch_rebuilds(self):+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=sm_86"), 1)+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=sm_80"), 1,+ f"{OBJ} built for sm_86 was reused by an sm_80 build: the "+ "engine links an object for the wrong architecture")++ def test_changed_gencode_preset_rebuilds(self):+ """The presets change -gencode just as -arch does."""+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=sm_86"), 1)+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=portable"), 1,+ f"{OBJ} was reused after the gencode preset changed")++ def test_changed_no_tc_rebuilds(self):+ """NO_TC adds -DCOLI_DSV4_NO_TC to the nvcc line, same object name."""+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=sm_86", "NO_TC=0"), 1)+ self.assertEqual(self.build("CUDA=1", "CUDA_ARCH=sm_86", "NO_TC=1"), 1,+ f"{OBJ} was reused after NO_TC changed")++ def test_a_dry_run_writes_nothing(self):+ """make -n must not create build state. The stamp is written by a recipe,+ not while the Makefile is read, so a dry run only prints it."""+ dry = self.make("-n", OBJ, "CUDA=1", "CUDA_ARCH=sm_86")+ self.assertEqual(dry.returncode, 0, dry.stdout + dry.stderr)+ self.assertFalse((self.dir / STAMP).exists(),+ f"make -n created {STAMP}")+ self.assertEqual(self.nvcc_calls(), 0, "make -n ran nvcc")+ clean = self.make("-n", "deepseek-v4-clean")+ self.assertEqual(clean.returncode, 0, clean.stdout + clean.stderr)+ self.assertFalse((self.dir / STAMP).exists(),+ f"make -n deepseek-v4-clean created {STAMP}")++ def test_clean_writes_nothing(self):+ """A clean recipe that removes the stamp must not recreate it first."""+ self.build("CUDA=1", "CUDA_ARCH=sm_86")+ result = self.make("deepseek-v4-clean")+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+ self.assertFalse((self.dir / STAMP).exists(),+ f"{STAMP} survived deepseek-v4-clean")++ def test_clean_removes_the_stamp(self):+ """A stale stamp after `make clean` would make the next build a no-op."""+ self.build("CUDA=1", "CUDA_ARCH=sm_86")+ subprocess.run([MAKE, "-f", "Makefile.deepseek-v4", "deepseek-v4-clean"],+ cwd=self.dir, text=True, capture_output=True, check=False,+ timeout=120)+ self.assertFalse((self.dir / STAMP).exists(),+ f"{STAMP} survived deepseek-v4-clean")+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tools/clean.py b/c/tools/clean.pyindex 4258f1a5..f610f763 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.cflags",+ "deepseek_v4", "deepseek_v4.exe", "deepseek_v4.cflags", "deepseek_v4.cudaflags","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",