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

Modern-Apps.git / library / ml / src / main / rust / shaders / conv_tiled.glsl

conv_tiled.glsl360 lines · 14.5 KiBRaw
1//! A dense (`group == 1`) convolution, register-blocked over channels and columns.
2//!
3//! Included by `conv.comp`, which branches here when `p.in1 == 1` (see `nets::Push::in1`);
4//! the untiled body there stays for every net that never opts in with
5//! `Builder::spatial_tiling`, and for grouped convolutions.
6//!
7//! Each invocation owns [`BLOCK_OC`] output channels by [`BLOCK_X`] adjacent columns of one
8//! output row, so every weight read feeds eight columns and every activation read feeds four
9//! channels. Invocations run along the row first, so neighbouring lanes read neighbouring
10//! activations and the same weights.
11//!
12//! The scalar untiled path is load-issue bound (~12 GMAC/s on a Pixel 8). Stride-1 3x3,
13//! dilated 3x3 and 5x5 kernels over quad-aligned rows read each (channel, kernel row) span
14//! once as aligned `f16vec4` quads and run every tap from registers (`dense_rows`, ~120
15//! GMAC/s); anything else reads masked scalars. Accumulation order per output is the
16//! untiled one - input channels, kernel rows, taps - so the two agree to rounding.
17
18#extension GL_EXT_control_flow_attributes : require
19
20layout(std430, binding = 0) readonly buffer ArenaQuad {
21 f16vec4 arena_quad[];
22};
23
24/// Output channels per invocation. **Must equal `SPATIAL_BLOCK_OC` in `nets`.**
25#define BLOCK_OC 4u
26
27/// Adjacent output columns per invocation. **Must equal `SPATIAL_BLOCK_X` in `nets`.**
28#define BLOCK_X 8u
29
30// Eight consecutive arena elements from `at`, through the aligned quads covering them. The
31// third quad is read only when `at` is unaligned, so a run ending a buffer reads nothing past it.
32void eight_at(uint at, out vec4 lo, out vec4 hi) {
33 uint q = at >> 2u;
34 uint rem = at & 3u;
35 f16vec4 a = arena_quad[q];
36 f16vec4 b = arena_quad[q + 1u];
37 if (rem == 0u) {
38 lo = vec4(a);
39 hi = vec4(b);
40 return;
41 }
42 f16vec4 c = arena_quad[q + 2u];
43 if (rem == 1u) {
44 lo = vec4(a.yzw, b.x);
45 hi = vec4(b.yzw, c.x);
46 } else if (rem == 2u) {
47 lo = vec4(a.zw, b.xy);
48 hi = vec4(b.zw, c.xy);
49 } else {
50 lo = vec4(a.w, b.xyz);
51 hi = vec4(b.w, c.xyz);
52 }
53}
54
55// The eight columns' activations for one tap: input row `row`, first column `ix`.
56void columns_at(uint row, int ix, bool interior, out vec4 lo, out vec4 hi) {
57 if (interior) {
58 eight_at(row + uint(ix), lo, hi);
59 return;
60 }
61 float x[BLOCK_X];
62 for (uint j = 0u; j < BLOCK_X; j++) {
63 int at = ix + int(j * p.stride_w);
64 x[j] = (at >= 0 && at < int(p.in_w)) ? float(arena[row + uint(at)]) : 0.0;
65 }
66 lo = vec4(x[0], x[1], x[2], x[3]);
67 hi = vec4(x[4], x[5], x[6], x[7]);
68}
69
70/// Quads in the widest window `dense_rows` loads: 8 columns + a 4-column reach + offset 3.
71#define WINDOW_QUADS 4
72
73// Element `e` of a window of quads; constant once `dense_rows` is inlined and unrolled.
74#define WIN(e) win[(e) >> 2][(e) & 3]
75
76// Dense stride-1 convolution over rows that all share one quad alignment.
77//
78// Per (input channel, kernel row) the block's whole input span - 8 columns plus the
79// kernel's dilated reach - is read as aligned quads, then every kernel tap reuses it from
80// registers. Columns outside the row are zeroed by a per-lane mask computed once, so edge
81// blocks take the same branch-free path as interior ones (a per-lane edge branch diverged
82// most warps). `rem` (the window start's offset into its quad), `kw` and `dil` are
83// literals at every call site, so after inlining every window index is a constant.
84void dense_rows(
85 const int rem, const int kw, const int dil,
86 uint oy, int ix0, uint wrow[BLOCK_OC], inout vec4 lo[BLOCK_OC], inout vec4 hi[BLOCK_OC]
87) {
88 int quads = (rem + int(BLOCK_X) + (kw - 1) * dil + 3) >> 2;
89 uint plane = p.in_h * p.in_w;
90 uint taps = p.kh * p.kw;
91 int c0 = ix0 - rem;
92 bvec4 keep[WINDOW_QUADS];
93 [[unroll]] for (int q = 0; q < quads; q++) {
94 ivec4 c = ivec4(c0 + 4 * q) + ivec4(0, 1, 2, 3);
95 keep[q] = equal(clamp(c, ivec4(0), ivec4(int(p.in_w) - 1)), c);
96 }
97 int last = int((p.in0 + p.in_c * plane - 1u) >> 2u);
98 for (uint ic = 0u; ic < p.in_c; ic++) {
99 uint channel = p.in0 + ic * plane;
100 for (uint ky = 0u; ky < p.kh; ky++) {
101 int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);
102 if (iy < 0 || iy >= int(p.in_h)) {
103 continue;
104 }
105 int first = (int(channel + uint(iy) * p.in_w) + c0) >> 2;
106 vec4 win[WINDOW_QUADS];
107 [[unroll]] for (int q = 0; q < quads; q++) {
108 vec4 v = vec4(arena_quad[clamp(first + q, 0, last)]);
109 win[q] = mix(vec4(0.0), v, keep[q]);
110 }
111 uint tap0 = ic * taps + ky * p.kw;
112 [[unroll]] for (int kx = 0; kx < kw; kx++) {
113 int e = rem + kx * dil;
114 vec4 xl = vec4(WIN(e), WIN(e + 1), WIN(e + 2), WIN(e + 3));
115 vec4 xh = vec4(WIN(e + 4), WIN(e + 5), WIN(e + 6), WIN(e + 7));
116 [[unroll]] for (uint i = 0u; i < BLOCK_OC; i++) {
117 float w = float(weights[wrow[i] + tap0 + uint(kx)]);
118 lo[i] += w * xl;
119 hi[i] += w * xh;
120 }
121 }
122 }
123 }
124}
125
126// `dense_rows` for the four alignments of one kernel shape; `rem` is uniform per dispatch.
127#define DENSE_ROWS_ANY(KW, DIL) \
128 if (rem == 0) dense_rows(0, KW, DIL, oy, ix0, wrow, lo, hi); \
129 else if (rem == 1) dense_rows(1, KW, DIL, oy, ix0, wrow, lo, hi); \
130 else if (rem == 2) dense_rows(2, KW, DIL, oy, ix0, wrow, lo, hi); \
131 else dense_rows(3, KW, DIL, oy, ix0, wrow, lo, hi);
132
133/// Quads in the window `strided_rows` loads: 15 + 3 columns + offset 3.
134#define STRIDED_QUADS 6
135
136// `dense_rows` for a stride-2 3x3: the block's 17-column input span per (channel, kernel row)
137// is read once as quads and the even elements feed every tap.
138void strided_rows(
139 const int rem, uint oy, int ix0, uint wrow[BLOCK_OC], inout vec4 lo[BLOCK_OC],
140 inout vec4 hi[BLOCK_OC]
141) {
142 int quads = (rem + 2 * int(BLOCK_X) + 1 + 3) >> 2;
143 uint plane = p.in_h * p.in_w;
144 uint taps = p.kh * 3u;
145 int c0 = ix0 - rem;
146 bvec4 keep[STRIDED_QUADS];
147 [[unroll]] for (int q = 0; q < quads; q++) {
148 ivec4 c = ivec4(c0 + 4 * q) + ivec4(0, 1, 2, 3);
149 keep[q] = equal(clamp(c, ivec4(0), ivec4(int(p.in_w) - 1)), c);
150 }
151 int last = int((p.in0 + p.in_c * plane - 1u) >> 2u);
152 for (uint ic = 0u; ic < p.in_c; ic++) {
153 uint channel = p.in0 + ic * plane;
154 for (uint ky = 0u; ky < p.kh; ky++) {
155 int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);
156 if (iy < 0 || iy >= int(p.in_h)) {
157 continue;
158 }
159 int first = (int(channel + uint(iy) * p.in_w) + c0) >> 2;
160 vec4 win[STRIDED_QUADS];
161 [[unroll]] for (int q = 0; q < quads; q++) {
162 vec4 v = vec4(arena_quad[clamp(first + q, 0, last)]);
163 win[q] = mix(vec4(0.0), v, keep[q]);
164 }
165 uint tap0 = ic * taps + ky * 3u;
166 [[unroll]] for (int kx = 0; kx < 3; kx++) {
167 int e = rem + kx;
168 vec4 xl = vec4(WIN(e), WIN(e + 2), WIN(e + 4), WIN(e + 6));
169 vec4 xh = vec4(WIN(e + 8), WIN(e + 10), WIN(e + 12), WIN(e + 14));
170 [[unroll]] for (uint i = 0u; i < BLOCK_OC; i++) {
171 float w = float(weights[wrow[i] + tap0 + uint(kx)]);
172 lo[i] += w * xl;
173 hi[i] += w * xh;
174 }
175 }
176 }
177 }
178}
179
180void tiled_spatial() {
181 uint x_blocks = (p.out_w + BLOCK_X - 1u) / BLOCK_X;
182 uint oc_blocks = (p.out_c + BLOCK_OC - 1u) / BLOCK_OC;
183 uint invocation = global_index();
184 if (invocation >= oc_blocks * p.out_h * x_blocks) {
185 return;
186 }
187 uint ox0 = (invocation % x_blocks) * BLOCK_X;
188 uint rest = invocation / x_blocks;
189 uint oy = rest % p.out_h;
190 uint channel_base = (rest / p.out_h) * BLOCK_OC;
191
192 // Channels past the last read the last one's weights; never stored.
193 uint rows[BLOCK_OC];
194 for (uint i = 0u; i < BLOCK_OC; i++) {
195 rows[i] = min(channel_base + i, p.out_c - 1u);
196 }
197 vec4 lo[BLOCK_OC];
198 vec4 hi[BLOCK_OC];
199 for (uint i = 0u; i < BLOCK_OC; i++) {
200 lo[i] = vec4(float(weights[p.bias + rows[i]]));
201 hi[i] = lo[i];
202 }
203
204 uint taps = p.kh * p.kw;
205 uint plane = p.in_h * p.in_w;
206 int ix0 = int(ox0 * p.stride_w) - int(p.pad_l);
207 bool interior = p.stride_w == 1u && ix0 >= 0
208 && ix0 + int(BLOCK_X - 1u + (p.kw - 1u) * p.dil_w) < int(p.in_w);
209
210 uint wrow[BLOCK_OC];
211 for (uint i = 0u; i < BLOCK_OC; i++) {
212 wrow[i] = p.weight + rows[i] * p.in_c * taps;
213 }
214
215 // Every row of the input starts at the same quad offset, so the window alignment
216 // `rem` is one value for the whole dispatch.
217 bool aligned_rows = p.stride_w == 1u && (p.in_w & 3u) == 0u && (p.in0 & 3u) == 0u;
218 int rem = (int(p.in0) + ix0) & 3;
219 if (p.pad_edge != 0u) {
220 // Replicate the border like conv.comp: rows clamp to the last row, columns to
221 // `in_w - pad_edge`. Masked scalars only.
222 int last_x = int(p.in_w) - int(p.pad_edge);
223 for (uint ic = 0u; ic < p.in_c; ic++) {
224 uint channel = p.in0 + ic * plane;
225 for (uint ky = 0u; ky < p.kh; ky++) {
226 int iy = clamp(int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t), 0, int(p.in_h) - 1);
227 uint row = channel + uint(iy) * p.in_w;
228 uint tap0 = ic * taps + ky * p.kw;
229 for (uint kx = 0u; kx < p.kw; kx++) {
230 int ix = ix0 + int(kx * p.dil_w);
231 float x[BLOCK_X];
232 for (uint j = 0u; j < BLOCK_X; j++) {
233 x[j] = float(arena[row + uint(clamp(ix + int(j * p.stride_w), 0, last_x))]);
234 }
235 vec4 xl = vec4(x[0], x[1], x[2], x[3]);
236 vec4 xh = vec4(x[4], x[5], x[6], x[7]);
237 for (uint i = 0u; i < BLOCK_OC; i++) {
238 float w = float(weights[wrow[i] + tap0 + kx]);
239 lo[i] += w * xl;
240 hi[i] += w * xh;
241 }
242 }
243 }
244 }
245 } else if (aligned_rows && p.kw == 3u && p.dil_w == 1u) {
246 DENSE_ROWS_ANY(3, 1)
247 } else if (aligned_rows && p.kw == 3u && p.dil_w == 2u) {
248 DENSE_ROWS_ANY(3, 2)
249 } else if (aligned_rows && p.kw == 5u && p.dil_w == 1u) {
250 DENSE_ROWS_ANY(5, 1)
251 } else if (p.stride_w == 2u && p.kw == 3u && p.dil_w == 1u && (p.in_w & 3u) == 0u
252 && (p.in0 & 3u) == 0u) {
253 if (rem == 0) strided_rows(0, oy, ix0, wrow, lo, hi);
254 else if (rem == 1) strided_rows(1, oy, ix0, wrow, lo, hi);
255 else if (rem == 2) strided_rows(2, oy, ix0, wrow, lo, hi);
256 else strided_rows(3, oy, ix0, wrow, lo, hi);
257 } else {
258 for (uint ic = 0u; ic < p.in_c; ic++) {
259 uint channel = p.in0 + ic * plane;
260 for (uint ky = 0u; ky < p.kh; ky++) {
261 int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);
262 if (iy < 0 || iy >= int(p.in_h)) {
263 continue;
264 }
265 uint row = channel + uint(iy) * p.in_w;
266 uint tap0 = ic * taps + ky * p.kw;
267 for (uint kx = 0u; kx < p.kw; kx++) {
268 vec4 xl, xh;
269 columns_at(row, ix0 + int(kx * p.dil_w), interior, xl, xh);
270 for (uint i = 0u; i < BLOCK_OC; i++) {
271 float w = float(weights[wrow[i] + tap0 + kx]);
272 lo[i] += w * xl;
273 hi[i] += w * xh;
274 }
275 }
276 }
277 }
278 }
279
280 uint positions = p.out_h * p.out_w;
281 for (uint i = 0u; i < BLOCK_OC; i++) {
282 uint channel = channel_base + i;
283 if (channel >= p.out_c) {
284 continue;
285 }
286 for (uint j = 0u; j < BLOCK_X; j++) {
287 if (ox0 + j >= p.out_w) {
288 continue;
289 }
290 uint index = channel * positions + oy * p.out_w + ox0 + j;
291 float value = j < 4u ? lo[i][j] : hi[i][j - 4u];
292 arena[p.out_at + index] = float16_t(fused_store(value, p.act, channel, index));
293 }
294 }
295}
296
297// Eight output columns' taps for a depthwise row: stride 1 reads 8 contiguous elements,
298// stride 2 reads 16 and keeps the even ones; edges and other strides gather masked scalars.
299void depthwise_columns(uint row, int ix, bool interior, out vec4 lo, out vec4 hi) {
300 if (interior && p.stride_w == 1u) {
301 eight_at(row + uint(ix), lo, hi);
302 return;
303 }
304 if (interior && p.stride_w == 2u) {
305 vec4 a, b, c, d;
306 eight_at(row + uint(ix), a, b);
307 eight_at(row + uint(ix) + 8u, c, d);
308 lo = vec4(a.xz, b.xz);
309 hi = vec4(c.xz, d.xz);
310 return;
311 }
312 columns_at(row, ix, false, lo, hi);
313}
314
315/// A depthwise convolution (`group == in_c == out_c`): one channel by [`BLOCK_X`] columns
316/// of one output row per invocation, so each weight is read once per eight outputs.
317void tiled_depthwise() {
318 uint x_blocks = (p.out_w + BLOCK_X - 1u) / BLOCK_X;
319 uint invocation = global_index();
320 if (invocation >= p.out_c * p.out_h * x_blocks) {
321 return;
322 }
323 uint ox0 = (invocation % x_blocks) * BLOCK_X;
324 uint rest = invocation / x_blocks;
325 uint oy = rest % p.out_h;
326 uint channel = rest / p.out_h;
327
328 vec4 lo = vec4(float(weights[p.bias + channel]));
329 vec4 hi = lo;
330 int ix0 = int(ox0 * p.stride_w) - int(p.pad_l);
331 bool interior = ix0 >= 0
332 && ix0 + int(BLOCK_X * p.stride_w - 1u + (p.kw - 1u) * p.dil_w) < int(p.in_w);
333 uint plane = p.in0 + channel * p.in_h * p.in_w;
334 uint kernel_at = p.weight + channel * p.kh * p.kw;
335 for (uint ky = 0u; ky < p.kh; ky++) {
336 int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);
337 if (iy < 0 || iy >= int(p.in_h)) {
338 continue;
339 }
340 uint row = plane + uint(iy) * p.in_w;
341 uint tap = kernel_at + ky * p.kw;
342 for (uint kx = 0u; kx < p.kw; kx++) {
343 vec4 xl, xh;
344 depthwise_columns(row, ix0 + int(kx * p.dil_w), interior, xl, xh);
345 float w = float(weights[tap + kx]);
346 lo += w * xl;
347 hi += w * xh;
348 }
349 }
350
351 uint positions = p.out_h * p.out_w;
352 for (uint j = 0u; j < BLOCK_X; j++) {
353 if (ox0 + j >= p.out_w) {
354 continue;
355 }
356 uint index = channel * positions + oy * p.out_w + ox0 + j;
357 float value = j < 4u ? lo[j] : hi[j - 4u];
358 arena[p.out_at + index] = float16_t(fused_store(value, p.act, channel, index));
359 }
360}