Files
leleka/crates/gpu-npu-worker/examples/dump_logprobs_split.rs
T
Pavlo RatushnyiandClaude Opus 5.5 3ebcb3ffd6 experiment: one prompt prefilled by the iGPU and the NPU together; measured, no gain (STRA-220)
leleka-gpu-npu-worker (crates/gpu-npu-worker) splits a long prompt: the NPU path takes the
first share of the tokens and the GPU path the rest, on two threads. They meet only in the
KV cache, through the new leleka_gptoss::LayerCache, which both prefill paths now use instead
of &mut State (same behaviour). The GPU part's cache waits for each layer the NPU part
stores, so the GPU runs layer l while the NPU runs l+1. The last-token distribution matches
the GPU alone (KL <= 1e-4).

Measured: 2048 tokens 0.89-1.13x against the GPU alone, depending on share; agent episode
(4 turns of ~2,060 new tokens, 6 alternating pairs) 0.81-1.16x per pair, a net loss. Both
paths stream all weights whatever their token share and share the 15 W budget, and the NPU
part's CPU attention over the growing context is the slow side. Not adopted; stays
experimental. Also: NpuPrefill::with_cpu_threads, and -ArmA/-ArmB/-LogDir in
Compare-PrefillWorkers.ps1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 15:42:12 +02:00

97 lines
3.9 KiB
Rust

//! Teacher-forced log-probabilities with the prompt prefilled by the NPU and the GPU together (the first `share` of
//! the tokens through the NPU service, the rest on the GPU): the format of `dump_logprobs`, for `compare_logprobs`.
//! Every position's hidden state comes from the split prefill; the output projection runs on the CPU.
//!
//! Run: `cargo run --release -p leleka-gpu-npu-worker --example dump_logprobs_split -- <model.gguf> <openvino root> <service exe> <device> <ids.txt> <out.jsonl> [top_n] [npu share]`
use leleka_cpu_kernels::Dispatch;
use leleka_cpu_sys::mapping::Mapping;
use leleka_gguf::Gguf;
use leleka_gptoss::{Model, State};
use leleka_gptoss_igpu::GpuPrefill;
use leleka_gptoss_npu::NpuPrefill;
use leleka_gptoss_npu::client::ServiceConfig;
use leleka_gpu_npu_worker::{DEFAULT_NPU_SHARE, GpuNpuPrefill};
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
use std::path::Path;
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
if args.len() < 6 {
eprintln!(
"usage: dump_logprobs_split <model.gguf> <openvino root> <service exe> <device> <ids.txt> <out.jsonl> [top_n] [npu share]"
);
std::process::exit(2);
}
let ids: Vec<u32> = std::fs::read_to_string(&args[4])
.expect("ids file")
.trim()
.split(',')
.map(|s| s.trim().parse().expect("token id"))
.collect();
let top_n: usize = args.get(6).and_then(|s| s.parse().ok()).unwrap_or(20);
let share: f64 = args
.get(7)
.and_then(|s| s.parse().ok())
.unwrap_or(DEFAULT_NPU_SHARE);
let path = Path::new(&args[0]);
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 model = Model::load(
std::sync::Arc::new(Mapping::open(path).expect("map model")),
&gguf,
Dispatch::from_env().expect("kernel level"),
std::thread::available_parallelism().map_or(1, |n| n.get()),
)
.expect("load model");
let e = model.config.n_embd;
let mut state = State::new(&model.config, ids.len() + 1);
let config = ServiceConfig {
service: args[2].clone().into(),
openvino_root: args[1].clone().into(),
device: args[3].clone(),
model: path.to_path_buf(),
cache_dir: None,
};
let npu = NpuPrefill::start(&config, leleka_gptoss_npu::DEFAULT_BATCH)
.expect("start the NPU service");
let mut accel = GpuNpuPrefill::new(GpuPrefill::new().expect("a GPU"), npu)
.with_npu_share(share)
.with_split_min_tokens(2);
let hidden = accel
.prefill_all_hidden(&model, &mut state, &ids, 0)
.expect("split prefill");
eprintln!("{}", accel.timings().report());
let mut out = BufWriter::new(File::create(&args[5]).expect("output file"));
for pos in 0..ids.len() {
model
.next_from_hidden(&mut state, &hidden[pos * e..(pos + 1) * e])
.expect("output projection");
let logits = &state.logits;
let max = logits.iter().copied().fold(f32::NEG_INFINITY, f32::max);
let z: f64 = logits.iter().map(|l| f64::from(l - max).exp()).sum();
let mut order: Vec<usize> = (0..logits.len()).collect();
order.sort_by(|a, b| logits[*b].total_cmp(&logits[*a]).then(a.cmp(b)));
let top: Vec<String> = order
.iter()
.take(top_n)
.map(|i| format!("[{},{:.6}]", i, f64::from(logits[*i] - max) - z.ln()))
.collect();
let next = ids
.get(pos + 1)
.map_or("null".to_owned(), |n| n.to_string());
writeln!(
out,
"{{\"pos\":{pos},\"next\":{next},\"top\":[{}]}}",
top.join(",")
)
.expect("write");
}
eprintln!("wrote {} positions", ids.len());
}