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

vulkan: the drafting heads' experts on the tier for colibri and deepseek_v41

JustVugge7f363f848b1
diff --git a/c/colibri.c b/c/colibri.c
index fd1b3780..0d1d834d 100644
--- a/c/colibri.c
+++ b/c/colibri.c
@@ -666,7 +666,8 @@ static inline int vk_reg_served(int layer,int eid){
/* The shared tier's view of this model's experts (vk_tier_start). */
static struct {
int on; /* vkt_init accepted the experts */
- int gu_fmt, gu_gs, dn_fmt, dn_gs; /* the QT formats it was given (gate = up) */
+ int nl; /* the model's layers; the MTP head's (index nl) is [1] below */
+ int gu_fmt[2], gu_gs[2], dn_fmt[2], dn_gs[2]; /* the QT formats it was given (gate = up) */
size_t exp_bytes; /* one expert on the device */
} g_vkt;
static int vk_tier_resident_count(Model *m);
@@ -5572,20 +5573,23 @@ static int mb_gs_compat(int est_fmt, int est_gs, int fmt, int gs){
static const void *vk_qt_codes(const QT *t){
return t->fmt==0 ? (const void*)t->qf : (t->fmt==1||t->fmt==8) ? (const void*)t->q8 : (const void*)t->q4;
}
-/* An expert slot as the tier reads it, or 0 when its tensors are not in the format the
- * tier was configured with (a mixed container): such an expert stays on the CPU. */
-static int vk_slot_src(const ESlot *e, VktExpertSrc *src){
- if(!g_vkt.on || e->g.fmt!=g_vkt.gu_fmt || e->u.fmt!=g_vkt.gu_fmt || e->g.gs!=g_vkt.gu_gs ||
- e->u.gs!=g_vkt.gu_gs || e->d.fmt!=g_vkt.dn_fmt || e->d.gs!=g_vkt.dn_gs ||
+/* An expert slot of `layer` as the tier reads it, or 0 when its tensors are not in the
+ * format the tier was configured with for that layer (the model's, or the MTP head's:
+ * int8 beside int4; a mixed container): such an expert stays on the CPU. */
+static int vk_slot_src(const ESlot *e, int layer, VktExpertSrc *src){
+ int k = layer>=g_vkt.nl;
+ if(!g_vkt.on || e->g.fmt!=g_vkt.gu_fmt[k] || e->u.fmt!=g_vkt.gu_fmt[k] || e->g.gs!=g_vkt.gu_gs[k] ||
+ e->u.gs!=g_vkt.gu_gs[k] || e->d.fmt!=g_vkt.dn_fmt[k] || e->d.gs!=g_vkt.dn_gs[k] ||
e->g.planar || e->u.planar || e->d.planar) return 0;
*src=(VktExpertSrc){vk_qt_codes(&e->g),vk_qt_codes(&e->u),vk_qt_codes(&e->d),e->g.s,e->u.s,e->d.s};
return src->g && src->u && src->d;
}
-/* Does this MoE call go through moe_vk? Layers 0..n_layers-1 only: the MTP head's
- * experts are int8 and stay on the CPU. CUDA, Metal, the cluster workers and the
+/* Does this MoE call go through moe_vk? The layers the tier serves: the model's, and
+ * the MTP head's (index n_layers) when the tier took it as an extra layer (its experts
+ * int8 beside int4, COLI_VK_TIER_MTP). CUDA, Metal, the cluster workers and the
* ablation harness keep moe()'s own loop. */
static int moe_vk_on(Model *m,int layer){
- if(!g_vulkan || layer<0 || layer>=m->c.n_layers || omp_in_parallel()) return 0;
+ if(!g_vulkan || layer<0 || layer>=(vkt_ready() ? vkt_layers() : m->c.n_layers) || omp_in_parallel()) return 0;
if(!vkt_ready() && g_vk_reg_n2<=0) return 0;
if(g_abl.mode || g_metal_enabled || g_pre_idx) return 0;
#if !defined(_WIN32)
@@ -5649,7 +5653,7 @@ static void vk_cpu_pairs(Model *m,int layer,const float *x,int rows,int K,const
if(g_prof){ m->t_ecpu+=dt; m->cpu_expert_rows+=(uint64_t)nr;
m->cpu_expert_bytes+=qt_bytes(&e->g)+qt_bytes(&e->u)+qt_bytes(&e->d); }
VktExpertSrc vs;
- if(note && vk_slot_src(e,&vs)) vkt_note(layer,eid,&vs); /* may promote it to the device */
+ if(note && vk_slot_src(e,layer,&vs)) vkt_note(layer,eid,&vs); /* may promote it to the device */
}
ecache_promote_ws(m,layer,nmiss);
}
@@ -10851,7 +10855,7 @@ static int vk_load_batch(void *ctx,int layer,const int *e,int n,VktExpertSrc *sr
for(int q=0;q<nmiss;q++) expert_load(m,layer,e[miss[q]],&m->ws[q],1,1);
m->t_ewait += now_s()-t0; }
int k=0;
- for(;k<n;k++){ if(!vk_slot_src(use[k],&srcs[k])) break; h[k]=m; }
+ for(;k<n;k++){ if(!vk_slot_src(use[k],layer,&srcs[k])) break; h[k]=m; }
g_vks_layer=layer; g_vks_nmiss=nmiss; g_vks_held=k;
if(!k) vk_stream_promote(m);
return k;
@@ -10911,8 +10915,24 @@ static void vk_tier_start(Model *m){
if(g_glmc_partial) dense+=(double)g_glmc_lazy;
{ const char *r=getenv("COLI_VK_RESERVE_GB"), *tr=getenv("COLI_VK_TIER_RESERVE_GB");
if(r && *r){ double extra=atof(r)-(tr&&*tr?atof(tr):1.0); if(extra>0) dense+=extra*1073741824.0; } }
+ /* The MTP head's layer (index NL): its experts as the container keeps them (int8
+ * beside int4), the tier's extra layer. By default on a discrete GPU only: on a GPU
+ * that shares the RAM, qwen38's head measured no faster there (docs/vulkan.md, "The
+ * MTP head's layer on the tier"); COLI_VK_TIER_MTP=1 or 0 decides. */
+ VktFmt xgu={VKT_SRC_NONE,0}, xdn={VKT_SRC_NONE,0}; int xf[3]={0,0,0}, xgs[3]={0,0,0};
+ { const char *tm=getenv("COLI_VK_TIER_MTP");
+ int want = tm&&*tm ? *tm!='0' : !coli_vk_device_shares_ram();
+ if(m->has_mtp && want){
+ for(int k=0;k<3;k++) xf[k]=vk_expert_fmt(m,NL,0,k,&xgs[k]);
+ if(xf[0]!=xf[1] || xgs[0]!=xgs[1] || !vk_src_kind(xf[0],xgs[0],&xgu) || !vk_src_kind(xf[2],xgs[2],&xdn)){
+ fprintf(stderr,"[VK] tier colibri: the MTP head's experts in fmt %d/%d/%d have no device form, they stay on the CPU\n",
+ xf[0],xf[1],xf[2]);
+ xgu.kind=VKT_SRC_NONE;
+ }
+ } }
VktConfig vc={.engine="colibri", .layers=NL, .experts=E, .hidden=c->hidden, .inter=c->moe_inter, .topk=c->topk,
.gate_up=gu, .down=dn, .act=VKT_ACT_SWIGLU, .act_limit=0.f,
+ .extra_layers=xgu.kind!=VKT_SRC_NONE, .extra_gate_up=xgu, .extra_down=xdn,
.max_rows=GLM_VK_ROWS*c->topk,
.ram_reserve=(size_t)((double)m->ecap*expert_cache_row_bytes(m,m->ebits)),
.dense_bytes=(size_t)dense,
@@ -10921,7 +10941,8 @@ static void vk_tier_start(Model *m){
.load=vk_load, .release=vk_unhold, .load_ctx=m, .load_batch=vk_load_batch};
atexit(coli_vk_shutdown); /* before vkt_init, which makes the expert batch's pipelines and can still refuse (no room): the device goes at exit either way, after the tier's teardown */
if(!vkt_init(&vc,m->eusage)) return;
- g_vkt.on=1; g_vkt.gu_fmt=f[0]; g_vkt.gu_gs=gs[0]; g_vkt.dn_fmt=f[2]; g_vkt.dn_gs=gs[2];
+ g_vkt.on=1; g_vkt.nl=NL; g_vkt.gu_fmt[0]=f[0]; g_vkt.gu_gs[0]=gs[0]; g_vkt.dn_fmt[0]=f[2]; g_vkt.dn_gs[0]=gs[2];
+ g_vkt.gu_fmt[1]=xf[0]; g_vkt.gu_gs[1]=xgs[0]; g_vkt.dn_fmt[1]=xf[2]; g_vkt.dn_gs[1]=xgs[2];
g_vkt.exp_bytes=vkt_expert_bytes(c->hidden,c->moe_inter,gu,dn);
atexit(vkt_shutdown);
g_vk_model=m; atexit(vk_tier_report_run); /* runs first: the tier is still up */
@@ -10937,9 +10958,9 @@ static void vk_tier_start(Model *m){
#pragma omp parallel for schedule(dynamic,4)
for(int i=0;i<n;i++){
VktExpertSrc vs; ESlot *p=pin_indexed(m,ql[i],qe[i]);
- if(p && vk_slot_src(p,&vs)){ vkt_put(ql[i],qe[i],&vs); continue; }
+ if(p && vk_slot_src(p,ql[i],&vs)){ vkt_put(ql[i],qe[i],&vs); continue; }
ESlot *t=&tmp[omp_get_thread_num()];
- int ok = expert_load(m,ql[i],qe[i],t,0,0)==0 && vk_slot_src(t,&vs); /* demand=0: startup */
+ int ok = expert_load(m,ql[i],qe[i],t,0,0)==0 && vk_slot_src(t,ql[i],&vs); /* demand=0: startup */
vkt_put(ql[i],qe[i],ok?&vs:NULL); /* NULL: planned but not placed (a mixed container) */
}
vkt_put_done();
diff --git a/c/deepseek_v41.c b/c/deepseek_v41.c
index 60de2eca..65abcabd 100644
--- a/c/deepseek_v41.c
+++ b/c/deepseek_v41.c
@@ -1614,15 +1614,20 @@ static void ehit_mark(Model *m, int layer, int eid) {
* DSpark stage: same slot shapes, same LRU, a different set of experts. Only the
* backbone's routing reaches the dashboard's grid -- the draft head has its own,
* smaller expert set, and a row of it would not line up with anything. */
-/* The slot a full cache gives up: the least recently used, and before it (the
- * backbone's layers: the DSpark stages keep their experts on the CPU) the least
+/* The Vulkan tier's index of a MoE call's layer: a backbone layer's own, a DSpark
+ * stage's n_layers + stage (the tier's extra layers, COLI_VK_TIER_MTP; a stage the tier
+ * did not take answers no to everything there). */
+static int v41_tier_layer(const Model *m, const char *kind, int layer) {
+ return !strcmp(kind, "layers") ? layer : m->c.n_layers + layer;
+}
+/* The slot a full cache gives up: the least recently used, and before it the least
* recently used of those whose expert the Vulkan tier holds (vkt_ram_first). */
-static int v41_victim(const LCache *cache, const char *kind, int layer) {
- int oldest = 0, dev = -1, backbone = !strcmp(kind, "layers");
+static int v41_victim(const Model *m, const LCache *cache, const char *kind, int layer) {
+ int oldest = 0, dev = -1, tl = v41_tier_layer(m, kind, layer);
for (int i = 1; i < cache->n; i++)
if (cache->slot[i].used < cache->slot[oldest].used) oldest = i;
- for (int i = 0; backbone && i < cache->n; i++)
- if (cache->slot[i].eid >= 0 && vkt_ram_first(layer, cache->slot[i].eid) &&
+ for (int i = 0; i < cache->n; i++)
+ if (cache->slot[i].eid >= 0 && vkt_ram_first(tl, cache->slot[i].eid) &&
(dev < 0 || cache->slot[i].used < cache->slot[dev].used)) dev = i;
if (dev >= 0) { vkt_ram_gave(); return dev; }
return oldest;
@@ -1640,7 +1645,7 @@ static Slot *expert_slot_at(Model *m, LCache *cache, const char *kind, int layer
if (cache->n < cache->cap) {
victim = &cache->slot[cache->n++];
} else {
- victim = &cache->slot[v41_victim(cache, kind, layer)];
+ victim = &cache->slot[v41_victim(m, cache, kind, layer)];
}
victim->used = ++m->clock;
expert_fetch(m, kind, layer, &victim, &eid, 1);
@@ -1679,7 +1684,7 @@ static void expert_slots_at(Model *m, LCache *cache, const char *kind, int layer
if (cache->n < cache->cap) {
victim = &cache->slot[cache->n++];
} else {
- victim = &cache->slot[v41_victim(cache, kind, layer)];
+ victim = &cache->slot[v41_victim(m, cache, kind, layer)];
}
victim->used = ++m->clock;
victim->eid = -1; /* not this expert yet: the read is still pending */
@@ -2623,8 +2628,9 @@ static void moe_gate(Model *m, Layer *l, int E, int topk, float *scores,
* device's rows and the CPU's alike (w x row, then the add), and the shared expert
* after them, as moe_run_at adds them. So the order of the sum never depends on
* which experts were resident. Every expert the CPU computed passes its slot to the
- * tier (vkt_note), which may promote it. The DSpark stages ("mtp") have expert
- * sets and caches of their own and stay on the CPU. */
+ * tier (vkt_note), which may promote it. The DSpark stages ("mtp"), with caches of
+ * their own, are the tier's extra layers n_layers + stage when it took them
+ * (COLI_VK_TIER_MTP). */
static char g_vk_usage[2100]; /* the tier's history (vk_tier_start); empty = none */
static void vk_tier_save(void) { if (g_vk_usage[0]) rt_save(g_vk_usage, 1); }
@@ -2634,7 +2640,7 @@ static VktExpertSrc vk_slot_src(const Slot *s) {
/* The draws want[d] of a block on the CPU into contrib[d] = w x expert(x): moe_run_at's
* expert-major union over those draws only. */
-static void moe_vk_cpu(Model *m, LCache *cache, int layer, int topk, const float *xc, int rows,
+static void moe_vk_cpu(Model *m, LCache *cache, const char *kind, int layer, int topk, const float *xc, int rows,
const int *chosen, const float *weights, const uint8_t *want, float *contrib) {
Cfg *c = &m->c;
int dim = c->dim, draws = rows * topk;
@@ -2659,7 +2665,7 @@ static void moe_vk_cpu(Model *m, LCache *cache, int layer, int topk, const float
for (int u0 = 0; u0 < n_uniq; u0 += step) {
int ne = n_uniq - u0 < step ? n_uniq - u0 : step;
Slot *slot[MOE_ROW_CHUNK];
- expert_slots_at(m, cache, "layers", layer, uniq + u0, ne, slot);
+ expert_slots_at(m, cache, kind, layer, uniq + u0, ne, slot);
for (int u = 0; u < ne; u++) {
int cnt = count[u0 + u], at = 0;
for (int d = head[u0 + u]; d >= 0; d = next[d], at++)
@@ -2675,26 +2681,28 @@ static void moe_vk_cpu(Model *m, LCache *cache, int layer, int topk, const float
}
m->hits += (uint64_t)(cnt - 1);
VktExpertSrc src = vk_slot_src(slot[u]);
- vkt_note(layer, uniq[u0 + u], &src);
+ vkt_note(v41_tier_layer(m, kind, layer), uniq[u0 + u], &src);
}
}
free(down); free(gathered); free(uniq);
}
-static void moe_vk_block(Model *m, Layer *l, LCache *cache, int layer, int topk, const float *xc,
+/* kind "layers" (the backbone) or "mtp" (a DSpark stage, the tier's extra layer: no
+ * history row and no EMAP mark, as on the CPU path) */
+static void moe_vk_block(Model *m, Layer *l, LCache *cache, const char *kind, int layer, int topk, const float *xc,
int rows, float *outc, const int *chosen, const float *weights, int with_shared) {
- int dim = m->c.dim, draws = rows * topk;
+ int dim = m->c.dim, draws = rows * topk, backbone = !strcmp(kind, "layers");
uint8_t *taken = xmalloc((size_t)draws * 2, "device draws"), *want = taken + draws;
const float **dev = xmalloc((size_t)draws * sizeof(*dev), "device rows");
- for (int r = 0; r < rows; r++) rt_count(layer, chosen + r * topk, topk);
- int ndev = vkt_issue(layer, xc, rows, topk, chosen, taken);
- for (int d = 0; d < draws; d++) { want[d] = !taken[d]; if (taken[d]) ehit_mark(m, layer, chosen[d]); }
+ for (int r = 0; backbone && r < rows; r++) rt_count(layer, chosen + r * topk, topk);
+ int ndev = vkt_issue(v41_tier_layer(m, kind, layer), xc, rows, topk, chosen, taken);
+ for (int d = 0; d < draws; d++) { want[d] = !taken[d]; if (taken[d] && backbone) ehit_mark(m, layer, chosen[d]); }
float *contrib = xmalloc((size_t)draws * dim * sizeof(float), "expert contributions");
float *shared = xmalloc((size_t)rows * dim * sizeof(float), "shared expert");
- moe_vk_cpu(m, cache, layer, topk, xc, rows, chosen, weights, want, contrib);
+ moe_vk_cpu(m, cache, kind, layer, topk, xc, rows, chosen, weights, want, contrib);
if (with_shared) shared_ffn_down(m, l, xc, rows, shared);
if (ndev && !vkt_join(dev)) { /* the batch failed (the tier stops): those draws here */
- moe_vk_cpu(m, cache, layer, topk, xc, rows, chosen, weights, taken, contrib);
+ moe_vk_cpu(m, cache, kind, layer, topk, xc, rows, chosen, weights, taken, contrib);
memset(taken, 0, (size_t)draws);
}
for (int r = 0; r < rows; r++) {
@@ -2772,7 +2780,7 @@ static void moe_run_at(Model *m, Layer *l, LCache *cache, const char *kind, int
for (int r = 0; r < n; r++)
moe_gate(m, l, E, topk, scores + (size_t)r * E, chosen + r * topk, weights + r * topk);
free(scores);
- moe_vk_block(m, l, cache, layer, topk, x, n, out, chosen, weights, with_shared);
+ moe_vk_block(m, l, cache, kind, layer, topk, x, n, out, chosen, weights, with_shared);
free(chosen); free(weights);
return;
}
@@ -2791,8 +2799,8 @@ static void moe_run_at(Model *m, Layer *l, LCache *cache, const char *kind, int
chosen + r * topk, weights + r * topk);
free(scores);
#ifdef COLI_VULKAN
- if (vkt_ready() && !strcmp(kind, "layers")) {
- moe_vk_block(m, l, cache, layer, topk, xc, rows, outc, chosen, weights, with_shared);
+ if (vkt_ready() && v41_tier_layer(m, kind, layer) < vkt_layers()) { /* the backbone, a stage the tier took */
+ moe_vk_block(m, l, cache, kind, layer, topk, xc, rows, outc, chosen, weights, with_shared);
continue;
}
#endif
@@ -4522,8 +4530,8 @@ static size_t vk_dense_bytes(Model *m) {
/* Is the expert in this layer's slots now (the tier's balance asks)? */
static int vk_in_ram(void *ctx, int layer, int e) {
Model *m = ctx;
- if (layer < 0 || layer >= m->c.n_layers) return 0;
- LCache *cache = &m->cache[layer];
+ if (layer < 0 || layer >= m->c.n_layers + (m->spec.active ? m->c.n_mtp : 0)) return 0;
+ LCache *cache = layer < m->c.n_layers ? &m->cache[layer] : &m->spec.cache[layer - m->c.n_layers]; /* a DSpark stage's */
for (int i = 0; i < cache->n; i++) if (cache->slot[i].eid == e) return 1;
return 0;
}
@@ -4533,15 +4541,23 @@ static void vk_tier_start(Model *m, const char *snap, int cap) {
VktFmt f = {VKT_SRC_MXFP4_E8M0, 32};
size_t expert = 0;
for (int part = 0; part < V41_EXPERT_TENSORS; part++) expert += (size_t)expert_part_bytes(c, part);
+ /* The DSpark stages (index n_layers + stage): their experts are the backbone's form,
+ * the tier's extra layers. By default on a discrete GPU only: on a GPU that shares
+ * the RAM, qwen38's head measured no faster there (docs/vulkan.md, "The MTP head's
+ * layer on the tier"); COLI_VK_TIER_MTP=1 or 0 decides. */
+ const char *tm = getenv("COLI_VK_TIER_MTP");
+ int want_mtp = tm && *tm ? *tm != '0' : !coli_vk_device_shares_ram();
+ int stages = want_mtp && m->spec.active && c->spec_routed <= c->n_routed && c->spec_activated <= MOE_TOPK_MAX ? c->n_mtp : 0;
VktConfig vc = {.engine = "deepseek_v41", .layers = c->n_layers, .experts = c->n_routed,
.hidden = c->dim, .inter = c->moe_inter, .topk = c->n_activated,
.gate_up = f, .down = f, .act = VKT_ACT_SWIGLU, .act_limit = c->swiglu_limit,
+ .extra_layers = stages, .extra_gate_up = f, .extra_down = f,
.max_rows = MOE_ROW_CHUNK * c->n_activated,
.ram_reserve = expert * (size_t)cap * (size_t)c->n_layers,
.dense_bytes = vk_dense_bytes(m), .in_ram = vk_in_ram, .ram_ctx = m,
.load = vk_load, .release = vk_unhold, .load_ctx = m, .load_batch = vk_load_batch};
rt_init("deepseek_v41", c->n_layers, c->n_routed);
- rt_drop_row(c->n_layers); /* no MTP row: the DSpark stages stay on the CPU */
+ rt_drop_row(c->n_layers); /* no MTP row: the history is the backbone's */
const char *up = getenv("COLI_USAGE");
if (up && *up) snprintf(g_vk_usage, sizeof g_vk_usage, "%s", up);
else snprintf(g_vk_usage, sizeof g_vk_usage, "%s/.coli_usage", snap);
diff --git a/c/tests/vulkan_engines.sh b/c/tests/vulkan_engines.sh
index 64b9a85f..0ce58688 100755
--- a/c/tests/vulkan_engines.sh
+++ b/c/tests/vulkan_engines.sh
@@ -488,10 +488,10 @@ family_qwen() {
! 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)"
+mtp_on_device() { # <tag> [log, vk.log]: the tier served experts of the MTP head's layers (its extra layers)
+ local log=${2:-vk.log} n; n=$(grep -a -o 'extra layers ([0-9]*): [0-9]*' "$log" | tail -1 | grep -o '[0-9]*$')
+ [ "${n:-0}" -gt 0 ] || { grep -a '\[VK\] tier' "$log"; fail "$1: no expert of the MTP head's layers ran on the device"; }
+ echo "OK $1: $(grep -a -o 'extra layers ([0-9]*): [0-9]* of [0-9]* routed experts on the device' "$log" | tail -1)"
}
# The routed-expert tier under ASan and UBSan: a sanitized VK=1 build of both qwen
@@ -536,7 +536,7 @@ family_qwen_sanitize() {
done
san 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 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"
+ mtp_on_device "asan qwen38 MTP" san.log
san qwen38 "asan qwen38 tier alone" COLI_VK_DENSE=0 SNAP=qwen38_tiny_int4 ./qwen38 4 8 qwen38_tiny_int4/ref_int4.json
make clean >/dev/null 2>&1 || true
}
@@ -929,9 +929,17 @@ family_deepseek() {
}
for cap in 1 2 8; do v41_tier "cap=$cap" SNAP=dsv41_tiny ./deepseek_v41 $cap dsv41_tiny/ref.json; done
for cap in 2 8; do v41_tier "40-token prompt cap=$cap" SNAP=dsv41_long ./deepseek_v41 $cap dsv41_long/ref.json; done
- # DSpark: the drafts stay on the CPU, the verify rows of the backbone take the device
+ # DSpark: the verify rows of the backbone take the device, and the drafts stay on the
+ # CPU (the default on Lavapipe, which shares the RAM); with COLI_VK_TIER_MTP=1 (the
+ # default on a discrete GPU) the stages are the tier's extra layers, their experts on
+ # the device too
for force in 1 2 3 4 5; do
v41_tier "DSpark spec=$force" SNAP=dsv41_tiny V41_DSPARK=1 V41_SPEC_FORCE=$force ./deepseek_v41 1 dsv41_tiny/ref.json
+ ! grep -aq 'extra layers' v41-vk.err || { grep -a '\[VK\] tier' v41-vk.err; fail "deepseek_v41 DSpark spec=$force: the stages went on the tier by default on shared RAM"; }
+ done
+ for force in 1 3 5; do
+ v41_tier "DSpark stages on the tier spec=$force" SNAP=dsv41_tiny V41_DSPARK=1 V41_SPEC_FORCE=$force COLI_VK_TIER_MTP=1 ./deepseek_v41 1 dsv41_tiny/ref.json
+ mtp_on_device "deepseek_v41 DSpark stages on the tier spec=$force" v41-vk.err
done
EVICT=1 v41_tier "a budget of three experts" COLI_VK_TIER_GB=0.00005 SNAP=dsv41_long ./deepseek_v41 8 dsv41_long/ref.json
v41_tier "trunk on the device too" COLI_VK_DENSE=1 SNAP=dsv41_long ./deepseek_v41 2 dsv41_long/ref.json
@@ -1080,6 +1088,8 @@ PY
local cap force p
for cap in 1 8; do san deepseek_v41 "asan deepseek_v41 cap=$cap" SNAP=dsv41_long ./deepseek_v41 $cap dsv41_long/ref.json; done
for force in 2 4; do san deepseek_v41 "asan deepseek_v41 DSpark spec=$force" SNAP=dsv41_tiny V41_DSPARK=1 V41_SPEC_FORCE=$force ./deepseek_v41 1 dsv41_tiny/ref.json; done
+ san deepseek_v41 "asan deepseek_v41 DSpark stages on the tier" SNAP=dsv41_tiny V41_DSPARK=1 V41_SPEC_FORCE=3 COLI_VK_TIER_MTP=1 ./deepseek_v41 1 dsv41_tiny/ref.json
+ mtp_on_device "asan deepseek_v41 DSpark stages on the tier" san.log
san deepseek_v41 "asan deepseek_v41 eviction" COLI_VK_TIER_GB=0.00005 SNAP=dsv41_long ./deepseek_v41 8 dsv41_long/ref.json
san deepseek_v41 "asan deepseek_v41 trunk on the device" COLI_VK_DENSE=1 SNAP=dsv41_tiny ./deepseek_v41 2 dsv41_tiny/ref.json
p=$($PY -c 'import json; c=json.load(open("deepseek_v4_tiny_t/ref.json"))["cases"]["long"]; print("".join("<t%03d>" % t for t in c["prompt_ids"]))')
@@ -1494,6 +1504,17 @@ family_glm() {
cmp -s <(grep -a '^GLM C engine' cpu.log) <(grep -a '^GLM C engine' vk.log) || { cat vk.log; fail "colibri COLI_VK_TIER=0: tokens differ"; }
! grep -qa '^\[VK\] tier colibri' vk.log || { cat vk.log; fail "colibri COLI_VK_TIER=0: the tier started"; }
echo "OK colibri COLI_VK_TIER=0: tokens = CPU, no tier"
+ # the MTP head's layer as the tier's extra layer (COLI_VK_TIER_MTP=1, the default on a
+ # discrete GPU): its experts f32 and quantized at load, two drafts; Lavapipe shares the
+ # RAM, where the default keeps them on the CPU
+ $PY tools/make_glm_mtp_tiny.py --src glm_tiny --out glm_tiny_mtp > /dev/null && cp ref_glm.json glm_tiny_mtp/
+ glm_tier "colibri MTP head on the tier, f32" glm_tiny_mtp glm_tiny_mtp/ref_glm.json DRAFT=2 COLI_VK_TIER_MTP=1 -- 64 16 16
+ mtp_on_device "colibri MTP head on the tier, f32"
+ glm_tier "colibri MTP head on the tier, 4-bit" glm_tiny_mtp glm_tiny_mtp/ref_glm.json DRAFT=2 IDOT=0 COLI_VK_TIER_MTP=1 -- 2 4 4
+ mtp_on_device "colibri MTP head on the tier, 4-bit"
+ glm_tier "colibri MTP head, shared RAM default" glm_tiny_mtp glm_tiny_mtp/ref_glm.json DRAFT=2 -- 64 16 16
+ ! grep -aq 'extra layers' vk.log || { grep -a '\[VK\] tier' vk.log; fail "colibri: the MTP head's layer went on the tier by default on shared RAM"; }
+ echo "OK colibri shared RAM: the MTP head's experts on the CPU by default"
# glm53: its int4-gs64 streaming container (swiglu_limit 10), dense matrices f32 and int4
local ids
@@ -1544,6 +1565,9 @@ family_glm_sanitize() {
HIST=1 gsan colibri "asan colibri tier + COLI_VK_DEV2" glm_tiny_i4r SNAP=glm_tiny_i4r REF=glm_tiny_i4r/ref_glm.json COLI_VK_EXPERTS=4 COLI_VK_DEV2=0 ./colibri 64 4 4
gsan colibri "asan colibri int4 at load, PIPE=1" glm_tiny SNAP=glm_tiny REF=ref_glm.json PIPE=1 ./colibri 1 4 4
gsan colibri "asan colibri int8 at load, PILOT=1" glm_tiny SNAP=glm_tiny REF=ref_glm.json PILOT=1 ./colibri 2 8 8
+ $PY tools/make_glm_mtp_tiny.py --src glm_tiny --out glm_tiny_mtp > /dev/null && cp ref_glm.json glm_tiny_mtp/
+ gsan colibri "asan colibri MTP head on the tier" glm_tiny_mtp SNAP=glm_tiny_mtp REF=glm_tiny_mtp/ref_glm.json DRAFT=2 IDOT=0 COLI_VK_TIER_MTP=1 ./colibri 2 4 4
+ mtp_on_device "asan colibri MTP head on the tier" san.log
local ids
ids=$($PY -c "print(','.join(str((i*37+5)%120+2) for i in range(100)))")
gsan glm53 "asan glm53 decode" - GLM53_BITS=32 ./glm53 --model glm53_stream-i4 --ids 5,7,9,11,13,17,19,23 --greedy 20
diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md
index 334a4ced..7fb3a5f0 100644
--- a/docs/ENVIRONMENT.md
+++ b/docs/ENVIRONMENT.md
@@ -244,7 +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_MTP` | on a discrete GPU, off on one that shares the RAM | `1` puts a drafting head's routed experts on the tier as extra layers, in the form the snapshot keeps them: qwen38's MTP head (FP8 beside the int4 sidecar), colibri's MTP head (int8 beside int4), deepseek_v41's DSpark stages; `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. |
@@ -526,7 +526,7 @@ checkpoint layout and the text-only capability boundary.
| `COLI_MAP_EXPERTS` | `0` | Point the native-FP8 routed-expert slots (or the int4-g64 sidecar's records) at a read-only mapping of their shard instead of copying 14 MB per miss into a slab. Same variable as in `glm53`. |
| `Q38_FP8_KERNEL` | vector | The routed experts' e4m3 blocks are decoded eight at a time in registers and multiplied with FMA (AVX2 builds); `scalar` restores `quant.h`'s table kernel, which differs only by float summation order inside a block. |
| `COLI_TIMERS` | `0` (off) | Set to `1` for the detailed Qwen3.8 phase breakdown on stderr. The shared per-request `PROF` frame is emitted regardless. |
-| `COLI_VULKAN` | `0` | `VK=1` build: the trunk on the Vulkan device, and the routed experts of the model's layers on the shared expert tier (`COLI_VK_TIER*`, `COLI_VK_DENSE`, see [Vulkan](#vulkan-any-gpu-with-a-vulkan-12-driver)); the MTP head's layer stays on the CPU. |
+| `COLI_VULKAN` | `0` | `VK=1` build: the trunk on the Vulkan device, and the routed experts of the model's layers on the shared expert tier (`COLI_VK_TIER*`, `COLI_VK_DENSE`, see [Vulkan](#vulkan-any-gpu-with-a-vulkan-12-driver)); the MTP head's layer too on a discrete GPU (`COLI_VK_TIER_MTP`). |
## DeepSeek V4 engine (`deepseek_v4`)
@@ -565,7 +565,7 @@ Read **only** by `c/deepseek_v41.c`. See [deepseek-v41.md](deepseek-v41.md).
| Variable | Default | Effect |
|---|---|---|
| `V41_ENGRAM_ROWS` | 65536 | DeepSeek V4.1: rows of engram cache per table. The n-gram traffic is Zipfian, so a small cache absorbs most of it; 65536 rows is 64 MB per table on the released head_dim. |
-| `COLI_VULKAN` | `0` | DeepSeek V4.1, `VK=1` build: the trunk (vision tower included) on the Vulkan device, and the backbone's routed experts on the shared expert tier (`COLI_VK_TIER*`, `COLI_VK_DENSE`, see [Vulkan](#vulkan-any-gpu-with-a-vulkan-12-driver)); the DSpark stages keep their experts on the CPU. With the tier on, the engine keeps the expert history `COLI_USAGE` (default `<snap>/.coli_usage`), saved at every run and serve turn end; it keeps none otherwise. |
+| `COLI_VULKAN` | `0` | DeepSeek V4.1, `VK=1` build: the trunk (vision tower included) on the Vulkan device, and the backbone's routed experts on the shared expert tier (`COLI_VK_TIER*`, `COLI_VK_DENSE`, see [Vulkan](#vulkan-any-gpu-with-a-vulkan-12-driver)); the DSpark stages' experts too on a discrete GPU (`COLI_VK_TIER_MTP`). With the tier on, the engine keeps the expert history `COLI_USAGE` (default `<snap>/.coli_usage`), saved at every run and serve turn end; it keeps none otherwise. |
| `V41_INDEX_OWNER` | unset | DeepSeek V4.1: score each layer against its OWN index keys instead of the last published cache. The default reproduces the released inference code; this changes the model's behaviour, see docs/deepseek-v41.md. |
| `V41_MAX_IMAGE_TOKENS` | the checkpoint's `max_image_tokens` | DeepSeek V4.1: ceiling on what one image costs in prompt tokens. |
| `V41_TRACE` | unset | DeepSeek V4.1: print per-sublayer checksums, matching tools/dsv41_ref.py's, to locate a divergence by diffing two columns. `2` follows the first row of a speculative step rather than the last. |
diff --git a/docs/qwen38.md b/docs/qwen38.md
index 2b63684b..6f92bc69 100644
--- a/docs/qwen38.md
+++ b/docs/qwen38.md
@@ -375,7 +375,8 @@ step. It takes every expert form this engine reads: the int4-g64 sidecar's plana
records (as int4 groups of 64 on the device), the release's FP8 with the 128x128
block-scale bank (fmt 12, the block scale repeated over its rows), BF16. Decode and
prefill both use it; an MTP verify's two rows take the device's per-row route, so
-they get a decode step's bits; the MTP head's own layer stays on the CPU. Every
+they get a decode step's bits; the MTP head's own layer goes on the tier as an extra
+layer on a discrete GPU (`COLI_VK_TIER_MTP`, [vulkan.md](vulkan.md#the-mtp-heads-layer-on-the-tier-coli_vk_tier_mtp)). Every
expert's output joins its row in rank order, device or not.
```bash
diff --git a/docs/vulkan.md b/docs/vulkan.md
index 7de01522..7c13895c 100644
--- a/docs/vulkan.md
+++ b/docs/vulkan.md
@@ -119,7 +119,7 @@ the shared expert tier ([below](#the-routed-expert-tier-vk_tierc)).
| 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 |
| mimo | trunk and vision tower; routed experts on the expert tier | native fp8/bf16, int8, f32 (`MIMO_DENSE_BITS`); experts MXFP4 with e8m0 scales (fmt 7) | router, the experts the tier does not hold |
-| deepseek_v41 | the trunk, vision included; routed experts on the expert tier | fp8 in 32x32 ue8m0 tiles, bf16; experts MXFP4 (fp4, a ue8m0 scale per 32) | the DSpark stages and their experts, the experts the tier does not hold |
+| deepseek_v41 | the trunk, vision included; routed experts on the expert tier | fp8 in 32x32 ue8m0 tiles, bf16; experts MXFP4 (fp4, a ue8m0 scale per 32) | the DSpark stages (their experts on the tier on a discrete GPU), the experts the tier does not hold |
| deepseek_v4 | resident dense layers, head, router, compressors; routed experts on the expert tier | fp8 in 128x128 blocks, bf16; experts MXFP4 (fp4, a ue8m0 scale per 32) with [an activation of its own](#deepseek-v4s-activation) | the indexer's `weights_proj`, DSpark stages, the `--oracle` path's dense layers, the experts the tier does not hold |
| glm53 (GLM-5.3 Flash) | the resident matrices (an f32 checkpoint's experts among them); the streaming container's routed experts on the expert tier | int8 and int4-g64 (`GLM53_BITS`); experts int4-gs64 | f32 matrices (`GLM53_BITS=32`), the streamed experts the tier does not hold, all of them when `swiglu_limit` is 0 |
| qwenimage | the DiT's matrices | int8, bf16, f32 (`COLI_IMG_BITS`) | text encoder, VAE, attention |
@@ -456,7 +456,7 @@ order its CPU-only run does, and keeps a history for the warm start where it has
| 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) |
| mimo (MiMo-V2.6 Flash and Pro) | the release's MXFP4 `MXFP4_E8M0` 32 (fmt 7) | SwiGLU | none: the tier fills as experts pass by | [Kimi K3 and MiMo](#kimi-k3-and-mimo) |
-| deepseek_v41 (DeepSeek V4.1 Flash) | MXFP4 `MXFP4_E8M0` 32 (fmt 7), as the checkpoint stores it | SwiGLU with `swiglu_limit` | `COLI_USAGE` (default `<snap>/.coli_usage`), kept only while the tier is on | the backbone's layers; the DSpark stages keep their own experts on the CPU |
+| deepseek_v41 (DeepSeek V4.1 Flash) | MXFP4 `MXFP4_E8M0` 32 (fmt 7), as the checkpoint stores it | SwiGLU with `swiglu_limit` | `COLI_USAGE` (default `<snap>/.coli_usage`), kept only while the tier is on | the backbone's layers; the DSpark stages, with caches of their own, are extra layers ([below](#the-mtp-heads-layer-on-the-tier-coli_vk_tier_mtp)) |
| deepseek_v4 (DeepSeek V4 Flash) | MXFP4 `MXFP4_E8M0` 32 (fmt 7); pinned experts unpacked from rows16 | `VKT_ACT_SWIGLU_V4` | the store's own `<model>/.coli_usage` | [DeepSeek V4's activation](#deepseek-v4s-activation) |
| colibri (GLM-5.2) | `F32` (fmt 10), int8 `I8_ROW` (fmt 1), int4 per row `I4U_PAIRS_ROW` (fmt 2), int4-gs `I4U_PAIRS_GS` (fmt 4), int3-g64 `I3_G64` (fmt 5); down may have its own | SwiGLU | `<snap>/.coli_usage` | [GLM-5.2 and GLM-5.3 Flash](#glm-52-and-glm-53-flash-on-the-tier) |
| glm53 (GLM-5.3 Flash) | the streaming container's int4-gs64 `I4U_PAIRS_GS` 64 (fmt 4) | SwiGLU with `swiglu_limit`, run only above 0 | `COLI_USAGE` (default `<snap>/.coli_usage`) | [GLM-5.2 and GLM-5.3 Flash](#glm-52-and-glm-53-flash-on-the-tier) |
@@ -608,12 +608,29 @@ 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.
+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'.
+
+**The other engines with a drafting head.**
+
+| Engine | Extra layers | Their experts |
+|---|---|---|
+| colibri (GLM-5.2) | the MTP head's layer, index `n_layers` | as the container keeps them (int8 beside int4 in a converted GLM-5.2), checked like the model's |
+| deepseek_v41 | the DSpark stages, `n_layers + stage` | the backbone's MXFP4; each stage's cache is the RAM side the balance and the exclusive RAM cache read |
+| deepseek_v4 | none | its full DSpark drafter (three stages, the DSpark supplement) keeps its experts on the CPU: the tiny fixture has one MTP layer, so no test reaches that path |
+| glm53 | none | its MTP layer has no routed experts |
+
+The same `COLI_VK_TIER_MTP` decides, with the same default.
+
+**Tests.** `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). The `qwen` family runs the head's
+layer on the device on both MTP fixtures and its default on Lavapipe; `glm` colibri's
+head with f32 and 4-bit experts at two drafts, and its default; `deepseek` V4.1's two
+DSpark stages at three forced acceptances, the default at five. Each must give the CPU's
+tokens and serve experts of the extra layers from the device; the sanitizer families run
+one of each.
### A second device (`COLI_VK_DEV2`)
@@ -750,7 +767,8 @@ int3-g64 when a bf16 or FP8 checkpoint is quantized at load, the int4-gs contain
down may have its own (`--down-bits 3`; an int4-g64 container's rows narrower than the
group stay per row). E8/IQ3 experts (fmt 6, whose input is rotated), int2 and fp8 have
no device form and stay on the CPU (`[VK] tier colibri: experts in fmt 6/6/6 ... stay
-on the CPU`), and so does the MTP head's layer (int8). The tier serves every row count:
+on the CPU`). The MTP head's layer (its experts int8 in a converted container) is an extra
+layer ([above](#the-mtp-heads-layer-on-the-tier-coli_vk_tier_mtp)). The tier serves every row count:
decode, the MTP and n-gram verify rows and the batched prefill, by blocks of 64 rows; the
fixed set it replaces served S <= 4 only.
@@ -1409,8 +1427,9 @@ forward of the session and when another session takes the device, and goes up af
pin or a state is restored.
**Drafts and the MTP head.** colibri's speculative decode runs as before: the verify
-rows go through the chain, the MTP head stays on the CPU and reads the chain's final
-rows, and n-gram drafts work the same way. glm53 has no draft path.
+rows go through the chain, the MTP head runs on the CPU (its routed experts on the
+expert tier on a discrete GPU) and reads the chain's final rows, and n-gram drafts work
+the same way. glm53 has no draft path.
**A lost device.** The forward that failed runs again on the CPU from its input (the
chain keeps the caller's rows untouched until its last chunk is through), and the CPU