#version 450 //! `conv_point_int8.comp` for a **4-bit** kernel with **per-block** scales. //! //! Same register-blocked layout: a 32 x 32 output tile per workgroup, 4 channels x 4 positions //! per lane, no shared memory. When `in_c` is a multiple of `I4_BLOCK` (32) and the weights are //! 16-byte aligned, one `uvec4` load is a whole block of one row, the 32 taps of a block //! accumulate unscaled, and the row's block scale multiplies the partial once. That is the //! difference from the int8 shader, which scales the finished accumulator: a per-block scale has //! to reach each block's sum. //! //! # Push fields //! //! As `conv_point_int8.comp`, with `p.act_weight` a rank-2 `(out_channels, blocks)` table instead //! of a rank-1 per-row one. Dispatched with `CONV_POINT_INT8_TILE` tiles. #include "common.glsl" /// The arena as quads of fp16, for the wide activation loads. Read-only, and never a range this /// dispatch writes. layout(std430, binding = 0) readonly buffer ArenaQuad { f16vec4 arena_quad[]; }; #define TILE 32u #define BLOCK 4u /// The same arena as 16-byte groups of eight fp16, for `wide()` when the eight positions a lane /// owns are one aligned group. layout(std430, binding = 0) readonly buffer ArenaOct { uvec4 arena_oct[]; }; void store8(uint channel, uint position_base, uint positions, vec4 lo, vec4 hi) { if (channel >= p.out_c) { return; } float offset = float(weights[p.bias + channel]); for (uint j = 0u; j < 8u; j++) { uint position = position_base + j; if (position >= positions) { continue; } float acc = j < 4u ? lo[j] : hi[j - 4u]; uint index = channel * positions + position; arena[p.out_at + index] = float16_t(quant_store(fused_store(acc + offset, p.act, channel, index))); } } /// `p.in1 == 1` or `4`: 4 channels x 8 positions per lane over a 32 x 64 tile, chosen by the emit only /// when `in_c % 32`, `p.weight % 4`, `positions % 4` and `p.in0 % 4` are all zero. Each nibble /// unpack feeds 8 FMAs instead of 4, and the block scale is folded into the tap so the lane keeps /// 32 accumulators and no per-block partials (scale per tap, not per block sum: rounding moves /// within the int4 fixtures' tolerance). Weights come one word (8 taps) per row at a time rather /// than a whole block up front: fewer live registers, 13% faster on a Pixel 8. `in1` 4 (positions /// and `p.in0` % 8 == 0) reads the eight activations as one 16-byte load instead of two 8-byte /// ones: ConvPointInt4 of a 200-token Gemma prefill 3.06 -> 2.25 s with both. void wide() { uint positions = p.out_h * p.out_w; uint tiles_across = (positions + 63u) / 64u; uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint lid = gl_LocalInvocationID.x; uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; uint position_base = (tile % tiles_across) * 64u + (lid & 7u) * 8u; if (position_base >= positions) { return; } uint blocks = p.in_c >> 5u; uint rows[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { rows[i] = min(channel_base + i, p.out_c - 1u); } vec4 lo[BLOCK]; vec4 hi[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { lo[i] = vec4(0.0); hi[i] = vec4(0.0); } uint quads = positions >> 2u; uint quad = (p.in0 + position_base) >> 2u; bool second = position_base + 4u < positions; for (uint block = 0u; block < blocks; block++) { float scale[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { scale[i] = int4_scale(rows[i], block, blocks); } for (uint word = 0u; word < 4u; word++) { uint w[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { w[i] = weights32[p.weight + (rows[i] * blocks + block) * 4u + word]; } uint k = block * I4_BLOCK + word * 8u; for (uint t = 0u; t < 8u; t++) { uint at = quad + (k + t) * quads; vec4 x0 = vec4(arena_quad[at]); vec4 x1 = second ? vec4(arena_quad[at + 1u]) : vec4(0.0); for (uint i = 0u; i < BLOCK; i++) { float tap = float(bitfieldExtract(int(w[i]), int(t << 2u), 4)) * scale[i]; lo[i] += tap * x0; hi[i] += tap * x1; } } } } for (uint i = 0u; i < BLOCK; i++) { store8(channel_base + i, position_base, positions, lo[i], hi[i]); } } /// `wide()` with the 16-byte activation load (`in1` 4). void wide_oct() { uint positions = p.out_h * p.out_w; uint tiles_across = (positions + 63u) / 64u; uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint lid = gl_LocalInvocationID.x; uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; uint position_base = (tile % tiles_across) * 64u + (lid & 7u) * 8u; if (position_base >= positions) { return; } uint blocks = p.in_c >> 5u; uint rows[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { rows[i] = min(channel_base + i, p.out_c - 1u); } vec4 lo[BLOCK]; vec4 hi[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { lo[i] = vec4(0.0); hi[i] = vec4(0.0); } uint octs = positions >> 3u; uint oct = (p.in0 + position_base) >> 3u; for (uint block = 0u; block < blocks; block++) { float scale[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { scale[i] = int4_scale(rows[i], block, blocks); } for (uint word = 0u; word < 4u; word++) { uint w[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { w[i] = weights32[p.weight + (rows[i] * blocks + block) * 4u + word]; } uint k = block * I4_BLOCK + word * 8u; for (uint t = 0u; t < 8u; t++) { uvec4 u = arena_oct[oct + (k + t) * octs]; vec4 x0 = vec4(unpackHalf2x16(u.x), unpackHalf2x16(u.y)); vec4 x1 = vec4(unpackHalf2x16(u.z), unpackHalf2x16(u.w)); for (uint i = 0u; i < BLOCK; i++) { float tap = float(bitfieldExtract(int(w[i]), int(t << 2u), 4)) * scale[i]; lo[i] += tap * x0; hi[i] += tap * x1; } } } } for (uint i = 0u; i < BLOCK; i++) { store8(channel_base + i, position_base, positions, lo[i], hi[i]); } } void main() { if (p.in1 == 4u) { wide_oct(); return; } if (p.in1 == 1u) { wide(); return; } uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint positions = p.out_h * p.out_w; uint tiles_across = (positions + TILE - 1u) / TILE; uint lid = gl_LocalInvocationID.x; uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; uint blocks = (p.in_c + I4_BLOCK - 1u) / I4_BLOCK; // Rows past the output read the last row and are dropped at the store. 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; } } if ((p.in_c & 31u) == 0u && (p.weight & 3u) == 0u) { // No barriers in this shader, so a lane with no live position may leave. if (position_base >= positions) { return; } // A row starts at word `p.weight + row * in_c / 8`, a whole `uvec4` when both terms are. uint rows32[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { rows32[i] = (p.weight >> 2u) + rows[i] * (p.in_c >> 5u); } vec4 sum[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { sum[i] = vec4(0.0); } bool aligned = (positions & 3u) == 0u && (p.in0 & 3u) == 0u; uint quads = positions >> 2u; uint quad = (p.in0 + position_base) >> 2u; uint first = p.in0 + position_base; uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u; for (uint block = 0u; block < blocks; block++) { uvec4 w[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { w[i] = weights128[rows32[i] + block]; } vec4 part[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { part[i] = vec4(0.0); } uint k = block * I4_BLOCK; for (uint t = 0u; t < I4_BLOCK; t++) { vec4 x; if (aligned) { x = vec4(arena_quad[quad + (k + t) * quads]); } else { // Four positions straddling two quads; the clamp keeps the tail read in // bounds, and the lanes it substitutes are not live. uint at = first + (k + t) * positions; uint q = at >> 2u; uint s = at & 3u; f16vec4 a = arena_quad[q]; f16vec4 b = arena_quad[min(q + 1u, last_quad)]; f16vec4 h = s == 0u ? a : s == 1u ? f16vec4(a.yzw, b.x) : s == 2u ? f16vec4(a.zw, b.xy) : f16vec4(a.w, b.xyz); x = vec4(h); } for (uint i = 0u; i < BLOCK; i++) { float tap = float(bitfieldExtract(int(w[i][t >> 3u]), int((t & 7u) << 2u), 4)); part[i] += tap * x; } } for (uint i = 0u; i < BLOCK; i++) { sum[i] += part[i] * int4_scale(rows[i], block, blocks); } } for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { acc[i][j] = sum[i][j]; } } } else { // Any `in_c`: a nibble and its block scale per tap. for (uint k = 0u; k < p.in_c; k++) { 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++) { float w = float(int4_at(p.weight, rows[i] * p.in_c + k)) * int4_scale(rows[i], k / I4_BLOCK, blocks); for (uint j = 0u; j < BLOCK; j++) { acc[i][j] += w * x[j]; } } } } for (uint i = 0u; i < BLOCK; i++) { uint channel = channel_base + i; if (channel >= p.out_c) { continue; } 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); } } }