diff --git a/crates/gpu-npu-worker/examples/split_prefill.rs b/crates/gpu-npu-worker/examples/split_prefill.rs index f674cbac..2d09bacd 100644 --- a/crates/gpu-npu-worker/examples/split_prefill.rs +++ b/crates/gpu-npu-worker/examples/split_prefill.rs @@ -26,15 +26,9 @@ fn log_softmax(logits: &[f32]) -> Vec { logits.iter().map(|l| f64::from(l - max) - z.ln()).collect() } -fn main() { - let args: Vec = std::env::args().skip(1).collect(); - if args.len() < 5 { - eprintln!( - "usage: split_prefill [rounds] [npu share] [prefix] [gpu batch] [npu batch]" - ); - std::process::exit(2); - } - let ids: Vec = if let Some(n) = args[4].strip_prefix("synth:") { +/// The token ids: `synth:N` makes N deterministic pseudo-random ids, else a file of comma separated ids. +fn parse_ids(arg: &str) -> Vec { + if let Some(n) = arg.strip_prefix("synth:") { let mut s = 0x2545_F491_4F6C_DD1Du64; (0..n.parse::().expect("synth:N")) .map(|_| { @@ -45,38 +39,32 @@ fn main() { }) .collect() } else { - std::fs::read_to_string(&args[4]) + std::fs::read_to_string(arg) .expect("ids file") .trim() .split(',') .map(|s| s.trim().parse().expect("token id")) .collect() - }; - let num = |i: usize, d: usize| args.get(i).and_then(|s| s.parse().ok()).unwrap_or(d); - let rounds = num(5, 2); - let share: f64 = args - .get(6) - .and_then(|s| s.parse().ok()) - .unwrap_or(DEFAULT_NPU_SHARE); - let prefix = num(7, 0).min(ids.len().saturating_sub(1)); - let gpu_batch = num(8, leleka_gptoss_igpu::DEFAULT_BATCH); - let npu_batch = num(9, leleka_gptoss_npu::DEFAULT_BATCH); - let path = Path::new(&args[0]); + } +} + +fn load_model(path: &Path) -> Model { let size = std::fs::metadata(path).expect("model file").len(); - let gguf = Gguf::read( - BufReader::with_capacity(1 << 20, File::open(path).expect("open")), - size, - ) - .expect("valid GGUF"); + let gguf = + Gguf::read(BufReader::with_capacity(1 << 20, File::open(path).expect("open")), size).expect("valid GGUF"); let map = Mapping::open(path).expect("map model"); map.touch_pages(); - let model = Model::load( + Model::load( std::sync::Arc::new(map), &gguf, Dispatch::from_env().expect("kernel level"), std::thread::available_parallelism().map_or(1, |n| n.get()), ) - .expect("load model"); + .expect("load model") +} + +/// The GPU and the NPU service together, ready to split prompts. +fn start_split(args: &[String], path: &Path, (gpu_batch, npu_batch): (usize, usize)) -> GpuNpuPrefill { let npu = NpuPrefill::start( &ServiceConfig { service: args[2].clone().into(), @@ -94,55 +82,87 @@ fn main() { .unwrap_or(0); let npu = npu.with_cpu_threads(npu_threads); let gpu = GpuPrefill::new().expect("a GPU").with_batch(gpu_batch); - let mut split = GpuNpuPrefill::new(gpu, npu).with_split_min_tokens(2); + GpuNpuPrefill::new(gpu, npu).with_split_min_tokens(2) +} + +/// 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( + model: &Model, + split: &mut GpuNpuPrefill, + (ids, prefix): (&[u32], usize), + (npu_share, warm): (f64, bool), +) -> (f64, u32, Vec) { let rest = &ids[prefix..]; - println!( - "{} tokens after a prefix of {prefix}; npu share {share} ({} tokens); gpu batch {gpu_batch}, npu batch {npu_batch}", - rest.len(), - ((rest.len() as f64 * share).round() as usize).min(rest.len() - 1) - ); - let mut run = |npu_share: f64, warm: bool| -> (f64, u32, Vec) { - let mut state = State::new(&model.config, ids.len() + 1); - if prefix > 0 { - split.set_npu_share(0.0); - split - .prefill(&model, &mut state, &ids[..prefix], 0, &mut |_| true) - .expect("prefix") - .expect("not cancelled"); - } - split.set_npu_share(npu_share); - let t = Instant::now(); - let hidden = split - .prefill(&model, &mut state, rest, prefix, &mut |_| true) - .expect("prefill") + let mut state = State::new(&model.config, ids.len() + 1); + if prefix > 0 { + split.set_npu_share(0.0); + split + .prefill(model, &mut state, &ids[..prefix], 0, &mut |_| true) + .expect("prefix") .expect("not cancelled"); - let seconds = t.elapsed().as_secs_f64(); - if !warm { - println!(" gpu: {}", split.gpu().timings().report()); - } - 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()); - } - (seconds, next, log_softmax(&state.logits)) - }; + } + split.set_npu_share(npu_share); + let t = Instant::now(); + let hidden = split + .prefill(model, &mut state, rest, prefix, &mut |_| true) + .expect("prefill") + .expect("not cancelled"); + let seconds = t.elapsed().as_secs_f64(); + if !warm { + println!(" gpu: {}", split.gpu().timings().report()); + } + 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()); + } + (seconds, next, log_softmax(&state.logits)) +} + +/// The paired speed-ups of the split over the GPU alone. +fn report_ratios(gpu_times: &[f64], split_times: &[f64]) { + let ratios: Vec = gpu_times.iter().zip(split_times).map(|(g, s)| g / s).collect(); + println!( + "paired speed-up of the split over the GPU alone: {:?} (mean {:.2})", + ratios.iter().map(|r| (r * 100.0).round() / 100.0).collect::>(), + ratios.iter().sum::() / ratios.len() as f64 + ); +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + if args.len() < 5 { + eprintln!( + "usage: split_prefill [rounds] [npu share] [prefix] [gpu batch] [npu batch]" + ); + std::process::exit(2); + } + let ids = parse_ids(&args[4]); + let num = |i: usize, d: usize| args.get(i).and_then(|s| s.parse().ok()).unwrap_or(d); + let rounds = num(5, 2); + let share: f64 = args.get(6).and_then(|s| s.parse().ok()).unwrap_or(DEFAULT_NPU_SHARE); + let prefix = num(7, 0).min(ids.len().saturating_sub(1)); + let gpu_batch = num(8, leleka_gptoss_igpu::DEFAULT_BATCH); + let npu_batch = num(9, leleka_gptoss_npu::DEFAULT_BATCH); + let path = Path::new(&args[0]); + let model = load_model(path); + let mut split = start_split(&args, path, (gpu_batch, npu_batch)); + let rest_len = ids.len() - prefix; + println!( + "{rest_len} tokens after a prefix of {prefix}; npu share {share} ({} tokens); gpu batch {gpu_batch}, npu batch {npu_batch}", + ((rest_len as f64 * share).round() as usize).min(rest_len - 1) + ); // one untimed run of each first: graphs, pipelines and caches - run(0.0, true); - run(share, true); + 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 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(s, false); - let rate = rest.len() as f64 / seconds; + 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" } @@ -161,17 +181,5 @@ fn main() { results[0].1 == results[1].1 ); } - let ratios: Vec = gpu_times - .iter() - .zip(&split_times) - .map(|(g, s)| g / s) - .collect(); - println!( - "paired speed-up of the split over the GPU alone: {:?} (mean {:.2})", - ratios - .iter() - .map(|r| (r * 100.0).round() / 100.0) - .collect::>(), - ratios.iter().sum::() / ratios.len() as f64 - ); + report_ratios(&gpu_times, &split_times); }