#version 450 //! `conv_point_int8.comp` for a **single position**: a matrix-vector product, not a tiny matrix. //! //! `out[o] = act(scale[o] * sum_i W[o][i] * x[i] + bias[o])`. //! //! # Why the tiled shader is the wrong shape here //! //! `conv_point_int8.comp` tiles `16 x 16` over output channels and positions, so its workgroup //! count is `out_c.div_ceil(16) * positions.div_ceil(16)`. At **one** position the second factor is //! 1 and the tile is 16 channels by 16 positions of which 15 are padding, so: //! //! * Of 64 invocations, the 56 whose `local_col` is not 0 compute nothing that is stored. The //! workgroup produces 16 outputs where it is built to produce 256. //! * Each staging step zero-fills 240 of the 256 `input_tile` slots, and the activation vector is //! re-staged once per channel tile — 16 times over for a 256-channel output. //! //! That is roughly a 16x waste, and it lands on exactly the wrong op. A SMaLL-100 decode step is //! *entirely* single-position: 30 projections and the 128,112-class head, 128 times per //! translation, with the head alone at 131 million multiply-accumulates. Nothing else in a //! translation is close. //! //! # The shape that fits //! //! One workgroup per **group of [`ROWS`] output channels**, with the 64 lanes striding along the //! input channels: //! //! ```text //! for (k = lane; k < in_c; k += 64) // lane's slice of the activation //! x = arena[in0 + k] // read once, into a register //! for (r = 0; r < ROWS; r++) // reused by every row //! acc[r] += W[channel_base + r][k] * x //! ``` //! //! Three things fall out: //! //! * **Every lane contributes to every output.** No idle lanes and no padding. //! * **The weight reads are coalesced.** Lane `l` reads `W[c][k0 + l]`, which is contiguous, and //! four adjacent lanes fall in one 32-bit word of the int8 view. //! * **The activation is read exactly once per workgroup** and reused from a register across all //! `ROWS` rows, rather than staged into shared memory `out_c / 16` times over. That is why there //! is no activation tile here: at one position a lane needs only the elements it strides over. //! //! What it costs is a cross-lane reduction, which the tiled shader does not need because each of //! its invocations owns a whole dot product. [`partial`] is the only shared memory, `ROWS * 64` //! floats, and lanes `0 .. ROWS` finish the sums. At 2 KB that is an eighth of the 16 KB //! `maxComputeSharedMemorySize` the spec guarantees. //! //! # Precision and the push block //! //! Identical to `conv_point_int8.comp`, deliberately: `nets::reference` serves all three int8 kinds //! from one implementation, so any difference here would be a difference from the oracle. fp32 //! accumulation, the scale applied to the finished accumulator, then the bias, then the activation. //! `p.weight` is a **32-bit word** offset and `p.act_weight` the scale as an fp16 element index, //! which is why `Act::PRelu` cannot ride along. //! //! # Wide loads //! //! When `in_c % 16 == 0` and the weight and activation bases are 16-byte aligned - every //! projection and head here - each lane reads sixteen taps per row with one `uvec4` load and the //! matching sixteen activations with four `f16vec4` loads, reused across the rows. The byte-per-lane //! loop below it issued one load per tap per row; on a Pixel 8 it streamed NLLB's decode weights at //! 15 GB/s, 85% of a step. // See the tail of `main`, and `conv_vec_int4.comp` which does the same. #extension GL_KHR_shader_subgroup_basic : require #extension GL_KHR_shader_subgroup_arithmetic : require #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 per workgroup. /// /// Eight rather than one because a workgroup per output channel would spend 64 lanes on a single /// dot product and reduce 64 partials for one number; eight amortises the reduction's barrier over /// eight outputs while keeping [`partial`] at 2 KB. It also bounds the register pressure at eight /// accumulators, which is what lets the activation stay in a register across the row loop. // Output channels one workgroup owns. // // # Why this is small // // Eight meant a 1,536-wide projection launched 192 workgroups - about 12k threads, barely over a // Mali G715's capacity, so there was nothing queued to run while a workgroup waited on memory. // The logits head launches 32,768 and reaches 14.9 GB/s; the per-layer projections reached 3.6. // Two gives four times the workgroups for the same weight traffic, at the cost of re-reading the // 3 KB activation vector four times more often - which is cache-resident and cheap beside the // megabytes of weights each one streams. #define ROWS 2u /// Per-lane partial sums, one row of 64 per output channel. shared float partial[ROWS][64]; void main() { // The channel group this workgroup owns. Uniform across the workgroup, so an early return // cannot strand a barrier — every invocation takes the same branch. uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint channel_base = tile * ROWS; uint lane = gl_LocalInvocationID.x; float acc[ROWS]; for (uint r = 0u; r < ROWS; r++) { acc[r] = 0.0; } // Rows past the output are accumulated anyway and discarded at the store. Skipping them would // branch inside the hot loop for at most seven channels of the last workgroup. bool wide = (p.in_c & 15u) == 0u && (p.weight & 3u) == 0u && (p.in0 & 3u) == 0u; if (wide) { uint row16[ROWS]; for (uint r = 0u; r < ROWS; r++) { uint channel = min(channel_base + r, p.out_c - 1u); row16[r] = (p.weight + ((channel * p.in_c) >> 2u)) >> 2u; } uint quad0 = p.in0 >> 2u; for (uint k = lane * 16u; k < p.in_c; k += 1024u) { vec4 x0 = vec4(arena_quad[quad0 + (k >> 2u)]); vec4 x1 = vec4(arena_quad[quad0 + (k >> 2u) + 1u]); vec4 x2 = vec4(arena_quad[quad0 + (k >> 2u) + 2u]); vec4 x3 = vec4(arena_quad[quad0 + (k >> 2u) + 3u]); for (uint r = 0u; r < ROWS; r++) { uvec4 w = weights128[row16[r] + (k >> 4u)]; acc[r] += dot(unpack_int8x4(w.x), x0) + dot(unpack_int8x4(w.y), x1) + dot(unpack_int8x4(w.z), x2) + dot(unpack_int8x4(w.w), x3); } } } else { for (uint k = lane; k < p.in_c; k += 64u) { float x = float(arena[p.in0 + k]); for (uint r = 0u; r < ROWS; r++) { acc[r] += float(int8_at(p.weight, (channel_base + r) * p.in_c + k)) * x; } } } // Reduce within each subgroup in registers, then across the handful a workgroup holds. // // The comment that stood here reasoned a log-step tree would cost six barriers to save 56 // adds, so the serial tail was fine. That weighed the wrong things: the cost was never the // adds but the **one** cross-subgroup barrier and the 56 idle lanes behind it, and a // subgroup reduction needs no barrier at all. Measured on a Mali G715, whose subgroup is 16 // wide, the per-layer projections reached 3.6 GB/s against the logits head's 14.9 - the head // being the one kernel with enough work per tile to hide this. for (uint r = 0u; r < ROWS; r++) { float summed = subgroupAdd(acc[r]); if (subgroupElect()) { partial[r][gl_SubgroupID] = summed; } } barrier(); if (lane >= ROWS) { return; } uint channel = channel_base + lane; if (channel >= p.out_c) { return; } float total = 0.0; for (uint i = 0u; i < gl_NumSubgroups; i++) { total += partial[lane][i]; } float scale = float(weights[p.act_weight + channel]); float offset = float(weights[p.bias + channel]); arena[p.out_at + channel] = float16_t(quant_store(fused_store(total * scale + offset, p.act, channel, channel))); }