Modern Apps git

← Modern-Apps.git

ml: stride-2 3x3 window path in blocked conv, modnet Conv 146 -> 135 ms

Commit
50a908219a6db6dd00d36e25fad8d643571cca69
Author
vayun-mathur <[email protected]>
When
2026-10-26 01:24 UTC (1 month ago)
Parents
adb428e

Diff

library/ml/src/main/rust/shaders/conv_tiled.glsl
diff --git a/library/ml/src/main/rust/shaders/conv_tiled.glsl b/library/ml/src/main/rust/shaders/conv_tiled.glslindex e8764506b9..f0ef94ec93 100644--- a/library/ml/src/main/rust/shaders/conv_tiled.glsl+++ b/library/ml/src/main/rust/shaders/conv_tiled.glsl@@ -130,6 +130,53 @@ void dense_rows(     else if (rem == 2) dense_rows(2, KW, DIL, oy, ix0, wrow, lo, hi); \     else dense_rows(3, KW, DIL, oy, ix0, wrow, lo, hi); +/// Quads in the window `strided_rows` loads: 15 + 3 columns + offset 3.+#define STRIDED_QUADS 6++// `dense_rows` for a stride-2 3x3: the block's 17-column input span per (channel, kernel row)+// is read once as quads and the even elements feed every tap.+void strided_rows(+    const int rem, uint oy, int ix0, uint wrow[BLOCK_OC], inout vec4 lo[BLOCK_OC],+    inout vec4 hi[BLOCK_OC]+) {+    int quads = (rem + 2 * int(BLOCK_X) + 1 + 3) >> 2;+    uint plane = p.in_h * p.in_w;+    uint taps = p.kh * 3u;+    int c0 = ix0 - rem;+    bvec4 keep[STRIDED_QUADS];+    [[unroll]] for (int q = 0; q < quads; q++) {+        ivec4 c = ivec4(c0 + 4 * q) + ivec4(0, 1, 2, 3);+        keep[q] = equal(clamp(c, ivec4(0), ivec4(int(p.in_w) - 1)), c);+    }+    int last = int((p.in0 + p.in_c * plane - 1u) >> 2u);+    for (uint ic = 0u; ic < p.in_c; ic++) {+        uint channel = p.in0 + ic * plane;+        for (uint ky = 0u; ky < p.kh; ky++) {+            int iy = int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t);+            if (iy < 0 || iy >= int(p.in_h)) {+                continue;+            }+            int first = (int(channel + uint(iy) * p.in_w) + c0) >> 2;+            vec4 win[STRIDED_QUADS];+            [[unroll]] for (int q = 0; q < quads; q++) {+                vec4 v = vec4(arena_quad[clamp(first + q, 0, last)]);+                win[q] = mix(vec4(0.0), v, keep[q]);+            }+            uint tap0 = ic * taps + ky * 3u;+            [[unroll]] for (int kx = 0; kx < 3; kx++) {+                int e = rem + kx;+                vec4 xl = vec4(WIN(e), WIN(e + 2), WIN(e + 4), WIN(e + 6));+                vec4 xh = vec4(WIN(e + 8), WIN(e + 10), WIN(e + 12), WIN(e + 14));+                [[unroll]] for (uint i = 0u; i < BLOCK_OC; i++) {+                    float w = float(weights[wrow[i] + tap0 + uint(kx)]);+                    lo[i] += w * xl;+                    hi[i] += w * xh;+                }+            }+        }+    }+}+ void tiled_spatial() {     uint x_blocks = (p.out_w + BLOCK_X - 1u) / BLOCK_X;     uint oc_blocks = (p.out_c + BLOCK_OC - 1u) / BLOCK_OC;@@ -201,6 +248,12 @@ void tiled_spatial() {         DENSE_ROWS_ANY(3, 2)     } else if (aligned_rows && p.kw == 5u && p.dil_w == 1u) {         DENSE_ROWS_ANY(5, 1)+    } else if (p.stride_w == 2u && p.kw == 3u && p.dil_w == 1u && (p.in_w & 3u) == 0u+        && (p.in0 & 3u) == 0u) {+        if (rem == 0) strided_rows(0, oy, ix0, wrow, lo, hi);+        else if (rem == 1) strided_rows(1, oy, ix0, wrow, lo, hi);+        else if (rem == 2) strided_rows(2, oy, ix0, wrow, lo, hi);+        else strided_rows(3, oy, ix0, wrow, lo, hi);     } else {         for (uint ic = 0u; ic < p.in_c; ic++) {             uint channel = p.in0 + ic * plane;
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 1360cc7446..d93a99d0d0 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -430,3 +430,20 @@ fn a_wide_pointwise_conv_agrees_with_the_reference() {     )));     compare("a wide pointwise conv", plan, source.into_data(), &[&input]); }++/// The stride-2 3x3 window path (`strided_rows`): quad-aligned 44-column rows, left pads 0-3+/// for every window alignment, and ragged output rows (22 or fewer columns) at the right edge.+#[test]+#[ignore = "needs a Vulkan device"]+fn strided_window_convs_agree_with_the_reference() {+    let input = spread(8 * 20 * 44, 0.3);+    for left in 0u32..4 {+        blocked_conv_agrees(+            &format!("a stride-2 3x3 window, left pad {left}"),+            2,+            &[Shape::new(8, 20, 44)],+            &[&input],+            |b, ids| b.conv(ids[0], 0, 12, (3, 3), (2, 2), (1, 1), (1, left, 1, 1), 1, Act::Relu),+        );+    }+}