Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
#version 450
// The expert batch's grouped GEMV (backend_vulkan.c, xb_issue below the grouped GEMM's
// rows: decode, an MTP verify): ONE dispatch covers every expert of a batch, each
// workgroup one (expert, row, RPW outputs) item with the expert's weights read through
// qmatmul_grp.comp's address table. The same bindings (but binding 4) and expert entries.
// y[r,o] = sum_i x[r,i] * dequant(W[o,i]) (default)
// hid[r,o] = silu(gate[r,o]) * up[r,o], clamped as expert_act.comp's act 0 (-DGATE_UP)
// x is staged in shared memory once per workgroup; each row's 16-byte weight words are
// spread over lpr lanes (a power of two up to 64), RPS rows in flight per lane group,
// partial sums joined with clustered subgroup adds. A row's arithmetic does not depend
// on the batch around it.
// Host: I % 32 == 0, I <= 8192, rowWords % 4 == 0; fmt 4: gs % 32 == 0.
#extension GL_KHR_shader_subgroup_arithmetic : require
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_clustered : require
#extension GL_EXT_control_flow_attributes : enable
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
layout(local_size_x = 256) in;
layout(constant_id = 0) const uint UNUSED = 0;
layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer WRef4 { uvec4 v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer SRef { float v[]; };
layout(std430, binding = 0) readonly buffer X { vec4 x4[]; };
layout(std430, binding = 1) readonly buffer IT { uvec4 items[]; };
layout(std430, binding = 2) readonly buffer ET { uint etab[]; };
layout(std430, binding = 3) writeonly buffer Y { float y[]; };
#ifdef GATE_UP
layout(std430, binding = 4) readonly buffer AM { int amap[]; }; // kgat > 0: as qmatmul_grp.comp's
#endif
layout(push_constant) uniform P { int I, O; float limit; int ibase; int kgat; int rpw; } p;
#ifndef RPW
#define RPW 64u
#endif
#ifndef RPS
#define RPS 4u
#endif
#ifndef XSHN
#define XSHN 2048
#endif
#define MAXNR 1u
shared vec4 xsh[XSHN];
float i8(uint w, uint k) { return float(int(w << (24u - 8u * k)) >> 24); }
float d8(uint w, vec4 a) {
return a.x * i8(w, 0u) + a.y * i8(w, 1u) + a.z * i8(w, 2u) + a.w * i8(w, 3u);
}
float d4(uint w, vec4 a, vec4 b) {
return a.x * float(int(w & 15u) - 8) + a.y * float(int((w >> 4u) & 15u) - 8) +
a.z * float(int((w >> 8u) & 15u) - 8) + a.w * float(int((w >> 12u) & 15u) - 8) +
b.x * float(int((w >> 16u) & 15u) - 8) + b.y * float(int((w >> 20u) & 15u) - 8) +
b.z * float(int((w >> 24u) & 15u) - 8) + b.w * float(int(w >> 28u) - 8);
}
float red(float v, uint lpr) {
if (lpr == gl_SubgroupSize) return subgroupAdd(v);
if (lpr == 64u) return subgroupClusteredAdd(v, 64u);
if (lpr == 32u) return subgroupClusteredAdd(v, 32u);
if (lpr == 16u) return subgroupClusteredAdd(v, 16u);
if (lpr == 8u) return subgroupClusteredAdd(v, 8u);
if (lpr == 4u) return subgroupClusteredAdd(v, 4u);
if (lpr == 2u) return subgroupClusteredAdd(v, 2u);
return v;
}
// Called by each lane in the current word iteration, including padding rows.
// Pair sharing is enabled only
// for complete pairs of words: both lanes then have the same row, validity,
// scale index and loop participation. Odd W4 (a final half-group) uses the
// ordinary load throughout. The collective is outside the even-lane load branch.
float group_scale(SRef sc, uint o, uint ng, uint j, uint gs, bool ok, bool pair_scale) {
return ok ? sc.v[o * ng + (j * 32u) / gs] : 0.0;
}
// One matrix, one RPS block of output rows. The word-major inner loop shares each
// input pair across the RPS rows.
void matrix_block(WRef4 w, SRef sc, int fmt, uint rowWords, uint gs, uint ng,
uint o0, uint qbase, uint remaining, uint RW, uint rg,
uint jj, uint lpr, uint W4, uint nr,
out float acc[RPS * MAXNR]) {
uint I4 = uint(p.I) / 4u;
[[unroll]] for (uint a = 0u; a < RPS * MAXNR; a++) acc[a] = 0.0;
bool pair_scale = gs == 64u && lpr >= 2u && (W4 & 1u) == 0u;
for (uint j = jj; j < W4; j += lpr) {
uvec4 v[RPS];
float s[RPS];
[[unroll]] for (uint t = 0u; t < RPS; t++) {
uint q = qbase + t * rg, o = o0 + q;
bool ok = t * rg < remaining && q < RW && o < uint(p.O);
v[t] = ok ? w.v[o * (rowWords / 4u) + j] : uvec4(0u);
}
[[unroll]] for (uint t = 0u; t < RPS; t++) {
uint q = qbase + t * rg, o = o0 + q;
bool ok = t * rg < remaining && q < RW && o < uint(p.O);
s[t] = 1.0;
if (fmt == 4) s[t] = group_scale(sc, o, ng, j, gs, ok, pair_scale);
}
[[unroll]] for (uint k = 0u; k < MAXNR; k++) if (k < nr) {
float d[RPS];
[[unroll]] for (uint t = 0u; t < RPS; t++) d[t] = 0.0;
if (fmt == 1) {
uint x0 = k * I4 + j * 4u;
[[unroll]] for (uint h = 0u; h < 4u; h++) {
vec4 a = xsh[x0 + h];
[[unroll]] for (uint t = 0u; t < RPS; t++) {
float z = d8(v[t][h], a);
d[t] = h == 0u ? z : d[t] + z;
}
}
} else {
uint x0 = k * I4 + j * 8u;
[[unroll]] for (uint h = 0u; h < 4u; h++) {
vec4 a = xsh[x0 + h * 2u], b = xsh[x0 + h * 2u + 1u];
[[unroll]] for (uint t = 0u; t < RPS; t++) {
float z = d4(v[t][h], a, b);
d[t] = h == 0u ? z : d[t] + z;
}
}
}
[[unroll]] for (uint t = 0u; t < RPS; t++) acc[t * MAXNR + k] += s[t] * d[t];
}
}
[[unroll]] for (uint t = 0u; t < RPS; t++) {
uint q = qbase + t * rg, o = o0 + q;
bool writer = jj == 0u && t * rg < remaining && q < RW && o < uint(p.O);
// One per-row scale load, only in the lane which writes the result.
float s = 1.0;
if (writer && fmt != 4) s = sc.v[o];
[[unroll]] for (uint k = 0u; k < MAXNR; k++) if (k < nr)
acc[t * MAXNR + k] = red(acc[t * MAXNR + k], lpr) * s;
}
}
void main() {
uvec4 it = items[uint(p.ibase) + gl_WorkGroupID.x];
uint eb = it.x * 16u;
WRef4 wg = WRef4(packUint2x32(uvec2(etab[eb], etab[eb + 1u])));
SRef gsc = SRef(packUint2x32(uvec2(etab[eb + 2u], etab[eb + 3u])));
#ifdef GATE_UP
WRef4 wu = WRef4(packUint2x32(uvec2(etab[eb + 4u], etab[eb + 5u])));
SRef usc = SRef(packUint2x32(uvec2(etab[eb + 6u], etab[eb + 7u])));
#endif
uint xoff = etab[eb + 9u], rowWords = etab[eb + 11u];
int fmt = int(etab[eb + 10u]);
uint gs = max(etab[eb + 12u], 1u), I = uint(p.I), I4 = I / 4u;
uint nr = 1u;
for (uint k = 0u; k < nr; k++) {
uint xr = xoff + it.y + k;
#ifdef GATE_UP
if (p.kgat > 0) xr = uint(amap[xr]) / uint(p.kgat);
#endif
for (uint i = gl_LocalInvocationID.x; i < I4; i += 256u)
xsh[k * I4 + i] = x4[xr * I4 + i];
}
barrier();
uint W4 = fmt == 1 ? I / 16u : I / 32u;
uint cap = min(min(gl_SubgroupSize, W4), 64u);
// Cluster sizes must be powers of two, even for non-power-of-two I.
// I=512 int4 keeps 16 adjacent lanes per row (four rows per wave64).
uint lpr = 1u;
while (lpr * 2u <= cap) lpr *= 2u;
uint rg = gl_SubgroupSize / lpr, rr = gl_SubgroupInvocationID / lpr;
uint jj = gl_SubgroupInvocationID % lpr;
uint RW = p.rpw > 0 ? uint(p.rpw) : RPW;
uint rows_sg = (RW + gl_NumSubgroups - 1u) / gl_NumSubgroups;
uint ng = (I + gs - 1u) / gs;
for (uint base = 0u; base < rows_sg; base += RPS * rg) {
uint qbase = gl_SubgroupID * rows_sg + base + rr;
// Saturate before subtracting: the final cluster may lie past rows_sg.
uint remaining = rows_sg - min(rows_sg, base + rr);
float ag[RPS * MAXNR];
matrix_block(wg, gsc, fmt, rowWords, gs, ng, it.z, qbase, remaining,
RW, rg, jj, lpr, W4, nr, ag);
#ifdef GATE_UP
float au[RPS * MAXNR];
matrix_block(wu, usc, fmt, rowWords, gs, ng, it.z, qbase, remaining,
RW, rg, jj, lpr, W4, nr, au);
#endif
[[unroll]] for (uint t = 0u; t < RPS; t++) {
uint q = qbase + t * rg, o = it.z + q;
if (jj == 0u && t * rg < remaining && q < RW && o < uint(p.O)) {
[[unroll]] for (uint k = 0u; k < MAXNR; k++) if (k < nr) {
float g = ag[t * MAXNR + k];
#ifdef GATE_UP
float u = au[t * MAXNR + k];
if (p.limit > 0.0) { g = min(g, p.limit); u = clamp(u, -p.limit, p.limit); }
y[(xoff + it.y + k) * uint(p.O) + o] = (g / (1.0 + exp(-g))) * u;
#else
y[(xoff + it.y + k) * uint(p.O) + o] = g;
#endif
}
}
}
}
}