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 branch 'feat/vk-kv-cold-device' into integ/release-batch-6

JustVugg3289bd650a9f
diff --cc c/tests/test_vk_chain.c
index 1328c374,2c18409e..0bad3629
--- a/c/tests/test_vk_chain.c
+++ b/c/tests/test_vk_chain.c
@@@ -2559,8 -2544,7 +2609,8 @@@ int main(int argc, char **argv)
printf("dsv4 rounding done\n");
}
if (!vkc_kvs_ready()) { fails++; printf("FAIL: the split KV shader did not load\n"); }
- else { test_kvs(); printf("kvs done\n"); }
+ else { test_kvs(); test_kvs_cold(); printf("kvs done\n"); }
+ if (getenv("COLI_VK_DEV2")) { test_dev2(spv); printf("dev2 done\n"); }
VkcStats st; vkc_stats(&st);
printf("chain: %llu frames, %llu ops, %llu matmuls (%llu GEMM), %llu barriers\n", st.frames, st.ops, st.matmuls, st.gemms, st.barriers);
vkc_shutdown();
diff --cc c/tests/vulkan_engines.sh
index 546777ce,d9b65504..b755a35b
--- a/c/tests/vulkan_engines.sh
+++ b/c/tests/vulkan_engines.sh
@@@ -4196,15 -4118,8 +4231,17 @@@ case "${1:-}" i
prefill-deepseek) family_prefill_deepseek ;;
kv-split) family_kv_split ;;
kv-split-sanitize) family_kv_split_sanitize ;;
+ kv-split-cold) family_kv_split_cold ;;
+ kv-split-cold-sanitize) family_kv_split_cold_sanitize ;;
+ layers-dev2) family_layers_dev2 ;;
+ layers-dev2-sanitize) family_layers_dev2_sanitize ;;
+ layers-dev2-mla) family_layers_dev2_mla ;;
+ layers-dev2-mla-sanitize) family_layers_dev2_mla_sanitize ;;
+ layers-dev2-deepseek) family_layers_dev2_deepseek ;;
+ layers-dev2-deepseek-sanitize) family_layers_dev2_deepseek_sanitize ;;
+ layers-dev2-*) e=${1#layers-dev2-} # one engine's gates (the engine already built)
+ declare -F "ld2_$e" >/dev/null || { echo "no layers-dev2 engine $e" >&2; exit 2; }
+ OMP_NUM_THREADS=2 "ld2_$e" ;;
kv-split-deepseek) family_kv_split_deepseek ;;
kv-split-deepseek-sanitize) family_kv_split_deepseek_sanitize ;;
staged) family_staged ;;
@@@ -4225,5 -4140,5 +4262,5 @@@
partial-*) g=${1#partial-}; fn=ptl_family_${g//-/_}
declare -F "$fn" >/dev/null || { echo "no partial-chain group ${g}" >&2; exit 2; }
"$fn" ;;
- *) echo "usage: $0 decide|decide-sanitize|staged|<family>-staged|shader|qwen|qwen-sanitize|inkling-olmoe|inkling-olmoe-sanitize|mimo-qwenimage|deepseek|deepseek-sanitize|kimi|kimi-mimo-sanitize|glm|glm-sanitize|qwen-chain|qwen-chain-sanitize|qwen-spec|qwen-spec-sanitize|mimo-chain|mimo-chain-sanitize|inkling-olmoe-chain|inkling-olmoe-chain-sanitize|glm-chain|glm-chain-sanitize|kimi-chain|kimi-chain-sanitize|deepseek-chain|deepseek-chain-sanitize|dense-only-<group>[-sanitize]|partial-<group>[-sanitize]|prefill-qwen|prefill-qwen-sanitize|prefill-inkling-olmoe|prefill-mimo-kimi|prefill-glm|prefill-deepseek|kv-split|kv-split-sanitize|layers-dev2[-mla|-deepseek][-sanitize]|layers-dev2-<engine>|kv-split-deepseek|kv-split-deepseek-sanitize|dev2|dev2-deepseek-kimi-mimo|dev2-sanitize" >&2; exit 2 ;;
- *) echo "usage: $0 decide|decide-sanitize|staged|<family>-staged|shader|qwen|qwen-sanitize|inkling-olmoe|inkling-olmoe-sanitize|mimo-qwenimage|deepseek|deepseek-sanitize|kimi|kimi-mimo-sanitize|glm|glm-sanitize|qwen-chain|qwen-chain-sanitize|qwen-spec|qwen-spec-sanitize|mimo-chain|mimo-chain-sanitize|inkling-olmoe-chain|inkling-olmoe-chain-sanitize|glm-chain|glm-chain-sanitize|kimi-chain|kimi-chain-sanitize|deepseek-chain|deepseek-chain-sanitize|dense-only-<group>[-sanitize]|partial-<group>[-sanitize]|prefill-qwen|prefill-qwen-sanitize|prefill-inkling-olmoe|prefill-mimo-kimi|prefill-glm|prefill-deepseek|kv-split|kv-split-sanitize|kv-split-cold|kv-split-cold-sanitize|kv-split-deepseek|kv-split-deepseek-sanitize" >&2; exit 2 ;;
++ *) echo "usage: $0 decide|decide-sanitize|staged|<family>-staged|shader|qwen|qwen-sanitize|inkling-olmoe|inkling-olmoe-sanitize|mimo-qwenimage|deepseek|deepseek-sanitize|kimi|kimi-mimo-sanitize|glm|glm-sanitize|qwen-chain|qwen-chain-sanitize|qwen-spec|qwen-spec-sanitize|mimo-chain|mimo-chain-sanitize|inkling-olmoe-chain|inkling-olmoe-chain-sanitize|glm-chain|glm-chain-sanitize|kimi-chain|kimi-chain-sanitize|deepseek-chain|deepseek-chain-sanitize|dense-only-<group>[-sanitize]|partial-<group>[-sanitize]|prefill-qwen|prefill-qwen-sanitize|prefill-inkling-olmoe|prefill-mimo-kimi|prefill-glm|prefill-deepseek|kv-split|kv-split-sanitize|kv-split-cold|kv-split-cold-sanitize|layers-dev2[-mla|-deepseek][-sanitize]|layers-dev2-<engine>|kv-split-deepseek|kv-split-deepseek-sanitize|dev2|dev2-deepseek-kimi-mimo|dev2-sanitize" >&2; exit 2 ;;
esac
diff --cc c/vk_chain.c
index b59f514f,7674e0a6..cb553f27
--- a/c/vk_chain.c
+++ b/c/vk_chain.c
@@@ -218,14 -196,15 +219,15 @@@ VkcBuf *vkc_buf(size_t bytes, int kind
}
static void buf_release(VkcBuf *b) {
+ if (b->kind == VKC_HOST) { coli_vk_host_buffer_free((void *)b->buf, (void *)b->mem, b->bytes); free(b); return; }
- VkcPool *P = &K.pool[b->kind];
- vkDestroyBuffer(K.dev, b->buf, NULL);
+ VkcPool *P = &KC.pool[b->kind];
+ vkDestroyBuffer(KC.dev, b->buf, NULL);
if (vka_free(&P->p, b->r)) {
VkcBlock *e = P->p.b[b->r.block].user;
- if (e) { if (e->map) vkUnmapMemory(K.dev, e->mem); vkFreeMemory(K.dev, e->mem, NULL); free(e); }
+ if (e) { if (e->map) vkUnmapMemory(KC.dev, e->mem); vkFreeMemory(KC.dev, e->mem, NULL); free(e); }
vka_pool_drop_block(&P->p, b->r.block);
}
- K.st.dev_bytes -= b->bytes;
+ KC.st.dev_bytes -= b->bytes;
free(b);
}
@@@ -260,6 -233,19 +262,21 @@@ int vkc_reserve(VkcBuf **b, size_t byte
}
void *vkc_ptr(const VkcBuf *b) { return b ? b->ptr : NULL; }
size_t vkc_bytes(const VkcBuf *b) { return b ? b->bytes : 0; }
+ VkcBuf *vkc_host(const void *ptr, size_t bytes, size_t *off) {
- if (!vkc_ready() || !ptr || !bytes) return NULL;
++ /* the primary device's import: on the second device's chain the host's part of a
++ * split stays on the CPU (vkc_kv_shadow says so) */
++ if (!vkc_ready() || !ptr || !bytes || g_kd) return NULL;
+ void *buf = NULL, *mem = NULL;
+ size_t o = 0;
+ if (!coli_vk_host_buffer(ptr, bytes, &buf, &mem, &o)) return NULL;
+ size_t al = coli_vk_import_alignment(), sz = (o + bytes + al - 1) / al * al; /* the pages, as imported */
+ VkcBuf *b = calloc(1, sizeof *b);
+ if (!b) { coli_vk_host_buffer_free(buf, mem, sz); return NULL; }
+ b->kind = VKC_HOST; b->buf = (VkBuffer)buf; b->mem = (VkDeviceMemory)mem;
+ b->bytes = sz;
+ *off = o;
+ return b;
+ }
/* ---- init ------------------------------------------------------------------------ */
static VkShaderModule load_module(const char *dir_spv, const char *file) {
@@@ -862,15 -838,46 +879,46 @@@ int vkc_attn_w(VkcBuf *q, VkcBuf *kc, V
if (w.a.sel_row > 0) x.a.sel_off += r0 * w.a.sel_row;
if (r0 && !attn_slice_next()) return 0;
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) {
- K.kind = PK_ATTN;
++ 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));
- K.st.attn_blocked += ok;
++ KC.st.attn_blocked += ok;
+ }
+ return ok;
+ }
+ int vkc_attn_part(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, const VkcAttnW *p, int blk, int anchor, int st_off) {
+ return attn_part_rec(q, kc, vc, o, p, blk, anchor, st_off, 0, 1, 0, 0);
+ }
+ 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) {
+ return ch > 0 && attn_part_rec(q, kc, vc, o, p, blk, anchor, st_off, ch, nz, o_z, st_z);
+ }
int vkc_dnconv(VkcBuf *in, VkcBuf *w, VkcBuf *ring, VkcBuf *out, VkcBuf *snap, const VkcDnConv *p) {
- K.kind = PK_DNCONV;
+ KC.kind = PK_DNCONV;
if (p->CK < 1 || p->CK > 9) return 0;
VkcBind bd[5] = {B(in, 0), B(w, 0), B(ring, 1), B(out, 1), B(snap, snap != NULL && snap != ring)};
if (snap == ring) bd[4].written = 0; /* the same buffer: tracked once, as written */
@@@ -1704,6 -1706,16 +1752,16 @@@ int vkc_kvs_merge(VkcBuf *dev, VkcBuf *
uint32_t gx, gy; grid((uint64_t)p->n, &gx, &gy);
return record(pipe, bd, 4, &pc, sizeof pc, gx, gy, 1);
}
+ int vkc_kvs_join(VkcBuf *parts, VkcBuf *out, const VkcKvsJoin *p) {
+ VkPipeline pipe = kvs_pipe();
+ if (!pipe || !parts || !out || p->n < 0 || p->d < 1 || p->nz < 0) return 0;
- if (p->n == 0) return open_frame() && !K.lost;
++ if (p->n == 0) return open_frame() && !KC.lost;
+ struct { int mode; VkcKvsJoin b; } pc = {5, *p};
- K.kind = PK_ATTN;
++ KC.kind = PK_ATTN;
+ VkcBind bd[4] = {B(parts, 0), B(NULL, 0), B(NULL, 0), B(out, 1)};
+ uint32_t gx, gy; grid((uint64_t)p->n, &gx, &gy);
+ return record(pipe, bd, 4, &pc, sizeof pc, gx, gy, 1);
+ }
int vkc_kvs_rel(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *out, VkcBuf *r, VkcBuf *tau, VkcBuf *relp, VkcBuf *tab,
const VkcKvsRel *p) {
VkPipeline pipe = kvs_pipe();
diff --cc docs/vulkan.md
index d48432dd,7bf7eae0..04adcb8d
--- a/docs/vulkan.md
+++ b/docs/vulkan.md
@@@ -2272,6 -2064,38 +2273,40 @@@ round trip is paid only on layers with
| `vkc_kvs_ds` | `chain_kvs` (mode 4) | DeepSeek's sparse attention with its sink over the window rows and the resident compressed rows; with every listed row resident, `vkc_dsv4_attn`'s bits |
| `vkc_kvs_merge` | `chain_kvs` (mode 2) | the two parts joined, a gate or V4's bf16 rounding after |
+ **The host's part on the device (`COLI_VK_KV_COLD=device`, off by default).** The
+ host's rows can also be attended on the device, so a step with a host part runs in one
+ frame with no round trip:
+ - **The copy.** Each split layer keeps a shadow of the host's rows in page-aligned memory
+ the device reads in place (an imported host allocation, `VK_EXT_external_memory_host`).
+ The shadow follows the host's cache row by row and is lowered with it on a rewind or a
+ rollback.
+ - **The ops.** `chain_kvs` reads the shadow with its COLD flag (the GQA, MLA and Inkling
+ forms; position t is row t). The prompt's rows take the blocked attention
+ (`chain_attnb`'s part mode) in chunks of 512 positions, one workgroup each, and mode 5
+ of `chain_kvs` joins them in order.
+ - **The bits.** The chunks are fixed by position, so a row's bits do not depend on how a
+ forward is cut into steps, as on the CPU path.
+ - **Where it applies.** It needs a device that imports host memory, no read-based pins
- and no staged uploads. DeepSeek's sparse forms keep the host's part on the CPU. Where
- it cannot run, the line says so and the CPU computes the host's part.
++ and no staged uploads. DeepSeek's sparse forms keep the host's part on the CPU, and so
++ does the second device's chain ([Layers on two devices](#layers-on-two-devices)): the
++ import is the primary's. Where it cannot run, the line says so and the CPU computes the
++ host's part.
+
+ The run's report adds `| the host's part on the device: N layer steps, R rows copied
+ to the shadow in T ms (M MiB held)`.
+
+ | Qwen3.6-35B-A3B, 7579-token prompt, Radeon 780M, the chain | first token after | 31 decode tokens |
+ |---|---|---|
+ | 1024 positions a layer on the device, the host's part on the CPU | 121 s | 3.5 s |
+ | the same, the host's part on the device | 114 s | 20 s |
+ | 4096 positions a layer, on the CPU | 143 s | |
+ | the same, on the device | 137 s | |
+
+ The prompt gains 4 to 6%. Decode is 5.7 times slower: with the default, the CPU
+ computes the host's part while the device runs its own, and on this integrated GPU the
+ decode attention is slower than the CPU's. Hence the default. A dedicated GPU, whose
+ attention outruns the CPU's by more, has not been measured.
+
`vk_kvsplit.h` carries the rest: the plan, the tables, the uploads and the stores of a
step's rows, the host's part (`vkc_kv_host_attn`, with the same bias, tau and V4
rounding), and one call per form for an engine (`vkc_kv_gqa`, `vkc_kv_mla`,