Files
leleka/crates/cpu-worker/tests/real_model.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 7a78e87ea8 feat: add leleka-igpu-worker, the CPU worker with GPU prefill for long prompts (STRA-219, phase D)
- cpu-worker: GptOssEngine can take a PrefillAccelerator (falls back to the CPU and drops it on failure)
- igpu-worker: separate executable, same protocol, --gpu-min-tokens/--gpu-batch; CPU-only with a message when
  no GPU is usable
- real-model worker tests no longer assert llama.cpp's later greedy tokens (near-ties that moved with the
  attention/KV/router changes of STRA-214; the first token and the reuse/determinism checks stay); the
  teacher-forced gate is the comparison with llama.cpp

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-15 00:21:20 +02:00

115 lines
4.2 KiB
Rust

//! End to end with the real gpt-oss-20b file: spawn the worker, speak the line protocol to it.
//!
//! Needs `LELEKA_GPTOSS_GGUF=<path to gpt-oss-20b-MXFP4.gguf>` (about 12 GB); skipped otherwise.
//! The first token is llama.cpp's greedy answer to "The capital of France is"; the rest checks the
//! protocol, the prefix reuse and determinism.
use std::io::{BufRead, BufReader, Write};
use std::process::{Child, ChildStdin, Command, Stdio};
const PROMPT: [u32; 5] = [976, 9029, 328, 10128, 382];
/// llama.cpp's first greedy token after "The capital of France is". The later tokens of the continuation
/// are near-ties that tiny numeric changes flip (the decoder's own history moved them), so they are
/// not asserted here; `benchmarks/quality/gpt-oss-20b-teacher-forced` is the comparison with llama.cpp.
const FIRST_TOKEN: u32 = 12650;
struct Worker {
child: Child,
stdin: ChildStdin,
stdout: BufReader<std::process::ChildStdout>,
}
impl Worker {
fn start(model: &str) -> Self {
let mut child = Command::new(env!("CARGO_BIN_EXE_leleka-cpu-worker"))
.args([
"--serve",
"--model",
model,
"--threads",
"4",
"--ctx",
"512",
"--no-warm",
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect("spawn the worker");
let stdin = child.stdin.take().unwrap();
let mut stdout = BufReader::new(child.stdout.take().unwrap());
let mut line = String::new();
stdout.read_line(&mut line).expect("READY line");
assert_eq!(line.trim_end(), "READY 512 stop");
Self {
child,
stdin,
stdout,
}
}
/// Send one GEN line; return the generated tokens and the DONE line.
fn generate(&mut self, max_new: u32, prompt: &[u32]) -> (Vec<u32>, String, usize) {
let ids: Vec<String> = prompt.iter().map(u32::to_string).collect();
writeln!(self.stdin, "GEN {max_new} {}", ids.join(",")).unwrap();
self.stdin.flush().unwrap();
let (mut tokens, mut reused) = (Vec::new(), 0);
loop {
let mut line = String::new();
assert!(
self.stdout.read_line(&mut line).unwrap() > 0,
"worker exited"
);
let line = line.trim_end().to_owned();
if let Some(t) = line.strip_prefix("T ") {
tokens.push(t.parse().unwrap());
} else if let Some(n) = line.strip_prefix("REUSED ") {
reused = n.parse().unwrap();
} else if line.starts_with("DONE ") {
return (tokens, line, reused);
} else {
assert!(line.starts_with("PP "), "unexpected line {line:?}");
}
}
}
}
impl Drop for Worker {
fn drop(&mut self) {
let _ = writeln!(self.stdin, "QUIT");
let _ = self.stdin.flush();
let _ = self.child.wait();
}
}
#[test]
fn the_worker_continues_a_prompt_and_reuses_the_prefix() {
let Ok(model) = std::env::var("LELEKA_GPTOSS_GGUF") else {
eprintln!("skipped: set LELEKA_GPTOSS_GGUF to the gpt-oss-20b GGUF to run this test");
return;
};
let mut worker = Worker::start(&model);
let (tokens, done, reused) = worker.generate(16, &PROMPT);
assert_eq!(tokens.len(), 16);
assert_eq!(tokens[0], FIRST_TOKEN);
assert_eq!(reused, 0);
let fields: Vec<&str> = done.split_whitespace().collect();
assert_eq!(&fields[1..3], ["16", "5"], "{done}");
assert_eq!(fields[5], "length");
// the prompt plus the first four generated tokens: everything cached is reused, and the
// continuation is exactly what the first request produced
let mut longer = PROMPT.to_vec();
longer.extend_from_slice(&tokens[..4]);
let (continued, done, reused) = worker.generate(12, &longer);
assert_eq!(reused, longer.len() - 1, "{done}");
assert_eq!(continued, tokens[4..16]);
// a different prompt shares nothing but still works after the reuse
let (tokens, _, reused) = worker.generate(3, &[976, 9029]);
assert_eq!(tokens.len(), 3);
assert_eq!(reused, 1);
}