diff --git a/crates/cpu-worker/examples/ab_slots.rs b/crates/cpu-worker/examples/ab_slots.rs index cf05c655..f776cd0b 100644 --- a/crates/cpu-worker/examples/ab_slots.rs +++ b/crates/cpu-worker/examples/ab_slots.rs @@ -186,6 +186,19 @@ struct Comparison { identical: bool, } +/// The line of one finished request. +fn print_turn((pair, slots, a, t): (usize, usize, usize, usize), prompt_len: usize, r: &Run, same: bool) { + println!( + "pair {pair} {} slot(s) agent {a} turn {t}: {} prompt tokens, {} reused, prompt {:.0} ms, decode {:.0} ms, {} tokens, identical={same}", + slots, + prompt_len, + r.reused, + r.prompt_ms, + r.decode_ms, + r.tokens.len() + ); +} + /// Every agent's every turn in turn order with `slots` cache slots; the prompt seconds. fn run_scenario( engine: &mut engine::GptOssEngine, @@ -209,15 +222,7 @@ fn run_scenario( } let same = cmp.reference[index] == r.tokens; cmp.identical &= same; - println!( - "pair {pair} {} slot(s) agent {a} turn {t}: {} prompt tokens, {} reused, prompt {:.0} ms, decode {:.0} ms, {} tokens, identical={same}", - slots, - prompt.len(), - r.reused, - r.prompt_ms, - r.decode_ms, - r.tokens.len() - ); + print_turn((pair, slots, a, t), prompt.len(), &r, same); index += 1; } } @@ -238,16 +243,8 @@ fn report_totals(agents: usize, totals: &[Vec; 2], identical: bool) { ); } -fn main() { - let args: Vec = std::env::args().skip(1).collect(); - if args.is_empty() { - eprintln!( - "usage: ab_slots [agents] [turns] [pairs] [max_new] [threads] [header chars] [tool chars]" - ); - std::process::exit(2); - } - let st = parse_settings(&args); - let model = Path::new(&args[0]); +/// The prompts of every agent and turn, tokenized with the model's vocabulary, and the line that describes them. +fn prepare_prompts(model: &Path, st: &Settings) -> Vec>> { let size = std::fs::metadata(model).expect("model file").len(); let gguf = Gguf::read( BufReader::with_capacity(1 << 20, File::open(model).expect("open")), @@ -255,7 +252,7 @@ fn main() { ) .expect("valid GGUF"); let tokenizer = GgufBpe::from_gguf(&gguf).expect("tokenizer"); - let prompts = build_prompts(&tokenizer, &st); + let prompts = build_prompts(&tokenizer, st); let sizes: Vec> = prompts.iter().map(|p| p.iter().map(Vec::len).collect()).collect(); let shared = prompts[0][0] .iter() @@ -266,6 +263,20 @@ fn main() { "{} agents x {} turns, prompt tokens per agent and turn {sizes:?}, shared prefix {shared} tokens, max_new {}, {} threads", st.agents, st.turns, st.max_new, st.threads ); + prompts +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + if args.is_empty() { + eprintln!( + "usage: ab_slots [agents] [turns] [pairs] [max_new] [threads] [header chars] [tool chars]" + ); + std::process::exit(2); + } + let st = parse_settings(&args); + let model = Path::new(&args[0]); + let prompts = prepare_prompts(model, &st); let longest = prompts.iter().flatten().map(Vec::len).max().unwrap_or(0); let loaded = engine::load(&LoadOptions { model, diff --git a/crates/gpu-npu-worker/examples/split_prefill.rs b/crates/gpu-npu-worker/examples/split_prefill.rs index 2d09bacd..c8d70807 100644 --- a/crates/gpu-npu-worker/examples/split_prefill.rs +++ b/crates/gpu-npu-worker/examples/split_prefill.rs @@ -85,6 +85,12 @@ fn start_split(args: &[String], path: &Path, (gpu_batch, npu_batch): (usize, usi GpuNpuPrefill::new(gpu, npu).with_split_min_tokens(2) } +/// The timings of the split and of its NPU part. +fn print_split_timings(split: &GpuNpuPrefill) { + println!(" {}", split.timings().report()); + println!(" npu: {}", split.npu().timings().report()); +} + /// One prefill of `ids[prefix..]` after a GPU-only prefix, with the given NPU share: seconds, the next token and the /// last distribution. Timings are printed unless `warm`. fn run_once( @@ -114,8 +120,7 @@ fn run_once( } let next = model.next_from_hidden(&mut state, &hidden).expect("output projection"); if !warm && npu_share > 0.0 { - println!(" {}", split.timings().report()); - println!(" npu: {}", split.npu().timings().report()); + print_split_timings(split); } (seconds, next, log_softmax(&state.logits)) } @@ -130,6 +135,37 @@ fn report_ratios(gpu_times: &[f64], split_times: &[f64]) { ); } +/// The timed rounds, alternating which arm goes first, and the speed-up of the split over the GPU alone. +fn run_rounds(model: &Model, split: &mut GpuNpuPrefill, (ids, prefix): (&[u32], usize), (rounds, share): (usize, f64)) { + let rest_len = ids.len() - prefix; + let (mut gpu_times, mut split_times) = (Vec::new(), Vec::new()); + for r in 0..rounds { + let order = if r % 2 == 0 { [0.0, share] } else { [share, 0.0] }; + let mut results = Vec::new(); + for s in order { + let (seconds, next, lp) = run_once(model, split, (ids, prefix), (s, false)); + let rate = rest_len as f64 / seconds; + println!( + "round {r} {}: {seconds:.2} s ({rate:.1} tok/s), next {next}", + if s == 0.0 { "gpu alone" } else { "npu+gpu" } + ); + if s == 0.0 { + gpu_times.push(seconds) + } else { + split_times.push(seconds) + } + results.push((s, next, lp)); + } + let (a, b) = (&results[0].2, &results[1].2); + let kl: f64 = a.iter().zip(b).map(|(p, q)| p.exp() * (p - q)).sum(); + println!( + " last token: same top-1 {}, KL between the two arms {kl:.6}", + results[0].1 == results[1].1 + ); + } + report_ratios(&gpu_times, &split_times); +} + fn main() { let args: Vec = std::env::args().skip(1).collect(); if args.len() < 5 { @@ -156,30 +192,5 @@ fn main() { // one untimed run of each first: graphs, pipelines and caches run_once(&model, &mut split, (&ids, prefix), (0.0, true)); run_once(&model, &mut split, (&ids, prefix), (share, true)); - let (mut gpu_times, mut split_times) = (Vec::new(), Vec::new()); - for r in 0..rounds { - let order = if r % 2 == 0 { [0.0, share] } else { [share, 0.0] }; - let mut results = Vec::new(); - for s in order { - let (seconds, next, lp) = run_once(&model, &mut split, (&ids, prefix), (s, false)); - let rate = rest_len as f64 / seconds; - println!( - "round {r} {}: {seconds:.2} s ({rate:.1} tok/s), next {next}", - if s == 0.0 { "gpu alone" } else { "npu+gpu" } - ); - if s == 0.0 { - gpu_times.push(seconds) - } else { - split_times.push(seconds) - } - results.push((s, next, lp)); - } - let (a, b) = (&results[0].2, &results[1].2); - let kl: f64 = a.iter().zip(b).map(|(p, q)| p.exp() * (p - q)).sum(); - println!( - " last token: same top-1 {}, KL between the two arms {kl:.6}", - results[0].1 == results[1].1 - ); - } - report_ratios(&gpu_times, &split_times); + run_rounds(&model, &mut split, (&ids, prefix), (rounds, share)); }