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

vulkan: COLI_VK_DEV picks the device; the coop GEMM's epilogue stride aligned to 16 bytes (#1908)

JustVugg674d45196b5f
diff --git a/c/backend_vulkan.c b/c/backend_vulkan.c
index bfbe0124..ad9b9073 100644
--- a/c/backend_vulkan.c
+++ b/c/backend_vulkan.c
@@ -546,12 +546,15 @@ static int gemm_pipeline(VkGemmTile t, int slot) {
return 1;
}
+/* qmatmul_coop.comp's EST: floats per epilogue row of its accumulator staging (a
+ * multiple of 4 floats, so coopMatStore's stride is 16-byte aligned; #1908). */
+#define VK_COOP_EST 20
/* Subgroups of exactly G.coop_sg lanes; BK a multiple of 32 (group sizes are). */
static int coop_tile_ok(VkCoopTile t, const VkPhysicalDeviceLimits *lim) {
if (t.wm < 16 || t.wn < 16 || t.wm % 16 || t.wn % 16 || t.bm % t.wm || t.bn % t.wn ||
t.bk < 32 || t.bk % 32 || G.coop_sg < 16 || 256 % G.coop_sg) return 0;
long nw = (long)(t.bm / t.wm) * (t.bn / t.wn), threads = nw * G.coop_sg;
- long lds = (long)(t.bm + 2 * t.bn) * (t.bk + 8) * 2 + (long)t.bm * 4 + nw * 16 * 17 * 4;
+ long lds = (long)(t.bm + 2 * t.bn) * (t.bk + 8) * 2 + (long)t.bm * 4 + nw * 16 * VK_COOP_EST * 4;
return threads <= (long)lim->maxComputeWorkGroupInvocations &&
threads <= (long)lim->maxComputeWorkGroupSize[0] && lds <= (long)lim->maxComputeSharedMemorySize;
}
@@ -619,9 +622,18 @@ int coli_vk_init(const char *spv_path) {
vkEnumeratePhysicalDevices(G.inst, &nd, devs);
// Prefer a real GPU over a CPU/software device (llvmpipe) on multi-adapter hosts:
// discrete > integrated > virtual > other/cpu. Falls back to devs[0] if all equal.
+ // COLI_VK_DEV=<index> takes that entry of the enumeration instead (the order
+ // vulkaninfo lists as GPU0, GPU1, ...): with two discrete GPUs the ranking always
+ // took the first one (#1908). Out of range or not a number: a line, then the ranking.
G.phys = devs[0];
- int bestrank = -1;
- for (uint32_t i = 0; i < nd; i++) {
+ int bestrank = -1, forced = 0;
+ const char *dv = getenv("COLI_VK_DEV");
+ if (dv && *dv) {
+ char *end = NULL; long k = strtol(dv, &end, 10);
+ if (end != dv && *end == 0 && k >= 0 && k < (long)nd) { G.phys = devs[k]; forced = 1; }
+ else fprintf(stderr, "[VK] COLI_VK_DEV=%s ignored: %u device%s, numbered from 0\n", dv, nd, nd == 1 ? "" : "s");
+ }
+ for (uint32_t i = 0; i < nd && !forced; i++) {
VkPhysicalDeviceProperties p; vkGetPhysicalDeviceProperties(devs[i], &p);
int rank = p.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU ? 4 :
p.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU ? 3 :
@@ -3125,7 +3137,7 @@ static void xb_coop_init(void) {
VkCoopTile t = G.coop_t[k];
int nw = (t.bm / t.wm) * (t.bn / t.wn), nt = nw * G.coop_sg;
size_t shared = (size_t)(t.bm + 2 * t.bn) * (t.bk + 8) * 2 +
- (size_t)t.bm * 4 + (size_t)nw * 16 * 17 * 4 + (size_t)(nt + t.bn) * 4;
+ (size_t)t.bm * 4 + (size_t)nw * 16 * VK_COOP_EST * 4 + (size_t)(nt + t.bn) * 4;
if (shared > pp.limits.maxComputeSharedMemorySize || nt % (nt < t.bn ? 1 : nt / t.bn)) continue;
int32_t sv[8] = {t.bm, t.bn, t.wm, t.wn, t.bk, G.coop_sg, nt, 1};
VkSpecializationMapEntry me[8];
diff --git a/c/shaders/qmatmul_coop.comp b/c/shaders/qmatmul_coop.comp
index cb244fbe..6edbc66e 100644
--- a/c/shaders/qmatmul_coop.comp
+++ b/c/shaders/qmatmul_coop.comp
@@ -51,7 +51,12 @@ const int FM = WM / 16, FN = WN / 16; // 16 x 16 accumulators per subgroup
const int NW = (BM / WM) * (BN / WN); // subgroups
const int NT = NW * SG;
const int WST = BK + 8; // halves per staged row, 16 bytes of padding
-const int EST = 17; // floats per epilogue row: odd, no bank conflicts
+// floats per epilogue row. coopMatStore's stride must be a multiple of 16 bytes here
+// (VUID-RuntimeSpirv-OpCooperativeMatrixLoadKHR-08986: the lesser of 16 bytes and a
+// row's natural alignment, 64 bytes for 16 floats). The odd 17 it was broke that rule:
+// RADV took it, NVIDIA's driver returned garbage (black Qwen-Image pictures, #1908).
+// 20 (80 bytes) keeps the rows of a store off the same banks for 8 rows in a row.
+const int EST = 20;
shared float16_t wsh[BM * WST]; // [o][i]: the MMA's A, row-major
shared float16_t xhs[BN * WST]; // [s][i]: its B, column-major; hi part
shared float16_t xls[BN * WST]; // lo part
diff --git a/c/shaders/qmatmul_coop.spv b/c/shaders/qmatmul_coop.spv
index 1454a6d1..ad38a2be 100644
Binary files a/c/shaders/qmatmul_coop.spv and b/c/shaders/qmatmul_coop.spv differ
diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md
index 29dc4bce..e62d9c1b 100644
--- a/docs/ENVIRONMENT.md
+++ b/docs/ENVIRONMENT.md
@@ -215,7 +215,7 @@ Per-drive byte counts are reported in a `MIRROR:` stats line. Combine with `DIRE
| Variable | Default | Effect |
|---|---|---|
| `COLI_VULKAN` | off | Enable the Vulkan backend. Requires a `make VK=1` build. The GLM engine fails at startup (no silent fallback) if libvulkan or the compiled shaders are missing. The other engines (qwen36, qwen38, inkling, olmoe, deepseek_v41, deepseek_v4, kimi_k3, mimo, glm53, qwenimage) print one line and run on the CPU. What each one puts on the device: [vulkan.md](vulkan.md#the-other-engines). |
-| `COLI_VK_DEV` | unset | Select the primary Vulkan physical-device enumeration index. Without it, the backend prefers a discrete GPU, then integrated/virtual devices. |
+| `COLI_VK_DEV` | unset | The primary Vulkan device by its index in the enumeration (0, 1, ...: the order `vulkaninfo --summary` lists as GPU0, GPU1, ...), for a machine with two GPUs of the same kind. Without it, the backend prefers a discrete GPU, then integrated/virtual devices, and with two discrete GPUs takes the first. An index out of range is ignored with a `[VK] COLI_VK_DEV=... ignored` line. `[VK] ready:` names the device chosen. |
| `COLI_VK_SHADERS` | auto | Path to the compiled `qmatmul.spv` **or** the directory holding the `.spv` set; the other shaders are found next to it. Unset: `shaders/` next to the binary, then CWD-relative `shaders/qmatmul.spv`. |
| `COLI_VK_EXPERTS` | unset | Deprecated. The GLM engine's routed experts are on the shared expert tier (below), sized by its budget. `N`: the tier holds at most N experts (it was the size of a fixed top-N set, 320 by default); `0`: no tier, as `COLI_VK_TIER=0`. ~19 MB per int4 expert. |
| `COLI_VK_DENSE` | `0` in this engine | Run the resident dense matmuls (attention projections, shared expert) on the GPU. The other engines read the same variable with a default of on (see the expert tier below); all of them go through `coli_vk_dense_decide()` in the backend. |