#version 450 //! A `1 x 1` convolution with int8 weights over a sequence: an int8-by-fp16 matrix product. //! //! `out[o][p] = act(scale[o] * sum_i W[o][i] * in[i][p] + bias[o])`, `p` over positions. //! //! # Why this exists as well as `conv_int8.comp` //! //! `conv_int8.comp` is shaped like `conv.comp`: one invocation per output element, each reading //! its own weight row from global memory with no reuse. That is survivable for a spatial kernel //! and catastrophic for a wide `1 x 1` over a sequence, which is every encoder projection and every //! prefill: the weight row is re-read once per position. //! //! # Register blocking, not shared-memory tiling //! //! Each invocation owns a 4 x 4 block - four output channels by four positions - and the 64 of a //! workgroup tile a 32 x 32 block of the output, eight across and eight down. Every weight word read //! feeds four positions and every activation read feeds four channels, which is the whole of the //! reuse; it is held in registers rather than staged through shared memory. //! //! The previous version staged 16 x 16 tiles of both operands through shared memory with two //! barriers per 16 input channels. On Mali, shared memory is ordinary cached memory and a barrier //! stalls the whole workgroup, so that bought no reuse the cache was not already giving and paid //! for it 128 times per 1,024-wide projection: on a Pixel 8 it reached 9-16 GMAC/s, which made it //! 98% of NLLB's encoder and a third of whisper's. //! //! # Four input channels per weight load //! //! Int8 weights pack four to a 32-bit word along the input axis, so one load from the word view //! gives four taps of one output channel. That needs every row to start on a word, which is //! `in_c % 4 == 0`; every projection in every net here satisfies it, and the rare one that does not //! takes the element-at-a-time loop instead. //! //! # The scale //! //! Per **output channel**, so each of an invocation's four rows carries one scale applied to its //! finished accumulators. `p.weight` is a **32-bit word** offset, as in `conv_int8.comp`; //! `p.act_weight` holds the scale tensor as an fp16 element index, which is why `Act::PRelu` //! cannot ride along. //! //! # Wide loads //! //! When `in_c % 16 == 0` weights are read sixteen taps at a time through the `uvec4` view, at any //! width. Activations come four positions at a time through a read-only `f16vec4` view of the //! arena when the position count is a multiple of four (every encoder and prefill here), and as //! four scalar reads otherwise (Supertonic's sampler runs at whatever frame count the duration //! predictor gives). The narrow paths below remain for `in_c % 16 != 0`. #include "common.glsl" /// The arena again, four fp16 elements per entry, for the wide activation loads. /// /// The same binding as `arena`; reads only. The aligned path reads only the input tensor; the /// ragged path's quads can overhang it by up to three elements each side, which only ever land in /// lanes that are shifted out or belong to dead columns, so a concurrent write there is harmless. /// Indexing needs the element offset divisible by four, which the arena's 16-byte allocation /// alignment and a position count divisible by four together give on the aligned path. 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 the workgroups. #define TILE 32u /// Channels and positions per invocation. #define BLOCK 4u /// Channels per invocation on the split path (`p.in1 == 1`). #define SPLIT 2u /// The wide path with two channels per invocation instead of four, for GEMMs whose 32 x 32 tiles /// are too few to fill the GPU (Supertonic's sampler: 512 x 52 is 32 workgroups). Twice the /// workgroups, each 16 channels x 32 positions. Every output sums the same terms in the same order /// as the four-row path, so the result is bit-identical. Selected only when `in_c % 16 == 0` and /// the weights are 16-byte aligned (see `nets`). void split_rows() { 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 / BLOCK * SPLIT) + (lid >> 3u) * SPLIT; uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; if (position_base >= positions) { return; } uint rows16[SPLIT]; for (uint i = 0u; i < SPLIT; i++) { uint row = min(channel_base + i, p.out_c - 1u); rows16[i] = (p.weight + ((row * p.in_c) >> 2u)) >> 2u; } vec4 sum[SPLIT]; for (uint i = 0u; i < SPLIT; i++) { sum[i] = vec4(0.0); } if ((positions & 3u) == 0u && (p.in0 & 3u) == 0u) { uint quads = positions >> 2u; uint quad = (p.in0 + position_base) >> 2u; for (uint k = 0u; k < p.in_c; k += 16u) { uvec4 w[SPLIT]; for (uint i = 0u; i < SPLIT; i++) { w[i] = weights128[rows16[i] + (k >> 4u)]; } for (uint t = 0u; t < 16u; t++) { vec4 x = vec4(arena_quad[quad + (k + t) * quads]); for (uint i = 0u; i < SPLIT; i++) { float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); sum[i] += tap * x; } } } } else { uint first = p.in0 + position_base; uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u; for (uint k = 0u; k < p.in_c; k += 16u) { uvec4 w[SPLIT]; for (uint i = 0u; i < SPLIT; i++) { w[i] = weights128[rows16[i] + (k >> 4u)]; } for (uint t = 0u; t < 16u; t++) { 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); vec4 x = vec4(h); for (uint i = 0u; i < SPLIT; i++) { float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); sum[i] += tap * x; } } } } for (uint i = 0u; i < SPLIT; i++) { uint channel = channel_base + i; if (channel >= p.out_c) { continue; } float scale = float(weights[p.act_weight + channel]); float offset = float(weights[p.bias + channel]); for (uint j = 0u; j < BLOCK; j++) { if (position_base + j >= positions) { continue; } uint index = channel * positions + position_base + j; float value = quant_store(fused_store(sum[i][j] * scale + offset, p.act, channel, index)); arena[p.out_at + index] = float16_t(value); } } } /// A one-row `1 x kw` convolution as this GEMM (`p.in1 == 2`): K walks (input channel, tap), /// which is the weights' `[oc][ic][kx]` order, and the activation for position `x` and tap /// `kx` is `in[ic][x * stride + kx * dil - pad_l]`, zero outside the row. Same summation order /// as `conv_int8.comp`. Selected only when `in_c * kw % 16 == 0` and the weights are 16-byte /// aligned (see `nets`). void row_taps() { uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint positions = 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; if (position_base >= positions) { return; } uint taps = p.in_c * p.kw; uint rows16[BLOCK]; vec4 sum[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { uint row = min(channel_base + i, p.out_c - 1u); rows16[i] = (p.weight + ((row * taps) >> 2u)) >> 2u; sum[i] = vec4(0.0); } ivec4 cols = (ivec4(position_base) + ivec4(0, 1, 2, 3)) * int(p.stride_w) - int(p.pad_l); int width = int(p.in_w); uint ic = 0u; uint kx = 0u; for (uint k = 0u; k < taps; k += 16u) { uvec4 w[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { w[i] = weights128[rows16[i] + (k >> 4u)]; } for (uint t = 0u; t < 16u; t++) { ivec4 c = cols + int(kx * p.dil_w); uint row = p.in0 + ic * p.in_w; vec4 x; for (uint j = 0u; j < BLOCK; j++) { x[j] = (c[j] >= 0 && c[j] < width) ? float(arena[row + uint(c[j])]) : 0.0; } for (uint i = 0u; i < BLOCK; i++) { float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); sum[i] += tap * x; } kx++; if (kx == p.kw) { kx = 0u; ic++; } } } for (uint i = 0u; i < BLOCK; i++) { uint channel = channel_base + i; if (channel >= p.out_c) { continue; } float scale = float(weights[p.act_weight + channel]); float offset = float(weights[p.bias + channel]); for (uint j = 0u; j < BLOCK; j++) { if (position_base + j >= positions) { continue; } uint index = channel * positions + position_base + j; float value = quant_store(fused_store(sum[i][j] * scale + offset, p.act, channel, index)); arena[p.out_at + index] = float16_t(value); } } } #include "conv_point_widened.glsl" void main() { if (p.in1 == 1u) { split_rows(); return; } if (p.in1 == 3u) { widened_block(); return; } if (p.in1 == 4u) { widened_split(); return; } if (p.in1 == 2u) { row_taps(); 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; // Rows past the last channel read the last row instead, so the inner loop needs no guard; // their results are discarded at the store. uint rows[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { rows[i] = min(channel_base + i, p.out_c - 1u); } // Columns past the last position are read as zero and never stored. 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 & 15u) == 0u && (p.weight & 3u) == 0u) { if (position_base >= positions) { return; } uint rows16[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { rows16[i] = (p.weight + ((rows[i] * p.in_c) >> 2u)) >> 2u; } vec4 sum[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { sum[i] = vec4(0.0); } if ((positions & 3u) == 0u && (p.in0 & 3u) == 0u) { uint quads = positions >> 2u; uint quad = (p.in0 + position_base) >> 2u; for (uint k = 0u; k < p.in_c; k += 16u) { uvec4 w[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { w[i] = weights128[rows16[i] + (k >> 4u)]; } for (uint t = 0u; t < 16u; t++) { vec4 x = vec4(arena_quad[quad + (k + t) * quads]); for (uint i = 0u; i < BLOCK; i++) { float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); sum[i] += tap * x; } } } } else { // Ragged width: a row's four positions straddle two quads. Load both and shift; the // shift is the same for every invocation. The second quad is clamped to the tensor's // last one, which only ever substitutes for dead columns, never stored. uint first = p.in0 + position_base; uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u; for (uint k = 0u; k < p.in_c; k += 16u) { uvec4 w[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { w[i] = weights128[rows16[i] + (k >> 4u)]; } for (uint t = 0u; t < 16u; t++) { 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); vec4 x = vec4(h); for (uint i = 0u; i < BLOCK; i++) { float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); sum[i] += tap * x; } } } } for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { acc[i][j] = sum[i][j]; } } } else if ((p.in_c & 3u) == 0u) { uint words[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { words[i] = p.weight + ((rows[i] * p.in_c) >> 2u); } for (uint k = 0u; k < p.in_c; k += 4u) { float x[4][BLOCK]; for (uint t = 0u; t < 4u; t++) { uint at = p.in0 + (k + t) * positions + position_base; for (uint j = 0u; j < BLOCK; j++) { x[t][j] = live[j] ? float(arena[at + j]) : 0.0; } } for (uint i = 0u; i < BLOCK; i++) { int word = int(weights32[words[i] + (k >> 2u)]); for (uint t = 0u; t < 4u; t++) { float w = float(bitfieldExtract(word, int(t << 3u), 8)); for (uint j = 0u; j < BLOCK; j++) { acc[i][j] += w * x[t][j]; } } } } } else { 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(int8_at(p.weight, rows[i] * p.in_c + k)); 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 scale = float(weights[p.act_weight + channel]); 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] * scale + offset, p.act, channel, index)); arena[p.out_at + index] = float16_t(value); } } }