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.

6.1 KB
Raw Blame
/* SPDX-License-Identifier: GPL-3.0-or-later
   Copyright (C) 2026 Maurizio Cammalleri */
#version 450
// The fast prompt for IQ4_XS: the tiling of q4k_mmq.comp, as q3k_mmq.comp.
// A group of 32 weights has one 6-bit scale (less 32); a weight is a nibble
// through the sixteen signed levels of IQ4_NL (iq4xs_matvec.comp), so the
// products need no correction with the activations' sums. A block is 136
// bytes, 34 words: d and scales_h, scales_l, then group g's 16 bytes
// holding its weights 0-15 (low nibbles) and 16-31 (high). 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 = 64u, 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;

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[]; };

layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;

struct ACache { uint qs[8]; float dsc; }; // a group: its levels as bytes, d * scale
struct BCache { int qs[8]; float d8; };   // quants, d8
shared ACache sa[BM * KS];
shared BCache sb[BN * KS];

const int IQ4[16] = int[16](-127, -104, -83, -65, -49, -35, -22, -10,
                            1, 13, 25, 38, 53, 69, 89, 113);

/* each byte of w (a nibble) through the IQ4_NL table */
uint iq4(uint w)
{
    return (uint(IQ4[w & 15u]) & 0xffu) | ((uint(IQ4[(w >> 8) & 15u]) & 0xffu) << 8) |
           ((uint(IQ4[(w >> 16) & 15u]) & 0xffu) << 16) |
           ((uint(IQ4[(w >> 24) & 15u]) & 0xffu) << 24);
}

void main()
{
    U4 X = U4(p.x);
    U1 W = 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;
    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 are in one block (g0 is a multiple of
                // 4): words 0-3 of a group are the low nibbles of its 4 words
                // of qs, words 4-7 the high
                uint b = g0 / 8u, gb = g0 % 8u;
                uint off = (r * p.nb + b) * 34u;
                [[unroll]] for (uint k = 0; k < KS; k++)
                    [[unroll]] for (uint t = 0; t < 2u; t++) {
                        uint kw = 2u * iqs + t;
                        uint q = W.v[off + 2u + 4u * (gb + k) + (kw & 3u)];
                        sa[k * BM + rl + l].qs[kw] =
                            iq4(kw < 4u ? q & 0x0f0f0f0fu : (q >> 4) & 0x0f0f0f0fu);
                    }
                if (iqs == 0u) {
                    uint w0 = W.v[off], sl = W.v[off + 1u];
                    float d = unpackHalf2x16(w0).x;
                    uint sh = w0 >> 16;
                    [[unroll]] for (uint k = 0; k < KS; k++) {
                        uint g = gb + k;
                        uint ls = ((sl >> (8u * (g / 2u) + 4u * (g & 1u))) & 15u) |
                                  (((sh >> (2u * g)) & 3u) << 4);
                        sa[k * BM + rl + l].dsc = d * float(int(ls) - 32);
                    }
                }
            }
        }
        // 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 = g0 + k, b = g / 8u, G = g % 8u;
                uint xo = (v * p.xs + 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)
                    sb[k * BN + c].d8 = uintBitsToFloat(X.v[xo + 18u].x);
            }
        }
        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(int(ca[cr].qs[i]), cb.qs[i]);
                        sums[(wsic * TN + cc) * TM + cr] += cb.d8 * ca[cr].dsc * 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];
            }
}