Modern Apps git

← Modern-Apps.git

ml: blocked 4oc x 8col int8 conv, whisper stem 193 -> 142 ms on Pixel

Commit
34680dcda9f40166ebb78a3059cd4fd49cd7881e
Author
vayun-mathur <[email protected]>
When
2026-10-26 00:23 UTC (1 month ago)
Parents
07fd683

Diff

library/ml/src/main/rust/shaders/conv_int8.comp
diff --git a/library/ml/src/main/rust/shaders/conv_int8.comp b/library/ml/src/main/rust/shaders/conv_int8.compindex d7f85b1ff9..f68918023a 100644--- a/library/ml/src/main/rust/shaders/conv_int8.comp+++ b/library/ml/src/main/rust/shaders/conv_int8.comp@@ -35,8 +35,81 @@ //! **32-bit word** index, because that is the view it is unpacked through.  #include "common.glsl"+#include "conv_tiled.glsl"++// The register-blocked path (`p.in1 == 1`, ungrouped only): 4 output channels x 8 columns+// of one row per invocation. Same accumulation order as `main`.+void tiled_int8() {+    uint x_blocks = (p.out_w + BLOCK_X - 1u) / BLOCK_X;+    uint oc_blocks = (p.out_c + BLOCK_OC - 1u) / BLOCK_OC;+    uint invocation = global_index();+    if (invocation >= oc_blocks * 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_base = (rest / p.out_h) * BLOCK_OC;++    uint taps = p.kh * p.kw;+    uint wrow[BLOCK_OC];+    vec4 lo[BLOCK_OC];+    vec4 hi[BLOCK_OC];+    for (uint i = 0u; i < BLOCK_OC; i++) {+        wrow[i] = min(channel_base + i, p.out_c - 1u) * p.in_c * taps;+        lo[i] = vec4(0.0);+        hi[i] = vec4(0.0);+    }+    uint plane = p.in_h * p.in_w;+    int ix0 = int(ox0 * p.stride_w) - int(p.pad_l);+    bool interior = p.stride_w == 1u && ix0 >= 0+        && ix0 + int(BLOCK_X - 1u + (p.kw - 1u) * p.dil_w) < int(p.in_w);+    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;+            }+            uint row = channel + uint(iy) * p.in_w;+            uint tap0 = ic * taps + ky * p.kw;+            for (uint kx = 0u; kx < p.kw; kx++) {+                vec4 xl, xh;+                columns_at(row, ix0 + int(kx * p.dil_w), interior, xl, xh);+                for (uint i = 0u; i < BLOCK_OC; i++) {+                    float w = float(int8_at(p.weight, wrow[i] + tap0 + kx));+                    lo[i] += w * xl;+                    hi[i] += w * xh;+                }+            }+        }+    }++    uint positions = p.out_h * p.out_w;+    for (uint i = 0u; i < BLOCK_OC; i++) {+        uint oc = channel_base + i;+        if (oc >= p.out_c) {+            continue;+        }+        float scale = float(weights[p.act_weight + oc]);+        float bias = float(weights[p.bias + oc]);+        for (uint j = 0u; j < BLOCK_X; j++) {+            if (ox0 + j >= p.out_w) {+                continue;+            }+            uint index = oc * positions + oy * p.out_w + ox0 + j;+            float total = j < 4u ? lo[i][j] : hi[i][j - 4u];+            arena[p.out_at + index] =+                float16_t(quant_store(fused_store(total * scale + bias, p.act, oc, index)));+        }+    }+}  void main() {+    if (p.in1 == 1u) {+        tiled_int8();+        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 f3e9ec6331..a6ea8792fb 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -77,6 +77,9 @@ impl<'a> Builder<'a> {                     && si.c % 16 == 0                     && *weight % 4 == 0                     && int8_tiles < CONV_POINT_INT8_SPLIT_BELOW;+                // The untiled shader's register-blocked path (`in1` = 1): 4 channels x 8 columns+                // of a row per invocation. Ungrouped only: maia's grouped 1x1 got slower.+                let int8_blocked = kind == Kind::ConvInt8 && *group == 1;                 // Workgroups for the staged kinds, output elements for the untiled one.                 let count = match kind {                     Kind::ConvVecInt8 | Kind::ConvVecInt4 | Kind::ConvVecQ2K => rows,@@ -90,7 +93,7 @@ impl<'a> Builder<'a> {                     kind,                     push: Push {                         in0: at(*input)?,-                        in1: u32::from(split),+                        in1: u32::from(split || int8_blocked),                         out: at(*out)?,                         // A word offset, not an fp16 one: int8 is unpacked through the                         // 32-bit view of the weights buffer.@@ -137,6 +140,9 @@ impl<'a> Builder<'a> {                         | Kind::ConvPointInt4                         | Kind::ConvVecQ2K                         | Kind::ConvPointQ2K => count * 64,+                        Kind::ConvInt8 if int8_blocked => {+                            so.c.div_ceil(SPATIAL_BLOCK_OC) * so.h * so.w.div_ceil(SPATIAL_BLOCK_X)+                        }                         _ => count,                     },                 });
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 b481626a3a..37c69cf5c6 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -469,3 +469,45 @@ fn a_split_int8_gemm_agrees_with_the_reference() {         compare(&what, plan, weights.data().to_vec(), &[&input]);     } }++/// `conv_int8.comp`'s blocked path (`in1` = 1): the whisper stem's stride-2 1x3 at a ragged+/// width and channel count, a stride-1 1x3, and a 4x4 stride-4 patch embed (clip) over a+/// ragged map.+#[test]+#[ignore = "needs a Vulkan device"]+fn blocked_int8_convs_agree_with_the_reference() {+    #[allow(clippy::type_complexity)]+    let cases: [(&str, u32, u32, Shape, (u32, u32), (u32, u32), (u32, u32, u32, u32), u32, Act); 3] = [+        ("stride-2 1x3", 10, 6, Shape::new(6, 1, 37), (1, 3), (1, 2), (0, 1, 0, 1), 1, Act::Gelu),+        ("stride-1 1x3", 12, 5, Shape::new(5, 1, 29), (1, 3), (1, 1), (0, 1, 0, 1), 1, Act::None),+        ("4x4 stride-4", 6, 3, Shape::new(3, 18, 22), (4, 4), (4, 4), (0, 0, 0, 0), 1, Act::None),+    ];+    for (what, out_channels, in_channels, shape, kernel, stride, pad, group, act) in cases {+        let taps = (in_channels / group) * kernel.0 * kernel.1;+        let weights: Vec<i8> =+            (0..(out_channels * taps) as i32).map(|i| ((i * 37) % 251 - 125) as i8).collect();+        let scales: Vec<f32> =+            (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 5) as f32)).collect();+        let blob = write_mixed(+            graph::SUPERTONIC_VE,+            &[+                Fixture::I8(vec![out_channels, in_channels / group, kernel.0, kernel.1], weights),+                Fixture::F16(vec![out_channels], scales),+                Fixture::F16(vec![out_channels], spread(out_channels as usize, 0.5)),+            ],+        );+        let source = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");+        let input = spread(shape.len() as usize, 0.7);+        let mut builder = Builder::new(&source);+        let first = builder.input(shape);+        let last =+            builder.conv_int8(first, 0, out_channels, kernel, stride, (1, 1), pad, group, act);+        let plan = builder.finish(&[last]).expect("the blocked int8 plan builds");+        assert!(plan.ops.iter().any(|op| matches!(+            op,+            crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvInt8, push, .. }+                if push.in1 == 1+        )), "{what} missed the blocked path");+        compare(&format!("a blocked int8 {what}"), plan, source.data().to_vec(), &[&input]);+    }+}\ No newline at end of file