Modern Apps git

← Modern-Apps.git

ml: gemma4 plan build overlaps the combine, head modes warmed at load

Commit
64a9cd71f628be1b792908f9d3e3f2f58bbf96a1
Author
vayun-mathur <[email protected]>
When
2026-10-26 18:35 UTC (1 month ago)
Parents
8467638

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 5b5a465c84..1f4733a178 100644--- a/library/ml/src/main/rust/examples/check_gemma4_parity.rs+++ b/library/ml/src/main/rust/examples/check_gemma4_parity.rs@@ -563,6 +563,7 @@ struct PrefillTimes {     rows: f64,     combine: f64,     rotary: f64,+    plan_wait: f64,     record: f64,     gpu: f64,     /// The last token's final-normed state, which the head reads.@@ -591,20 +592,19 @@ fn prefill_app(         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 tick = Instant::now();-        let (rows, plan) = std::thread::scope(|scope| {-            let building = pending.map(|(plan, offsets)| scope.spawn(move || plan(&offsets, mode)));-            let rows = g::token_rows(embed, &fed).and_then(|h| Ok((h, g::layer_rows(embed, &fed)?)));-            (rows, building.map(|thread| thread.join().map_err(|_| "the plan thread panicked".to_string())))-        });-        let (hidden, embedded) = rows?;-        if let Some(plan) = plan {-            net.adopt(mode, plan??);-        }+        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)?)))?;         times.rows += tick.elapsed().as_secs_f64() * 1000.0;         let tick = Instant::now();         let per_layer = gather.at(width)?.infer_raw_many(&[&hidden, &embedded])?.into_iter().next().ok_or("no combine")?;         times.combine += tick.elapsed().as_secs_f64() * 1000.0;         let tick = Instant::now();+        if let Some(thread) = building {+            let plan = thread.join().map_err(|_| "the plan thread panicked".to_string())?;+            net.adopt(mode, plan?);+        }+        times.plan_wait += tick.elapsed().as_secs_f64() * 1000.0;+        let tick = Instant::now();         let mut local = vec![0f32; (gemma4::HEAD_DIM * width) as usize];         let mut global = vec![0f32; (gemma4::GLOBAL_HEAD_DIM * width) as usize];         for column in 0..width {@@ -659,6 +659,11 @@ fn time_prefill(         for width in [256, 1] {             gather.at(width)?;         }+        if let Some(head) = head_net.as_mut() {+            for out in [HeadOut::Logits, HeadOut::TopK, HeadOut::Greedy] {+                head.at(out)?;+            }+        }         Ok(())     })();     if let Err(why) = warmed {@@ -698,10 +703,10 @@ fn time_prefill(             };             let head_ms = tick.elapsed().as_secs_f64() * 1000.0;             println!(-                "{} prefill {width}+1 tokens: rows {:.0} + combine {:.0} + rotary {:.0} + record {:.0} + gpu {:.0} = {prefill_ms:.0} ms; \+                "{} prefill {width}+1 tokens: rows {:.0} + plan wait {:.0} + combine {:.0} + rotary {:.0} + record {:.0} + gpu {:.0} = {prefill_ms:.0} ms; \                  head {head_ms:.0} ms (id {id:?}); TTFT {:.0} ms",                 if run == 0 { "cold" } else { "warm" },-                p.rows, p.combine, p.rotary, p.record, p.gpu,+                p.rows, p.plan_wait, p.combine, p.rotary, p.record, p.gpu,                 prefill_ms + head_ms,             );         }
library/ml/src/main/rust/src/bridge_part11.rs
diff --git a/library/ml/src/main/rust/src/bridge_part11.rs b/library/ml/src/main/rust/src/bridge_part11.rsindex 22dae55ae0..69f3d198c1 100644--- a/library/ml/src/main/rust/src/bridge_part11.rs+++ b/library/ml/src/main/rust/src/bridge_part11.rs@@ -211,7 +211,15 @@ fn build_gemma4<'l>(     warm_gemma4(&mut net, gemma4::CONTEXT_TIERS[0])?;     // Never `Reshaped::streamed` over EMBED: it uploads the whole 3.6 GB file, which on top of     // TEXT OOM-kills a Pixel 8. The gather net is windowed to the tensors it reads.-    let head = head_file.as_ref().and_then(gemma4_head_file_plan);+    let mut head = head_file.as_ref().and_then(gemma4_head_file_plan);+    if let Some(head) = head.as_mut() {+        // Each mode's first recording costs ~70 ms on a Pixel 8; a sampled first token would pay+        // it. Logits first: it has the largest readback, so the staging grows once.+        use crate::nets::gemma4_head::HeadOut;+        for out in [HeadOut::Logits, HeadOut::TopK, HeadOut::Greedy] {+            head.at(out)?;+        }+    }     let mut gather_net = gemma4_gather_plan(&embed);     if let Some(gather) = gather_net.as_mut() {         gather.at(CHUNK)?;
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 4cbeb0f667..36da7fb141 100644--- a/library/ml/src/main/rust/src/bridge_part9.rs+++ b/library/ml/src/main/rust/src/bridge_part9.rs@@ -397,20 +397,16 @@ impl Gemma4Handle {         }         .at(self.context);         let pending = (!self.net.holds(mode)).then(|| self.net.planner());-        let embed = &self.embed;-        let (gathered, plan) = std::thread::scope(|scope| {-            let building = pending.map(|(plan, offsets)| scope.spawn(move || plan(&offsets, mode)));-            let gathered = rows(&embed.reader());-            let plan = building.map(|thread| thread.join().map_err(|_| "the plan thread panicked".to_string()));-            (gathered, plan)-        });-        let (hidden, embedded) = gathered?;-        if let Some(plan) = plan {-            self.net.adopt(mode, plan??);-        }+        // 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())?;         let gathered = started.elapsed();         let per_layer = self.combine_block(&hidden, &embedded, width)?;         let combined = started.elapsed() - gathered;+        if let Some(thread) = building {+            let plan = thread.join().map_err(|_| "the plan thread panicked".to_string())?;+            self.net.adopt(mode, plan?);+        }         let wide = width as usize;         let (l, g) = (gemma4::HEAD_DIM as usize, gemma4::GLOBAL_HEAD_DIM as usize);         let (mut local, mut global) = (vec![0f32; l * wide], vec![0f32; g * wide]);