Modern Apps git

← Modern-Apps.git

ml: blocked depthwise conv path (in1=2), scrfd depthwise -8% on Pixel

Commit
7aac61cca65cd8b5cdd5f497584889585f995607
Author
vayun-mathur <[email protected]>
When
2026-10-25 23:27 UTC (1 month ago)
Parents
e2ecf55

Diff

library/ml/src/main/rust/shaders/conv.comp
diff --git a/library/ml/src/main/rust/shaders/conv.comp b/library/ml/src/main/rust/shaders/conv.compindex 47f5eba921..a8f76fe7d9 100644--- a/library/ml/src/main/rust/shaders/conv.comp+++ b/library/ml/src/main/rust/shaders/conv.comp@@ -35,6 +35,10 @@ void main() {         tiled_spatial();         return;     }+    if (p.in1 == 2u) {+        tiled_depthwise();+        return;+    }     uint index = global_index();     if (index >= p.count) {         return;
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 cf54ff1d26..31f441342e 100644--- a/library/ml/src/main/rust/shaders/conv_tiled.glsl+++ b/library/ml/src/main/rust/shaders/conv_tiled.glsl@@ -214,3 +214,68 @@ void tiled_spatial() {         }     } }++// Eight output columns' taps for a depthwise row: stride 1 reads 8 contiguous elements,+// stride 2 reads 16 and keeps the even ones; edges and other strides gather masked scalars.+void depthwise_columns(uint row, int ix, bool interior, out vec4 lo, out vec4 hi) {+    if (interior && p.stride_w == 1u) {+        eight_at(row + uint(ix), lo, hi);+        return;+    }+    if (interior && p.stride_w == 2u) {+        vec4 a, b, c, d;+        eight_at(row + uint(ix), a, b);+        eight_at(row + uint(ix) + 8u, c, d);+        lo = vec4(a.xz, b.xz);+        hi = vec4(c.xz, d.xz);+        return;+    }+    columns_at(row, ix, false, lo, hi);+}++/// A depthwise convolution (`group == in_c == out_c`): one channel by [`BLOCK_X`] columns+/// of one output row per invocation, so each weight is read once per eight outputs.+void tiled_depthwise() {+    uint x_blocks = (p.out_w + BLOCK_X - 1u) / BLOCK_X;+    uint invocation = global_index();+    if (invocation >= p.out_c * p.out_h * x_blocks) {+        return;+    }+    uint ox0 = (invocation % x_blocks) * BLOCK_X;+    uint rest = invocation / x_blocks;+    uint oy = rest % p.out_h;+    uint channel = rest / p.out_h;++    vec4 lo = vec4(float(weights[p.bias + channel]));+    vec4 hi = lo;+    int ix0 = int(ox0 * p.stride_w) - int(p.pad_l);+    bool interior = ix0 >= 0+        && ix0 + int(BLOCK_X * p.stride_w - 1u + (p.kw - 1u) * p.dil_w) < int(p.in_w);+    uint plane = p.in0 + channel * p.in_h * p.in_w;+    uint kernel_at = p.weight + channel * p.kh * p.kw;+    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;+        }+        uint row = plane + uint(iy) * p.in_w;+        uint tap = kernel_at + ky * p.kw;+        for (uint kx = 0u; kx < p.kw; kx++) {+            vec4 xl, xh;+            depthwise_columns(row, ix0 + int(kx * p.dil_w), interior, xl, xh);+            float w = float(weights[tap + kx]);+            lo += w * xl;+            hi += w * xh;+        }+    }++    uint positions = p.out_h * p.out_w;+    for (uint j = 0u; j < BLOCK_X; j++) {+        if (ox0 + j >= p.out_w) {+            continue;+        }+        uint index = channel * positions + oy * p.out_w + ox0 + j;+        float value = j < 4u ? lo[j] : hi[j - 4u];+        arena[p.out_at + index] = float16_t(fused_store(value, p.act, channel, index));+    }+}
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 446a7c1498..603c72f9c8 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -193,17 +193,19 @@ impl<'a> Builder<'a> {                 // the untiled path below; `in1` = 1 selects the path in `conv.comp` and                 // there is one invocation per 4-channel x 8-column block of a row.                 // Transposed convolutions (different weight layout), edge padding-                // (Supertonic only) and grouped convolutions stay untiled.-                if self.spatial_tiled && !*transpose && !*pad_edge && *group == 1 {-                    let blocks = so.c.div_ceil(SPATIAL_BLOCK_OC)-                        * so.h-                        * so.w.div_ceil(SPATIAL_BLOCK_X);+                // (Supertonic only) and grouped convolutions stay untiled. A depthwise+                // convolution takes `in1` = 2: one invocation per channel x 8-column block.+                let depthwise = *group > 1 && *group == si.c && *group == so.c;+                if self.spatial_tiled && !*transpose && !*pad_edge && (*group == 1 || depthwise) {+                    let channel_blocks =+                        if depthwise { so.c } else { so.c.div_ceil(SPATIAL_BLOCK_OC) };+                    let blocks = channel_blocks * so.h * so.w.div_ceil(SPATIAL_BLOCK_X);                     ops.push(Op::Dispatch {                         kind: Kind::Conv,                         push: Push {                             in0: at(*input)?,                             // The tiled selector. See `Push::in1`.-                            in1: 1,+                            in1: if depthwise { 2 } else { 1 },                             out: at(*out)?,                             weight: *weight,                             bias: *bias,
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 329d316be3..a209bfc8f3 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -304,3 +304,67 @@ fn blocked_convs_at_every_window_alignment_agree_with_the_reference() {         },     ); }++/// Count the plan's `Kind::Conv` dispatches by `in1` selector (untiled, dense, depthwise).+fn conv_selectors(plan: &crate::nets::Plan) -> [usize; 3] {+    let mut seen = [0; 3];+    for op in &plan.ops {+        if let crate::nets::Op::Dispatch { kind: crate::nets::Kind::Conv, push, .. } = op {+            seen[push.in1 as usize] += 1;+        }+    }+    seen+}++/// Depthwise convolutions on the blocked path (`in1` = 2): MobileFaceNet/SCRFD shapes at+/// stride 1 and 2 over 56 columns (interior quad reads), then 45-wide rows (ragged column+/// block, every alignment), a dilated 3x3, a 5x5 and a fused residual.+#[test]+#[ignore = "needs a Vulkan device"]+fn blocked_depthwise_convs_agree_with_the_reference() {+    let wide = spread(24 * 56 * 56, 0.7);+    let source = Invented::new(4);+    let plan = build(&source, &[Shape::new(24, 56, 56)], |b, ids| {+        b.spatial_tiling();+        let x = b.conv(ids[0], 0, 24, (3, 3), (1, 1), (1, 1), (1, 1, 1, 1), 24, Act::Relu);+        b.conv(x, 2, 24, (3, 3), (2, 2), (1, 1), (1, 1, 1, 1), 24, Act::None)+    });+    assert_eq!(conv_selectors(&plan), [0, 0, 2], "depthwise convs missed the blocked path");+    compare("blocked depthwise 3x3s over 56 columns", plan, source.into_data(), &[&wide]);++    let ragged = spread(10 * 23 * 45, -1.1);+    let source = Invented::new(10);+    let plan = build(&source, &[Shape::new(10, 23, 45)], |b, ids| {+        b.spatial_tiling();+        let x = b.conv(ids[0], 0, 10, (3, 3), (2, 2), (1, 1), (0, 0, 1, 1), 10, Act::HardSwish);+        let x = b.conv(x, 2, 10, (3, 3), (1, 1), (2, 2), (2, 2, 2, 2), 10, Act::Relu);+        let x = b.conv(x, 4, 10, (5, 5), (1, 1), (1, 1), (2, 2, 2, 2), 10, Act::None);+        let y = b.conv(x, 6, 10, (3, 3), (1, 1), (1, 1), (1, 1, 1, 1), 10, Act::None);+        let y = b.add(x, y);+        b.conv(y, 8, 10, (3, 3), (2, 2), (1, 1), (1, 1, 1, 1), 10, Act::Relu)+    });+    assert_eq!(conv_selectors(&plan), [0, 0, 5], "depthwise convs missed the blocked path");+    compare("blocked depthwise convs over 45 columns", plan, source.into_data(), &[&ragged]);+}++/// Edge padding (`pad_edge` 2: clamp columns to `in_w - 2`) stays on the untiled path even+/// with spatial tiling on, and still agrees with the reference.+#[test]+#[ignore = "needs a Vulkan device"]+fn an_edge_padded_conv_stays_untiled_under_spatial_tiling() {+    let input = spread(6 * 9 * 30, 0.2);+    let source = Invented::new(4);+    let mut plan = build(&source, &[Shape::new(6, 9, 30)], |b, ids| {+        b.spatial_tiling();+        b.edge_padding();+        let x = b.conv(ids[0], 0, 6, (3, 3), (1, 1), (1, 1), (1, 1, 1, 1), 6, Act::None);+        b.conv(x, 2, 8, (3, 3), (1, 1), (1, 1), (1, 1, 1, 1), 1, Act::Relu)+    });+    assert_eq!(conv_selectors(&plan), [2, 0, 0], "an edge-padded conv took a blocked path");+    for op in &mut plan.ops {+        if let crate::nets::Op::Dispatch { kind: crate::nets::Kind::Conv, push, .. } = op {+            push.pad_edge = 2;+        }+    }+    compare("edge-padded convs under spatial tiling", plan, source.into_data(), &[&input]);+}\ No newline at end of file