Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / library / ml / src / main / rust / shaders / conv_q2k.comp

conv_q2k.comp75 lines · 3.4 KiBRaw
1#version 450
2//! `conv_int8.comp` with GGUF Q2_K weights: the untiled lowering.
3//!
4//! `out[o][p] = act(sum over superblocks of (d_all*(q*lo) - d_min*hi) * x + bias[o])`.
5//!
6//! # When this runs instead of the staged pair
7//!
8//! The lowering sends a convolution here when it is pointwise but neither single-position
9//! (which wants `conv_vec_q2k.comp`) nor multi-position (which wants `conv_point_q2k.comp`).
10//! In practice that is almost nothing in an encoder-decoder translation net — both passes
11//! are one shape or the other — but the kind exists so the lowering is total, exactly as
12//! `conv_int8.comp` backs the int8 pair.
13//!
14//! One invocation per output element, like `conv_int8.comp`. The dequantisation is inline
15//! per tap: `p.weight` is a 32-bit **word** offset to the Q2_K payload, `p.act_weight` the
16//! `(d, dmin)` table as an fp16 element index, `p.bias` the bias. `Act::PRelu` cannot ride
17//! along, for the same reason as the int8 pair.
18
19#include "common.glsl"
20
21/// Taps one Q2_K superblock covers. Mirrors `weights::Q2K_BLOCK`.
22#define Q2K_BLOCK 256u
23
24void main() {
25 uint index = global_index();
26 if (index >= p.count) {
27 return;
28 }
29 // Output element -> (channel, position).
30 uint positions = p.out_h * p.out_w;
31 uint channel = index / positions;
32 uint position = index % positions;
33 // Input position: pointwise, so the same position.
34 float total = 0.0;
35 uint blocks = (p.in_c + Q2K_BLOCK - 1u) / Q2K_BLOCK;
36 for (uint block = 0u; block < blocks; block++) {
37 uint pair = p.act_weight + (channel * blocks + block) * 2u;
38 float d = float(weights[pair]);
39 float m = float(weights[pair + 1u]);
40 float dall = d * d;
41 float dmin = m * d;
42 uint sbo = (p.weight * 4u) + (channel * blocks + block) * 84u;
43 uint last = min(block * Q2K_BLOCK + Q2K_BLOCK, p.in_c) - block * Q2K_BLOCK;
44 for (uint half_idx = 0u; half_idx < 2u; half_idx++) {
45 for (uint pair_idx = 0u; pair_idx < 4u; pair_idx++) {
46 uint sc = q2k_scale(sbo, half_idx, pair_idx * 2u);
47 uint sc2 = q2k_scale(sbo, half_idx, pair_idx * 2u + 1u);
48 float lo = float(sc & 15u);
49 float hi = float(sc >> 4u);
50 float lo2 = float(sc2 & 15u);
51 float hi2 = float(sc2 >> 4u);
52 for (uint l = 0u; l < 16u; l++) {
53 uint tap = half_idx * 128u + pair_idx * 32u + l;
54 if (tap >= last) {
55 break;
56 }
57 uint code = q2k_code(sbo, half_idx, tap);
58 total += (dall * float(code) * lo - dmin * hi)
59 * float(arena[p.in0 + (block * Q2K_BLOCK + tap) * positions + position]);
60 }
61 for (uint l = 0u; l < 16u; l++) {
62 uint tap = half_idx * 128u + pair_idx * 32u + 16u + l;
63 if (tap >= last) {
64 break;
65 }
66 uint code = q2k_code(sbo, half_idx, tap);
67 total += (dall * float(code) * lo2 - dmin * hi2)
68 * float(arena[p.in0 + (block * Q2K_BLOCK + tap) * positions + position]);
69 }
70 }
71 }
72 }
73 float offset = float(weights[p.bias + channel]);
74 arena[p.out_at + index] = float16_t(quant_store(fused_store(total + offset, p.act, channel, index)));
75}