LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
LR-EDIT-DETAIL-DENOISE: check the transposed convolution's stores exactly too
Claude2c085e0ae58c
diff --git a/crates/gpu/src/nn.rs b/crates/gpu/src/nn.rsindex b5a0f39f6..55420598b 100644--- a/crates/gpu/src/nn.rs+++ b/crates/gpu/src/nn.rs@@ -921,24 +921,44 @@ mod tests {a.iter().zip(b).fold(0f32, |m, (x, y)| m.max((x - y).abs())) / scale}+ /// A 1 × 1 convolution from 4 channels to `cout` that copies input channel `co % 4` to each output channel `co`.+ fn copy(src: usize, cout: usize) -> Op {+ let mut weight = vec![0f32; cout * 4];+ for co in 0..cout {+ weight[co * 4 + co % 4] = 1.0;+ }+ Op::Conv(lightcraft_denoise::net::Conv { src, src2: None, cin: 4, cout, k: 1, weight, bias: vec![0.0; cout], leaky: None })+ }+/// Two 1 × 1 layers that only copy channels, so the right answer is exact: the first layer's stores (mode 0) went/// wrong on DX12 with FXC on an NVIDIA GTX 1070, where every second pixel came out with its channels shifted.#[test]fn a_layer_stores_every_pixels_channels_where_the_next_reads_them() {- use lightcraft_denoise::net::{Conv, Op};- let copy = |src: usize, cout: usize| {- let mut weight = vec![0f32; cout * 4];- for co in 0..cout {- weight[co * 4 + co % 4] = 1.0;- }- Op::Conv(Conv { src, src2: None, cin: 4, cout, k: 1, weight, bias: vec![0.0; cout], leaky: None })- };let n = Net::new(4, vec![copy(0, 4), copy(1, 12), Op::DepthToSpace2 { src: 2 }]).unwrap();let Some(g) = gpu(&n, 3) else { return };let x: Vec<f32> = (1..=36).map(|i| i as f32).collect();assert_eq!(g.run(&x).unwrap(), reference::run(&n, 3, &x).unwrap());}+ /// The same for a transposed convolution's stores (mode 1): a max-pool halves the tile, and the 2 × 2 transposed+ /// convolution copies each pooled pixel's channels to the four pixels it becomes.+ #[test]+ fn a_transposed_convolution_stores_every_pixels_channels_where_the_next_reads_them() {+ use lightcraft_denoise::net::ConvT2;+ // [cin][cout][2][2]: channel c to channel c at all four places+ let mut weight = vec![0f32; 4 * 4 * 4];+ for c in 0..4 {+ for q in 0..4 {+ weight[(c * 4 + c) * 4 + q] = 1.0;+ }+ }+ let up = Op::ConvT2(ConvT2 { src: 1, cin: 4, cout: 4, weight, bias: vec![0.0; 4], leaky: None });+ let n = Net::new(4, vec![Op::MaxPool2 { src: 0 }, up, copy(2, 12), Op::DepthToSpace2 { src: 3 }]).unwrap();+ let Some(g) = gpu(&n, 6) else { return };+ let x: Vec<f32> = (1..=144).map(|i| i as f32).collect();+ assert_eq!(g.run(&x).unwrap(), reference::run(&n, 6, &x).unwrap());+ }+#[test]fn the_gpu_gives_what_the_reference_gives() {// (tile, channels, depth): a tile that is not a multiple of the block size, odd channel counts, a deeper netdiff --git a/crates/gpu/src/wgsl/nn_conv.wgsl b/crates/gpu/src/wgsl/nn_conv.wgslindex 75389b517..ee35e88ef 100644--- a/crates/gpu/src/wgsl/nn_conv.wgsl+++ b/crates/gpu/src/wgsl/nn_conv.wgsl@@ -124,8 +124,9 @@ fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_index) t}let bv = bias[n / 4u];// Four channels that follow each other are stored one by one, `one` apart. With a literal 1u, FXC- // (DX12) merges them into one 16-byte store, and NVIDIA's DX12 driver ran that wrong on a GTX- // 1070: every second pixel's channels came out shifted. FXC cannot see that `one` is 1.+ // (DX12) merges them into one 16-byte store, and NVIDIA's DX12 driver ran mode 0's wrong on a+ // GTX 1070: every second pixel's channels came out shifted. Mode 1's merged store ran right+ // there, but it is the same instruction, so it steps the same way. FXC cannot see that `one` is 1.let one = 1u + p.zero;for (var i = 0u; i < 4u; i++) {let m = m0 + ty * 4u + i;diff --git a/docs/gpu-pipeline.md b/docs/gpu-pipeline.mdindex 246b4c158..2e9d0db3b 100644--- a/docs/gpu-pipeline.md+++ b/docs/gpu-pipeline.md@@ -56,10 +56,11 @@ FXC builds the compute kernels somewhat slower than DXC (≈ 9 s vs 7.5 s for thRTX 4080), off the startup path.FXC merges stores to neighbouring elements of a storage array into one wider store. NVIDIA's DX12 driver ran-the 16-byte stores it made of the denoise convolution's output wrong on a GTX 1070 (every second pixel's channels+the 16-byte store it made of a denoise convolution's output wrong on a GTX 1070 (every second pixel's channelsshifted; DXC and Vulkan were right), so `nn_conv.wgsl` steps those stores by `1 + zero` from its uniform and FXC-leaves them apart. `nn::tests::a_layer_stores_every_pixels_channels_where_the_next_reads_them` catches it on such-a card.+leaves them apart. The transposed convolution's merged store ran right on that card, but it steps the same way.+`nn::tests::a_layer_stores_every_pixels_channels_where_the_next_reads_them` catches the fault on such a card;+`a_transposed_convolution_stores_every_pixels_channels_where_the_next_reads_them` checks the other store exactly.**Off the startup path.** The compute device is created on a background thread once the window isup (`gpu::warm_up` from the first frame's settings), and never while GPU rendering is off: with