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.
C avx2ccpu-inferenceggufinference-enginelinuxllmlocal-llmmixture-of-expertsmoenvmequantizationqwen3simdspeculative-decodingvulkan
/* SPDX-License-Identifier: GPL-3.0-or-later
Copyright (C) 2026 Maurizio Cammalleri */
#version 450
// The fast prompt for IQ4_NL (KIND 0), MXFP4 (1), Q5_0 (2) and Q2_0 (3):
// q80_mmq.comp's tiling and activations (blocks of 32), a group's weights
// its 16 bytes of nibbles through the sixteen signed levels of its table
// (weights 0-15 the low nibbles, 16-31 the high), its scale the block's d
// (IQ4_NL) or the power of two of its E8M0 byte (MXFP4, quant.c
// janas_e8m0_half). Q5_0: the nibbles and qh's fifth bits, q - 16; Q2_0:
// each half of a block of 64 a group, its 8 bytes of two-bit weights
// spread a byte each, q - 1, the block's d. A block is 18 bytes (IQ4_NL,
// Q2_0), 17 (MXFP4) or 22 (Q5_0), so its words are read realigned. A row
// may end inside a group of 256 (tail blocks of 32 past the whole groups:
// qwen4exp's widths of 640), the stages past its end empty. 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;
layout(constant_id = 2) const uint KIND = 0;
/* bytes a group of 32 weights (Q2_0: half a block) */
const uint S = KIND == 3u ? 9u : KIND == 2u ? 22u : KIND == 1u ? 17u : 18u;
const uint BM = 64u, BN = 64u, BK = 32u, KS = 4u;
const uint WM = 32u, WN = 32u, TM = 2u, TN = 2u, WARP = 32u;
const uint WNITER = (WM * WN) / (WARP * TM * TN); // 8
const uint WSUBN = WN / WNITER; // 4
const uint XB = 20u; // uvec4 an activations' block
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[]; };
// w: the first row (rows of nb * 8 + tail groups of S bytes); x: vector v,
// block b at uvec4 (v * xs + b) * 20; y: row r of vector v at y[v * ys + r]
layout(push_constant) uniform P {
uvec2 w, x, y;
uint rows, nb, nv, xs, ys, level;
uvec2 p1, p0;
uint tail; /* blocks of 32 past the whole groups */
} p;
struct ACache { int qs[8]; float d; }; // a group: its 32 levels, d
struct BCache { int qs[8]; float d; }; // a group: quants, its d32
shared ACache sa[BM * KS];
shared BCache sb[BN * KS];
// word i of the 32-bit words from byte off of w
uint bword(U1 W, uint off, uint i)
{
uint a = (off >> 2) + i, sh = 8u * (off & 3u);
return sh != 0u ? (W.v[a] >> sh) | (W.v[a + 1u] << (32u - sh)) : W.v[a];
}
const int IQ4[16] = int[16](-127, -104, -83, -65, -49, -35, -22, -10,
1, 13, 25, 38, 53, 69, 89, 113);
const int MX[16] = int[16](0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12);
int lvl(uint i) { return KIND == 1u ? MX[i] : IQ4[i]; }
/* each byte of w (a nibble) through the table */
uint iq4(uint w)
{
return (uint(lvl(w & 15u)) & 0xffu) | ((uint(lvl((w >> 8) & 15u)) & 0xffu) << 8) |
((uint(lvl((w >> 16) & 15u)) & 0xffu) << 16) |
((uint(lvl((w >> 24) & 15u)) & 0xffu) << 24);
}
/* four bits (weights 4j..4j+3) spread to bit 4 of each byte */
uint spread5(uint b)
{
return ((b & 1u) << 4) | ((b & 2u) << 11) | ((b & 4u) << 18) | ((b & 8u) << 25);
}
/* a byte of Q2_0 (four weights of two bits) spread to a byte each */
uint spread2(uint b)
{
uint t = (b | (b << 12)) & 0x000f000fu;
return (t | (t << 6)) & 0x03030303u;
}
/* c taken from each byte of v (bytes of 0 to 127, results as signed) */
uint sub_bytes(uint v, uint c)
{
return ((v | 0x80808080u) - c) ^ 0x80808080u;
}
/* the scale of the block at byte off */
float scale(U1 W, uint off)
{
uint w = bword(W, off, 0u);
return KIND == 1u ? uintBitsToFloat((w & 0xffu) < 2u ? 0x00200000u << (w & 0xffu)
: ((w & 0xffu) - 1u) << 23)
: unpackHalf2x16(w & 0xffffu).x;
}
void main()
{
U4 X = U4(p.x);
U1 W1 = U1(p.w);
F Y = F(p.y);
uint id = gl_LocalInvocationID.x;
uint r0 = gl_WorkGroupID.x * BM, v0 = gl_WorkGroupID.y * BN;
uint warp = id / WARP, tiw = id % WARP;
uint tiwr = tiw % (WM / TM), tiwc = tiw / (WM / TM);
uint warp_r = warp % (BM / WM), warp_c = warp / (BM / WM);
float sums[WNITER * TN * TM];
[[unroll]] for (uint i = 0; i < WNITER * TN * TM; i++) sums[i] = 0.0;
uint ngroups = p.nb * 8u + p.tail;
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);
[[unroll]] for (uint k = 0; k < KS; k++) {
uint g = g0 + k;
if (g >= ngroups) { /* past the row's end: nothing */
[[unroll]] for (uint t = 0; t < 2u; t++)
sa[k * BM + rl + l].qs[2u * iqs + t] = 0;
if (iqs == 0u)
sa[k * BM + rl + l].d = 0.0;
continue;
}
uint off = (r * ngroups + g) * S;
if (KIND == 3u) {
/* the block's first byte, its half's quants */
uint bo = off - (g % 2u) * S, qo = bo + 2u + 8u * (g % 2u);
uint q = bword(W1, qo, iqs / 2u);
[[unroll]] for (uint t = 0; t < 2u; t++) {
uint kw = 2u * iqs + t;
sa[k * BM + rl + l].qs[kw] =
int(sub_bytes(spread2((q >> (8u * (kw % 4u))) & 0xffu), 0x01010101u));
}
if (iqs == 0u)
sa[k * BM + rl + l].d = scale(W1, bo);
continue;
}
uint qh = KIND == 2u ? bword(W1, off + 2u, 0u) : 0u;
[[unroll]] for (uint t = 0; t < 2u; t++) {
uint kw = 2u * iqs + t, q = bword(W1, off + S - 16u, kw & 3u);
uint nib = kw < 4u ? q & 0x0f0f0f0fu : (q >> 4) & 0x0f0f0f0fu;
if (KIND == 2u)
nib = sub_bytes(nib | spread5((qh >> (4u * kw)) & 15u), 0x10101010u);
else
nib = iq4(nib);
sa[k * BM + rl + l].qs[kw] = int(nib);
}
if (iqs == 0u)
sa[k * BM + rl + l].d = scale(W1, off);
}
}
}
// activations: 2 invocations a vector, each half a group's quants
{
uint iqs = id % 2u, c = id / 2u;
uint v = min(v0 + c, p.nv - 1u);
[[unroll]] for (uint k = 0; k < KS; k++) {
uint g = min(g0 + k, ngroups - 1u), b = g / 8u, j = g % 8u;
uint xo = (v * p.xs + b) * XB;
uvec4 q = X.v[xo + 2u * j + 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 dq = X.v[xo + 18u + j / 4u];
uint dw = j % 4u == 0u ? dq.x : j % 4u == 1u ? dq.y : j % 4u == 2u ? dq.z : dq.w;
sb[k * BN + c].d = uintBitsToFloat(dw);
}
}
}
barrier();
for (uint k = 0; k < KS; k++) {
ACache ca[TM];
[[unroll]] for (uint cr = 0; cr < TM; cr++)
ca[cr] = sa[k * BM + warp_r * WM + tiwr * TM + cr];
[[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++)
[[unroll]] for (uint cc = 0; cc < TN; cc++) {
BCache cb = sb[k * BN + warp_c * WN + wsic * WSUBN + tiwc * TN + cc];
[[unroll]] for (uint cr = 0; cr < TM; cr++) {
int q = 0;
[[unroll]] for (uint i = 0; i < 8u; i++)
q += dotPacked4x8EXT(ca[cr].qs[i], cb.qs[i]);
sums[(wsic * TN + cc) * TM + cr] += ca[cr].d * cb.d * float(q);
}
}
}
barrier();
}
[[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++)
[[unroll]] for (uint cc = 0; cc < TN; cc++)
[[unroll]] for (uint cr = 0; cr < TM; cr++) {
uint row = r0 + warp_r * WM + tiwr * TM + cr;
uint v = v0 + warp_c * WN + wsic * WSUBN + tiwc * TN + cc;
if (row < p.rows && v < p.nv)
Y.v[v * p.ys + row] = sums[(wsic * TN + cc) * TM + cr];
}
}