ml: pad_edge on the blocked dense conv path (vocoder head opt-in ready)
- Commit
987bcec0193ec527b57af4988e38b3417dd6046f- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 01:03 UTC (1 month ago)
- Parents
050f0da
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 31f441342e..e8764506b9 100644--- a/library/ml/src/main/rust/shaders/conv_tiled.glsl+++ b/library/ml/src/main/rust/shaders/conv_tiled.glsl@@ -169,7 +169,33 @@ void tiled_spatial() { // `rem` is one value for the whole dispatch. bool aligned_rows = p.stride_w == 1u && (p.in_w & 3u) == 0u && (p.in0 & 3u) == 0u; int rem = (int(p.in0) + ix0) & 3;- if (aligned_rows && p.kw == 3u && p.dil_w == 1u) {+ if (p.pad_edge != 0u) {+ // Replicate the border like conv.comp: rows clamp to the last row, columns to+ // `in_w - pad_edge`. Masked scalars only.+ int last_x = int(p.in_w) - int(p.pad_edge);+ 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 = clamp(int(oy * p.stride_h + ky * p.dil_h) - int(p.pad_t), 0, int(p.in_h) - 1);+ uint row = channel + uint(iy) * p.in_w;+ uint tap0 = ic * taps + ky * p.kw;+ for (uint kx = 0u; kx < p.kw; kx++) {+ int ix = ix0 + int(kx * p.dil_w);+ float x[BLOCK_X];+ for (uint j = 0u; j < BLOCK_X; j++) {+ x[j] = float(arena[row + uint(clamp(ix + int(j * p.stride_w), 0, last_x))]);+ }+ vec4 xl = vec4(x[0], x[1], x[2], x[3]);+ vec4 xh = vec4(x[4], x[5], x[6], x[7]);+ for (uint i = 0u; i < BLOCK_OC; i++) {+ float w = float(weights[wrow[i] + tap0 + kx]);+ lo[i] += w * xl;+ hi[i] += w * xh;+ }+ }+ }+ }+ } else if (aligned_rows && p.kw == 3u && p.dil_w == 1u) { DENSE_ROWS_ANY(3, 1) } else if (aligned_rows && p.kw == 3u && p.dil_w == 2u) { DENSE_ROWS_ANY(3, 2)
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 0830c776be..dc9dac0db9 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -220,11 +220,14 @@ impl<'a> Builder<'a> { // in with `Builder::spatial_tiling`. Same `Kind::Conv` and push block as // 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. A depthwise+ // Transposed convolutions (different weight layout), edge-padded depthwise+ // convolutions and other 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) {+ if self.spatial_tiled+ && !*transpose+ && (*group == 1 || (depthwise && !*pad_edge))+ { 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);
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 c1ebdfdbd6..1691ebf587 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -386,26 +386,30 @@ fn blocked_depthwise_convs_agree_with_the_reference() { 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.+/// Edge padding (`pad_edge` 1 and 2: clamp columns to `in_w - pad_edge`, rows to the last+/// row) under spatial tiling: the depthwise conv stays untiled, the dense 3x3 with PReLU takes+/// the blocked path, and both agree with the reference. 30 columns leave a ragged x block. #[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;+fn edge_padded_convs_agree_under_spatial_tiling() {+ for edge in [1u32, 2] {+ let input = spread(6 * 9 * 30, 0.2);+ let source = Invented::new(5);+ 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::PRelu(4))+ });+ assert_eq!(conv_selectors(&plan), [1, 1, 0], "edge padding took the wrong paths");+ for op in &mut plan.ops {+ if let crate::nets::Op::Dispatch { kind: crate::nets::Kind::Conv, push, .. } = op {+ push.pad_edge = edge;+ } }+ let what = format!("edge-padded convs under spatial tiling, pad_edge {edge}");+ compare(&what, plan, source.into_data(), &[&input]); }- 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@@ -583,4 +587,20 @@ fn a_full_window_conv_agrees_with_the_reference() { if push.in_c == 24 * 5 * 7 ))); compare("a full-window conv", plan, source.into_data(), &[&input]);+}++/// The Supertonic vocoder head's shape on the blocked path: one row, a 1x3 edge-padded on the+/// left only (2, 0) with PReLU, over 37 columns and 10 ragged output channels.+#[test]+#[ignore = "needs a Vulkan device"]+fn a_blocked_edge_padded_1x3_agrees_with_the_reference() {+ let input = spread(8 * 37, 0.4);+ let source = Invented::new(3);+ let plan = build(&source, &[Shape::new(8, 1, 37)], |b, ids| {+ b.spatial_tiling();+ b.edge_padding();+ b.conv(ids[0], 0, 10, (1, 3), (1, 1), (1, 1), (0, 2, 0, 0), 1, Act::PRelu(2))+ });+ assert_eq!(conv_selectors(&plan), [0, 1, 0], "the edge-padded 1x3 missed the blocked path");+ compare("a blocked edge-padded 1x3", plan, source.into_data(), &[&input]); }\ No newline at end of file