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
1.3 KB
Raw Blame
#version 450
// RMS norm over S rows of length D: y[s,i] = x[s,i] * w[i] / sqrt(mean(x[s,:]^2) + eps).
// Mirrors colibri.c rmsnorm() (fp32 accumulate here vs f64 on CPU: for the q-latent
// D=1536 the difference is ~1e-7 relative — far under the quantized-weight noise).
// One 256-thread workgroup per row; used to CHAIN q_a -> norm -> q_b inside one
// submit (coli_vk_attn_qprep) so the middle CPU roundtrip and its fence disappear.

layout(local_size_x = 256) in;

layout(std430, binding = 0) readonly  buffer X { float x[]; };
layout(std430, binding = 1) readonly  buffer W { float w[]; };
layout(std430, binding = 2) writeonly buffer Y { float y[]; };

layout(push_constant) uniform P {
    int S, D;
    float eps;
} p;

shared float red[256];

void main() {
    int s = int(gl_WorkGroupID.x);
    if (s >= p.S) return;
    int tid = int(gl_LocalInvocationID.x);
    uint base = uint(s) * uint(p.D);

    float acc = 0.0;
    for (int i = tid; i < p.D; i += 256) { float v = x[base + uint(i)]; acc += v * v; }
    red[tid] = acc;
    barrier();
    for (int off = 128; off > 0; off >>= 1) {
        if (tid < off) red[tid] += red[tid + off];
        barrier();
    }
    float r = 1.0 / sqrt(red[0] / float(p.D) + p.eps);
    for (int i = tid; i < p.D; i += 256) y[base + uint(i)] = x[base + uint(i)] * r * w[i];
}