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

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

conv_vec_int4.comp128 lines · 5.4 KiBRaw
1#version 450
2//! `conv_vec_int8.comp` for a **4-bit** kernel with **per-block** scales.
3//!
4//! The scale table is rank 2, `(out_channels, ceil(in_c / I4_BLOCK))`, at `p.act_weight`, so the
5//! scale multiplies each block's partial sum rather than the finished accumulator.
6//!
7//! # Lane layout
8//!
9//! A workgroup owns [`ROWS`] output channels. Lane `l` owns the row pair `2 * (l % 4)` and
10//! `2 * (l % 4) + 1` and the blocks `l / 4, l / 4 + 16, ...`. Every Gemma projection has a block
11//! count divisible by 16 (48, 64, 192, 384), so all 64 lanes do the same work; the previous
12//! layout (a lane per block, all eight rows) left 16 of 64 lanes idle at `in_c` 1536 and re-read
13//! the activations per row. Here one block is two `uvec4` weight loads and eight read-only
14//! `f16vec4` activation loads shared by both rows.
15//!
16//! # Push fields
17//!
18//! As `conv_vec_int8.comp`: `p.weight` is a 32-bit **word** offset, `p.act_weight` the scale
19//! table, `p.bias` the bias, `p.in_c` the taps, `p.out_c` the output channels and `p.count` the
20//! workgroup count, one per group of [`ROWS`] channels.
21#include "common.glsl"
22
23/// The arena as quads of fp16, for the wide activation loads. Read-only, and never a range this
24/// dispatch writes.
25layout(std430, binding = 0) readonly buffer ArenaQuad {
26 f16vec4 arena_quad[];
27};
28
29/// Output channels one workgroup owns. `CONV_VEC_INT4_ROWS` in `nets` must equal this.
30#define ROWS 8u
31
32shared float partial[2][64];
33
34/// The eight sign-extended nibbles of `bits` dotted with eight activations.
35float dot8(uint bits, vec4 lo, vec4 hi) {
36 ivec4 v = ivec4(bits);
37 vec4 a = vec4((v << ivec4(28, 24, 20, 16)) >> 28);
38 vec4 b = vec4((v << ivec4(12, 8, 4, 0)) >> 28);
39 return dot(a, lo) + dot(b, hi);
40}
41
42void main() {
43 uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
44 if (tile >= p.count) {
45 return;
46 }
47 uint lane = gl_LocalInvocationID.x;
48 uint channel_base = tile * ROWS;
49 uint blocks = (p.in_c + I4_BLOCK - 1u) / I4_BLOCK;
50 // Rows past the output read the last row and are dropped at the store.
51 uint row0 = min(channel_base + 2u * (lane & 3u), p.out_c - 1u);
52 uint row1 = min(channel_base + 2u * (lane & 3u) + 1u, p.out_c - 1u);
53 float acc0 = 0.0;
54 float acc1 = 0.0;
55 bool wide = (p.in_c & 31u) == 0u && (p.weight & 3u) == 0u && (p.in0 & 3u) == 0u;
56 if (wide) {
57 uint per_row = p.in_c >> 5u;
58 uint w0 = (p.weight >> 2u) + row0 * per_row;
59 uint w1 = (p.weight >> 2u) + row1 * per_row;
60 uint quad0 = p.in0 >> 2u;
61 for (uint block = lane >> 2u; block < blocks; block += 16u) {
62 uint q = quad0 + block * 8u;
63 vec4 x0 = vec4(arena_quad[q]);
64 vec4 x1 = vec4(arena_quad[q + 1u]);
65 vec4 x2 = vec4(arena_quad[q + 2u]);
66 vec4 x3 = vec4(arena_quad[q + 3u]);
67 vec4 x4 = vec4(arena_quad[q + 4u]);
68 vec4 x5 = vec4(arena_quad[q + 5u]);
69 vec4 x6 = vec4(arena_quad[q + 6u]);
70 vec4 x7 = vec4(arena_quad[q + 7u]);
71 uvec4 a = weights128[w0 + block];
72 uvec4 b = weights128[w1 + block];
73 float s0 = dot8(a.x, x0, x1) + dot8(a.y, x2, x3) + dot8(a.z, x4, x5) + dot8(a.w, x6, x7);
74 float s1 = dot8(b.x, x0, x1) + dot8(b.y, x2, x3) + dot8(b.z, x4, x5) + dot8(b.w, x6, x7);
75 acc0 += s0 * int4_scale(row0, block, blocks);
76 acc1 += s1 * int4_scale(row1, block, blocks);
77 }
78 } else {
79 // Any `in_c` or alignment: a row starts wherever `channel * in_c` puts it, so a block's
80 // nibbles can straddle words; walk word by word and take only what each word holds.
81 for (uint block = lane >> 2u; block < blocks; block += 16u) {
82 uint base = block * I4_BLOCK;
83 uint span = min(base + I4_BLOCK, p.in_c) - base;
84 for (uint r = 0u; r < 2u; r++) {
85 uint channel = r == 0u ? row0 : row1;
86 uint row = channel * p.in_c;
87 float total = 0.0;
88 uint taken = 0u;
89 while (taken < span) {
90 uint at = row + base + taken;
91 uint word = weights32[p.weight + (at >> 3u)];
92 uint offset = at & 7u;
93 uint run = min(8u - offset, span - taken);
94 for (uint n = 0u; n < run; n++) {
95 int code = bitfieldExtract(int(word), int((offset + n) << 2u), 4);
96 total += float(code) * float(arena[p.in0 + base + taken + n]);
97 }
98 taken += run;
99 }
100 float scaled = total * int4_scale(channel, block, blocks);
101 if (r == 0u) {
102 acc0 += scaled;
103 } else {
104 acc1 += scaled;
105 }
106 }
107 }
108 }
109 partial[0][lane] = acc0;
110 partial[1][lane] = acc1;
111 barrier();
112 if (lane >= ROWS) {
113 return;
114 }
115 uint channel = channel_base + lane;
116 if (channel >= p.out_c) {
117 return;
118 }
119 // Row `lane` lives in slot `lane & 1` of the lanes congruent to `lane / 2` mod 4.
120 uint pair = lane >> 1u;
121 uint slot = lane & 1u;
122 float total = 0.0;
123 for (uint i = 0u; i < 16u; i++) {
124 total += partial[slot][pair + 4u * i];
125 }
126 float offset = float(weights[p.bias + channel]);
127 arena[p.out_at + channel] = float16_t(quant_store(fused_store(total + offset, p.act, channel, channel)));
128}