Skip to content

armgit / janas

Public Imported from GitHub
0

37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.

7.0 KB
Raw Blame
/* SPDX-License-Identifier: GPL-3.0-or-later
   Copyright (C) 2026 Maurizio Cammalleri */
#version 450
// The fast prompt for a mixture's Q3_K experts (gpu.c experts_id, and a
// mixture's layers whole on the GPU): every expert of a group in one
// dispatch, as q4k_mmid.comp - the workgroup's z the expert, its weights,
// activations and outputs from a table (P.t), tiles of 64 rows by 16
// vectors, each invocation 2 rows by 4 - with the weights read as
// q3k_mmq.comp reads them: a group of 32 is two of 16 with a 6-bit scale
// each (less 32), a weight's three bits two of qs and one of hmask, put
// together unsigned (0-7) from the 110-byte blocks read a word at a time
// and realigned, the -4 taken off with the activations' sums of 16. Until
// 21 Oct 2026 these experts stayed on the CPU: unsloth's 3-bit
// Qwen3-30B-A3B read a prompt at 98 tokens/s where llama.cpp read 114. NOT
// bit for bit with the CPU: rounded group by group.
#extension GL_EXT_control_flow_attributes : require
#extension GL_EXT_integer_dot_product : require
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_buffer_reference_uvec2 : require

layout(local_size_x = 128) in;
layout(constant_id = 0) const uint NV = 8; // unused: the table's constants
layout(constant_id = 1) const uint SG = 8;

const uint BM = 64u, BN = 16u, KS = 4u, XB = 20u;

layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };

// t: the table, 3 uvec4 an expert: (w, x), (y, nv, ys), (p1, p0); then
// from uvec4 tiles on, one a workgroup's z: (expert, first vector)
layout(push_constant) uniform P { uvec2 t; uint rows, nb, tiles; } p;

struct ACache { uint qs[8]; vec2 dsc; }; // a group: three-bit values, d * scale of each 16
struct BCache { int qs[8]; vec2 d8s; float d8; }; // quants, 4 x the sums of 16, d8
shared ACache sa[BM * KS];
shared BCache sb[BN * KS];

// word i (0-27) of the block at byte off from w, realigned
uint hword(U1 W, uint off, uint i)
{
    uint a = (off >> 2) + i;
    return (off & 2u) != 0u ? (W.v[a] >> 16) | (W.v[a + 1u] << 16) : W.v[a];
}

