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

qwenimage chain: the attention in slices of rows, a submission each

JustVugg095af8950ee0
diff --git a/c/qwenimage_chain.h b/c/qwenimage_chain.h
index 3de8b3af..6cb62a3c 100644
--- a/c/qwenimage_chain.h
+++ b/c/qwenimage_chain.h
@@ -126,6 +126,24 @@ static int qic_prefix(Dit *d, const Prefix *p) {
return 1;
}
+/* The image rows' attention, in slices of rows that end their submission (the chain's
+ * COLI_VK_ATTN_SLICE budget of rows x positions x width): a 1024x1024 step's attention
+ * is seconds a block on an integrated GPU, past a driver's job timeout (amdgpu 10 s). */
+static int qic_attn(const VkcEncAttn *at, int N, int L) {
+ long long b = vkc_attn_slice_budget(), per = (long long)(L + N) * at->H * at->hd;
+ int rr = b > 0 ? (int)(b / per / 16 * 16) : N;
+ if (rr < 16) rr = 16;
+ int ok = 1;
+ for (int r0 = 0; ok && r0 < N; r0 += rr) {
+ VkcEncAttn x = *at;
+ x.S = N - r0 < rr ? N - r0 : rr;
+ x.q_off = at->q_off + r0 * at->kv_row; x.o_off = at->o_off + r0 * at->o_row;
+ ok = vkc_enc_attn(g_qic.qkv, NULL, NULL, g_qic.a, g_qic.rng, NULL, NULL, &x) &&
+ (r0 + rr >= N || (vkc_submit(0) && vkc_begin()));
+ }
+ return ok;
+}
+
/* One denoising step on the device: out[N][in_ch]. 0: not taken (the CPU runs it). */
static int qic_forward(Dit *d, const Prefix *p, DitStep *s, const float *lat, float t, float *out) {
if (g_qic_on < 0) g_qic_on = g_vk_ready && vkc_init() &&
@@ -179,7 +197,7 @@ static int qic_forward(Dit *d, const Prefix *p, DitStep *s, const float *lat, fl
vkc_norm(g_qic.qkv, g_qic.prm, g_qic.qkv, &nq) && vkc_norm(g_qic.qkv, g_qic.prm, g_qic.qkv, &nk) &&
vkc_mla_rope(g_qic.qkv, g_qic.cs, g_qic.qkv, &rq) && vkc_mla_rope(g_qic.qkv, g_qic.cs, g_qic.qkv, &rk) &&
vkc_copy(g_qic.qkv, KO, g_qic.pk[l], 0, (size_t)L * D) && vkc_copy(g_qic.qkv, VO, g_qic.pv[l], 0, (size_t)L * D) &&
- vkc_enc_attn(g_qic.qkv, NULL, NULL, g_qic.a, g_qic.rng, NULL, NULL, &at) &&
+ qic_attn(&at, N, L) &&
vkc_matmul(qic_tensor(&B->o), g_qic.a, 0, g_qic.h, 0, N) &&
vkc_ew(g_qic.x, g_qic.x, g_qic.h, NULL, g_qic.mod, &g1) &&
vkc_enc_norm(g_qic.x, NULL, g_qic.mod, g_qic.h, &n2) &&
diff --git a/c/vk_chain.c b/c/vk_chain.c
index d0e151ea..a7c94037 100644
--- a/c/vk_chain.c
+++ b/c/vk_chain.c
@@ -895,6 +895,7 @@ static long long attn_slice_budget(void) {
long long v = e && *e ? atoll(e) : (1LL << 32);
return v < 0 ? 0 : v;
}
+long long vkc_attn_slice_budget(void) { return attn_slice_budget(); }
static int attn_slice_rows(int S, int pos_base, double width, int align) {
long long b = attn_slice_budget();
if (align < 1) align = 1;
diff --git a/c/vk_chain.h b/c/vk_chain.h
index 5071e547..a5c3c263 100644
--- a/c/vk_chain.h
+++ b/c/vk_chain.h
@@ -142,6 +142,7 @@ int vkc_attn_part_chunks(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, const Vk
typedef struct { int n, d, nz, a_off, a_z, sa_off, sa_z, o_off, so_off; } VkcKvsJoin;
int vkc_kvs_join(VkcBuf *parts, VkcBuf *out, const VkcKvsJoin *p);
int vkc_attn_flash_rows(void); /* COLI_VK_CHAIN_FLASH: rows from which chain_attn_flash runs (0 = never) */
+long long vkc_attn_slice_budget(void); /* COLI_VK_ATTN_SLICE below: rows x positions x width a submission (0 = never) */
/* The attention ops (vkc_attn, vkc_attn_w, vkc_mla_core, vkc_relattn) whose rows x positions
* x heads x head dim pass COLI_VK_ATTN_SLICE (2^32; 0 = never) record their rows in slices,
* each ending its frame (submitted, not waited for) and the next in a new one: no single