176 lines
6.0 KiB
Rust
176 lines
6.0 KiB
Rust
//! `vetta-proto`: unified `--serve` wire protocol (`strata-ctl` compatible, including `GEN 0` pre-warm)
|
|
//! and lightweight Lyra LAN control-plane message schemas.
|
|
|
|
use vetta_core::sampler::SamplingOpts;
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum ServeCommand {
|
|
Gen(ParsedGen),
|
|
Reset,
|
|
Stop,
|
|
Quit,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct ParsedGen {
|
|
pub max_new: usize,
|
|
pub sampling: SamplingOpts,
|
|
/// `None` = engine default, `Some(0)` = AR off, `Some(1)` = adaptive, `Some(k)` = fixed k
|
|
pub spec_mode: Option<usize>,
|
|
pub draft_top_k: Option<usize>,
|
|
pub domain_hint: Option<String>,
|
|
pub ids: Vec<i32>,
|
|
}
|
|
|
|
pub fn parse_id_csv(csv: &str, vocab_size: usize) -> Result<Vec<i32>, String> {
|
|
if csv.is_empty() {
|
|
return Err("empty token id list".into());
|
|
}
|
|
let mut out = Vec::new();
|
|
for piece in csv.split(',') {
|
|
if piece.is_empty() {
|
|
return Err("empty token id in CSV".into());
|
|
}
|
|
let id: u64 = piece
|
|
.parse()
|
|
.map_err(|_| format!("invalid token id '{piece}'"))?;
|
|
if id >= vocab_size as u64 {
|
|
return Err(format!("token id {id} out of vocab bounds [0, {vocab_size})"));
|
|
}
|
|
out.push(id as i32);
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
pub fn parse_serve_line(line: &str, vocab_size: usize) -> Result<ServeCommand, String> {
|
|
let trimmed = line.trim();
|
|
match trimmed {
|
|
"QUIT" | "EXIT" => return Ok(ServeCommand::Quit),
|
|
"RESET" | "CLEAR" => return Ok(ServeCommand::Reset),
|
|
"STOP" => return Ok(ServeCommand::Stop),
|
|
_ => {}
|
|
}
|
|
let toks: Vec<&str> = trimmed.split_whitespace().collect();
|
|
if toks.len() < 3 || toks[0] != "GEN" {
|
|
return Err("GEN: expected 'GEN <max_new> [key=val ...] <ids>'".into());
|
|
}
|
|
let max_new: u64 = toks[1].parse().map_err(|_| "GEN: bad max_new".to_string())?;
|
|
if max_new > 1_000_000 {
|
|
return Err("GEN: max_new out of range".into());
|
|
}
|
|
|
|
let mut sampling = SamplingOpts::default();
|
|
let mut spec_mode = None;
|
|
let mut draft_top_k = None;
|
|
let mut domain_hint = None;
|
|
|
|
for &kv in &toks[2..toks.len() - 1] {
|
|
let (k, v) = kv
|
|
.split_once('=')
|
|
.ok_or_else(|| format!("GEN: bad key=value '{kv}'"))?;
|
|
if k.is_empty() || v.is_empty() {
|
|
return Err(format!("GEN: bad key=value '{kv}'"));
|
|
}
|
|
match k {
|
|
"spec" => {
|
|
spec_mode = Some(match v {
|
|
"auto" | "adapt" => 1,
|
|
"off" | "ar" | "0" => 0,
|
|
_ => {
|
|
let n: usize = v.parse().map_err(|_| format!("GEN: bad spec={v}"))?;
|
|
if n > 32 {
|
|
return Err(format!("GEN: spec={v} > 32"));
|
|
}
|
|
n
|
|
}
|
|
});
|
|
}
|
|
"domain" => {
|
|
domain_hint = Some(v.to_string());
|
|
}
|
|
"seed" => {
|
|
sampling.seed = v.parse().map_err(|_| "GEN: bad seed".to_string())?;
|
|
}
|
|
"temperature" | "temp" => {
|
|
let d = parse_finite_f32(v, k)?;
|
|
sampling.temperature = d.max(0.0);
|
|
}
|
|
"top_p" => {
|
|
sampling.top_p = parse_finite_f32(v, k)?.clamp(0.0, 1.0);
|
|
}
|
|
"top_k" => {
|
|
let n: i64 = v.parse().map_err(|_| format!("GEN: bad {k}"))?;
|
|
sampling.top_k = if n <= 0 { 0 } else { n as usize };
|
|
}
|
|
"min_p" => {
|
|
sampling.min_p = parse_finite_f32(v, k)?.clamp(0.0, 1.0);
|
|
}
|
|
"penalty_repeat" | "repetition_penalty" => {
|
|
sampling.penalty_repeat = parse_finite_f32(v, k)?;
|
|
}
|
|
"penalty_freq" | "frequency_penalty" => {
|
|
sampling.penalty_freq = parse_finite_f32(v, k)?;
|
|
}
|
|
"penalty_present" | "presence_penalty" => {
|
|
sampling.penalty_present = parse_finite_f32(v, k)?;
|
|
}
|
|
"penalty_last_n" => {
|
|
let n: i64 = v.parse().map_err(|_| format!("GEN: bad {k}"))?;
|
|
sampling.penalty_last_n = n.max(0) as usize;
|
|
}
|
|
"draft_top_k" => {
|
|
let n: usize = v.parse().map_err(|_| format!("GEN: bad {k}"))?;
|
|
draft_top_k = Some(n.min(16));
|
|
}
|
|
_ => return Err(format!("GEN: unknown key '{k}'")),
|
|
}
|
|
}
|
|
|
|
let ids = parse_id_csv(toks[toks.len() - 1], vocab_size)?;
|
|
Ok(ServeCommand::Gen(ParsedGen {
|
|
max_new: max_new as usize,
|
|
sampling,
|
|
spec_mode,
|
|
draft_top_k,
|
|
domain_hint,
|
|
ids,
|
|
}))
|
|
}
|
|
|
|
fn parse_finite_f32(v: &str, key: &str) -> Result<f32, String> {
|
|
let f: f32 = v.parse().map_err(|_| format!("GEN: bad numeric value for {key}"))?;
|
|
if !f.is_finite() {
|
|
return Err(format!("GEN: non-finite value for {key}"));
|
|
}
|
|
Ok(f)
|
|
}
|
|
|
|
/// Compact LAN RPC messages for Sogliola <-> Lyra (`< 1 KB/step` on 2.5 Gbps Ethernet).
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum LyraRpc {
|
|
DraftRequest { session_id: u64, accepted_tail: Vec<i32>, max_k: u8 },
|
|
DraftResponse { session_id: u64, proposed: Vec<i32>, probs: Vec<f32> },
|
|
DomainHint { session_id: u64, domain: String, confidence: f32 },
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn gen_zero_prewarm_and_strict_validation() {
|
|
let cmd = parse_serve_line("GEN 0 domain=lean4 1,2,3", 100).unwrap();
|
|
match cmd {
|
|
ServeCommand::Gen(g) => {
|
|
assert_eq!(g.max_new, 0);
|
|
assert_eq!(g.domain_hint.as_deref(), Some("lean4"));
|
|
assert_eq!(g.ids, vec![1, 2, 3]);
|
|
}
|
|
_ => panic!("expected Gen"),
|
|
}
|
|
assert!(parse_serve_line("GEN 16 1,100", 100).is_err());
|
|
assert!(parse_serve_line("GEN 16 temperature=nan 1,2", 100).is_err());
|
|
assert!(parse_serve_line("GEN 16 unknown=1 1,2", 100).is_err());
|
|
}
|
|
}
|