Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
vk: GLM-5.2 layers on two devices; inkling reports a lost second device at setup
JustVugg23c51498351d
diff --git a/c/colibri.c b/c/colibri.cindex 3444ce56..72a4d1ec 100644--- a/c/colibri.c+++ b/c/colibri.c@@ -509,6 +509,7 @@ typedef struct {#ifdef COLI_VULKANint *vk_kv_valid; /* righe [0,v) specchiate nella cache KV Vulkan */void *vkchain; /* the dense chain's device state (glm_chain.h), NULL until it starts */+ void *vkchain2; /* its layers on COLI_VK_DEV2's device, after the primary's (glm_chain.h) */#endifESlot ws[64]; /* working set del layer corrente (load paralleli) */ESlot **pin; int *npin; /* HOT-STORE: expert pinnati in RAM (mai evicted) */@@ -783,16 +784,18 @@ static pthread_t g_dho_thread;static int qt_vk_fmt(const QT *t){ return t->fmt==0 ? 10 : t->fmt; }static int qt_dho_matmul(QT *t, float *y, const float *x, int S){if(omp_in_parallel() || !pthread_equal(pthread_self(), g_dho_thread) || !t->vk) return 0;+ if(coli_vk_tensor_dev(t->vk)) return 0; /* a matrix of the second device's layers: only its chain reads it */return coli_vk_matmul(&t->vk, y, x, NULL, NULL, qt_vk_fmt(t), S, t->I, t->O, t->fmt==4 ? t->gs : 0);}static int vk_matmul_qt(QT *t, float *y, const float *x, int S){- if(!g_vk_dense || !VK_FMT_OK(t) || !VK_MAY(t)) return 0;+ if(!g_vk_dense || !VK_FMT_OK(t) || !VK_MAY(t) || (t->vk && coli_vk_tensor_dev(t->vk))) return 0;const void *w = t->fmt==1 ? (const void*)t->q8 : (const void*)t->q4;return coli_vk_matmul(&t->vk, y, x, w, t->s, t->fmt, S, t->I, t->O, t->gs);}/* Two same-input resident matmuls in one submit (q_a + kv_a read the same x). */static int vk_matmul_pair_qt(QT *a, float *ya, QT *b, float *yb, const float *x, int S){if(!g_vk_dense || a->fmt!=b->fmt || !VK_FMT_OK(a) || a->gs!=b->gs || a->I!=b->I || !VK_MAY(a) || !VK_MAY(b)) return 0;+ if((a->vk && coli_vk_tensor_dev(a->vk)) || (b->vk && coli_vk_tensor_dev(b->vk))) return 0;const void *wa = a->fmt==1 ? (const void*)a->q8 : (const void*)a->q4;const void *wb = b->fmt==1 ? (const void*)b->q8 : (const void*)b->q4;return coli_vk_matmul_pair(&a->vk, ya, wa, a->s, a->O,diff --git a/c/glm_chain.h b/c/glm_chain.hindex 8fd59f12..bbb8e1af 100644--- a/c/glm_chain.h+++ b/c/glm_chain.h@@ -72,7 +72,8 @@typedef struct {int ok, failed;- int n; /* layers on the device: 0..n-1 (the rest run on the CPU) */+ int lo, d; /* its layers start at lo, on device d (vkc_device) */+ int n; /* layers on the device: lo..lo+n-1 (the rest run elsewhere) */int rows; /* scratch capacity in rows */int cap; /* host positions the mirror covers */int dev_rows; /* device latent/rope rows a layer: cap, or the split's */@@ -100,6 +101,17 @@ static int g_glmc_fitted;static size_t g_glmc_lazy; /* what the chain allocates at its first forward: one chunk's* scratch and the N layers' KV mirrors at their first size */static size_t *g_glmc_mir; /* each layer's KV mirror at its first size */+/* The layers after the primary's on COLI_VK_DEV2's device (docs/vulkan.md, "Layers on two+ * devices"): a second chain from layer g_glmc_fit.n, its own fit (glmc_fit_plan), set up+ * after the primary's. A forward runs every row through the primary's layers, brings them+ * back, and runs them through these (with the DSA selection the primary's last full layer+ * made, when the first layer here reuses it); the CPU runs what is left. */+static VkcFit g_glmc_fit2;+static int g_glmc_fit2_on;+static int g_glmc_dev; /* the device glmc_tensor uploads a new matrix to */+static GlmChain *glmc_of(Model *m, int d) { return (GlmChain *)(d ? m->vkchain2 : m->vkchain); }+static const char *glmc_name(const GlmChain *ch) { return ch && ch->d ? "colibri dev2" : "colibri"; }+static int glmc_dev2_wanted(void) { const char *e = getenv("COLI_VK_CHAIN_DEV2"); return !(e && *e == '0'); }/* The device's copy of a resident QT, uploaded once (the per-matrix path's own copy* when COLI_VK_DENSE put it there); f32 goes up as the backend's fmt 10. NULL: no@@ -116,7 +128,10 @@ static ColiVkTensor *glmc_tensor(QT *t) {default: return NULL;}if (!w || t->O <= 0 || t->I <= 0) return NULL;- return coli_vk_tensor_ensure(&t->vk, w, t->fmt == 0 ? NULL : t->s, fmt, t->I, t->O, t->fmt == 4 ? t->gs : 0) ? t->vk : NULL;+ const float *sc = t->fmt == 0 ? NULL : t->s;+ int gs = t->fmt == 4 ? t->gs : 0;+ return (g_glmc_dev ? coli_vk_tensor_ensure2(&t->vk, w, sc, fmt, t->I, t->O, gs)+ : coli_vk_tensor_ensure(&t->vk, w, sc, fmt, t->I, t->O, gs)) ? t->vk : NULL;}/* glmc_tensor's backend format for t without uploading it (-1: no device form), and its* group size; the fit counts the bytes from it. */@@ -145,32 +160,39 @@ static int glmc_layer_qts(Model *m, int i, QT **q) {/* ---- the host's writes to its KV cache: the mirror's watermark follows ------------ *//* The CPU's attention writes rows [pos..] of each row's KV state for this layer. */+/* (each covers both chains; a chain's split indexes its own layers from 0) */static void glmc_cpu_rows(Model *m, int layer, KVState *const *kvs, const int *positions, int pos_base, int S) {- GlmChain *ch = (GlmChain *)m->vkchain;- if (!ch || !ch->kv_valid || layer < 0 || layer >= ch->n) return; /* a CPU layer of a partial chain: no mirror */- for (int s = 0; s < S; s++) {- KVState *ks = kvs ? kvs[s] : m->kv;- int pos = positions ? positions[s] : pos_base + s;- if (ks == ch->owner && ch->kv_valid[layer] > pos) ch->kv_valid[layer] = pos;- if (ks == ch->owner) vkc_kv_lower(&ch->ks, layer, pos);+ for (int d = 0; d < 2; d++) {+ GlmChain *ch = glmc_of(m, d);+ if (!ch || !ch->kv_valid || layer < ch->lo || layer >= ch->lo + ch->n) continue; /* a CPU layer: no mirror */+ for (int s = 0; s < S; s++) {+ KVState *ks = kvs ? kvs[s] : m->kv;+ int pos = positions ? positions[s] : pos_base + s;+ if (ks == ch->owner && ch->kv_valid[layer] > pos) ch->kv_valid[layer] = pos;+ if (ks == ch->owner) vkc_kv_lower(&ch->ks, layer - ch->lo, pos);+ }}}/* Rows from `from` of every layer of k were written on the host. */static void glmc_host_rows(Model *m, KVState *k, int from) {- GlmChain *ch = (GlmChain *)m->vkchain;- if (!ch || !ch->kv_valid || k != ch->owner) return;- for (int i = 0; i < ch->n; i++) {- if (ch->kv_valid[i] > from) ch->kv_valid[i] = from < 0 ? 0 : from;- vkc_kv_lower(&ch->ks, i, from < 0 ? 0 : from);+ for (int d = 0; d < 2; d++) {+ GlmChain *ch = glmc_of(m, d);+ if (!ch || !ch->kv_valid || k != ch->owner) continue;+ for (int i = ch->lo; i < ch->lo + ch->n; i++) {+ if (ch->kv_valid[i] > from) ch->kv_valid[i] = from < 0 ? 0 : from;+ vkc_kv_lower(&ch->ks, i - ch->lo, from < 0 ? 0 : from);+ }}}/* kv_alloc gave the bound state new arrays: nothing on the device is theirs. */static void glmc_kv_reset(Model *m) {- GlmChain *ch = (GlmChain *)m->vkchain;- if (!ch || !ch->kv_valid) return;- for (int i = 0; i < ch->n; i++) ch->kv_valid[i] = 0;- vkc_kv_reset(&ch->ks);- ch->owner = NULL;+ for (int d = 0; d < 2; d++) {+ GlmChain *ch = glmc_of(m, d);+ if (!ch || !ch->kv_valid) continue;+ for (int i = ch->lo; i < ch->lo + ch->n; i++) ch->kv_valid[i] = 0;+ vkc_kv_reset(&ch->ks);+ ch->owner = NULL;+ }}/* ---- setup ---------------------------------------------------------------------------- */@@ -262,6 +284,20 @@ static void glmc_fit_plan(Model *m) {g_glmc_fitted = 1;g_glmc_lazy = scratch;g_glmc_mir = mir;+ /* the layers the primary leaves, on COLI_VK_DEV2's device: a fit of its own from layer+ * n0, with that device's free memory, its pipelines up now */+ int n0 = g_glmc_fit.n;+ if (vkc_fit_partial(&g_glmc_fit) && n0 > 0 && n0 < L && glmc_dev2_wanted() && getenv("COLI_VK_DEV2") &&+ coli_vk_dev2_open_env()) {+ vkc_device(1);+ int n2 = vkc_fit("colibri dev2", L - n0, lb + n0, mb + n0, fixed, 0, &g_glmc_fit2);+ if (n2 > 0 && !(vkc_init() && vkc_mla_ready())) {+ fprintf(stderr, "[VK] colibri chain: the second device's pipelines did not come up; its layers stay on the CPU\n");+ n2 = 0;+ }+ g_glmc_fit2_on = n2 > 0;+ vkc_device(0);+ }free(lb); free(mb);}@@ -271,28 +307,32 @@ static void glmc_fit_plan(Model *m) {* promised, a failed upload) is freed whole and the chain keeps the layers before it* (vkc_fit_shrink); nothing of it stays on the device and its host copies were never* dropped. 0 = no layer on the device: the chain stays off. */-static int glmc_setup(Model *m) {- Cfg *c = &m->c; int L = c->n_layers, D = c->hidden, N = g_glmc_fit.n;+/* d = 1: the second device's chain, its layers from g_glmc_fit.n (device 1 current). */+static int glmc_setup_dev(Model *m, int d) {+ Cfg *c = &m->c; int L = c->n_layers, D = c->hidden, N = d ? g_glmc_fit2.n : g_glmc_fit.n, lo = d ? g_glmc_fit.n : 0;+ VkcFit *fit = d ? &g_glmc_fit2 : &g_glmc_fit;+ const char *nm = d ? "colibri dev2" : "colibri";GlmChain *ch = (GlmChain *)calloc(1, sizeof *ch);if (!ch) return 0;+ ch->d = d; ch->lo = lo;ch->o_in = calloc(L, sizeof(size_t)); ch->o_post = calloc(L, sizeof(size_t));ch->o_ixw = calloc(L, sizeof(size_t)); ch->o_ixb = calloc(L, sizeof(size_t));ch->mla = calloc(L, sizeof(VkcMla)); ch->kv = calloc(L, sizeof(VkcMlaCache));ch->ik = calloc(L, sizeof(VkcBuf *)); ch->kv_valid = calloc(L, sizeof(int));if (!ch->o_in || !ch->o_post || !ch->o_ixw || !ch->o_ixb || !ch->mla || !ch->kv || !ch->ik || !ch->kv_valid) return 0;- m->vkchain = ch;+ if (d) m->vkchain2 = ch; else m->vkchain = ch;/* the parameter arena of the N layers: in_ln, post_ln, the q and kv latent norms, the* index key norm */size_t n = 0, *o_qn = calloc(L, sizeof(size_t)), *o_kn = calloc(L, sizeof(size_t));if (!o_qn || !o_kn) { free(o_qn); free(o_kn); return 0; }- for (int i = 0; i < N; i++) {+ for (int i = lo; i < lo + N; i++) {ch->o_in[i] = n; n += D; ch->o_post[i] = n; n += D;o_qn[i] = n; n += c->q_lora; o_kn[i] = n; n += c->kv_lora;if (glmc_full(m, i)) { ch->o_ixw[i] = n; n += c->index_hd; ch->o_ixb[i] = n; n += c->index_hd; }}float *arena = calloc(n ? n : 1, sizeof(float));if (!arena) { free(o_qn); free(o_kn); return 0; }- for (int i = 0; i < N; i++) {+ for (int i = lo; i < lo + N; i++) {Layer *l = &m->L[i];memcpy(arena + ch->o_in[i], l->in_ln, D * sizeof(float));memcpy(arena + ch->o_post[i], l->post_ln, D * sizeof(float));@@ -309,11 +349,13 @@ static int glmc_setup(Model *m) {if (!ok) {vkc_free(ch->prm); ch->prm = NULL;free(o_qn); free(o_kn);- vkc_fit_shrink("colibri", &g_glmc_fit, 0, "the parameters' buffer was refused");+ vkc_fit_shrink(nm, fit, 0, vkc_lost() ? "the device was lost" : "the parameters' buffer was refused");return 0;}- /* the tensors: the same device copies the per-matrix path uses */- for (int i = 0; i < N; i++) {+ /* the tensors: the same device copies the per-matrix path uses (the second device's:+ * its own, there) */+ g_glmc_dev = d;+ for (int i = lo; i < lo + N; i++) {Layer *l = &m->L[i];QT *q[11]; int nq = glmc_layer_qts(m, i, q), up = 1;for (int k = 0; k < nq && up; k++) up = glmc_tensor(q[k]) != NULL;@@ -321,33 +363,37 @@ static int glmc_setup(Model *m) {ColiVkTensor **t[11];for (int k = 0; k < nq; k++) t[k] = &q[k]->vk;vkc_layer_free(t, nq, NULL, 0);- vkc_fit_shrink("colibri", &g_glmc_fit, i, vkc_lost() ? "the device was lost" : "a matrix was refused");+ vkc_fit_shrink(nm, fit, i - lo, vkc_lost() ? "the device was lost" : "a matrix was refused");break;}ch->mla[i] = (VkcMla){c->n_heads, c->qk_nope, c->qk_rope, c->v_head, c->kv_lora, D, c->q_lora, c->eps, c->attn_scale,VKC_ROPE_INTERLEAVED, l->q_a.vk, l->q_b.vk, l->kv_a.vk, l->kv_b.vk, NULL, NULL, l->o.vk,ch->prm, o_qn[i], o_kn[i]};- glm_dho_drop_layer(m, i); /* COLI_VK_DENSE_HOST: all of the layer is there, its host copies go */- vkc_fit_mark(&g_glmc_fit, i);+ if (!d) glm_dho_drop_layer(m, i); /* COLI_VK_DENSE_HOST: all of the layer is there, its host copies go */+ vkc_fit_mark(fit, i - lo);}+ g_glmc_dev = 0;free(o_qn); free(o_kn);- ch->n = g_glmc_fit.n;+ ch->n = fit->n;if (!ch->n) { vkc_free(ch->prm); ch->prm = NULL; return 0; }ch->ok = 1;int nfull = 0, ndense = 0;- for (int i = 0; i < ch->n; i++) { nfull += glmc_full(m, i); ndense += !m->L[i].sparse; }- fprintf(stderr, "[VK] colibri chain: %d layers on the device (%d dense, %d with the DSA indexer), %.1f MiB of parameters\n",- ch->n, ndense, nfull, n * 4 / 1048576.0);+ for (int i = lo; i < lo + ch->n; i++) { nfull += glmc_full(m, i); ndense += !m->L[i].sparse; }+ if (d) fprintf(stderr, "[VK] colibri chain: layers %d..%d on the second device (%d dense, %d with the DSA indexer), "+ "%.1f MiB of parameters\n", lo, lo + ch->n - 1, ndense, nfull, n * 4 / 1048576.0);+ else fprintf(stderr, "[VK] colibri chain: %d layers on the device (%d dense, %d with the DSA indexer), %.1f MiB of parameters\n",+ ch->n, ndense, nfull, n * 4 / 1048576.0);return 1;}+static int glmc_setup(Model *m) { return glmc_setup_dev(m, 0); }/* Prompt rows per chunk (vkc_chunk_rows): the chain's scratch a row, counted from the* reservations (the MLA scratch as vkc_mla_scratch sizes it; the DSA scores at the* model's context), and the routed experts' outputs (the tier's rows, the CPU's* contributions, and the host's sum). */static int glmc_chunk_rows(GlmChain *ch, Model *m) {- if (!vkc_chunk_auto()) return vkc_chunk_rows("colibri", 0);- const VkcMla *a = &ch->mla[0];+ if (!vkc_chunk_auto()) return vkc_chunk_rows(glmc_name(ch), 0);+ const VkcMla *a = &ch->mla[ch->lo];size_t mla = (size_t)(a->q_lora > 0 ? a->q_lora : 1) + (size_t)a->H * (a->Q + a->R) + (size_t)(a->K + a->R) +(size_t)a->H * a->K + (size_t)a->H * a->K + (size_t)a->H * a->V;size_t ksz = (size_t)ch->kvd_layer;@@ -359,7 +405,7 @@ static int glmc_chunk_rows(GlmChain *ch, Model *m) {* second device's packed inputs and outputs. */row += 2 * ((size_t)m->c.hidden + m->c.moe_inter) * sizeof(float);if (g_vk_reg_n2 > 0) row += 2 * (size_t)m->c.topk * m->c.hidden * sizeof(float);- return vkc_chunk_rows("colibri", row);+ return vkc_chunk_rows(glmc_name(ch), row);}/* scratch for `rows` rows at contexts up to `ctx` positions */@@ -368,7 +414,7 @@ static int glmc_scratch(GlmChain *ch, Model *m, int rows, int ctx) {int SI = c->moe_inter * c->n_shared, DI = c->dense_inter, MI = SI > DI ? SI : DI;size_t r = (size_t)rows;ch->kvd_layer = r * (c->kv_lora + c->qk_rope + (m->has_dsa ? ID : 0));- int ok = (g_glmc_count >= 0 || vkc_mla_scratch(&ch->sc, &ch->mla[0], rows)) &&+ int ok = (g_glmc_count >= 0 || vkc_mla_scratch(&ch->sc, &ch->mla[ch->lo], rows)) &&glmc_res(&ch->x, r * D, VKC_DEV) && glmc_res(&ch->nrm, r * D, VKC_DEV) && glmc_res(&ch->tmp, r * D, VKC_DEV) &&glmc_res(&ch->h2, r * D, VKC_DEV) && glmc_res(&ch->gs, r * MI, VKC_DEV) && glmc_res(&ch->us, r * MI, VKC_DEV) &&glmc_res(&ch->hs, r * MI, VKC_DEV) && glmc_res(&ch->ds, r * D, VKC_DEV) &&@@ -390,7 +436,7 @@ static int glmc_scratch(GlmChain *ch, Model *m, int rows, int ctx) {glmc_res(&ch->sel, r * (1 + (size_t)c->index_topk), VKC_DEV);/* a partial chain whose CPU layer n reuses the selection of a device layer: it comes* back with the rows (the fit counts it whenever there is an indexer) */- if (ok && m->has_dsa && (g_glmc_count_fit || (ch->n < L && !glmc_full(m, ch->n))))+ if (ok && m->has_dsa && (g_glmc_count_fit || (ch->lo + ch->n < L && !glmc_full(m, ch->lo + ch->n))))ok = glmc_res(&ch->seld, r * (1 + (size_t)c->index_topk), VKC_DOWN);if (!ok || g_glmc_count >= 0) return ok; /* counting: the buffers only */if (ch->rows < rows) {@@ -406,21 +452,21 @@ static int glmc_scratch(GlmChain *ch, Model *m, int rows, int ctx) {* planned again (vkc_kv_plan): whole when it fits the device's budget, else the split* (latent and rope rows; the index keys stay whole), for steps of `rows` rows. */static int glmc_mirror(GlmChain *ch, Model *m, int need, int rows) {- Cfg *c = &m->c; int L = ch->n; /* the device's layers (a partial chain: the first n) */- if (ch->owner != m->kv) { for (int i = 0; i < L; i++) ch->kv_valid[i] = 0; ch->owner = m->kv; vkc_kv_reset(&ch->ks); }+ Cfg *c = &m->c; int L = ch->n, lo = ch->lo; /* the device's layers: lo..lo+n-1 */+ if (ch->owner != m->kv) { for (int i = lo; i < lo + L; i++) ch->kv_valid[i] = 0; ch->owner = m->kv; vkc_kv_reset(&ch->ks); }if (ch->cap >= need) return 1;int cap = 256; while (cap < need) cap *= 2;if (cap > m->max_t) cap = m->max_t;if (cap < need) return 0;size_t row = (size_t)(c->kv_lora + c->qk_rope) * sizeof(float);- if (!vkc_kv_plan(&ch->ks, "colibri", L, row, cap, rows, m->has_dsa, (size_t)L * ch->dev_rows * row)) return 0;+ if (!vkc_kv_plan(&ch->ks, glmc_name(ch), L, row, cap, rows, m->has_dsa, (size_t)L * ch->dev_rows * row)) return 0;int dr = ch->ks.on ? ch->ks.rows : cap;- for (int i = 0; i < L; i++) {+ for (int i = lo; i < lo + L; i++) {vkc_free(ch->kv[i].lat); vkc_free(ch->kv[i].rope); vkc_free(ch->ik[i]);ch->kv[i] = (VkcMlaCache){NULL, NULL, 0}; ch->ik[i] = NULL; ch->kv_valid[i] = 0;}ch->cap = 0; ch->dev_rows = 0;- for (int i = 0; i < L; i++) {+ for (int i = lo; i < lo + L; i++) {ch->kv[i].lat = vkc_buf((size_t)dr * c->kv_lora * sizeof(float), VKC_DEV);ch->kv[i].rope = c->qk_rope > 0 ? vkc_buf((size_t)dr * c->qk_rope * sizeof(float), VKC_DEV) : NULL;ch->kv[i].cap = dr;@@ -435,11 +481,11 @@ static int glmc_mirror(GlmChain *ch, Model *m, int need, int rows) {* n_rows rows (the split: its window placed first). */static int glmc_push_kv(GlmChain *ch, Model *m, int pos_base, int n_rows) {Cfg *c = &m->c; int ok = 1, K = c->kv_lora, R = c->qk_rope, ID = c->index_hd;- for (int i = 0; i < ch->n && ok; i++) {+ for (int i = ch->lo; i < ch->lo + ch->n && ok; i++) {if (ch->ks.on) { /* the split: the window placed, its rows below pos_base uploaded */VkcKvPart pt[2] = {{1, K, m->Lc[i], 0, ch->kv[i].lat, 0}, {1, R, m->Rc[i], 0, ch->kv[i].rope, 0}};- vkc_kv_place(&ch->ks, i, pos_base, n_rows);- ok = vkc_kv_push(&ch->ks, i, pt, R ? 2 : 1, pos_base);+ vkc_kv_place(&ch->ks, i - ch->lo, pos_base, n_rows);+ ok = vkc_kv_push(&ch->ks, i - ch->lo, pt, R ? 2 : 1, pos_base);int t0 = ch->kv_valid[i], n = pos_base - t0; /* the index keys stay whole */if (ok && n > 0 && glmc_full(m, i))ok = vkc_write(ch->ik[i], (size_t)t0 * ID, coli_kv_row(m->Ic[i], t0, ID), (size_t)n * ID * sizeof(float));@@ -459,7 +505,7 @@ static int glmc_push_kv(GlmChain *ch, Model *m, int pos_base, int n_rows) {static void glmc_pull_kv(GlmChain *ch, Model *m, int from, int to, int pb, int n) {Cfg *c = &m->c; int K = c->kv_lora, R = c->qk_rope, ID = c->index_hd;for (int i = from; i < to; i++) {- const float *kv = (const float *)vkc_ptr(ch->kvd) + (size_t)i * ch->kvd_layer;+ const float *kv = (const float *)vkc_ptr(ch->kvd) + (size_t)(i - ch->lo) * ch->kvd_layer;for (int s = 0; s < n; s++) {memcpy(coli_kv_row(m->Lc[i], pb + s, K), kv + (size_t)s * K, K * sizeof(float));if (R) memcpy(coli_kv_row(m->Rc[i], pb + s, R), kv + (size_t)n * K + (size_t)s * R, R * sizeof(float));@@ -479,7 +525,7 @@ static int glmc_norm(VkcBuf *x, VkcBuf *w, size_t wo, VkcBuf *y, int rows, int D* selection. *active: the selection list holds this layer's choice. */static int glmc_dsa(GlmChain *ch, Model *m, int i, int n, int pb, int *active) {Cfg *c = &m->c; int IH = c->index_nh, ID = c->index_hd, R = c->qk_rope;- size_t kof = (size_t)i * ch->kvd_layer + (size_t)n * (c->kv_lora + R);+ size_t kof = (size_t)(i - ch->lo) * ch->kvd_layer + (size_t)n * (c->kv_lora + R);VkcMlaRow ln = {n, 1, 0, ID, 0, 0, ID, 0, pb * ID, ID, 0, 0, 0, (int)ch->o_ixw[i], (int)ch->o_ixb[i], 1, 1e-6f};VkcMlaRow rk = {n, 1, R, ID, VKC_ROPE_INTERLEAVED, pb * ID, ID, 0, pb * ID, ID, 0, 0, R, 0, 0, 0, 0.f};int ok = vkc_matmul(glmc_tensor(&m->ix_wk[i]), ch->nrm, 0, ch->ikd, 0, n) &&@@ -510,37 +556,25 @@ static int glmc_combine(GlmChain *ch, Model *m, int n) {return vkc_ew(ch->x, ch->x, ch->routed, ch->ds, NULL, &p);}-/* The device's layers (every layer, or the first n of a partial chain) for S rows of xh- * (positions pos_base..), the rows after the last of them back into xh. Returns how many- * layers ran: L, or n (the caller's CPU loop runs layers n..L-1 from xh); 0 = not taken:- * the CPU runs the forward (from xh, untouched). */-static int glmc_forward(Model *m, float *xh, int S, int pos_base) {- if (!g_vk_chain) return 0;- if (g_vk_chain == COLI_VK_CHAIN_PREFILL && S <= 2) return 0; /* prompts only: decode on the CPU */- if (g_pilot || g_pilot_real || g_looka || g_kv8 || g_tq || (exact_verify_on() && g_spec_live)) return 0;- GlmChain *ch = (GlmChain *)m->vkchain;- if (!ch || !ch->ok || ch->failed || vkc_lost()) return 0;- Cfg *c = &m->c; int D = c->hidden, L = c->n_layers, R = c->qk_rope, N = ch->n;- if (!m->Lc || !m->Rc || (m->has_dsa && !m->Ic)) return 0;- for (int i = 0; i < L; i++) if (m->kv_start[i] != 0 || !m->Lc[i]) return 0;- /* layer N on the CPU reuses the DSA selection of the device's last full layer (a- * shared indexer layer): the selection comes back with the rows, into the CPU's */- int hand_sel = N < L && m->has_dsa && !glmc_full(m, N), TK = c->index_topk;+/* One chain's layers (lo..lo+n-1, on its device) for S rows of xh in chunks of `rows`,+ * the rows after the last of them back into xh. sel_in (the second device's chain): the+ * raw DSA selection rows the primary's last full layer made, and each chunk's on flag,+ * read by the first layers here when they reuse it; sel_out (the primary's, with a second+ * chain): where to keep them. 0 = not taken (*lost = 1: a frame failed, the chains off). */+static int glmc_forward_seg(Model *m, GlmChain *ch, float *xh, int S, int pos_base, int rows,+ const int *sel_in, const unsigned char *on_in, int *sel_out, unsigned char *on_out, int *lost) {+ *lost = 0;+ Cfg *c = &m->c; int D = c->hidden, L = c->n_layers, R = c->qk_rope, N = ch->n, lo = ch->lo, hi = lo + N;+ /* the layer after these on the CPU reuses the DSA selection of their last full layer+ * (a shared indexer layer): the selection comes back with the rows, into the CPU's */+ int hand_sel = hi < L && m->has_dsa && !glmc_full(m, hi), TK = c->index_topk;if (hand_sel && (int64_t)S * TK > m->dsa_scap) {int *sl = malloc((size_t)S * TK * sizeof(int)), *ns = malloc((size_t)S * sizeof(int));if (!sl || !ns) { free(sl); free(ns); return 0; }free(m->dsa_sel); free(m->dsa_nsel);m->dsa_sel = sl; m->dsa_nsel = ns; m->dsa_scap = (int)((int64_t)S * TK);}- int mirror_ok = glmc_mirror(ch, m, pos_base + S, 1);- int CH = mirror_ok ? glmc_chunk_rows(ch, m) : 1;- if (m->has_dsa && (pos_base + S > c->index_topk || g_dsa_force)) { /* the scores' scratch: rows x context */- int64_t lim = ((int64_t)32 << 20) / (pos_base + S);- if (lim < CH) CH = lim < 1 ? 1 : (int)lim;- }- int rows = S < CH ? S : CH;- if (ch->ks.on && rows > ch->ks.chunk) rows = ch->ks.chunk; /* a step's rows fit the split's window */- if (!mirror_ok || !glmc_scratch(ch, m, rows, pos_base + S)) {+ if (!glmc_scratch(ch, m, rows, pos_base + S)) {fprintf(stderr, "[VK] colibri chain: device memory for %d rows at %d positions refused; per-matrix path\n",rows, pos_base + S);ch->failed = 1;@@ -553,7 +587,7 @@ static int glmc_forward(Model *m, float *xh, int S, int pos_base) {if (!outs) return 0;float *inv = R ? malloc((size_t)(R / 2) * sizeof(float)) : NULL;for (int j = 0; j < R / 2; j++) inv[j] = powf(c->theta, -2.0f * j / R); /* rope_interleave's frequencies */- for (int c0 = 0; c0 < S; c0 += rows) {+ for (int c0 = 0, ci = 0; c0 < S; c0 += rows, ci++) {int n = S - c0 < rows ? S - c0 : rows, pb = pos_base + c0;for (int s = 0; s < n && R; s++) /* the CPU's own angles, cosines and sines */for (int j = 0; j < R / 2; j++) {@@ -563,20 +597,24 @@ static int glmc_forward(Model *m, float *xh, int S, int pos_base) {}if (!vkc_begin() || !vkc_write(ch->x, 0, xh + (size_t)c0 * D, (size_t)n * D * sizeof(float)) ||!glmc_push_kv(ch, m, pb, n)) goto lost;- int ok = 1, pending = 0, pulled = 0, sel_on = 0;- for (int i = 0; i < N && ok; i++) {+ int ok = 1, pending = 0, pulled = lo, sel_on = 0;+ if (sel_in && on_in[ci]) { /* the primary's selection, for the layers here that reuse it */+ ok = vkc_write(ch->sel, 0, sel_in + (size_t)c0 * (1 + TK), (size_t)n * (1 + TK) * sizeof(int));+ sel_on = 1;+ }+ for (int i = lo; i < hi && ok; i++) {Layer *l = &m->L[i];if (g_spec && g_prefetch && l->sparse && m->enr[i] > 0) /* the CPU path's I/O hint */for (int z = 0; z < m->enr[i]; z++) if (!(n <= 4 && vk_reg_served(i, m->eroute[i][z]))) expert_prefetch(m, i, m->eroute[i][z]);if (pending) { ok = glmc_combine(ch, m, n); pending = 0; }+ size_t ko = (size_t)(i - lo) * ch->kvd_layer;ok = ok && glmc_norm(ch->x, ch->prm, ch->o_in[i], ch->nrm, n, D, c->eps) &&- (ch->ks.on ? vkc_kv_mla_qkv(&ch->ks, i, &ch->mla[i], &ch->sc, ch->nrm, 0, n, pb, ch->cs, &ch->kv[i], &ch->kvtmp,- ch->kvd, (size_t)i * ch->kvd_layer)- : vkc_mla_qkv(&ch->mla[i], &ch->sc, ch->nrm, 0, n, pb, ch->cs, &ch->kv[i], ch->kvd,- (size_t)i * ch->kvd_layer));+ (ch->ks.on ? vkc_kv_mla_qkv(&ch->ks, i - lo, &ch->mla[i], &ch->sc, ch->nrm, 0, n, pb, ch->cs, &ch->kv[i], &ch->kvtmp,+ ch->kvd, ko)+ : vkc_mla_qkv(&ch->mla[i], &ch->sc, ch->nrm, 0, n, pb, ch->cs, &ch->kv[i], ch->kvd, ko));if (ok && glmc_full(m, i)) ok = glmc_dsa(ch, m, i, n, pb, &sel_on);if (ok && ch->ks.on) /* the split: the core over the device's blocks and the host's rows */- ok = vkc_kv_mla_attn(&ch->ks, i, &ch->mla[i], &ch->sc, n, pb, 0, &ch->kv[i], sel_on ? ch->sel : NULL, 0,+ ok = vkc_kv_mla_attn(&ch->ks, i - lo, &ch->mla[i], &ch->sc, n, pb, 0, &ch->kv[i], sel_on ? ch->sel : NULL, 0,1 + c->index_topk, NULL, 0, ch->tmp, 0, m->Lc[i], m->Rc[i]);else ok = ok && vkc_mla_attn(&ch->mla[i], &ch->sc, n, pb, 0, &ch->kv[i], sel_on ? ch->sel : NULL, 0,1 + c->index_topk, NULL, 0, ch->tmp, 0);@@ -605,19 +643,24 @@ static int glmc_forward(Model *m, float *xh, int S, int pos_base) {ok = ok && vkc_begin();pending = 1;}+ int keep_sel = hand_sel || sel_out;if (ok && pending) ok = glmc_combine(ch, m, n);ok = ok && vkc_copy(ch->xd, 0, ch->x, 0, (size_t)n * D) &&- (!hand_sel || !sel_on || vkc_copy(ch->seld, 0, ch->sel, 0, (size_t)n * (1 + TK))) && vkc_submit(1);+ (!keep_sel || !sel_on || vkc_copy(ch->seld, 0, ch->sel, 0, (size_t)n * (1 + TK))) && vkc_submit(1);if (!ok) goto lost;- glmc_pull_kv(ch, m, pulled, N, pb, n);+ glmc_pull_kv(ch, m, pulled, hi, pb, n);memcpy(outs + (size_t)c0 * D, vkc_ptr(ch->xd), (size_t)n * D * sizeof(float));+ if (sel_out) { /* the raw rows, for the second device's chain */+ on_out[ci] = (unsigned char)sel_on;+ if (sel_on) memcpy(sel_out + (size_t)c0 * (1 + TK), vkc_ptr(ch->seld), (size_t)n * (1 + TK) * sizeof(int));+ }for (int s = 0; hand_sel && s < n; s++) { /* in the CPU's form: a count (0 = every position), the positions */const int *sr = (const int *)vkc_ptr(ch->seld) + (size_t)s * (1 + TK);int k = sel_on && sr[0] > 0 ? (sr[0] < TK ? sr[0] : TK) : 0;if (k) memcpy(m->dsa_sel + (size_t)(c0 + s) * TK, sr + 1, (size_t)k * sizeof(int));m->dsa_nsel[c0 + s] = k;}- for (int i = 0; i < N; i++) { ch->kv_valid[i] = pb + n; vkc_kv_done(&ch->ks, i, pb + n); }+ for (int i = lo; i < hi; i++) { ch->kv_valid[i] = pb + n; vkc_kv_done(&ch->ks, i - lo, pb + n); }}memcpy(xh, outs, (size_t)S * D * sizeof(float));free(inv); free(outs);@@ -625,22 +668,95 @@ static int glmc_forward(Model *m, float *xh, int S, int pos_base) {return N;lost: /* a frame failed: the device is gone (or would not take a command); the CPU takes over */free(inv); free(outs);- if (!vkc_lost()) { vkc_finish(); coli_vk_mark_lost(); }+ if (!vkc_lost()) { vkc_finish(); coli_vk_mark_lost_dev(ch->d); }g_vk_chain = 0; ch->failed = 1;- fprintf(stderr, "[VK] colibri chain: the device was lost; the CPU runs this forward again and from here on "- "(the KV cache is the host's: there is no state to rebuild)\n");+ { GlmChain *o = glmc_of(m, !ch->d); if (o) o->failed = 1; }+ fprintf(stderr, "[VK] %s chain: the device was lost; the CPU runs this forward again and from here on "+ "(the KV cache is the host's: there is no state to rebuild)\n", glmc_name(ch));+ *lost = 1;return 0;}+/* The devices' layers (every layer, or the primary's first n of a partial chain and the+ * second device's after them) for S rows of xh (positions pos_base..), the rows after the+ * last of them back into xh. Returns how many layers ran: L, or fewer (the caller's CPU+ * loop runs the rest from xh); 0 = not taken: the CPU runs the forward (from xh, as it+ * came in). */+static int glmc_forward(Model *m, float *xh, int S, int pos_base) {+ if (!g_vk_chain) return 0;+ if (g_vk_chain == COLI_VK_CHAIN_PREFILL && S <= 2) return 0; /* prompts only: decode on the CPU */+ if (g_pilot || g_pilot_real || g_looka || g_kv8 || g_tq || (exact_verify_on() && g_spec_live)) return 0;+ GlmChain *ch = (GlmChain *)m->vkchain, *ch2 = (GlmChain *)m->vkchain2;+ if (!ch || !ch->ok || ch->failed || vkc_lost()) return 0;+ if (ch2 && (!ch2->ok || ch2->failed || ch2->lo != ch->lo + ch->n)) ch2 = NULL;+ Cfg *c = &m->c; int D = c->hidden, L = c->n_layers, TK = c->index_topk;+ if (!m->Lc || !m->Rc || (m->has_dsa && !m->Ic)) return 0;+ for (int i = 0; i < L; i++) if (m->kv_start[i] != 0 || !m->Lc[i]) return 0;+ /* the chunk: the smaller of the two chains' (the selection crosses chunk by chunk) */+ int mirror_ok = glmc_mirror(ch, m, pos_base + S, 1);+ int CH = mirror_ok ? glmc_chunk_rows(ch, m) : 1;+ if (ch2 && mirror_ok) {+ vkc_device(1);+ if (!glmc_mirror(ch2, m, pos_base + S, 1)) ch2 = NULL;+ else { int CH2 = glmc_chunk_rows(ch2, m); if (CH2 < CH) CH = CH2; }+ vkc_device(0);+ }+ if (m->has_dsa && (pos_base + S > TK || g_dsa_force)) { /* the scores' scratch: rows x context */+ int64_t lim = ((int64_t)32 << 20) / (pos_base + S);+ if (lim < CH) CH = lim < 1 ? 1 : (int)lim;+ }+ int rows = S < CH ? S : CH;+ if (ch->ks.on && rows > ch->ks.chunk) rows = ch->ks.chunk; /* a step's rows fit the split's window */+ if (ch2 && ch2->ks.on && rows > ch2->ks.chunk) rows = ch2->ks.chunk;+ if (!mirror_ok) {+ fprintf(stderr, "[VK] colibri chain: device memory for %d rows at %d positions refused; per-matrix path\n",+ rows, pos_base + S);+ ch->failed = 1;+ return 0;+ }+ /* the second device's first layer reusing the primary's last selection: it crosses */+ int cross = ch2 && m->has_dsa && !glmc_full(m, ch2->lo);+ int nch = (S + rows - 1) / rows;+ int *sel = cross ? malloc((size_t)S * (1 + TK) * sizeof(int)) : NULL;+ unsigned char *on = cross ? calloc((size_t)nch, 1) : NULL;+ float *keep = ch2 ? malloc((size_t)S * D * sizeof(float)) : NULL; /* the rows as they came in */+ if ((cross && (!sel || !on)) || (ch2 && !keep)) { free(sel); free(on); free(keep); ch2 = NULL; sel = NULL; on = NULL; keep = NULL; cross = 0; }+ if (keep) memcpy(keep, xh, (size_t)S * D * sizeof(float));+ int lost = 0;+ int n = glmc_forward_seg(m, ch, xh, S, pos_base, rows, NULL, NULL, sel, on, &lost);+ if (!n || !ch2) { free(sel); free(on); free(keep); return n; }+ vkc_device(1);+ int n2 = glmc_forward_seg(m, ch2, xh, S, pos_base, rows, sel, on, NULL, NULL, &lost);+ vkc_device(0);+ free(sel); free(on);+ if (lost) { memcpy(xh, keep, (size_t)S * D * sizeof(float)); free(keep); return 0; }+ free(keep);+ if (!n2) { /* declined: the CPU runs its layers this step (their mirror follows) */+ for (int i = ch2->lo; i < ch2->lo + ch2->n; i++) {+ if (ch2->kv_valid[i] > pos_base) ch2->kv_valid[i] = pos_base;+ vkc_kv_lower(&ch2->ks, i - ch2->lo, pos_base);+ }+ /* the CPU's layer after the primary's reuses its selection: the CPU form is there+ * only when the primary handed it over (its next layer is the second device's) */+ return n;+ }+ return n + n2;+}+static void glmc_report(Model *m) {- GlmChain *ch = m ? (GlmChain *)m->vkchain : NULL;- if (!ch || !ch->ok || !ch->forwards) return;- VkcStats st; vkc_stats(&st);- fprintf(stderr, "[VK] colibri chain: %llu forwards, %llu frames (%llu ops, %llu matmuls, %llu tiled GEMM), "- "%.1f ms waiting for the device, %.1f ms of routed experts on the host, %.1f MiB on the device\n",- ch->forwards, st.frames, st.ops, st.matmuls, st.gemms, st.wait_ms, ch->host_ms, st.dev_bytes / 1048576.0);- vkc_kv_report(&ch->ks);- vkc_prof_print();+ for (int d = 0; d < 2 && m; d++) {+ GlmChain *ch = glmc_of(m, d);+ if (!ch || !ch->ok || !ch->forwards) continue;+ int was = vkc_device(d);+ VkcStats st; vkc_stats(&st);+ fprintf(stderr, "[VK] %s chain: %llu forwards, %llu frames (%llu ops, %llu matmuls, %llu tiled GEMM), "+ "%.1f ms waiting for the device, %.1f ms of routed experts on the host, %.1f MiB on the device\n",+ glmc_name(ch), ch->forwards, st.frames, st.ops, st.matmuls, st.gemms, st.wait_ms, ch->host_ms,+ st.dev_bytes / 1048576.0);+ vkc_kv_report(&ch->ks);+ vkc_prof_print();+ vkc_device(was);+ }}static Model *g_glmc_model;static void glmc_report_atexit(void) { glmc_report(g_glmc_model); }@@ -671,7 +787,24 @@ static void glmc_start(Model *m) {if (no) g_glmc_fitted = 0; /* no chain after all: everything as before */glm_dho_start(m);if (!g_glmc_fitted) return;+ int n0 = g_glmc_fit.n;if (g_glmc_fit.n > 0 && glmc_setup(m)) g_vk_chain = on;+ if (g_glmc_fit2_on) {+ GlmChain *ch = (GlmChain *)m->vkchain;+ vkc_device(1);+ if (!g_vk_chain || !ch || ch->n < n0) {+ /* the primary placed fewer layers than its fit: the second device's would not+ * follow them, so they stay on the CPU too */+ fprintf(stderr, "[VK] colibri chain: the primary device stopped before layer %d; layers %d..%d stay on the CPU, "+ "not on the second device\n", n0, n0, n0 + g_glmc_fit2.n - 1);+ g_glmc_fit2_on = 0;+ vkc_shutdown();+ } else {+ if (!glmc_setup_dev(m, 1)) { g_glmc_fit2_on = 0; vkc_shutdown(); }+ vkc_fit_placed("colibri dev2", &g_glmc_fit2);+ }+ vkc_device(0);+ }g_glmc_partial = vkc_fit_partial(&g_glmc_fit);for (int i = 0; i < g_glmc_fit.n; i++) g_glmc_lazy += g_glmc_mir[i]; /* for the tier (vk_tier_start) */vkc_fit_placed("colibri", &g_glmc_fit);@@ -688,7 +821,7 @@ static void glmc_held_atexit(void) {/* After the tier's: at exit the report runs first, then the chain goes, then the device. */static void glmc_atexit(Model *m) {if (!g_glmc_inited) return;- atexit(vkc_shutdown);+ atexit(vkc_shutdown_all);if (g_vk_chain) { g_glmc_model = m; atexit(glmc_report_atexit); }if (g_glmc_fitted && g_glmc_partial) atexit(glmc_held_atexit);}diff --git a/c/inkling_chain.h b/c/inkling_chain.hindex 7ee6f938..2ce78723 100644--- a/c/inkling_chain.h+++ b/c/inkling_chain.h@@ -495,7 +495,15 @@ static InkChain *inkc_setup_dev(Model *m, int d) {ch->prm = vkc_buf(n * sizeof(float), VKC_DEV);int ok = ch->prm && vkc_begin() && vkc_write(ch->prm, 0, arena, n * sizeof(float)) && vkc_submit(1);free(arena);- if (!ok) { inkc_free_own(m, ch); return NULL; }+ if (!ok) {+ inkc_free_own(m, ch);+ if (d && fit) { /* the second device's parameter buffer: its layers stay on the CPU */+ vkc_fit_shrink(inkc_name(ch), fitp, 0, vkc_lost() ? "the device was lost" : "its parameter buffer was refused");+ g_inkc_fit2_on = 0;+ vkc_shutdown();+ }+ return NULL;+ }/* geometry of the scratch; the frames: a MoE layer ends one (its host step) */inkc_geom(ch, m);for (int i = lo; i < lo + nl; i++)@@ -518,7 +526,7 @@ static InkChain *inkc_setup_dev(Model *m, int d) {}static InkChain *inkc_setup(Model *m) { return inkc_setup_dev(m, 0); }/* The chain declined after its layers went up: the copies only it holds (its own, not the- * per-matrix path's tables) are freed, with whatever device memory it took. */+ * per-matrix path's tables; the second device's are all its own) are freed, with whatever device memory it took. */static void inkc_free_own(Model *m, InkChain *ch) {if (vkc_ready()) vkc_finish();int L = m->c.n_layers, nsl = m->c.n_shared > 0 ? m->c.n_shared : 1;@@ -526,17 +534,17 @@ static void inkc_free_own(Model *m, InkChain *ch) {Layer *l = &m->L[i];ColiVkTensor **t[] = {&ch->t_q[i], &ch->t_k[i], &ch->t_v[i], &ch->t_r[i], &ch->t_o[i], &ch->t_dg[i], &ch->t_du[i], &ch->t_dd[i]};Wt *w[] = {&l->q, &l->k, &l->v, &l->r, &l->o, &l->dg, &l->du, &l->dd};- for (size_t k = 0; k < sizeof t / sizeof t[0]; k++) { if (*t[k] && !w[k]->vk) coli_vk_tensor_free(*t[k]); *t[k] = NULL; }+ for (size_t k = 0; k < sizeof t / sizeof t[0]; k++) { if (*t[k] && (ch->d || !w[k]->vk)) coli_vk_tensor_free(*t[k]); *t[k] = NULL; }for (int j = 0; j < nsl; j++) {ColiVkTensor **s[3] = {&ch->t_sg[(size_t)i * nsl + j], &ch->t_su[(size_t)i * nsl + j], &ch->t_sd[(size_t)i * nsl + j]};Wt *sw[3] = {&l->sh_g, &l->sh_u, &l->sh_d};- for (int k = 0; k < 3; k++) { if (*s[k] && !sw[k]->vk) coli_vk_tensor_free(*s[k]); *s[k] = NULL; }+ for (int k = 0; k < 3; k++) { if (*s[k] && (ch->d || !sw[k]->vk)) coli_vk_tensor_free(*s[k]); *s[k] = NULL; }}if (ch->t_router[i]) coli_vk_tensor_free(ch->t_router[i]);ch->t_router[i] = NULL;for (int b = 0; b < 4; b++) { vkc_free(ch->ring[b][i]); ch->ring[b][i] = NULL; }}- if (ch->t_lm && !m->lm_head.vk) coli_vk_tensor_free(ch->t_lm);+ if (ch->t_lm && (ch->d || !m->lm_head.vk)) coli_vk_tensor_free(ch->t_lm);ch->t_lm = NULL;vkc_free(ch->prm); ch->prm = NULL;}diff --git a/c/tests/vulkan_engines.sh b/c/tests/vulkan_engines.shindex 8dde7921..78024189 100755--- a/c/tests/vulkan_engines.sh+++ b/c/tests/vulkan_engines.sh@@ -4183,6 +4183,9 @@ case "${1:-}" inkv-split-sanitize) family_kv_split_sanitize ;;layers-dev2) family_layers_dev2 ;;layers-dev2-sanitize) family_layers_dev2_sanitize ;;+ layers-dev2-*) e=${1#layers-dev2-} # one engine's gates (the engine already built)+ declare -F "ld2_$e" >/dev/null || { echo "no layers-dev2 engine $e" >&2; exit 2; }+ OMP_NUM_THREADS=2 "ld2_$e" ;;kv-split-deepseek) family_kv_split_deepseek ;;kv-split-deepseek-sanitize) family_kv_split_deepseek_sanitize ;;staged) family_staged ;;diff --git a/c/tests/vulkan_layers_dev2.sh b/c/tests/vulkan_layers_dev2.shindex f53e57d2..27656dfe 100644--- a/c/tests/vulkan_layers_dev2.sh+++ b/c/tests/vulkan_layers_dev2.sh@@ -6,16 +6,28 @@# layers-dev2 every chain engine against its own CPU run# layers-dev2-sanitize the same paths under ASan and UBSan#-# Each gate (ld2_gate) is chain_gate's (the CPU's tokens, logits within 1e-4 of the-# largest) with the split forced, and the second device's chain must have run.+# Each gate (ld2_gate; ld2_mla for the MLA engines) is chain_gate's or mla_gate's (the+# CPU's tokens, logits within 1e-4 of the largest) with the split forced, and the second+# device's chain must have run with neither device lost.# ld2_gate <engine> <tag> <n0> <n1> <env...> -- <argv...>ld2_gate() {local eng=$1 tag=$2 n0=$3 n1=$4; shift 4chain_gate "$eng" "$tag" 1 COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=$n0 COLI_VK_CHAIN_LAYERS2=$n1 COLI_VK_TIER_BALANCE=0 "$@"- local f; f=$(ld2_forwards "$eng" vk.log)- [ "$f" -gt 0 ] || { cat vk.log; fail "$tag: the second device's chain never ran"; }- echo " $tag: the second device's chain ran $f forwards"+ ld2_ran "$eng" "$tag" vk.log+}+# ld2_mla <engine> <tag> <n0> <n1> <env...> -- <argv...>+ld2_mla() {+ local eng=$1 tag=$2 n0=$3 n1=$4; shift 4+ mla_gate "$eng" "$tag" 1 COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=$n0 COLI_VK_CHAIN_LAYERS2=$n1 COLI_VK_TIER_BALANCE=0 "$@"+ ld2_ran "$eng" "$tag" vk.log+}+# ld2_ran <engine> <tag> <log>: the second device's chain ran, no device was lost+ld2_ran() {+ local f; f=$(ld2_forwards "$1" "$3")+ [ "$f" -gt 0 ] || { cat "$3"; fail "$2: the second device's chain never ran"; }+ ! grep -qa 'the device is lost\|the device was lost' "$3" || { grep -a 'lost' "$3"; fail "$2: a device was lost"; }+ echo " $2: the second device's chain ran $f forwards"}# ld2_forwards <engine> <log>: N from "[VK] <engine> dev2 chain: N forwards"ld2_forwards() {@@ -34,7 +46,11 @@ ld2_lost() {env "${envs[@]}" ./"$eng" "$@" > cpu.log 2>&1 || trueenv "${envs[@]}" COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=$n0 COLI_VK_CHAIN_LAYERS2=$n1 COLI_VK_CHAIN_FAULT2=$k \COLI_USAGE=chain.usage COLI_VK_TIER_SYNC=1 COLI_VK_TIER_BALANCE=0 COLI_VULKAN=1 COLI_VK_CHAIN=1 ./"$eng" "$@" > vk.log 2>&1 || true- same_tokens cpu.log vk.log "$tag"+ case $eng in+ colibri) mla_toks "$eng" cpu.log > cpu.tok; mla_toks "$eng" vk.log > vk.tok+ { [ -s cpu.tok ] && cmp -s cpu.tok vk.tok; } || { cat cpu.tok vk.tok; tail -20 vk.log; fail "$tag: the tokens differ from the CPU"; } ;;+ *) same_tokens cpu.log vk.log "$tag" ;;+ esacgrep -q "COLI_VK_CHAIN_FAULT2" vk.log || { cat vk.log; fail "$tag: the second device's fault never fired"; }if [ "${LD2_EXPECT:-}" = setup ]; thengrep -q "^\[VK\] $eng dev2 chain: 0 of [0-9]* layers on the device: the chain stays off (layer 0 did not reach the device: the device was lost" vk.log ||@@ -42,6 +58,12 @@ ld2_lost() {echo "OK $tag: tokens = CPU, the second device's layers on the CPU from the start"return 0fi+ if [ "${LD2_EXPECT:-}" = again ]; then # the KV cache is the host's: the CPU runs the forward again+ grep -q "^\[VK\] $eng dev2 chain: the device was lost; the CPU runs this forward again" vk.log ||+ { cat vk.log; fail "$tag: the CPU did not take the forward over"; }+ echo "OK $tag: tokens = CPU, the CPU ran the forward again"+ return 0+ fiif [ "${LD2_EXPECT:-}" = redo ]; thengrep -q "the CPU redoes the step" vk.log || { cat vk.log; fail "$tag: the CPU did not redo the step"; }echo "OK $tag: tokens = CPU, the CPU redid the step"@@ -149,9 +171,7 @@ ld2_olmoe() {ld2_mimo_gate() { # <tag> <case> <n0> <n1> <env...>local tag=$1 c=$2 n0=$3 n1=$4; shift 4mimo_chain_gate "$tag" "$c" COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=$n0 COLI_VK_CHAIN_LAYERS2=$n1 COLI_VK_TIER_BALANCE=0 "$@"- local f; f=$(ld2_forwards mimo mimo-vk.err)- [ "$f" -gt 0 ] || { cat mimo-vk.err; fail "$tag $c: the second device's chain never ran"; }- echo " $tag $c: the second device's chain ran $f forwards"+ ld2_ran mimo "$tag $c" mimo-vk.err}ld2_mimo() {$PY tools/make_mimo_tiny.py --output ./mimo_tiny --force --vision@@ -188,19 +208,65 @@ ld2_inkling() {for b in 4 8; do ld2_gate inkling "ld2 inkling runtime int$b" 3 5 $T -- 2 $b $R; doneld2_gate inkling "ld2 inkling experts on both devices too" 3 5 $T COLI_VK_TIER_GB=0.00002 -- 8 0 $RCHAINMODE=2 ld2_gate inkling "ld2 inkling prompts only" 3 5 $T -- 8 0 $R- # the global layers' KV split on both devices- ld2_gate inkling "ld2 inkling KV split on both" 4 4 COLI_VK_KV_DEVICE_ROWS=16 COLI_VK_KV_BLOCK=4 $T -- 8 0 tiny_inkling/ref_long.json- [ "$(kv_hostparts inkling vk.log)" -gt 0 ] && [ "$(kv_hostparts "inkling dev2" vk.log)" -gt 0 ] ||- { cat vk.log; fail "ld2 inkling KV split on both: a device's split ran no host part"; }+ # the global layer's KV split (the fixture has one global layer, 5): on the second+ # device, then on the first+ ld2_gate inkling "ld2 inkling KV split on the second device" 4 4 COLI_VK_KV_DEVICE_ROWS=16 COLI_VK_KV_BLOCK=4 $T -- 8 0 tiny_inkling/ref_long.json+ [ "$(kv_hostparts "inkling dev2" vk.log)" -gt 0 ] || { cat vk.log; fail "ld2 inkling KV split on the second device: no host part"; }+ ld2_gate inkling "ld2 inkling KV split on the first device" 6 2 COLI_VK_KV_DEVICE_ROWS=16 COLI_VK_KV_BLOCK=4 $T -- 8 0 tiny_inkling/ref_long.json+ [ "$(kv_hostparts inkling vk.log)" -gt 0 ] || { cat vk.log; fail "ld2 inkling KV split on the first device: no host part"; }LD2_EXPECT=setup ld2_lost inkling "ld2 inkling second device lost at its setup" 3 5 1 $T -- 8 0 $Rld2_lost inkling "ld2 inkling second device lost mid-decode" 3 5 30 $T -- 8 0 $RCHAIN_SERVE_EXPECT='inkling dev2 chain: [1-9][0-9]* forwards' \$PY tests/vulkan_chain_serve.py ./inkling tiny_inkling INK_PREFIX_LOG=1 COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=3 COLI_VK_CHAIN_LAYERS2=5}+# colibri (GLM-5.2): the head stays on the host. glm_tiny_shx shares indexers (full,+# shared, full, shared, shared): with DSA top-4 the selection of the layer before the+# second device's first one crosses to it.+ld2_colibri() {+ [ -f glm_tiny_serve/tokenizer.json ] || glm_chain_fixtures+ [ -d glm_tiny_shx ] || ptl_glm_shx glm_tiny glm_tiny_shx+ export CAP_RAISE=0+ local G="SNAP=glm_tiny REF=ref_glm.json USAGE_SAVE=0" X="SNAP=glm_tiny_shx REF=ref_glm.json USAGE_SAVE=0" s+ ld2_mla colibri "ld2 colibri 2 + 3, every layer on the devices" 2 3 $G -- 64 16 16+ ld2_mla colibri "ld2 colibri 1 + 2, the CPU the rest" 1 2 $G -- 64 16 16+ ld2_mla colibri "ld2 colibri cap 1" 3 2 $G -- 1 16 16+ ld2_mla colibri "ld2 colibri prefill in chunks of 3" 2 3 $G TF=1 COLI_VK_CHAIN_ROWS=3 -- 64 16 16+ ld2_mla colibri "ld2 colibri 4-bit trunk and experts" 2 3 $G IDOT=0 -- 2 4 4+ ld2_mla colibri "ld2 colibri i4 container" 2 3 SNAP=glm_tiny_i4 REF=glm_tiny_i4/ref_glm.json USAGE_SAVE=0 IDOT=0 -- 1 4 4+ ld2_mla colibri "ld2 colibri DSA top-4" 2 3 $G DSA_TOPK=4 -- 64 16 16+ ld2_mla colibri "ld2 colibri DSA_FORCE" 2 3 $G DSA_FORCE=1 -- 64 16 16+ for s in "1 2" "3 2"; do+ ld2_mla colibri "ld2 colibri DSA top-4, a shared indexer first on the second device, ${s/ / + }" ${s% *} ${s#* } $X DSA_TOPK=4 -- 64 16 16+ ld2_mla colibri "ld2 colibri DSA top-4, a shared indexer first on the second device, ${s/ / + }, prefill in chunks of 5" \+ ${s% *} ${s#* } $X DSA_TOPK=4 TF=1 COLI_VK_CHAIN_ROWS=5 -- 64 16 16+ done+ ld2_mla colibri "ld2 colibri n-gram drafts" 2 3 $G DRAFT=3 -- 64 16 16+ ld2_mla colibri "ld2 colibri MTP depth 2" 2 3 SNAP=glm_tiny_mtp REF=glm_tiny_mtp/ref_glm.json USAGE_SAVE=0 DRAFT=2 -- 64 16 16+ ld2_mla colibri "ld2 colibri MTP with DSA top-4" 1 2 SNAP=glm_tiny_mtp REF=glm_tiny_mtp/ref_glm.json USAGE_SAVE=0 DSA_TOPK=4 -- 64 16 16+ ld2_mla colibri "ld2 colibri experts on both devices too" 2 3 $G COLI_VK_TIER_GB=0.00002 -- 64 16 16+ CHAINMODE=2 ld2_mla colibri "ld2 colibri prompts only, drafts" 2 3 $G DRAFT=3 -- 64 16 16+ ld2_mla colibri "ld2 colibri KV split on both" 2 3 COLI_VK_KV_DEVICE_ROWS=8 COLI_VK_KV_BLOCK=4 $G DSA_TOPK=4 -- 64 16 16+ [ "$(kv_hostparts colibri vk.log)" -gt 0 ] && [ "$(kv_hostparts "colibri dev2" vk.log)" -gt 0 ] ||+ { cat vk.log; fail "ld2 colibri KV split on both: a device's split ran no host part"; }+ # the second device lost: at its setup, in the prompt (one forward), mid-decode+ LD2_EXPECT=setup ld2_lost colibri "ld2 colibri second device lost at its setup" 2 2 1 $G -- 64 16 16+ LD2_EXPECT=again ld2_lost colibri "ld2 colibri second device lost in the prompt" 2 2 2 $G TF=1 -- 64 16 16+ LD2_EXPECT=again ld2_lost colibri "ld2 colibri second device lost mid-decode" 2 2 10 $G -- 64 16 16+ LD2_EXPECT=again ld2_lost colibri "ld2 colibri second device lost with MTP" 2 2 10 SNAP=glm_tiny_mtp REF=glm_tiny_mtp/ref_glm.json \+ USAGE_SAVE=0 DRAFT=2 -- 64 16 16+ # serve sessions: pins, the prompt cache, the prefill read-out, two KV slots+ CHAIN_SERVE_EXPECT='colibri dev2 chain: [1-9][0-9]* forwards' CHAIN_SERVE_DIALECT=colibri \+ $PY tests/vulkan_chain_serve.py ./colibri glm_tiny_serve SERVE_BATCH=1 IDOT=0 COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=2 COLI_VK_CHAIN_LAYERS2=3+ CHAIN_SERVE_EXPECT='colibri dev2 chain: [1-9][0-9]* forwards' CHAIN_SERVE_DIALECT=colibri \+ $PY tests/vulkan_chain_serve.py ./colibri glm_tiny_serve SERVE_BATCH=1 IDOT=0 DSA_TOPK=4 DRAFT=3 COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=1 COLI_VK_CHAIN_LAYERS2=2+ CHAIN_SERVE_EXPECT='colibri dev2 chain: [1-9][0-9]* forwards' CHAIN_SERVE_SLOTS=2 CHAIN_SERVE_DIALECT=colibri \+ $PY tests/vulkan_chain_serve.py ./colibri glm_tiny_serve SERVE_BATCH=1 IDOT=0 KV_SLOTS=2 COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=2 COLI_VK_CHAIN_LAYERS2=2+}+family_layers_dev2() {export OMP_NUM_THREADS=2- make qwen36 qwen38 olmoe mimo inkling tests/test_vk_chain VK=1+ make qwen36 qwen38 olmoe mimo inkling colibri tests/test_vk_chain VK=1COLI_VK_DEV2=0 ./tests/test_vk_chain shaders/qmatmul.spv | tee vk_chain.logtail -1 vk_chain.log | grep -qx PASS || fail "the chain's ops on two devices"ld2_qwen36@@ -208,6 +274,7 @@ family_layers_dev2() {ld2_olmoeld2_mimold2_inkling+ ld2_colibriunset OMP_NUM_THREADS}family_layers_dev2_sanitize() {@@ -227,7 +294,7 @@ family_layers_dev2_sanitize() {COLI_VK_KV_DEVICE_ROWS=16 COLI_VK_KV_BLOCK=4 PILOT=1 WIDE=2 SNAP=olmoe_tiny_c ./olmoe 2 8 olmoe_tiny/ref_olmoe_long.json[ "$(ld2_forwards olmoe san.log)" -gt 0 ] || { cat san.log; fail "asan ld2 olmoe: the second device's chain never ran"; }[ -f tiny_inkling/ref_long.json ] || kv_inkling_fixtures- kv_san inkling "asan ld2 inkling KV split on both" COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=4 COLI_VK_CHAIN_LAYERS2=4 \+ kv_san inkling "asan ld2 inkling KV split on the second device" COLI_VK_DEV2=0 COLI_VK_CHAIN_LAYERS=4 COLI_VK_CHAIN_LAYERS2=4 \COLI_VK_KV_DEVICE_ROWS=16 COLI_VK_KV_BLOCK=4 SNAP=tiny_inkling ./inkling 8 0 tiny_inkling/ref_long.json[ "$(ld2_forwards inkling san.log)" -gt 0 ] || { cat san.log; fail "asan ld2 inkling: the second device's chain never ran"; }make clean >/dev/null 2>&1 || true