ml: whisper stem 1x3 convs as the int8 GEMM, stem 148 -> 90 ms on Pixel
- Commit
d354135460d73bee746f89ccc366679cc5b204eb- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 01:08 UTC (1 month ago)
- Parents
ea856b5
Diff
library/ml/src/main/rust/shaders/conv_point_int8.comp
diff --git a/library/ml/src/main/rust/shaders/conv_point_int8.comp b/library/ml/src/main/rust/shaders/conv_point_int8.compindex 6f66caaa0b..ae6931c14b 100644--- a/library/ml/src/main/rust/shaders/conv_point_int8.comp+++ b/library/ml/src/main/rust/shaders/conv_point_int8.comp@@ -156,11 +156,86 @@ void split_rows() { } } +/// A one-row `1 x kw` convolution as this GEMM (`p.in1 == 2`): K walks (input channel, tap),+/// which is the weights' `[oc][ic][kx]` order, and the activation for position `x` and tap+/// `kx` is `in[ic][x * stride + kx * dil - pad_l]`, zero outside the row. Same summation order+/// as `conv_int8.comp`. Selected only when `in_c * kw % 16 == 0` and the weights are 16-byte+/// aligned (see `nets`).+void row_taps() {+ uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;+ if (tile >= p.count) {+ return;+ }+ uint positions = p.out_w;+ uint tiles_across = (positions + TILE - 1u) / TILE;+ uint lid = gl_LocalInvocationID.x;+ uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK;+ uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK;+ if (position_base >= positions) {+ return;+ }+ uint taps = p.in_c * p.kw;+ uint rows16[BLOCK];+ vec4 sum[BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ uint row = min(channel_base + i, p.out_c - 1u);+ rows16[i] = (p.weight + ((row * taps) >> 2u)) >> 2u;+ sum[i] = vec4(0.0);+ }+ ivec4 cols = (ivec4(position_base) + ivec4(0, 1, 2, 3)) * int(p.stride_w) - int(p.pad_l);+ int width = int(p.in_w);+ uint ic = 0u;+ uint kx = 0u;+ for (uint k = 0u; k < taps; k += 16u) {+ uvec4 w[BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ w[i] = weights128[rows16[i] + (k >> 4u)];+ }+ for (uint t = 0u; t < 16u; t++) {+ ivec4 c = cols + int(kx * p.dil_w);+ uint row = p.in0 + ic * p.in_w;+ vec4 x;+ for (uint j = 0u; j < BLOCK; j++) {+ x[j] = (c[j] >= 0 && c[j] < width) ? float(arena[row + uint(c[j])]) : 0.0;+ }+ for (uint i = 0u; i < BLOCK; i++) {+ float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8));+ sum[i] += tap * x;+ }+ kx++;+ if (kx == p.kw) {+ kx = 0u;+ ic++;+ }+ }+ }+ for (uint i = 0u; i < BLOCK; i++) {+ uint channel = channel_base + i;+ if (channel >= p.out_c) {+ continue;+ }+ float scale = float(weights[p.act_weight + channel]);+ float offset = float(weights[p.bias + channel]);+ for (uint j = 0u; j < BLOCK; j++) {+ if (position_base + j >= positions) {+ continue;+ }+ uint index = channel * positions + position_base + j;+ float value = quant_store(fused_store(sum[i][j] * scale + offset, p.act, channel, index));+ arena[p.out_at + index] = float16_t(value);+ }+ }+}+ void main() { if (p.in1 == 1u) { split_rows(); return; }+ if (p.in1 == 2u) {+ row_taps();+ return;+ } uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= 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 dc9dac0db9..50847f5801 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -70,10 +70,21 @@ impl<'a> Builder<'a> { (Quant::Q2K, true, false) => Kind::ConvPointQ2K, (Quant::Q2K, false, false) => Kind::ConvQ2K, };+ // A one-row 1 x kw int8 convolution (whisper's stem) as the register-blocked+ // GEMM with K = (ic, kx): `row_taps` in conv_point_int8.comp, `in1` = 2.+ let row_gemm = kind == Kind::ConvInt8+ && *group == 1+ && kernel.0 == 1+ && si.h == 1+ && so.h == 1+ && (si.c * kernel.1) % 16 == 0+ && *weight % 4 == 0;+ let kind = if row_gemm { Kind::ConvPointInt8 } else { kind }; // Too few 32 x 32 tiles to fill the GPU: two channels per invocation instead of // four, bit-identical. See `split_rows` in conv_point_int8.comp. On a Pixel 8, // splitting everything under 256 tiles took Supertonic's sampler 2199 -> 1521 ms. let split = kind == Kind::ConvPointInt8+ && !row_gemm && si.c % 16 == 0 && *weight % 4 == 0 && int8_tiles < CONV_POINT_INT8_SPLIT_BELOW;@@ -93,7 +104,7 @@ impl<'a> Builder<'a> { kind, push: Push { in0: at(*input)?,- in1: u32::from(split || int8_blocked),+ in1: if row_gemm { 2 } else { 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.
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 1691ebf587..fec17e9401 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -603,4 +603,43 @@ fn a_blocked_edge_padded_1x3_agrees_with_the_reference() { }); 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]);+}++/// Whisper's stem shape on `conv_point_int8.comp`'s row path (`in1` = 2): one-row 1x3 int8+/// convolutions padded by one, at stride 1 and 2, over ragged widths and channel counts.+#[test]+#[ignore = "needs a Vulkan device"]+fn row_int8_gemms_agree_with_the_reference() {+ for (what, out_channels, in_channels, width, stride, act) in [+ ("stride 1", 20u32, 16u32, 37u32, 1u32, Act::Gelu),+ ("stride 2", 36, 32, 45, 2, Act::None),+ ] {+ let weights: Vec<i8> = (0..(out_channels * in_channels * 3) 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, 1, 3], 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((in_channels * width) as usize, 0.9);+ let mut builder = Builder::new(&source);+ let first = builder.input(Shape::new(in_channels, 1, width));+ let last = builder.conv_int8(+ first, 0, out_channels, (1, 3), (1, stride), (1, 1), (0, 1, 0, 1), 1, act,+ );+ let plan = builder.finish(&[last]).expect("the row gemm plan builds");+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt8, push, .. }+ if push.in1 == 2+ )), "{what} missed the row path");+ compare(&format!("a row int8 gemm, {what}"), plan, source.data().to_vec(), &[&input]);+ } }\ No newline at end of file