ml: grouped int8 1x1 at one position, wide loads; maia 31 -> 18 ms on Pixel
- Commit
173a480072e2a518e20b9535d7b5639cc06712e0- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 02:02 UTC (1 month ago)
- Parents
1d638b2
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 f68918023a..c4b3c11ee0 100644--- a/library/ml/src/main/rust/shaders/conv_int8.comp+++ b/library/ml/src/main/rust/shaders/conv_int8.comp@@ -105,11 +105,42 @@ void tiled_int8() { } } +// A grouped single-position 1x1 (`p.in1 == 2`, maia's attention-bias expansion): one output per+// invocation, its group's taps read 16 at a time through the `uvec4` and quad views but summed+// one by one in `main`'s order, so the result is identical. `nets` checks the alignment.+void grouped_rows() {+ uint oc = global_index();+ if (oc >= p.out_c) {+ return;+ }+ uint per_group = p.in_c / p.group;+ uint group = oc / (p.out_c / p.group);+ uint row16 = (p.weight + ((oc * per_group) >> 2u)) >> 2u;+ uint quad = (p.in0 + group * per_group) >> 2u;+ float total = 0.0;+ for (uint k = 0u; k < per_group; k += 16u) {+ uvec4 w = weights128[row16 + (k >> 4u)];+ for (uint q = 0u; q < 4u; q++) {+ vec4 x = vec4(arena_quad[quad + (k >> 2u) + q]);+ for (uint t = 0u; t < 4u; t++) {+ total += float(bitfieldExtract(int(w[q]), int(t << 3u), 8)) * x[t];+ }+ }+ }+ float scale = float(weights[p.act_weight + oc]);+ float biased = total * scale + float(weights[p.bias + oc]);+ arena[p.out_at + oc] = float16_t(quant_store(fused_store(biased, p.act, oc, oc)));+}+ void main() { if (p.in1 == 1u) { tiled_int8(); return; }+ if (p.in1 == 2u) {+ grouped_rows();+ 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 f65b1510bf..781ec884e7 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -99,6 +99,15 @@ impl<'a> Builder<'a> { && *weight % 4 == 0 && positions % 4 == 0 && input_at % 4 == 0;+ // A grouped single-position 1x1 (maia's bias expansion): `grouped_rows` in+ // conv_int8.comp, `in1` = 2, wide loads of each group's taps.+ let grouped_vec = kind == Kind::ConvInt8+ && *group > 1+ && positions == 1+ && kernel == &(1, 1)+ && (si.c / *group) % 16 == 0+ && *weight % 4 == 0+ && input_at % 4 == 0; // Workgroups for the staged kinds, output elements for the untiled one. let count = match kind { Kind::ConvVecInt8 | Kind::ConvVecInt4 | Kind::ConvVecQ2K => rows,@@ -113,7 +122,11 @@ impl<'a> Builder<'a> { kind, push: Push { in0: input_at,- in1: if row_gemm { 2 } else { u32::from(split || int8_blocked || int4_wide) },+ in1: if row_gemm || grouped_vec {+ 2+ } else {+ u32::from(split || int8_blocked || int4_wide)+ }, 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_part10.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part10.rs b/library/ml/src/main/rust/src/vulkan/parity_part10.rsindex e7916df1b9..2cff3edd4a 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part10.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part10.rs@@ -253,4 +253,42 @@ fn a_split_pointwise_conv_agrees_with_the_reference() { crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, push, .. } if push.in1 == 1 ))); compare("a split pointwise conv", plan, source.into_data(), &[&input]);+}++/// `conv_int8.comp`'s grouped single-position path (`in1` = 2, maia's attention-bias+/// expansion): 4 groups of 16 taps and 2 groups of 32, each group with 10 output channels.+#[test]+#[ignore = "needs a Vulkan device"]+fn grouped_int8_vectors_agree_with_the_reference() {+ for (in_channels, groups) in [(64u32, 4u32), (64, 2)] {+ let out_channels = 10 * groups;+ let taps = in_channels / groups;+ 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, taps, 1, 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(in_channels as usize, 0.9);+ let mut builder = Builder::new(&source);+ let first = builder.input(Shape::new(in_channels, 1, 1));+ let last = builder.conv_int8(+ first, 0, out_channels, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), groups, Act::None,+ );+ let plan = builder.finish(&[last]).expect("the grouped plan builds");+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvInt8, push, .. }+ if push.in1 == 2+ )), "{groups} groups missed the grouped path");+ let what = format!("a grouped int8 vector, {groups} groups");+ compare(&what, plan, source.data().to_vec(), &[&input]);+ } }\ No newline at end of file