Files
leleka/crates/engine-core/src/prompt.rs
T

148 lines
5.0 KiB
Rust

//! Original serve prompt scheduling, not a session/checkpoint capability.
//! Caller authenticates resume/reread and executes real target+draft transactions.
//! Source: pinned generate.cpp request_chunk/windows_ok/turn-root/segment loops.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PlanError {
InvalidInput,
Overflow,
}
type Result<T> = core::result::Result<T, PlanError>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PromptOptions {
pub context_tokens: usize,
pub prompt_cache: bool,
pub turn_token: Option<u32>,
pub root_min: usize,
pub short_read: usize,
pub force_batched: bool,
pub verify_width: usize,
pub auto_max: usize,
/// Prefix cache: every batched chunk ends on a multiple of `grid` (0 = off, the Original's cuts). The state
/// after such a chunk is a pure function of the tokens before it, whichever request read them, so it can be
/// shared between sessions and a request resumed from it reads exactly the chunks a full read would.
pub grid: usize,
}
/// Where a request restarts: the authenticated `resume` position and the optional end of the re-read prefix.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ResumePoint {
pub resume: usize,
pub reread_to: Option<usize>,
}
impl ResumePoint {
/// A request read from its first token, without a re-read.
pub const START: Self = Self { resume: 0, reread_to: None };
/// Resume at `resume` without a re-read.
pub const fn at(resume: usize) -> Self {
Self { resume, reread_to: None }
}
}
/// What the planner admits: the options and the admitted chunk capacity.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PlanLimits {
pub options: PromptOptions,
pub admitted_capacity: usize,
}
impl PlanLimits {
pub const fn new(options: PromptOptions, admitted_capacity: usize) -> Self {
Self { options, admitted_capacity }
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PromptPath {
Batched {
capacity: usize,
},
Verify,
/// Original first decode window: the actual last prompt token alone.
FinalSeed,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PromptAction {
pub first: usize,
pub count: usize,
pub path: PromptPath,
/// Only at the actual segment end, after real completed commit/drain.
pub checkpoint_after: bool,
}
/// At most four conditioning segments, followed by one real target seed.
/// No allocations, guessed retained prefix, output tokens or session mutation.
pub struct PromptPlan<'a> {
prompt: &'a [u32],
embedding_rows: Option<&'a [bool]>,
options: PromptOptions,
admitted_capacity: usize,
boundaries: [usize; 4],
turn: Option<usize>,
root: Option<usize>,
segment: usize,
position: usize,
end: usize,
path: Option<PromptPath>,
seed_done: bool,
}
/// What the serving owner executes for each action of a [`PromptPlan`]. The Original's `Prefill::run` chunks a
/// `Batched` action internally (`for c0 += m.T`); here one action is one chunk, `count <= capacity`, so the owner
/// runs exactly one `OriginalPromptExecutor::forward` + publish per action.
pub trait PromptRunner {
type Error;
/// `should_stop()` of the Original, checked before every action.
fn cancelled(&mut self) -> bool;
/// `Prefill::run` over `action.first .. action.first + action.count` (a real target+draft transaction).
fn batched(&mut self, action: &PromptAction) -> core::result::Result<(), Self::Error>;
/// Short segments: verifier windows of at most `verify_width` rows.
fn verify_window(&mut self, action: &PromptAction) -> core::result::Result<(), Self::Error>;
/// The last prompt token alone, left to the first decode window.
fn final_seed(&mut self, action: &PromptAction) -> core::result::Result<(), Self::Error>;
/// Only at a real segment end (`checkpoint_after`), after the completed commit/drain; `end` is the first
/// position NOT covered by the checkpointed prefix.
fn checkpoint(&mut self, end: usize) -> core::result::Result<(), Self::Error>;
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DriveSummary {
pub batched_chunks: usize,
pub batched_rows: usize,
pub verify_windows: usize,
pub verify_rows: usize,
pub checkpoints: usize,
pub final_seed: bool,
/// First position the plan covered, and one past the last (the prompt length).
pub first: usize,
pub end: usize,
}
#[derive(Debug, Eq, PartialEq)]
pub enum DriveError<E> {
Cancelled {
at: usize,
},
/// The plan skipped or repeated positions, or a batched action exceeds the admitted capacity.
Contract(&'static str),
Runner(E),
}
#[cfg(test)]
#[path = "prompt/tests.rs"]
mod tests;
#[cfg(test)]
#[path = "prompt/grid_tests.rs"]
mod grid_tests;
#[path = "prompt/impl_promptoptions.rs"]
mod impl_promptoptions;
#[allow(unused_imports)]
pub use impl_promptoptions::choose_auto_capacity;
#[allow(unused_imports)]
pub use impl_promptoptions::drive;
#[allow(unused_imports)]
pub use impl_promptoptions::request_capacity;