ml: blocked 2x2 stride-2 ConvTranspose, ppocr_det ConvT 50 -> 7.8 ms on Pixel
- Commit
a0d6ae513d76f33f3085127ed3394bb8285be489- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 00:09 UTC (1 month ago)
- Parents
a6271a5
Diff
library/ml/src/main/rust/shaders/conv_transpose.comp
diff --git a/library/ml/src/main/rust/shaders/conv_transpose.comp b/library/ml/src/main/rust/shaders/conv_transpose.compindex c9280657bb..8cbd77a6ac 100644--- a/library/ml/src/main/rust/shaders/conv_transpose.comp+++ b/library/ml/src/main/rust/shaders/conv_transpose.comp@@ -22,7 +22,58 @@ #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;
library/ml/src/main/rust/src/nets/mod_part9.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part9.rs b/library/ml/src/main/rust/src/nets/mod_part9.rsindex 603c72f9c8..8ad1a81277 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -236,10 +236,22 @@ impl<'a> Builder<'a> { }); return Ok(()); }+ // A 2x2 stride-2 unpadded transpose under spatial tiling: `in1` = 1 selects+ // the one-tap-per-output path in `conv_transpose.comp`, one invocation per+ // input pixel x 4 output channels.+ let blocked_transpose = self.spatial_tiled+ && *transpose+ && *group == 1+ && kernel == &(2, 2)+ && stride == &(2, 2)+ && pad == &(0, 0)+ && so.h == 2 * si.h+ && so.w == 2 * si.w; ops.push(Op::Dispatch { kind: if *transpose { Kind::ConvTranspose } else { Kind::Conv }, push: Push { in0: at(*input)?,+ in1: u32::from(blocked_transpose), out: at(*out)?, weight: *weight, bias: *bias,@@ -266,7 +278,11 @@ impl<'a> Builder<'a> { shift: Self::fuse_offset(*shift, &at)?, ..Push::default() },- invocations: so.len(),+ invocations: if blocked_transpose {+ so.c.div_ceil(4) * si.h * si.w+ } else {+ so.len()+ }, }); } Node::MaxPool { input, out, kernel, stride } => {
library/ml/src/main/rust/src/vulkan/parity_part6.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part6.rs b/library/ml/src/main/rust/src/vulkan/parity_part6.rsindex d2d1f17519..d23fbc4740 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -406,4 +406,29 @@ fn an_edge_padded_conv_stays_untiled_under_spatial_tiling() { } } compare("edge-padded convs under spatial tiling", plan, source.into_data(), &[&input]);+}+/// 2x2 stride-2 transposed convolutions on the blocked path (`in1` = 1): the ppocr_det head+/// pair (24 -> 6 -> 1 channels, ragged channel blocks) over an odd 19x37 input, and a fused+/// residual onto the second.+#[test]+#[ignore = "needs a Vulkan device"]+fn blocked_2x2_transposed_convs_agree_with_the_reference() {+ let input = spread(24 * 19 * 37, 0.5);+ let source = Invented::new(6);+ let plan = build(&source, &[Shape::new(24, 19, 37)], |b, ids| {+ b.spatial_tiling();+ let x = b.conv_transpose(ids[0], 0, 6, (2, 2), (2, 2), (0, 0, 0, 0), Act::Relu);+ let y = b.conv_transpose(x, 2, 6, (2, 2), (2, 2), (0, 0, 0, 0), Act::None);+ let z = b.add(y, y);+ b.conv_transpose(z, 4, 1, (2, 2), (2, 2), (0, 0, 0, 0), Act::Sigmoid)+ });+ let mut seen = 0;+ for op in &plan.ops {+ if let crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvTranspose, push, .. } = op {+ assert_eq!(push.in1, 1, "a 2x2 transpose missed the blocked path");+ seen += 1;+ }+ }+ assert_eq!(seen, 3);+ compare("blocked 2x2 transposed convs", plan, source.into_data(), &[&input]); }\ No newline at end of file