#version 450 //! A `1 x 1` convolution as a register-blocked matrix multiply. //! //! `out[o][p] = act(sum_i W[o][i] * in[i][p] + bias[o])`, where `p` runs over positions. //! //! # Why this exists //! //! `conv.comp` gives every output element its own invocation and reads that element's weights //! straight from global memory, with no reuse between invocations. For a spatial kernel that is //! survivable: a 3x3 is small and the cache serves it to the many pixels that share it. //! //! It is not survivable for a **wide** `1 x 1` over a **short** sequence, which is what every //! projection in a transformer is. Measured on a Tensor G4: the runtime achieved 15.2 GFLOP/s //! against a device peak of order 1000, because a 1024-to-1024 projection over 54 positions was //! moving about 113 MB of weights to do 113 MFLOP of work — every one of the 55,296 output //! elements re-reading a whole 1024-wide row. //! //! # The tiling //! //! Register-blocked like `conv_point_int8.comp`: each invocation owns four output channels by //! four positions and the 64 of a workgroup tile a [`TILE`] x [`TILE`] block, with no shared //! memory or barriers (on Mali those bought no reuse the cache did not already give). Weights are //! read eight at a time through the `uvec4` view when rows are 16-byte aligned, activations four //! positions at a time when the position count is a multiple of four. Each output sums its taps in //! input-channel order from zero and adds the bias last, as the shared-memory version did. //! //! # Where it is not used //! //! Grouped convolutions, and anything whose kernel is not `1 x 1`. `Builder::conv` routes to this //! only when both hold; everything else keeps `conv.comp`. The two want opposite memory layouts //! and one shader serving both would be worse at each. #include "common.glsl" /// The arena again, four fp16 elements per entry, for the wide activation loads. 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_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, for GEMMs with too few 32 x 32 tiles to fill // the GPU (Supertonic's text encoder: 56 positions). Twice the workgroups, each 16 channels x 32 // positions; every output sums the same terms in the same order, so the result is identical. // Selected only when the wide path's alignment holds (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 rows[SPLIT]; vec4 sum[SPLIT]; for (uint i = 0u; i < SPLIT; i++) { rows[i] = p.weight + min(channel_base + i, p.out_c - 1u) * p.in_c; sum[i] = vec4(0.0); } uint quads = positions >> 2u; uint quad = (p.in0 + position_base) >> 2u; for (uint k = 0u; k < p.in_c; k += 8u) { uvec4 w[SPLIT]; for (uint i = 0u; i < SPLIT; i++) { w[i] = weights128[(rows[i] + k) >> 3u]; } for (uint t = 0u; t < 8u; t++) { vec4 x = vec4(arena_quad[quad + (k + t) * quads]); for (uint i = 0u; i < SPLIT; i++) { float tap = unpackHalf2x16(w[i][t >> 1u])[t & 1u]; sum[i] += tap * x; } } } for (uint i = 0u; i < SPLIT; 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++) { uint position = position_base + j; if (position >= positions) { continue; } uint index = channel * positions + position; float value = fused_store(sum[i][j] + offset, p.act, channel, index); arena[p.out_at + index] = float16_t(value); } } } void main() { if (p.in1 == 1u) { split_rows(); 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; if (position_base >= positions) { return; } // Rows past the last channel read the last row; never stored. uint rows[BLOCK]; vec4 sum[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { rows[i] = p.weight + min(channel_base + i, p.out_c - 1u) * p.in_c; sum[i] = vec4(0.0); } bool wide = (p.in_c & 7u) == 0u && (p.weight & 7u) == 0u && (positions & 3u) == 0u && (p.in0 & 3u) == 0u; if (wide) { uint quads = positions >> 2u; uint quad = (p.in0 + position_base) >> 2u; for (uint k = 0u; k < p.in_c; k += 8u) { uvec4 w[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { w[i] = weights128[(rows[i] + k) >> 3u]; } for (uint t = 0u; t < 8u; t++) { vec4 x = vec4(arena_quad[quad + (k + t) * quads]); for (uint i = 0u; i < BLOCK; i++) { float tap = unpackHalf2x16(w[i][t >> 1u])[t & 1u]; sum[i] += tap * x; } } } } else { // Columns past the last position read as zero and are never stored. bvec4 live = lessThan(uvec4(position_base) + uvec4(0u, 1u, 2u, 3u), uvec4(positions)); for (uint k = 0u; k < p.in_c; k++) { uint at = p.in0 + k * positions + position_base; vec4 x; for (uint j = 0u; j < BLOCK; j++) { x[j] = live[j] ? float(arena[at + j]) : 0.0; } for (uint i = 0u; i < BLOCK; i++) { sum[i] += float(weights[rows[i] + k]) * x; } } } 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++) { uint position = position_base + j; if (position >= positions) { continue; } uint index = channel * positions + position; float value = fused_store(sum[i][j] + offset, p.act, channel, index); arena[p.out_at + index] = float16_t(value); } } }