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

glm53 vulkan: clamp SiLU*Up in the shared gate_up shader

Gyanu Mayank9f9cc05a2f32
diff --git a/c/backend_vulkan.c b/c/backend_vulkan.c
index e9a8185b..61ef9b29 100644
--- a/c/backend_vulkan.c
+++ b/c/backend_vulkan.c
@@ -105,6 +105,15 @@ static struct {
} G;
struct PC { int fmt, S, I, O, rowWords, gs; };
+/* gate_up shader only: extra SwiGLU clamp. limit<=0 keeps the unclamped path
+ * (GLM-5.2 / colibri.c). Must ride per dispatch: the same pipeline serves
+ * callers whose correct value is "no clamp". */
+struct PCGU { int fmt, S, I, O, rowWords, gs; float limit; int pad; };
+static float g_swiglu_limit = 0.f;
+void coli_vk_set_swiglu_limit(float limit) { g_swiglu_limit = limit > 0.f ? limit : 0.f; }
+static struct PCGU pcgu(int fmt, int S, int I, int O, int rowWords, int gs) {
+ return (struct PCGU){fmt, S, I, O, rowWords, gs, g_swiglu_limit, 0};
+}
struct PCN { int S, D; float eps; };
/* Push constants of the absorb attention kernel (must match attention_absorb.comp). */
struct PCAttn { int fmt, S, H, Q, R, V, K, st0, T, rowWords, cap; float scale; int gs; };
@@ -400,7 +409,7 @@ int coli_vk_init(const char *spv_path) {
* (single-matmul path keeps working). */
char gu_path[512]; derive_sibling(spv_path, "_gate_up.spv", gu_path, sizeof(gu_path));
G.shader_gu = load_spv(G.dev, gu_path);
- if (G.shader_gu && !build_pipeline(G.dev, 6, sizeof(struct PC), G.shader_gu, &G.dsl_gu, &G.plyt_gu, &G.pipe_gu, &G.dpool_gu, &G.dset_gu))
+ if (G.shader_gu && !build_pipeline(G.dev, 6, sizeof(struct PCGU), G.shader_gu, &G.dsl_gu, &G.plyt_gu, &G.pipe_gu, &G.dpool_gu, &G.dset_gu))
return 0;
/* Optional MLA absorb attention pipeline (same directory as the main shader). */
@@ -684,7 +693,7 @@ int coli_vk_gate_up(ColiVkTensor **gate, ColiVkTensor **up, float *hidden, const
VKCHECK(vkBeginCommandBuffer(G.cmd, &begin), "beginCmd");
vkCmdBindPipeline(G.cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G.pipe_gu);
vkCmdBindDescriptorSets(G.cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G.plyt_gu, 0, 1, &G.dset_gu, 0, NULL);
- struct PC pc = {fmt, S, D, I, tg->rowWords, tg->gs}; // PC.I = input D, PC.O = moe_inter I
+ struct PCGU pc = pcgu(fmt, S, D, I, tg->rowWords, tg->gs); // PC.I = input D, PC.O = moe_inter I
vkCmdPushConstants(G.cmd, G.plyt_gu, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
vkCmdDispatch(G.cmd, (uint32_t)((I + 7) / 8), (uint32_t)S, 1);
VKCHECK(vkEndCommandBuffer(G.cmd), "endCmd");
@@ -776,7 +785,7 @@ static int eg_prepare_submit(ColiVkTensor *const *gates, ColiVkTensor *const *up
/* phase 1: fused gate+up+silu -> hidden (per expert, bound to its x/hidden slices) */
vkCmdBindPipeline(G.eg_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G.pipe_gu);
for (int c = 0; c < count; c++) {
- struct PC pc = {fmt, rows[c], D, I, gates[c]->rowWords, gates[c]->gs};
+ struct PCGU pc = pcgu(fmt, rows[c], D, I, gates[c]->rowWords, gates[c]->gs);
vkCmdBindDescriptorSets(G.eg_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G.plyt_gu, 0, 1, &G.eg_gu[c], 0, NULL);
vkCmdPushConstants(G.eg_cmd, G.plyt_gu, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
vkCmdDispatch(G.eg_cmd, (uint32_t)((I + 7) / 8), (uint32_t)rows[c], 1);
@@ -1017,7 +1026,7 @@ int coli_vk_init_dev2(const char *spv_path, int devidx) {
if (!G2.sh_gu) { fprintf(stderr, "[VK] dev2: gate_up shader required for the tier\n"); return 0; }
VkDescriptorPool dp; VkDescriptorSet ds; /* build_pipeline's singleton set: unused here */
if (!build_pipeline(G2.dev, 4, sizeof(struct PC), G2.sh_qmm, &G2.dsl, &G2.plyt, &G2.pipe, &dp, &ds)) return 0;
- if (!build_pipeline(G2.dev, 6, sizeof(struct PC), G2.sh_gu, &G2.dsl_gu, &G2.plyt_gu, &G2.pipe_gu, &dp, &ds)) return 0;
+ if (!build_pipeline(G2.dev, 6, sizeof(struct PCGU), G2.sh_gu, &G2.dsl_gu, &G2.plyt_gu, &G2.pipe_gu, &dp, &ds)) return 0;
VkCommandPoolCreateInfo cpci = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, .queueFamilyIndex = G2.qfam};
VKCHECK(vkCreateCommandPool(G2.dev, &cpci, NULL, &G2.cpool), "d2 cmdPool");
@@ -1118,7 +1127,7 @@ static int eg2_prepare_submit(ColiVkTensor *const *gates, ColiVkTensor *const *u
VkMemoryBarrier mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, .dstAccessMask = VK_ACCESS_SHADER_READ_BIT};
vkCmdBindPipeline(G2.cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G2.pipe_gu);
for (int c = 0; c < count; c++) {
- struct PC pc = {fmt, rows[c], D, I, gates[c]->rowWords, gates[c]->gs};
+ struct PCGU pc = pcgu(fmt, rows[c], D, I, gates[c]->rowWords, gates[c]->gs);
vkCmdBindDescriptorSets(G2.cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G2.plyt_gu, 0, 1, &G2.gu[c], 0, NULL);
vkCmdPushConstants(G2.cmd, G2.plyt_gu, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
vkCmdDispatch(G2.cmd, (uint32_t)((I + 7) / 8), (uint32_t)rows[c], 1);
@@ -1758,7 +1767,7 @@ static double bench_gu_batched(ColiVkTensor *tg, const float *x, int fmt, int S,
vkBeginCommandBuffer(G.cmd, &begin);
vkCmdBindPipeline(G.cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G.pipe_gu);
vkCmdBindDescriptorSets(G.cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G.plyt_gu, 0, 1, &G.dset_gu, 0, NULL);
- struct PC pc = {fmt, S, D, I, tg->rowWords, tg->gs};
+ struct PCGU pc = pcgu(fmt, S, D, I, tg->rowWords, tg->gs);
vkCmdPushConstants(G.cmd, G.plyt_gu, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
VkMemoryBarrier mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, .dstAccessMask = VK_ACCESS_SHADER_READ_BIT};
@@ -1806,7 +1815,7 @@ static double bench_experts_fair(int fmt, int D, int I, int K, int Npass) {
VkCommandBufferBeginInfo begin = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
vkBeginCommandBuffer(G.cmd, &begin);
vkCmdBindPipeline(G.cmd, VK_PIPELINE_BIND_POINT_COMPUTE, G.pipe_gu);
- struct PC pc = {fmt, 1, D, I, tg[0]->rowWords, tg[0]->gs};
+ struct PCGU pc = pcgu(fmt, 1, D, I, tg[0]->rowWords, tg[0]->gs);
vkCmdPushConstants(G.cmd, G.plyt_gu, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
VkMemoryBarrier mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, .dstAccessMask = VK_ACCESS_SHADER_READ_BIT};
for (int pass = 0; pass < Npass; pass++) for (int c = 0; c < K; c++) {
diff --git a/c/backend_vulkan.h b/c/backend_vulkan.h
index 60673b10..e673b37d 100644
--- a/c/backend_vulkan.h
+++ b/c/backend_vulkan.h
@@ -20,6 +20,8 @@ int coli_vk_init(const char *spv_path);
void coli_vk_shutdown(void);
int coli_vk_available(void);
void coli_vk_mem_info(size_t *used_bytes, size_t *tensor_count);
+/* GLM-5.3 SwiGLU clamp for the fused gate_up kernel. 0 disables (GLM-5.2). */
+void coli_vk_set_swiglu_limit(float limit);
/* VRAM pressure-proofing (both no-ops when the extension is absent):
* alloc_priority sets the eviction-priority class of SUBSEQUENT weight uploads
diff --git a/c/glm53.c b/c/glm53.c
index 6f5ddc69..64388463 100644
--- a/c/glm53.c
+++ b/c/glm53.c
@@ -2150,7 +2150,8 @@ static void model_load_range(GModel *m, const char *dir, int layer_begin,
if (given && strstr(given, ".spv")) snprintf(spv, sizeof(spv), "%s", given);
else snprintf(spv, sizeof(spv), "%s/qmatmul.spv", given ? given : "shaders");
g_vk_ready = coli_vk_init(spv) && coli_vk_available();
- fprintf(stderr, g_vk_ready
+ if (g_vk_ready) coli_vk_set_swiglu_limit(m->c.swiglu_limit);
+ fprintf(stderr, g_vk_ready)
? "Vulkan: active for resident matrices\n"
: "Vulkan: no usable device (%s), falling back to CPU\n", spv);
}
diff --git a/c/shaders/qmatmul_gate_up.comp b/c/shaders/qmatmul_gate_up.comp
index 8db58d04..c69ee818 100644
--- a/c/shaders/qmatmul_gate_up.comp
+++ b/c/shaders/qmatmul_gate_up.comp
@@ -25,6 +25,7 @@ layout(push_constant) uniform P {
int S, I, O; // I = input dim (hidden), O = moe_inter
int rowWords; // uint32 words per weight row (of the I-dim rows)
int gs; // fmt=4 group size (multiple of 8; host gates otherwise)
+ float limit; // SwiGLU clamp; 0 = unclamped (GLM-5.2)
} p;
shared float xsh[6144];
@@ -99,6 +100,11 @@ void main() {
}
float gt = subgroupAdd(g), ut = subgroupAdd(u);
if (p.fmt != 5 && p.fmt != 4) { gt *= gscale[o]; ut *= uscale[o]; } // per-row formats only
+ if (p.limit > 0.0) {
+ if (gt > p.limit) gt = p.limit;
+ if (ut > p.limit) ut = p.limit;
+ if (ut < -p.limit) ut = -p.limit;
+ }
if (lane == 0) hid[s * p.O + o] = (gt / (1.0 + exp(-gt))) * ut; // silu(gate)*up
}
}
diff --git a/c/tests/test_glm53_vulkan_clamped_source.py b/c/tests/test_glm53_vulkan_clamped_source.py
new file mode 100644
index 00000000..f56443a3
--- /dev/null
+++ b/c/tests/test_glm53_vulkan_clamped_source.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+"""Source contract: Vulkan gate_up SwiGLU can clamp like the CPU glm53 path."""
+from pathlib import Path
+import unittest
+
+ROOT = Path(__file__).resolve().parents[1]
+SHADER = (ROOT / "shaders" / "qmatmul_gate_up.comp").read_text()
+SRC = (ROOT / "backend_vulkan.c").read_text()
+HDR = (ROOT / "backend_vulkan.h").read_text()
+GLM = (ROOT / "glm53.c").read_text()
+
+
+class Glm53VulkanClampedSourceTests(unittest.TestCase):
+ def test_shader_has_limit_and_clamp(self):
+ self.assertIn("float limit;", SHADER)
+ self.assertIn("if (p.limit > 0.0)", SHADER)
+ self.assertIn("if (gt > p.limit) gt = p.limit;", SHADER)
+ self.assertIn("if (ut < -p.limit) ut = -p.limit;", SHADER)
+
+ def test_host_push_constant_carries_limit(self):
+ self.assertIn("struct PCGU", SRC)
+ self.assertIn("coli_vk_set_swiglu_limit", HDR)
+ self.assertIn("sizeof(struct PCGU)", SRC)
+
+ def test_glm53_publishes_its_checkpoint_limit(self):
+ self.assertIn("coli_vk_set_swiglu_limit(m->c.swiglu_limit)", GLM)
+
+ def test_zero_limit_keeps_unclamped_path(self):
+ self.assertIn("g_swiglu_limit = limit > 0.f ? limit : 0.f", SRC)
+
+
+if __name__ == "__main__":
+ unittest.main()