Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / attn_apply.comp
attn_apply.comp173 lines · 7.0 KiBRaw
| 1 | #version 450 |
| 2 | //! `O[h][d][i] = sum_j S[h][i][j] * V[h][d][j]`, the second half of attention. |
| 3 | //! |
| 4 | //! Two uses per transformer block in PP-OCRv5 recognition, and Supertonic's text encoder mixes |
| 5 | //! 50 style tokens with it. |
| 6 | //! |
| 7 | //! # Queries and keys may be different counts |
| 8 | //! |
| 9 | //! `S` is `[heads, queries, keys]` from `softmax.comp`, `V` is `[d_model, 1, keys]` and the |
| 10 | //! output is `[d_model, 1, queries]`. `p.out_w` is the query count and `p.in_w` the key count; |
| 11 | //! for self-attention they are equal and this is the arithmetic it always was. |
| 12 | //! |
| 13 | //! # Back into the layout it started in |
| 14 | //! |
| 15 | //! `S` is `[heads, queries, keys]` from `softmax.comp`; `V` and the output share `d_model`, |
| 16 | //! the layout every projection in this runtime reads and writes. So the head |
| 17 | //! concatenation that follows attention is not an op: output channel `c` belongs to head |
| 18 | //! `c / head_dim` and is written in place, which *is* the concatenated result. No |
| 19 | //! `Reshape` and no `Permute` on this side either. |
| 20 | //! |
| 21 | //! One invocation per output element, reducing over the keys. Note that `V` needs no |
| 22 | //! head arithmetic at all - channel `c` of `V` is exactly channel `c` of the output - so |
| 23 | //! the head index is used only to pick which of `S`'s planes to read. |
| 24 | //! |
| 25 | //! # Precision |
| 26 | //! |
| 27 | //! `S` is a distribution, so every product is bounded by the corresponding `V`, and the |
| 28 | //! sum of a convex combination cannot exceed `V`'s range. Accumulating in `float` like |
| 29 | //! everywhere else here is more than enough; the fp16 store at the end is the only |
| 30 | //! rounding. |
| 31 | //! |
| 32 | //! # Tiled mode |
| 33 | //! |
| 34 | //! With `p.kw == 1` each invocation owns four channels by four queries and a workgroup a 32 x 32 |
| 35 | //! block, reading both operands four keys at a time through a read-only `f16vec4` view: each pair |
| 36 | //! of quads is a `dot` per output. `nets` picks it when the key count and both bases are divisible |
| 37 | //! by four, and then `p.count` is the workgroup count. One invocation per output re-read every |
| 38 | //! row of `S` and `V` from memory per element; on a Pixel 8 that made whisper's six 1,500-key |
| 39 | //! applications 1.5 s of a 3.4 s encoder. |
| 40 | |
| 41 | #include "common.glsl" |
| 42 | |
| 43 | layout(std430, binding = 0) readonly buffer ArenaQuad { |
| 44 | f16vec4 arena_quad[]; |
| 45 | }; |
| 46 | |
| 47 | #define TILE 32u |
| 48 | #define BLOCK 4u |
| 49 | |
| 50 | /// The tiled mode for key counts or bases that are not multiples of four (`p.kw == 2`): the same |
| 51 | /// four channels by four queries per invocation through scalar loads. Edge channels and queries |
| 52 | /// are clamped on load and dropped on store. |
| 53 | void ragged(uint channel_base, uint query_base, uint queries, uint keys, uint head_dim) { |
| 54 | uint head = channel_base / head_dim; |
| 55 | uvec4 v_row; |
| 56 | uvec4 s_row; |
| 57 | for (uint i = 0u; i < BLOCK; i++) { |
| 58 | uint channel = min(channel_base + i, p.out_c - 1u); |
| 59 | v_row[i] = p.in1 + (kv_head_of(head) * head_dim + channel % head_dim) * keys; |
| 60 | uint query = min(query_base + i, queries - 1u); |
| 61 | s_row[i] = p.in0 + (head * queries + query) * keys; |
| 62 | } |
| 63 | mat4 acc = mat4(0.0); |
| 64 | // Causal (`p.kh` 1): keys past this block's last query hold exact zeros. |
| 65 | uint bound = p.kh == 1u ? min(keys, min(query_base + BLOCK, queries)) : keys; |
| 66 | for (uint j = 0u; j < bound; j++) { |
| 67 | 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])); |
| 68 | 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])); |
| 69 | // Column `q` of `acc` is query `q`: `acc[q][c] += v[c] * s[q]`. |
| 70 | acc += outerProduct(v, s); |
| 71 | } |
| 72 | for (uint i = 0u; i < BLOCK && channel_base + i < p.out_c; i++) { |
| 73 | for (uint j = 0u; j < BLOCK && query_base + j < queries; j++) { |
| 74 | arena[p.out_at + (channel_base + i) * queries + query_base + j] = float16_t(acc[j][i]); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void tiled() { |
| 80 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 81 | if (tile >= p.count) { |
| 82 | return; |
| 83 | } |
| 84 | uint queries = p.out_w; |
| 85 | uint keys = p.in_w; |
| 86 | uint head_dim = p.out_c / p.group; |
| 87 | uint tiles_across = (queries + TILE - 1u) / TILE; |
| 88 | uint lid = gl_LocalInvocationID.x; |
| 89 | uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; |
| 90 | uint query_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; |
| 91 | if (channel_base >= p.out_c || query_base >= queries) { |
| 92 | return; |
| 93 | } |
| 94 | if (p.kw == 2u) { |
| 95 | ragged(channel_base, query_base, queries, keys, head_dim); |
| 96 | return; |
| 97 | } |
| 98 | // `head_dim` is a multiple of four, so a block of four channels never spans two heads. |
| 99 | uint head = channel_base / head_dim; |
| 100 | uint quads = keys >> 2u; |
| 101 | if (p.kh == 1u) { |
| 102 | // Causal: keys past this block's last query hold exact zeros, so only `+ 0` is skipped. |
| 103 | quads = min(quads, (min(query_base + BLOCK, queries) + 3u) >> 2u); |
| 104 | } |
| 105 | uint value_row[BLOCK]; |
| 106 | uint prob_row[BLOCK]; |
| 107 | for (uint i = 0u; i < BLOCK; i++) { |
| 108 | uint channel = min(channel_base + i, p.out_c - 1u); |
| 109 | value_row[i] = ((p.in1 + (kv_head_of(head) * head_dim + channel % head_dim) * keys) >> 2u); |
| 110 | uint query = min(query_base + i, queries - 1u); |
| 111 | prob_row[i] = ((p.in0 + (head * queries + query) * keys) >> 2u); |
| 112 | } |
| 113 | float acc[BLOCK][BLOCK]; |
| 114 | for (uint i = 0u; i < BLOCK; i++) { |
| 115 | for (uint j = 0u; j < BLOCK; j++) { |
| 116 | acc[i][j] = 0.0; |
| 117 | } |
| 118 | } |
| 119 | for (uint q = 0u; q < quads; q++) { |
| 120 | vec4 v[BLOCK]; |
| 121 | vec4 s[BLOCK]; |
| 122 | for (uint i = 0u; i < BLOCK; i++) { |
| 123 | v[i] = vec4(arena_quad[value_row[i] + q]); |
| 124 | s[i] = vec4(arena_quad[prob_row[i] + q]); |
| 125 | } |
| 126 | for (uint i = 0u; i < BLOCK; i++) { |
| 127 | for (uint j = 0u; j < BLOCK; j++) { |
| 128 | acc[i][j] += dot(v[i], s[j]); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | for (uint i = 0u; i < BLOCK; i++) { |
| 133 | uint channel = channel_base + i; |
| 134 | if (channel >= p.out_c) { |
| 135 | break; |
| 136 | } |
| 137 | for (uint j = 0u; j < BLOCK; j++) { |
| 138 | uint query = query_base + j; |
| 139 | if (query >= queries) { |
| 140 | break; |
| 141 | } |
| 142 | arena[p.out_at + channel * queries + query] = float16_t(acc[i][j]); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void main() { |
| 148 | if (p.kw != 0u) { |
| 149 | tiled(); |
| 150 | return; |
| 151 | } |
| 152 | uint index = global_index(); |
| 153 | if (index >= p.count) { |
| 154 | return; |
| 155 | } |
| 156 | // The output is `[d_model, 1, queries]`, so this is (channel, 0, query). |
| 157 | uint channel, unused_row, query; |
| 158 | unpack(index, channel, unused_row, query); |
| 159 | uint queries = p.out_w; |
| 160 | uint keys = p.in_w; |
| 161 | uint head_dim = p.out_c / p.group; |
| 162 | uint head = channel / head_dim; |
| 163 | // `S[head][query][*]`, contiguous in the key index. |
| 164 | uint row = p.in0 + (head * queries + query) * keys; |
| 165 | // `V`'s channel for this query head, which under multi-query attention is shared. |
| 166 | uint values = p.in1 + (kv_head_of(head) * head_dim + (channel % head_dim)) * keys; |
| 167 | |
| 168 | float total = 0.0; |
| 169 | for (uint j = 0u; j < keys; j++) { |
| 170 | total += float(arena[row + j]) * float(arena[values + j]); |
| 171 | } |
| 172 | arena[p.out_at + index] = float16_t(total); |
| 173 | } |