ml: keep gemma4 nets files under the 500-line limit
- Commit
a52c520ed57ae47f8db89915ff99571013fc3248- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 00:42 UTC (1 month ago)
- Parents
d563dbe
Diff
library/ml/src/main/rust/src/nets/gemma4_part1.rs
diff --git a/library/ml/src/main/rust/src/nets/gemma4_part1.rs b/library/ml/src/main/rust/src/nets/gemma4_part1.rsindex a119bdc3c8..ed335caf2e 100644--- a/library/ml/src/main/rust/src/nets/gemma4_part1.rs+++ b/library/ml/src/main/rust/src/nets/gemma4_part1.rs@@ -343,16 +343,6 @@ fn name_layer(b: &mut Builder, index: usize) { /// being written the same way: every projection, norm, activation and residual here is an /// elementwise or per-position op that already took a `[C, 1, T]` shape. ///-/// Where a prefill layer's queries find their keys.-#[derive(Clone, Copy)]-enum Attend {- /// The KV cache: every position before the chunk plus the chunk itself. Correct anywhere.- Cache,- /// The chunk's own K and V (a shared layer's carried in from its owner), masked causally.- /// Only for a chunk starting at position 0, where the two are the same keys.- Chunk(Option<(Id, Id)>),-}- /// Returns the layer's output and, for a layer that owns a cache, its `(K, V)` so the shared /// layers after it can attend over them in [`Attend::Chunk`]. #[allow(clippy::too_many_arguments)]
library/ml/src/main/rust/src/nets/gemma4_part3.rs
diff --git a/library/ml/src/main/rust/src/nets/gemma4_part3.rs b/library/ml/src/main/rust/src/nets/gemma4_part3.rsindex 39e2f6e26e..72c0687036 100644--- a/library/ml/src/main/rust/src/nets/gemma4_part3.rs+++ b/library/ml/src/main/rust/src/nets/gemma4_part3.rs@@ -233,10 +233,8 @@ mod tests { #[test] fn a_batched_prefill_widens_every_stage_and_attends_the_cache() {- // The whole chunk in one submit, so the projections become GEMMs. Every layer's- // attention reads the KV cache with one row per query (`attn_cached_multi`): queries- // after position 0 must see what earlier submits wrote, and a plan that attended only its- // own chunk produced fluent, wrong text with no shape error.+ // Projections become GEMMs; attention reads the KV cache, one row per query, so queries+ // after position 0 see earlier submits (attending only the chunk was fluent and wrong). for tokens in [2u32, 8, 64] { let plan = build(&Shapes::new(TENSORS), Mode::Prefill { tokens }.at(TEST_CONTEXT)) .unwrap_or_else(|e| panic!("{tokens} positions: {e}"));@@ -276,22 +274,12 @@ mod tests { // At position 0 the chunk is all there is, so it attends itself with the T x T kernels. let fresh = build(&Shapes::new(TENSORS), Mode::PrefillFresh { tokens }.at(TEST_CONTEXT)) .unwrap_or_else(|e| panic!("fresh {tokens} positions: {e}"));- let (mut causal, mut windowed) = (0, 0);- for op in &fresh.ops {- let Op::Dispatch { kind, push, .. } = op else { continue };- match kind {- Kind::SoftmaxCausal => {- causal += 1;- windowed += usize::from(push.kh == WINDOW);- assert_eq!((push.out_h, push.out_w), (tokens, tokens), "T x T scores");- }- Kind::AttnScoresCached | Kind::AttnApplyCached => {- panic!("a fresh prefill must not read the cache")- }- _ => {}- }- }- assert_eq!((causal, windowed), (LAYERS, 28));+ let kinds = || fresh.ops.iter().filter_map(|op| match op {+ Op::Dispatch { kind, push, .. } => Some((*kind, push)), _ => None });+ assert!(kinds().all(|(k, _)| !matches!(k, Kind::AttnScoresCached | Kind::AttnApplyCached)));+ let causal: Vec<_> = kinds().filter(|(k, _)| *k == Kind::SoftmaxCausal).collect();+ assert!(causal.iter().all(|(_, p)| (p.out_h, p.out_w) == (tokens, tokens)), "T x T scores");+ assert_eq!((causal.len(), causal.iter().filter(|(_, p)| p.kh == WINDOW).count()), (LAYERS, 28)); } assert!(build(&Shapes::new(TENSORS), Mode::Prefill { tokens: 0 }.at(TEST_CONTEXT)).is_err()); let past = MAX_CONTEXT + 1;
library/ml/src/main/rust/src/nets/gemma4_part7.rs
diff --git a/library/ml/src/main/rust/src/nets/gemma4_part7.rs b/library/ml/src/main/rust/src/nets/gemma4_part7.rsindex f457ddb277..92357eaedd 100644--- a/library/ml/src/main/rust/src/nets/gemma4_part7.rs+++ b/library/ml/src/main/rust/src/nets/gemma4_part7.rs@@ -68,3 +68,13 @@ impl Builder<'_> { out } }++/// Where a prefill layer's queries find their keys.+#[derive(Clone, Copy)]+enum Attend {+ /// The KV cache: every position before the chunk plus the chunk itself. Correct anywhere.+ Cache,+ /// The chunk's own K and V (a shared layer's carried in from its owner), masked causally.+ /// Only for a chunk starting at position 0, where the two are the same keys.+ Chunk(Option<(Id, Id)>),+}