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: Kimi K3's attention residuals, SiTU-GLU and KDA order on the chain

JustVugg0b60e0f30b59
diff --git a/c/Makefile b/c/Makefile
index a60020f5..ac51658d 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -721,6 +721,8 @@ VK_SPV += shaders/chain_norm.spv shaders/chain_rope.spv shaders/chain_attn.spv
VK_SPV += shaders/chain_mla.spv shaders/chain_hgemv.spv shaders/chain_dsa.spv
# Kimi Delta Attention and manifold-constrained hyper-connections (vk_chain.h)
VK_SPV += shaders/chain_kda.spv shaders/chain_mhc.spv
+# Kimi K3's attention residuals and SiTU-GLU (vk_chain.h)
+VK_SPV += shaders/chain_ares.spv
endif
# Optional AMD XDNA2 (Ryzen AI NPU) lane, Windows. Same opt-in shape as VK=1:
diff --git a/c/shaders/chain_ares.comp b/c/shaders/chain_ares.comp
new file mode 100644
index 00000000..3c6f7464
--- /dev/null
+++ b/c/shaders/chain_ares.comp
@@ -0,0 +1,94 @@
+#version 450
+// Kimi K3's attention residuals and its activation on the dense chain (vk_chain.c,
+// PM_ARES), two modes.
+//
+// mode 0, the residual mix (kimi_k3.c's res_mix), for S rows. Row s mixes nb + 1
+// vectors of D floats: the block snapshots v_e at b_off + s*b_row + e*D (e < nb) and the
+// running prefix v_nb at x_off + s*x_row:
+// score_e = (v_e . w) / sqrt(mean(v_e^2) + eps) (w at w_off in prm)
+// y[y_off + s*y_row + d] = sum_e softmax(score)_e v_e[d] (the sum in e order)
+// One workgroup per row. The CPU sums the dot and the squares in double; here they are a
+// float tree, so the two agree to rounding. The softmax is the CPU's softmax_: the
+// maximum, exp(x - max) in order with their sum, then a division. nb <= 15.
+//
+// mode 1, SiTU-GLU, one thread per element i < n, in the CPU's order (situf_):
+// y[y_off + i] = b1*tanh(g/b1)*sigmoid(g)*b2*tanh(u/b2), g = x[x_off + i], u = blk[b_off + i]
+layout(local_size_x = 256) in;
+
+layout(std430, binding = 0) readonly buffer XB { float xv[]; }; // 0: the prefix; 1: g
+layout(std430, binding = 1) readonly buffer BB { float bv[]; }; // 0: the block snapshots; 1: u
+layout(std430, binding = 2) readonly buffer WB { float wv[]; }; // 0: prm (the score weight)
+layout(std430, binding = 3) writeonly buffer YB { float yv[]; };
+
+layout(push_constant) uniform P { int v[32]; } p;
+#define S_ p.v[1]
+#define D_ p.v[2]
+#define NB p.v[3]
+#define X_OFF p.v[4]
+#define X_ROW p.v[5]
+#define B_OFF p.v[6]
+#define B_ROW p.v[7]
+#define W_OFF p.v[8]
+#define Y_OFF p.v[9]
+#define Y_ROW p.v[10]
+#define EPS intBitsToFloat(p.v[11])
+// mode 1
+#define N_ p.v[1]
+#define B1 intBitsToFloat(p.v[11])
+#define B2 intBitsToFloat(p.v[12])
+
+shared float rms[256];
+shared float rdt[256];
+shared float sc[16];
+
+void mixrow() {
+ int s = int(gl_WorkGroupID.x + gl_WorkGroupID.y * gl_NumWorkGroups.x);
+ if (s >= S_) return; // uniform across the workgroup
+ int tid = int(gl_LocalInvocationID.x);
+ for (int e = 0; e <= NB; e++) {
+ bool blk = e < NB;
+ int base = blk ? B_OFF + s * B_ROW + e * D_ : X_OFF + s * X_ROW;
+ float ms = 0.0, dt = 0.0;
+ for (int d = tid; d < D_; d += 256) {
+ float x = blk ? bv[base + d] : xv[base + d];
+ ms += x * x;
+ dt += x * wv[W_OFF + d];
+ }
+ rms[tid] = ms; rdt[tid] = dt;
+ barrier();
+ for (int off = 128; off > 0; off >>= 1) {
+ if (tid < off) { rms[tid] += rms[tid + off]; rdt[tid] += rdt[tid + off]; }
+ barrier();
+ }
+ if (tid == 0) sc[e] = rdt[0] / sqrt(rms[0] / float(D_) + EPS);
+ barrier();
+ }
+ if (tid == 0) { // softmax_, in order
+ float m = sc[0];
+ for (int e = 1; e <= NB; e++) if (sc[e] > m) m = sc[e];
+ float sum = 0.0;
+ for (int e = 0; e <= NB; e++) { sc[e] = exp(sc[e] - m); sum += sc[e]; }
+ for (int e = 0; e <= NB; e++) sc[e] /= sum;
+ }
+ barrier();
+ int xb = X_OFF + s * X_ROW, bb = B_OFF + s * B_ROW, yb = Y_OFF + s * Y_ROW;
+ for (int d = tid; d < D_; d += 256) {
+ float a = 0.0;
+ for (int e = 0; e < NB; e++) a += sc[e] * bv[bb + e * D_ + d];
+ a += sc[NB] * xv[xb + d];
+ yv[yb + d] = a;
+ }
+}
+
+void situ() {
+ int i = int(gl_GlobalInvocationID.x + gl_GlobalInvocationID.y * gl_NumWorkGroups.x * 256u);
+ if (i >= N_) return;
+ float g = xv[X_OFF + i], u = bv[B_OFF + i];
+ float sg = 1.0 / (1.0 + exp(-g));
+ yv[Y_OFF + i] = B1 * tanh(g / B1) * sg * B2 * tanh(u / B2);
+}
+
+void main() {
+ if (p.v[0] == 0) mixrow();
+ else situ();
+}
diff --git a/c/shaders/chain_ares.spv b/c/shaders/chain_ares.spv
new file mode 100644
index 00000000..8d162a61
Binary files /dev/null and b/c/shaders/chain_ares.spv differ
diff --git a/c/shaders/chain_kda.comp b/c/shaders/chain_kda.comp
index a30505ce..92af4bf1 100644
--- a/c/shaders/chain_kda.comp
+++ b/c/shaders/chain_kda.comp
@@ -22,6 +22,10 @@
// Each column runs the CPU's products and sums in its order; the two l2 sums and the
// output norm's sum are made by one thread in order, as on the CPU.
// Parameters in prm at prm_off: A_log[H], dt[P], norm[VD].
+// flags (Kimi K3's kda_forward; 0 is the above, GLM-5.3's):
+// 1 prm holds exp(A_log) itself (the engine keeps A = exp(A_log)), used as it is
+// 2 the l2 sums take eps after the squares, q is normalized and then scaled
+// ((q * rq) * qscale, k * rk), and the update is k * ((v - mem) * beta)
layout(local_size_x = 128) in;
layout(constant_id = 0) const int KD = 128;
@@ -64,6 +68,7 @@ layout(push_constant) uniform P { int v[32]; } p;
#define LB intBitsToFloat(p.v[17])
#define NEPS intBitsToFloat(p.v[18])
#define EPS intBitsToFloat(p.v[19])
+#define FLAGS p.v[20]
shared float sq[KD];
shared float sk[KD];
@@ -102,7 +107,7 @@ void rec() {
float col[KD];
int sb = ST_OFF + h * KD * VD;
[[unroll]] for (int k = 0; k < KD; k++) col[k] = own ? st[sb + k * VD + v] : 0.0;
- float ea = exp(w[PRM_OFF + h]);
+ float ea = (FLAGS & 1) != 0 ? w[PRM_OFF + h] : exp(w[PRM_OFF + h]);
float nw = own ? w[PRM_OFF + H_ + Pw + v] : 0.0;
for (int s = 0; s < S_; s++) {
int mb = M_OFF + s * M_ROW;
@@ -114,21 +119,39 @@ void rec() {
}
barrier();
if (v == 0) { // the l2 sums, in order, eps first
- float qs = NEPS, ks = NEPS;
- for (int k = 0; k < KD; k++) { qs += sq[k] * sq[k]; ks += sk[k] * sk[k]; }
- scal[0] = (1.0 / sqrt(float(KD))) / sqrt(qs);
- scal[1] = 1.0 / sqrt(ks);
+ if ((FLAGS & 2) != 0) {
+ float qs = 0.0, ks = 0.0;
+ for (int k = 0; k < KD; k++) { qs += sq[k] * sq[k]; ks += sk[k] * sk[k]; }
+ scal[0] = 1.0 / sqrt(qs + NEPS);
+ scal[1] = 1.0 / sqrt(ks + NEPS);
+ } else {
+ float qs = NEPS, ks = NEPS;
+ for (int k = 0; k < KD; k++) { qs += sq[k] * sq[k]; ks += sk[k] * sk[k]; }
+ scal[0] = (1.0 / sqrt(float(KD))) / sqrt(qs);
+ scal[1] = 1.0 / sqrt(ks);
+ }
}
barrier();
float qn = scal[0], kn = scal[1];
float beta = sig(br[B_OFF + s * B_ROW + h]);
float val = own ? xin[mb + 2 * Pw + h * VD + v] : 0.0;
float mem = 0.0;
- [[unroll]] for (int k = 0; k < KD; k++) { col[k] *= al[k]; mem += (sk[k] * kn) * col[k]; }
float res = 0.0;
- [[unroll]] for (int k = 0; k < KD; k++) {
- col[k] += (sk[k] * kn) * (val - mem) * beta;
- res += (sq[k] * qn) * col[k];
+ if ((FLAGS & 2) != 0) { // kimi_k3.c: q and k normalized first, then the sweeps
+ for (int k = v; k < KD; k += 128) { sq[k] = (sq[k] * qn) * (1.0 / sqrt(float(KD))); sk[k] = sk[k] * kn; }
+ barrier();
+ [[unroll]] for (int k = 0; k < KD; k++) { col[k] *= al[k]; mem += sk[k] * col[k]; }
+ float vt = (val - mem) * beta;
+ [[unroll]] for (int k = 0; k < KD; k++) {
+ col[k] += sk[k] * vt;
+ res += sq[k] * col[k];
+ }
+ } else {
+ [[unroll]] for (int k = 0; k < KD; k++) { col[k] *= al[k]; mem += (sk[k] * kn) * col[k]; }
+ [[unroll]] for (int k = 0; k < KD; k++) {
+ col[k] += (sk[k] * kn) * (val - mem) * beta;
+ res += (sq[k] * qn) * col[k];
+ }
}
ov[v] = own ? res : 0.0;
barrier();
diff --git a/c/shaders/chain_kda.spv b/c/shaders/chain_kda.spv
index 110ddcd8..89e30de2 100644
Binary files a/c/shaders/chain_kda.spv and b/c/shaders/chain_kda.spv differ
diff --git a/c/tests/test_vk_chain.c b/c/tests/test_vk_chain.c
index a17d1626..7c896d57 100644
--- a/c/tests/test_vk_chain.c
+++ b/c/tests/test_vk_chain.c
@@ -32,6 +32,10 @@
* decay, beta, output norm and gate
* mhc hyper_connections.h's split, collapse and write back, the mean, and the
* clamped SwiGLU
+ * kda k3 Kimi K3's KDA layer (kimi_k3.c's kda_forward): exp(A_log) as the engine
+ * keeps it, its order of the l2 norms and the update, over two submissions
+ * ares Kimi K3's attention residuals (res_mix) over 0 to 15 block snapshots, and
+ * SiTU-GLU on values both sides of its two constants
*
* make vk-chain-check VK=1 (VK_ICD_FILENAMES=.../lvp_icd.json for Lavapipe) */
#include <stdio.h>
@@ -976,6 +980,122 @@ static void test_kpool(int S, int pos_base, int IH, int ID, int pool, int topk,
free(keys); free(gates); free(ape); free(q); free(hw); free(valid); free(ref); free(hwd); free(rk); free(gk);
}
+/* Kimi K3's KDA layer, kimi_k3.c's kda_forward row by row (its scalar path): the window
+ * shifted and the taps summed in order, silu; q and k l2-normalized with the eps after
+ * the squares, q then scaled; alpha from A = exp(A_log) as the engine keeps it; the
+ * recurrence k * ((v - kS) * beta); the output RMSNorm (its sum in double) and gate.
+ * S rows over two calls (the state and the window carried on the device). */
+static void test_kda_k3(int H, int KD, int CK, float xs) {
+ int VD = KD, P = H * KD, C = 3 * P, S = 6;
+ float lb = -5.f, neps = 1e-6f, eps = 1e-5f, qscale = 1.f / sqrtf((float)KD);
+ float *qkv = fvec((size_t)S * C, xs), *f = fvec((size_t)S * P, 1.f), *b = fvec((size_t)S * H, 2.f), *g = fvec((size_t)S * P, 2.f);
+ float *taps = fvec((size_t)C * CK, 0.7f), *win = fvec((size_t)C * CK, 1.f), *state = fvec((size_t)H * KD * VD, 0.3f);
+ float *prm = malloc((size_t)(H + P + VD) * 4);
+ for (int i = 0; i < H; i++) prm[i] = expf(frnd() * 0.5f); /* A = exp(A_log), as kimi_k3.c keeps it */
+ for (int i = 0; i < P; i++) prm[H + i] = frnd() * 0.5f;
+ for (int i = 0; i < VD; i++) prm[H + P + i] = 0.5f + (rnd() % 100) / 100.f;
+ float *rs = malloc((size_t)H * KD * VD * 4), *rw = malloc((size_t)C * CK * 4), *ref = malloc((size_t)S * P * 4);
+ memcpy(rs, state, (size_t)H * KD * VD * 4); memcpy(rw, win, (size_t)C * CK * 4);
+ float *vec = malloc((size_t)C * 4), *kS = malloc((size_t)VD * 4), *oh = malloc((size_t)VD * 4);
+ float *qn = malloc((size_t)KD * 4), *kn = malloc((size_t)KD * 4), *al = malloc((size_t)KD * 4);
+ for (int s = 0; s < S; s++) {
+ for (int c = 0; c < C; c++) { /* the short convolution, the window oldest first */
+ float *wd = rw + (size_t)c * CK;
+ for (int j = 0; j < CK - 1; j++) wd[j] = wd[j + 1];
+ wd[CK - 1] = qkv[(size_t)s * C + c];
+ float acc = 0; for (int j = 0; j < CK; j++) acc += taps[(size_t)c * CK + j] * wd[j];
+ vec[c] = acc / (1.f + expf(-acc));
+ }
+ for (int h = 0; h < H; h++) {
+ const float *qh = vec + h * KD, *kh = vec + P + h * KD, *vh = vec + 2 * P + h * VD;
+ float sq = 0, sk = 0;
+ for (int i = 0; i < KD; i++) { sq += qh[i] * qh[i]; sk += kh[i] * kh[i]; }
+ sq = 1.f / sqrtf(sq + neps); sk = 1.f / sqrtf(sk + neps);
+ for (int i = 0; i < KD; i++) { qn[i] = qh[i] * sq * qscale; kn[i] = kh[i] * sk; }
+ for (int i = 0; i < KD; i++) al[i] = expf(lb * sigm(prm[h] * (f[(size_t)s * P + h * KD + i] + prm[H + h * KD + i])));
+ float beta = sigm(b[(size_t)s * H + h]);
+ float *St = rs + (size_t)h * KD * VD;
+ memset(kS, 0, (size_t)VD * 4);
+ for (int k = 0; k < KD; k++) { float *row = St + (size_t)k * VD; for (int v = 0; v < VD; v++) { row[v] *= al[k]; kS[v] += kn[k] * row[v]; } }
+ for (int v = 0; v < VD; v++) kS[v] = (vh[v] - kS[v]) * beta;
+ memset(oh, 0, (size_t)VD * 4);
+ for (int k = 0; k < KD; k++) { float *row = St + (size_t)k * VD; for (int v = 0; v < VD; v++) { row[v] += kn[k] * kS[v]; oh[v] += qn[k] * row[v]; } }
+ double ms = 0; for (int v = 0; v < VD; v++) ms += (double)oh[v] * oh[v];
+ float r = 1.f / sqrtf((float)(ms / VD) + eps);
+ for (int v = 0; v < VD; v++) ref[(size_t)s * P + h * VD + v] = oh[v] * r * prm[H + P + v] * sigm(g[(size_t)s * P + h * VD + v]);
+ }
+ }
+ /* the device: q, k, v in three blocks [3][S][P] (in_part = S*P), as an engine's three matmuls leave them */
+ float *blk = malloc((size_t)3 * S * P * 4);
+ for (int s = 0; s < S; s++) for (int part = 0; part < 3; part++)
+ memcpy(blk + (size_t)part * S * P + (size_t)s * P, qkv + (size_t)s * C + (size_t)part * P, (size_t)P * 4);
+ VkcBuf *ib = up(blk, (size_t)3 * S * P), *tb = up(taps, (size_t)C * CK), *wb = up(win, (size_t)C * CK), *sb = up(state, (size_t)H * KD * VD);
+ VkcBuf *fb = up(f, (size_t)S * P), *bb = up(b, (size_t)S * H), *gb = up(g, (size_t)S * P), *pb = up(prm, (size_t)H + P + VD);
+ VkcBuf *mb = vkc_buf((size_t)S * C * 4, VKC_DEV), *yb = vkc_buf((size_t)S * P * 4, VKC_DEV);
+ int ok = 1, s0 = 0;
+ for (int part = 0; part < 2 && ok; part++) {
+ int n = part ? S - 1 : 1;
+ VkcKdaConv cp = {n, C, CK, P, s0 * P, P, S * P, 0, C, 0, 0};
+ VkcKdaRec rp = {n, H, VD, P, 0, C, s0 * P, P, s0 * H, H, s0 * P, P, s0 * P, P, 0, 0, lb, neps, eps};
+ ok = vkc_begin() && vkc_kda_conv(ib, tb, wb, mb, &cp) &&
+ vkc_kda_rec_flags(KD, mb, fb, bb, gb, pb, sb, yb, &rp, VKC_KDA_EXP_A | VKC_KDA_K3) && vkc_submit(part);
+ s0 += n;
+ }
+ float *y = down(yb, 0, (size_t)S * P), *st2 = down(sb, 0, (size_t)H * KD * VD), *w2 = down(wb, 0, (size_t)C * CK);
+ double e = relerr(y, ref, (size_t)S * P, 1e-3), es = relerr(st2, rs, (size_t)H * KD * VD, 1e-3), ew = relerr(w2, rw, (size_t)C * CK, 1e-3);
+ CHECK(ok && e < 1e-5 && es < 1e-5 && ew == 0 && !bad(y, (size_t)S * P), "kda k3 H %d KD %d K %d: ok %d out %.2e state %.2e window %.2e",
+ H, KD, CK, ok, e, es, ew);
+ if (getenv("VKC_TEST_VERBOSE")) printf("kda k3 H %d KD %d: out %.2e state %.2e\n", H, KD, e, es);
+ vkc_free(ib); vkc_free(tb); vkc_free(wb); vkc_free(sb); vkc_free(fb); vkc_free(bb); vkc_free(gb); vkc_free(pb); vkc_free(mb); vkc_free(yb);
+ free(qkv); free(f); free(b); free(g); free(taps); free(win); free(state); free(prm); free(rs); free(rw); free(ref);
+ free(vec); free(kS); free(oh); free(qn); free(kn); free(al); free(blk); free(y); free(st2); free(w2);
+}
+
+/* kimi_k3.c's res_mix for S rows over nb block snapshots at a row stride, and SiTU-GLU */
+static void test_ares(int S, int D, int nb) {
+ int nbmax = nb > 0 ? nb + 1 : 1, xrow = D + 3, brow = nbmax * D, yrow = D + 5;
+ float eps = 1e-5f;
+ float *x = fvec((size_t)S * xrow, 2.f), *bl = fvec((size_t)S * brow, 1.5f), *w = fvec((size_t)D + 7, 0.3f);
+ for (int s = 0; s < S; s++) for (int d = 0; d < D; d++) x[(size_t)s * xrow + d] *= 1.f + (float)s; /* rows of different sizes */
+ float *ref = calloc((size_t)S * yrow, 4);
+ for (int s = 0; s < S; s++) {
+ const float *v[16]; float sc[16];
+ for (int e = 0; e < nb; e++) v[e] = bl + (size_t)s * brow + (size_t)e * D;
+ v[nb] = x + (size_t)s * xrow;
+ for (int e = 0; e <= nb; e++) {
+ double ms = 0, dot = 0;
+ for (int d = 0; d < D; d++) { double a = v[e][d]; ms += a * a; dot += a * (double)w[7 + d]; }
+ sc[e] = (float)(dot / sqrt(ms / D + eps));
+ }
+ float m = sc[0]; for (int e = 1; e <= nb; e++) if (sc[e] > m) m = sc[e];
+ float sum = 0; for (int e = 0; e <= nb; e++) { sc[e] = expf(sc[e] - m); sum += sc[e]; }
+ for (int e = 0; e <= nb; e++) sc[e] /= sum;
+ for (int d = 0; d < D; d++) { float a = 0; for (int e = 0; e <= nb; e++) a += sc[e] * v[e][d]; ref[(size_t)s * yrow + d] = a; }
+ }
+ VkcBuf *xb = up(x, (size_t)S * xrow), *bb = up(bl, (size_t)S * brow), *wb = up(w, (size_t)D + 7), *yb = vkc_buf((size_t)S * yrow * 4, VKC_DEV);
+ VkcAres ap = {S, D, nb, 0, xrow, 0, brow, 7, 0, yrow, eps};
+ int ok = vkc_begin() && vkc_ares_mix(xb, bb, wb, yb, &ap) && vkc_submit(1);
+ float *y = down(yb, 0, (size_t)S * yrow);
+ for (int s = 0; s < S; s++) for (int d = D; d < yrow; d++) y[(size_t)s * yrow + d] = 0.f; /* the gaps are not written */
+ double e = relerr(y, ref, (size_t)S * yrow, 1e-3);
+ CHECK(ok && e < 2e-6 && !bad(y, (size_t)S * yrow), "ares S %d D %d nb %d: ok %d err %.2e", S, D, nb, ok, e);
+ if (getenv("VKC_TEST_VERBOSE")) printf("ares S %d D %d nb %d: %.2e\n", S, D, nb, e);
+ vkc_free(xb); vkc_free(bb); vkc_free(wb); vkc_free(yb);
+ free(x); free(bl); free(w); free(ref); free(y);
+}
+static void test_situ(float b1, float b2) {
+ int n = 777;
+ float *ga = fvec(n, 3.f * b1), *ua = fvec(n, 3.f * b2), *rr = malloc((size_t)n * 4);
+ for (int i = 0; i < n; i++) rr[i] = b1 * tanhf(ga[i] / b1) * sigm(ga[i]) * b2 * tanhf(ua[i] / b2);
+ VkcBuf *gb = up(ga, n), *ub = up(ua, n), *yb = vkc_buf((size_t)n * 4 + 64, VKC_DEV);
+ VkcSitu sp = {n, 0, 0, 16, b1, b2};
+ int ok = vkc_begin() && vkc_situ(gb, ub, yb, &sp) && vkc_submit(1);
+ float *y = down(yb, 16, n);
+ double e = relerr(y, rr, n, 1e-3);
+ CHECK(ok && e < 1e-6 && !bad(y, n), "situ b1 %g b2 %g: ok %d err %.2e", b1, b2, ok, e);
+ vkc_free(gb); vkc_free(ub); vkc_free(yb); free(ga); free(ua); free(rr); free(y);
+}
+
/* ---- frames: many ops, frames in flight, ordering ---------------------------------------- */
static void test_frames(void) {
int n = 1000;
@@ -1092,6 +1212,15 @@ int main(int argc, char **argv) {
printf("kda done\n");
test_mhc(2, 64, 3); test_mhc(4, 96, 20); test_mhc(3, 40, 1);
printf("mhc done\n");
+ test_kda_k3(2, 16, 4, 1.f); test_kda_k3(3, 32, 4, 1.f); test_kda_k3(2, 128, 4, 1.f); test_kda_k3(1, 64, 2, 1.f);
+ test_kda_k3(2, 16, 4, 0.002f);
+ printf("kda k3 done\n");
+ }
+ if (!vkc_ares_ready()) { fails++; printf("FAIL: the AttnRes shader did not load\n"); }
+ else {
+ test_ares(1, 128, 0); test_ares(3, 128, 1); test_ares(5, 300, 4); test_ares(2, 7168, 8); test_ares(4, 96, 15);
+ test_situ(1.f, 1.f); test_situ(4.f, 25.f);
+ printf("ares done\n");
}
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);
diff --git a/c/vk_chain.c b/c/vk_chain.c
index ee112a1d..996eb629 100644
--- a/c/vk_chain.c
+++ b/c/vk_chain.c
@@ -36,15 +36,15 @@ static const char *const pipe_file[P_NPIPE] = {
/* COLI_VK_CHAIN_PROF=1: device time per kind of op (timestamps after every op; ops
* that run side by side between two barriers share the time unevenly) */
enum { PK_GEMV8, PK_GEMV, PK_GEMM, PK_NORM, PK_ROPE, PK_ATTN, PK_DNCONV, PK_DNREC, PK_EW, PK_QSA, PK_PLE, PK_COPY,
- PK_MLA, PK_MLAW, PK_DSA, PK_KDA, PK_MHC, PK_N };
+ PK_MLA, PK_MLAW, PK_DSA, PK_KDA, PK_MHC, PK_ARES, PK_N };
static const char *const pk_name[PK_N] = {"GEMV int8", "GEMV other", "tiled GEMM", "norm", "rope", "attention",
"dn conv", "dn recurrence", "element-wise", "qsa", "ple", "copy",
- "mla", "mla weights", "dsa", "kda", "mhc"};
-/* the MLA, KDA and mHC pipelines: optional (an engine without such layers needs none
- * of them, and a missing one turns off only its own ops) */
-enum { PM_MLA, PM_HGEMV, PM_DSA, PM_KDA, PM_MHC, PM_N };
+ "mla", "mla weights", "dsa", "kda", "mhc", "attnres"};
+/* the MLA, KDA, mHC and AttnRes pipelines: optional (an engine without such layers
+ * needs none of them, and a missing one turns off only its own ops) */
+enum { PM_MLA, PM_HGEMV, PM_DSA, PM_KDA, PM_MHC, PM_ARES, PM_N };
static const char *const mla_file[PM_N] = {"chain_mla.spv", "chain_hgemv.spv", "chain_dsa.spv", "chain_kda.spv",
- "chain_mhc.spv"};
+ "chain_mhc.spv", "chain_ares.spv"};
#define VKC_KDA_MAX 8
/* ---- memory: blocks per kind, buffers bound at offsets inside them -------------- */
@@ -879,11 +879,16 @@ static VkPipeline kda_pipe(int KD) {
return p;
}
int vkc_kda_rec(int KD, VkcBuf *m, VkcBuf *f, VkcBuf *b, VkcBuf *g, VkcBuf *prm, VkcBuf *st, VkcBuf *y, const VkcKdaRec *p) {
+ return vkc_kda_rec_flags(KD, m, f, b, g, prm, st, y, p, 0);
+}
+int vkc_kda_rec_flags(int KD, VkcBuf *m, VkcBuf *f, VkcBuf *b, VkcBuf *g, VkcBuf *prm, VkcBuf *st, VkcBuf *y,
+ const VkcKdaRec *p, int flags) {
K.kind = PK_KDA;
- if (!K.mpipe[PM_KDA] || KD < 1 || KD > 256 || p->VD < 1 || p->VD > 128 || p->H < 1 || p->S < 1) return 0;
+ if (!K.mpipe[PM_KDA] || KD < 1 || KD > 256 || p->VD < 1 || p->VD > 128 || p->H < 1 || p->S < 1 ||
+ (flags & ~(VKC_KDA_EXP_A | VKC_KDA_K3))) return 0;
VkPipeline pipe = kda_pipe(KD);
if (!pipe) return 0;
- struct { int mode; VkcKdaRec b; } pc = {1, *p};
+ struct { int mode; VkcKdaRec b; int flags; } pc = {1, *p, flags};
VkcBind bd[7] = {B(m, 0), B(prm, 0), B(st, 1), B(y, 1), B(f, 0), B(b, 0), B(g, 0)};
return record(pipe, bd, 7, &pc, sizeof pc, (uint32_t)p->H, 1, 1);
}
@@ -902,6 +907,28 @@ int vkc_mhc(int mode, VkcBuf *x, VkcBuf *m, VkcBuf *hp, VkcBuf *prm, VkcBuf *y,
return n == 0 || record(K.mpipe[PM_MHC], bd, 5, &pc, sizeof pc, gx, gy, 1);
}
+/* ---- attention residuals and SiTU-GLU (chain_ares.comp) ---------------------------- */
+int vkc_ares_ready(void) { return vkc_ready() && K.mpipe[PM_ARES]; }
+int vkc_ares_mix(VkcBuf *x, VkcBuf *blk, VkcBuf *prm, VkcBuf *y, const VkcAres *p) {
+ K.kind = PK_ARES;
+ if (!K.mpipe[PM_ARES] || p->S < 0 || p->D < 1 || p->nb < 0 || p->nb > 15 || (p->nb > 0 && !blk) || y == x || y == blk) return 0;
+ if (p->S == 0) return open_frame() && !K.lost;
+ struct { int mode; VkcAres b; } pc = {0, *p};
+ VkcBind bd[4] = {B(x, 0), B(p->nb > 0 ? blk : NULL, 0), B(prm, 0), B(y, 1)};
+ uint32_t gx, gy; grid((uint64_t)p->S, &gx, &gy);
+ return record(K.mpipe[PM_ARES], bd, 4, &pc, sizeof pc, gx, gy, 1);
+}
+int vkc_situ(VkcBuf *g, VkcBuf *u, VkcBuf *y, const VkcSitu *p) {
+ K.kind = PK_ARES;
+ if (!K.mpipe[PM_ARES] || p->n < 0) return 0;
+ if (p->n == 0) return open_frame() && !K.lost;
+ int32_t pc[13] = {1, p->n, 0, 0, p->g_off, 0, p->u_off, 0, 0, p->y_off, 0, 0, 0};
+ memcpy(&pc[11], &p->b1, 4); memcpy(&pc[12], &p->b2, 4);
+ VkcBind bd[4] = {B(g, 0), B(u, 0), B(NULL, 0), B(y, 1)};
+ uint32_t gx, gy; grid(((uint64_t)p->n + 255) / 256, &gx, &gy);
+ return record(K.mpipe[PM_ARES], bd, 4, pc, sizeof pc, gx, gy, 1);
+}
+
int vkc_mla_scratch(VkcMlaScratch *s, const VkcMla *m, int rows) {
if (s->rows >= rows && s->q) return 1;
size_t r = (size_t)rows, H = (size_t)m->H;
diff --git a/c/vk_chain.h b/c/vk_chain.h
index 784b3d2b..018d8e52 100644
--- a/c/vk_chain.h
+++ b/c/vk_chain.h
@@ -27,8 +27,8 @@
* chain_ew, chain_qsa, chain_ple. Offsets and strides are in floats.
* - multi-head latent attention: the MLA ops and the layer op below (chain_mla,
* chain_hgemv, chain_dsa, its k-pooled modes included), Kimi Delta Attention
- * (chain_kda) and manifold-constrained hyper-connections (chain_mhc): loaded beside
- * the others but optional.
+ * (chain_kda), manifold-constrained hyper-connections (chain_mhc) and Kimi K3's
+ * attention residuals and SiTU-GLU (chain_ares): loaded beside the others but optional.
*
* Threading: the engine thread only (the main queue is the backend's, used from the
* same thread by coli_vk_matmul; the expert tier submits on its own queue).
@@ -257,13 +257,22 @@ int vkc_mla(const VkcMla *m, VkcMlaScratch *s, VkcBuf *x, size_t x_off, int S, i
* the raw decay, beta and output-gate projections; prm at prm_off: A_log[H], dt[P],
* norm[VD]; alpha = exp(lb * sigmoid(exp(A_log) * (f + dt))), beta = sigmoid(b), q and k
* l2-normalized with neps inside the root; y = RMSNorm(o, eps) * norm * sigmoid(g).
- * The state [H][KD][VD] at st_off. */
+ * The state [H][KD][VD] at st_off.
+ * vkc_kda_rec_flags: the same with flags (0 is vkc_kda_rec, GLM-5.3's arithmetic);
+ * Kimi K3's (kimi_k3.c's kda_forward) sets both:
+ * VKC_KDA_EXP_A prm holds exp(A_log) itself, used as it is
+ * VKC_KDA_K3 the l2 sums take neps after the squares, q is normalized, then
+ * scaled, and the update is k * ((v - mem) * beta) */
int vkc_kda_ready(void);
typedef struct { int S, C, K, P, in_off, in_row, in_part, out_off, out_row, w_off, win_off; } VkcKdaConv;
int vkc_kda_conv(VkcBuf *in, VkcBuf *w, VkcBuf *win, VkcBuf *out, const VkcKdaConv *p);
+#define VKC_KDA_EXP_A 1
+#define VKC_KDA_K3 2
typedef struct { int S, H, VD, P, m_off, m_row, f_off, f_row, b_off, b_row, g_off, g_row, y_off, y_row, st_off, prm_off;
float lb, neps, eps; } VkcKdaRec;
int vkc_kda_rec(int KD, VkcBuf *m, VkcBuf *f, VkcBuf *b, VkcBuf *g, VkcBuf *prm, VkcBuf *st, VkcBuf *y, const VkcKdaRec *p);
+int vkc_kda_rec_flags(int KD, VkcBuf *m, VkcBuf *f, VkcBuf *b, VkcBuf *g, VkcBuf *prm, VkcBuf *st, VkcBuf *y,
+ const VkcKdaRec *p, int flags);
/* ---- manifold-constrained hyper-connections (chain_mhc.comp) ------------------------
* hyper_connections.h for S rows of H <= 8 streams of D floats ([S][H*D] at x_off,
@@ -286,6 +295,19 @@ typedef struct { int S, H, D, iters, x_off, x_row, m_off, m_row, hp_off, hp_row,
float eps, hc_eps, lim; } VkcMhc;
int vkc_mhc(int mode, VkcBuf *x, VkcBuf *m, VkcBuf *hp, VkcBuf *prm, VkcBuf *y, const VkcMhc *p);
+/* ---- attention residuals and SiTU-GLU (chain_ares.comp) ----------------------------
+ * Kimi K3's residual stream (AttnRes) and its activation, for S rows:
+ * vkc_ares_mix kimi_k3.c's res_mix: row s mixes the nb block snapshots ([nb][D] at
+ * b_off + s*b_row) and the running prefix (x_off + s*x_row) by the
+ * softmax of (v . w) / sqrt(mean(v^2) + eps), w at w_off in prm, into
+ * y_off + s*y_row; nb <= 15, y apart from x and blk
+ * vkc_situ y[i] = b1*tanh(g/b1)*sigmoid(g)*b2*tanh(u/b2), i < n, in the CPU's order */
+int vkc_ares_ready(void);
+typedef struct { int S, D, nb, x_off, x_row, b_off, b_row, w_off, y_off, y_row; float eps; } VkcAres;
+int vkc_ares_mix(VkcBuf *x, VkcBuf *blk, VkcBuf *prm, VkcBuf *y, const VkcAres *p);
+typedef struct { int n, g_off, u_off, y_off; float b1, b2; } VkcSitu;
+int vkc_situ(VkcBuf *g, VkcBuf *u, VkcBuf *y, const VkcSitu *p);
+
/* counters, for the engines' [VK] lines */
typedef struct {
unsigned long long frames, waits, ops, matmuls, gemms, barriers, bytes_up, bytes_down;