129 lines
5.4 KiB
Rust
129 lines
5.4 KiB
Rust
//! The worker for a text-level engine: the whole `leleka-api` API in front of anything that
|
|
//! implements [`leleka_backend_api::Backend`] (the Rust engine over gRPC, or a scripted engine in
|
|
//! tests).
|
|
//!
|
|
//! It is the second implementation of the worker seam. [`crate::worker::run_supervised`] feeds the
|
|
//! HTTP layer from a child process that speaks token ids on stdio; this module consumes the same
|
|
//! [`GenerationCommand`] channel, keeps the same [`WorkerStatus`] and answers the same [`Control`]
|
|
//! messages, but talks to the engine through the backend contract: a prompt string in, text deltas
|
|
//! and exactly one terminal event out. Nothing above this module changes.
|
|
//!
|
|
//! The conversions this edge must get right (all of them fail closed):
|
|
//!
|
|
//! * prompt: the ids the HTTP layer counted are decoded to text and must encode back to the same ids
|
|
//! (the engine encodes the string again with the same tokenizer); anything else is refused;
|
|
//! * output: every delta is re-encoded without special-token parsing and must decode to the same
|
|
//! bytes, so the bytes the client sees are the bytes the engine produced. The ids are not the
|
|
//! engine's sampled ids; usage comes from the engine's terminal report;
|
|
//! * stop: the literals `<|im_end|>` and `<|endoftext|>` end the text (the stdio engine reports them as
|
|
//! ids), the engine generation is cancelled and the result is `stop`;
|
|
//! * what the text contract cannot carry (images, reasoning-budget wrap-up, per-request engine tuning,
|
|
//! control-vector switching) is refused with a message instead of being dropped;
|
|
//! * a terminal event reaches the HTTP layer for every started generation: completed, failed,
|
|
//! cancelled, or an engine that vanished (the stream ended without one).
|
|
//!
|
|
//! The worker only attaches: [`Backend::load`] is expected to return the already loaded model (the
|
|
//! gRPC backend never issues a load RPC), so an HTTP request can never start a multi-gigabyte load.
|
|
|
|
use std::{
|
|
sync::{Arc, Mutex},
|
|
time::Duration,
|
|
};
|
|
|
|
use leleka_backend_api as api;
|
|
use tokio::{
|
|
sync::{RwLock, mpsc, watch},
|
|
time::Instant,
|
|
};
|
|
|
|
use crate::{
|
|
tokenizer::Tokenizer,
|
|
wire::{Done, Event, Finish, Generate},
|
|
worker::{
|
|
Control, GenerationCommand, LoadReply, WorkerState, WorkerStatus, deliver, finish,
|
|
send_failure, update,
|
|
},
|
|
};
|
|
|
|
/// Literals that end the text, as the ids `<|im_end|>` / `<|endoftext|>` do on the stdio wire.
|
|
const STOP_LITERALS: [&str; 2] = ["<|im_end|>", "<|endoftext|>"];
|
|
|
|
pub struct BackendWorkerConfig {
|
|
/// The tokenizer the HTTP layer counts with; the engine must use the same vocabulary.
|
|
pub tokenizer: Arc<Tokenizer>,
|
|
/// The model to attach to, exactly as the engine describes it.
|
|
pub model: api::ModelDescriptor,
|
|
pub runtime: api::RuntimeConfig,
|
|
/// The engine's context in tokens (`n + max_new + 8 <= max_context` is checked before a request).
|
|
pub max_context: usize,
|
|
/// The most output tokens one request may ask for (the runtime facade caps it). A request above
|
|
/// it is refused with a message; a default budget is clamped to it by the HTTP layer.
|
|
pub max_output_tokens: u32,
|
|
/// Wall-clock limit of one generation; the engine is cancelled when it expires.
|
|
pub generation_timeout: Option<Duration>,
|
|
/// No engine event for this long during a generation cancels it. Off by default: the text
|
|
/// contract has no progress events, so a long prefill is indistinguishable from a stall.
|
|
pub stall_timeout: Option<Duration>,
|
|
/// How long to wait between attempts to attach to an engine that is not there.
|
|
pub attach_retry: Duration,
|
|
/// How often an idle worker asks the engine whether the model is still loaded, so readiness
|
|
/// drops when the runtime restarts without waiting for a request to find out.
|
|
pub health_interval: Duration,
|
|
/// How long a cancelled generation may take to produce its terminal event before the engine
|
|
/// is treated as lost.
|
|
pub cancel_drain_timeout: Duration,
|
|
/// Where the engine's prefix-cache summary is copied (health tick and after every request); `None` = not shown.
|
|
pub prefix_view: Option<Arc<crate::prefix_view::PrefixView>>,
|
|
/// Where the engine's memory-governor report is copied (health tick and after every request); `None` = not shown,
|
|
/// no gate.
|
|
pub memory_view: Option<Arc<crate::memory_view::MemoryView>>,
|
|
}
|
|
|
|
type Engine = Arc<Mutex<Box<dyn api::Backend>>>;
|
|
|
|
/// What reaches the worker from outside: the generation requests and the control messages.
|
|
pub struct WorkerInbox {
|
|
pub requests: mpsc::Receiver<GenerationCommand>,
|
|
pub control: mpsc::Receiver<Control>,
|
|
}
|
|
|
|
enum Served {
|
|
Done,
|
|
/// The engine is gone or violated the contract; attach again.
|
|
Lost(String),
|
|
Shutdown,
|
|
}
|
|
|
|
enum Cancel {
|
|
/// The HTTP client went away.
|
|
Client,
|
|
/// A stop literal was found in the text.
|
|
Marker,
|
|
/// The request must end as a failure with this message.
|
|
Failure(String),
|
|
}
|
|
|
|
struct Conclusion<'a> {
|
|
cancel: Option<Cancel>,
|
|
stops: &'a mut leleka_serving_policy::stop::LiteralStops,
|
|
usage: Option<api::Usage>,
|
|
delivered: u64,
|
|
started: Instant,
|
|
first_delta: Option<Instant>,
|
|
counted_input: u64,
|
|
tokenizer: &'a Tokenizer,
|
|
}
|
|
|
|
mod attach;
|
|
use attach::*;
|
|
mod generation_run;
|
|
use generation_run::*;
|
|
mod engine_request_checks;
|
|
use engine_request_checks::*;
|
|
mod worker_entry;
|
|
pub use worker_entry::*;
|
|
mod serve_generation;
|
|
use serve_generation::*;
|
|
mod conclude;
|
|
use conclude::*;
|