void main()
{
    U4 T = U4(p.t);
    uvec4 tl = T.v[p.tiles + gl_WorkGroupID.z];
    uint e = tl.x;
    uvec4 t0 = T.v[3u * e], t1 = T.v[3u * e + 1u];
    uint nv = t1.z, ys = t1.w;
    uint r0 = gl_WorkGroupID.x * BM, v0 = tl.y;
    U1 W = U1(t0.xy);
    U4 X = U4(t0.zw);
    F Y = F(t1.xy);
    uint id = gl_LocalInvocationID.x;
    uint rq = (id % 32u) * 2u, vq = (id / 32u) * 4u;
    float sums[8];
    [[unroll]] for (uint i = 0; i < 8u; i++) sums[i] = 0.0;
    uint ngroups = p.nb * 8u;
    for (uint g0 = 0; g0 < ngroups; g0 += KS) {
        // weights: 4 invocations a row, 2 words each of the group's 8
        {
            uint iqs = id % 4u, rl = id / 4u;
            [[unroll]] for (uint l = 0; l < BM; l += 32u) {
                uint r = min(r0 + rl + l, p.rows - 1u);
                // the 4 groups staged (j = 0-3 of one n) share their words
                // of qs (two bits each, shifted by 2j) and of hmask (bit
                // g = 4n + j): each read once
                uint b = g0 / 8u, gb = g0 % 8u, n = gb / 4u; // gb: in the block
                uint off = (r * p.nb + b) * 110u;
                [[unroll]] for (uint t = 0; t < 2u; t++) {
                    uint kw = 2u * iqs + t;
                    uint q = hword(W, off, 8u + 8u * n + kw), hm = hword(W, off, kw);
                    [[unroll]] for (uint k = 0; k < KS; k++)
                        sa[k * BM + rl + l].qs[kw] = ((q >> (2u * k)) & 0x03030303u) |
                                                     (((hm >> (gb + k)) & 0x01010101u) << 2);
                }
                if (iqs == 0u) {
                    float d = unpackHalf2x16(hword(W, off, 27u) & 0xffffu).x;
                    uint s0 = hword(W, off, 24u), s1 = hword(W, off, 25u), s2 = hword(W, off, 26u);
                    [[unroll]] for (uint k = 0; k < KS; k++) {
                        // scales 2g and 2g + 1: the low four bits from byte
                        // i % 8 (its low nibble below 8, high above), the top
                        // two from byte 8 + i % 4 at bits 2 (i / 4)
                        float sc[2];
                        [[unroll]] for (uint h = 0; h < 2u; h++) {
                            uint i = 2u * (gb + k) + h, lb = i % 8u;
                            uint lo = ((lb < 4u ? s0 : s1) >> (8u * (lb % 4u))) & 0xffu;
                            lo = i < 8u ? lo & 15u : lo >> 4;
                            uint hi = (s2 >> (8u * (i % 4u) + 2u * (i / 4u))) & 3u;
                            sc[h] = float(int(lo | (hi << 4)) - 32);
                        }
                        sa[k * BM + rl + l].dsc = vec2(d * sc[0], d * sc[1]);
                    }
                }
            }
        }
        // activations: 2 invocations a vector and group, each half its quants
        {
            uint k = id / 32u, c = (id % 32u) / 2u, iqs = id % 2u;
            uint v = min(v0 + c, nv - 1u);
            uint g = g0 + k, b = g / 8u, G = g % 8u;
            uint xo = (v * p.nb + b) * XB;
            uvec4 q = X.v[xo + 2u * G + iqs];
            sb[k * BN + c].qs[4u * iqs] = int(q.x);
            sb[k * BN + c].qs[4u * iqs + 1u] = int(q.y);
            sb[k * BN + c].qs[4u * iqs + 2u] = int(q.z);
            sb[k * BN + c].qs[4u * iqs + 3u] = int(q.w);
            if (iqs == 0u) {
                uvec4 bs = X.v[xo + 16u + G / 4u];
                uint bw = G % 4u == 0u ? bs.x : G % 4u == 1u ? bs.y : G % 4u == 2u ? bs.z : bs.w;
                sb[k * BN + c].d8s = vec2(4.0 * float(bitfieldExtract(int(bw), 0, 16)),
                                          4.0 * float(bitfieldExtract(int(bw), 16, 16)));
                sb[k * BN + c].d8 = uintBitsToFloat(X.v[xo + 18u].x);
            }
        }
        barrier();
        for (uint k = 0; k < KS; k++) {
            ACache ca[2];
            [[unroll]] for (uint r = 0; r < 2u; r++)
                ca[r] = sa[k * BM + rq + r];
            [[unroll]] for (uint c = 0; c < 4u; c++) {
                BCache cb = sb[k * BN + vq + c];
                [[unroll]] for (uint r = 0; r < 2u; r++) {
                    int q0 = 0, q1 = 0;
                    [[unroll]] for (uint i = 0; i < 4u; i++) {
                        q0 += dotPacked4x8EXT(int(ca[r].qs[i]), cb.qs[i]);
                        q1 += dotPacked4x8EXT(int(ca[r].qs[i + 4u]), cb.qs[i + 4u]);
                    }
                    sums[c * 2u + r] += cb.d8 * (ca[r].dsc.x * (float(q0) - cb.d8s.x) +
                                                 ca[r].dsc.y * (float(q1) - cb.d8s.y));
                }
            }
        }
        barrier();
    }
    [[unroll]] for (uint c = 0; c < 4u; c++)
        [[unroll]] for (uint r = 0; r < 2u; r++) {
            uint row = r0 + rq + r, v = v0 + vq + c;
            if (row < p.rows && v < nv)
                Y.v[v * ys + row] = sums[c * 2u + r];
        }
}