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

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

conv_int8.comp179 lines · 7.0 KiBRaw
1#version 450
2//! A convolution whose weights are int8, dequantised by one scale per output channel.
3//!
4//! `out[o][y][x] = act(scale[o] * sum(int8_w * in) + bias[o])`
5//!
6//! # Why int8 at all
7//!
8//! Only where the weights dominate a download. SMaLL-100 is 330 million parameters: 660 MB at
9//! fp16 and 330 MB here, against the 1.14 GB the ncnn build of the same model shipped. Every
10//! other network in this runtime is small enough that fp16 costs nothing worth reclaiming, and
11//! they keep using `conv.comp`.
12//!
13//! # What is quantised and what is not
14//!
15//! Weights only. The export quantises its activations too, dynamically, per tensor — a
16//! `DynamicQuantizeLinear` before every `MatMulInteger`, with a zero point to carry the
17//! asymmetry. This does not, and that is both simpler and *more* accurate: activations stay
18//! fp16 in the arena, so the only error introduced is the weights'. It costs nothing, because
19//! the weights are what take the space.
20//!
21//! Read from the export rather than assumed: the weight zero points are per-column vectors and
22//! every entry of every one of them is **0**, so the quantisation is symmetric and a value is
23//! just `int8 * scale`.
24//!
25//! # The scale multiplies once
26//!
27//! One scale per output column, which is what onnxruntime's dynamic quantiser emits — not the
28//! per-tensor scalar this was first written for. Either way it applies to the finished
29//! accumulator rather than to every tap, so the inner loop is an integer-to-float convert and a
30//! multiply-add, exactly as many operations as the fp16 version. A per-tap scale would not work
31//! this way, which is why the rank is checked.
32//!
33//! `p.act_weight` holds the scale tensor's offset in the weights buffer, as an fp16 element index;
34//! output channel `oc` reads its own entry. `p.weight` holds the int8 tensor's offset as a
35//! **32-bit word** index, because that is the view it is unpacked through.
36
37#include "common.glsl"
38#include "conv_tiled.glsl"
39
40// The register-blocked path (`p.in1 == 1`, ungrouped only): 4 output channels x 8 columns
41// of one row per invocation. Same accumulation order as `main`.
42void tiled_int8() {
43 uint x_blocks = (p.out_w + BLOCK_X - 1u) / BLOCK_X;
44 uint oc_blocks = (p.out_c + BLOCK_OC - 1u) / BLOCK_OC;
45 uint invocation = global_index();
46 if (invocation >= oc_blocks * p.out_h * x_blocks) {
47 return;
48 }
49 uint ox0 = (invocation % x_blocks) * BLOCK_X;
50 uint rest = invocation / x_blocks;
51 uint oy = rest % p.out_h;
52 uint channel_base = (rest / p.out_h) * BLOCK_OC;
53
54 uint taps = p.kh * p.kw;
55 uint wrow[BLOCK_OC];
56 vec4 lo[BLOCK_OC];
57 vec4 hi[BLOCK_OC];
58 for (uint i = 0u; i < BLOCK_OC; i++) {
59 wrow[i] = min(channel_base + i, p.out_c - 1u) * p.in_c * taps;
60 lo[i] = vec4(0.0);
61 hi[i] = vec4(0.0);
62 }
63 uint plane = p.in_h * p.in_w;
64 int ix0 = int(ox0 * p.stride_w) - int(p.pad_l);
65 bool interior = p.stride_w == 1u && ix0 >= 0
66 && ix0 + int(BLOCK_X - 1u + (p.kw - 1u) * p.dil_w) < int(p.in_w);
67 for (uint ic = 0u; ic < p.in_c; ic++) {
68 uint channel = p.in0 + ic * plane;
69 for (uint ky = 0u; ky < p.kh; ky++) {
70 int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);
71 if (iy < 0 || iy >= int(p.in_h)) {
72 continue;
73 }
74 uint row = channel + uint(iy) * p.in_w;
75 uint tap0 = ic * taps + ky * p.kw;
76 for (uint kx = 0u; kx < p.kw; kx++) {
77 vec4 xl, xh;
78 columns_at(row, ix0 + int(kx * p.dil_w), interior, xl, xh);
79 for (uint i = 0u; i < BLOCK_OC; i++) {
80 float w = float(int8_at(p.weight, wrow[i] + tap0 + kx));
81 lo[i] += w * xl;
82 hi[i] += w * xh;
83 }
84 }
85 }
86 }
87
88 uint positions = p.out_h * p.out_w;
89 for (uint i = 0u; i < BLOCK_OC; i++) {
90 uint oc = channel_base + i;
91 if (oc >= p.out_c) {
92 continue;
93 }
94 float scale = float(weights[p.act_weight + oc]);
95 float bias = float(weights[p.bias + oc]);
96 for (uint j = 0u; j < BLOCK_X; j++) {
97 if (ox0 + j >= p.out_w) {
98 continue;
99 }
100 uint index = oc * positions + oy * p.out_w + ox0 + j;
101 float total = j < 4u ? lo[i][j] : hi[i][j - 4u];
102 arena[p.out_at + index] =
103 float16_t(quant_store(fused_store(total * scale + bias, p.act, oc, index)));
104 }
105 }
106}
107
108// A grouped single-position 1x1 (`p.in1 == 2`, maia's attention-bias expansion): one output per
109// invocation, its group's taps read 16 at a time through the `uvec4` and quad views but summed
110// one by one in `main`'s order, so the result is identical. `nets` checks the alignment.
111void grouped_rows() {
112 uint oc = global_index();
113 if (oc >= p.out_c) {
114 return;
115 }
116 uint per_group = p.in_c / p.group;
117 uint group = oc / (p.out_c / p.group);
118 uint row16 = (p.weight + ((oc * per_group) >> 2u)) >> 2u;
119 uint quad = (p.in0 + group * per_group) >> 2u;
120 float total = 0.0;
121 for (uint k = 0u; k < per_group; k += 16u) {
122 uvec4 w = weights128[row16 + (k >> 4u)];
123 for (uint q = 0u; q < 4u; q++) {
124 vec4 x = vec4(arena_quad[quad + (k >> 2u) + q]);
125 for (uint t = 0u; t < 4u; t++) {
126 total += float(bitfieldExtract(int(w[q]), int(t << 3u), 8)) * x[t];
127 }
128 }
129 }
130 float scale = float(weights[p.act_weight + oc]);
131 float biased = total * scale + float(weights[p.bias + oc]);
132 arena[p.out_at + oc] = float16_t(quant_store(fused_store(biased, p.act, oc, oc)));
133}
134
135void main() {
136 if (p.in1 == 1u) {
137 tiled_int8();
138 return;
139 }
140 if (p.in1 == 2u) {
141 grouped_rows();
142 return;
143 }
144 uint index = global_index();
145 if (index >= p.count) {
146 return;
147 }
148 uint oc, oy, ox;
149 unpack(index, oc, oy, ox);
150
151 uint per_group = p.in_c / p.group;
152 uint group = oc / (p.out_c / p.group);
153 uint base = group * per_group;
154
155 float total = 0.0;
156 for (uint ic = 0u; ic < per_group; ic++) {
157 uint channel = base + ic;
158 for (uint ky = 0u; ky < p.kh; ky++) {
159 int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);
160 if (iy < 0 || iy >= int(p.in_h)) {
161 continue;
162 }
163 for (uint kx = 0u; kx < p.kw; kx++) {
164 int ix = int(ox * p.stride_w + kx * p.dil_w) - int(p.pad_l);
165 if (ix < 0 || ix >= int(p.in_w)) {
166 continue;
167 }
168 uint at = ((oc * per_group + ic) * p.kh + ky) * p.kw + kx;
169 float value =
170 float(arena[p.in0 + (channel * p.in_h + uint(iy)) * p.in_w + uint(ix)]);
171 total += float(int8_at(p.weight, at)) * value;
172 }
173 }
174 }
175
176 float scale = float(weights[p.act_weight + oc]);
177 float biased = total * scale + float(weights[p.bias + oc]);
178 arena[p.out_at + index] = float16_t(quant_store(fused_store(biased, p.act, oc, index)));
179}