Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
Merge #1935 into integ/release-batch-7
JustVugg3269520802c3
diff --cc c/tests/test_vk_chain.cindex 843c1732,80dfdbdc..60feaa14--- a/c/tests/test_vk_chain.c+++ b/c/tests/test_vk_chain.c@@@ -102,8 -102,7 +102,9 @@@ static float *down(VkcBuf *b, size_t of/* ---- matmul ----------------------------------------------------------------------- */static uint16_t f2bf(float f) { uint32_t u; memcpy(&u, &f, 4); return (uint16_t)((u + 0x7fff + ((u >> 16) & 1)) >> 16); }static float bf2f(uint16_t h) { uint32_t u = (uint32_t)h << 16; float f; memcpy(&f, &u, 4); return f; }+static int g_test_dev; /* the device test_matmul uploads to (the chain's current one) */+static float *g_mm_out; /* test_matmul's output, kept when set to non-NULL (the two devices' bits) */+ static double g_mm_tol = 2e-4; /* chain_gemm.comp's cases: x rounded to f16 */static void test_matmul(int fmt, int S, int I, int O, size_t xo, size_t yo) {float *x = fvec((size_t)S * I, 1.f), *W = malloc((size_t)O * I * sizeof(float));void *codes = NULL; float *sc = NULL; int gs = 0;@@@ -143,8 -141,7 +144,8 @@@vkc_submit(1);float *y = (float *)vkc_ptr(yb) + yo;double e = relerr(y, ref, (size_t)S * O, 1e-3);- CHECK(ok && e < 2e-4 && !bad(y, (size_t)S * O), "matmul fmt %d S %d I %d O %d xo %zu yo %zu: ok %d err %.2e", fmt, S, I, O, xo, yo, ok, e);+ CHECK(ok && e < g_mm_tol && !bad(y, (size_t)S * O), "matmul fmt %d S %d I %d O %d xo %zu yo %zu: ok %d err %.2e", fmt, S, I, O, xo, yo, ok, e);+ if (g_mm_out) memcpy(g_mm_out, y, (size_t)S * O * sizeof(float));vkc_free(xb); vkc_free(yb); coli_vk_tensor_free(t);free(x); free(W); free(codes); free(sc); free(ref);}diff --cc c/vk_chain.cindex 7fa357eb,ac728c0b..0c50e273--- a/c/vk_chain.c+++ b/c/vk_chain.c@@@ -100,10 -97,9 +100,12 @@@ typedef structVkPipeline gemm[VKC_GEMM_MAX]; int gemm_bm[VKC_GEMM_MAX], gemm_bn[VKC_GEMM_MAX], ngemm;VkPipeline dnrec[VKC_DNREC_MAX]; int dnrec_kd[VKC_DNREC_MAX], ndnrec;VkShaderModule mod_gemv4; VkPipeline gemv4; int gemv4_xs; /* chain_gemv.comp: the vectorized decode GEMV */+ /* chain_gemm.comp: int8/int4 prompt GEMMs, x rounded to f16 once (COLI_VK_CHAIN_GEMM) */+ VkShaderModule mod_tg; VkPipeline tg;VkShaderModule mmod[PM_N]; VkPipeline mpipe[PM_N]; int mla_ok; /* chain_mla, chain_hgemv, chain_dsa */+ /* chain_attn_flash.comp: the attention core on the matrix units for prompt chunks,+ * one pipeline per (head dim, query heads per kv head) */+ VkShaderModule mod_fa; VkPipeline fa[4]; int fa_hd[4], fa_gq[4], nfa;VkPipeline kdarec[VKC_KDA_MAX]; int kdarec_kd[VKC_KDA_MAX], nkdarec; /* chain_kda's recurrence per key dim */VkCommandPool cpool;VkcFrame fr[VKC_FRAMES];@@@ -363,17 -316,33 +365,36 @@@ 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 (KC.core.coop_sg == 64) KC.mod_fa = load_module(KC.core.spv_path, "chain_attn_flash.spv");dsv4_init();+ /* the int8/int4 prompt GEMM on the matrix units (chain_gemm.comp, subgroups of 64);+ * COLI_VK_CHAIN_GEMM=0 leaves those formats to the GEMMs below */+ {+ const char *e = getenv("COLI_VK_CHAIN_GEMM");- if (K.core.coop_sg == 64 && !(e && *e == '0') && (K.mod_tg = load_module(K.core.spv_path, "chain_gemm.spv"))) {++ if (KC.core.coop_sg == 64 && !(e && *e == '0') && (KC.mod_tg = load_module(KC.core.spv_path, "chain_gemm.spv"))) {+ int32_t tt = 4;+ VkSpecializationMapEntry me = {0, 0, 4};+ VkSpecializationInfo si = {1, &me, 4, &tt};+ 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_tg, .pName = "main",- .pSpecializationInfo = &si}, .layout = K.pl};- if (vkCreateComputePipelines(K.dev, VK_NULL_HANDLE, 1, &ci, NULL, &K.tg) != VK_SUCCESS) K.tg = VK_NULL_HANDLE;++ .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = KC.mod_tg, .pName = "main",++ .pSpecializationInfo = &si}, .layout = KC.pl};++ if (vkCreateComputePipelines(KC.dev, VK_NULL_HANDLE, 1, &ci, NULL, &KC.tg) != VK_SUCCESS) KC.tg = VK_NULL_HANDLE;+ }+ }/* 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};@@@ -754,13 -714,18 +775,18 @@@ static int matmul_aligned(const ColiVkTstruct { int fmt, S, I, O, rowWords, gs, lpr; } pc7 = {ti->fmt, S, ti->I, ti->O, ti->rowWords, ti->gs, lpr};int rows_wg = 256 / lpr; /* at least, at subgroups of 64 */int wg = (ti->O + rows_wg - 1) / rows_wg; if (wg > 1024) wg = 1024;- ok = record(K.gemv4, bd, 4, &pc7, sizeof pc7, (uint32_t)wg, (uint32_t)S, 1);+ ok = record(KC.gemv4, bd, 4, &pc7, sizeof pc7, (uint32_t)wg, (uint32_t)S, 1);}- else if (path >= 0 && K.tg && (ti->fmt == 1 || ti->fmt == 2 || (ti->fmt == 4 && ti->gs >= 8 && ti->gs % 8 == 0)) &&++ else if (path >= 0 && KC.tg && (ti->fmt == 1 || ti->fmt == 2 || (ti->fmt == 4 && ti->gs >= 8 && ti->gs % 8 == 0)) &&+ ti->I % 64 == 0 && ti->O % 128 == 0 && ti->rowWords % 4 == 0) {- ok = record(K.tg, bd, 4, &pc, sizeof pc, (uint32_t)((S + 63) / 64), (uint32_t)(ti->O / 128), 1);- K.st.tile_gemms += ok;++ ok = record(KC.tg, bd, 4, &pc, sizeof pc, (uint32_t)((S + 63) / 64), (uint32_t)(ti->O / 128), 1);++ KC.st.tile_gemms += ok;+ }else if (path >= 0)- ok = record(K.gemm[path], bd, 4, &pc, sizeof pc, (uint32_t)((ti->O + K.gemm_bm[path] - 1) / K.gemm_bm[path]),- (uint32_t)((S + K.gemm_bn[path] - 1) / K.gemm_bn[path]), 1);- else ok = record(K.pipe[P_GEMV], bd, 4, &pc, sizeof pc, (uint32_t)((ti->O + 7) / 8), (uint32_t)S, 1);- K.st.matmuls += ok; K.st.gemms += ok && path >= 0;+ ok = record(KC.gemm[path], bd, 4, &pc, sizeof pc, (uint32_t)((ti->O + KC.gemm_bm[path] - 1) / KC.gemm_bm[path]),+ (uint32_t)((S + KC.gemm_bn[path] - 1) / KC.gemm_bn[path]), 1);+ else ok = record(KC.pipe[P_GEMV], bd, 4, &pc, sizeof pc, (uint32_t)((ti->O + 7) / 8), (uint32_t)S, 1);+ KC.st.matmuls += ok; KC.st.gemms += ok && path >= 0;return ok;}int vkc_matmul(ColiVkTensor *t, VkcBuf *x, size_t xo, VkcBuf *y, size_t yo, int S) {@@@ -1610,28 -1500,28 +1636,30 @@@ 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);- if (K.tg) vkDestroyPipeline(K.dev, K.tg, NULL);- if (K.mod_tg) vkDestroyShaderModule(K.dev, K.mod_tg, 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);- 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);++ if (KC.tg) vkDestroyPipeline(KC.dev, KC.tg, NULL);++ if (KC.mod_tg) vkDestroyShaderModule(KC.dev, KC.mod_tg, 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.hindex 26ab935d,1eb3b7b7..95c58069--- a/c/vk_chain.h+++ b/c/vk_chain.h@@@ -676,8 -645,8 +676,9 @@@ typedef structdouble wait_ms; /* host time blocked in fence waits */size_t dev_bytes; /* live chain buffers */unsigned long long attn_blocked; /* attention calls through chain_attnb (blocks of rows) */+ unsigned long long attn_flash; /* attention calls through chain_attn_flash (matrix units) */unsigned long long attn_slices; /* extra submissions that cut a long attention (COLI_VK_ATTN_SLICE) */+ unsigned long long tile_gemms; /* prompt matmuls through chain_gemm.comp (matrix units) */} VkcStats;void vkc_stats(VkcStats *st);/* COLI_VK_CHAIN_PROF=1: one stderr line of device time per kind of op */