#version 450 //! Transposed convolution, written as a gather. //! //! One use in either network: the selfie net's final 2x2 stride-2 upsample from //! 16x128x128 to 1x256x256, which is what turns the decoder's feature map into an alpha. //! //! # Gather, not scatter //! //! The natural reading of a transposed convolution scatters — each input element adds //! into `kh * kw` outputs — which needs atomics or a separate clear-and-accumulate pass. //! Inverting it costs only a divisibility test: output row `oy` is fed by input row //! `(oy + pad_t - ky) / stride_h`, when that division is exact. So this stays one //! invocation per output element like every other shader here, with no atomics. //! //! The weight layout is ONNX's, which for `ConvTranspose` is `[in_c, out_c / group, //! kh, kw]` — input channels outermost, the transpose of `Conv`'s. That inversion is the //! one place the two convolution shaders genuinely differ, and getting it backwards //! would produce a mask that looks like noise rather than one that looks nearly right. //! //! `group` is 1 at the only call site and is not modelled: a grouped transposed //! convolution would need the input-channel range restricted the way `conv.comp` does it. #include "common.glsl" // A 2x2 stride-2 unpadded transpose (`p.in1 == 1`): every output reads exactly one tap, so // one invocation takes one input pixel for 4 output channels and writes their 2x2 blocks, // reading each input once instead of once per output and skipping the divisibility tests. void blocked_2x2() { uint oc_blocks = (p.out_c + 3u) / 4u; uint invocation = global_index(); if (invocation >= oc_blocks * p.in_h * p.in_w) { return; } uint ix = invocation % p.in_w; uint rest = invocation / p.in_w; uint iy = rest % p.in_h; uint channel_base = (rest / p.in_h) * 4u; uint rows[4]; vec4 acc[4]; for (uint i = 0u; i < 4u; i++) { rows[i] = min(channel_base + i, p.out_c - 1u); acc[i] = vec4(float(weights[p.bias + rows[i]])); } uint plane = p.in_h * p.in_w; uint at = p.in0 + iy * p.in_w + ix; for (uint ic = 0u; ic < p.in_c; ic++) { float x = float(arena[at + ic * plane]); for (uint i = 0u; i < 4u; i++) { uint k = p.weight + (ic * p.out_c + rows[i]) * 4u; acc[i] += x * vec4(float(weights[k]), float(weights[k + 1u]), float(weights[k + 2u]), float(weights[k + 3u])); } } uint positions = p.out_h * p.out_w; for (uint i = 0u; i < 4u; i++) { uint channel = channel_base + i; if (channel >= p.out_c) { continue; } for (uint t = 0u; t < 4u; t++) { uint oy = iy * 2u + t / 2u; uint ox = ix * 2u + t % 2u; if (oy >= p.out_h || ox >= p.out_w) { continue; } uint index = channel * positions + oy * p.out_w + ox; arena[p.out_at + index] = float16_t(fused_store(acc[i][t], p.act, channel, index)); } } } void main() { if (p.in1 == 1u) { blocked_2x2(); return; } uint index = global_index(); if (index >= p.count) { return; } uint oc, oy, ox; unpack(index, oc, oy, ox); float acc = float(weights[p.bias + oc]); for (uint ic = 0u; ic < p.in_c; ic++) { uint plane = p.in0 + ic * p.in_h * p.in_w; uint kernel_at = p.weight + (ic * p.out_c + oc) * p.kh * p.kw; for (uint ky = 0u; ky < p.kh; ky++) { int numerator = int(oy + p.pad_t) - int(ky); if (numerator < 0 || uint(numerator) % p.stride_h != 0u) { continue; } uint iy = uint(numerator) / p.stride_h; if (iy >= p.in_h) { continue; } uint row = plane + iy * p.in_w; uint tap = kernel_at + ky * p.kw; for (uint kx = 0u; kx < p.kw; kx++) { int denominator = int(ox + p.pad_l) - int(kx); if (denominator < 0 || uint(denominator) % p.stride_w != 0u) { continue; } uint ix = uint(denominator) / p.stride_w; if (ix >= p.in_w) { continue; } acc += float(arena[row + ix]) * float(weights[tap + kx]); } } } arena[p.out_at + index] = float16_t(fused_store(acc, p.act, oc, index)); }