ml: pad gemma4 audio tower to 8 positions, 30 s clip 2766 -> 1467 ms on Pixel
- Commit
713693ffbb948fb3704981b046c54df4e8f807d5- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 05:55 UTC (1 month ago)
- Parents
7b00abd
Diff
library/ml/src/main/rust/src/nets/gemma4_audio_part1.rs
diff --git a/library/ml/src/main/rust/src/nets/gemma4_audio_part1.rs b/library/ml/src/main/rust/src/nets/gemma4_audio_part1.rsindex 361693cb0c..a6023bdf37 100644--- a/library/ml/src/main/rust/src/nets/gemma4_audio_part1.rs+++ b/library/ml/src/main/rust/src/nets/gemma4_audio_part1.rs@@ -259,10 +259,17 @@ pub fn build(weights: &dyn WeightSource, mode: Mode) -> Result<Plan, String> { return builder.finish(&[sscp_out, projected]); } - let mut x = projected;+ // Layers run padded to a multiple of eight positions so the int4 GEMMs take their 16-byte+ // load path. Exact: the attention band and the depthwise conv only read earlier positions,+ // and every other op is per position.+ let padded = seq.next_multiple_of(8);+ let mut x = if padded == seq { projected } else { b.pad_positions(projected, padded) }; for index in 0..stop_after { x = layer(b, index, x)?; }+ if padded != seq {+ x = b.crop_positions(x, seq);+ } for index in stop_after..LAYERS { declare_layer(weights, index)?; name_layer(b, index);
library/ml/src/main/rust/src/nets/mod_part6.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part6.rs b/library/ml/src/main/rust/src/nets/mod_part6.rsindex f60e057ef7..c4297fbc26 100644--- a/library/ml/src/main/rust/src/nets/mod_part6.rs+++ b/library/ml/src/main/rust/src/nets/mod_part6.rs@@ -315,6 +315,18 @@ impl<'a> Builder<'a> { out } + /// `input` with zero positions appended up to `count`: the same 1x1 average pool, whose+ /// windows past the input's width sum nothing.+ pub fn pad_positions(&mut self, input: Id, count: u32) -> Id {+ let shape = self.shape_of(input);+ if count < shape.w {+ self.fail(format!("{shape:?} padded to {count} positions"));+ }+ let out = self.tensor(Shape::new(shape.c, shape.h, count));+ self.nodes.push(Node::AvgPool { input, out, kernel: (1, 1), stride: (1, 1) });+ out+ }+ /// [`Builder::softmax`] with each query's row truncated at the diagonal. /// /// The input must be a square score map `[heads, T, T]`: a causal mask is a statement about
library/ml/src/main/rust/src/vulkan/parity_part12.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part12.rs b/library/ml/src/main/rust/src/vulkan/parity_part12.rsindex 84edba3f60..6bf913e1ff 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part12.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part12.rs@@ -47,5 +47,9 @@ fn key_bounded_softmax_and_crop_agree_with_the_reference() { let source = Invented::new(0); let plan = build(&source, &[Shape::new(heads, queries, width)], |b, ids| b.softmax_keys(ids[0], keys)); compare(&format!("softmax over {keys} of {width} keys, zero tail"), plan, source.into_data(), &[&scores]);+ let source = Invented::new(0);+ let plan = build(&source, &[Shape::new(heads, queries, keys)], |b, ids| b.pad_positions(ids[0], width));+ let real = spread((heads * queries * keys) as usize, 0.29);+ compare(&format!("{keys} positions padded to {width}"), plan, source.into_data(), &[&real]); } }