Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / conv_point_int8.comp
conv_point_int8.comp395 lines · 15.9 KiBRaw
| 1 | #version 450 |
| 2 | //! A `1 x 1` convolution with int8 weights over a sequence: an int8-by-fp16 matrix product. |
| 3 | //! |
| 4 | //! `out[o][p] = act(scale[o] * sum_i W[o][i] * in[i][p] + bias[o])`, `p` over positions. |
| 5 | //! |
| 6 | //! # Why this exists as well as `conv_int8.comp` |
| 7 | //! |
| 8 | //! `conv_int8.comp` is shaped like `conv.comp`: one invocation per output element, each reading |
| 9 | //! its own weight row from global memory with no reuse. That is survivable for a spatial kernel |
| 10 | //! and catastrophic for a wide `1 x 1` over a sequence, which is every encoder projection and every |
| 11 | //! prefill: the weight row is re-read once per position. |
| 12 | //! |
| 13 | //! # Register blocking, not shared-memory tiling |
| 14 | //! |
| 15 | //! Each invocation owns a 4 x 4 block - four output channels by four positions - and the 64 of a |
| 16 | //! workgroup tile a 32 x 32 block of the output, eight across and eight down. Every weight word read |
| 17 | //! feeds four positions and every activation read feeds four channels, which is the whole of the |
| 18 | //! reuse; it is held in registers rather than staged through shared memory. |
| 19 | //! |
| 20 | //! The previous version staged 16 x 16 tiles of both operands through shared memory with two |
| 21 | //! barriers per 16 input channels. On Mali, shared memory is ordinary cached memory and a barrier |
| 22 | //! stalls the whole workgroup, so that bought no reuse the cache was not already giving and paid |
| 23 | //! for it 128 times per 1,024-wide projection: on a Pixel 8 it reached 9-16 GMAC/s, which made it |
| 24 | //! 98% of NLLB's encoder and a third of whisper's. |
| 25 | //! |
| 26 | //! # Four input channels per weight load |
| 27 | //! |
| 28 | //! Int8 weights pack four to a 32-bit word along the input axis, so one load from the word view |
| 29 | //! gives four taps of one output channel. That needs every row to start on a word, which is |
| 30 | //! `in_c % 4 == 0`; every projection in every net here satisfies it, and the rare one that does not |
| 31 | //! takes the element-at-a-time loop instead. |
| 32 | //! |
| 33 | //! # The scale |
| 34 | //! |
| 35 | //! Per **output channel**, so each of an invocation's four rows carries one scale applied to its |
| 36 | //! finished accumulators. `p.weight` is a **32-bit word** offset, as in `conv_int8.comp`; |
| 37 | //! `p.act_weight` holds the scale tensor as an fp16 element index, which is why `Act::PRelu` |
| 38 | //! cannot ride along. |
| 39 | //! |
| 40 | //! # Wide loads |
| 41 | //! |
| 42 | //! When `in_c % 16 == 0` weights are read sixteen taps at a time through the `uvec4` view, at any |
| 43 | //! width. Activations come four positions at a time through a read-only `f16vec4` view of the |
| 44 | //! arena when the position count is a multiple of four (every encoder and prefill here), and as |
| 45 | //! four scalar reads otherwise (Supertonic's sampler runs at whatever frame count the duration |
| 46 | //! predictor gives). The narrow paths below remain for `in_c % 16 != 0`. |
| 47 | |
| 48 | #include "common.glsl" |
| 49 | |
| 50 | /// The arena again, four fp16 elements per entry, for the wide activation loads. |
| 51 | /// |
| 52 | /// The same binding as `arena`; reads only. The aligned path reads only the input tensor; the |
| 53 | /// ragged path's quads can overhang it by up to three elements each side, which only ever land in |
| 54 | /// lanes that are shifted out or belong to dead columns, so a concurrent write there is harmless. |
| 55 | /// Indexing needs the element offset divisible by four, which the arena's 16-byte allocation |
| 56 | /// alignment and a position count divisible by four together give on the aligned path. |
| 57 | layout(std430, binding = 0) readonly buffer ArenaQuad { |
| 58 | f16vec4 arena_quad[]; |
| 59 | }; |
| 60 | |
| 61 | /// Output channels and positions per workgroup: 8 x 8 invocations of 4 x 4 each. |
| 62 | /// |
| 63 | /// **Must equal `CONV_POINT_INT8_TILE` in `nets`**, which counts the workgroups. |
| 64 | #define TILE 32u |
| 65 | |
| 66 | /// Channels and positions per invocation. |
| 67 | #define BLOCK 4u |
| 68 | |
| 69 | /// Channels per invocation on the split path (`p.in1 == 1`). |
| 70 | #define SPLIT 2u |
| 71 | |
| 72 | /// The wide path with two channels per invocation instead of four, for GEMMs whose 32 x 32 tiles |
| 73 | /// are too few to fill the GPU (Supertonic's sampler: 512 x 52 is 32 workgroups). Twice the |
| 74 | /// workgroups, each 16 channels x 32 positions. Every output sums the same terms in the same order |
| 75 | /// as the four-row path, so the result is bit-identical. Selected only when `in_c % 16 == 0` and |
| 76 | /// the weights are 16-byte aligned (see `nets`). |
| 77 | void split_rows() { |
| 78 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 79 | if (tile >= p.count) { |
| 80 | return; |
| 81 | } |
| 82 | uint positions = p.out_h * p.out_w; |
| 83 | uint tiles_across = (positions + TILE - 1u) / TILE; |
| 84 | uint lid = gl_LocalInvocationID.x; |
| 85 | uint channel_base = (tile / tiles_across) * (TILE / BLOCK * SPLIT) + (lid >> 3u) * SPLIT; |
| 86 | uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; |
| 87 | if (position_base >= positions) { |
| 88 | return; |
| 89 | } |
| 90 | uint rows16[SPLIT]; |
| 91 | for (uint i = 0u; i < SPLIT; i++) { |
| 92 | uint row = min(channel_base + i, p.out_c - 1u); |
| 93 | rows16[i] = (p.weight + ((row * p.in_c) >> 2u)) >> 2u; |
| 94 | } |
| 95 | vec4 sum[SPLIT]; |
| 96 | for (uint i = 0u; i < SPLIT; i++) { |
| 97 | sum[i] = vec4(0.0); |
| 98 | } |
| 99 | if ((positions & 3u) == 0u && (p.in0 & 3u) == 0u) { |
| 100 | uint quads = positions >> 2u; |
| 101 | uint quad = (p.in0 + position_base) >> 2u; |
| 102 | for (uint k = 0u; k < p.in_c; k += 16u) { |
| 103 | uvec4 w[SPLIT]; |
| 104 | for (uint i = 0u; i < SPLIT; i++) { |
| 105 | w[i] = weights128[rows16[i] + (k >> 4u)]; |
| 106 | } |
| 107 | for (uint t = 0u; t < 16u; t++) { |
| 108 | vec4 x = vec4(arena_quad[quad + (k + t) * quads]); |
| 109 | for (uint i = 0u; i < SPLIT; i++) { |
| 110 | float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); |
| 111 | sum[i] += tap * x; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } else { |
| 116 | uint first = p.in0 + position_base; |
| 117 | uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u; |
| 118 | for (uint k = 0u; k < p.in_c; k += 16u) { |
| 119 | uvec4 w[SPLIT]; |
| 120 | for (uint i = 0u; i < SPLIT; i++) { |
| 121 | w[i] = weights128[rows16[i] + (k >> 4u)]; |
| 122 | } |
| 123 | for (uint t = 0u; t < 16u; t++) { |
| 124 | uint at = first + (k + t) * positions; |
| 125 | uint q = at >> 2u; |
| 126 | uint s = at & 3u; |
| 127 | f16vec4 a = arena_quad[q]; |
| 128 | f16vec4 b = arena_quad[min(q + 1u, last_quad)]; |
| 129 | f16vec4 h = s == 0u ? a |
| 130 | : s == 1u ? f16vec4(a.yzw, b.x) |
| 131 | : s == 2u ? f16vec4(a.zw, b.xy) |
| 132 | : f16vec4(a.w, b.xyz); |
| 133 | vec4 x = vec4(h); |
| 134 | for (uint i = 0u; i < SPLIT; i++) { |
| 135 | float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); |
| 136 | sum[i] += tap * x; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | for (uint i = 0u; i < SPLIT; i++) { |
| 142 | uint channel = channel_base + i; |
| 143 | if (channel >= p.out_c) { |
| 144 | continue; |
| 145 | } |
| 146 | float scale = float(weights[p.act_weight + channel]); |
| 147 | float offset = float(weights[p.bias + channel]); |
| 148 | for (uint j = 0u; j < BLOCK; j++) { |
| 149 | if (position_base + j >= positions) { |
| 150 | continue; |
| 151 | } |
| 152 | uint index = channel * positions + position_base + j; |
| 153 | float value = quant_store(fused_store(sum[i][j] * scale + offset, p.act, channel, index)); |
| 154 | arena[p.out_at + index] = float16_t(value); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// A one-row `1 x kw` convolution as this GEMM (`p.in1 == 2`): K walks (input channel, tap), |
| 160 | /// which is the weights' `[oc][ic][kx]` order, and the activation for position `x` and tap |
| 161 | /// `kx` is `in[ic][x * stride + kx * dil - pad_l]`, zero outside the row. Same summation order |
| 162 | /// as `conv_int8.comp`. Selected only when `in_c * kw % 16 == 0` and the weights are 16-byte |
| 163 | /// aligned (see `nets`). |
| 164 | void row_taps() { |
| 165 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 166 | if (tile >= p.count) { |
| 167 | return; |
| 168 | } |
| 169 | uint positions = p.out_w; |
| 170 | uint tiles_across = (positions + TILE - 1u) / TILE; |
| 171 | uint lid = gl_LocalInvocationID.x; |
| 172 | uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; |
| 173 | uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; |
| 174 | if (position_base >= positions) { |
| 175 | return; |
| 176 | } |
| 177 | uint taps = p.in_c * p.kw; |
| 178 | uint rows16[BLOCK]; |
| 179 | vec4 sum[BLOCK]; |
| 180 | for (uint i = 0u; i < BLOCK; i++) { |
| 181 | uint row = min(channel_base + i, p.out_c - 1u); |
| 182 | rows16[i] = (p.weight + ((row * taps) >> 2u)) >> 2u; |
| 183 | sum[i] = vec4(0.0); |
| 184 | } |
| 185 | ivec4 cols = (ivec4(position_base) + ivec4(0, 1, 2, 3)) * int(p.stride_w) - int(p.pad_l); |
| 186 | int width = int(p.in_w); |
| 187 | uint ic = 0u; |
| 188 | uint kx = 0u; |
| 189 | for (uint k = 0u; k < taps; k += 16u) { |
| 190 | uvec4 w[BLOCK]; |
| 191 | for (uint i = 0u; i < BLOCK; i++) { |
| 192 | w[i] = weights128[rows16[i] + (k >> 4u)]; |
| 193 | } |
| 194 | for (uint t = 0u; t < 16u; t++) { |
| 195 | ivec4 c = cols + int(kx * p.dil_w); |
| 196 | uint row = p.in0 + ic * p.in_w; |
| 197 | vec4 x; |
| 198 | for (uint j = 0u; j < BLOCK; j++) { |
| 199 | x[j] = (c[j] >= 0 && c[j] < width) ? float(arena[row + uint(c[j])]) : 0.0; |
| 200 | } |
| 201 | for (uint i = 0u; i < BLOCK; i++) { |
| 202 | float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); |
| 203 | sum[i] += tap * x; |
| 204 | } |
| 205 | kx++; |
| 206 | if (kx == p.kw) { |
| 207 | kx = 0u; |
| 208 | ic++; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | for (uint i = 0u; i < BLOCK; i++) { |
| 213 | uint channel = channel_base + i; |
| 214 | if (channel >= p.out_c) { |
| 215 | continue; |
| 216 | } |
| 217 | float scale = float(weights[p.act_weight + channel]); |
| 218 | float offset = float(weights[p.bias + channel]); |
| 219 | for (uint j = 0u; j < BLOCK; j++) { |
| 220 | if (position_base + j >= positions) { |
| 221 | continue; |
| 222 | } |
| 223 | uint index = channel * positions + position_base + j; |
| 224 | float value = quant_store(fused_store(sum[i][j] * scale + offset, p.act, channel, index)); |
| 225 | arena[p.out_at + index] = float16_t(value); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | #include "conv_point_widened.glsl" |
| 231 | |
| 232 | void main() { |
| 233 | if (p.in1 == 1u) { |
| 234 | split_rows(); |
| 235 | return; |
| 236 | } |
| 237 | if (p.in1 == 3u) { |
| 238 | widened_block(); |
| 239 | return; |
| 240 | } |
| 241 | if (p.in1 == 4u) { |
| 242 | widened_split(); |
| 243 | return; |
| 244 | } |
| 245 | if (p.in1 == 2u) { |
| 246 | row_taps(); |
| 247 | return; |
| 248 | } |
| 249 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 250 | if (tile >= p.count) { |
| 251 | return; |
| 252 | } |
| 253 | uint positions = p.out_h * p.out_w; |
| 254 | uint tiles_across = (positions + TILE - 1u) / TILE; |
| 255 | uint lid = gl_LocalInvocationID.x; |
| 256 | uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; |
| 257 | uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; |
| 258 | |
| 259 | // Rows past the last channel read the last row instead, so the inner loop needs no guard; |
| 260 | // their results are discarded at the store. |
| 261 | uint rows[BLOCK]; |
| 262 | for (uint i = 0u; i < BLOCK; i++) { |
| 263 | rows[i] = min(channel_base + i, p.out_c - 1u); |
| 264 | } |
| 265 | // Columns past the last position are read as zero and never stored. |
| 266 | bool live[BLOCK]; |
| 267 | for (uint j = 0u; j < BLOCK; j++) { |
| 268 | live[j] = position_base + j < positions; |
| 269 | } |
| 270 | |
| 271 | float acc[BLOCK][BLOCK]; |
| 272 | for (uint i = 0u; i < BLOCK; i++) { |
| 273 | for (uint j = 0u; j < BLOCK; j++) { |
| 274 | acc[i][j] = 0.0; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ((p.in_c & 15u) == 0u && (p.weight & 3u) == 0u) { |
| 279 | if (position_base >= positions) { |
| 280 | return; |
| 281 | } |
| 282 | uint rows16[BLOCK]; |
| 283 | for (uint i = 0u; i < BLOCK; i++) { |
| 284 | rows16[i] = (p.weight + ((rows[i] * p.in_c) >> 2u)) >> 2u; |
| 285 | } |
| 286 | vec4 sum[BLOCK]; |
| 287 | for (uint i = 0u; i < BLOCK; i++) { |
| 288 | sum[i] = vec4(0.0); |
| 289 | } |
| 290 | if ((positions & 3u) == 0u && (p.in0 & 3u) == 0u) { |
| 291 | uint quads = positions >> 2u; |
| 292 | uint quad = (p.in0 + position_base) >> 2u; |
| 293 | for (uint k = 0u; k < p.in_c; k += 16u) { |
| 294 | uvec4 w[BLOCK]; |
| 295 | for (uint i = 0u; i < BLOCK; i++) { |
| 296 | w[i] = weights128[rows16[i] + (k >> 4u)]; |
| 297 | } |
| 298 | for (uint t = 0u; t < 16u; t++) { |
| 299 | vec4 x = vec4(arena_quad[quad + (k + t) * quads]); |
| 300 | for (uint i = 0u; i < BLOCK; i++) { |
| 301 | float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); |
| 302 | sum[i] += tap * x; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } else { |
| 307 | // Ragged width: a row's four positions straddle two quads. Load both and shift; the |
| 308 | // shift is the same for every invocation. The second quad is clamped to the tensor's |
| 309 | // last one, which only ever substitutes for dead columns, never stored. |
| 310 | uint first = p.in0 + position_base; |
| 311 | uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u; |
| 312 | for (uint k = 0u; k < p.in_c; k += 16u) { |
| 313 | uvec4 w[BLOCK]; |
| 314 | for (uint i = 0u; i < BLOCK; i++) { |
| 315 | w[i] = weights128[rows16[i] + (k >> 4u)]; |
| 316 | } |
| 317 | for (uint t = 0u; t < 16u; t++) { |
| 318 | uint at = first + (k + t) * positions; |
| 319 | uint q = at >> 2u; |
| 320 | uint s = at & 3u; |
| 321 | f16vec4 a = arena_quad[q]; |
| 322 | f16vec4 b = arena_quad[min(q + 1u, last_quad)]; |
| 323 | f16vec4 h = s == 0u ? a |
| 324 | : s == 1u ? f16vec4(a.yzw, b.x) |
| 325 | : s == 2u ? f16vec4(a.zw, b.xy) |
| 326 | : f16vec4(a.w, b.xyz); |
| 327 | vec4 x = vec4(h); |
| 328 | for (uint i = 0u; i < BLOCK; i++) { |
| 329 | float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8)); |
| 330 | sum[i] += tap * x; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | for (uint i = 0u; i < BLOCK; i++) { |
| 336 | for (uint j = 0u; j < BLOCK; j++) { |
| 337 | acc[i][j] = sum[i][j]; |
| 338 | } |
| 339 | } |
| 340 | } else if ((p.in_c & 3u) == 0u) { |
| 341 | uint words[BLOCK]; |
| 342 | for (uint i = 0u; i < BLOCK; i++) { |
| 343 | words[i] = p.weight + ((rows[i] * p.in_c) >> 2u); |
| 344 | } |
| 345 | for (uint k = 0u; k < p.in_c; k += 4u) { |
| 346 | float x[4][BLOCK]; |
| 347 | for (uint t = 0u; t < 4u; t++) { |
| 348 | uint at = p.in0 + (k + t) * positions + position_base; |
| 349 | for (uint j = 0u; j < BLOCK; j++) { |
| 350 | x[t][j] = live[j] ? float(arena[at + j]) : 0.0; |
| 351 | } |
| 352 | } |
| 353 | for (uint i = 0u; i < BLOCK; i++) { |
| 354 | int word = int(weights32[words[i] + (k >> 2u)]); |
| 355 | for (uint t = 0u; t < 4u; t++) { |
| 356 | float w = float(bitfieldExtract(word, int(t << 3u), 8)); |
| 357 | for (uint j = 0u; j < BLOCK; j++) { |
| 358 | acc[i][j] += w * x[t][j]; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } else { |
| 364 | for (uint k = 0u; k < p.in_c; k++) { |
| 365 | uint at = p.in0 + k * positions + position_base; |
| 366 | float x[BLOCK]; |
| 367 | for (uint j = 0u; j < BLOCK; j++) { |
| 368 | x[j] = live[j] ? float(arena[at + j]) : 0.0; |
| 369 | } |
| 370 | for (uint i = 0u; i < BLOCK; i++) { |
| 371 | float w = float(int8_at(p.weight, rows[i] * p.in_c + k)); |
| 372 | for (uint j = 0u; j < BLOCK; j++) { |
| 373 | acc[i][j] += w * x[j]; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | for (uint i = 0u; i < BLOCK; i++) { |
| 380 | uint channel = channel_base + i; |
| 381 | if (channel >= p.out_c) { |
| 382 | continue; |
| 383 | } |
| 384 | float scale = float(weights[p.act_weight + channel]); |
| 385 | float offset = float(weights[p.bias + channel]); |
| 386 | for (uint j = 0u; j < BLOCK; j++) { |
| 387 | if (!live[j]) { |
| 388 | continue; |
| 389 | } |
| 390 | uint index = channel * positions + position_base + j; |
| 391 | float value = quant_store(fused_store(acc[i][j] * scale + offset, p.act, channel, index)); |
| 392 | arena[p.out_at + index] = float16_t(value); |
| 393 | } |
| 394 | } |
| 395 | } |