Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
vulkan: the MTP head's experts on the tier (qwen38), in a pool of their own
JustVugg17fae83b3a6a
diff --git a/c/backend_vulkan.c b/c/backend_vulkan.cindex dcd52a5d..fc85d261 100644--- a/c/backend_vulkan.c+++ b/c/backend_vulkan.c@@ -1258,11 +1258,14 @@ static VkWPool g_wpool = {.p = {.block_bytes = VK_WBLOCK}, .mx = PTHREAD_MUTEX_static VkWPool g_tpool = {.p = {.block_bytes = VK_WBLOCK}, .mx = PTHREAD_MUTEX_INITIALIZER, .dev = 0, .prio = 0.4f};static VkWPool g_wpool2 = {.p = {.block_bytes = VK_WBLOCK}, .mx = PTHREAD_MUTEX_INITIALIZER, .dev = 1, .prio = -1.f};static VkWPool g_tpool2 = {.p = {.block_bytes = VK_WBLOCK}, .mx = PTHREAD_MUTEX_INITIALIZER, .dev = 1, .prio = -1.f}; /* the tier's experts on COLI_VK_DEV2 */+/* the tier's extra layers (an MTP head's), whose experts have another size than the+ * main ones': a pool of their own, so neither leaves holes the other cannot use */+static VkWPool g_xpool = {.p = {.block_bytes = VK_WBLOCK}, .mx = PTHREAD_MUTEX_INITIALIZER, .dev = 0, .prio = 0.4f};/* Device k's pools take VK_WBLOCK blocks, smaller ones under COLI_VK_DEVICE_CAP_MB. */static void vk_pool_blocks(int k) {size_t bb = VK_WBLOCK;if (g_mem.cap[k]) { bb = (size_t)64 << 10; while ((uint64_t)bb < g_mem.cap[k] / 4096 && bb < VK_WBLOCK) bb <<= 1; }- if (k == 0) { g_wpool.p.block_bytes = bb; g_tpool.p.block_bytes = bb; }+ if (k == 0) { g_wpool.p.block_bytes = bb; g_tpool.p.block_bytes = bb; g_xpool.p.block_bytes = bb; }else { g_wpool2.p.block_bytes = bb; g_tpool2.p.block_bytes = bb; }}static VkDevice pool_device(const VkWPool *P);@@ -3261,7 +3264,9 @@ void coli_vk_tensor_free(ColiVkTensor *t) {tensor_release(t);}-static VkWPool *pool_of(int which) { return which == 1 ? &g_tpool : which == 2 ? &g_wpool2 : which == 3 ? &g_tpool2 : &g_wpool; }+static VkWPool *pool_of(int which) {+ return which == 1 ? &g_tpool : which == 2 ? &g_wpool2 : which == 3 ? &g_tpool2 : which == 4 ? &g_xpool : &g_wpool;+}void coli_vk_pool_stats(int which, ColiVkPoolStats *st) {VkWPool *P = pool_of(which);VkaStats v;@@ -3289,6 +3294,7 @@ static void tier_pool_limit(VkWPool *P, size_t bytes) {}void coli_vk_tier_pool_limit(size_t bytes) { tier_pool_limit(&g_tpool, bytes); }void coli_vk_tier_pool_limit_dev(int dev, size_t bytes) { tier_pool_limit(dev == 1 ? &g_tpool2 : &g_tpool, bytes); }+void coli_vk_tier_extra_pool_limit(size_t bytes) { tier_pool_limit(&g_xpool, bytes); }size_t coli_vk_tensor_bytes(const ColiVkTensor *t) { return t ? t->wbytes : 0; }@@ -4250,6 +4256,15 @@ int coli_vk_tier_tensor(ColiVkTensor **t, int fmt, int I, int O, int gs,uint8_t **rows, size_t *stride, float **scales) {return coli_vk_tier_tensor_dev(0, t, fmt, I, O, gs, rows, stride, scales);}+int coli_vk_tier_tensor_extra(ColiVkTensor **t, int fmt, int I, int O, int gs,+ uint8_t **rows, size_t *stride, float **scales) {+ if (!G.ready || !fmt_uploadable(fmt, gs)) return 0;+ void *w, *s;+ ColiVkTensor *n = tensor_alloc(&g_xpool, fmt, I, O, gs, &w, &s);+ if (!n) return 0;+ *t = n; *rows = w; *stride = (size_t)n->rowWords * 4; *scales = s;+ return 1;+}/* Staged uploads: the host images of tensors filled in place (coli_vk_tier_tensor) go* to their device-local ranges, all of them before this returns, and are freed. Mapped* memory: nothing to do. Any thread; the tensors of one call on one device. */@@ -4310,8 +4325,9 @@ static void place_report(void) {if (!u->on) return;VkPhysicalDeviceMemoryProperties mp;vkGetPhysicalDeviceMemoryProperties(G.phys, &mp);- ColiVkPoolStats w, t;- coli_vk_pool_stats(0, &w); coli_vk_pool_stats(1, &t);+ ColiVkPoolStats w, t, x;+ coli_vk_pool_stats(0, &w); coli_vk_pool_stats(1, &t); coli_vk_pool_stats(4, &x);+ t.peak_used += x.peak_used; /* the expert tier: its extra layers' pool too */size_t kv = 0, ln = 0;for (int l = 0; l < VK_KV_LAYERS; l++) {if (G.kv[l].bl) kv += (size_t)G.kv[l].rows * (G.kv[l].K + G.kv[l].R) * 4;@@ -4426,6 +4442,7 @@ void coli_vk_shutdown(void) {pthread_mutex_unlock(&g_imports_mx);pool_destroy(&g_wpool); /* weight blocks: unmapped/freed with the device */pool_destroy(&g_tpool);+ pool_destroy(&g_xpool);if (PW.buf) { vkDestroyBuffer(G.dev, PW.buf, NULL); vkFreeMemory(G.dev, PW.mem, NULL); }free(PW.cp);memset(&PW, 0, sizeof PW);diff --git a/c/backend_vulkan.h b/c/backend_vulkan.hindex 882fb7d1..30b41c92 100644--- a/c/backend_vulkan.h+++ b/c/backend_vulkan.h@@ -193,6 +193,12 @@ void coli_vk_pool_stats(int pool, ColiVkPoolStats *st);/* The expert tier's budget: its pool never holds more block bytes than this. */void coli_vk_tier_pool_limit(size_t bytes);void coli_vk_tier_pool_limit_dev(int dev, size_t bytes); /* dev 1: COLI_VK_DEV2's pool (3) */+/* The tier's extra layers (an MTP head's experts, another size than the main ones'):+ * a pool of their own on the primary device (pool 4), its budget, and a tensor in it+ * (as coli_vk_tier_tensor). */+void coli_vk_tier_extra_pool_limit(size_t bytes);+int coli_vk_tier_tensor_extra(ColiVkTensor **t, int fmt, int I, int O, int gs,+ uint8_t **rows, size_t *stride, float **scales);/* A tensor in the tier's pool, to fill in place: O rows of coli_vk_tensor_row_bytes* at *stride apart (padding zeroed) and coli_vk_tensor_scale_count floats of scales* (fmt 10/11: one, set it to 1), then coli_vk_tensor_commit. Thread-safe. Returns 0diff --git a/c/qwen38.c b/c/qwen38.cindex c12c7fd8..6350deb2 100644--- a/c/qwen38.c+++ b/c/qwen38.c@@ -2383,7 +2383,7 @@ static int q38_segment_expert_layout(Model *m,uint32_t begin,uint32_t end,uint64_t *fixed_scale_bytes,unsigned *numeric_kinds){if(!m||!bytes_per_capacity||!fixed_scale_bytes||!numeric_kinds||begin>=end||- end>(uint32_t)m->c.layers)return -1;+ end>(uint32_t)q38_layer_rows(m))return -1;Cfg *c=&m->c;uint64_t range_bytes=0,range_scales=0;unsigned kinds=0;for(uint32_t layer=begin;layer<end;layer++){char name[320];q38_name(m,name,sizeof name,(int)layer,@@ -2502,7 +2502,9 @@ static void q38_expert_report(Model *m, int cap) {* the tier how this snapshot's experts sit in RAM, what else will live on the* device, and how much RAM the expert cache may still take; then fill it from the* history, reading the planned experts straight from the files (not through the- * LRU, which keeps its own working set). The MTP head's layer is not offered. */+ * LRU, which keeps its own working set). The MTP head's layer goes on the tier as+ * an extra layer, its experts in the form the snapshot keeps them (FP8 beside the+ * int4 sidecar); it has no history, so the drafts fill it. */#ifdef COLI_VULKANstatic void q38_vk_dense_add(const Q38Weight *w,size_t *bytes){if(!w||!q38_vk_eligible(w))return;@@ -2578,6 +2580,24 @@ static int q38_vk_load_batch(void *ctx,int layer,const int *e,int n,VktExpertSrcfor(int i=0;i<n;i++){srcs[i]=q38_vk_src(sl[i]);h[i]=sl[i];}return n;}+/* The tier's source form of layers [begin,end)'s routed experts as the snapshot+ * keeps them (not the int4 sidecar's), and one expert's bytes in RAM; VKT_SRC_NONE+ * when they mix formats. */+static VktFmt q38_vk_native_fmt(Model *m,int begin,int end,size_t *ram_expert){+ Cfg *c=&m->c;int H=c->hidden,F=c->inter;+ uint64_t per_capacity=0,fixed=0;unsigned kinds=0;+ VktFmt f={VKT_SRC_NONE,0};+ if(q38_segment_expert_layout(m,(uint32_t)begin,(uint32_t)end,&per_capacity,&fixed,&kinds))return f;+ if(kinds==Q38_EXPERT_FP8_BLOCK&&m->native_fp8){+ for(int l=begin;l<end;l++)+ if(!q38_prepare_expert_scale_bank(m,l))return f;+ f.kind=VKT_SRC_FP8_BLOCK; f.gs=FP8_BLOCK; *ram_expert=(size_t)3*H*F;+ } else if(kinds==Q38_EXPERT_BF16&&m->native_bf16){ f.kind=VKT_SRC_BF16; *ram_expert=(size_t)3*H*F*2; }+ else if(kinds==Q38_EXPERT_BF16||kinds==Q38_EXPERT_F16||kinds==Q38_EXPERT_F32||kinds==Q38_EXPERT_FP8_EXPANDED){+ f.kind=VKT_SRC_F32; *ram_expert=(size_t)3*H*F*4;+ }+ return f;+}static void q38_vk_tier_start(Model *m,int cap){if(!g_vk_ready||qt_ready()){if(g_vk_ready&&qt_ready())@@ -2585,25 +2605,26 @@ static void q38_vk_tier_start(Model *m,int cap){return;}Cfg *c=&m->c;int H=c->hidden,F=c->inter;- uint64_t per_capacity=0,fixed=0;unsigned kinds=0;VktFmt f={VKT_SRC_NONE,0};size_t ram_expert=0;if(m->x4){ f.kind=VKT_SRC_I4U_PLANAR64; f.gs=64; ram_expert=(size_t)m->x4->record_bytes; }- else if(!q38_segment_expert_layout(m,0,(uint32_t)c->layers,&per_capacity,&fixed,&kinds)){- if(kinds==Q38_EXPERT_FP8_BLOCK&&m->native_fp8){- for(int l=0;l<c->layers;l++)- if(!q38_prepare_expert_scale_bank(m,l)){ kinds=0; break; }- if(kinds){ f.kind=VKT_SRC_FP8_BLOCK; f.gs=FP8_BLOCK; ram_expert=(size_t)3*H*F; }- } else if(kinds==Q38_EXPERT_BF16&&m->native_bf16){ f.kind=VKT_SRC_BF16; ram_expert=(size_t)3*H*F*2; }- else if(kinds==Q38_EXPERT_BF16||kinds==Q38_EXPERT_F16||kinds==Q38_EXPERT_F32||kinds==Q38_EXPERT_FP8_EXPANDED){- f.kind=VKT_SRC_F32; ram_expert=(size_t)3*H*F*4;- }- }+ else f=q38_vk_native_fmt(m,0,c->layers,&ram_expert);if(f.kind==VKT_SRC_NONE){fprintf(stderr,"[VK] tier qwen38: the routed experts mix formats; they stay on the CPU\n");return;}+ /* the MTP head's layer (index c->layers): by default on a discrete GPU only. On a+ * Radeon 780M (shared RAM) it ran 3.44 tok/s against 3.48 on the CPU, its share of+ * the budget taken from the model's layers; COLI_VK_TIER_MTP=1 or 0 decides. */+ VktFmt fm={VKT_SRC_NONE,0};size_t ram_mtp=0;+ const char *tm=getenv("COLI_VK_TIER_MTP");+ int want_mtp=tm&&*tm?*tm!='0':!coli_vk_device_shares_ram();+ if(m->mtp&&want_mtp){+ fm=q38_vk_native_fmt(m,c->layers,c->layers+1,&ram_mtp);+ if(fm.kind==VKT_SRC_NONE)fprintf(stderr,"[VK] tier qwen38: the MTP head's experts mix formats; they stay on the CPU\n");+ }VktConfig vc={.engine="qwen38",.layers=c->layers,.experts=c->experts,.hidden=H,.inter=F,.topk=c->topk,.gate_up=f,.down=f,.act=VKT_ACT_SWIGLU,+ .extra_layers=fm.kind!=VKT_SRC_NONE,.extra_gate_up=fm,.extra_down=fm,.max_rows=q38_moe_prefill_rows(c,q38_prefill_batch_rows())*c->topk,.ram_reserve=ram_expert*(size_t)cap*(size_t)c->layers,.dense_bytes=q38_vk_dense_bytes(m),diff --git a/c/qwen38_core.h b/c/qwen38_core.hindex d7c5e82a..43a13480 100644--- a/c/qwen38_core.h+++ b/c/qwen38_core.h@@ -2694,9 +2694,9 @@ static void q38_tier_start(Model *m,int cap) {* The tier computes the resident experts of a step on the device while the CPU* computes the rest; every expert's output then joins the row in rank order, the* device's and the CPU's alike, so the sum's order is the CPU run's whatever was- * resident. The model's layers only: the MTP head's layer keeps its FP8 experts on- * the CPU (the sidecar's int4 covers the model's layers). A slot's bytes are what- * the tier reads when it promotes the expert: codes and scales of each matrix. */+ * resident. The MTP head's layer too (index c.layers, the tier's extra layer), with+ * the experts the snapshot keeps (FP8 beside the sidecar's int4). A slot's bytes are+ * what the tier reads when it promotes the expert: codes and scales of each matrix. */static VktExpertSrc q38_vk_src(const Slot *ex) {VktExpertSrc s={ex->gate.data,ex->up.data,ex->down.data,ex->gate.scales,ex->up.scales,ex->down.scales};return s;@@ -2718,7 +2718,7 @@ static void q38_moe_decode_ex(Model *m,Layer *l,int layer,const float *x,int S,fCfg *c=&m->c;int H=c->hidden,E=c->experts,K=c->topk,I=c->inter,SI=c->shared_inter;float *logits=falloc(E),*sg=falloc(SI),*su=falloc(SI),*sh=falloc(SI),*shared=falloc(H);float *eg=falloc(I),*eu=falloc(I),*eh=falloc(I),*eo=falloc(H);- int vk=vkt_ready()&&layer<c->layers; /* the Vulkan tier: outputs buffered per rank */+ int vk=vkt_ready()&&layer<vkt_layers(); /* the Vulkan tier: outputs buffered per rank */float *ebuf=vk?falloc((int64_t)K*H):NULL;for(int s=0;s<S;s++){const float *xs=x+(int64_t)s*H;float *ys=out+(int64_t)s*H;memset(ys,0,(size_t)H*sizeof(float));@@ -2870,7 +2870,7 @@ static void q38_moe_prefill(Model *m,Layer *l,int layer,const float *x,}/* the Vulkan tier: per assignment, whether the device takes it and its output */- int vk=vkt_ready()&&layer<c->layers;+ int vk=vkt_ready()&&layer<vkt_layers();int *vk_idx=vk?(int*)malloc((size_t)max_assign*sizeof(int)):NULL;uint8_t *vk_taken=vk?(uint8_t*)calloc((size_t)max_assign,1):NULL;const float **vk_dev=vk?(const float**)malloc((size_t)max_assign*sizeof(*vk_dev)):NULL;diff --git a/c/tests/test_vk_tier.c b/c/tests/test_vk_tier.cindex ee59dbc9..20c3390f 100644--- a/c/tests/test_vk_tier.c+++ b/c/tests/test_vk_tier.c@@ -18,6 +18,12 @@* books device + CPU = routed, resident <= budget, no failed upload;* v4 DeepSeek V4's activation (route weight on the device, its bf16 and E4M3* roundings): device rows against the engine's CPU arithmetic;+ * extra an extra layer (an MTP head's) in another, bigger form: each layer's rows+ * on the device equal the reference, its experts sit in a pool of their+ * own with its share of the budget (each pool evicts its own kind, no+ * upload refused), it gets its expert in beside a full main pool on its+ * own promotion rate, and with no device form for it the tier serves the+ * main layers;* stream big prefill steps with streaming (vkt_issue's sub-batches, the staging* slots, prefetch, experts cut into parts): every device row, resident or* streamed, equals the reference, the sum the all-CPU sum, the cold experts@@ -144,11 +150,10 @@ static void expert_ref(const Ex *e, const float *x, float *y) {static VktExpertSrc src_of(const Ex *e) {return (VktExpertSrc){e->g.codes, e->u.codes, e->d.codes, e->g.scales, e->u.scales, e->d.scales};}-static void model_make(VktFmt gu, VktFmt dn) {- for (int l = 0; l < L; l++) for (int e = 0; e < E; e++) {- mat_make(&ex[l][e].g, gu, H, F); mat_make(&ex[l][e].u, gu, H, F); mat_make(&ex[l][e].d, dn, F, H);- }+static void layer_make(int l, VktFmt gu, VktFmt dn) {+ for (int e = 0; e < E; e++) { mat_make(&ex[l][e].g, gu, H, F); mat_make(&ex[l][e].u, gu, H, F); mat_make(&ex[l][e].d, dn, F, H); }}+static void model_make(VktFmt gu, VktFmt dn) { for (int l = 0; l < L; l++) layer_make(l, gu, dn); }static void model_free(void) {for (int l = 0; l < L; l++) for (int e = 0; e < E; e++) { mat_free(&ex[l][e].g); mat_free(&ex[l][e].u); mat_free(&ex[l][e].d); }}@@ -404,6 +409,94 @@ static void sync_evict(void) {vkt_shutdown(); model_free();}+/* An extra layer: layer 0 is the model's (int4 v+8 gs64), layer 1 an MTP head's held+ * as f32 (VktConfig.extra_layers), eight times the bytes. Its experts sit in a pool of+ * their own with the extra layer's share of the budget; uploads awaited throughout. */+static void extra(void) {+ VktFmt fm = {VKT_SRC_I4U_PAIRS_GS, 64}, fx = {VKT_SRC_F32, 0};+ layer_make(0, fm, fm); layer_make(1, fx, fx); g_act = VKT_ACT_SWIGLU; g_limit = 0;+ size_t mb = vkt_expert_bytes(H, F, fm, fm), xb = vkt_expert_bytes(H, F, fx, fx);+ int per = (int)((xb + mb - 1) / mb);+ unsigned long long ev, failed;+ int idx[4 * K]; float w[4 * K];+ VktConfig vc = cfg_of(fm, fm, VKT_ACT_SWIGLU, 0);+ vc.layers = 1; vc.extra_layers = 1; vc.extra_gate_up = fx; vc.extra_down = fx;+ /* Half of a budget of 2 * per + 4 main experts is the extra layer's: one f32 expert,+ * whose room leaves the main layer all ten of its own. Both layers route, the hot+ * set moves halfway: each pool evicts in its own kind, every row is right. */+ int units = 2 * per + 4;+ set_budget(units, fm, fm);+ setenv("COLI_VK_TIER_RATE", "64", 1);+ setenv("COLI_VK_TIER_SYNC", "1", 1);+ int on = vkt_init(&vc, NULL);+ unsetenv("COLI_VK_TIER_SYNC");+ CHECK(on && vkt_layers() == 2, "extra: the tier did not start with its extra layer (%d layers)", vkt_layers());+ if (!on) { model_free(); return; }+ Books b0 = {0, 0}, b1 = {0, 0}; double worst = 0;+ for (int t = 0; t < 140; t++) /* the new hot set needs a while to outweigh the old one */+ for (int l = 0; l < 2; l++) {+ route(4, t < 40 ? 0 : 5, 5, idx, w);+ double r = step(l, 4, idx, w, l ? &b1 : &b0);+ if (r > worst) worst = r;+ }+ int r0 = 0, r1 = 0;+ for (int e = 0; e < E; e++) { r0 += vkt_resident(0, e); r1 += vkt_resident(1, e); }+ report_counts(&ev, &failed);+ ColiVkPoolStats xp; coli_vk_pool_stats(4, &xp);+ printf(" extra: an f32 expert is %d int4 ones; device main %llu of %llu, extra %llu of %llu; resident main %d, "+ "extra %d (its pool %zu of %zu bytes), evictions %llu, failed %llu, worst relative error %.2e\n",+ per, b0.dev, b0.routed, b1.dev, b1.routed, r0, r1, xp.used, xp.limit, ev, failed, worst);+ CHECK(per > 1, "extra: the f32 form takes no more room than the int4 one (%zu, %zu bytes)", xb, mb);+ CHECK(b0.dev > 0 && b1.dev > 0, "extra: a layer ran nothing on the device (main %llu, extra %llu)", b0.dev, b1.dev);+ CHECK(r0 == E && r1 == 1, "extra: resident main %d (want %d), extra %d (want 1)", r0, E, r1);+ CHECK(xp.used > 0 && xp.used <= xp.limit, "extra: the extra pool holds %zu bytes of %zu", xp.used, xp.limit);+ CHECK(ev >= 1 && failed == 0, "extra: %llu evictions, %llu failed uploads", ev, failed);+ CHECK(worst < 2e-3, "extra: relative error %.3g", worst);+ vkt_shutdown();+ /* A budget of per + 4: the extra layer's share is under one of its experts, so it+ * gets one (two main ones still fit) and the main layer the four places left. The+ * main layer fills them, then keeps routing and spends each forward's promotion+ * (COLI_VK_TIER_RATE=1) before the extra layer runs: the extra layer's expert must+ * still get in, on its own promotion. */+ units = per + 4;+ set_budget(units, fm, fm);+ setenv("COLI_VK_TIER_RATE", "1", 1);+ setenv("COLI_VK_TIER_SYNC", "1", 1);+ on = vkt_init(&vc, NULL);+ unsetenv("COLI_VK_TIER_SYNC");+ CHECK(on && vkt_layers() == 2, "extra: the tier did not start for the full-tier case");+ if (on) {+ Books c0 = {0, 0}, c1 = {0, 0}; worst = 0;+ for (int t = 0; t < 40; t++) { route(4, 0, E, idx, w); double r = step(0, 4, idx, w, &c0); if (r > worst) worst = r; }+ int full = 0; for (int e = 0; e < E; e++) full += vkt_resident(0, e);+ for (int t = 0; t < 60; t++) {+ route(4, 0, E, idx, w); double r = step(0, 4, idx, w, &c0); if (r > worst) worst = r;+ route(4, 0, 5, idx, w); r = step(1, 4, idx, w, &c1); if (r > worst) worst = r;+ }+ r0 = r1 = 0;+ for (int e = 0; e < E; e++) { r0 += vkt_resident(0, e); r1 += vkt_resident(1, e); }+ report_counts(&ev, &failed);+ printf(" extra, full tier: main resident %d after the first phase; then main %d + extra %d, "+ "extra device %llu of %llu, evictions %llu, failed %llu, worst %.2e\n",+ full, r0, r1, c1.dev, c1.routed, ev, failed, worst);+ CHECK(full == 4 && r0 == 4, "extra: the main layer holds %d then %d of its 4 places", full, r0);+ CHECK(r1 == 1 && c1.dev > 0, "extra: no extra expert got in beside a full main pool (resident %d, device %llu)", r1, c1.dev);+ CHECK(ev >= 1 && failed == 0, "extra, full tier: %llu evictions, %llu failed uploads", ev, failed);+ CHECK(worst < 2e-3, "extra, full tier: relative error %.3g", worst);+ vkt_shutdown();+ }+ /* an extra form with no device form: the main layers only */+ vc.extra_gate_up = vc.extra_down = (VktFmt){VKT_SRC_NONE, 0};+ on = vkt_init(&vc, NULL);+ CHECK(on && vkt_layers() == 1, "extra: no device form, the tier has %d layers (want the main one)", vkt_layers());+ if (on) {+ float x[H] = {0}; int id[K] = {0, 1, 2}; uint8_t taken[K];+ CHECK(vkt_issue(1, x, 1, K, id, taken) == 0, "extra: a layer past the tier's took rows");+ vkt_shutdown();+ }+ model_free();+}+/* DeepSeek V4's activation (VKT_ACT_SWIGLU_V4) through the tier: MXFP4 ue8m0 experts,* x rounded to E4M3 per 128 here as the engine rounds it, the route weight applied on* the device (vkt_issue_w), each device row rounded to bf16 and added with no weight@@ -708,10 +801,11 @@ int main(int argc, char **argv) {printf("adapt:\n"); adapt();printf("partial:\n"); partial();printf("sync:\n"); sync_evict();+ printf("extra:\n"); extra();printf("DeepSeek V4:\n"); v4_act();printf("stream:\n"); stream();- ColiVkPoolStats ps; coli_vk_pool_stats(1, &ps);- CHECK(ps.live == 0, "%d tier ranges still live after every shutdown", ps.live);+ ColiVkPoolStats ps, px; coli_vk_pool_stats(1, &ps); coli_vk_pool_stats(4, &px);+ CHECK(ps.live == 0 && px.live == 0, "%d + %d tier ranges still live after every shutdown", ps.live, px.live);coli_vk_shutdown(); /* with staged uploads: where the experts were ("[VK] memory at exit") */stream_commit_failure(spv);printf(fails ? "FAIL (%d)\n" : "PASS\n", fails);diff --git a/c/tests/vulkan_engines.sh b/c/tests/vulkan_engines.shindex ef0ddf62..64b9a85f 100755--- a/c/tests/vulkan_engines.sh+++ b/c/tests/vulkan_engines.sh@@ -473,12 +473,25 @@ family_qwen() {EVICT=1 tier_gate qwen38 "qwen38 tier, a budget of two experts" OMP_NUM_THREADS=2 Q38_PREFILL_BATCH=0 COLI_VK_TIER_GB=0.0000065 SNAP=qwen38_tiny_fp8 -- 1 8 qwen38_tiny_fp8/ref.json$PY tools/make_qwen38_tiny.py --out qwen38_tiny_fp8_mtp --fp8-experts --mtp$PY tools/make_qwen38_tiny.py --out qwen38_tiny_int4_mtp --fp8-experts --int4-experts --expert-gain 3 --mtp+ # the MTP head's layer as the tier's extra layer (COLI_VK_TIER_MTP=1; the default on a+ # discrete GPU): its experts (FP8, beside the int4 sidecar on the second fixture) run+ # on the device too. Lavapipe shares the RAM, where the default keeps them on the CPU;+ # the tokens are the same either way.for fx in qwen38_tiny_fp8_mtp qwen38_tiny_int4_mtp; dofor batch in 0 1; doD=; [ $batch = 1 ] && D=COLI_VK_DENSE=1- tier_gate qwen38 "qwen38 tier MTP $fx batch=$batch" OMP_NUM_THREADS=2 $D Q38_MTP=1 Q38_PREFILL_BATCH=$batch SNAP=$fx -- 2 8 $fx/ref.json+ tier_gate qwen38 "qwen38 tier MTP $fx batch=$batch" OMP_NUM_THREADS=2 $D Q38_MTP=1 COLI_VK_TIER_MTP=1 Q38_PREFILL_BATCH=$batch SNAP=$fx -- 2 8 $fx/ref.json+ mtp_on_device "qwen38 tier MTP $fx batch=$batch"donedone+ tier_gate qwen38 "qwen38 tier MTP head on the CPU (shared RAM default)" OMP_NUM_THREADS=2 Q38_MTP=1 Q38_PREFILL_BATCH=0 SNAP=qwen38_tiny_int4_mtp -- 2 8 qwen38_tiny_int4_mtp/ref.json+ ! grep -aq 'extra layers' vk.log || { grep -a '\[VK\] tier' vk.log; fail "shared RAM: the MTP head's layer went on the tier by default"; }+ echo "OK shared RAM: the MTP head's experts on the CPU by default"+}+mtp_on_device() { # <tag>: vk.log's tier served experts of the MTP head's layer+ local n; n=$(grep -a -o 'extra layers (1): [0-9]*' vk.log | tail -1 | grep -o '[0-9]*$')+ [ "${n:-0}" -gt 0 ] || { grep -a '\[VK\] tier' vk.log; fail "$1: no expert of the MTP head's layer ran on the device"; }+ echo "OK $1: $(grep -a -o 'extra layers (1): [0-9]* of [0-9]* routed experts on the device' vk.log | tail -1)"}# The routed-expert tier under ASan and UBSan: a sanitized VK=1 build of both qwen@@ -522,7 +535,8 @@ family_qwen_sanitize() {san qwen38 "asan qwen38 int4 batch=$b" $D Q38_PREFILL_BATCH=$b SNAP=qwen38_tiny_int4 ./qwen38 1 8 qwen38_tiny_int4/ref_int4.jsondonesan qwen38 "asan qwen38 eviction" Q38_PREFILL_BATCH=0 COLI_VK_TIER_GB=0.0000065 SNAP=qwen38_tiny_fp8 ./qwen38 1 8 qwen38_tiny_fp8/ref.json- san qwen38 "asan qwen38 MTP" COLI_VK_DENSE=1 Q38_MTP=1 Q38_PREFILL_BATCH=0 SNAP=qwen38_tiny_int4_mtp ./qwen38 2 8 qwen38_tiny_int4_mtp/ref.json+ san qwen38 "asan qwen38 MTP" COLI_VK_DENSE=1 Q38_MTP=1 COLI_VK_TIER_MTP=1 Q38_PREFILL_BATCH=0 SNAP=qwen38_tiny_int4_mtp ./qwen38 2 8 qwen38_tiny_int4_mtp/ref.json+ cp san.log vk.log; mtp_on_device "asan qwen38 MTP"san qwen38 "asan qwen38 tier alone" COLI_VK_DENSE=0 SNAP=qwen38_tiny_int4 ./qwen38 4 8 qwen38_tiny_int4/ref_int4.jsonmake clean >/dev/null 2>&1 || true}diff --git a/c/vk_tier.c b/c/vk_tier.cindex bb1a835e..0fbb447c 100644--- a/c/vk_tier.c+++ b/c/vk_tier.c@@ -55,8 +55,17 @@ static struct {int on;VktConfig c;char engine[32];- int gu_fmt, gu_gs, dn_fmt, dn_gs;- size_t gu_codes, gu_scales, d_codes, d_scales, stage_bytes; /* one expert as the engine holds it */+ /* the forms of the experts, as the engine holds them and on the device: [0] the main+ * layers', [1] the extra layers' (VktConfig.extra_layers, an MTP head's), from+ * main_layers on */+ int main_layers;+ struct { VktFmt gu, dn; int gu_fmt, gu_gs, dn_fmt, dn_gs; size_t gu_codes, gu_scales, d_codes, d_scales, stage_bytes; } form[2];+ /* the extra layers' experts: on the primary device, in a pool of their own (another+ * size than the main ones', so neither leaves holes the other cannot use), at most+ * xmax of x_bytes each in x_budget */+ int xmax, xres, xque;+ size_t x_bytes, x_budget;+ unsigned long long extra_served, extra_routed;size_t exp_bytes, budget;int max_resident, resident, queued, rate, uma; /* totals over the devices *//* per device: the most it holds, resident, queued (dmax[1] = 0 without a second one) */@@ -79,7 +88,7 @@ static struct {double d2_ms;VSlot *s;uint32_t tick, decay_at; /* tokens seen (rows of the forward's first layer) */- int last_layer, first_layer, promos, promo_cap;+ int last_layer, first_layer, promos[2], promo_cap; /* promotions this forward, the main layers' and the extra ones', each up to the cap */int begin; /* vkt_begin_forward: the next issue starts a forward *//* uploader */pthread_t th; int th_on, stop;@@ -93,7 +102,7 @@ static struct {/* evicted, freed at the next quiescent point */int *evict; int nevict, cevict;/* the coldest residents, refreshed once per forward */- int cand[VKT_CAND]; uint64_t cand_score[VKT_CAND]; int ncand;+ int cand[2][VKT_CAND]; uint64_t cand_score[2][VKT_CAND]; int ncand[2]; /* per kind: main, extra *//* the step in flight */int inflight, S, K;int *map; int cmap;@@ -133,6 +142,7 @@ static const char *human(double b, char *buf, size_t n) {}static double vkt_now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }static VSlot *slot(int layer, int eid) { return &T.s[(size_t)layer * T.c.experts + eid]; }+static int kind_of(int layer) { return layer >= T.main_layers; } /* the form of a layer's experts *//* ---- source formats ---------------------------------------------------------- */static int src_per_row(VktSrc k) {@@ -250,17 +260,19 @@ static void convert_par(VktFmt f, int I, int O, const uint8_t *codes, const void/* Upload one expert from its RAM form to device dev's pool (any thread). 0 when the pool* refused, -1 when the copy to the device failed (staged uploads: the room was there,* and is given back). */-static int upload(int dev, const uint8_t *g, const uint8_t *u, const uint8_t *d,+static int upload(int dev, int kind, const uint8_t *g, const uint8_t *u, const uint8_t *d,const void *gs, const void *us, const void *ds, ColiVkTensor *t[3]) {const int H = T.c.hidden, F = T.c.inter;+ const __typeof__(T.form[0]) *fm = &T.form[kind];const uint8_t *codes[3] = {g, u, d}; const void *sc[3] = {gs, us, ds};t[0] = t[1] = t[2] = NULL;for (int k = 0; k < 3; k++) {- VktFmt f = k < 2 ? T.c.gate_up : T.c.down;- int fmt = k < 2 ? T.gu_fmt : T.dn_fmt, fgs = k < 2 ? T.gu_gs : T.dn_gs;+ VktFmt f = k < 2 ? fm->gu : fm->dn;+ int fmt = k < 2 ? fm->gu_fmt : fm->dn_fmt, fgs = k < 2 ? fm->gu_gs : fm->dn_gs;int I = k < 2 ? H : F, O = k < 2 ? F : H;uint8_t *rows; size_t stride; float *scales;- if (!coli_vk_tier_tensor_dev(dev, &t[k], fmt, I, O, fgs, &rows, &stride, &scales)) {+ if (!(kind ? coli_vk_tier_tensor_extra(&t[k], fmt, I, O, fgs, &rows, &stride, &scales) /* its own pool */+ : coli_vk_tier_tensor_dev(dev, &t[k], fmt, I, O, fgs, &rows, &stride, &scales))) {for (int j = 0; j < k; j++) { coli_vk_tensor_free(t[j]); t[j] = NULL; }return 0;}@@ -325,8 +337,10 @@ static void *uploader(void *arg) {VQ e = T.q[T.qh]; T.qh = (T.qh + 1) % VKT_QCAP; T.qn--; T.busy = 1;pthread_mutex_unlock(&T.mx);const uint8_t *b = e.buf;- const uint8_t *g = b, *u = g + T.gu_codes, *d = u + T.gu_codes;- const uint8_t *gs = d + T.d_codes, *us = gs + T.gu_scales, *ds = us + T.gu_scales;+ const int kd = kind_of(e.layer);+ const size_t guc = T.form[kd].gu_codes, dc = T.form[kd].d_codes, gus = T.form[kd].gu_scales;+ const uint8_t *g = b, *u = g + guc, *d = u + guc;+ const uint8_t *gs = d + dc, *us = gs + gus, *ds = us + gus;ColiVkTensor *t[3];int ok = 0, r = 0;/* Refused: the pool is at its budget until the engine thread frees the victim@@ -335,7 +349,7 @@ static void *uploader(void *arg) {* and try again; refused again after three frees, or nothing freed for a* minute (the engine stopped stepping), the pool is full for real. */for (int frees = 0, waited = 0; !ok; ) {- r = upload(e.dev, g, u, d, gs, us, ds, t);+ r = upload(e.dev, kd, g, u, d, gs, us, ds, t);ok = r > 0;/* sync: every free decided so far came first (above), and the engine now waits* on us, not we on it: the pool is full for real. A failed copy is no question@@ -386,12 +400,12 @@ static void quiesce(void) {pthread_mutex_unlock(&T.mx);for (int i = 0; i < n; i++) {VSlot *v = slot(d[i].layer, d[i].eid);- int dv = v->dev;- T.queued--; T.dque[dv]--;+ int dv = v->dev, kd = kind_of(d[i].layer);+ T.queued--; if (kd) T.xque--; else T.dque[dv]--;/* an upload to a second device that has stopped meanwhile (d2_stop) is dropped */ColiVkExpert *e = d[i].ok && (dv == 0 || T.d2) ? coli_vk_xb_expert(d[i].g, d[i].u, d[i].d) : NULL;if (e) {- v->ex = e; v->state = VS_RESIDENT; T.resident++; T.dres[dv]++;+ v->ex = e; v->state = VS_RESIDENT; T.resident++; if (kd) T.xres++; else T.dres[dv]++;T.uploads++; T.upload_bytes += coli_vk_tensor_bytes(d[i].g) + coli_vk_tensor_bytes(d[i].u) + coli_vk_tensor_bytes(d[i].d);if (dv) T.d2_uploads++;} else {@@ -402,7 +416,8 @@ static void quiesce(void) {/* refused: the budget holds fewer than the count said (fragmentation, or the* device ran out first): stop planning past what is there. A failed copy gave* its room back: the budget stands, the expert may come again. */- if (!d[i].copy_failed && T.dmax[dv] > T.dres[dv] + T.dque[dv]) {+ if (!d[i].copy_failed && kd && T.xmax > T.xres + T.xque) T.xmax = T.xres + T.xque;+ if (!d[i].copy_failed && !kd && T.dmax[dv] > T.dres[dv] + T.dque[dv]) {T.max_resident -= T.dmax[dv] - (T.dres[dv] + T.dque[dv]);T.dmax[dv] = T.dres[dv] + T.dque[dv];}@@ -430,9 +445,11 @@ static void d2_stop(const char *why) {T.engine, why, coli_vk_device_name());}-/* The device a newcomer goes to when there is room: the primary device first, then the- * second one; -1 when both are full. */-static int room_dev(void) {+/* The device a newcomer of layer `layer` goes to when there is room: a main layer's to+ * the primary device first, then the second one; an extra layer's to its pool on the+ * primary device; -1 when they are full. */+static int room_dev(int layer) {+ if (kind_of(layer)) return T.xres + T.xque < T.xmax ? 0 : -1;if (T.dres[0] + T.dque[0] < T.dmax[0]) return 0;if (T.d2 && T.dres[1] + T.dque[1] < T.dmax[1]) return 1;return -1;@@ -441,32 +458,34 @@ static int room_dev(void) {/* ---- placement ------------------------------------------------------------------ */static uint64_t score(const VSlot *v) { return tier_lfru_score(v->heat, v->last, T.tick); }-/* The coldest residents, ascending, once per forward. */+/* The coldest residents of each kind, ascending, once per forward. */static void refresh_candidates(void) {- T.ncand = 0;+ T.ncand[0] = T.ncand[1] = 0;size_t n = (size_t)T.c.layers * T.c.experts;for (size_t i = 0; i < n; i++) {VSlot *v = &T.s[i];if (v->state != VS_RESIDENT) continue;- uint64_t sc = score(v);- if (T.ncand == VKT_CAND && sc >= T.cand_score[VKT_CAND - 1]) continue;- int at = T.ncand < VKT_CAND ? T.ncand++ : VKT_CAND - 1;- while (at > 0 && T.cand_score[at - 1] > sc) { T.cand[at] = T.cand[at - 1]; T.cand_score[at] = T.cand_score[at - 1]; at--; }- T.cand[at] = (int)i; T.cand_score[at] = sc;+ int k = kind_of((int)(i / (size_t)T.c.experts)), *c = T.cand[k];+ uint64_t sc = score(v), *cs = T.cand_score[k];+ if (T.ncand[k] == VKT_CAND && sc >= cs[VKT_CAND - 1]) continue;+ int at = T.ncand[k] < VKT_CAND ? T.ncand[k]++ : VKT_CAND - 1;+ while (at > 0 && cs[at - 1] > sc) { c[at] = c[at - 1]; cs[at] = cs[at - 1]; at--; }+ c[at] = (int)i; cs[at] = sc;}}-/* The resident a newcomer of score hs would displace, or -1: the coldest candidate- * still resident, when the newcomer beats it by tier.h's LFRU margin (25% + 4- * counts). It stays first in the list until drop_victim takes it. */-static void drop_victim(void) {- memmove(T.cand, T.cand + 1, (size_t)--T.ncand * sizeof(int));- memmove(T.cand_score, T.cand_score + 1, (size_t)T.ncand * sizeof(uint64_t));+/* The resident a newcomer of kind k and score hs would displace, or -1: the coldest+ * candidate of its kind (each kind has its own pool) still resident, when the newcomer+ * beats it by tier.h's LFRU margin (25% + 4 counts). It stays first in the list until+ * drop_victim takes it. */+static void drop_victim(int k) {+ memmove(T.cand[k], T.cand[k] + 1, (size_t)--T.ncand[k] * sizeof(int));+ memmove(T.cand_score[k], T.cand_score[k] + 1, (size_t)T.ncand[k] * sizeof(uint64_t));}-static int peek_victim(uint64_t hs) {- while (T.ncand) {- int i = T.cand[0];+static int peek_victim(int k, uint64_t hs) {+ while (T.ncand[k]) {+ int i = T.cand[k][0];VSlot *v = &T.s[i];- if (v->state != VS_RESIDENT) { drop_victim(); continue; }+ if (v->state != VS_RESIDENT) { drop_victim(k); continue; }uint64_t cs = score(v);return hs > cs + (cs >> 2) + (4u << 8) ? i : -1;}@@ -477,7 +496,7 @@ static int peek_victim(uint64_t hs) {* blocks of rows, or rows one by one, is the same forward). The tick counts tokens:* the rows of the forward's first layer, every block of them. */static void new_forward(void) {- T.promos = 0; T.promo_cap = 0; T.said_fwd = 0;+ T.promos[0] = T.promos[1] = 0; T.promo_cap = 0; T.said_fwd = 0;if (T.tick >= T.decay_at) {size_t n = (size_t)T.c.layers * T.c.experts;for (size_t i = 0; i < n; i++) T.s[i].heat = tier_decay_value(T.s[i].heat);@@ -493,38 +512,42 @@ void vkt_note(int layer, int eid, const VktExpertSrc *src) {if (!T.on || !src || layer < 0 || layer >= T.c.layers || eid < 0 || eid >= T.c.experts) return;VSlot *v = slot(layer, eid);if (v->state != VS_NONE || !src->g || !src->u || !src->d) return;- if (T.promos >= T.promo_cap) { T.rated++; return; }- if (T.gu_scales && (!src->gs || !src->us || !src->ds)) return;- int victim = -1, dev = room_dev();- if (dev < 0 && (!v->heat || (victim = peek_victim(score(v))) < 0)) return;+ /* the extra layers (an MTP head's) come last in a forward: their promotions are+ * counted apart, or the model's layers would always have spent the rate first */+ const int kd = kind_of(layer);+ if (T.promos[kd] >= T.promo_cap) { T.rated++; return; }+ const __typeof__(T.form[0]) *fm = &T.form[kd];+ if (fm->gu_scales && (!src->gs || !src->us || !src->ds)) return;+ int victim = -1, dev = room_dev(layer);+ if (dev < 0 && (!v->heat || (victim = peek_victim(kd, score(v))) < 0)) return;if (victim >= 0) dev = T.s[victim].dev; /* the newcomer takes its place, on its device */pthread_mutex_lock(&T.mx);int qfull = T.qn >= VKT_QCAP;pthread_mutex_unlock(&T.mx);if (qfull) { T.qfull++; return; }- uint8_t *buf = malloc(T.stage_bytes);+ uint8_t *buf = malloc(fm->stage_bytes);if (!buf) return;- if (victim >= 0) drop_victim();+ if (victim >= 0) drop_victim(kd);uint8_t *p = buf;- memcpy(p, src->g, T.gu_codes); p += T.gu_codes;- memcpy(p, src->u, T.gu_codes); p += T.gu_codes;- memcpy(p, src->d, T.d_codes); p += T.d_codes;- if (T.gu_scales) { memcpy(p, src->gs, T.gu_scales); p += T.gu_scales; memcpy(p, src->us, T.gu_scales); p += T.gu_scales; }- if (T.d_scales) memcpy(p, src->ds, T.d_scales);+ memcpy(p, src->g, fm->gu_codes); p += fm->gu_codes;+ memcpy(p, src->u, fm->gu_codes); p += fm->gu_codes;+ memcpy(p, src->d, fm->d_codes); p += fm->d_codes;+ if (fm->gu_scales) { memcpy(p, src->gs, fm->gu_scales); p += fm->gu_scales; memcpy(p, src->us, fm->gu_scales); p += fm->gu_scales; }+ if (fm->d_scales) memcpy(p, src->ds, fm->d_scales);if (victim >= 0) {VSlot *w = &T.s[victim];- w->state = VS_EVICT; T.resident--; T.dres[w->dev]--; T.evictions++;+ w->state = VS_EVICT; T.resident--; if (kd) T.xres--; else T.dres[w->dev]--; T.evictions++;if (T.nevict == T.cevict) {int nc = T.cevict ? 2 * T.cevict : 64;int *n = realloc(T.evict, (size_t)nc * sizeof(int));- if (!n) { w->state = VS_RESIDENT; T.resident++; T.dres[w->dev]++; T.evictions--; free(buf); return; }+ if (!n) { w->state = VS_RESIDENT; T.resident++; if (kd) T.xres++; else T.dres[w->dev]++; T.evictions--; free(buf); return; }T.evict = n; T.cevict = nc;}T.evict[T.nevict++] = victim;pthread_mutex_lock(&T.mx); T.evict_pending = T.nevict; pthread_mutex_unlock(&T.mx);quiesce(); /* nothing in flight: free it right away; else at the join */}- v->state = VS_QUEUED; v->dev = (uint8_t)dev; T.queued++; T.dque[dev]++; T.promos++;+ v->state = VS_QUEUED; v->dev = (uint8_t)dev; T.queued++; if (kd) T.xque++; else T.dque[dev]++; T.promos[kd]++;pthread_mutex_lock(&T.mx);T.q[(T.qh + T.qn) % VKT_QCAP] = (VQ){layer, eid, dev, buf}; T.qn++;pthread_cond_signal(&T.cv);@@ -535,8 +558,8 @@ void vkt_note(int layer, int eid, const VktExpertSrc *src) {int vkt_wants(int layer, int eid) {if (!T.on || layer < 0 || layer >= T.c.layers || eid < 0 || eid >= T.c.experts) return 0;VSlot *v = slot(layer, eid);- if (v->state != VS_NONE || T.promos >= T.promo_cap) return 0;- if (room_dev() < 0 && (!v->heat || peek_victim(score(v)) < 0)) return 0;+ if (v->state != VS_NONE || T.promos[kind_of(layer)] >= T.promo_cap) return 0;+ if (room_dev(layer) < 0 && (!v->heat || peek_victim(kind_of(layer), score(v)) < 0)) return 0;pthread_mutex_lock(&T.mx);int qfull = T.qn >= VKT_QCAP;pthread_mutex_unlock(&T.mx);@@ -554,7 +577,7 @@ static int plan_cmp(const void *a, const void *b) {static uint32_t *const *g_heat_hist;int vkt_plan(int *layers, int *eids, int max) {if (!T.on || !g_heat_hist) return 0;- int L = T.c.layers, E = T.c.experts, n = 0;+ int L = T.main_layers, E = T.c.experts, n = 0; /* the history: the main layers' */int *order = malloc((size_t)L * E * sizeof(int));if (!order) return 0;for (int l = 0; l < L; l++)@@ -562,10 +585,10 @@ int vkt_plan(int *layers, int *eids, int max) {g_plan_heat = (const uint32_t *const *)g_heat_hist; g_plan_E = E;qsort(order, (size_t)n, sizeof(int), plan_cmp);int k = 0;- for (int i = 0; i < n && k < max && room_dev() >= 0; i++) {+ for (int i = 0; i < n && k < max && room_dev(0) >= 0; i++) {VSlot *v = &T.s[order[i]];if (v->state != VS_NONE) continue;- int dev = room_dev(); /* the hottest fill the primary device, the next ones the second */+ int dev = room_dev(0); /* the hottest fill the primary device, the next ones the second */v->state = VS_QUEUED; v->dev = (uint8_t)dev; T.queued++; T.dque[dev]++;layers[k] = order[i] / E; eids[k] = order[i] % E; k++;}@@ -576,7 +599,7 @@ int vkt_put(int layer, int eid, const VktExpertSrc *src) {if (!T.on || layer < 0 || layer >= T.c.layers || eid < 0 || eid >= T.c.experts) return 0;ColiVkTensor *t[3] = {NULL, NULL, NULL};int dev = slot(layer, eid)->dev; /* vkt_plan's choice */- int r = src && src->g && src->u && src->d ? upload(dev, src->g, src->u, src->d, src->gs, src->us, src->ds, t) : 0;+ int r = src && src->g && src->u && src->d ? upload(dev, kind_of(layer), src->g, src->u, src->d, src->gs, src->us, src->ds, t) : 0;int ok = r > 0;push_done(layer, eid, ok, r < 0, t);if (ok) __atomic_add_fetch(&T.warm, 1, __ATOMIC_RELAXED);@@ -677,7 +700,7 @@ static int st_alloc(void) {VStage *v = &T.st[n];int ok = 1;for (int k = 0; k < 3 && ok; k++) {- int fmt = k < 2 ? T.gu_fmt : T.dn_fmt, gs = k < 2 ? T.gu_gs : T.dn_gs;+ int fmt = k < 2 ? T.form[0].gu_fmt : T.form[0].dn_fmt, gs = k < 2 ? T.form[0].gu_gs : T.form[0].dn_gs;uint8_t *rows; size_t stride; float *sc;ok = coli_vk_tier_tensor(&v->t[k], fmt, k < 2 ? H : F, k < 2 ? F : H, gs, &rows, &stride, &sc);if (!ok) v->t[k] = NULL;@@ -706,12 +729,12 @@ static int st_alloc(void) {* parallel, each expert into its own slot. */static int st_put(int layer, int eid, VStage *v, const VktExpertSrc *src, int par) {const int H = T.c.hidden, F = T.c.inter;- if (!src->g || !src->u || !src->d || (T.gu_scales && (!src->gs || !src->us || !src->ds))) return 0;+ if (!src->g || !src->u || !src->d || (T.form[0].gu_scales && (!src->gs || !src->us || !src->ds))) return 0;const uint8_t *codes[3] = {src->g, src->u, src->d}; const void *scs[3] = {src->gs, src->us, src->ds};for (int k = 0; k < 3; k++) {uint8_t *rows; size_t stride; float *sc;if (!coli_vk_tensor_refill(v->t[k], &rows, &stride, &sc)) return 0;- convert_par(k < 2 ? T.c.gate_up : T.c.down, k < 2 ? H : F, k < 2 ? F : H, codes[k], scs[k], rows, stride, sc, par);+ convert_par(k < 2 ? T.form[0].gu : T.form[0].dn, k < 2 ? H : F, k < 2 ? F : H, codes[k], scs[k], rows, stride, sc, par);}(void)layer; (void)eid;return 1;@@ -1098,7 +1121,7 @@ static int join_big(const float **rows) {/* layer's likely streamed experts into the free slots, while the device runs its attention */int vkt_stream_prefetch(int layer, int S) {- if (!T.on || !T.st_ok || T.inflight || layer < 0 || layer >= T.c.layers || S < T.gemm_rows) return 0;+ if (!T.on || !T.st_ok || T.inflight || layer < 0 || layer >= T.main_layers || S < T.gemm_rows) return 0;if (!coli_vk_xb_ready() || !st_alloc()) return 0;const int E = T.c.experts, K = T.c.topk;int R = stream_min_rows();@@ -1163,7 +1186,7 @@ static int issue(int layer, const float *x, int S, int K, const int *idx, const}T.last_layer = layer;quiesce();- if (T.st_ok && S >= T.gemm_rows) return issue_big(layer, x, S, K, idx, w, taken);+ if (T.st_ok && S >= T.gemm_rows && layer < T.main_layers) return issue_big(layer, x, S, K, idx, w, taken);int E = T.c.experts, H = T.c.hidden, n = S * K;if (!grow_map(n)) return 0;/* heat for every routed expert, groups for the resident ones (first seen first) */@@ -1173,6 +1196,7 @@ static int issue(int layer, const float *x, int S, int K, const int *idx, constint e = idx[i];if (e < 0 || e >= E) continue;T.routed++;+ if (layer >= T.main_layers) T.extra_routed++;VSlot *v = slot(layer, e);if (v->heat < 0xFFFFFFFFu) v->heat++;v->last = T.tick;@@ -1276,6 +1300,7 @@ static int issue(int layer, const float *x, int S, int K, const int *idx, const}if (!took) return 0;T.inflight = 1; T.S = S; T.K = K; T.steps++; T.served += (unsigned long long)took;+ if (layer >= T.main_layers) T.extra_served += (unsigned long long)took;if (T.d2_inflight) { T.d2_served += (unsigned long long)(total - rows0); T.d2_steps++; }T.t_issued = vkt_now_ms();return took;@@ -1343,6 +1368,21 @@ int vkt_wanted(void) {return !(on && *on == '0');}+/* A form of the experts: the device formats and the bytes of one expert as the engine+ * holds it. 0 when it has no device form. */+static int form_init(int k, VktFmt gu, VktFmt dn) {+ __typeof__(T.form[0]) *fm = &T.form[k];+ const int H = T.c.hidden, F = T.c.inter;+ if (!dev_fmt(gu, &fm->gu_fmt, &fm->gu_gs) || !dev_fmt(dn, &fm->dn_fmt, &fm->dn_gs)) return 0;+ fm->gu = gu; fm->dn = dn;+ fm->gu_codes = src_row_bytes(gu.kind, H) * (size_t)F;+ fm->d_codes = src_row_bytes(dn.kind, F) * (size_t)H;+ fm->gu_scales = src_scale_bytes(gu, H, F);+ fm->d_scales = src_scale_bytes(dn, F, H);+ fm->stage_bytes = 2 * fm->gu_codes + fm->d_codes + 2 * fm->gu_scales + fm->d_scales;+ return 1;+}+/* COLI_VK_DEV2: the second device's share. Its budget is what it has free less* COLI_VK_RESERVE2_GB (0.5 GiB: its batch's scratch, the driver), an integrated GPU's a* quarter of what the RAM leaves (as the primary's), capped by COLI_VK_EXPERTS2 experts@@ -1412,7 +1452,8 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {T.c = *cfg;snprintf(T.engine, sizeof T.engine, "%s", eng);if (T.c.layers < 1 || T.c.experts < 1 || T.c.hidden < 1 || T.c.inter < 1 || T.c.topk < 1) return 0;- if (!dev_fmt(T.c.gate_up, &T.gu_fmt, &T.gu_gs) || !dev_fmt(T.c.down, &T.dn_fmt, &T.dn_gs)) {+ T.main_layers = T.c.layers;+ if (!form_init(0, T.c.gate_up, T.c.down)) {fprintf(stderr, "[VK] tier %s: expert format %d/%d (gs %d/%d) has no device form, the experts stay on the CPU\n",eng, T.c.gate_up.kind, T.c.down.kind, T.c.gate_up.gs, T.c.down.gs);return 0;@@ -1424,12 +1465,17 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {return 0;}int H = T.c.hidden, F = T.c.inter;- T.gu_codes = src_row_bytes(T.c.gate_up.kind, H) * (size_t)F;- T.d_codes = src_row_bytes(T.c.down.kind, F) * (size_t)H;- T.gu_scales = src_scale_bytes(T.c.gate_up, H, F);- T.d_scales = src_scale_bytes(T.c.down, F, H);- T.stage_bytes = 2 * T.gu_codes + T.d_codes + 2 * T.gu_scales + T.d_scales;T.exp_bytes = vkt_expert_bytes(H, F, T.c.gate_up, T.c.down);+ /* the extra layers (an MTP head's): on the tier when their form has a device form */+ if (T.c.extra_layers > 0) {+ if (form_init(1, T.c.extra_gate_up, T.c.extra_down) && T.exp_bytes &&+ (T.x_bytes = vkt_expert_bytes(H, F, T.c.extra_gate_up, T.c.extra_down)))+ T.c.layers += T.c.extra_layers;+ else+ fprintf(stderr, "[VK] tier %s: the extra layers' expert format %d/%d (gs %d/%d) has no device form, their experts stay on the CPU\n",+ eng, T.c.extra_gate_up.kind, T.c.extra_down.kind, T.c.extra_gate_up.gs, T.c.extra_down.gs);+ }+ if (T.main_layers == T.c.layers) T.c.extra_layers = 0;/* The budget. Discrete: what VK_EXT_memory_budget says is free on the device,* minus a reserve (scratch, KV mirrors, driver) and the dense weights still to@@ -1462,8 +1508,29 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {size_t full = (size_t)want / blk, rest = (size_t)want - full * blk;fit = (long long)full * (long long)(blk / T.exp_bytes) + (long long)(rest / T.exp_bytes);}- long long all = (long long)T.c.layers * T.c.experts, fit_raw = fit;+ long long all = (long long)T.main_layers * T.c.experts, fit_raw = fit;+ /* The extra layers' pool: all their experts when the budget holds them beside every+ * main one, else their layers' share of the budget (at least one expert, when two+ * main ones still fit beside it). Its room, in main experts, leaves the main count. */+ if (T.c.extra_layers) {+ long long xall = (long long)T.c.extra_layers * T.c.experts, xn;+ double bud = (double)fit_raw * T.exp_bytes;+ if (bud >= (double)all * T.exp_bytes + (double)xall * T.x_bytes) xn = xall;+ else xn = (long long)(bud * T.c.extra_layers / (T.main_layers + T.c.extra_layers) / T.x_bytes);+ if (xn < 1 && bud >= (double)T.x_bytes + 2.0 * T.exp_bytes) xn = 1;+ if (xn > xall) xn = xall;+ if (xn < 1) {+ char hx[32];+ fprintf(stderr, "[VK] tier %s: no room for an extra layer's expert (%s), they stay on the CPU\n",+ eng, human((double)T.x_bytes, hx, sizeof hx));+ T.c.layers = T.main_layers; T.c.extra_layers = 0;+ } else {+ long long take = (long long)(((size_t)xn * T.x_bytes + T.exp_bytes - 1) / T.exp_bytes);+ T.xmax = (int)xn; T.x_budget = (size_t)take * T.exp_bytes; fit_raw -= take;+ }+ }if (fit > all) fit = all;+ if (fit > fit_raw) fit = fit_raw;if (fit < 2) {char hb[32], he[32];fprintf(stderr, "[VK] tier %s: no room for experts (budget %s, %s each; COLI_VK_TIER_GB sets it), the experts stay on the CPU\n",@@ -1511,10 +1578,11 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {/* Every expert fits in one block's worth: the pool's limit (and so its one block)* is what they take, not the budget, so a small model does not hold a 256 MB* block for a few experts. */- size_t lim = T.budget, need = (size_t)(all + 1 + (T.st_ok ? T.st_slots : 0)) * T.exp_bytes;+ size_t lim = T.budget - T.x_budget, need = (size_t)(all + 1 + (T.st_ok ? T.st_slots : 0)) * T.exp_bytes;if (fit + (T.st_ok ? T.st_slots : 0) >= all && fit_raw >= all + (T.st_ok ? T.st_slots : 0) &&need < lim && need <= coli_vk_block_bytes((size_t)256 << 20)) lim = need;coli_vk_tier_pool_limit(lim);+ if (T.xmax) coli_vk_tier_extra_pool_limit(T.x_budget);const char *ex = getenv("COLI_VK_TIER_EXCLUSIVE");T.excl = !(ex && *ex == '0');if (T.excl) {@@ -1528,7 +1596,7 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {const char *r = getenv("COLI_VK_TIER_RATE");T.rate = r && *r ? atoi(r) : 16;if (T.rate < 0) T.rate = 0;- T.s = calloc((size_t)all, sizeof(VSlot));+ T.s = calloc((size_t)T.c.layers * T.c.experts, sizeof(VSlot));T.grp = malloc((size_t)T.c.experts * sizeof(int));if (!T.s || !T.grp) { free(T.s); free(T.grp); return 0; }for (int e = 0; e < T.c.experts; e++) T.grp[e] = -1;@@ -1539,7 +1607,7 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {* from a long history would hold the tier for hours). */g_heat_hist = heat;if (heat)- for (int l = 0; l < T.c.layers; l++) {+ for (int l = 0; l < T.main_layers; l++) { /* the history has the main layers */if (!heat[l]) continue;uint32_t mx = 0;for (int e = 0; e < T.c.experts; e++) if (heat[l][e] > mx) mx = heat[l][e];@@ -1555,15 +1623,20 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {char hb[32], he[32];fprintf(stderr, "[VK] tier %s: on, %s, budget %s = %d experts of %s (fmt %d", eng,coli_vk_device_name(), human((double)T.budget, hb, sizeof hb), T.dmax[0],- human((double)T.exp_bytes, he, sizeof he), T.gu_fmt);- if (T.gu_gs) fprintf(stderr, " gs %d", T.gu_gs);- fprintf(stderr, ", down fmt %d", T.dn_fmt);- if (T.dn_gs) fprintf(stderr, " gs %d", T.dn_gs);+ human((double)T.exp_bytes, he, sizeof he), T.form[0].gu_fmt);+ if (T.form[0].gu_gs) fprintf(stderr, " gs %d", T.form[0].gu_gs);+ fprintf(stderr, ", down fmt %d", T.form[0].dn_fmt);+ if (T.form[0].dn_gs) fprintf(stderr, " gs %d", T.form[0].dn_gs);fprintf(stderr, "), %s, %s queue, up to %d promotions per token%s%s",T.uma ? (cap && *cap ? "shared RAM (COLI_VK_TIER_GB)" : "shared RAM: a quarter of what the expert cache leaves"): "device memory", coli_vk_xb_queue_shared() ? "shared" : "own", T.rate,T.balance ? ", balanced against the CPU" : "", T.sync ? ", uploads awaited (COLI_VK_TIER_SYNC)" : "");if (T.st_ok) fprintf(stderr, ", streaming cold experts of prompt steps through %d slots", T.st_slots);+ if (T.xmax) {+ char hx[32], hp[32];+ fprintf(stderr, "; the extra layers' experts (%d, fmt %d): %d of %s in a pool of %s", T.c.extra_layers,+ T.form[1].gu_fmt, T.xmax, human((double)T.x_bytes, hx, sizeof hx), human((double)T.x_budget, hp, sizeof hp));+ }fprintf(stderr, "\n");d2_init(fit, all, act); /* COLI_VK_DEV2: the experts after these on a second device */return 1;@@ -1586,6 +1659,9 @@ void vkt_report(const char *scope, unsigned long long ram_hits, unsigned long lohuman((double)T.upload_bytes, hl, sizeof hl), T.warm, T.evictions, T.qfull, T.rated, T.failed,T.dev_ms, T.cpu_ms, T.wait_ms, T.dev_ms > 0 ? 100.0 * hidden / T.dev_ms : 0.0);if (T.balance) fprintf(stderr, " | balance: device share %.2f, %llu rows handed to the CPU", T.share, T.handed);+ if (T.c.extra_layers)+ fprintf(stderr, " | extra layers (%d): %llu of %llu routed experts on the device, resident %d (budget %d)",+ T.c.extra_layers, T.extra_served, T.extra_routed, T.xres, T.xmax);if (T.excl) fprintf(stderr, " | exclusive: %llu RAM copies of device experts given up first",__atomic_load_n(&T.ram_gave, __ATOMIC_RELAXED));if (T.st_ok && T.st_steps) {@@ -1611,6 +1687,7 @@ void vkt_report(const char *scope, unsigned long long ram_hits, unsigned long lo}int vkt_devices(void) { return !T.on ? 0 : T.d2 ? 2 : 1; }+int vkt_layers(void) { return T.on ? T.c.layers : 0; }static VSlot *g_old_slots; /* the slot table of the tier shut down last (vkt_shutdown) */static uint32_t *g_old_routed, *g_old_seq;diff --git a/c/vk_tier.h b/c/vk_tier.hindex 586240da..c496fbaf 100644--- a/c/vk_tier.h+++ b/c/vk_tier.h@@ -197,6 +197,13 @@ typedef struct {* the first ones in order. A partial batch is consumed and released before the* next batch; 0 falls back to load for the next expert. */int (*load_batch)(void *ctx, int layer, const int *eids, int n, VktExpertSrc *srcs, void **h);+ /* Optional: layers past `layers` (an MTP head's) whose experts the engine holds in another+ * form (extra_gate_up, extra_down), with the same geometry: index them layers..+ * layers+extra_layers-1 in every call. They never stream (an MTP head's steps are a few+ * rows; streaming's slots are in the main form) and have no history (heat rows: the+ * main layers only). 0: none. */+ int extra_layers;+ VktFmt extra_gate_up, extra_down;} VktConfig;@@ -242,6 +249,9 @@ int vkt_step_rows(int S, int block);int vkt_stream_prefetch(int layer, int S);/* How many devices hold experts: 0 (the tier is off), 1, or 2 (COLI_VK_DEV2's too). */int vkt_devices(void);+/* The layers the tier serves: VktConfig.layers plus the extra layers it took (an MTP+ * head's, when their form has a device form); 0 when it is off. */+int vkt_layers(void);/* Exclusive RAM/VRAM: 1 when the device holds this expert and the tier wants the RAM* copy given up first (COLI_VK_TIER_EXCLUSIVE unset or not 0). An engine's RAM cache* asks it while it picks the slot to evict (prefer such a slot to its LRU choice) and@@ -269,6 +279,7 @@ static inline size_t vkt_expert_bytes(int h,int i,VktFmt a,VktFmt b){(void)h;(vostatic inline int vkt_step_rows(int S,int b){return S<b?S:b;}static inline int vkt_stream_prefetch(int l,int S){(void)l;(void)S;return 0;}static inline int vkt_devices(void){return 0;}+static inline int vkt_layers(void){return 0;}static inline int vkt_ram_first(int l,int e){(void)l;(void)e;return 0;}static inline void vkt_ram_gave(void){}#endifdiff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.mdindex 6d2d8025..334a4ced 100644--- a/docs/ENVIRONMENT.md+++ b/docs/ENVIRONMENT.md@@ -244,6 +244,7 @@ With `COLI_VULKAN=1` every MoE engine (qwen36, qwen38, inkling, olmoe, kimi_k3,| `COLI_VK_TIER_RESERVE_GB` | `1` | Device memory left to everything but the experts (scratch, KV mirrors, the driver), on top of the dense weights. || `COLI_VK_TIER_RATE` | `16` | Promotions per token at most (a prompt's forward gets this many per prompt token); each copies one expert on the engine thread before the uploader thread writes it to the device. || `COLI_VK_TIER_WARM` | on | `0`: no warm start from the history; the tier fills as experts pass by. |+| `COLI_VK_TIER_MTP` | on a discrete GPU, off on one that shares the RAM | qwen38: `1` puts the MTP head's MoE layer on the tier as an extra layer, its experts in the form the snapshot keeps them (FP8 beside the int4 sidecar); `0` keeps them on the CPU. On a Radeon 780M it measured 3.44 tok/s against 3.48 without, hence the default there ([vulkan.md](vulkan.md#the-mtp-heads-layer-on-the-tier-coli_vk_tier_mtp)). || `COLI_VK_TIER_SYNC` | `0` | `1`: every layer step waits for the uploads staged so far before it runs, so which experts are resident depends on the routing alone, not on when the uploader thread ran (the tests set it: a tiny fixture's run can end before that thread is first scheduled). With `COLI_VK_TIER_BALANCE=0` as well, a run with the tier is reproducible. A promotion that displaces a resident while a batch is in flight waits for the batch's join to free it. Costs the overlap of uploads with compute. || `COLI_VK_TIER_BALANCE` | on | `0`: the device takes every resident expert of a step. On, when a join keeps waiting for the device, the step's resident experts the CPU also holds in RAM go back to the CPU beyond the device's share, which moves with every join (down when the CPU waited more than a tenth of its own time, up when the device finished before 80% of it). || `COLI_VK_TIER_GEMM_ROWS` | `16` | Rows from which an expert of a step takes the tiled GEMM (prefill) instead of the per-row GEMV; `0` never. Below it a row's bits do not depend on how many rows share the dispatch. |diff --git a/docs/vulkan.md b/docs/vulkan.mdindex 75c06bed..7de01522 100644--- a/docs/vulkan.md+++ b/docs/vulkan.md@@ -114,7 +114,7 @@ the shared expert tier ([below](#the-routed-expert-tier-vk_tierc)).| Engine | On the device | Weight formats | Stays on the CPU ||---|---|---|---|| qwen36 (Qwen3.6, Qwen3-Coder, Qwen3.8-27B, Clef) | the dense trunk; routed experts on the expert tier | int8 rows; int4-g64 with `COLI_DENSE_BITS=4`; f16 (fmt 14) with `COLI_DENSE_BITS=16`; f32 with `COLI_DENSE_I8=0`; experts int4-g64, int4 per row, int8 per row or gs64 | DeltaNet `dn_a`/`dn_b`, vision tower, Clef's joint head, the experts the tier does not hold |-| qwen38 (Qwen3.8 Flash Next) | the trunk; routed experts on the expert tier | int8 trunk rows, bf16, f32 (`Q38_NATIVE_BF16=0`); experts int4-g64 (sidecar), FP8 128x128 blocks, bf16 | the MTP head's experts, the experts the tier does not hold |+| qwen38 (Qwen3.8 Flash Next) | the trunk; routed experts on the expert tier, the MTP head's too on a discrete GPU | int8 trunk rows, bf16, f32 (`Q38_NATIVE_BF16=0`); experts int4-g64 (sidecar), FP8 128x128 blocks, bf16 | the experts the tier does not hold || inkling | dense and shared-expert matrices; routed experts on the expert tier | int8 and int4-g64 (dense-int4g64 container), f32, bf16; experts int4 or int8 per row (container or runtime quantization), f32 | embedding and audio lookups, CUDA residents (with CUDA or Metal on, the experts too); bf16 on CPUs with the AVX512-BF16 dot (see below); the experts the tier does not hold || olmoe | attention q/k/v/o, router, lm_head; routed experts on the expert tier | f32; experts int8 per row | embedding, the experts the tier does not hold || kimi_k3 (Kimi K3) | the shared experts' matrices (one row at a time: decode); routed experts on the expert tier | shared experts int8 rows, int4-g64, f32 (`K3_BITS`); experts MXFP4 with ue8m0 scales (fmt 7), SiTU-GLU in the latent space | KDA, MLA, the latent projections, router, head, prefill's shared experts, the experts the tier does not hold |@@ -451,7 +451,7 @@ order its CPU-only run does, and keeps a history for the warm start where it has| Engine | Experts in RAM (`VktSrc`), device format | Activation | Warm start from | Of its own ||---|---|---|---|---|| qwen36 (Qwen3.6, Qwen3-Coder, the 2.4T geometry) | int8 per row `I8_ROW` or gs64 `I8_GS` (fmt 1, 13); int4 per row or gs64 from the int8-slot kernel, `I8_AS_I4_ROW` / `I8_AS_I4_GS` (fmt 2, 4); planar int4-g64 from the int4 kernel, `I4U_PLANAR64` (fmt 4); the mixed container's int4 gate/up and int8 down | SwiGLU | `COLI_USAGE` (default `<snap>/.coli_usage`), kept only while the tier is on | the tier's experts match `QWEN_EXPERT_ACT=f32` (the default kernel rounds activations to int8) |-| qwen38 (Qwen3.8 Flash Next) | the int4-g64 sidecar `I4U_PLANAR64` (fmt 4); the release's FP8 in 128x128 blocks `FP8_BLOCK` (fmt 12); `BF16` (fmt 11); `F32` (fmt 10) | SwiGLU | `COLI_USAGE` (default `<snap>/.coli_usage`), always kept | the MTP head's layer stays on the CPU (its experts are FP8 beside an int4 sidecar) |+| qwen38 (Qwen3.8 Flash Next) | the int4-g64 sidecar `I4U_PLANAR64` (fmt 4); the release's FP8 in 128x128 blocks `FP8_BLOCK` (fmt 12); `BF16` (fmt 11); `F32` (fmt 10) | SwiGLU | `COLI_USAGE` (default `<snap>/.coli_usage`), always kept | the MTP head's layer is an extra layer of its own form (FP8 beside an int4 sidecar), [below](#the-mtp-heads-layer-on-the-tier-coli_vk_tier_mtp) || inkling | int4 container `I4U_PAIRS_ROW` (fmt 2); int8 container `I8_ROW` (fmt 1); runtime int8 rows, `I8_AS_I4_ROW` at 2 to 4 bits (fmt 2) and `I8_ROW` above; `F32` at `bits=0` (fmt 10). Gate and up come from the fused `gate_up` tensor, up I rows in | SwiGLU | `<snap>/.coli_usage` or `PIN=<path>`, in the generate and serve modes; the ref.json oracle reads none | [Inkling and OLMoE](#inkling-and-olmoe) || olmoe | int8 rows `I8_ROW` (fmt 1), gate, up and down as the merged container holds them | SwiGLU | `COLI_USAGE` only | [Inkling and OLMoE](#inkling-and-olmoe) || kimi_k3 (Kimi K3) | the checkpoint's MXFP4 with ue8m0 scales `MXFP4_E8M0` 32 (fmt 7): gate `w1`, up `w3`, down `w2`, in the latent space | SiTU-GLU (`VKT_ACT_SITU`) | `COLI_USAGE` (default `<snap>/.coli_usage`) | [Kimi K3 and MiMo](#kimi-k3-and-mimo) |@@ -564,6 +564,57 @@ The `dev2` families' `excl` and `noexcl` cases run every engine with a RAM cacheor two against its CPU run. On DeepSeek V4's 16-expert fixture with 6 slots a layer, therun with it gave up 21 RAM copies and read 46 experts from disk, against 51 without it.+### The MTP head's layer on the tier (`COLI_VK_TIER_MTP`)++Qwen3.8's MTP head drafts with a MoE layer of its own (index `layers`), whose experts the+snapshot keeps in their own form: FP8 in 128x128 blocks beside the int4-g64 sidecar, which+covers the model's layers only. With `COLI_VK_TIER_MTP=1`, the default on a discrete GPU,+the tier takes that layer as an extra layer (`VktConfig.extra_layers`, with+`extra_gate_up` and `extra_down`): its experts are promoted+as the drafts pass by, served by the same batches as any layer's, and given up to the+exclusive RAM cache the same way. What differs:++- **A pool of its own.** An FP8 expert is about twice an int4 one, and in one pool the+ holes an evicted int4 expert leaves are too small for it. The extra layer's experts sit+ in a pool of their own on the primary device, where a newcomer displaces only another+ extra expert, so each pool holds one size. The pool gets every extra expert when the+ budget holds them beside every main one, else the extra layers' share of the budget+ (their layers over all the layers, at least one expert), and its bytes leave the main+ experts' count. They never go to a second device.+- **No history, no streaming.** The history and the warm start cover the model's layers;+ the drafts fill the extra layer, with `COLI_VK_TIER_RATE` promotions per token of its+ own. It comes last in a forward, after the model's layers have spent theirs: on the+ release they always do, and with one shared rate the head's layer got no expert at+ all. Big prompt steps never reach it (the head drafts a few rows at a time), so it+ takes no streaming slots.+- **The lines.** The startup line adds `; the extra layers' experts (1, fmt 12): X of S+ in a pool of P`, the run's line `| extra layers (1): N of M routed experts on the+ device, resident R (budget X)`.++**Measured** on the Radeon 780M of [speculative.md](speculative.md#measured) with the same+command as its Vulkan runs (the tier alone, int4-g64 sidecar, three drafts, cap 170, the+code-edit prompt, 128 tokens, the same starting history, model pages evicted before every+run), three runs each, alternating:++| MTP head's experts | tok/s | head's routed experts on the device | model's layers on the device |+|---|---|---|---|+| on the CPU (`COLI_VK_TIER_MTP=0`) | 3.48, 3.49, 3.46 | | 90382-90410 of 176640 |+| on the tier (`COLI_VK_TIER_MTP=1`) | 3.44, 3.44, 3.46 | 1095-1160 of 4280 (31 resident, 4.8 MiB each) | 89773-90343 of 176640 |++All six answers were byte-identical, and so was the head's acceptance (95 of 95 drafts).+On this integrated GPU the head's experts on the device bought nothing: their pool (149+MiB) came out of the model's layers' budget, whose share on the device fell a little. So+the default puts them on the tier on a discrete GPU only;+`COLI_VK_TIER_MTP=1` or `0` decides either way. A discrete GPU was not measured here.++When the head's experts have no device form (`[VK] tier qwen38: the extra layers' expert+format ... has no device form`), mix formats or find no room, they stay on the CPU. The tokens are the CPU run's either way:+the MTP layer's experts join the row in rank order like the others'.+`tests/test_vk_tier`'s `extra` case gates the pools (an f32 extra layer, eight times an+int4 expert: each pool evicts its own kind, no upload refused, an extra expert gets in+beside a full main pool on its own promotions), and the `qwen` family runs the head's+layer on the device on both MTP fixtures.+### A second device (`COLI_VK_DEV2`)A machine with two GPUs (a V100 beside a GTX 1070, an RX 9070 beside an RX 580) can@@ -959,7 +1010,9 @@ partial-qwen36-olmoe` gates it on Lavapipe ([below](#olmoe-and-inkling)).**What stays on the CPU.** The routed experts the tier does not hold, the router'stop-k, the embedding gather and the vision tower's rows, Qwen3.8's n-gram table reads-and the MTP head (its experts are FP8 beside the int4 sidecar, a few rows per draft).+and the MTP head's layer apart from its matrices (with the full chain the per-matrix path+runs those on the device, and its routed experts are on the expert tier as an extra+layer).The chain declines, and the per-matrix path runs with the state synced first, underthe CUDA expert tier (CUDA keeps its priority), a qpack container, PILOT prefetch, ora geometry outside its shaders (head dim above 256, a DeltaNet value head above 128 or@@ -2166,7 +2219,9 @@ in short:(`VKT_ACT_SWIGLU` with an optional clamp, `VKT_ACT_SITU`, `VKT_ACT_SWIGLU_V4`), the most assignments astep carries, the RAM the expert cache may still take and the dense bytes still tocome to the device; optionally `.max_experts`, a count the engine's users already- size its device tier in (GLM-5.2's `COLI_VK_EXPERTS`). `atexit(coli_vk_shutdown)`,+ size its device tier in (GLM-5.2's `COLI_VK_EXPERTS`), and `.extra_layers` with+ their own `VktFmt` for layers past the model's whose experts RAM holds in another+ form (an MTP head's: [above](#the-mtp-heads-layer-on-the-tier-coli_vk_tier_mtp)). `atexit(coli_vk_shutdown)`,then `vkt_init(&cfg, rt_counts_all())` after the device and the history, then`atexit(vkt_shutdown)` when it succeeds: at exit the tier lets go of its expertsfirst and the device is destroyed before the drivers unload, whether or not the