diff --git a/architecture.json b/architecture.json index 0c650341..d658bf1d 100644 --- a/architecture.json +++ b/architecture.json @@ -53,7 +53,7 @@ "exceptions": [ {"crate": "leleka-cuda-model", "file": "src/env_switch.rs", "ratchet": "env-read", "reason": "EnvSwitch is the one reader of the operator environment in this crate: it reads one named variable on the first get() and applies a typed rule"}, {"crate": "leleka-cuda-model", "file": "src/env_switch.rs", "ratchet": "process-global", "reason": "the OnceLock inside EnvSwitch caches the switch position for the process life (an immutable operator setting; a hot path pays one atomic load)"}, - {"crate": "leleka-cuda-model", "file": "src/serving_context.rs", "ratchet": "process-global", "reason": "the serving context and the resident KV window are fixed once at engine start (configure_*) and read by every kernel geometry; injecting them through every owner is the settings follow-up of S13"}, + {"crate": "leleka-cuda-model", "file": "src/serving_context.rs", "ratchet": "process-global", "reason": "the serving context is fixed once at engine start (configure_serving_context) and read by every kernel geometry; injecting it through every owner is the settings follow-up of S13. The resident KV window is not here any more: it belongs to the driver Context"}, {"crate": "leleka-cpu-worker", "file": "src/settings.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate: it returns the operator's switches as typed values"}, {"crate": "leleka-tokenizer", "file": "src/settings.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate: it returns the operator's switches as typed values"}, {"crate": "leleka-gptoss-npu", "file": "src/settings.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate: it returns the operator's switches as typed values"}, diff --git a/crates/cuda-driver/src/context_kv_resident.rs b/crates/cuda-driver/src/context_kv_resident.rs new file mode 100644 index 00000000..25999f3f --- /dev/null +++ b/crates/cuda-driver/src/context_kv_resident.rs @@ -0,0 +1,31 @@ +//! The resident KV window of a Context (`--kv-resident N`, the Original's `qsa_set_kv_resident`): the cell count its +//! streamed QSA owners keep in VRAM. It belongs to the Context, not to the process: every owner sizes its pool from +//! the value when it is created, so an owner created before the window is set keeps the default window consistently. + +use super::{Context, Error, Result}; +use crate::serving_context::{KV_RESIDENT_DEFAULT_CELLS, geometry}; + +impl Context<'_> { + /// Fix the resident KV window of this context. `0` keeps the whole context resident; a positive count is raised to + /// `qsa_kv_resident_min()` (20480) and rounded up to whole pages. The first call wins; a later call must request + /// the same count. + pub fn configure_kv_resident(&self, cells: usize) -> Result<()> { + geometry().resident_pages_for(cells).map_err(|_| Error::InvalidDriverValue("resident KV window"))?; + match self.retained.kv_resident.get() { + Some(set) if set != cells => { + Err(Error::InvalidDriverValue("resident KV window already configured differently")) + } + _ => { + self.retained.kv_resident.set(Some(cells)); + Ok(()) + } + } + } + + /// Resident pages (VRAM slots) a new streamed QSA owner of this context allocates. + pub(crate) fn kv_resident_pages(&self) -> Result { + geometry() + .resident_pages_for(self.retained.kv_resident.get().unwrap_or(KV_RESIDENT_DEFAULT_CELLS)) + .map_err(|_| Error::InvalidDriverValue("resident KV window")) + } +} diff --git a/crates/cuda-driver/src/lib.rs b/crates/cuda-driver/src/lib.rs index de0cd116..cf31551a 100644 --- a/crates/cuda-driver/src/lib.rs +++ b/crates/cuda-driver/src/lib.rs @@ -273,6 +273,7 @@ use q6_k_shape::q6_k_shape; use q6_k_shape::vector_shape; mod context; mod context_gate_up_projection_admission; +mod context_kv_resident; mod context_matvec_k_quant_projections; mod context_projection_kernel_slots; mod context_routed_gate_up_projection; diff --git a/crates/cuda-driver/src/prompt_sync_trace_types/retained.rs b/crates/cuda-driver/src/prompt_sync_trace_types/retained.rs index b909d156..1a7b5862 100644 --- a/crates/cuda-driver/src/prompt_sync_trace_types/retained.rs +++ b/crates/cuda-driver/src/prompt_sync_trace_types/retained.rs @@ -1,10 +1,11 @@ -//! The host-side lists a `Context` keeps for the verify-window modules and the deferred buffer frees. +//! What a `Context` keeps for itself: the verify-window modules, the deferred buffer frees and the resident KV window +//! its streamed QSA owners are sized from. -use super::{Rc, RefCell, ffi}; +use super::{Cell, Rc, RefCell, ffi}; #[cfg(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))] use super::original_verify; -/// Lists that live and die with the context: both start empty. +/// State that lives and dies with the context: the lists start empty, the KV window unset. #[derive(Default)] pub(crate) struct Retained { /// Sealed verify-window modules loaded under this context, shared by every verifier bank created later @@ -13,4 +14,6 @@ pub(crate) struct Retained { /// Buffers released while a prompt chunk's expert copies are in flight (cuMemFree synchronizes the whole /// device and would wait for every queued copy): freed by `flush_deferred_frees`. They stay charged. pub(crate) deferred_frees: RefCell>, + /// The resident KV window of this context's streamed QSA owners, in cells; `None` keeps the default window. + pub(crate) kv_resident: Cell>, } diff --git a/crates/cuda-driver/src/qsa_prompt_kv/staged_append/qsa_prompt_kv_bring_up.rs b/crates/cuda-driver/src/qsa_prompt_kv/staged_append/qsa_prompt_kv_bring_up.rs index dfb3cece..0873a244 100644 --- a/crates/cuda-driver/src/qsa_prompt_kv/staged_append/qsa_prompt_kv_bring_up.rs +++ b/crates/cuda-driver/src/qsa_prompt_kv/staged_append/qsa_prompt_kv_bring_up.rs @@ -10,7 +10,7 @@ use super::{ impl<'ctx> QsaPromptKv<'ctx> { pub(in super::super) fn new(c: &'ctx Context<'ctx>) -> Result { - let slots = crate::serving_context::kv_resident_pages()?; + let slots = c.kv_resident_pages()?; admit( c, geometry().resident_bytes_with(slots, Resolver::map_bytes(slots)?).map_err(|_| Error::BudgetExceeded)?, diff --git a/crates/cuda-driver/src/root_exports.rs b/crates/cuda-driver/src/root_exports.rs index 6ab48a34..a7165607 100644 --- a/crates/cuda-driver/src/root_exports.rs +++ b/crates/cuda-driver/src/root_exports.rs @@ -63,7 +63,7 @@ pub use leleka_cuda_model::original_mtp_loader::{ }; pub use leleka_cuda_model::serving_context::{ KV_RESIDENT_DEFAULT_CELLS, KV_RESIDENT_MIN_CELLS, MAX_CONTEXT_CELLS, ServingContext, TRAINED_CONTEXT_CELLS, - configure_kv_resident, configure_serving_context, kv_resident_cells, serving_context, + configure_serving_context, serving_context, }; #[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64", feature = "cuda-driver"))] diff --git a/crates/cuda-model/src/serving_context.rs b/crates/cuda-model/src/serving_context.rs index 4e2b0206..5b11f744 100644 --- a/crates/cuda-model/src/serving_context.rs +++ b/crates/cuda-model/src/serving_context.rs @@ -78,33 +78,6 @@ pub const KV_RESIDENT_DEFAULT_CELLS: usize = leleka_context::geometry::KV_RESIDE /// `qsa_kv_resident_min()`. pub const KV_RESIDENT_MIN_CELLS: usize = leleka_context::geometry::KV_RESIDENT_MIN_CELLS; -static KV_RESIDENT: OnceLock = OnceLock::new(); - -/// Fix the resident KV window of the streamed QSA layers for this process (`--kv-resident N`, the Original's -/// `qsa_set_kv_resident` before any state exists). `0` keeps the whole context resident; a positive count is -/// raised to `qsa_kv_resident_min()` (20480) and rounded up to whole pages. The first call wins; a later call -/// must request the same count. Every owner sizes its pool from the value when it is created and launches -/// with its own slot count, so an owner created before this call keeps the default window consistently. -pub fn configure_kv_resident(cells: usize) -> Result<()> { - geometry().resident_pages_for(cells).map_err(|_| Error::InvalidDriverValue("resident KV window"))?; - if *KV_RESIDENT.get_or_init(|| cells) == cells { - Ok(()) - } else { - Err(Error::InvalidDriverValue("resident KV window already configured differently")) - } -} - -/// The configured resident KV window in cells (the default 32768 when none was configured). -#[inline] -pub fn kv_resident_cells() -> usize { - KV_RESIDENT.get().copied().unwrap_or(KV_RESIDENT_DEFAULT_CELLS) -} - -/// Resident pages (VRAM slots) a new streamed QSA owner allocates. -pub fn kv_resident_pages() -> Result { - geometry().resident_pages_for(kv_resident_cells()).map_err(|_| Error::InvalidDriverValue("resident KV window")) -} - #[cfg(test)] mod tests { use super::*; @@ -121,7 +94,7 @@ mod tests { fn the_default_resident_window_is_the_frozen_8192_page_pool() { assert_eq!(KV_RESIDENT_DEFAULT_CELLS, 32_768); assert!(geometry().resident_pages_for(KV_RESIDENT_DEFAULT_CELLS) == Ok(8192)); - assert!(configure_kv_resident(MAX_CONTEXT_CELLS + 4).is_err()); + assert!(geometry().resident_pages_for(MAX_CONTEXT_CELLS + 4).is_err()); } /// Wherever the Original streams a layer (`kv_plan` mode 1, golden-tested in `leleka-experimental-ref`)