Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / examples / common / gemma4_tower_time.rs
gemma4_tower_time.rs61 lines · 2.3 KiBRaw
| 1 | //! Shared by the Gemma 4 tower parity examples: `--time N` builds the net once, runs it `N` |
| 2 | //! times and prints wall time per run plus, with `MODELRUNNER_TIMESTAMPS=1`, a per-kind table. |
| 3 | |
| 4 | use modelrunner::vulkan::reshape::Reshaped; |
| 5 | use std::time::Instant; |
| 6 | |
| 7 | /// Build-free timing of an already-built net: one warm-up run, then `repeats` timed ones. |
| 8 | pub fn time_runs<S: Copy + PartialEq>( |
| 9 | net: &mut Reshaped<S>, |
| 10 | mode: S, |
| 11 | inputs: &[&[f32]], |
| 12 | repeats: usize, |
| 13 | ) -> Result<(), String> { |
| 14 | let mut walls = Vec::with_capacity(repeats); |
| 15 | for pass in 0..=repeats { |
| 16 | let tick = Instant::now(); |
| 17 | net.at(mode)?.infer_raw_many(inputs)?; |
| 18 | let ms = tick.elapsed().as_secs_f64() * 1000.0; |
| 19 | println!("run {pass}: {ms:.1} ms{}", if pass == 0 { " (first)" } else { "" }); |
| 20 | if pass > 0 { |
| 21 | walls.push(ms); |
| 22 | } |
| 23 | } |
| 24 | walls.sort_by(f64::total_cmp); |
| 25 | if let Some(median) = walls.get(walls.len() / 2) { |
| 26 | println!("median of {} warm runs: {median:.1} ms", walls.len()); |
| 27 | } |
| 28 | if modelrunner::knobs::is_set("timestamps") { |
| 29 | print_kinds(&net.at(mode)?.op_times()?); |
| 30 | } |
| 31 | Ok(()) |
| 32 | } |
| 33 | |
| 34 | /// Per-kind GPU table for one submit, plus its ten hottest dispatches. |
| 35 | fn print_kinds(report: &[(String, f64, f64)]) { |
| 36 | let mut per_kind: Vec<(String, usize, f64)> = Vec::new(); |
| 37 | let (mut total, mut gaps) = (0.0, 0.0); |
| 38 | for (label, us, gap) in report { |
| 39 | total += us; |
| 40 | gaps += gap; |
| 41 | let kind = label.split(':').nth(1).unwrap_or("?").trim().to_string(); |
| 42 | match per_kind.iter_mut().find(|(k, _, _)| *k == kind) { |
| 43 | Some((_, n, t)) => { |
| 44 | *n += 1; |
| 45 | *t += us; |
| 46 | } |
| 47 | None => per_kind.push((kind, 1, *us)), |
| 48 | } |
| 49 | } |
| 50 | per_kind.sort_by(|a, b| b.2.total_cmp(&a.2)); |
| 51 | println!("{} dispatches, {:.1} ms timestamped, {:.1} ms gaps", report.len(), total / 1000.0, gaps / 1000.0); |
| 52 | println!("{:>6} {:>10} {:>10} kind", "count", "total-ms", "mean-us"); |
| 53 | for (kind, n, us) in &per_kind { |
| 54 | println!("{n:>6} {:>10.1} {:>10.1} {kind}", us / 1000.0, us / *n as f64); |
| 55 | } |
| 56 | let mut hottest: Vec<&(String, f64, f64)> = report.iter().collect(); |
| 57 | hottest.sort_by(|a, b| b.1.total_cmp(&a.1)); |
| 58 | for (label, us, _) in hottest.into_iter().take(10) { |
| 59 | println!(" {us:>9.1} us {label}"); |
| 60 | } |
| 61 | } |