Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / conv_point_int4.comp
conv_point_int4.comp293 lines · 11.2 KiBRaw
| 1 | #version 450 |
| 2 | //! `conv_point_int8.comp` for a **4-bit** kernel with **per-block** scales. |
| 3 | //! |
| 4 | //! Same register-blocked layout: a 32 x 32 output tile per workgroup, 4 channels x 4 positions |
| 5 | //! per lane, no shared memory. When `in_c` is a multiple of `I4_BLOCK` (32) and the weights are |
| 6 | //! 16-byte aligned, one `uvec4` load is a whole block of one row, the 32 taps of a block |
| 7 | //! accumulate unscaled, and the row's block scale multiplies the partial once. That is the |
| 8 | //! difference from the int8 shader, which scales the finished accumulator: a per-block scale has |
| 9 | //! to reach each block's sum. |
| 10 | //! |
| 11 | //! # Push fields |
| 12 | //! |
| 13 | //! As `conv_point_int8.comp`, with `p.act_weight` a rank-2 `(out_channels, blocks)` table instead |
| 14 | //! of a rank-1 per-row one. Dispatched with `CONV_POINT_INT8_TILE` tiles. |
| 15 | #include "common.glsl" |
| 16 | |
| 17 | /// The arena as quads of fp16, for the wide activation loads. Read-only, and never a range this |
| 18 | /// dispatch writes. |
| 19 | layout(std430, binding = 0) readonly buffer ArenaQuad { |
| 20 | f16vec4 arena_quad[]; |
| 21 | }; |
| 22 | |
| 23 | #define TILE 32u |
| 24 | #define BLOCK 4u |
| 25 | |
| 26 | /// The same arena as 16-byte groups of eight fp16, for `wide()` when the eight positions a lane |
| 27 | /// owns are one aligned group. |
| 28 | layout(std430, binding = 0) readonly buffer ArenaOct { |
| 29 | uvec4 arena_oct[]; |
| 30 | }; |
| 31 | |
| 32 | void store8(uint channel, uint position_base, uint positions, vec4 lo, vec4 hi) { |
| 33 | if (channel >= p.out_c) { |
| 34 | return; |
| 35 | } |
| 36 | float offset = float(weights[p.bias + channel]); |
| 37 | for (uint j = 0u; j < 8u; j++) { |
| 38 | uint position = position_base + j; |
| 39 | if (position >= positions) { |
| 40 | continue; |
| 41 | } |
| 42 | float acc = j < 4u ? lo[j] : hi[j - 4u]; |
| 43 | uint index = channel * positions + position; |
| 44 | arena[p.out_at + index] = float16_t(quant_store(fused_store(acc + offset, p.act, channel, index))); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// `p.in1 == 1` or `4`: 4 channels x 8 positions per lane over a 32 x 64 tile, chosen by the emit only |
| 49 | /// when `in_c % 32`, `p.weight % 4`, `positions % 4` and `p.in0 % 4` are all zero. Each nibble |
| 50 | /// unpack feeds 8 FMAs instead of 4, and the block scale is folded into the tap so the lane keeps |
| 51 | /// 32 accumulators and no per-block partials (scale per tap, not per block sum: rounding moves |
| 52 | /// within the int4 fixtures' tolerance). Weights come one word (8 taps) per row at a time rather |
| 53 | /// than a whole block up front: fewer live registers, 13% faster on a Pixel 8. `in1` 4 (positions |
| 54 | /// and `p.in0` % 8 == 0) reads the eight activations as one 16-byte load instead of two 8-byte |
| 55 | /// ones: ConvPointInt4 of a 200-token Gemma prefill 3.06 -> 2.25 s with both. |
| 56 | void wide() { |
| 57 | uint positions = p.out_h * p.out_w; |
| 58 | uint tiles_across = (positions + 63u) / 64u; |
| 59 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 60 | if (tile >= p.count) { |
| 61 | return; |
| 62 | } |
| 63 | uint lid = gl_LocalInvocationID.x; |
| 64 | uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; |
| 65 | uint position_base = (tile % tiles_across) * 64u + (lid & 7u) * 8u; |
| 66 | if (position_base >= positions) { |
| 67 | return; |
| 68 | } |
| 69 | uint blocks = p.in_c >> 5u; |
| 70 | uint rows[BLOCK]; |
| 71 | for (uint i = 0u; i < BLOCK; i++) { |
| 72 | rows[i] = min(channel_base + i, p.out_c - 1u); |
| 73 | } |
| 74 | vec4 lo[BLOCK]; |
| 75 | vec4 hi[BLOCK]; |
| 76 | for (uint i = 0u; i < BLOCK; i++) { |
| 77 | lo[i] = vec4(0.0); |
| 78 | hi[i] = vec4(0.0); |
| 79 | } |
| 80 | uint quads = positions >> 2u; |
| 81 | uint quad = (p.in0 + position_base) >> 2u; |
| 82 | bool second = position_base + 4u < positions; |
| 83 | for (uint block = 0u; block < blocks; block++) { |
| 84 | float scale[BLOCK]; |
| 85 | for (uint i = 0u; i < BLOCK; i++) { |
| 86 | scale[i] = int4_scale(rows[i], block, blocks); |
| 87 | } |
| 88 | for (uint word = 0u; word < 4u; word++) { |
| 89 | uint w[BLOCK]; |
| 90 | for (uint i = 0u; i < BLOCK; i++) { |
| 91 | w[i] = weights32[p.weight + (rows[i] * blocks + block) * 4u + word]; |
| 92 | } |
| 93 | uint k = block * I4_BLOCK + word * 8u; |
| 94 | for (uint t = 0u; t < 8u; t++) { |
| 95 | uint at = quad + (k + t) * quads; |
| 96 | vec4 x0 = vec4(arena_quad[at]); |
| 97 | vec4 x1 = second ? vec4(arena_quad[at + 1u]) : vec4(0.0); |
| 98 | for (uint i = 0u; i < BLOCK; i++) { |
| 99 | float tap = float(bitfieldExtract(int(w[i]), int(t << 2u), 4)) * scale[i]; |
| 100 | lo[i] += tap * x0; |
| 101 | hi[i] += tap * x1; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | for (uint i = 0u; i < BLOCK; i++) { |
| 107 | store8(channel_base + i, position_base, positions, lo[i], hi[i]); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// `wide()` with the 16-byte activation load (`in1` 4). |
| 112 | void wide_oct() { |
| 113 | uint positions = p.out_h * p.out_w; |
| 114 | uint tiles_across = (positions + 63u) / 64u; |
| 115 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 116 | if (tile >= p.count) { |
| 117 | return; |
| 118 | } |
| 119 | uint lid = gl_LocalInvocationID.x; |
| 120 | uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; |
| 121 | uint position_base = (tile % tiles_across) * 64u + (lid & 7u) * 8u; |
| 122 | if (position_base >= positions) { |
| 123 | return; |
| 124 | } |
| 125 | uint blocks = p.in_c >> 5u; |
| 126 | uint rows[BLOCK]; |
| 127 | for (uint i = 0u; i < BLOCK; i++) { |
| 128 | rows[i] = min(channel_base + i, p.out_c - 1u); |
| 129 | } |
| 130 | vec4 lo[BLOCK]; |
| 131 | vec4 hi[BLOCK]; |
| 132 | for (uint i = 0u; i < BLOCK; i++) { |
| 133 | lo[i] = vec4(0.0); |
| 134 | hi[i] = vec4(0.0); |
| 135 | } |
| 136 | uint octs = positions >> 3u; |
| 137 | uint oct = (p.in0 + position_base) >> 3u; |
| 138 | for (uint block = 0u; block < blocks; block++) { |
| 139 | float scale[BLOCK]; |
| 140 | for (uint i = 0u; i < BLOCK; i++) { |
| 141 | scale[i] = int4_scale(rows[i], block, blocks); |
| 142 | } |
| 143 | for (uint word = 0u; word < 4u; word++) { |
| 144 | uint w[BLOCK]; |
| 145 | for (uint i = 0u; i < BLOCK; i++) { |
| 146 | w[i] = weights32[p.weight + (rows[i] * blocks + block) * 4u + word]; |
| 147 | } |
| 148 | uint k = block * I4_BLOCK + word * 8u; |
| 149 | for (uint t = 0u; t < 8u; t++) { |
| 150 | uvec4 u = arena_oct[oct + (k + t) * octs]; |
| 151 | vec4 x0 = vec4(unpackHalf2x16(u.x), unpackHalf2x16(u.y)); |
| 152 | vec4 x1 = vec4(unpackHalf2x16(u.z), unpackHalf2x16(u.w)); |
| 153 | for (uint i = 0u; i < BLOCK; i++) { |
| 154 | float tap = float(bitfieldExtract(int(w[i]), int(t << 2u), 4)) * scale[i]; |
| 155 | lo[i] += tap * x0; |
| 156 | hi[i] += tap * x1; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | for (uint i = 0u; i < BLOCK; i++) { |
| 162 | store8(channel_base + i, position_base, positions, lo[i], hi[i]); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void main() { |
| 167 | if (p.in1 == 4u) { |
| 168 | wide_oct(); |
| 169 | return; |
| 170 | } |
| 171 | if (p.in1 == 1u) { |
| 172 | wide(); |
| 173 | return; |
| 174 | } |
| 175 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 176 | if (tile >= p.count) { |
| 177 | return; |
| 178 | } |
| 179 | uint positions = p.out_h * p.out_w; |
| 180 | uint tiles_across = (positions + TILE - 1u) / TILE; |
| 181 | uint lid = gl_LocalInvocationID.x; |
| 182 | uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK; |
| 183 | uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; |
| 184 | uint blocks = (p.in_c + I4_BLOCK - 1u) / I4_BLOCK; |
| 185 | // Rows past the output read the last row and are dropped at the store. |
| 186 | uint rows[BLOCK]; |
| 187 | for (uint i = 0u; i < BLOCK; i++) { |
| 188 | rows[i] = min(channel_base + i, p.out_c - 1u); |
| 189 | } |
| 190 | bool live[BLOCK]; |
| 191 | for (uint j = 0u; j < BLOCK; j++) { |
| 192 | live[j] = position_base + j < positions; |
| 193 | } |
| 194 | float acc[BLOCK][BLOCK]; |
| 195 | for (uint i = 0u; i < BLOCK; i++) { |
| 196 | for (uint j = 0u; j < BLOCK; j++) { |
| 197 | acc[i][j] = 0.0; |
| 198 | } |
| 199 | } |
| 200 | if ((p.in_c & 31u) == 0u && (p.weight & 3u) == 0u) { |
| 201 | // No barriers in this shader, so a lane with no live position may leave. |
| 202 | if (position_base >= positions) { |
| 203 | return; |
| 204 | } |
| 205 | // A row starts at word `p.weight + row * in_c / 8`, a whole `uvec4` when both terms are. |
| 206 | uint rows32[BLOCK]; |
| 207 | for (uint i = 0u; i < BLOCK; i++) { |
| 208 | rows32[i] = (p.weight >> 2u) + rows[i] * (p.in_c >> 5u); |
| 209 | } |
| 210 | vec4 sum[BLOCK]; |
| 211 | for (uint i = 0u; i < BLOCK; i++) { |
| 212 | sum[i] = vec4(0.0); |
| 213 | } |
| 214 | bool aligned = (positions & 3u) == 0u && (p.in0 & 3u) == 0u; |
| 215 | uint quads = positions >> 2u; |
| 216 | uint quad = (p.in0 + position_base) >> 2u; |
| 217 | uint first = p.in0 + position_base; |
| 218 | uint last_quad = (p.in0 + p.in_c * positions - 1u) >> 2u; |
| 219 | for (uint block = 0u; block < blocks; block++) { |
| 220 | uvec4 w[BLOCK]; |
| 221 | for (uint i = 0u; i < BLOCK; i++) { |
| 222 | w[i] = weights128[rows32[i] + block]; |
| 223 | } |
| 224 | vec4 part[BLOCK]; |
| 225 | for (uint i = 0u; i < BLOCK; i++) { |
| 226 | part[i] = vec4(0.0); |
| 227 | } |
| 228 | uint k = block * I4_BLOCK; |
| 229 | for (uint t = 0u; t < I4_BLOCK; t++) { |
| 230 | vec4 x; |
| 231 | if (aligned) { |
| 232 | x = vec4(arena_quad[quad + (k + t) * quads]); |
| 233 | } else { |
| 234 | // Four positions straddling two quads; the clamp keeps the tail read in |
| 235 | // bounds, and the lanes it substitutes are not live. |
| 236 | uint at = first + (k + t) * positions; |
| 237 | uint q = at >> 2u; |
| 238 | uint s = at & 3u; |
| 239 | f16vec4 a = arena_quad[q]; |
| 240 | f16vec4 b = arena_quad[min(q + 1u, last_quad)]; |
| 241 | f16vec4 h = s == 0u ? a |
| 242 | : s == 1u ? f16vec4(a.yzw, b.x) |
| 243 | : s == 2u ? f16vec4(a.zw, b.xy) |
| 244 | : f16vec4(a.w, b.xyz); |
| 245 | x = vec4(h); |
| 246 | } |
| 247 | for (uint i = 0u; i < BLOCK; i++) { |
| 248 | float tap = float(bitfieldExtract(int(w[i][t >> 3u]), int((t & 7u) << 2u), 4)); |
| 249 | part[i] += tap * x; |
| 250 | } |
| 251 | } |
| 252 | for (uint i = 0u; i < BLOCK; i++) { |
| 253 | sum[i] += part[i] * int4_scale(rows[i], block, blocks); |
| 254 | } |
| 255 | } |
| 256 | for (uint i = 0u; i < BLOCK; i++) { |
| 257 | for (uint j = 0u; j < BLOCK; j++) { |
| 258 | acc[i][j] = sum[i][j]; |
| 259 | } |
| 260 | } |
| 261 | } else { |
| 262 | // Any `in_c`: a nibble and its block scale per tap. |
| 263 | for (uint k = 0u; k < p.in_c; k++) { |
| 264 | uint at = p.in0 + k * positions + position_base; |
| 265 | float x[BLOCK]; |
| 266 | for (uint j = 0u; j < BLOCK; j++) { |
| 267 | x[j] = live[j] ? float(arena[at + j]) : 0.0; |
| 268 | } |
| 269 | for (uint i = 0u; i < BLOCK; i++) { |
| 270 | float w = float(int4_at(p.weight, rows[i] * p.in_c + k)) |
| 271 | * int4_scale(rows[i], k / I4_BLOCK, blocks); |
| 272 | for (uint j = 0u; j < BLOCK; j++) { |
| 273 | acc[i][j] += w * x[j]; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | for (uint i = 0u; i < BLOCK; i++) { |
| 279 | uint channel = channel_base + i; |
| 280 | if (channel >= p.out_c) { |
| 281 | continue; |
| 282 | } |
| 283 | float offset = float(weights[p.bias + channel]); |
| 284 | for (uint j = 0u; j < BLOCK; j++) { |
| 285 | if (!live[j]) { |
| 286 | continue; |
| 287 | } |
| 288 | uint index = channel * positions + position_base + j; |
| 289 | float value = quant_store(fused_store(acc[i][j] + offset, p.act, channel, index)); |
| 290 | arena[p.out_at + index] = float16_t(value); |
| 291 | } |
| 292 | } |
| 293 | } |