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

修复 DeepSeek V4 融合 MoE 的共享专家权重绑定

ZacharyZcR8c094e5240d9
diff --git a/c/deepseek_v4.c b/c/deepseek_v4.c
index bc8f3862..af6bfa37 100644
--- a/c/deepseek_v4.c
+++ b/c/deepseek_v4.c
@@ -5155,8 +5155,8 @@ static int moe_token_pipeline(float *output,
}
if (!result && gpu_compute && store->gpu) {
void *sg = coli_v4_layer_gpu(weights, "ffn.shared_experts.w1");
- void *su = coli_v4_layer_gpu(weights, "ffn.shared_experts.w2");
- void *sd = coli_v4_layer_gpu(weights, "ffn.shared_experts.w3");
+ void *su = coli_v4_layer_gpu(weights, "ffn.shared_experts.w3");
+ void *sd = coli_v4_layer_gpu(weights, "ffn.shared_experts.w2");
int moe_ok = sg && su && sd;
void **gates = malloc((size_t)selected * sizeof(*gates));
void **ups = malloc((size_t)selected * sizeof(*ups));
diff --git a/c/tests/test_v4_shared_expert_bindings.py b/c/tests/test_v4_shared_expert_bindings.py
new file mode 100644
index 00000000..3c8f965d
--- /dev/null
+++ b/c/tests/test_v4_shared_expert_bindings.py
@@ -0,0 +1,25 @@
+"""Check fused decode bindings against the checkpoint tensor plan, without CUDA."""
+from pathlib import Path
+import re
+import unittest
+
+
+class SharedExpertBindingsTest(unittest.TestCase):
+ def test_decode_fused_moe_receives_gate_up_down_shapes(self):
+ source = (Path(__file__).resolve().parents[1] / "deepseek_v4.c").read_text()
+ shapes = dict((name, (rows, columns)) for rows, columns, name in re.findall(
+ r'add_fp8\(plan, (\w+), (\w+), "(ffn\.shared_experts\.w[123])"', source))
+ bindings = re.findall(
+ r'void \*(s[gud]) = coli_v4_layer_gpu\(weights, "([^"]+)"\);', source)
+ # Gate and up project hidden -> moe; down projects moe -> hidden.
+ expected = {"sg": ("moe", "hidden"), "su": ("moe", "hidden"),
+ "sd": ("hidden", "moe")}
+ self.assertTrue(bindings, "no fused decode shared-expert bindings found")
+ self.assertEqual(set(name for name, _ in bindings), set(expected))
+ for name, tensor in bindings:
+ with self.subTest(binding=name, tensor=tensor):
+ self.assertEqual(shapes[tensor], expected[name])
+
+
+if __name__ == "__main__":
+ unittest.main()