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

Merge #1934 into integ/release-batch-7

JustVugg550da7f84b7d
diff --cc c/backend_vulkan.c
index c06c4707,b85ebb20..01494c38
--- a/c/backend_vulkan.c
+++ b/c/backend_vulkan.c
@@@ -4537,69 -4474,6 +4538,70 @@@ int coli_vk_tensor_info(const ColiVkTen
}
/* A fence the chain waited on failed: the device is gone, everyone falls back. */
void coli_vk_mark_lost(void) { G.ready = 0; }
+
+/* ---- the dense chain on either device (vk_chain.c keeps a context per device) --------
+ * d = 0 is the primary device, what the calls without _dev answer; d = 1 is COLI_VK_DEV2's
+ * (G2), which until now held only routed experts. The chain's layers on it run the same
+ * shaders: the core below is G2's, with the primary's GEMM tiles (a tile is a speed
+ * choice; every tile computes the same bits). */
+int coli_vk_core_dev(int d, ColiVkCore *o) {
+ if (d == 0) return coli_vk_core(o);
+ if (d != 1 || !G2.ready || !G.ready || !o) return 0;
+ if (!coli_vk_core(o)) return 0; /* the tiles, the shader path: the primary's */
+ o->phys = (void *)G2.phys; o->device = (void *)G2.dev; o->queue = (void *)G2.queue; o->qfam = G2.qfam;
+ o->memtype_host = g_up[1].on ? g_up[1].mt_stage : G2.memtype;
+ o->memtype_cached = G2.memtype_cached; o->memtype_dev = G2.memtype_dev;
+ o->ssbo_align = G2.ssbo_align; o->ssbo_range = G2.ssbo_range;
+ o->has_prio = 0;
+ o->integrated = G2.integrated; o->shares_ram = G2.shares_ram;
++ o->coop_sg = 0; /* the second device's cooperative matrices are not probed: its chain keeps the plain shaders */
+ return 1;
+}
+int coli_vk_available_dev(int d) { return d == 1 ? G2.ready : G.ready; }
+void coli_vk_mark_lost_dev(int d) { if (d == 1) G2.ready = 0; else G.ready = 0; }
+int coli_vk_queue_submit_dev(int d, void *queue, const void *submit_info, void *fence) {
+ return (int)vk_submit(d == 1 ? 1 : 0, (VkQueue)queue, (const VkSubmitInfo *)submit_info, (VkFence)fence);
+}
+int coli_vk_tensor_info_dev(const ColiVkTensor *t, ColiVkTensorInfo *o, int *dev) {
+ if (!t || !o || t->dev < 0 || t->dev > 1) return 0;
+ o->wbuf = (void *)t->wbuf; o->sbuf = (void *)t->sbuf;
+ o->fmt = t->fmt; o->I = t->I; o->O = t->O; o->rowWords = t->rowWords; o->gs = t->gs;
+ if (dev) *dev = t->dev;
+ return 1;
+}
+int coli_vk_mem_budget_dev(int d, double *used_gb, double *budget_gb) {
+ return d == 1 ? coli_vk_mem_budget2(used_gb, budget_gb) : coli_vk_mem_budget(used_gb, budget_gb);
+}
+size_t coli_vk_device_used_dev(int d) { return (size_t)__atomic_load_n(&g_mem.used[d == 1 ? 1 : 0], __ATOMIC_RELAXED); }
+size_t coli_vk_device_local_bytes_dev(int d) {
+ if (d != 1) return coli_vk_device_local_bytes();
+ if (!G2.phys) return 0;
+ VkPhysicalDeviceMemoryProperties mp;
+ vkGetPhysicalDeviceMemoryProperties(G2.phys, &mp);
+ VkDeviceSize m = 0;
+ for (uint32_t i = 0; i < mp.memoryHeapCount; i++)
+ if ((mp.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) && mp.memoryHeaps[i].size > m) m = mp.memoryHeaps[i].size;
+ if (g_mem.cap[1] && g_mem.cap[1] < m) m = g_mem.cap[1]; /* COLI_VK_DEVICE_CAP_MB */
+ return (size_t)m;
+}
+size_t coli_vk_free_bytes_dev(int d) {
+ if (d != 1) return coli_vk_free_bytes();
+ if (!G2.phys) return 0;
+ if (g_mem.cap[1]) return (size_t)vk_mem_room(1);
+ double used = 0, bud = 0;
+ if (coli_vk_mem_budget2(&used, &bud)) return bud > used ? (size_t)((bud - used) * 1e9) : 0;
+ size_t dl = coli_vk_device_local_bytes_dev(1), u = coli_vk_device_used_dev(1);
+ return dl > u ? dl - u : 0;
+}
+void coli_vk_mem_info_dev(int d, size_t *used, size_t *count) {
+ if (d != 1) { coli_vk_mem_info(used, count); return; }
+ if (used) *used = (size_t)__atomic_load_n(&g_wpool2.bytes, __ATOMIC_RELAXED);
+ if (count) *count = (size_t)__atomic_load_n(&g_wpool2.tensors, __ATOMIC_RELAXED);
+}
+size_t coli_vk_buffer_alignment_dev(int d) {
+ if (d != 1) return coli_vk_buffer_alignment();
+ return G2.buf_align ? G2.buf_align : 256;
+}
/* The chain's submits: through the lock the staged uploader takes on a shared queue. */
int coli_vk_queue_submit(void *queue, const void *submit_info, void *fence) {
return (int)vk_submit(0, (VkQueue)queue, (const VkSubmitInfo *)submit_info, (VkFence)fence);
diff --cc c/vk_chain.c
index cb553f27,c69c3550..7fa357eb
--- a/c/vk_chain.c
+++ b/c/vk_chain.c
@@@ -360,14 -317,17 +363,17 @@@ int vkc_init(void)
f = fopen(path, "rb");
}
if (f) fclose(f);
- if (f && (K.mmod[i] = load_module(K.core.spv_path, mla_file[i]))) K.mpipe[i] = make_pipe(K.mmod[i], NULL);
+ if (f && (KC.mmod[i] = load_module(KC.core.spv_path, mla_file[i]))) KC.mpipe[i] = make_pipe(KC.mmod[i], NULL);
}
- K.mla_ok = K.mpipe[PM_MLA] && K.mpipe[PM_HGEMV] && K.mpipe[PM_DSA];
+ KC.mla_ok = KC.mpipe[PM_MLA] && KC.mpipe[PM_HGEMV] && KC.mpipe[PM_DSA];
+ /* the attention core on the matrix units (vkc_attn_flash_rows): subgroups of 64 with
+ * cooperative matrices, the shader's tiling assumes four of them */
- if (K.core.coop_sg == 64) K.mod_fa = load_module(K.core.spv_path, "chain_attn_flash.spv");
++ if (KC.core.coop_sg == 64) KC.mod_fa = load_module(KC.core.spv_path, "chain_attn_flash.spv");
dsv4_init();
/* the fp32 tiled GEMM at the backend's tiles; none = every S on the GEMV */
- K.mod_gemm = K.core.gemm_tiles ? load_module(K.core.spv_path, "qmatmul_gemm.spv") : VK_NULL_HANDLE;
- for (int k = 0; K.mod_gemm && k < K.core.gemm_tiles && k < VKC_GEMM_MAX; k++) {
- const int *t = K.core.gemm_tile[k];
+ KC.mod_gemm = KC.core.gemm_tiles ? load_module(KC.core.spv_path, "qmatmul_gemm.spv") : VK_NULL_HANDLE;
+ for (int k = 0; KC.mod_gemm && k < KC.core.gemm_tiles && k < VKC_GEMM_MAX; k++) {
+ const int *t = KC.core.gemm_tile[k];
int32_t sv[7] = {t[0], t[1], t[2], t[3], t[4], (t[0] / t[3]) * (t[1] / t[4]), t[5] ? 1 : 0};
VkSpecializationMapEntry me[7];
for (int i = 0; i < 7; i++) me[i] = (VkSpecializationMapEntry){(uint32_t)i, (uint32_t)(i * 4), 4};
@@@ -852,9 -802,41 +858,41 @@@ static int attn_slice_rows(int S, int p
return rr >= S ? S : (int)rr;
}
static int attn_slice_next(void) { /* the open frame out, a new one in */
- K.st.attn_slices++;
+ KC.st.attn_slices++;
return vkc_submit(0) && vkc_begin();
}
+ /* chain_attn_flash.comp's pipeline for this head dim and grouping, made on first use;
+ * NULL when the shader is missing or the pipeline fails */
+ static VkPipeline fa_pipe(int hd, int gq) {
- for (int i = 0; i < K.nfa; i++) if (K.fa_hd[i] == hd && K.fa_gq[i] == gq) return K.fa[i];
- if (!K.mod_fa || K.nfa == 4) return VK_NULL_HANDLE;
++ for (int i = 0; i < KC.nfa; i++) if (KC.fa_hd[i] == hd && KC.fa_gq[i] == gq) return KC.fa[i];
++ if (!KC.mod_fa || KC.nfa == 4) return VK_NULL_HANDLE;
+ int32_t sv[2] = {hd, gq};
+ VkSpecializationMapEntry me[2] = {{0, 0, 4}, {1, 4, 4}};
+ VkSpecializationInfo si = {2, me, sizeof sv, sv};
+ VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT rss = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO_EXT,
+ .requiredSubgroupSize = 64};
+ VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
+ .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pNext = &rss,
+ .flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT,
- .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = K.mod_fa, .pName = "main",
- .pSpecializationInfo = &si}, .layout = K.pl};
++ .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = KC.mod_fa, .pName = "main",
++ .pSpecializationInfo = &si}, .layout = KC.pl};
+ VkPipeline p = VK_NULL_HANDLE;
- if (vkCreateComputePipelines(K.dev, VK_NULL_HANDLE, 1, &ci, NULL, &p) != VK_SUCCESS) {
- vkDestroyShaderModule(K.dev, K.mod_fa, NULL); K.mod_fa = VK_NULL_HANDLE; /* not again */
++ if (vkCreateComputePipelines(KC.dev, VK_NULL_HANDLE, 1, &ci, NULL, &p) != VK_SUCCESS) {
++ vkDestroyShaderModule(KC.dev, KC.mod_fa, NULL); KC.mod_fa = VK_NULL_HANDLE; /* not again */
+ return VK_NULL_HANDLE;
+ }
- K.fa[K.nfa] = p; K.fa_hd[K.nfa] = hd; K.fa_gq[K.nfa++] = gq;
++ KC.fa[KC.nfa] = p; KC.fa_hd[KC.nfa] = hd; KC.fa_gq[KC.nfa++] = gq;
+ return p;
+ }
+ /* From COLI_VK_CHAIN_FLASH rows (16; 0 = never) a plain causal attention (no list,
+ * window, ring, sink or second V width, head dim a multiple of 64) takes
+ * chain_attn_flash where the device has it, ahead of the blocked shader. */
+ int vkc_attn_flash_rows(void) {
+ const char *e = getenv("COLI_VK_CHAIN_FLASH");
+ int v = e && *e ? atoi(e) : 16;
+ return v < 0 ? 0 : v;
+ }
int vkc_attn_block_rows(void) {
const char *e = getenv("COLI_VK_ATTN_BLOCK");
int v = e && *e ? atoi(e) : 16;
@@@ -867,9 -849,14 +905,14 @@@ int vkc_attn_w(VkcBuf *q, VkcBuf *kc, V
if (w.vd <= 0) w.vd = w.a.hd;
if (w.a.hd > 256 || w.vd > 256 || w.a.H % w.a.KVH || w.win < 0 || w.ring < 0 || (w.sink && !snk)) return 0;
VkcBind bd[7] = {B(q, 0), B(kc, 0), B(vc, 0), B(o, 1), B(gate, 0), B(sel, 0), B(snk, 0)};
- int G = w.a.H / w.a.KVH, blk = vkc_attn_block_rows();
- int blocked = blk > 0 && w.a.S >= blk && G <= 32 && kab_pipe(), br = w.a.sel_row > 0 ? 1 : 32 / G;
- int S = w.a.S, rr = attn_slice_rows(S, w.a.pos_base, (double)w.a.H * (w.a.hd > w.vd ? w.a.hd : w.vd), blocked ? br : 1);
+ int G = w.a.H / w.a.KVH, blk = vkc_attn_block_rows(), fl = vkc_attn_flash_rows();
+ /* a prompt chunk's plain causal attention on the matrix units (chain_attn_flash),
+ * 16 / G rows a workgroup; else the blocked shader, else chain_attn */
- VkPipeline fp = K.mod_fa && fl > 0 && w.a.S >= fl && !(sel && w.a.sel_row > 0) && !w.win && !w.ring && !w.kv_pm &&
++ VkPipeline fp = KC.mod_fa && fl > 0 && w.a.S >= fl && !(sel && w.a.sel_row > 0) && !w.win && !w.ring && !w.kv_pm &&
+ !w.sink && w.vd == w.a.hd && w.a.hd % 64 == 0 && 16 % G == 0 ? fa_pipe(w.a.hd, G) : VK_NULL_HANDLE;
+ int blocked = !fp && blk > 0 && w.a.S >= blk && G <= 32 && kab_pipe(), br = w.a.sel_row > 0 ? 1 : 32 / G;
+ int S = w.a.S, rr = attn_slice_rows(S, w.a.pos_base, (double)w.a.H * (w.a.hd > w.vd ? w.a.hd : w.vd),
+ fp ? 16 / G : blocked ? br : 1);
int ok = 1;
for (int r0 = 0; ok && r0 < S; r0 += rr) {
VkcAttnW x = w;
@@@ -878,35 -865,14 +921,38 @@@
x.a.q_off += r0 * w.a.q_row; x.a.g_off += r0 * w.a.g_row; x.a.o_off += r0 * w.a.o_row;
if (w.a.sel_row > 0) x.a.sel_off += r0 * w.a.sel_row;
if (r0 && !attn_slice_next()) return 0;
- if (blocked) {
+ if (fp) {
+ ok = record(fp, bd, 7, &x, sizeof x, (uint32_t)((x.a.S + 16 / G - 1) / (16 / G)), (uint32_t)x.a.KVH, 1);
- K.st.attn_flash += ok;
++ KC.st.attn_flash += ok;
+ } else if (blocked) {
- struct { VkcAttnW w; int br; } pc = {x, br};
+ struct { VkcAttnW w; int br, B, anchor, part, st_off; } pc = {x, br, 0, 0, 0, 0};
ok = record(KAB.pipe, bd, 7, &pc, sizeof pc, (uint32_t)x.a.KVH, (uint32_t)((x.a.S + br - 1) / br), 1);
- K.st.attn_blocked += ok;
- } else ok = record(K.pipe[P_ATTN], bd, 7, &x, sizeof x, (uint32_t)x.a.H, (uint32_t)x.a.S, 1);
+ KC.st.attn_blocked += ok;
+ } else ok = record(KC.pipe[P_ATTN], bd, 7, &x, sizeof x, (uint32_t)x.a.H, (uint32_t)x.a.S, 1);
+ }
+ return ok;
+}
+static int attn_part_rec(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, const VkcAttnW *p, int blk, int anchor, int st_off,
+ int ch, int nz, int o_z, int st_z) {
+ KC.kind = PK_ATTN;
+ VkcAttnW w = *p;
+ if (w.vd <= 0) w.vd = w.a.hd;
+ int G = w.a.KVH > 0 ? w.a.H / w.a.KVH : 0;
+ if (w.a.hd > 256 || w.vd > 256 || G < 1 || G > 32 || w.a.H % w.a.KVH || w.a.sel_row > 0 || w.sink || w.ring || blk < 1 ||
+ anchor < 0 || ch < 0 || (ch > 0 && (ch % 16 || nz < 1 || nz > 65535)) || !kab_pipe()) return 0;
+ w.a.has_gate = 0; w.a.g_off = ch; w.a.g_row = o_z; w.a.g_seg = st_z; /* the chunks (unused by part otherwise) */
+ VkcBind bd[7] = {B(q, 0), B(kc, 0), B(vc, 0), B(o, 1), B(NULL, 0), B(NULL, 0), B(NULL, 0)};
+ int br = 32 / G, S = w.a.S, rr = attn_slice_rows(S, w.a.pos_base, (double)w.a.H * (w.a.hd > w.vd ? w.a.hd : w.vd), br);
+ int ok = 1;
+ for (int r0 = 0; ok && r0 < S; r0 += rr) {
+ VkcAttnW x = w;
+ int n = S - r0 < rr ? S - r0 : rr;
+ x.a.S = n; x.a.pos_base = w.a.pos_base + r0;
+ x.a.q_off += r0 * w.a.q_row; x.a.o_off += r0 * w.a.o_row;
+ if (r0 && !attn_slice_next()) return 0;
+ struct { VkcAttnW w; int br, B, anchor, part, st_off; } pc = {x, br, blk, anchor, 1, st_off + r0 * w.a.H * 2};
+ ok = record(KAB.pipe, bd, 7, &pc, sizeof pc, (uint32_t)x.a.KVH, (uint32_t)((x.a.S + br - 1) / br), (uint32_t)(ch > 0 ? nz : 1));
+ KC.st.attn_blocked += ok;
}
return ok;
}
@@@ -1564,26 -1520,28 +1610,28 @@@ void vkc_shutdown(void)
kab_shutdown();
ke_shutdown();
for (int i = 0; i < P_NPIPE; i++) {
- if (K.pipe[i]) vkDestroyPipeline(K.dev, K.pipe[i], NULL);
- if (K.mod[i]) vkDestroyShaderModule(K.dev, K.mod[i], NULL);
+ if (KC.pipe[i]) vkDestroyPipeline(KC.dev, KC.pipe[i], NULL);
+ if (KC.mod[i]) vkDestroyShaderModule(KC.dev, KC.mod[i], NULL);
}
- for (int i = 0; i < K.nkdarec; i++) vkDestroyPipeline(K.dev, K.kdarec[i], NULL);
+ for (int i = 0; i < KC.nkdarec; i++) vkDestroyPipeline(KC.dev, KC.kdarec[i], NULL);
for (int i = 0; i < PM_N; i++) {
- if (K.mpipe[i]) vkDestroyPipeline(K.dev, K.mpipe[i], NULL);
- if (K.mmod[i]) vkDestroyShaderModule(K.dev, K.mmod[i], NULL);
+ if (KC.mpipe[i]) vkDestroyPipeline(KC.dev, KC.mpipe[i], NULL);
+ if (KC.mmod[i]) vkDestroyShaderModule(KC.dev, KC.mmod[i], NULL);
}
- for (int i = 0; i < K.ngemm; i++) vkDestroyPipeline(K.dev, K.gemm[i], NULL);
- for (int i = 0; i < K.ndnrec; i++) vkDestroyPipeline(K.dev, K.dnrec[i], NULL);
- if (K.gemv4) vkDestroyPipeline(K.dev, K.gemv4, NULL);
- if (K.mod_gemv4) vkDestroyShaderModule(K.dev, K.mod_gemv4, NULL);
- if (K.mod_gemm) vkDestroyShaderModule(K.dev, K.mod_gemm, NULL);
- if (K.mod_dnrec) vkDestroyShaderModule(K.dev, K.mod_dnrec, NULL);
- for (int i = 0; i < K.nfa; i++) vkDestroyPipeline(K.dev, K.fa[i], NULL);
- if (K.mod_fa) vkDestroyShaderModule(K.dev, K.mod_fa, NULL);
- if (K.pl) vkDestroyPipelineLayout(K.dev, K.pl, NULL);
- if (K.dsl) vkDestroyDescriptorSetLayout(K.dev, K.dsl, NULL);
+ for (int i = 0; i < KC.ngemm; i++) vkDestroyPipeline(KC.dev, KC.gemm[i], NULL);
+ for (int i = 0; i < KC.ndnrec; i++) vkDestroyPipeline(KC.dev, KC.dnrec[i], NULL);
+ if (KC.gemv4) vkDestroyPipeline(KC.dev, KC.gemv4, NULL);
+ if (KC.mod_gemv4) vkDestroyShaderModule(KC.dev, KC.mod_gemv4, NULL);
+ if (KC.mod_gemm) vkDestroyShaderModule(KC.dev, KC.mod_gemm, NULL);
+ if (KC.mod_dnrec) vkDestroyShaderModule(KC.dev, KC.mod_dnrec, NULL);
++ for (int i = 0; i < KC.nfa; i++) vkDestroyPipeline(KC.dev, KC.fa[i], NULL);
++ if (KC.mod_fa) vkDestroyShaderModule(KC.dev, KC.mod_fa, NULL);
+ if (KC.pl) vkDestroyPipelineLayout(KC.dev, KC.pl, NULL);
+ if (KC.dsl) vkDestroyDescriptorSetLayout(KC.dev, KC.dsl, NULL);
dsv4_shutdown();
kvs_shutdown();
- memset(&K, 0, sizeof K);
- K.cur = -1; K.gemm_rows = -1;
+ memset(&KC, 0, sizeof KC);
+ KC.cur = -1; KC.gemm_rows = -1;
}
/* ---- DeepSeek V4.1 Flash and DeepSeek V4 attention (chain_dsv4.comp) ------------------
diff --cc c/vk_chain.h
index 56ef5458,39f4e42e..26ab935d
--- a/c/vk_chain.h
+++ b/c/vk_chain.h
@@@ -124,23 -112,7 +124,24 @@@ int vkc_attn_w(VkcBuf *q, VkcBuf *kc,
* chain_attnb.comp: a workgroup per KV head and block of rows, each K and V row read once
* per block instead of once per (head, row); below it chain_attn as before. */
int vkc_attn_block_rows(void);
+/* chain_attnb's part mode (vk_kvsplit.h, the host's part on the device): each row's causal
+ * positions before its share of a split cache, [first, (pos/B - anchor)*B), from a cache
+ * in the host's head-major layout (p->a.cap positions a KV head), the value sum
+ * unnormalized at o_off + s*o_row + h*vd and (m, l) at st_off + (s*H + h)*2. Blocked for
+ * any S, its tiles at multiples of their size: a row's bits do not depend on the step
+ * around it. No lists, sink or ring; 0 without the blocked pipeline. */
+int vkc_attn_part(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, const VkcAttnW *p, int blk, int anchor, int st_off);
+/* The same over chunks of `ch` positions (a multiple of 16), nz of them, chunk z's part at
+ * o_off + z*o_z + s*o_row + h*vd and (m, l) at st_off + z*st_z + (s*H + h)*2 (vkc_kvs_join
+ * joins them): a decode row's positions over many workgroups. */
+int vkc_attn_part_chunks(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, const VkcAttnW *p, int blk, int anchor, int st_off,
+ int ch, int nz, int o_z, int st_z);
+/* chain_kvs.comp mode 5: the chunks of a part joined in order (vkc_attn_part_chunks): segment
+ * g of d floats, chunk z's sum at a_off + z*a_z + g*d and (m, l) at sa_off + z*sa_z + 2g of
+ * parts, into out at o_off + g*d and so_off + 2g. */
+typedef struct { int n, d, nz, a_off, a_z, sa_off, sa_z, o_off, so_off; } VkcKvsJoin;
+int vkc_kvs_join(VkcBuf *parts, VkcBuf *out, const VkcKvsJoin *p);
+ int vkc_attn_flash_rows(void); /* COLI_VK_CHAIN_FLASH: rows from which chain_attn_flash runs (0 = never) */
/* The attention ops (vkc_attn, vkc_attn_w, vkc_mla_core, vkc_relattn) whose rows x positions
* x heads x head dim pass COLI_VK_ATTN_SLICE (2^32; 0 = never) record their rows in slices,
* each ending its frame (submitted, not waited for) and the next in a new one: no single