[ABI][KN][LD][CC][RT][LAB][SP][NT][TM][GW][Proofs][Docs] M0.5: realign workspace to vetta-infer-* microkernel architecture
Dissolve the M0 crates (core/io/proto/telemetry/hip/cli) into the approved service layout abi/kn/ld/cc/rt/lab/sp/nt/tm/gw (docs/ARCHITECTURE.md). - kn: Lean-verified control bus + service registry (bus_lifecycle.lean, 14 thms) and SlabRing/UMA broker; rt: PrefixState + hybrid checkpoint pool. - sp: faithful throughput-aware Adaptive-K port of clover_policy.h (cooldowns, probes, throughput guards) with scenario parity tests. - gw: strata-ctl-compatible wire emitters + serve session FSM (serve_session.lean, 10 thms); abi::serve accepts image= (Vision delta closed). - abi: pure contracts, checked JSON/VTC1/GEN parsers, zero-alloc sampler. - ld/cc: checked safetensors reader + write-once VTC1 writer / O_RDONLY reader. - tm: lock-free telemetry; nt: LyraRpc; lab shares the rt executor core. - Docs: ARCHITECTURE.md decision-complete spec (MoE-only scope, .vbundle, .vplan->CompiledPlan, wire contract, migration map); AGENTS.md Lean rule + tandem/review workflow. Verified live: cargo test --workspace -D warnings = 46/46 PASS; lean proofs = 34 theorems (slab_ring_dma 10, bus_lifecycle 14, serve_session 10), 0 sorryAx, standard axioms only; vetta-infer info on Inkling = 1244 tensors, 18.18 GiB, checked bounds.
This commit is contained in:
1 parent
63b1668a92
commit
7b50f1c07c
49 files changed
+3211
-1208
No files matched your search
@@ -0,0 +1,143 @@
|
||||
//! `vetta-infer-gw`: gateway & CLI (docs/ARCHITECTURE.md §2, §6).
|
||||
//!
|
||||
//! Engine → consumer wire emitters, matching `strata-ctl/src/engine/protocol.rs`
|
||||
//! (`EngineLine::parse`) field for field:
|
||||
//! `READY <ctx> [stop] [hold] [holdb]`, `INFO k=v …`, `T <id>`,
|
||||
//! `PP <read> <total> [ms] [tok_s]`, `RESUME <n>`,
|
||||
//! `DONE <gen> <prompt> <prompt_ms> <decode_ms> <finish> [extra…]`, `ERR <msg>`.
|
||||
//!
|
||||
//! `DONE` fields 1–5 are stable; the tail is kept raw by the consumer, so new
|
||||
//! statistics can be appended without breaking it. Floats use Python-repr
|
||||
//! spelling (`1.0`), which the consumer's parser expects.
|
||||
|
||||
pub mod session;
|
||||
|
||||
/// Python-repr float spelling (always a decimal point), as `strata-ctl` emits.
|
||||
fn fmt_float(v: f64) -> String {
|
||||
let s = format!("{v}");
|
||||
if s.contains('.') {
|
||||
s
|
||||
} else {
|
||||
format!("{s}.0")
|
||||
}
|
||||
}
|
||||
|
||||
/// First meaningful line after startup. `INFO` may precede it; the consumer
|
||||
/// treats any other pre-`READY` line as startup chatter and `ERR` as refusal.
|
||||
pub fn ready_line(max_context: u32, can_stop: bool, can_hold: bool, can_hold_batch: bool) -> String {
|
||||
let mut s = format!("READY {max_context}");
|
||||
if can_stop {
|
||||
s.push_str(" stop");
|
||||
}
|
||||
if can_hold {
|
||||
s.push_str(" hold");
|
||||
}
|
||||
if can_hold_batch {
|
||||
s.push_str(" holdb");
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
pub fn info_line(pairs: &[(&str, &str)]) -> String {
|
||||
let mut s = String::from("INFO");
|
||||
for (k, v) in pairs {
|
||||
s.push(' ');
|
||||
s.push_str(k);
|
||||
s.push('=');
|
||||
s.push_str(v);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
pub fn token_line(id: u32) -> String {
|
||||
format!("T {id}")
|
||||
}
|
||||
|
||||
/// Prefill progress, one per chunk. `tok_s_mean` is optional (4th field).
|
||||
pub fn pp_line(read: u32, total: u32, ms: f64, tok_s_mean: Option<f64>) -> String {
|
||||
match tok_s_mean {
|
||||
Some(ts) => format!("PP {read} {total} {} {}", fmt_float(ms), fmt_float(ts)),
|
||||
None => format!("PP {read} {total} {}", fmt_float(ms)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resume_line(n: u32) -> String {
|
||||
format!("RESUME {n}")
|
||||
}
|
||||
|
||||
/// End of request. Fields 1–5 stable across versions; `extra` is the raw tail
|
||||
/// (draft stats, cache hits, expert tiers, …) the consumer preserves verbatim.
|
||||
pub fn done_line(
|
||||
generated: u32,
|
||||
prompt_tokens: u32,
|
||||
prompt_ms: f64,
|
||||
decode_ms: f64,
|
||||
finish: &str,
|
||||
extra: &[String],
|
||||
) -> String {
|
||||
let mut s = format!(
|
||||
"DONE {generated} {prompt_tokens} {} {} {finish}",
|
||||
fmt_float(prompt_ms),
|
||||
fmt_float(decode_ms)
|
||||
);
|
||||
for e in extra {
|
||||
s.push(' ');
|
||||
s.push_str(e);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
pub fn err_line(msg: &str) -> String {
|
||||
format!("ERR {msg}")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn ready_flags_match_consumer_parse() {
|
||||
assert_eq!(ready_line(262144, true, true, false), "READY 262144 stop hold");
|
||||
assert_eq!(ready_line(4096, false, false, false), "READY 4096");
|
||||
assert_eq!(ready_line(8192, true, false, true), "READY 8192 stop holdb");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn info_and_token_shapes() {
|
||||
assert_eq!(
|
||||
info_line(&[("version", "0.1.0"), ("expert_slots", "64")]),
|
||||
"INFO version=0.1.0 expert_slots=64"
|
||||
);
|
||||
assert_eq!(token_line(151645), "T 151645");
|
||||
assert_eq!(resume_line(512), "RESUME 512");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefill_progress_optional_mean() {
|
||||
assert_eq!(pp_line(1024, 8192, 431.5, Some(2373.2)), "PP 1024 8192 431.5 2373.2");
|
||||
assert_eq!(pp_line(128, 512, 90.0, None), "PP 128 512 90.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn done_five_stable_fields_and_raw_tail() {
|
||||
assert_eq!(
|
||||
done_line(41, 128, 90.5, 310.25, "stop", &[]),
|
||||
"DONE 41 128 90.5 310.25 stop"
|
||||
);
|
||||
let extra: Vec<String> = ["7", "10", "55", "chunk=2048"].iter().map(|s| s.to_string()).collect();
|
||||
assert_eq!(
|
||||
done_line(41, 128, 90.5, 310.25, "length", &extra),
|
||||
"DONE 41 128 90.5 310.25 length 7 10 55 chunk=2048"
|
||||
);
|
||||
// Integral floats keep the Python-repr ".0" spelling.
|
||||
assert_eq!(done_line(1, 2, 3.0, 4.0, "stop", &[]), "DONE 1 2 3.0 4.0 stop");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn err_shape() {
|
||||
assert_eq!(
|
||||
err_line("GEN: unknown/unsupported key pen_from"),
|
||||
"ERR GEN: unknown/unsupported key pen_from"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
//! `vetta-infer` CLI entrypoint (gateway crate).
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::env;
|
||||
use std::process::ExitCode;
|
||||
use vetta_infer_ld::SafetensorsArchive;
|
||||
|
||||
fn usage() {
|
||||
eprintln!("vetta-infer {} — MoE-first microkernel inference engine (Rust + HIP)", env!("CARGO_PKG_VERSION"));
|
||||
eprintln!("Usage:");
|
||||
eprintln!(" vetta-infer info <safetensors-dir> Inspect shards, dtypes, and checked bounds");
|
||||
eprintln!(" vetta-infer version Print version & build info");
|
||||
}
|
||||
|
||||
fn cmd_info(dir: &str) -> Result<(), String> {
|
||||
let t0 = std::time::Instant::now();
|
||||
let ar = SafetensorsArchive::open(dir)?;
|
||||
let dt_ms = t0.elapsed().as_secs_f64() * 1000.0;
|
||||
|
||||
let mut by_dtype: BTreeMap<String, (usize, u64)> = BTreeMap::new();
|
||||
for t in ar.tensors.values() {
|
||||
let entry = by_dtype.entry(t.dtype.clone()).or_insert((0, 0));
|
||||
entry.0 += 1;
|
||||
entry.1 += t.byte_len();
|
||||
}
|
||||
|
||||
let tot_gib = ar.total_tensor_bytes() as f64 / (1024.0 * 1024.0 * 1024.0);
|
||||
println!("dir: {}", ar.dir.display());
|
||||
println!("shards: {}", ar.shards.len());
|
||||
println!("tensors: {} (all bounds checked in {:.1} ms)", ar.tensors.len(), dt_ms);
|
||||
println!("payload: {:.2} GiB", tot_gib);
|
||||
println!("dtypes:");
|
||||
for (dt, (cnt, bytes)) in by_dtype {
|
||||
println!(" {:10} {:6} tensors ({:6.2} GiB)", dt, cnt, bytes as f64 / (1024.0 * 1024.0 * 1024.0));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let args: Vec<String> = env::args().skip(1).collect();
|
||||
match args.first().map(|s| s.as_str()) {
|
||||
Some("version") | Some("--version") | Some("-V") => {
|
||||
println!("vetta-infer {} (edition 2024, MoE-first HIP microkernel)", env!("CARGO_PKG_VERSION"));
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Some("info") => {
|
||||
let Some(dir) = args.get(1) else {
|
||||
eprintln!("error: missing <safetensors-dir>");
|
||||
usage();
|
||||
return ExitCode::from(2);
|
||||
};
|
||||
match cmd_info(dir) {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(e) => {
|
||||
eprintln!("error: {e}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
usage();
|
||||
ExitCode::from(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
//! `--serve` request lifecycle — implements `proofs/serve_session.lean`.
|
||||
//!
|
||||
//! The engine-side spine that talks to `strata-ctl` (docs/ARCHITECTURE.md §6):
|
||||
//! GEN/STOP/QUIT, `GEN 0` pre-warm, and the STOP→DONE drain guarantee. Pure
|
||||
//! state machine emitting wire lines via the `gw` emitters; the GPU backend
|
||||
//! feeds it events, so the whole contract is CPU-testable without a device.
|
||||
|
||||
use vetta_infer_abi::serve::ParsedGen;
|
||||
|
||||
use crate::{done_line, err_line, pp_line, resume_line, token_line};
|
||||
|
||||
/// Mirrors `ReqState` in `proofs/serve_session.lean`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SessionState {
|
||||
Idle,
|
||||
Prefilling,
|
||||
Decoding,
|
||||
Stopping,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ServeSession {
|
||||
state: SessionState,
|
||||
max_new: usize,
|
||||
generated: u32,
|
||||
prompt_tokens: u32,
|
||||
reused: u32,
|
||||
prompt_ms: f64,
|
||||
decode_ms: f64,
|
||||
finish: String,
|
||||
}
|
||||
|
||||
impl Default for ServeSession {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ServeSession {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
state: SessionState::Idle,
|
||||
max_new: 0,
|
||||
generated: 0,
|
||||
prompt_tokens: 0,
|
||||
reused: 0,
|
||||
prompt_ms: 0.0,
|
||||
decode_ms: 0.0,
|
||||
finish: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn state(&self) -> SessionState {
|
||||
self.state
|
||||
}
|
||||
|
||||
pub fn generated(&self) -> u32 {
|
||||
self.generated
|
||||
}
|
||||
|
||||
fn done(&mut self, finish: &str, decode_ms: f64) -> Vec<String> {
|
||||
self.decode_ms = decode_ms;
|
||||
self.finish = finish.to_string();
|
||||
self.state = SessionState::Done;
|
||||
vec![done_line(
|
||||
self.generated,
|
||||
self.prompt_tokens,
|
||||
self.prompt_ms,
|
||||
self.decode_ms,
|
||||
&self.finish,
|
||||
&[],
|
||||
)]
|
||||
}
|
||||
|
||||
/// `ReqStep.gen`: Idle -> Prefilling. Wrong state is an explicit error.
|
||||
pub fn on_gen(&mut self, g: &ParsedGen) -> Result<Vec<String>, String> {
|
||||
if self.state != SessionState::Idle {
|
||||
return Err(format!("GEN while {:?}", self.state));
|
||||
}
|
||||
self.max_new = g.max_new;
|
||||
self.prompt_tokens = g.ids.len() as u32;
|
||||
self.generated = 0;
|
||||
self.reused = 0;
|
||||
self.prompt_ms = 0.0;
|
||||
self.decode_ms = 0.0;
|
||||
self.finish.clear();
|
||||
self.state = SessionState::Prefilling;
|
||||
Ok(Vec::new()) // PP lines arrive per chunk via on_prefill_chunk
|
||||
}
|
||||
|
||||
/// Prefill progress (Prefilling only): one `PP` line per chunk.
|
||||
pub fn on_prefill_chunk(&mut self, read: u32, total: u32, ms: f64) -> Result<Vec<String>, String> {
|
||||
if self.state != SessionState::Prefilling {
|
||||
return Err(format!("PP while {:?}", self.state));
|
||||
}
|
||||
Ok(vec![pp_line(read, total, ms, None)])
|
||||
}
|
||||
|
||||
/// `ReqStep.prefillComplete` (max_new > 0) or `ReqStep.prewarm` (max_new == 0).
|
||||
pub fn on_prefill_complete(&mut self, reused: u32, prompt_ms: f64) -> Result<Vec<String>, String> {
|
||||
if self.state != SessionState::Prefilling {
|
||||
return Err(format!("prefill-complete while {:?}", self.state));
|
||||
}
|
||||
self.reused = reused;
|
||||
self.prompt_ms = prompt_ms;
|
||||
let mut out = Vec::new();
|
||||
if reused > 0 {
|
||||
out.push(resume_line(reused));
|
||||
}
|
||||
if self.max_new == 0 {
|
||||
// GEN 0 pre-warm: DONE immediately, never enters decode (Theorem 7).
|
||||
out.extend(self.done("stop", 0.0));
|
||||
} else {
|
||||
self.state = SessionState::Decoding;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Token emission is gated to Decoding (Theorem 9).
|
||||
pub fn on_token(&mut self, id: u32) -> Result<Vec<String>, String> {
|
||||
if self.state != SessionState::Decoding {
|
||||
return Err(format!("T while {:?}", self.state));
|
||||
}
|
||||
self.generated += 1;
|
||||
Ok(vec![token_line(id)])
|
||||
}
|
||||
|
||||
/// `ReqStep.stopPrefill` / `stopDecode`. STOP in Idle/Done is a no-op
|
||||
/// (Theorem 2); STOP while already Stopping is idempotent.
|
||||
pub fn on_stop(&mut self) -> Vec<String> {
|
||||
match self.state {
|
||||
SessionState::Prefilling | SessionState::Decoding => {
|
||||
self.state = SessionState::Stopping;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// `ReqStep.drain`: Stopping -> Done. The STOP guarantee (Theorem 4):
|
||||
/// a drained request always ends in exactly one DONE, with no tokens.
|
||||
pub fn on_drain_complete(&mut self, decode_ms: f64) -> Result<Vec<String>, String> {
|
||||
if self.state != SessionState::Stopping {
|
||||
return Err(format!("drain while {:?}", self.state));
|
||||
}
|
||||
Ok(self.done("stop", decode_ms))
|
||||
}
|
||||
|
||||
/// `ReqStep.finish`: Decoding -> Done (stop / length / nonfinite).
|
||||
pub fn on_finish(&mut self, reason: &str, decode_ms: f64) -> Result<Vec<String>, String> {
|
||||
if self.state != SessionState::Decoding {
|
||||
return Err(format!("finish while {:?}", self.state));
|
||||
}
|
||||
Ok(self.done(reason, decode_ms))
|
||||
}
|
||||
|
||||
/// Failure: emit `ERR`, and a trailing `DONE` if a request was in flight
|
||||
/// (the consumer swallows it — `swallow_trailing_done`).
|
||||
pub fn on_error(&mut self, msg: &str) -> Vec<String> {
|
||||
let mut out = vec![err_line(msg)];
|
||||
if matches!(self.state, SessionState::Prefilling | SessionState::Decoding | SessionState::Stopping) {
|
||||
out.extend(self.done("error", self.decode_ms));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `ReqStep.nextRequest`: Done -> Idle (clean request boundary, Theorem 8/10).
|
||||
pub fn on_next(&mut self) -> Result<(), String> {
|
||||
if self.state != SessionState::Done {
|
||||
return Err(format!("next while {:?}", self.state));
|
||||
}
|
||||
self.state = SessionState::Idle;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use vetta_infer_abi::serve::parse_serve_line;
|
||||
|
||||
fn gen_cmd(line: &str) -> ParsedGen {
|
||||
match parse_serve_line(line, 1000).unwrap() {
|
||||
vetta_infer_abi::serve::ServeCommand::Gen(g) => g,
|
||||
_ => panic!("expected GEN"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_zero_prewarm_emits_done_without_tokens() {
|
||||
let mut s = ServeSession::new();
|
||||
s.on_gen(&gen_cmd("GEN 0 1,2,3")).unwrap();
|
||||
assert_eq!(s.state(), SessionState::Prefilling);
|
||||
let out = s.on_prefill_complete(0, 12.0).unwrap();
|
||||
assert_eq!(s.state(), SessionState::Done);
|
||||
assert!(out.iter().any(|l| l.starts_with("DONE 0 3")));
|
||||
assert!(!out.iter().any(|l| l.starts_with("T ")));
|
||||
assert!(s.on_token(5).is_err()); // never entered decode
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normal_flow_prefill_decode_finish() {
|
||||
let mut s = ServeSession::new();
|
||||
s.on_gen(&gen_cmd("GEN 3 1,2")).unwrap();
|
||||
let pp = s.on_prefill_chunk(2, 2, 8.0).unwrap();
|
||||
assert_eq!(pp, vec!["PP 2 2 8.0"]);
|
||||
let out = s.on_prefill_complete(1, 8.0).unwrap();
|
||||
assert_eq!(out, vec!["RESUME 1"]);
|
||||
assert_eq!(s.state(), SessionState::Decoding);
|
||||
assert_eq!(s.on_token(10).unwrap(), vec!["T 10"]);
|
||||
assert_eq!(s.on_token(11).unwrap(), vec!["T 11"]);
|
||||
let done = s.on_finish("length", 40.0).unwrap();
|
||||
assert_eq!(done, vec!["DONE 2 2 8.0 40.0 length"]);
|
||||
assert_eq!(s.state(), SessionState::Done);
|
||||
assert!(s.on_gen(&gen_cmd("GEN 1 1")).is_err()); // must pass through idle
|
||||
s.on_next().unwrap();
|
||||
assert_eq!(s.state(), SessionState::Idle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_mid_decode_drains_to_done_no_more_tokens() {
|
||||
let mut s = ServeSession::new();
|
||||
s.on_gen(&gen_cmd("GEN 8 1,2")).unwrap();
|
||||
s.on_prefill_complete(0, 5.0).unwrap();
|
||||
s.on_token(10).unwrap();
|
||||
assert!(s.on_stop().is_empty());
|
||||
assert_eq!(s.state(), SessionState::Stopping);
|
||||
assert!(s.on_token(11).is_err()); // no tokens while draining
|
||||
let done = s.on_drain_complete(20.0).unwrap();
|
||||
assert_eq!(done, vec!["DONE 1 2 5.0 20.0 stop"]);
|
||||
assert_eq!(s.state(), SessionState::Done);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_in_idle_is_noop_and_stop_prefill_drains() {
|
||||
let mut s = ServeSession::new();
|
||||
assert!(s.on_stop().is_empty());
|
||||
assert_eq!(s.state(), SessionState::Idle);
|
||||
|
||||
s.on_gen(&gen_cmd("GEN 4 1,2,3")).unwrap();
|
||||
s.on_stop();
|
||||
assert_eq!(s.state(), SessionState::Stopping);
|
||||
let done = s.on_drain_complete(0.0).unwrap();
|
||||
assert_eq!(done, vec!["DONE 0 3 0.0 0.0 stop"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_emits_err_and_trailing_done_when_active() {
|
||||
let mut s = ServeSession::new();
|
||||
s.on_gen(&gen_cmd("GEN 4 1")).unwrap();
|
||||
let out = s.on_error("boom");
|
||||
assert_eq!(out[0], "ERR boom");
|
||||
assert!(out[1].starts_with("DONE "));
|
||||
assert_eq!(s.state(), SessionState::Done);
|
||||
|
||||
// ERR in idle: no trailing DONE (nothing in flight)
|
||||
let mut s2 = ServeSession::new();
|
||||
let out2 = s2.on_error("bad command");
|
||||
assert_eq!(out2, vec!["ERR bad command"]);
|
||||
assert_eq!(s2.state(), SessionState::Idle);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user