107 lines
3.6 KiB
Rust
107 lines
3.6 KiB
Rust
// A scripted stand-in for `strata --serve`, compiled by tests/reasoning_budget_live.rs (std only).
|
|
//
|
|
// scripted_engine --serve DIR STOP_IDS
|
|
//
|
|
// DIR/script holds one line of comma-separated token ids per GEN/GENI the worker will send, in order. For each
|
|
// request the engine prints `T id` for the first max_new ids of the next line, ends early after a stop id (finish
|
|
// stop) or when it reads STOP (finish cancel), and logs "max_new ids" to DIR/requests. A STOP that arrives while no
|
|
// request runs is ignored, as the real engine does.
|
|
use std::io::{BufRead, Write};
|
|
use std::sync::mpsc;
|
|
|
|
fn read_script(dir: &std::path::Path) -> Vec<String> {
|
|
std::fs::read_to_string(dir.join("script"))
|
|
.unwrap_or_default()
|
|
.lines()
|
|
.map(str::to_owned)
|
|
.collect()
|
|
}
|
|
|
|
/// The lines the worker writes, read on a thread so that a STOP can be noticed between two tokens.
|
|
fn spawn_stdin_reader() -> mpsc::Receiver<String> {
|
|
let (sender, lines) = mpsc::channel::<String>();
|
|
std::thread::spawn(move || {
|
|
for line in std::io::stdin().lock().lines() {
|
|
let Ok(line) = line else { break };
|
|
if sender.send(line).is_err() {
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
lines
|
|
}
|
|
|
|
fn log_request(dir: &std::path::Path, max_new: usize, prompt: &str) {
|
|
let mut log = std::fs::OpenOptions::new()
|
|
.create(true)
|
|
.append(true)
|
|
.open(dir.join("requests"))
|
|
.unwrap();
|
|
writeln!(log, "{max_new} {prompt}").unwrap();
|
|
}
|
|
|
|
/// Prints `T id` for the first `max_new` ids; returns how many were sent and the finish reason.
|
|
fn play_stream(
|
|
out: &mut impl Write,
|
|
lines: &mpsc::Receiver<String>,
|
|
(stream, max_new): (&[u32], usize),
|
|
stops: &[u32],
|
|
) -> (usize, &'static str) {
|
|
let (mut sent, mut finish) = (0usize, "length");
|
|
for id in stream.iter().take(max_new) {
|
|
if let Ok(next) = lines.try_recv() {
|
|
if next == "STOP" {
|
|
finish = "cancel";
|
|
break;
|
|
}
|
|
}
|
|
writeln!(out, "T {id}").unwrap();
|
|
out.flush().unwrap();
|
|
sent += 1;
|
|
if stops.contains(id) {
|
|
finish = "stop";
|
|
break;
|
|
}
|
|
}
|
|
(sent, finish)
|
|
}
|
|
|
|
/// The ids of the `call`-th script line.
|
|
fn script_stream(script: &[String], call: usize) -> Vec<u32> {
|
|
script
|
|
.get(call)
|
|
.map(|l| l.split(',').filter_map(|s| s.parse().ok()).collect())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
fn main() {
|
|
let args: Vec<String> = std::env::args().collect();
|
|
let dir = std::path::PathBuf::from(&args[2]);
|
|
let stops: Vec<u32> = args[3].split(',').filter_map(|s| s.parse().ok()).collect();
|
|
let script = read_script(&dir);
|
|
let lines = spawn_stdin_reader();
|
|
let out = std::io::stdout();
|
|
let mut out = out.lock();
|
|
writeln!(out, "READY 1000000 stop").unwrap();
|
|
out.flush().unwrap();
|
|
let mut call = 0;
|
|
while let Ok(line) = lines.recv() {
|
|
if line == "QUIT" {
|
|
return;
|
|
}
|
|
if !line.starts_with("GEN") {
|
|
continue; // a late STOP
|
|
}
|
|
let fields: Vec<&str> = line.split(' ').collect();
|
|
let max_new: usize = fields[1].parse().unwrap();
|
|
let prompt = fields.last().unwrap();
|
|
log_request(&dir, max_new, prompt);
|
|
let stream = script_stream(&script, call);
|
|
call += 1;
|
|
let (sent, finish) = play_stream(&mut out, &lines, (&stream, max_new), &stops);
|
|
let prompt_tokens = prompt.split(',').filter(|s| !s.is_empty()).count();
|
|
writeln!(out, "DONE {sent} {prompt_tokens} 1.0 1.0 {finish}").unwrap();
|
|
out.flush().unwrap();
|
|
}
|
|
}
|