Modern Apps git

← Modern-Apps.git

ml: gemma4 builds a fused prefill plan inline; cold 72-token TTFT 912 -> 675 ms

Commit
1aa74686a6621210e5d90ba2e56ef9adb54181c3
Author
vayun-mathur <[email protected]>
When
2026-10-26 19:23 UTC (1 month ago)
Parents
756a5b7

Diff

library/ml/src/main/rust/examples/check_gemma4_parity.rs
diff --git a/library/ml/src/main/rust/examples/check_gemma4_parity.rs b/library/ml/src/main/rust/examples/check_gemma4_parity.rsindex 2040307bc4..a68db08f56 100644--- a/library/ml/src/main/rust/examples/check_gemma4_parity.rs+++ b/library/ml/src/main/rust/examples/check_gemma4_parity.rs@@ -610,7 +610,15 @@ fn prefill_app(         let mut fed = chunk.to_vec();         fed.resize(width as usize, 0);         let mode = if start == 0 { gemma4::Mode::PrefillFresh { tokens: width } } else { gemma4::Mode::Prefill { tokens: width } }.at(TIER);-        let pending = (!net.holds(mode)).then(|| net.planner());+        let mut pending = (!net.holds(mode)).then(|| net.planner());+        let tick = Instant::now();+        if fused() {+            // As `Gemma4Handle::prefill_chunk`: built on this thread, which keeps its core awake.+            if let Some((plan, offsets)) = pending.take() {+                net.adopt(mode, plan(&offsets, mode)?);+            }+        }+        times.plan_wait += tick.elapsed().as_secs_f64() * 1000.0;         let tick = Instant::now();         let building = pending.map(|(plan, offsets)| std::thread::spawn(move || plan(&offsets, mode)));         let (hidden, embedded) = g::token_rows(embed, &fed).and_then(|h| Ok((h, g::layer_rows(embed, &fed)?)))?;
library/ml/src/main/rust/src/bridge_part9.rs
diff --git a/library/ml/src/main/rust/src/bridge_part9.rs b/library/ml/src/main/rust/src/bridge_part9.rsindex c500ea3fef..9d97da2017 100644--- a/library/ml/src/main/rust/src/bridge_part9.rs+++ b/library/ml/src/main/rust/src/bridge_part9.rs@@ -396,7 +396,14 @@ impl Gemma4Handle {             gemma4::Mode::Prefill { tokens: width }         }         .at(self.context);-        let pending = (!self.net.holds(mode)).then(|| self.net.planner());+        let mut pending = (!self.net.holds(mode)).then(|| self.net.planner());+        if self.fused() {+            // Nothing left to overlap with, and a thread blocked in `join` lets this core clock+            // down: on a Pixel 8 that made the record ~5x slower (23 -> 120 ms).+            if let Some((plan, offsets)) = pending.take() {+                self.net.adopt(mode, plan(&offsets, mode)?);+            }+        }         // Owns its inputs, so it keeps building through the rows and the combine below.         let building = pending.map(|(plan, offsets)| std::thread::spawn(move || plan(&offsets, mode)));         let (hidden, embedded) = rows(&self.embed.reader())?;