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: Inkling's short convolution and attention as chain ops

JustVugg0b46757c64b1
diff --git a/c/Makefile b/c/Makefile
index f374f148..2609caf6 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -716,6 +716,9 @@ VK_OBJ += vk_chain.o
VK_SPV += shaders/chain_norm.spv shaders/chain_rope.spv shaders/chain_attn.spv shaders/chain_dnconv.spv \
shaders/chain_dnrec.spv shaders/chain_ew.spv shaders/chain_qsa.spv shaders/chain_ple.spv \
shaders/chain_gemv.spv
+# inkling's chain ops (made on first use): the short convolution and the attention
+# with its relative-position bias
+VK_SPV += shaders/chain_sconv.spv shaders/chain_relattn.spv
endif
# Optional AMD XDNA2 (Ryzen AI NPU) lane, Windows. Same opt-in shape as VK=1:
diff --git a/c/backend_vulkan.c b/c/backend_vulkan.c
index 2f06e123..20178258 100644
--- a/c/backend_vulkan.c
+++ b/c/backend_vulkan.c
@@ -1233,6 +1233,8 @@ int coli_vk_dense(void) { return g_dense_on; }
* 38.7 s, and lost decode, 3.2 against 3.8 tok/s: the device's GEMV at the GPU's
* 800 MHz floor is slower than the CPU's int8 one on a 4.3 GB trunk; prompts only
* still lost 5% of decode, the trunk's device copy taking from the tier's budget).
+ * An engine that has no measurement there passes COLI_VK_CHAIN_UNMEASURED: off, and
+ * the line says it was not measured.
* Unset, a CPU device (Lavapipe): off.
* Printed as a [VK] line with an engine name (NULL: silent). */
int coli_vk_chain_decide(const char *engine, int tier_on, int igpu) {
@@ -1244,12 +1246,12 @@ int coli_vk_chain_decide(const char *engine, int tier_on, int igpu) {
on = v == 0 ? COLI_VK_CHAIN_OFF : v == 2 ? COLI_VK_CHAIN_PREFILL : COLI_VK_CHAIN_ON;
snprintf(why, sizeof why, "COLI_VK_CHAIN=%s", e);
} else if (coli_vk_device_integrated()) {
- on = tier_on ? igpu : COLI_VK_CHAIN_OFF;
+ on = tier_on && igpu != COLI_VK_CHAIN_UNMEASURED ? igpu : COLI_VK_CHAIN_OFF;
snprintf(why, sizeof why, "an integrated GPU%s: %s; COLI_VK_CHAIN=0 off, 1 on, 2 prompts only",
tier_on ? " with the expert tier" : " without the expert tier",
on == COLI_VK_CHAIN_ON ? "measured faster on decode and prefill"
: on == COLI_VK_CHAIN_PREFILL ? "measured faster on prefill, slower on decode"
- : tier_on ? "measured slower on decode" : "not measured");
+ : tier_on && igpu != COLI_VK_CHAIN_UNMEASURED ? "measured slower on decode" : "not measured");
} else if (coli_vk_device_shares_ram()) {
on = COLI_VK_CHAIN_OFF;
snprintf(why, sizeof why, "a CPU device; COLI_VK_CHAIN=1 turns it on");
diff --git a/c/backend_vulkan.h b/c/backend_vulkan.h
index b811baff..64391814 100644
--- a/c/backend_vulkan.h
+++ b/c/backend_vulkan.h
@@ -249,6 +249,9 @@ unsigned long long coli_vk_matmul_calls(void);
#define COLI_VK_CHAIN_OFF 0
#define COLI_VK_CHAIN_ON 1
#define COLI_VK_CHAIN_PREFILL 2 /* forwards of more than two rows only */
+/* `igpu` only: the engine was not measured on an integrated GPU; off there, and the
+ * [VK] line says so */
+#define COLI_VK_CHAIN_UNMEASURED 3
int coli_vk_chain_decide(const char *engine, int tier_on, int igpu);
/* The device as the chain sees it: Vulkan handles as void * (VkInstance,
* VkPhysicalDevice, VkDevice, VkQueue), the memory types the backend picked, the
diff --git a/c/shaders/chain_relattn.comp b/c/shaders/chain_relattn.comp
new file mode 100644
index 00000000..5749774f
--- /dev/null
+++ b/c/shaders/chain_relattn.comp
@@ -0,0 +1,115 @@
+#version 450
+// Inkling's attention core (vk_chain.c, vkc_relattn): grouped-query attention of S query
+// rows with a learned relative-position bias, a log-length scale and an optional sliding
+// window, over a K/V cache on the device kept as a ring of `cap` rows per KV head
+// (position t at row t % cap; a global layer's cap is the whole context, so t itself).
+// One workgroup per (head h = WorkGroupID.x, row s = WorkGroupID.y), qpos = pos_base + s:
+// the positions t0 .. qpos, t0 = window > 0 ? max(0, qpos - window + 1) : 0;
+// score_t = tau[s] * (q . k_t * scale + (d < ext ? bias(d) : 0)), d = qpos - t,
+// bias(d) = sum_j r[s][h][j] * relp[j][d] (j < d_rel in order: attention()'s rl[])
+// ctx = sum_t softmax(score)_t v_t.
+// Rows of positions inside this step (t >= pos_base) come from the step's own K/V rows
+// (kvs: k at ks_off, v at vs_off, kv_row floats a row), the earlier ones from the ring,
+// as inkling.c's attention() reads them: the step's rows go into the ring after this
+// dispatch, so a step that wraps the ring never overwrites a row an earlier query of
+// the same step still reads. tau comes from the host (the CPU's double log, one per row).
+// The softmax runs online over tiles of 128 positions, as chain_attn.comp's does; the
+// CPU normalizes first, so the two agree to rounding.
+// Limits: hd <= 256 (two dims per thread), d_rel <= 64.
+layout(local_size_x = 128) in;
+
+layout(std430, binding = 0) readonly buffer QB { float q[]; };
+layout(std430, binding = 1) readonly buffer KB { float kc[]; };
+layout(std430, binding = 2) readonly buffer VB { float vc[]; };
+layout(std430, binding = 3) writeonly buffer OB { float o[]; };
+layout(std430, binding = 4) readonly buffer SB { float kvs[]; };
+layout(std430, binding = 5) readonly buffer RB { float r[]; };
+layout(std430, binding = 6) readonly buffer PB { float relp[]; };
+layout(std430, binding = 7) readonly buffer TB { float tau[]; };
+
+layout(push_constant) uniform P {
+ int S, H, KVH, hd, pos_base, cap, window, ext, d_rel;
+ int q_off, q_row, o_off, o_row;
+ int k_off, v_off, ks_off, vs_off, kv_row;
+ int r_off, r_row, relp_off, tau_off;
+ float scale;
+} p;
+
+shared float qs[256];
+shared float rs[64];
+shared float pe[128];
+shared int pt[128];
+shared float red[128];
+
+void main() {
+ int h = int(gl_WorkGroupID.x), s = int(gl_WorkGroupID.y);
+ int tid = int(gl_LocalInvocationID.x);
+ int kvh = h / (p.H / p.KVH);
+ int qb = p.q_off + s * p.q_row + h * p.hd;
+ for (int d = tid; d < p.hd; d += 128) qs[d] = q[qb + d];
+ for (int j = tid; j < p.d_rel; j += 128) rs[j] = r[p.r_off + s * p.r_row + h * p.d_rel + j];
+ int qpos = p.pos_base + s;
+ int t0 = p.window > 0 ? max(0, qpos - p.window + 1) : 0;
+ int n = qpos - t0 + 1;
+ float ts = tau[p.tau_off + s];
+ barrier();
+ float m = -3.0e38, l = 0.0, acc0 = 0.0, acc1 = 0.0;
+ for (int b0 = 0; b0 < n; b0 += 128) {
+ int i = b0 + tid;
+ float sv = -3.0e38;
+ if (i < n) {
+ int t = t0 + i;
+ pt[tid] = t;
+ int kb = t < p.pos_base ? p.k_off + (kvh * p.cap + t % p.cap) * p.hd
+ : p.ks_off + (t - p.pos_base) * p.kv_row + kvh * p.hd;
+ float a = 0.0;
+ if (t < p.pos_base) for (int d = 0; d < p.hd; d++) a += qs[d] * kc[kb + d];
+ else for (int d = 0; d < p.hd; d++) a += qs[d] * kvs[kb + d];
+ int dist = qpos - t;
+ float bias = 0.0;
+ if (dist < p.ext)
+ for (int j = 0; j < p.d_rel; j++) bias += rs[j] * relp[p.relp_off + j * p.ext + dist];
+ sv = ts * (a * p.scale + bias);
+ }
+ red[tid] = sv;
+ barrier();
+ for (int off = 64; off > 0; off >>= 1) {
+ if (tid < off) red[tid] = max(red[tid], red[tid + off]);
+ barrier();
+ }
+ float mn = max(m, red[0]);
+ barrier();
+ float e = i < n ? exp(sv - mn) : 0.0;
+ pe[tid] = e;
+ red[tid] = e;
+ barrier();
+ for (int off = 64; off > 0; off >>= 1) {
+ if (tid < off) red[tid] += red[tid + off];
+ barrier();
+ }
+ float corr = exp(m - mn);
+ l = l * corr + red[0];
+ acc0 *= corr; acc1 *= corr;
+ int cnt = min(128, n - b0);
+ int d0 = tid, d1 = tid + 128;
+ for (int j = 0; j < cnt; j++) {
+ int t = pt[j];
+ float w = pe[j];
+ if (t < p.pos_base) {
+ int vb = p.v_off + (kvh * p.cap + t % p.cap) * p.hd;
+ if (d0 < p.hd) acc0 += w * vc[vb + d0];
+ if (d1 < p.hd) acc1 += w * vc[vb + d1];
+ } else {
+ int vb = p.vs_off + (t - p.pos_base) * p.kv_row + kvh * p.hd;
+ if (d0 < p.hd) acc0 += w * kvs[vb + d0];
+ if (d1 < p.hd) acc1 += w * kvs[vb + d1];
+ }
+ }
+ m = mn;
+ barrier();
+ }
+ float inv = l > 0.0 ? 1.0 / l : 0.0;
+ int ob = p.o_off + s * p.o_row + h * p.hd;
+ if (tid < p.hd) o[ob + tid] = acc0 * inv;
+ if (tid + 128 < p.hd) o[ob + tid + 128] = acc1 * inv;
+}
diff --git a/c/shaders/chain_relattn.spv b/c/shaders/chain_relattn.spv
new file mode 100644
index 00000000..da24087e
Binary files /dev/null and b/c/shaders/chain_relattn.spv differ
diff --git a/c/shaders/chain_sconv.comp b/c/shaders/chain_sconv.comp
new file mode 100644
index 00000000..1b32c63f
--- /dev/null
+++ b/c/shaders/chain_sconv.comp
@@ -0,0 +1,52 @@
+#version 450
+// Inkling's depthwise causal short convolution (vk_chain.c, vkc_sconv), and the scalar
+// multiply and divide its dense MLP and its logits take. In place on x.
+// mode 0: one thread per channel c walks the S rows in order with the channel's ring
+// of the last CK-1 inputs (it lives on the device across steps):
+// acc = sum_{k < CK-1} w[c][k] * ring[k] + w[c][CK-1] * x[s][c]
+// x[s][c] = acc + x[s][c] (the residual inside the convolution)
+// then the ring drops its oldest input and takes x[s][c] as it was. That is
+// inkling.c's sconv_apply, the taps in its order, with no activation.
+// mode 1: x[i] = x[i] * fc, i < n (the dense MLP's global scale)
+// mode 2: x[i] = x[i] / fc, i < n (the logits' width multiplier)
+// x[x_off + s*x_row + c]; w[w_off + c*CK + k]; ring[ring_off + c*(CK-1) + k], oldest first.
+// Limits: CK <= 9.
+layout(local_size_x = 64) in;
+
+layout(std430, binding = 0) buffer XB { float x[]; };
+layout(std430, binding = 1) readonly buffer WB { float w[]; };
+layout(std430, binding = 2) buffer RB { float ring[]; };
+
+layout(push_constant) uniform P {
+ int mode, S, C, CK;
+ int x_off, x_row, w_off, ring_off, n;
+ float fc;
+} p;
+
+void main() {
+ int c = int(gl_GlobalInvocationID.x + gl_GlobalInvocationID.y * gl_NumWorkGroups.x * 64u);
+ if (p.mode != 0) {
+ if (c >= p.n) return;
+ float v = x[p.x_off + c];
+ x[p.x_off + c] = p.mode == 1 ? v * p.fc : v / p.fc;
+ return;
+ }
+ if (c >= p.C) return;
+ int nh = p.CK - 1;
+ float hist[8];
+ for (int k = 0; k < 8; k++) hist[k] = k < nh ? ring[p.ring_off + c * nh + k] : 0.0;
+ int wb = p.w_off + c * p.CK;
+ float wl = w[wb + p.CK - 1];
+ for (int s = 0; s < p.S; s++) {
+ int xi = p.x_off + s * p.x_row + c;
+ float cur = x[xi];
+ float acc = 0.0;
+ /* constant trip counts: the ring stays in registers */
+ for (int k = 0; k < 8; k++) if (k < nh) acc += w[wb + k] * hist[k];
+ acc += wl * cur;
+ x[xi] = acc + cur;
+ for (int k = 0; k < 7; k++) if (k + 1 < nh) hist[k] = hist[k + 1];
+ for (int k = 0; k < 8; k++) if (k == nh - 1) hist[k] = cur;
+ }
+ for (int k = 0; k < 8; k++) if (k < nh) ring[p.ring_off + c * nh + k] = hist[k];
+}
diff --git a/c/shaders/chain_sconv.spv b/c/shaders/chain_sconv.spv
new file mode 100644
index 00000000..5d1b6e33
Binary files /dev/null and b/c/shaders/chain_sconv.spv differ
diff --git a/c/tests/test_vk_chain.c b/c/tests/test_vk_chain.c
index cf036906..e965e27f 100644
--- a/c/tests/test_vk_chain.c
+++ b/c/tests/test_vk_chain.c
@@ -18,6 +18,12 @@
* qsa block keys, and the selection with ties broken as the CPU sorts
* ple the gate and the dilated convolution with its ring and snapshot
* frames vkc_write, vkc_read, a frame left in flight and one ordered after it
+ * sconv inkling's short convolution (residual inside, the ring carried across
+ * calls) and its scalar multiply and divide
+ * relattn inkling's attention: the relative-position bias, tau, a sliding window
+ * over a ring the step wraps, the step's rows read from its own K/V, and a
+ * global layer; the shared experts' weighted add (HC_APPLY over one stream)
+ * inkling's hidden size: the GEMV and the norm at D = 6144, the GEMV at I = 24576
*
* make vk-chain-check VK=1 (VK_ICD_FILENAMES=.../lvp_icd.json for Lavapipe) */
#include <stdio.h>
@@ -333,6 +339,145 @@ static void test_ew(void) {
free(a); free(b); free(c); free(e); free(ref);
}
+/* ---- inkling: the short convolution, the scale, the attention ----------------------- */
+/* inkling.c's sconv_apply for S rows of C channels, in place, the ring (CK-1 raw inputs) carried */
+static void sconv_ref(int S, int C, int CK, float *x, const float *w, float *ring) {
+ int P = CK - 1;
+ float *col = malloc((size_t)(P + S) * sizeof *col);
+ for (int c = 0; c < C; c++) {
+ for (int j = 0; j < P; j++) col[j] = ring[c * P + j];
+ for (int t = 0; t < S; t++) col[P + t] = x[(size_t)t * C + c];
+ for (int t = 0; t < S; t++) {
+ float acc = 0.f;
+ for (int j = 0; j < CK; j++) acc += w[c * CK + j] * col[t + j];
+ x[(size_t)t * C + c] = acc + col[P + t];
+ }
+ for (int j = 0; j < P; j++) ring[c * P + j] = col[S + j];
+ }
+ free(col);
+}
+static void test_sconv(int C, int CK) {
+ int S1 = 6, S2 = 1, P = CK - 1, xo = 8, ro = 4;
+ size_t nx = (size_t)(S1 + S2) * C;
+ float *w = fvec((size_t)C * CK, 0.5f), *x = fvec(nx, 1.f), *ring = fvec((size_t)C * P, 1.f);
+ float *xr = malloc(nx * sizeof *xr), *rr = malloc((size_t)C * P * sizeof *rr);
+ memcpy(xr, x, nx * sizeof *xr); memcpy(rr, ring, (size_t)C * P * sizeof *rr);
+ sconv_ref(S1, C, CK, xr, w, rr);
+ sconv_ref(S2, C, CK, xr + (size_t)S1 * C, w, rr);
+ VkcBuf *wb = up(w, (size_t)C * CK), *xb = vkc_buf((xo + nx) * 4, VKC_DEV), *rb = vkc_buf((ro + (size_t)C * P) * 4, VKC_DEV);
+ vkc_begin(); vkc_write(xb, xo, x, nx * 4); vkc_write(rb, ro, ring, (size_t)C * P * 4); vkc_submit(1);
+ VkcSconv p1 = {0, S1, C, CK, xo, C, 0, ro, 0, 1.f}, p2 = {0, S2, C, CK, xo + S1 * C, C, 0, ro, 0, 1.f};
+ vkc_begin(); int ok = vkc_sconv(xb, wb, rb, &p1); vkc_submit(0); /* left in flight */
+ vkc_begin(); ok &= vkc_sconv(xb, wb, rb, &p2); vkc_submit(1);
+ float *o = down(xb, xo, nx), *r = down(rb, ro, (size_t)C * P);
+ double e1 = relerr(o, xr, nx, 1e-3), e2 = relerr(r, rr, (size_t)C * P, 1e-3);
+ CHECK(ok && e1 < 1e-6 && e2 == 0, "sconv C %d CK %d: out %.2e ring %.2e", C, CK, e1, e2);
+ /* the scalar multiply and divide over n floats at an offset */
+ int n = 3 * C + 5; float fc = 24.f;
+ for (int mode = 1; mode <= 2; mode++) {
+ vkc_begin(); vkc_write(xb, xo, x, (size_t)n * 4); vkc_submit(1);
+ VkcSconv ps = {mode, 0, 0, 0, xo, 0, 0, 0, n, fc};
+ vkc_begin(); ok = vkc_sconv(xb, NULL, NULL, &ps); vkc_submit(1);
+ float *y = down(xb, xo, n), *ref = malloc((size_t)n * sizeof *ref);
+ for (int i = 0; i < n; i++) ref[i] = mode == 1 ? x[i] * fc : x[i] / fc;
+ double e = relerr(y, ref, n, 1e-6);
+ CHECK(ok && e < 1e-6, "sconv mode %d: err %.2e", mode, e);
+ free(y); free(ref);
+ }
+ vkc_free(wb); vkc_free(xb); vkc_free(rb);
+ free(w); free(x); free(ring); free(xr); free(rr); free(o); free(r);
+}
+/* inkling.c's attention() for S rows from pos_base: the K/V of every position in kt/vt
+ * ([T][KVH*hd]), r [S][H*dr], relp [dr][ext], tau [S]; the device sees the positions
+ * before pos_base through a ring of cap rows (t % cap, the latest such t < pos_base)
+ * and the step's own rows in a scratch, k at ks, v at vs, kvd floats a row. */
+static void test_relattn(int S, int pos_base, int window, int cap, int hd, int dr, int ext) {
+ int H = 4, KVH = 2, kvd = KVH * hd, T = pos_base + S, koff = 32, roff = 16, poff = 24, toff = 8;
+ float *q = fvec((size_t)S * H * hd, 1.f), *kt = fvec((size_t)T * kvd, 1.f), *vt = fvec((size_t)T * kvd, 1.f);
+ float *r = fvec((size_t)S * H * dr, 0.5f), *relp = fvec((size_t)dr * ext, 0.5f), *tau = malloc(S * sizeof *tau);
+ for (int s = 0; s < S; s++) tau[s] = 1.f + (rnd() % 1000) / 2000.f;
+ float scale = 1.f / (float)hd;
+ float *ref = malloc((size_t)S * H * hd * sizeof *ref);
+ for (int s = 0; s < S; s++) for (int h = 0; h < H; h++) {
+ int kvh = h / (H / KVH), qpos = pos_base + s, t0 = window > 0 && qpos - window + 1 > 0 ? qpos - window + 1 : 0;
+ int n = qpos - t0 + 1;
+ double *sc = malloc(n * sizeof *sc), mx = -1e300, sum = 0;
+ for (int j = 0; j < n; j++) {
+ int t = t0 + j, dist = qpos - t;
+ double a = 0; for (int d = 0; d < hd; d++) a += (double)q[((size_t)s * H + h) * hd + d] * kt[(size_t)t * kvd + kvh * hd + d];
+ double b = 0; if (dist < ext) for (int k = 0; k < dr; k++) b += (double)r[((size_t)s * H + h) * dr + k] * relp[k * ext + dist];
+ sc[j] = tau[s] * (a * scale + b); if (sc[j] > mx) mx = sc[j];
+ }
+ for (int j = 0; j < n; j++) { sc[j] = exp(sc[j] - mx); sum += sc[j]; }
+ for (int d = 0; d < hd; d++) {
+ double a = 0; for (int j = 0; j < n; j++) a += sc[j] / sum * vt[(size_t)(t0 + j) * kvd + kvh * hd + d];
+ ref[((size_t)s * H + h) * hd + d] = (float)a;
+ }
+ free(sc);
+ }
+ /* the ring as the host keeps it after the positions before pos_base, and the scratch */
+ size_t nr = (size_t)koff + (size_t)KVH * cap * hd;
+ float *kr = calloc(nr, sizeof *kr), *vr = calloc(nr, sizeof *vr);
+ for (int t = 0; t < pos_base; t++) for (int h = 0; h < KVH; h++) {
+ memcpy(kr + koff + ((size_t)h * cap + t % cap) * hd, kt + (size_t)t * kvd + h * hd, hd * sizeof(float));
+ memcpy(vr + koff + ((size_t)h * cap + t % cap) * hd, vt + (size_t)t * kvd + h * hd, hd * sizeof(float));
+ }
+ size_t ks = 4, vs = ks + (size_t)S * kvd, nsc = vs + (size_t)S * kvd;
+ float *scr = calloc(nsc, sizeof *scr);
+ memcpy(scr + ks, kt + (size_t)pos_base * kvd, (size_t)S * kvd * sizeof(float));
+ memcpy(scr + vs, vt + (size_t)pos_base * kvd, (size_t)S * kvd * sizeof(float));
+ float *rbuf = calloc(roff + (size_t)S * H * dr, sizeof *rbuf), *pbuf = calloc(poff + (size_t)dr * ext, sizeof *pbuf), *tbuf = calloc(toff + S, sizeof *tbuf);
+ memcpy(rbuf + roff, r, (size_t)S * H * dr * sizeof(float)); memcpy(pbuf + poff, relp, (size_t)dr * ext * sizeof(float));
+ memcpy(tbuf + toff, tau, S * sizeof(float));
+ VkcBuf *qb = up(q, (size_t)S * H * hd), *kb = up(kr, nr), *vb = up(vr, nr), *sb = up(scr, nsc), *rb = up(rbuf, roff + (size_t)S * H * dr);
+ VkcBuf *pb = up(pbuf, poff + (size_t)dr * ext), *tb = up(tbuf, toff + S), *ob = vkc_buf((size_t)S * H * hd * 4, VKC_DOWN);
+ VkcRelAttn p = {S, H, KVH, hd, pos_base, cap, window, ext, dr, 0, H * hd, 0, H * hd, koff, koff, (int)ks, (int)vs, kvd,
+ roff, H * dr, poff, toff, scale};
+ vkc_begin(); int ok = vkc_relattn(qb, kb, vb, ob, sb, rb, pb, tb, &p); vkc_submit(1);
+ double e = relerr((float *)vkc_ptr(ob), ref, (size_t)S * H * hd, 1e-3);
+ CHECK(ok && e < 2e-5, "relattn S %d pos %d window %d cap %d hd %d d_rel %d ext %d: err %.2e", S, pos_base, window, cap, hd, dr, ext, e);
+ vkc_free(qb); vkc_free(kb); vkc_free(vb); vkc_free(sb); vkc_free(rb); vkc_free(pb); vkc_free(tb); vkc_free(ob);
+ free(q); free(kt); free(vt); free(r); free(relp); free(tau); free(ref); free(kr); free(vr); free(scr); free(rbuf); free(pbuf); free(tbuf);
+}
+/* the shared experts joining the routed sum: y = routed; y += w_j[r] * sh_j[r] in order */
+static void test_weighted_add(void) {
+ int R = 3, D = 70, NS = 2;
+ float *routed = fvec((size_t)R * D, 2.f), *sh = fvec((size_t)NS * R * D, 2.f), *w = fvec((size_t)NS * R, 1.f), *ref = malloc((size_t)R * D * sizeof *ref);
+ memcpy(ref, routed, (size_t)R * D * sizeof *ref);
+ for (int j = 0; j < NS; j++) for (int r = 0; r < R; r++) for (int d = 0; d < D; d++) ref[r * D + d] += w[j * R + r] * sh[((size_t)j * R + r) * D + d];
+ VkcBuf *yb = up(routed, (size_t)R * D), *sb = up(sh, (size_t)NS * R * D), *wb = up(w, (size_t)NS * R);
+ vkc_begin(); int ok = 1;
+ for (int j = 0; j < NS; j++) { VkcEw p = {VKC_EW_HC_APPLY, R * D, D, 1, 0, 1, 0, j * R, j * R * D, 0, 0, 1.f}; ok &= vkc_ew(yb, wb, sb, NULL, NULL, &p); }
+ vkc_submit(1);
+ float *y = down(yb, 0, (size_t)R * D);
+ double e = relerr(y, ref, (size_t)R * D, 1e-3);
+ CHECK(ok && e < 1e-6, "weighted add: err %.2e", e);
+ vkc_free(yb); vkc_free(sb); vkc_free(wb); free(routed); free(sh); free(w); free(ref); free(y);
+}
+static void test_inkling(void) {
+ test_sconv(37, 4); test_sconv(6144, 4); test_sconv(9, 1); test_sconv(5, 9);
+ test_relattn(1, 0, 0, 64, 16, 4, 32); /* the first position, global */
+ test_relattn(5, 200, 0, 300, 32, 16, 64); /* global, distances past ext (bias 0) */
+ test_relattn(1, 40, 16, 16, 32, 4, 16); /* decode, a sliding window over a wrapped ring */
+ test_relattn(20, 30, 16, 16, 16, 4, 16); /* a step that wraps the ring more than once */
+ test_relattn(130, 3, 0, 200, 24, 4, 200); /* two tiles of positions */
+ test_relattn(3, 513, 512, 512, 128, 16, 512); /* the real window and ring, hd 128 */
+ test_relattn(2, 9, 0, 64, 256, 64, 8); /* the largest head and bank */
+ test_weighted_add();
+ for (int k = 0; k < 4; k++) { int f[4] = {1, 4, 10, 11}; test_matmul(f[k], 1, 6144, 48, 0, 0); }
+ test_matmul(10, 1, 24576, 16, 0, 0); /* past chain_gemv's staging: qmatmul.comp */
+ test_matmul(4, 1, 24576, 16, 0, 0);
+ VkcNorm np = {2, 6144, 1, 0, 6144, 6144, 0, 6144, 6144, 0, 0, 0, 1e-6f, 1.f};
+ float *x = fvec(2 * 6144, 1.f), *w = fvec(6144, 1.f), *ref = malloc(2 * 6144 * sizeof *ref);
+ for (int r = 0; r < 2; r++) { double ms = 0; for (int i = 0; i < 6144; i++) ms += (double)x[r * 6144 + i] * x[r * 6144 + i];
+ float rr = 1.f / sqrtf((float)(ms / 6144) + 1e-6f); for (int i = 0; i < 6144; i++) ref[r * 6144 + i] = x[r * 6144 + i] * rr * w[i]; }
+ VkcBuf *xb = up(x, 2 * 6144), *wb = up(w, 6144), *yb = vkc_buf(2 * 6144 * 4, VKC_DOWN);
+ vkc_begin(); int ok = vkc_norm(xb, wb, yb, &np); vkc_submit(1);
+ double e = relerr((float *)vkc_ptr(yb), ref, 2 * 6144, 1e-3);
+ CHECK(ok && e < 1e-5, "norm D 6144: err %.2e", e);
+ vkc_free(xb); vkc_free(wb); vkc_free(yb); free(x); free(w); free(ref);
+}
+
/* ---- QSA: block keys and selection ----------------------------------------------------- */
static void test_qsa(void) {
int ID = 12, R = 2, half = 3, cap = 40, nbmax = cap / R, IQ = 2, budget = 6; /* take = 3 blocks */
@@ -521,6 +666,8 @@ int main(int argc, char **argv) {
test_qsa();
test_ple();
test_frames();
+ if (vkc_sconv_ready() && vkc_relattn_ready()) { test_inkling(); printf("inkling done\n"); }
+ else CHECK(0, "inkling's ops: chain_sconv.spv or chain_relattn.spv did not load");
VkcStats st; vkc_stats(&st);
printf("chain: %llu frames, %llu ops, %llu matmuls (%llu GEMM), %llu barriers\n", st.frames, st.ops, st.matmuls, st.gemms, st.barriers);
vkc_shutdown();
diff --git a/c/vk_chain.c b/c/vk_chain.c
index f4b81f04..f17b0909 100644
--- a/c/vk_chain.c
+++ b/c/vk_chain.c
@@ -758,6 +758,47 @@ int vkc_ple(VkcBuf *keys, VkcBuf *hyp, VkcBuf *val, VkcBuf *prm, VkcBuf *gated,
return record(K.pipe[P_PLE], bd, 8, p, sizeof *p, gx, gy, 1);
}
+/* ---- inkling's ops: chain_sconv.comp and chain_relattn.comp, made on first use ---- */
+static struct { VkShaderModule mod[2]; VkPipeline pipe[2]; int tried[2]; } KX;
+static VkPipeline kx_pipe(int k) {
+ static const char *const file[2] = {"chain_sconv.spv", "chain_relattn.spv"};
+ if (KX.pipe[k] || KX.tried[k] || !vkc_ready()) return KX.pipe[k];
+ KX.tried[k] = 1;
+ if ((KX.mod[k] = load_module(K.core.spv_path, file[k]))) KX.pipe[k] = make_pipe(KX.mod[k], NULL);
+ return KX.pipe[k];
+}
+static void kx_shutdown(void) {
+ for (int k = 0; k < 2; k++) {
+ if (KX.pipe[k]) vkDestroyPipeline(K.dev, KX.pipe[k], NULL);
+ if (KX.mod[k]) vkDestroyShaderModule(K.dev, KX.mod[k], NULL);
+ }
+ memset(&KX, 0, sizeof KX);
+}
+int vkc_sconv_ready(void) { return kx_pipe(0) != VK_NULL_HANDLE; }
+int vkc_relattn_ready(void) { return kx_pipe(1) != VK_NULL_HANDLE; }
+int vkc_sconv(VkcBuf *x, VkcBuf *w, VkcBuf *ring, const VkcSconv *p) {
+ VkPipeline pipe = kx_pipe(0);
+ if (!pipe || (p->mode == 0 && (p->CK < 1 || p->CK > 9))) return 0;
+ K.kind = p->mode == 0 ? PK_DNCONV : PK_EW;
+ uint32_t gx, gy;
+ if (p->mode == 0) {
+ VkcBind bd[3] = {B(x, 1), B(w, 0), B(ring, 1)};
+ grid(((uint64_t)p->C + 63) / 64, &gx, &gy);
+ return record(pipe, bd, 3, p, sizeof *p, gx, gy, 1);
+ }
+ VkcBind bd[1] = {B(x, 1)};
+ grid(((uint64_t)p->n + 63) / 64, &gx, &gy);
+ return record(pipe, bd, 1, p, sizeof *p, gx, gy, 1);
+}
+int vkc_relattn(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, VkcBuf *kvs, VkcBuf *r, VkcBuf *relp, VkcBuf *tau,
+ const VkcRelAttn *p) {
+ VkPipeline pipe = kx_pipe(1);
+ if (!pipe || p->hd > 256 || p->d_rel > 64 || p->d_rel < 0 || p->KVH < 1 || p->H % p->KVH || p->cap < 1) return 0;
+ K.kind = PK_ATTN;
+ VkcBind bd[8] = {B(q, 0), B(kc, 0), B(vc, 0), B(o, 1), B(kvs, 0), B(r, 0), B(relp, 0), B(tau, 0)};
+ return record(pipe, bd, 8, p, sizeof *p, (uint32_t)p->H, (uint32_t)p->S, 1);
+}
+
void vkc_stats(VkcStats *st) { *st = K.st; }
void vkc_prof_print(void) {
if (!K.prof) return;
@@ -793,6 +834,7 @@ void vkc_shutdown(void) {
vka_pool_destroy(P);
}
if (K.cpool) vkDestroyCommandPool(K.dev, K.cpool, NULL);
+ kx_shutdown();
for (int i = 0; i < P_NPIPE; i++) {
if (K.pipe[i]) vkDestroyPipeline(K.dev, K.pipe[i], NULL);
if (K.mod[i]) vkDestroyShaderModule(K.dev, K.mod[i], NULL);
diff --git a/c/vk_chain.h b/c/vk_chain.h
index 5c177295..006baddc 100644
--- a/c/vk_chain.h
+++ b/c/vk_chain.h
@@ -119,6 +119,22 @@ typedef struct { int mode, S, C, H, CK, NG, keys_off, hyp_off, val_off, snap_row
int vkc_ple(VkcBuf *keys, VkcBuf *hyp, VkcBuf *val, VkcBuf *prm, VkcBuf *gated, VkcBuf *normv,
VkcBuf *conv, VkcBuf *ring, const VkcPle *p);
+/* Inkling's ops, their pipelines made on first use (an engine checks *_ready at setup:
+ * a build without the shader keeps every other op, and that engine's chain off).
+ * chain_sconv.comp (mode 0: the depthwise causal short convolution, residual inside, in
+ * place, its ring carried; mode 1: x *= fc over n floats; mode 2: x /= fc) */
+typedef struct { int mode, S, C, CK, x_off, x_row, w_off, ring_off, n; float fc; } VkcSconv;
+int vkc_sconv_ready(void);
+int vkc_sconv(VkcBuf *x, VkcBuf *w, VkcBuf *ring, const VkcSconv *p);
+/* chain_relattn.comp: attention with a relative-position bias bank, a per-row scale
+ * tau and a sliding window over a ring cache, the step's own rows read from kvs */
+typedef struct { int S, H, KVH, hd, pos_base, cap, window, ext, d_rel;
+ int q_off, q_row, o_off, o_row, k_off, v_off, ks_off, vs_off, kv_row;
+ int r_off, r_row, relp_off, tau_off; float scale; } VkcRelAttn;
+int vkc_relattn_ready(void);
+int vkc_relattn(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, VkcBuf *kvs, VkcBuf *r, VkcBuf *relp, VkcBuf *tau,
+ const VkcRelAttn *p);
+
/* counters, for the engines' [VK] lines */
typedef struct {
unsigned long long frames, waits, ops, matmuls, gemms, barriers, bytes_up, bytes_down;