Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
vulkan: VK_PROF phase split for dev2 issue + worker/join timers
Steve Markgraf3cba6ce121f5
diff --git a/c/backend_vulkan.c b/c/backend_vulkan.cindex c5f6bed7..a3de36dd 100644--- a/c/backend_vulkan.c+++ b/c/backend_vulkan.c@@ -1050,9 +1050,15 @@ static int eg2_prepare_submit(ColiVkTensor *const *gates, ColiVkTensor *const *udowns[c]->I != I || downs[c]->O != D || downs[c]->fmt != dfmt) return 0;}size_t xb = (size_t)total*D*4, hb = (size_t)total*I*4, yb = (size_t)total*D*4;+ /* VK_PROF=1: phase split of the dev2 issue cost (same scheme as the dense path) —+ * localizes the per-block tax between our copy, descriptors, recording and the+ * driver's submit on the chipset-x4 Polaris path. */+ static double q_x, q_desc, q_rec, q_sub; static long q_n;+ double t0 = G.eg_prof ? vk_now() : 0, tA;if (!scratch_reserve_d2(&G2.x, xb, G2.memtype) || !scratch_reserve_d2(&G2.h, hb, G2.memtype) ||!scratch_reserve_d2(&G2.y, yb, G2.memtype_cached)) return 0;memcpy(G2.x.ptr, x, xb);+ if (G.eg_prof) { tA = vk_now(); q_x += tA - t0; t0 = tA; }if (!G2.pool) {VkDescriptorPoolSize ps = {.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = 64*6 + 64*4};VkDescriptorPoolCreateInfo dpi = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 128, .poolSizeCount = 1, .pPoolSizes = &ps};@@ -1077,6 +1083,7 @@ static int eg2_prepare_submit(ColiVkTensor *const *gates, ColiVkTensor *const *u{downs[c]->sbuf, 0, VK_WHOLE_SIZE}, {G2.y.buf, yo, (VkDeviceSize)rows[c]*D*4}};wr_desc_dev(G2.dev, G2.dn[c], 4, di);}+ if (G.eg_prof) { tA = vk_now(); q_desc += tA - t0; t0 = tA; }VKCHECK(vkResetCommandBuffer(G2.cmd, 0), "d2 eg resetCmd");VkCommandBufferBeginInfo begin = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};VKCHECK(vkBeginCommandBuffer(G2.cmd, &begin), "d2 eg beginCmd");@@ -1097,9 +1104,15 @@ static int eg2_prepare_submit(ColiVkTensor *const *gates, ColiVkTensor *const *uvkCmdDispatch(G2.cmd, (uint32_t)((D + 7) / 8), (uint32_t)rows[c], 1);}VKCHECK(vkEndCommandBuffer(G2.cmd), "d2 eg endCmd");+ if (G.eg_prof) { tA = vk_now(); q_rec += tA - t0; t0 = tA; }VkSubmitInfo si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, .pCommandBuffers = &G2.cmd};VKCHECK(vkResetFences(G2.dev, 1, &G2.fence), "d2 eg resetFence");VKCHECK(vkQueueSubmit(G2.queue, 1, &si, G2.fence), "d2 eg queueSubmit");+ if (G.eg_prof) { tA = vk_now(); q_sub += tA - t0;+ if ((++q_n & 2047) == 0)+ fprintf(stderr, "[VK_PROF d2iss] n=%ld | memcpy_x %.0f | desc %.0f | record %.0f | submit %.0f ms\n",+ q_n, q_x, q_desc, q_rec, q_sub);+ }G2.pending_yb = yb; G2.inflight = 1;return 1;}diff --git a/c/colibri.c b/c/colibri.cindex 9ba34e53..892e34eb 100644--- a/c/colibri.c+++ b/c/colibri.c@@ -317,7 +317,7 @@ static int g_vk_budget2; /* COLI_VK_EXPERTS2: dev2 expert-tier cap (with COstatic int g_vk_reg_n2; /* experts resident on the dev2 tier *//* PROF anatomy of the VK expert block (master-thread accumulated in moe(), printed by* profile_print): where a decode block's wall goes besides t_ecpu/t_ewait/t_egpu. */-static double g_vkb_cls, g_vkb_issue, g_vkb_acc;+static double g_vkb_cls, g_vkb_issue, g_vkb_acc, g_vkb_wrk, g_vkb_join;static int64_t g_vkb_blocks, g_vkb_nvk, g_vkb_nvk2, g_vkb_ncpu;static int g_vk_attn; /* COLI_VK_ATTN=1: run the MLA absorb attention core on Vulkan* (mirrors COLI_CUDA_ATTN; KV latent cache mirrored on-device) */@@ -3055,10 +3055,12 @@ static void attention(Model *m, Layer *l, int layer, float *x, int S, int pos_ba* dev0-only) while the 580's exec itself finishes early (take-wait 0.1s) — off-thread* it overlaps dev0's issue + the CPU expert share. All Vulkan state it touches is* G2-private, and moe() joins before take2, preserving the one-in-flight invariant. */-typedef struct { ColiVkTensor **g,**u,**d; const int *rows; int n; const float *x; int rc; } Vk2Iss;+typedef struct { ColiVkTensor **g,**u,**d; const int *rows; int n; const float *x; int rc; double dt; } Vk2Iss;static void *vk2_issue_worker(void *p){Vk2Iss *j=(Vk2Iss*)p;+ double t0=now_s();j->rc = coli_vk_expert_group_issue2(j->g,j->u,j->d,j->rows,j->n,j->x);+ j->dt = now_s()-t0;return NULL;}#endif@@ -3801,7 +3803,7 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out, int/* issue the SLOWER device first so it gets the longest overlap window;* dev2's submit runs on a worker thread (joined before take2 below) so its* per-block cost overlaps dev0 issue + the CPU share instead of serializing */- Vk2Iss iss2 = { vg2, vu2, vd2, vrows2, nvk2, vk_xh2, 0 };+ Vk2Iss iss2 = { vg2, vu2, vd2, vrows2, nvk2, vk_xh2, 0, 0 };pthread_t iss2_th; int iss2_threaded=0;if(nvk2>0){if(pthread_create(&iss2_th,NULL,vk2_issue_worker,&iss2)==0) iss2_threaded=1;@@ -3863,7 +3865,8 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out, int}/* dev2 group: taken AFTER dev0's accumulate so the slower card gets the* extra overlap; same per-expert CPU recompute fallback on failure. */- if(iss2_threaded) pthread_join(iss2_th,NULL);+ if(iss2_threaded){ double t_j0=now_s(); pthread_join(iss2_th,NULL);+ if(g_prof){ g_vkb_join+=now_s()-t_j0; g_vkb_wrk+=iss2.dt; } }int vk2_ok = iss2.rc && coli_vk_expert_group_take2(vk_yh2);for(int c2=0;c2<nvk2;c2++){ int nr=vrows2[c2];if(vk2_ok){ int o=voff2[c2];@@ -5582,9 +5585,9 @@ static void profile_print(Model *m, double elapsed){elapsed-m->t_ewait-m->t_emm-m->t_attn-m->t_head-m->t_route-m->t_p2p:0);#ifdef COLI_VULKANif(g_prof && g_vkb_blocks)- printf("VK-BLOCK: %lld blocks | avg nvk %.2f (dev2 %.2f) / ncpu %.2f | classify %.3fs | issue %.3fs | take+acc %.3fs (take-wait %.3fs) | cpu exec %.3fs wait %.3fs\n",+ printf("VK-BLOCK: %lld blocks | avg nvk %.2f (dev2 %.2f) / ncpu %.2f | classify %.3fs | issue %.3fs | take+acc %.3fs (take-wait %.3fs) | d2-iss wrk %.3fs join %.3fs | cpu exec %.3fs wait %.3fs\n",(long long)g_vkb_blocks,(double)g_vkb_nvk/g_vkb_blocks,(double)g_vkb_nvk2/g_vkb_blocks,(double)g_vkb_ncpu/g_vkb_blocks,- g_vkb_cls,g_vkb_issue,g_vkb_acc,m->t_egpu,m->t_ecpu,m->t_ewait);+ g_vkb_cls,g_vkb_issue,g_vkb_acc,m->t_egpu,g_vkb_wrk,g_vkb_join,m->t_ecpu,m->t_ewait);#endifif(g_mirror){double br[MIR_REPS]={0}, bt=0;@@ -5614,7 +5617,7 @@ static void profile_reset(Model *m){m->t_ecpu=m->t_egpu=m->t_route=m->t_p2p=0;m->n_p2p=0;m->cpu_expert_bytes=0;m->cpu_expert_rows=0;#ifdef COLI_VULKAN- g_vkb_cls=g_vkb_issue=g_vkb_acc=0; g_vkb_blocks=g_vkb_nvk=g_vkb_nvk2=g_vkb_ncpu=0;+ g_vkb_cls=g_vkb_issue=g_vkb_acc=g_vkb_wrk=g_vkb_join=0; g_vkb_blocks=g_vkb_nvk=g_vkb_nvk2=g_vkb_ncpu=0;#endifm->t_aproj=m->t_acore=m->t_aout=0;atomic_store_explicit(&g_edisk_ns,0,memory_order_relaxed);