Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / conv_point_q2k.comp
conv_point_q2k.comp207 lines · 8.7 KiBRaw
| 1 | #version 450 |
| 2 | //! A `1 x 1` convolution with GGUF Q2_K weights over a sequence: a Q2_K-by-fp16 matrix product. |
| 3 | //! |
| 4 | //! `out[o][p] = act(sum over superblocks of (d_all*(q*lo) - d_min*hi) * x + bias[o])`, |
| 5 | //! `p` over positions. MADLAD's encoder and prefill. |
| 6 | //! |
| 7 | //! # Shape |
| 8 | //! |
| 9 | //! `conv_point_int8.comp`'s: each invocation owns four output channels by four positions and a |
| 10 | //! workgroup a 32 x 32 block, with the reuse held in registers rather than staged through shared |
| 11 | //! memory. The previous version staged 16 x 16 dequantised tiles with two barriers per 16 taps; on a |
| 12 | //! Pixel 8 it ran MADLAD's 192 encoder projections in 1.3 s. |
| 13 | //! |
| 14 | //! # Sixteen taps per weight word |
| 15 | //! |
| 16 | //! When `in_c % 256 == 0`, the position count is a multiple of four and the activation base is |
| 17 | //! 4-aligned - every MADLAD projection - the loop walks the superblock's `qs` one 32-bit word at a |
| 18 | //! time, as `conv_vec_q2k.comp` does: one word is the 2-bit codes of 16 taps, four per code lane. |
| 19 | //! Each lane's affine pair factors out of its four taps, |
| 20 | //! `sum((dall*lo*c - dmin*hi) * x) = dall*lo*sum(c*x) - dmin*hi*sum(x)`, and the activations are |
| 21 | //! read four positions at a time through a read-only `f16vec4` view, shared by the four rows. |
| 22 | //! Anything else takes an element-at-a-time loop through `q2k_code`/`q2k_scale`. |
| 23 | //! |
| 24 | //! `p.weight` is a **32-bit word** offset to the Q2_K payload, as in `conv_int8.comp`. |
| 25 | //! `p.act_weight` holds the `(d, dmin)` table as an fp16 element index: pair `2*b` and `2*b+1` for |
| 26 | //! superblock `b` of the row. `Act::PRelu` cannot ride along, for the same reason as the int8 pair. |
| 27 | |
| 28 | #include "common.glsl" |
| 29 | |
| 30 | layout(std430, binding = 0) readonly buffer ArenaQuad { |
| 31 | f16vec4 arena_quad[]; |
| 32 | }; |
| 33 | |
| 34 | /// Output channels and positions per workgroup: 8 x 8 invocations of 4 x 4 each. |
| 35 | /// |
| 36 | /// **Must equal `CONV_POINT_INT8_TILE` in `nets`**, which counts this kind's workgroups too. |
| 37 | #define TILE 32u |
| 38 | |
| 39 | /// Channels and positions per invocation. |
| 40 | #define BLOCK 4u |
| 41 | |
| 42 | /// Partial sums from the K-split's other slices, combined by slice 0 in slice order. |
| 43 | shared float partial[64][BLOCK * BLOCK]; |
| 44 | |
| 45 | void main() { |
| 46 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 47 | if (tile >= p.count) { |
| 48 | return; |
| 49 | } |
| 50 | // `p.in1` = s > 0 (word path only): the workgroup's lanes split into 2^s slices, each summing |
| 51 | // a contiguous 1/2^s of the taps for a (32 >> s) x 32 tile, so a GEMM with few tiles (MADLAD's |
| 52 | // 8192 -> 1024 at 32 positions is 32) runs 2^s times the workgroups at the same loads per MAC. |
| 53 | uint split = p.in1; |
| 54 | uint slice_lanes = 64u >> split; |
| 55 | uint positions = p.out_h * p.out_w; |
| 56 | uint tiles_across = (positions + TILE - 1u) / TILE; |
| 57 | uint lid = gl_LocalInvocationID.x; |
| 58 | uint slice = lid / slice_lanes; |
| 59 | uint channel_base = (tile / tiles_across) * (TILE >> split) + ((lid % slice_lanes) >> 3u) * BLOCK; |
| 60 | uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; |
| 61 | bool busy = position_base < positions; |
| 62 | if (!busy && split == 0u) { |
| 63 | return; |
| 64 | } |
| 65 | uint blocks = (p.in_c + Q2K_BLOCK - 1u) / Q2K_BLOCK; |
| 66 | |
| 67 | uint rows[BLOCK]; |
| 68 | for (uint i = 0u; i < BLOCK; i++) { |
| 69 | rows[i] = min(channel_base + i, p.out_c - 1u); |
| 70 | } |
| 71 | bool live[BLOCK]; |
| 72 | for (uint j = 0u; j < BLOCK; j++) { |
| 73 | live[j] = position_base + j < positions; |
| 74 | } |
| 75 | float acc[BLOCK][BLOCK]; |
| 76 | for (uint i = 0u; i < BLOCK; i++) { |
| 77 | for (uint j = 0u; j < BLOCK; j++) { |
| 78 | acc[i][j] = 0.0; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | bool by_word = (p.in_c % Q2K_BLOCK) == 0u && (positions & 3u) == 0u && (p.in0 & 3u) == 0u; |
| 83 | if (by_word) { |
| 84 | uint quads = positions >> 2u; |
| 85 | uint quad0 = (p.in0 + position_base) >> 2u; |
| 86 | vec4 sum[BLOCK]; |
| 87 | for (uint i = 0u; i < BLOCK; i++) { |
| 88 | sum[i] = vec4(0.0); |
| 89 | } |
| 90 | bool pair_word = (p.act_weight & 1u) == 0u; |
| 91 | uint units = (blocks * 16u) >> split; |
| 92 | uint unit0 = busy ? slice * units : 0u; |
| 93 | uint unit_end = busy ? unit0 + units : 0u; |
| 94 | for (uint unit = unit0; unit < unit_end; unit++) { |
| 95 | uint block = unit >> 4u; |
| 96 | uint w = unit & 15u; |
| 97 | uint half_idx = w >> 3u; |
| 98 | uint odd = (w >> 2u) & 1u; |
| 99 | uint first = block * Q2K_BLOCK + half_idx * 128u + odd * 16u + (w & 3u) * 4u; |
| 100 | // Per row: the code word, the two scale words for this half, and the affine pair. |
| 101 | uint codes[BLOCK]; |
| 102 | uint scales[BLOCK][2]; |
| 103 | float dall[BLOCK]; |
| 104 | float dmin[BLOCK]; |
| 105 | for (uint i = 0u; i < BLOCK; i++) { |
| 106 | uint sb = p.weight + (rows[i] * blocks + block) * 21u; |
| 107 | codes[i] = weights32[sb + 4u + w]; |
| 108 | scales[i][0] = weights32[sb + half_idx * 2u]; |
| 109 | scales[i][1] = weights32[sb + half_idx * 2u + 1u]; |
| 110 | uint pair = p.act_weight + (rows[i] * blocks + block) * 2u; |
| 111 | vec2 dm; |
| 112 | if (pair_word) { |
| 113 | dm = unpackHalf2x16(weights32[pair >> 1u]); |
| 114 | } else { |
| 115 | dm = vec2(float(weights[pair]), float(weights[pair + 1u])); |
| 116 | } |
| 117 | dall[i] = dm.x * dm.x; |
| 118 | dmin[i] = dm.y * dm.x; |
| 119 | } |
| 120 | for (uint q = 0u; q < 4u; q++) { |
| 121 | // Taps `first + q*32 + 0..3`, four positions each. |
| 122 | vec4 x[4]; |
| 123 | for (uint t = 0u; t < 4u; t++) { |
| 124 | x[t] = vec4(arena_quad[quad0 + (first + q * 32u + t) * quads]); |
| 125 | } |
| 126 | vec4 xsum = x[0] + x[1] + x[2] + x[3]; |
| 127 | uint shift = q << 1u; |
| 128 | uint byte = (((q & 1u) << 1u) + odd) << 3u; |
| 129 | for (uint i = 0u; i < BLOCK; i++) { |
| 130 | uint sc = (scales[i][q >> 1u] >> byte) & 255u; |
| 131 | vec4 cx = float((codes[i] >> shift) & 3u) * x[0] |
| 132 | + float((codes[i] >> (shift + 8u)) & 3u) * x[1] |
| 133 | + float((codes[i] >> (shift + 16u)) & 3u) * x[2] |
| 134 | + float((codes[i] >> (shift + 24u)) & 3u) * x[3]; |
| 135 | sum[i] += dall[i] * float(sc & 15u) * cx - dmin[i] * float(sc >> 4u) * xsum; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | if (split > 0u) { |
| 140 | if (slice > 0u) { |
| 141 | for (uint i = 0u; i < BLOCK; i++) { |
| 142 | for (uint j = 0u; j < BLOCK; j++) { |
| 143 | partial[lid][i * BLOCK + j] = sum[i][j]; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | barrier(); |
| 148 | if (slice > 0u || !busy) { |
| 149 | return; |
| 150 | } |
| 151 | for (uint k = 1u; k < (1u << split); k++) { |
| 152 | for (uint i = 0u; i < BLOCK; i++) { |
| 153 | for (uint j = 0u; j < BLOCK; j++) { |
| 154 | sum[i][j] += partial[lid + k * slice_lanes][i * BLOCK + j]; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | for (uint i = 0u; i < BLOCK; i++) { |
| 160 | for (uint j = 0u; j < BLOCK; j++) { |
| 161 | acc[i][j] = sum[i][j]; |
| 162 | } |
| 163 | } |
| 164 | } else { |
| 165 | for (uint k = 0u; k < p.in_c; k++) { |
| 166 | uint block = k / Q2K_BLOCK; |
| 167 | uint inblock = k % Q2K_BLOCK; |
| 168 | uint half_idx = inblock / 128u; |
| 169 | uint lane = (inblock % 128u) / 32u; |
| 170 | uint odd = (inblock % 32u) / 16u; |
| 171 | uint at = p.in0 + k * positions + position_base; |
| 172 | float x[BLOCK]; |
| 173 | for (uint j = 0u; j < BLOCK; j++) { |
| 174 | x[j] = live[j] ? float(arena[at + j]) : 0.0; |
| 175 | } |
| 176 | for (uint i = 0u; i < BLOCK; i++) { |
| 177 | uint sbo = (p.weight * 4u) + (rows[i] * blocks + block) * Q2K_BYTES; |
| 178 | uint sc = q2k_scale(sbo, half_idx, lane * 2u + odd); |
| 179 | uint code = q2k_code(sbo, half_idx, inblock); |
| 180 | uint pair = p.act_weight + (rows[i] * blocks + block) * 2u; |
| 181 | float d = float(weights[pair]); |
| 182 | float m = float(weights[pair + 1u]); |
| 183 | float value = d * d * float(code) * float(sc & 15u) - m * d * float(sc >> 4u); |
| 184 | for (uint j = 0u; j < BLOCK; j++) { |
| 185 | acc[i][j] += value * x[j]; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | for (uint i = 0u; i < BLOCK; i++) { |
| 192 | uint channel = channel_base + i; |
| 193 | if (channel >= p.out_c) { |
| 194 | continue; |
| 195 | } |
| 196 | // Already scaled inside the loop (per-lane scales cannot hoist), so only the bias is left. |
| 197 | float offset = float(weights[p.bias + channel]); |
| 198 | for (uint j = 0u; j < BLOCK; j++) { |
| 199 | if (!live[j]) { |
| 200 | continue; |
| 201 | } |
| 202 | uint index = channel * positions + position_base + j; |
| 203 | float value = quant_store(fused_store(acc[i][j] + offset, p.act, channel, index)); |
| 204 | arena[p.out_at + index] = float16_t(value); |
| 205 | } |
| 206 | } |
| 207 | } |