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.

5.5 KB
Raw Blame
/* SPDX-License-Identifier: GPL-3.0-or-later
   Copyright (C) 2026 Maurizio Cammalleri */
#version 450
// Attention of one token, second step, with the quantization of its output
// for the products that follow: one workgroup per block of 256 of the
// n_head x hd outputs, a value a thread (element e: head e / hd, dimension
// e % hd). The runs of attn_decode.comp joined in their order - the
// largest maximum M, each run's sums and l scaled by e^(m - M), the sums
// divided by the total - then quantized to Q8_K as norm_quant.comp does
// (q256, and q32 if set): no dispatch of its own between the two. With
// gate set (qwen3.5's attention), each output times sigmoid of its gate
// (gs floats a token) before. With a window (Gemma 4's sliding layers) the
// runs wholly before it, which attn_decode.comp left, are not read.
#extension GL_EXT_buffer_reference : require
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_clustered : require
#extension GL_EXT_buffer_reference_uvec2 : require

layout(local_size_x = 256) in;

const uint C = 128u; /* positions a run, as attn_decode.comp */

layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer R { float v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer U { uint v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RP { uint v[]; };

layout(push_constant) uniform P {
    uvec2 part, q256, q32;
    uint hd, nch, ps; /* ps: floats from a token's runs to the next's */
    uint gs;  /* the gate's floats a token */
    uvec2 pos;
    uvec2 gate;
    uvec2 list; /* attn_decode.comp's, if set: the positions' count */
    uint ls, window;
} p;

shared float m8[32];        /* the block's maxima, eight values each */
shared int s8a[32], s8b[32]; /* and its quants' sums, eight each */

bool set(uvec2 a) { return a.x != 0u || a.y != 0u; }

float recip_det(float d)
{
    precise float y = uintBitsToFloat(0x7EF311C3u - floatBitsToUint(d));
    for (int i = 0; i < 3; i++)
        y = fma(y, fma(-d, y, 1.0), y);
    return y;
}

/* a / b correctly rounded (b normal, positive), as the CPU's division:
   the output quantized as janas_q8k_quantize does it (att_out_tok) */
float div_cr(float a, float b)
{
    precise float r = recip_det(b);
    precise float q = a * r;
    precise float e = fma(-q, b, a);
    return fma(e, r, q);
}

int q8(float v, float id)
{
    precise float q = roundEven(v * id);
    return int(clamp(q, -127.0, 127.0));
}

void main()
{
    uint t = gl_LocalInvocationID.x, b = gl_WorkGroupID.x, hd = p.hd;
    U Q = U(p.q256), Q32 = U(p.q32);
    /* token j of a block (workgroup y): positions 0 .. pos + j */
    uint j0 = gl_WorkGroupID.y;
    bool lst = set(p.list);
    uint npos = lst ? RP(p.list).v[j0 * p.ls] : RP(p.pos).v[0] + j0 + 1u;
    uint runs = (npos + C - 1u) / C; /* rounded up */
    uint lo = !lst && p.window != 0u && npos > p.window ? npos - p.window : 0u;
    uint c0 = lo / C; /* the first run attn_decode.comp computed */
    R PR = R(p.part);
    uint e = 256u * b + t, h = e / hd, i = e % hd;
    uint at = j0 * p.ps + h * p.nch * (hd + 2u);
    float M = PR.v[at + c0 * (hd + 2u) + hd];
    for (uint c = c0 + 1u; c < runs; c++)
        M = max(M, PR.v[at + c * (hd + 2u) + hd]);
    float L = 0.0, a = 0.0;
    for (uint c = c0; c < runs; c++) {
        uint o = at + c * (hd + 2u);
        float f = exp(PR.v[o + hd] - M);
        L += PR.v[o + hd + 1u] * f;
        a += PR.v[o + i] * f;
    }
    float v = a / L;
    if (set(p.gate)) {
        float gv = R(p.gate).v[j0 * p.gs + e];
        v *= 1.0 / (1.0 + exp(-gv));
    }
    /* the maxima of the groups of 32 and of the block */
    float mc = subgroupClusteredMax(abs(v), 8u);
    if (t % 8u == 0u)
        m8[t / 8u] = mc;
    barrier();
    uint l = t / 32u;
    float m32 = max(max(m8[4u * l], m8[4u * l + 1u]), max(m8[4u * l + 2u], m8[4u * l + 3u]));
    float m256 = 0.0;
    for (uint k = 0; k < 32u; k++)
        m256 = max(m256, m8[k]);
    precise float dd = div_cr(m256, 127.0);
    float id = dd > 0.0 ? div_cr(1.0, dd) : 0.0;
    precise float d32 = div_cr(m32, 127.0);
    float id32 = d32 > 0.0 ? div_cr(1.0, d32) : 0.0;
    int qa = q8(v, id), qb = q8(v, id32);
    /* four quants a word, from four neighbouring threads */
    uint wa = subgroupClusteredOr((uint(qa) & 0xffu) << (8u * (t % 4u)), 4u);
    uint wb = subgroupClusteredOr((uint(qb) & 0xffu) << (8u * (t % 4u)), 4u);
    uint base = (j0 * gl_NumWorkGroups.x + b) * 80u; /* 320 bytes a block */
    if (t % 4u == 0u) {
        Q.v[base + t / 4u] = wa;
        if (set(p.q32))
            Q32.v[base + t / 4u] = wb;
    }
    /* the sums of 16 quants: two of eight each */
    int ca = subgroupClusteredAdd(qa, 8u), cb = subgroupClusteredAdd(qb, 8u);
    if (t % 8u == 0u) {
        s8a[t / 8u] = ca;
        s8b[t / 8u] = cb;
    }
    barrier();
    if (t < 8u) {
        int sa0 = s8a[4u * t] + s8a[4u * t + 1u], sa1 = s8a[4u * t + 2u] + s8a[4u * t + 3u];
        Q.v[base + 64u + t] = (uint(sa0) & 0xffffu) | (uint(sa1) << 16);
        if (set(p.q32)) {
            int sb0 = s8b[4u * t] + s8b[4u * t + 1u], sb1 = s8b[4u * t + 2u] + s8b[4u * t + 3u];
            Q32.v[base + 64u + t] = (uint(sb0) & 0xffffu) | (uint(sb1) << 16);
            precise float dt =
                div_cr(max(max(m8[4u * t], m8[4u * t + 1u]), max(m8[4u * t + 2u], m8[4u * t + 3u])), 127.0);
            Q32.v[base + 72u + t] = floatBitsToUint(dt);
        }
    }
    if (t == 0u)
        Q.v[base + 72u] = floatBitsToUint(dd);
}