Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / examples / profile_gemma4_decode.rs
profile_gemma4_decode.rs130 lines · 5.2 KiBRaw
| 1 | //! Profile Gemma-4's decode step: op mix, weight traffic, and the two ceilings. |
| 2 | //! |
| 3 | //! ```text |
| 4 | //! cargo run --offline --release -p modelrunner --example profile_gemma4_decode |
| 5 | //! ``` |
| 6 | //! |
| 7 | //! # Why |
| 8 | //! |
| 9 | //! P8 measures 554 ms/token steady-state (1.80 tok/s) against LiteRT's ~10 tok/s. |
| 10 | //! This attributes that number host-side (no device, no weights — the plan is |
| 11 | //! built against synthetic shapes, exactly as `report_barriers` does): per-Kind |
| 12 | //! op counts, per-Kind weight bytes read per step, total weight traffic per |
| 13 | //! step, and the dispatch-rate ceiling the op count implies. What it cannot do |
| 14 | //! is split a step into dispatch overhead vs kernel execution — that needs |
| 15 | //! on-device timestamps the runtime does not emit yet. |
| 16 | use modelrunner::nets::{gemma4, Kind, Op, WeightSource}; |
| 17 | |
| 18 | /// A [`WeightSource`] that invents offsets and no data. Same trick as |
| 19 | /// `report_barriers::Shapes`: plan-building never looks at a weight value. |
| 20 | struct Shapes { |
| 21 | count: usize, |
| 22 | next: std::cell::Cell<u32>, |
| 23 | } |
| 24 | |
| 25 | impl WeightSource for Shapes { |
| 26 | fn shaped(&self, _index: usize, dims: &[u32]) -> Result<u32, String> { |
| 27 | let elements: u32 = dims.iter().copied().product::<u32>().max(1); |
| 28 | let at = self.next.get(); |
| 29 | self.next.set(at + elements.next_multiple_of(8)); |
| 30 | Ok(at) |
| 31 | } |
| 32 | |
| 33 | fn shaped_words(&self, _index: usize, dims: &[u32]) -> Result<u32, String> { |
| 34 | let elements: u32 = dims.iter().copied().product::<u32>().max(1); |
| 35 | let at = self.next.get(); |
| 36 | self.next.set(at + elements.div_ceil(4).next_multiple_of(8)); |
| 37 | Ok(at) |
| 38 | } |
| 39 | |
| 40 | fn count(&self) -> usize { |
| 41 | self.count |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Weight bytes one op reads per decode step, from the geometry the builder |
| 46 | /// resolved (`Push` carries in/out channels + positions; scales/biases are |
| 47 | /// small beside the kernels and are folded into the per-kind factor). |
| 48 | fn weight_bytes(kind: &Kind, push: &modelrunner::nets::Push) -> u64 { |
| 49 | let taps = u64::from(push.in_c) * u64::from(push.kh).max(1) * u64::from(push.kw).max(1); |
| 50 | let chans = u64::from(push.out_c); |
| 51 | let positions = u64::from(push.out_w).max(1); |
| 52 | // Decode: one position. Prefill shapes would multiply by positions — |
| 53 | // decode only, stated. |
| 54 | let _ = positions; |
| 55 | match kind { |
| 56 | Kind::ConvPointInt4 | Kind::ConvVecInt4 => chans * taps / 2 + chans * taps / 32, |
| 57 | Kind::ConvInt8 | Kind::ConvPointInt8 | Kind::ConvVecInt8 => { |
| 58 | chans * taps + chans * 2 |
| 59 | } |
| 60 | Kind::ConvQ2K | Kind::ConvPointQ2K | Kind::ConvVecQ2K => chans * taps / 4, |
| 61 | Kind::Conv | Kind::ConvPoint => chans * taps * 2, |
| 62 | // Norms/gammas: one vector of out_c fp16. |
| 63 | Kind::RmsNorm | Kind::LayerNorm => chans * 2, |
| 64 | // Everything else reads no weights (caches, softmaxes, elementwise). |
| 65 | _ => 0, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn main() { |
| 70 | let source = Shapes { count: 1 << 20, next: std::cell::Cell::new(0) }; |
| 71 | let plan = match gemma4::build(&source, gemma4::Mode::DecodeStep.at(4096)) { |
| 72 | Ok(plan) => plan, |
| 73 | Err(why) => { |
| 74 | // Same two-attempt dance as report_barriers::synthetic. |
| 75 | let wanted = why |
| 76 | .split("never reads tensor ") |
| 77 | .nth(1) |
| 78 | .and_then(|rest| rest.split(' ').next()) |
| 79 | .and_then(|n| n.parse::<usize>().ok()); |
| 80 | match wanted { |
| 81 | Some(n) => { |
| 82 | let retry = |
| 83 | Shapes { count: n, next: std::cell::Cell::new(0) }; |
| 84 | match gemma4::build(&retry, gemma4::Mode::DecodeStep.at(4096)) { |
| 85 | Ok(plan) => plan, |
| 86 | Err(why) => return println!("the decode plan does not build: {why}"), |
| 87 | } |
| 88 | } |
| 89 | None => return println!("the decode plan does not build: {why}"), |
| 90 | } |
| 91 | } |
| 92 | }; |
| 93 | println!("decode plan: {} ops, {} barriers today", plan.ops.len(), plan.ops.len()); |
| 94 | let mut per_kind: Vec<(Kind, usize, u64)> = Vec::new(); |
| 95 | for op in &plan.ops { |
| 96 | let Op::Dispatch { kind, push, .. } = op else { continue }; |
| 97 | let bytes = weight_bytes(kind, push); |
| 98 | match per_kind.iter_mut().find(|(k, _, _)| k == kind) { |
| 99 | Some((_, n, b)) => { |
| 100 | *n += 1; |
| 101 | *b += bytes; |
| 102 | } |
| 103 | None => per_kind.push((*kind, 1, bytes)), |
| 104 | } |
| 105 | } |
| 106 | per_kind.sort_by(|a, b| b.2.cmp(&a.2)); |
| 107 | println!(); |
| 108 | println!("{:>6} {:>10} {:?}", "count", "wt-bytes", "kind"); |
| 109 | let mut total_bytes = 0u64; |
| 110 | for (kind, n, bytes) in &per_kind { |
| 111 | println!("{n:>6} {bytes:>10} {kind:?}"); |
| 112 | total_bytes += bytes; |
| 113 | } |
| 114 | println!(); |
| 115 | println!("total weight traffic per step: ~{:.1} MB", total_bytes as f64 / 1e6); |
| 116 | println!("arena: {:.1} MB", f64::from(plan.arena_elems) * 2.0 / 1e6); |
| 117 | println!(); |
| 118 | println!("ceilings at P8-measured 587 us/dispatch (plan) vs 5 GB/s buffers:"); |
| 119 | println!( |
| 120 | " dispatch ceiling: {} ops x 587 us = {:.0} ms", |
| 121 | plan.ops.len(), |
| 122 | plan.ops.len() as f64 * 0.587 |
| 123 | ); |
| 124 | println!( |
| 125 | " bandwidth ceiling: {:.1} MB / 5 GB/s = {:.0} ms", |
| 126 | total_bytes as f64 / 1e6, |
| 127 | total_bytes as f64 / 5e9 * 1e3 |
| 128 | ); |
| 129 | println!(" measured on-device: 554 ms/token steady-state"); |
| 130 | } |