* worker.rs (1271 lines, 1120 of them tests) is 149 lines; the supervisor tests are worker/tests.rs (shared fixtures) and
worker/tests/{deadlines,handshake,generation,restart,lifecycle,reasoning}.rs. The test names are unchanged; their path
gains the topic module (worker::tests::restart::..., see scripts/dev/test-outcomes.py compare: 23 MOVED, outcomes equal).
* vision.rs 402 -> 120 lines (tests are vision/tests.rs, same test ids); prometheus.rs 441 -> 50 (the series table is
prometheus/series.rs); service_install.rs 436 -> 30 (the unix half is service_install/unix.rs and unix/tests.rs, same ids).
* config: Config::load_with_path (430 lines in one function) hands its limits, engine-worker and MCP sections to
config/methods/load_sections.rs; the same checks run in the same order with the same messages.
* mcp, request_log, vision, worker, config: impl_*/note_*/check_* file names are now hub.rs, server.rs, stdio_transport.rs,
http_transport.rs, parse_config.rs, obs_handle.rs, engine_counters.rs, encoder.rs, cleanup.rs, generation_result.rs, ...
Public paths are unchanged (the modules re-export with glob as before).
Architecture gate: five oversize entries leave the baseline (worker.rs, vision.rs, prometheus.rs, service_install.rs,
config impl_config_2_2.rs); no number rose.
121 lines
4.0 KiB
Rust
121 lines
4.0 KiB
Rust
//! The image encoder client: the resident `strata-vision` process (llama.cpp mtmd plus the model's mmproj
|
|
//! file), its `ENC <image> <out>` line protocol, the content-hash cache, and the per-request embeddings file
|
|
//! (`serve/server.py` `Vision` and the image branch of `Service.prepare`).
|
|
//!
|
|
//! Everything format-level (SVE1 records, `<|image_pad|>` expansion, base64 wording, native formats) lives in
|
|
//! `leleka_serving_policy::vision`; this module owns the process and the files.
|
|
//!
|
|
//! Differences from the Python server, all deliberate:
|
|
//! * images in formats `stb_image` cannot read (WebP, TIFF, AVIF, ...) are refused with a clear message: Python
|
|
//! converts them with Pillow, which a Python-free serving path does not have;
|
|
//! * a fetched `http(s)` image is capped at [`MAX_IMAGE_BYTES`], and a network failure is a 400 naming it;
|
|
//! * the per-request `req-*.sve` file is removed when the request ends (Python leaves it behind);
|
|
//! * the encoder's stderr is inherited, not discarded.
|
|
|
|
use std::{
|
|
collections::VecDeque,
|
|
path::{Path, PathBuf},
|
|
process::Stdio,
|
|
sync::{
|
|
Arc,
|
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
|
},
|
|
time::Duration,
|
|
};
|
|
|
|
use leleka_serving_policy::vision as policy;
|
|
use serde::Deserialize;
|
|
use sha2::{Digest, Sha256};
|
|
use tokio::{
|
|
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
|
|
process::{Child, ChildStdin, ChildStdout, Command},
|
|
sync::Mutex,
|
|
time::timeout,
|
|
};
|
|
|
|
/// Encoded images kept on disk, oldest evicted first (`len(self.cache) > 64`).
|
|
pub const CACHE_LIMIT: usize = 64;
|
|
/// A fetched image larger than this is refused (Python reads without a limit).
|
|
pub const MAX_IMAGE_BYTES: usize = 64 * 1024 * 1024;
|
|
const FETCH_TIMEOUT: Duration = Duration::from_secs(60);
|
|
const START_TIMEOUT: Duration = Duration::from_secs(600);
|
|
|
|
/// The config's `vision` section.
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct VisionConfig {
|
|
/// The `strata-vision` executable.
|
|
pub exe: PathBuf,
|
|
pub mmproj: PathBuf,
|
|
/// The text model's first GGUF split (opened vocab-only for the tokenizer).
|
|
pub model: PathBuf,
|
|
#[serde(default)]
|
|
pub gpu: bool,
|
|
pub threads: Option<u32>,
|
|
pub max_tokens: Option<u32>,
|
|
/// Run the encoder on this card alone (`CUDA_DEVICE_ORDER=PCI_BUS_ID`, `CUDA_VISIBLE_DEVICES`), so a spare GPU can
|
|
/// hold it while the engine keeps the VRAM of its own cards.
|
|
pub cuda_device: Option<u32>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum VisionError {
|
|
/// The caller's image is the problem: HTTP 400 (`ValueError` in Python).
|
|
Invalid(String),
|
|
/// The encoder itself failed (the pipe is gone, the process would not start).
|
|
Failed(String),
|
|
}
|
|
|
|
/// One encoded image: its SVE1 file and the number of image tokens it stands for.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Encoded {
|
|
pub path: PathBuf,
|
|
pub tokens: usize,
|
|
}
|
|
|
|
/// Keeps a request's temporary files alive until the last clone of the request is dropped.
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct Keep(Option<Arc<TempFile>>);
|
|
|
|
#[derive(Debug)]
|
|
struct TempFile(PathBuf);
|
|
|
|
struct Proc {
|
|
child: Child,
|
|
stdin: ChildStdin,
|
|
lines: tokio::io::Lines<BufReader<ChildStdout>>,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct Inner {
|
|
proc: Option<Proc>,
|
|
/// Content hash (first 32 hex of SHA-256 of the normalized bytes) -> encoded file; insertion order.
|
|
cache: VecDeque<(String, Encoded)>,
|
|
}
|
|
|
|
pub struct Vision {
|
|
config: VisionConfig,
|
|
dir: PathBuf,
|
|
inner: Mutex<Inner>,
|
|
stopped: AtomicBool,
|
|
serial: AtomicU64,
|
|
/// Taken for the length of an encode and for every engine generation: the engine and the encoder (two processes)
|
|
/// must not use the GPU at the same time. Python does the same with its request FIFO.
|
|
pub gate: Arc<Mutex<()>>,
|
|
}
|
|
|
|
static NEXT_DIR: AtomicU64 = AtomicU64::new(0);
|
|
|
|
#[cfg(all(test, unix))]
|
|
mod tests;
|
|
|
|
mod config;
|
|
#[allow(unused_imports)]
|
|
pub use config::*;
|
|
mod encoder;
|
|
#[allow(unused_imports)]
|
|
pub use encoder::*;
|
|
mod cleanup;
|
|
#[allow(unused_imports)]
|
|
pub use cleanup::*;
|