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

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

conv.comp83 lines · 3.4 KiBRaw
1#version 450
2//! Convolution: dense, grouped, depthwise and dilated, with a fused activation.
3//!
4//! One shader for all 173 convolutions across both networks. The variants that would
5//! usually get their own kernel — 1x1, 3x3, 5x5, depthwise, dilation 1/2/4/8, stride 1/2,
6//! asymmetric padding — differ only in the loop bounds and strides below, all of which
7//! come from push constants.
8//!
9//! # Gather, not scatter
10//!
11//! Each invocation owns one output element and reads everything it needs. Nothing is
12//! written twice, so there are no atomics and no clearing pass, and the bias is simply
13//! the accumulator's initial value rather than a separate op.
14//!
15//! Weights are ONNX's layout verbatim, `[m, in_c / group, kh, kw]` row-major, so
16//! `maml_convert.py` transposes nothing and there is no layout to disagree about.
17//!
18//! # What this deliberately does not do
19//!
20//! No tiling, no shared-memory staging of the input patch, no vectorised loads. A
21//! tiled kernel would read each input element once per output tile instead of once per
22//! output element, which is the obvious next optimisation if U^2-Netp's ~2.2 GMAC at
23//! 320x320 turns out too slow. It is not done yet because a naive gather is the version
24//! whose indexing can be reasoned about, and there is no numeric reference to catch a
25//! tiling bug.
26
27#include "common.glsl"
28#include "conv_tiled.glsl"
29
30void main() {
31 // The register-blocked tiled path, for nets that opted in with
32 // `Builder::spatial_tiling`. The untiled body below is unchanged for
33 // everything else — same Kind, same push block, same dispatches.
34 if (p.in1 == 1u) {
35 tiled_spatial();
36 return;
37 }
38 if (p.in1 == 2u) {
39 tiled_depthwise();
40 return;
41 }
42 uint index = global_index();
43 if (index >= p.count) {
44 return;
45 }
46 uint oc, oy, ox;
47 unpack(index, oc, oy, ox);
48
49 // Grouped convolution: output channel `oc` reads only its own group's slice of the
50 // input. group == in_c == out_c is depthwise, where this collapses to one channel.
51 uint in_per_group = p.in_c / p.group;
52 uint out_per_group = p.out_c / p.group;
53 uint first_in = (oc / out_per_group) * in_per_group;
54
55 float acc = float(weights[p.bias + oc]);
56 uint kernel_at = p.weight + oc * in_per_group * p.kh * p.kw;
57
58 for (uint ic = 0u; ic < in_per_group; ic++) {
59 uint plane = p.in0 + (first_in + ic) * p.in_h * p.in_w;
60 for (uint ky = 0u; ky < p.kh; ky++) {
61 int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);
62 if (p.pad_edge != 0u) {
63 // Replicate the border rather than read zeros. See `Push::pad_edge`.
64 iy = clamp(iy, 0, int(p.in_h) - 1);
65 } else if (iy < 0 || iy >= int(p.in_h)) {
66 continue;
67 }
68 uint row = plane + uint(iy) * p.in_w;
69 uint tap = kernel_at + (ic * p.kh + ky) * p.kw;
70 for (uint kx = 0u; kx < p.kw; kx++) {
71 int ix = int(ox * p.stride_w + kx * p.dil_w) - int(p.pad_l);
72 if (p.pad_edge != 0u) {
73 ix = clamp(ix, 0, int(p.in_w) - int(p.pad_edge));
74 } else if (ix < 0 || ix >= int(p.in_w)) {
75 continue;
76 }
77 acc += float(arena[row + uint(ix)]) * float(weights[tap + kx]);
78 }
79 }
80 }
81
82 arena[p.out_at + index] = float16_t(fused_store(acc, p.act, oc, index));
83}