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: VK=1 in Makefile.deepseek-v4, Vulkan init, hook and report

JustVuggb57586ef65f1
diff --git a/c/Makefile.deepseek-v4 b/c/Makefile.deepseek-v4
index fccd0bda..20e17870 100644
--- a/c/Makefile.deepseek-v4
+++ b/c/Makefile.deepseek-v4
@@ -212,13 +212,39 @@ LDFLAGS += -L$(CUDA_HOME)/lib64 -L$(CUDA_HOME)/lib64/stubs -Wl,-rpath,$(CUDA_HOM
endif
endif
+# ---- Vulkan (opt-in): VK=1 ---------------------------------------------------
+# The parent Makefile's VK=1, for this engine: -DCOLI_VULKAN, the backend object
+# and the SPIR-V shaders it loads at run time (COLI_VK_SHADERS, else shaders/ next
+# to the binary). `make deepseek-v4 VK=1` in the parent reaches here through
+# MAKEFLAGS. The object has its own name because the parent builds
+# backend_vulkan.o with its own CFLAGS while these carry -flto and the units'
+# flags stamp: one file written by two builds is #1702 again. Independent of
+# CUDA=1, whose tier keeps running first when both are built.
+VK ?= 0
+GLSLC ?= glslc
+V4_VK_OBJ =
+V4_VK_SPV =
+ifeq ($(VK),1)
+CFLAGS += -DCOLI_VULKAN
+ifneq ($(IS_WIN),)
+# As in the parent: the loader is vulkan-1.dll, and -static would skip its
+# import library, so search dynamically for this one library only.
+LDFLAGS += -Wl,-Bdynamic -lvulkan-1 -Wl,-Bstatic
+else
+LDFLAGS += -lvulkan
+endif
+V4_VK_OBJ = backend_vulkan_v4.o
+V4_VK_SPV = shaders/qmatmul.spv shaders/qmatmul_gate_up.spv \
+ shaders/attention_absorb.spv shaders/rmsnorm.spv
+endif
+
include Makefile.deepseek-v4.units
TARGET_OBJS = $(addsuffix .o,$(V4_TARGET_UNITS))
TEST_UNIT_OBJS = $(addsuffix .o,$(V4_TEST_UNITS))
V4_HOT_TEST_OBJ = COLI_V4_UNIT_EXPERT_STORE_HOT_ROWS16_TEST.o
V4_BATCH_TEST_OBJ = COLI_V4_UNIT_NATIVE_QUANT_BATCH_TEST.o
-V4_OBJS = $(TARGET_OBJS) $(V4_WIN_EXTRA_OBJS) $(REGISTRY_OBJ)
+V4_OBJS = $(TARGET_OBJS) $(V4_WIN_EXTRA_OBJS) $(REGISTRY_OBJ) $(V4_VK_OBJ)
REGISTRY_OBJ = expert_store_registry.o
V4_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))
@@ -277,9 +303,16 @@ deepseek-v4-objs: $(V4_OBJS)
print-v4-objs:
@echo $(V4_OBJS)
-$(V4_BINARY): $(V4_OBJS)
+$(V4_BINARY): $(V4_OBJS) $(V4_VK_SPV)
$(CC) $(V4_OBJS) -o $@ $(LDFLAGS)
+# VK=1: the Vulkan backend compiled with this engine's flags, and the shaders.
+backend_vulkan_v4.o: backend_vulkan.c backend_vulkan.h $(V4_FLAGS_STAMP)
+ $(CC) $(CFLAGS) -c backend_vulkan.c -o $@
+shaders/%.spv: shaders/%.comp
+ @command -v $(GLSLC) >/dev/null 2>&1 || { echo "glslc not found: install shaderc (for VK=1)" >&2; exit 1; }
+ $(GLSLC) --target-env=vulkan1.2 $< -o $@
+
# 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 \
@@ -340,4 +373,4 @@ deepseek-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_CUDA_FLAGS_STAMP)
+ $(V4_CUDA_FLAGS_STAMP) backend_vulkan_v4.o
diff --git a/c/deepseek_v4.c b/c/deepseek_v4.c
index af6bfa37..225512ca 100644
--- a/c/deepseek_v4.c
+++ b/c/deepseek_v4.c
@@ -14519,6 +14519,35 @@ static int v4_serve_one(ColiV4Engine *engine, ColiV4Session *session,
return 0;
}
+/* Vulkan for the engine binary (Makefile.deepseek-v4 VK=1). The backend is
+ * touched from this unit only: it is the one the binary and its serve test link,
+ * while the parent Makefile links other units into its own tests without
+ * backend_vulkan, so a reference anywhere else would break those links under
+ * VK=1. Opened after the engine (CUDA, when built, has opened first), reported
+ * once at the end; v4_vk_fp8_matvec says why the count is 0 on this engine. */
+#ifdef COLI_VULKAN
+#include "backend_vulkan.h"
+static int g_v4_vk_ready = 0;
+#endif
+
+static void v4_vk_open(void) {
+#ifdef COLI_VULKAN
+ g_v4_vk_ready = coli_vk_init_env("deepseek_v4");
+ if (g_v4_vk_ready)
+ fprintf(stderr, "[VK] deepseek_v4: no resident matrix is in a format the "
+ "shaders decode (dense E4M3 with 128x128 block scales, "
+ "bf16), nothing is sent to the Vulkan device\n");
+#endif
+}
+
+static void v4_vk_report(void) {
+#ifdef COLI_VULKAN
+ if (g_v4_vk_ready)
+ fprintf(stderr, "[VK] deepseek_v4: %llu matmuls on the GPU\n",
+ coli_vk_matmul_calls());
+#endif
+}
+
static int v4_serve_main(void) {
const char *model_dir = getenv("SNAP");
if (!model_dir || !*model_dir) {
@@ -14546,6 +14575,7 @@ static int v4_serve_main(void) {
fprintf(stderr, "%s\n", error);
return 1;
}
+ v4_vk_open();
context = engine->runtime.context_tokens;
if (coli_v4_session_create(
&session, engine,
@@ -14590,6 +14620,7 @@ static int v4_serve_main(void) {
if (fatal < 0) break;
}
}
+ v4_vk_report();
coli_v4_session_destroy(session);
coli_v4_engine_destroy(engine);
return 0;
@@ -14682,6 +14713,7 @@ int main(int argc, char **argv) {
goto cleanup;
}
}
+ v4_vk_open();
config = *coli_v4_engine_config(engine);
index = coli_v4_engine_target_index(engine);
experts = coli_v4_engine_expert_store(engine);
@@ -14941,6 +14973,7 @@ int main(int argc, char **argv) {
}
result = 0;
cleanup:
+ v4_vk_report();
v4_generate_cleanup(session, prompt_storage, engine, attention,
layers, prompt_ids, generated, state, next, hidden,
text, full_ids, tf_pred, tf_state,
@@ -17121,6 +17154,34 @@ static int fp8_matvec_compute(float *output, const ColiTensorView *weight,
return 0;
}
+#ifdef COLI_VULKAN
+/* The Vulkan hook of the resident dense projections (Makefile.deepseek-v4 VK=1),
+ * at the CUDA tier's place and tried after it. It sends nothing, and the reason is
+ * the format. qmatmul.comp decodes int8 rows (fmt 1), int4 rows (2), grouped int4
+ * (4), int3-g64 (5) and MXFP4 (7); every resident matrix here is E4M3 in 128x128
+ * blocks with f32 scales (attention, indexer wq_b, shared experts), bf16 (router,
+ * compressors, indexer projections, head) or f32, and requantising to int8 would
+ * change the numbers the tiny oracle holds this engine to. The routed experts are
+ * fp4 with a UE8M0 scale per 32 columns, which fmt 7 reads once the scales are
+ * expanded to f32, but they come from disk into the expert store's cache, which
+ * evicts and re-pins them: a device tier for them, like kimi_k3's or this engine's
+ * CUDA mirrors, is other work than this hook, and they stay on the CPU. The batched
+ * and paired fp8 entries (NATIVE_QUANT_BATCH, _DUAL) would take the same hook; with
+ * nothing to send, only these two single-vector entries carry it.
+ *
+ * What would turn this into a call: an E4M3 format in qmatmul.comp, one byte per
+ * weight (the fmt 1 row layout, unpacked from the rows8 tiles the AVX2 path keeps,
+ * as v4_gpu_upload_fp8_fmt does for CUDA) and one f32 scale per 128 inputs (the
+ * fmt 4/7 group layout, gs = 128, each block scale repeated over its 128 rows), with
+ * the input rounded to E4M3 per 128 first as coli_fp8_activation_qdq_ref does, and
+ * a call from the main thread only. Until then it declines and the CPU runs. */
+static int v4_vk_fp8_matvec(const ColiTensorView *weight, float *output,
+ const float *input) {
+ (void)weight; (void)output; (void)input;
+ return -1;
+}
+#endif
+
int coli_fp8_matvec_ref(float *output, const ColiTensorView *weight,
const float *input) {
if (!output || !input || fp8_matvec_validate(weight))
@@ -17135,6 +17196,10 @@ int coli_fp8_matvec_ref(float *output, const ColiTensorView *weight,
* through to the CPU reference below. */
if (weight->gpu && coli_v4_gpu_fp8_matvec(weight, output, input) == 0)
return 0;
+#endif
+#ifdef COLI_VULKAN
+ if (v4_vk_fp8_matvec(weight, output, input) == 0)
+ return 0;
#endif
float *activation; uint8_t *activation_scales;
if (coli_v4_qdq_scratch(columns, scale_columns, &activation, &activation_scales))
@@ -17161,6 +17226,10 @@ int coli_fp8_matvec_pre(float *output, const ColiTensorView *weight,
#ifdef COLI_V4_GPU_TIER
if (weight->gpu && coli_v4_gpu_fp8_matvec(weight, output, input) == 0)
return 0;
+#endif
+#ifdef COLI_VULKAN
+ if (v4_vk_fp8_matvec(weight, output, input) == 0)
+ return 0;
#endif
return fp8_matvec_compute(output, weight, activation);
}