#version 450 //! `conv_vec_int8.comp` for a **4-bit** kernel with **per-block** scales. //! //! The scale table is rank 2, `(out_channels, ceil(in_c / I4_BLOCK))`, at `p.act_weight`, so the //! scale multiplies each block's partial sum rather than the finished accumulator. //! //! # Lane layout //! //! A workgroup owns [`ROWS`] output channels. Lane `l` owns the row pair `2 * (l % 4)` and //! `2 * (l % 4) + 1` and the blocks `l / 4, l / 4 + 16, ...`. Every Gemma projection has a block //! count divisible by 16 (48, 64, 192, 384), so all 64 lanes do the same work; the previous //! layout (a lane per block, all eight rows) left 16 of 64 lanes idle at `in_c` 1536 and re-read //! the activations per row. Here one block is two `uvec4` weight loads and eight read-only //! `f16vec4` activation loads shared by both rows. //! //! # Push fields //! //! As `conv_vec_int8.comp`: `p.weight` is a 32-bit **word** offset, `p.act_weight` the scale //! table, `p.bias` the bias, `p.in_c` the taps, `p.out_c` the output channels and `p.count` the //! workgroup count, one per group of [`ROWS`] channels. #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[]; }; /// Output channels one workgroup owns. `CONV_VEC_INT4_ROWS` in `nets` must equal this. #define ROWS 8u shared float partial[2][64]; /// The eight sign-extended nibbles of `bits` dotted with eight activations. float dot8(uint bits, vec4 lo, vec4 hi) { ivec4 v = ivec4(bits); vec4 a = vec4((v << ivec4(28, 24, 20, 16)) >> 28); vec4 b = vec4((v << ivec4(12, 8, 4, 0)) >> 28); return dot(a, lo) + dot(b, hi); } void main() { uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint lane = gl_LocalInvocationID.x; uint channel_base = tile * ROWS; 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 row0 = min(channel_base + 2u * (lane & 3u), p.out_c - 1u); uint row1 = min(channel_base + 2u * (lane & 3u) + 1u, p.out_c - 1u); float acc0 = 0.0; float acc1 = 0.0; bool wide = (p.in_c & 31u) == 0u && (p.weight & 3u) == 0u && (p.in0 & 3u) == 0u; if (wide) { uint per_row = p.in_c >> 5u; uint w0 = (p.weight >> 2u) + row0 * per_row; uint w1 = (p.weight >> 2u) + row1 * per_row; uint quad0 = p.in0 >> 2u; for (uint block = lane >> 2u; block < blocks; block += 16u) { uint q = quad0 + block * 8u; vec4 x0 = vec4(arena_quad[q]); vec4 x1 = vec4(arena_quad[q + 1u]); vec4 x2 = vec4(arena_quad[q + 2u]); vec4 x3 = vec4(arena_quad[q + 3u]); vec4 x4 = vec4(arena_quad[q + 4u]); vec4 x5 = vec4(arena_quad[q + 5u]); vec4 x6 = vec4(arena_quad[q + 6u]); vec4 x7 = vec4(arena_quad[q + 7u]); uvec4 a = weights128[w0 + block]; uvec4 b = weights128[w1 + block]; float s0 = dot8(a.x, x0, x1) + dot8(a.y, x2, x3) + dot8(a.z, x4, x5) + dot8(a.w, x6, x7); float s1 = dot8(b.x, x0, x1) + dot8(b.y, x2, x3) + dot8(b.z, x4, x5) + dot8(b.w, x6, x7); acc0 += s0 * int4_scale(row0, block, blocks); acc1 += s1 * int4_scale(row1, block, blocks); } } else { // Any `in_c` or alignment: a row starts wherever `channel * in_c` puts it, so a block's // nibbles can straddle words; walk word by word and take only what each word holds. for (uint block = lane >> 2u; block < blocks; block += 16u) { uint base = block * I4_BLOCK; uint span = min(base + I4_BLOCK, p.in_c) - base; for (uint r = 0u; r < 2u; r++) { uint channel = r == 0u ? row0 : row1; uint row = channel * p.in_c; float total = 0.0; uint taken = 0u; while (taken < span) { uint at = row + base + taken; uint word = weights32[p.weight + (at >> 3u)]; uint offset = at & 7u; uint run = min(8u - offset, span - taken); for (uint n = 0u; n < run; n++) { int code = bitfieldExtract(int(word), int((offset + n) << 2u), 4); total += float(code) * float(arena[p.in0 + base + taken + n]); } taken += run; } float scaled = total * int4_scale(channel, block, blocks); if (r == 0u) { acc0 += scaled; } else { acc1 += scaled; } } } } partial[0][lane] = acc0; partial[1][lane] = acc1; barrier(); if (lane >= ROWS) { return; } uint channel = channel_base + lane; if (channel >= p.out_c) { return; } // Row `lane` lives in slot `lane & 1` of the lanes congruent to `lane / 2` mod 4. uint pair = lane >> 1u; uint slot = lane & 1u; float total = 0.0; for (uint i = 0u; i < 16u; i++) { total += partial[slot][pair + 4u * i]; } float offset = float(weights[p.bias + channel]); arena[p.out_at + channel] = float16_t(quant_store(fused_store(total + offset, p.act, channel, channel))); }