From a4ff60233fce134e8f779ef001872dfd9a8f9923 Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 21 Mar 2026 08:38:29 +0100 Subject: feat(cnn_v3): Phase 3 complete — WGSL U-Net inference shaders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 5 compute shaders + cnn_v3/common snippet: enc0: Conv(20→4,3×3) + FiLM + ReLU full-res enc1: AvgPool + Conv(4→8,3×3) + FiLM + ReLU half-res bottleneck: AvgPool + Conv(8→8,1×1) + ReLU quarter-res dec1: NearestUp + cat(enc1) + Conv(16→4) + FiLM half-res dec0: NearestUp + cat(enc0) + Conv(8→4) + FiLM + Sigmoid full-res Parity rules: zero-pad conv, AvgPool down, NearestUp, FiLM after conv+bias, skip=concat, OIHW weights+bias layout. Matches PyTorch train_cnn_v3.py forward() exactly. Registered in workspaces/main/assets.txt + src/effects/shaders.cc. Weight layout + Params struct documented in cnn_v3/docs/HOWTO.md §7. Next: Phase 4 — C++ CNNv3Effect + FiLM uniform upload. Co-Authored-By: Claude Sonnet 4.6 --- cnn_v3/shaders/cnn_v3_dec1.wgsl | 72 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 cnn_v3/shaders/cnn_v3_dec1.wgsl (limited to 'cnn_v3/shaders/cnn_v3_dec1.wgsl') diff --git a/cnn_v3/shaders/cnn_v3_dec1.wgsl b/cnn_v3/shaders/cnn_v3_dec1.wgsl new file mode 100644 index 0000000..28ae3dc --- /dev/null +++ b/cnn_v3/shaders/cnn_v3_dec1.wgsl @@ -0,0 +1,72 @@ +// CNN v3 — Decoder level 1 +// NearestUp2x(bottleneck) + cat(enc1_skip) -> Conv(16->4, 3x3, zero-pad) + FiLM + ReLU +// +// Inputs: bottleneck_tex (rgba32uint, 8xf16) quarter-res +// enc1_tex (rgba32uint, 8xf16) half-res (skip connection) +// Output: dec1_out (rgba16float, 4ch) half-res (dispatch at half-res dims) +// +// Weight layout (f16, OIHW + bias): +// [0 .. 16*4*9) conv: w[out][in][ky][kx] (in=16: 8 bottleneck + 8 enc1 skip) +// [576 .. +4) bias: b[out] + +#include "cnn_v3/common" + +const DEC1_IN: u32 = 16u; +const DEC1_OUT: u32 = 4u; + +struct Params { + weight_offset: u32, + _pad: vec3u, + gamma: vec4f, + beta: vec4f, +} + +@group(0) @binding(0) var bottleneck_tex: texture_2d; +@group(0) @binding(1) var enc1_tex: texture_2d; +@group(0) @binding(2) var weights: array; +@group(0) @binding(3) var params: Params; +@group(0) @binding(4) var dec1_out: texture_storage_2d; + +// Load 16 concatenated channels at half-res coord hcoord: +// ch 0-7: bottleneck nearest-up (bottleneck_tex[hcoord/2]) +// ch 8-15: enc1 skip (enc1_tex[hcoord]) +// Returns zeros for OOB hcoord (zero-padding for the conv). +fn load_dec1_concat(hcoord: vec2i, half_dims: vec2i) -> array { + if (hcoord.x < 0 || hcoord.y < 0 || hcoord.x >= half_dims.x || hcoord.y >= half_dims.y) { + return array(0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.); + } + let quart_dims = half_dims / 2; + let qc = clamp(hcoord / 2, vec2i(0), quart_dims - vec2i(1)); + let b = unpack_8ch(bottleneck_tex, qc); + let s = unpack_8ch(enc1_tex, hcoord); + return array( + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], + s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7] + ); +} + +@compute @workgroup_size(8, 8) +fn dec1_main(@builtin(global_invocation_id) id: vec3u) { + let half_dims = vec2i(textureDimensions(enc1_tex)); + let coord = vec2i(id.xy); + if (coord.x >= half_dims.x || coord.y >= half_dims.y) { return; } + + let wo = params.weight_offset; + var out: array; + + for (var o: u32 = 0u; o < DEC1_OUT; o++) { + var sum = get_w(wo, DEC1_OUT * DEC1_IN * 9u + o); // bias + for (var ky: i32 = -1; ky <= 1; ky++) { + for (var kx: i32 = -1; kx <= 1; kx++) { + let feat = load_dec1_concat(coord + vec2i(kx, ky), half_dims); + let ki = u32(ky + 1) * 3u + u32(kx + 1); + for (var i: u32 = 0u; i < DEC1_IN; i++) { + sum += get_w(wo, o * DEC1_IN * 9u + i * 9u + ki) * feat[i]; + } + } + } + out[o] = max(0.0, params.gamma[o] * sum + params.beta[o]); + } + + textureStore(dec1_out, coord, vec4f(out[0], out[1], out[2], out[3])); +} -- cgit v1.2.3