#version 450 //! `O[h][d][i] = sum_j S[h][i][j] * V[h][d][j]`, the second half of attention. //! //! Two uses per transformer block in PP-OCRv5 recognition, and Supertonic's text encoder mixes //! 50 style tokens with it. //! //! # Queries and keys may be different counts //! //! `S` is `[heads, queries, keys]` from `softmax.comp`, `V` is `[d_model, 1, keys]` and the //! output is `[d_model, 1, queries]`. `p.out_w` is the query count and `p.in_w` the key count; //! for self-attention they are equal and this is the arithmetic it always was. //! //! # Back into the layout it started in //! //! `S` is `[heads, queries, keys]` from `softmax.comp`; `V` and the output share `d_model`, //! the layout every projection in this runtime reads and writes. So the head //! concatenation that follows attention is not an op: output channel `c` belongs to head //! `c / head_dim` and is written in place, which *is* the concatenated result. No //! `Reshape` and no `Permute` on this side either. //! //! One invocation per output element, reducing over the keys. Note that `V` needs no //! head arithmetic at all - channel `c` of `V` is exactly channel `c` of the output - so //! the head index is used only to pick which of `S`'s planes to read. //! //! # Precision //! //! `S` is a distribution, so every product is bounded by the corresponding `V`, and the //! sum of a convex combination cannot exceed `V`'s range. Accumulating in `float` like //! everywhere else here is more than enough; the fp16 store at the end is the only //! rounding. //! //! # Tiled mode //! //! With `p.kw == 1` each invocation owns four channels by four queries and a workgroup a 32 x 32 //! block, reading both operands four keys at a time through a read-only `f16vec4` view: each pair //! of quads is a `dot` per output. `nets` picks it when the key count and both bases are divisible //! by four, and then `p.count` is the workgroup count. One invocation per output re-read every //! row of `S` and `V` from memory per element; on a Pixel 8 that made whisper's six 1,500-key //! applications 1.5 s of a 3.4 s encoder. #include "common.glsl" layout(std430, binding = 0) readonly buffer ArenaQuad { f16vec4 arena_quad[]; }; #define TILE 32u #define BLOCK 4u /// The tiled mode for key counts or bases that are not multiples of four (`p.kw == 2`): the same /// four channels by four queries per invocation through scalar loads. Edge channels and queries /// are clamped on load and dropped on store. void ragged(uint channel_base, uint query_base, uint queries, uint keys, uint head_dim) { uint head = channel_base / head_dim; uvec4 v_row; uvec4 s_row; for (uint i = 0u; i < BLOCK; i++) { uint channel = min(channel_base + i, p.out_c - 1u); v_row[i] = p.in1 + (kv_head_of(head) * head_dim + channel % head_dim) * keys; uint query = min(query_base + i, queries - 1u); s_row[i] = p.in0 + (head * queries + query) * keys; } mat4 acc = mat4(0.0); // Causal (`p.kh` 1): keys past this block's last query hold exact zeros. uint bound = p.kh == 1u ? min(keys, min(query_base + BLOCK, queries)) : keys; for (uint j = 0u; j < bound; j++) { vec4 v = vec4(float(arena[v_row.x + j]), float(arena[v_row.y + j]), float(arena[v_row.z + j]), float(arena[v_row.w + j])); vec4 s = vec4(float(arena[s_row.x + j]), float(arena[s_row.y + j]), float(arena[s_row.z + j]), float(arena[s_row.w + j])); // Column `q` of `acc` is query `q`: `acc[q][c] += v[c] * s[q]`. acc += outerProduct(v, s); } for (uint i = 0u; i < BLOCK && channel_base + i < p.out_c; i++) { for (uint j = 0u; j < BLOCK && query_base + j < queries; j++) { arena[p.out_at + (channel_base + i) * queries + query_base + j] = float16_t(acc[j][i]); } } } void tiled() { uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint queries = p.out_w; uint keys = p.in_w; uint head_dim = p.out_c / p.group; uint tiles_across = (queries + TILE - 1u) / TILE; uint lid = gl_LocalInvocationID.x; uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; uint query_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; if (channel_base >= p.out_c || query_base >= queries) { return; } if (p.kw == 2u) { ragged(channel_base, query_base, queries, keys, head_dim); return; } // `head_dim` is a multiple of four, so a block of four channels never spans two heads. uint head = channel_base / head_dim; uint quads = keys >> 2u; if (p.kh == 1u) { // Causal: keys past this block's last query hold exact zeros, so only `+ 0` is skipped. quads = min(quads, (min(query_base + BLOCK, queries) + 3u) >> 2u); } uint value_row[BLOCK]; uint prob_row[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { uint channel = min(channel_base + i, p.out_c - 1u); value_row[i] = ((p.in1 + (kv_head_of(head) * head_dim + channel % head_dim) * keys) >> 2u); uint query = min(query_base + i, queries - 1u); prob_row[i] = ((p.in0 + (head * queries + query) * keys) >> 2u); } float acc[BLOCK][BLOCK]; for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { acc[i][j] = 0.0; } } for (uint q = 0u; q < quads; q++) { vec4 v[BLOCK]; vec4 s[BLOCK]; for (uint i = 0u; i < BLOCK; i++) { v[i] = vec4(arena_quad[value_row[i] + q]); s[i] = vec4(arena_quad[prob_row[i] + q]); } for (uint i = 0u; i < BLOCK; i++) { for (uint j = 0u; j < BLOCK; j++) { acc[i][j] += dot(v[i], s[j]); } } } for (uint i = 0u; i < BLOCK; i++) { uint channel = channel_base + i; if (channel >= p.out_c) { break; } for (uint j = 0u; j < BLOCK; j++) { uint query = query_base + j; if (query >= queries) { break; } arena[p.out_at + channel * queries + query] = float16_t(acc[i][j]); } } } void main() { if (p.kw != 0u) { tiled(); return; } uint index = global_index(); if (index >= p.count) { return; } // The output is `[d_model, 1, queries]`, so this is (channel, 0, query). uint channel, unused_row, query; unpack(index, channel, unused_row, query); uint queries = p.out_w; uint keys = p.in_w; uint head_dim = p.out_c / p.group; uint head = channel / head_dim; // `S[head][query][*]`, contiguous in the key index. uint row = p.in0 + (head * queries + query) * keys; // `V`'s channel for this query head, which under multi-query attention is shared. uint values = p.in1 + (kv_head_of(head) * head_dim + (channel % head_dim)) * keys; float total = 0.0; for (uint j = 0u; j < keys; j++) { total += float(arena[row + j]) * float(arena[values + j]); } arena[p.out_at + index] = float16_t(total); }