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

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

conv_transpose.comp115 lines · 4.4 KiBRaw
1#version 450
2//! Transposed convolution, written as a gather.
3//!
4//! One use in either network: the selfie net's final 2x2 stride-2 upsample from
5//! 16x128x128 to 1x256x256, which is what turns the decoder's feature map into an alpha.
6//!
7//! # Gather, not scatter
8//!
9//! The natural reading of a transposed convolution scatters — each input element adds
10//! into `kh * kw` outputs — which needs atomics or a separate clear-and-accumulate pass.
11//! Inverting it costs only a divisibility test: output row `oy` is fed by input row
12//! `(oy + pad_t - ky) / stride_h`, when that division is exact. So this stays one
13//! invocation per output element like every other shader here, with no atomics.
14//!
15//! The weight layout is ONNX's, which for `ConvTranspose` is `[in_c, out_c / group,
16//! kh, kw]` — input channels outermost, the transpose of `Conv`'s. That inversion is the
17//! one place the two convolution shaders genuinely differ, and getting it backwards
18//! would produce a mask that looks like noise rather than one that looks nearly right.
19//!
20//! `group` is 1 at the only call site and is not modelled: a grouped transposed
21//! convolution would need the input-channel range restricted the way `conv.comp` does it.
22
23#include "common.glsl"
24
25// A 2x2 stride-2 unpadded transpose (`p.in1 == 1`): every output reads exactly one tap, so
26// one invocation takes one input pixel for 4 output channels and writes their 2x2 blocks,
27// reading each input once instead of once per output and skipping the divisibility tests.
28void blocked_2x2() {
29 uint oc_blocks = (p.out_c + 3u) / 4u;
30 uint invocation = global_index();
31 if (invocation >= oc_blocks * p.in_h * p.in_w) {
32 return;
33 }
34 uint ix = invocation % p.in_w;
35 uint rest = invocation / p.in_w;
36 uint iy = rest % p.in_h;
37 uint channel_base = (rest / p.in_h) * 4u;
38 uint rows[4];
39 vec4 acc[4];
40 for (uint i = 0u; i < 4u; i++) {
41 rows[i] = min(channel_base + i, p.out_c - 1u);
42 acc[i] = vec4(float(weights[p.bias + rows[i]]));
43 }
44 uint plane = p.in_h * p.in_w;
45 uint at = p.in0 + iy * p.in_w + ix;
46 for (uint ic = 0u; ic < p.in_c; ic++) {
47 float x = float(arena[at + ic * plane]);
48 for (uint i = 0u; i < 4u; i++) {
49 uint k = p.weight + (ic * p.out_c + rows[i]) * 4u;
50 acc[i] += x * vec4(float(weights[k]), float(weights[k + 1u]),
51 float(weights[k + 2u]), float(weights[k + 3u]));
52 }
53 }
54 uint positions = p.out_h * p.out_w;
55 for (uint i = 0u; i < 4u; i++) {
56 uint channel = channel_base + i;
57 if (channel >= p.out_c) {
58 continue;
59 }
60 for (uint t = 0u; t < 4u; t++) {
61 uint oy = iy * 2u + t / 2u;
62 uint ox = ix * 2u + t % 2u;
63 if (oy >= p.out_h || ox >= p.out_w) {
64 continue;
65 }
66 uint index = channel * positions + oy * p.out_w + ox;
67 arena[p.out_at + index] = float16_t(fused_store(acc[i][t], p.act, channel, index));
68 }
69 }
70}
71
72void main() {
73 if (p.in1 == 1u) {
74 blocked_2x2();
75 return;
76 }
77 uint index = global_index();
78 if (index >= p.count) {
79 return;
80 }
81 uint oc, oy, ox;
82 unpack(index, oc, oy, ox);
83
84 float acc = float(weights[p.bias + oc]);
85
86 for (uint ic = 0u; ic < p.in_c; ic++) {
87 uint plane = p.in0 + ic * p.in_h * p.in_w;
88 uint kernel_at = p.weight + (ic * p.out_c + oc) * p.kh * p.kw;
89 for (uint ky = 0u; ky < p.kh; ky++) {
90 int numerator = int(oy + p.pad_t) - int(ky);
91 if (numerator < 0 || uint(numerator) % p.stride_h != 0u) {
92 continue;
93 }
94 uint iy = uint(numerator) / p.stride_h;
95 if (iy >= p.in_h) {
96 continue;
97 }
98 uint row = plane + iy * p.in_w;
99 uint tap = kernel_at + ky * p.kw;
100 for (uint kx = 0u; kx < p.kw; kx++) {
101 int denominator = int(ox + p.pad_l) - int(kx);
102 if (denominator < 0 || uint(denominator) % p.stride_w != 0u) {
103 continue;
104 }
105 uint ix = uint(denominator) / p.stride_w;
106 if (ix >= p.in_w) {
107 continue;
108 }
109 acc += float(arena[row + ix]) * float(weights[tap + kx]);
110 }
111 }
112 }
113
114 arena[p.out_at + index] = float16_t(fused_store(acc, p.act, oc, index));
115}