#version 450 //! A `1 x 1` convolution with GGUF Q2_K weights over a sequence: a Q2_K-by-fp16 matrix product. //! //! `out[o][p] = act(sum over superblocks of (d_all*(q*lo) - d_min*hi) * x + bias[o])`, //! `p` over positions. MADLAD's encoder and prefill. //! //! # Shape //! //! `conv_point_int8.comp`'s: each invocation owns four output channels by four positions and a //! workgroup a 32 x 32 block, with the reuse held in registers rather than staged through shared //! memory. The previous version staged 16 x 16 dequantised tiles with two barriers per 16 taps; on a //! Pixel 8 it ran MADLAD's 192 encoder projections in 1.3 s. //! //! # Sixteen taps per weight word //! //! When `in_c % 256 == 0`, the position count is a multiple of four and the activation base is //! 4-aligned - every MADLAD projection - the loop walks the superblock's `qs` one 32-bit word at a //! time, as `conv_vec_q2k.comp` does: one word is the 2-bit codes of 16 taps, four per code lane. //! Each lane's affine pair factors out of its four taps, //! `sum((dall*lo*c - dmin*hi) * x) = dall*lo*sum(c*x) - dmin*hi*sum(x)`, and the activations are //! read four positions at a time through a read-only `f16vec4` view, shared by the four rows. //! Anything else takes an element-at-a-time loop through `q2k_code`/`q2k_scale`. //! //! `p.weight` is a **32-bit word** offset to the Q2_K payload, as in `conv_int8.comp`. //! `p.act_weight` holds the `(d, dmin)` table as an fp16 element index: pair `2*b` and `2*b+1` for //! superblock `b` of the row. `Act::PRelu` cannot ride along, for the same reason as the int8 pair. #include "common.glsl" layout(std430, binding = 0) readonly buffer ArenaQuad { f16vec4 arena_quad[]; }; /// Output channels and positions per workgroup: 8 x 8 invocations of 4 x 4 each. /// /// **Must equal `CONV_POINT_INT8_TILE` in `nets`**, which counts this kind's workgroups too. #define TILE 32u /// Channels and positions per invocation. #define BLOCK 4u /// Partial sums from the K-split's other slices, combined by slice 0 in slice order. shared float partial[64][BLOCK * BLOCK]; void main() { uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } // `p.in1` = s > 0 (word path only): the workgroup's lanes split into 2^s slices, each summing // a contiguous 1/2^s of the taps for a (32 >> s) x 32 tile, so a GEMM with few tiles (MADLAD's // 8192 -> 1024 at 32 positions is 32) runs 2^s times the workgroups at the same loads per MAC. uint split = p.in1; uint slice_lanes = 64u >> split; uint positions = p.out_h * p.out_w; uint tiles_across = (positions + TILE - 1u) / TILE; uint lid = gl_LocalInvocationID.x; uint slice = lid / slice_lanes; uint channel_base = (tile / tiles_across) * (TILE >> split) + ((lid % slice_lanes) >> 3u) * BLOCK; uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; bool busy = position_base < positions; if (!busy && split == 0u) { return; } uint blocks = (p.in_c + Q2K_BLOCK - 1u) / Q2K_BLOCK; uint rows[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { rows[i] = min(channel_base + i, p.out_c - 1u); } bool live[BLOCK]; for (uint j = 0u; j < BLOCK; j++) { live[j] = position_base + j < positions; } float acc[BLOCK][BLOCK]; for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { acc[i][j] = 0.0; } } bool by_word = (p.in_c % Q2K_BLOCK) == 0u && (positions & 3u) == 0u && (p.in0 & 3u) == 0u; if (by_word) { uint quads = positions >> 2u; uint quad0 = (p.in0 + position_base) >> 2u; vec4 sum[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { sum[i] = vec4(0.0); } bool pair_word = (p.act_weight & 1u) == 0u; uint units = (blocks * 16u) >> split; uint unit0 = busy ? slice * units : 0u; uint unit_end = busy ? unit0 + units : 0u; for (uint unit = unit0; unit < unit_end; unit++) { uint block = unit >> 4u; uint w = unit & 15u; uint half_idx = w >> 3u; uint odd = (w >> 2u) & 1u; uint first = block * Q2K_BLOCK + half_idx * 128u + odd * 16u + (w & 3u) * 4u; // Per row: the code word, the two scale words for this half, and the affine pair. uint codes[BLOCK]; uint scales[BLOCK][2]; float dall[BLOCK]; float dmin[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { uint sb = p.weight + (rows[i] * blocks + block) * 21u; codes[i] = weights32[sb + 4u + w]; scales[i][0] = weights32[sb + half_idx * 2u]; scales[i][1] = weights32[sb + half_idx * 2u + 1u]; uint pair = p.act_weight + (rows[i] * blocks + block) * 2u; vec2 dm; if (pair_word) { dm = unpackHalf2x16(weights32[pair >> 1u]); } else { dm = vec2(float(weights[pair]), float(weights[pair + 1u])); } dall[i] = dm.x * dm.x; dmin[i] = dm.y * dm.x; } for (uint q = 0u; q < 4u; q++) { // Taps `first + q*32 + 0..3`, four positions each. vec4 x[4]; for (uint t = 0u; t < 4u; t++) { x[t] = vec4(arena_quad[quad0 + (first + q * 32u + t) * quads]); } vec4 xsum = x[0] + x[1] + x[2] + x[3]; uint shift = q << 1u; uint byte = (((q & 1u) << 1u) + odd) << 3u; for (uint i = 0u; i < BLOCK; i++) { uint sc = (scales[i][q >> 1u] >> byte) & 255u; vec4 cx = float((codes[i] >> shift) & 3u) * x[0] + float((codes[i] >> (shift + 8u)) & 3u) * x[1] + float((codes[i] >> (shift + 16u)) & 3u) * x[2] + float((codes[i] >> (shift + 24u)) & 3u) * x[3]; sum[i] += dall[i] * float(sc & 15u) * cx - dmin[i] * float(sc >> 4u) * xsum; } } } if (split > 0u) { if (slice > 0u) { for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { partial[lid][i * BLOCK + j] = sum[i][j]; } } } barrier(); if (slice > 0u || !busy) { return; } for (uint k = 1u; k < (1u << split); k++) { for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { sum[i][j] += partial[lid + k * slice_lanes][i * BLOCK + j]; } } } } for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { acc[i][j] = sum[i][j]; } } } else { for (uint k = 0u; k < p.in_c; k++) { uint block = k / Q2K_BLOCK; uint inblock = k % Q2K_BLOCK; uint half_idx = inblock / 128u; uint lane = (inblock % 128u) / 32u; uint odd = (inblock % 32u) / 16u; uint at = p.in0 + k * positions + position_base; float x[BLOCK]; for (uint j = 0u; j < BLOCK; j++) { x[j] = live[j] ? float(arena[at + j]) : 0.0; } for (uint i = 0u; i < BLOCK; i++) { uint sbo = (p.weight * 4u) + (rows[i] * blocks + block) * Q2K_BYTES; uint sc = q2k_scale(sbo, half_idx, lane * 2u + odd); uint code = q2k_code(sbo, half_idx, inblock); uint pair = p.act_weight + (rows[i] * blocks + block) * 2u; float d = float(weights[pair]); float m = float(weights[pair + 1u]); float value = d * d * float(code) * float(sc & 15u) - m * d * float(sc >> 4u); for (uint j = 0u; j < BLOCK; j++) { acc[i][j] += value * x[j]; } } } } for (uint i = 0u; i < BLOCK; i++) { uint channel = channel_base + i; if (channel >= p.out_c) { continue; } // Already scaled inside the loop (per-lane scales cannot hoist), so only the bias is left. float offset = float(weights[p.bias + channel]); for (uint j = 0u; j < BLOCK; j++) { if (!live[j]) { continue; } uint index = channel * positions + position_base + j; float value = quant_store(fused_store(acc[i][j] + offset, p.act, channel, index)); arena[p.out_at + index] = float16_t(value); } } }