cuda-driver: the VRAM allocation ledger belongs to the Context, not a process cell (STRA-338)
The per-call-site ledger of LELEKA_VRAM_LEDGER is now part of what a Context retains: Context::allocate and the DeviceBuffer free note into the ledger of their own context, and the engine reports through Context::report_vram_ledger, so the OnceLock singleton of vram_ledger.rs and its architecture.json exception are gone. The operator switch stays an EnvSwitch and the default is unchanged (off: one cached bool read per allocation); with the switch on, each context now accounts for its own allocations instead of every context of the process sharing one table.
This commit is contained in:
@@ -221,7 +221,6 @@
|
||||
{"crate": "leleka-cuda-driver", "file": "src/qsa_prompt_resolver.rs", "ratchet": "process-global", "reason": "kept: pure memo of the resolver ABI proof of the embedded module, a function of the embedded bytes alone"},
|
||||
{"crate": "leleka-cuda-driver", "file": "src/qwen_ffi/asset_admission.rs", "ratchet": "process-global", "reason": "kept: per-operation pure memo of the PTX proof of an embedded sealed image, a function of the embedded bytes alone"},
|
||||
{"crate": "leleka-cuda-driver", "file": "src/qwen_ffi/n_types.rs", "ratchet": "process-global", "reason": "kept: the table of the PTX proof memos of asset_admission.rs, one cell per operation; pure functions of embedded bytes"},
|
||||
{"crate": "leleka-cuda-driver", "file": "src/vram_ledger.rs", "ratchet": "process-global", "reason": "kept for now (diagnostic, NOT a boundary): the LELEKA_VRAM_LEDGER singleton is fed by every allocation site through free functions; the correct owner is the Context, which every allocation already reaches. Not moved in this change (STRA-338 follow-up)"},
|
||||
{"crate": "leleka-cuda-driver", "file": "src/marker_files.rs", "ratchet": "process-global", "reason": "kept: the marker files of the A/B controls are operator-named paths read once with the time of the first look, so a hot path stats the disk at most every 250 ms; one type serves three controls"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_bench_frontend.rs", "ratchet": "process-global", "reason": "kept: FFI. `static mut stdout` is libc's C stream, declared in an extern block; it is the C library's own global, not ours"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/settings.rs", "ratchet": "process-global", "reason": "kept: the cell of each `static Setting` (operator switches and the CPU probes the kernels dispatch on), computed on first read from the immutable environment and the CPU; the kernels are free functions called from pool worker threads that carry no context to pass a value through, and a hot path pays one atomic load"}
|
||||
|
||||
@@ -54,6 +54,11 @@ impl Context<'_> {
|
||||
}
|
||||
Ok(CurrentGuard { context: self, active: true })
|
||||
}
|
||||
/// Print the allocation sites of this context that changed since the last report (nothing while
|
||||
/// `LELEKA_VRAM_LEDGER` is off).
|
||||
pub fn report_vram_ledger(&self, label: &str) {
|
||||
vram_ledger::report(&self.retained.vram_ledger, label);
|
||||
}
|
||||
pub fn allocate(&self, bytes: usize) -> Result<DeviceBuffer<'_>> {
|
||||
valid_size(bytes)?;
|
||||
let total = self.allocated.get().checked_add(bytes).ok_or(Error::BudgetExceeded)?;
|
||||
@@ -68,7 +73,7 @@ impl Context<'_> {
|
||||
// A successful allocation whose pop fails stays charged. The poisoned
|
||||
// context owns that allocation until destruction, so no unguarded free.
|
||||
let handle = combine(allocation, guard.finish())?;
|
||||
vram_ledger::note_alloc(handle.qwen_address(), bytes);
|
||||
vram_ledger::note_alloc(&self.retained.vram_ledger, handle.qwen_address(), bytes);
|
||||
Ok(DeviceBuffer { context: self, handle: Some(handle), bytes, _thread: PhantomData, borrowed: false })
|
||||
}
|
||||
/// Queue a buffer's release for after the chunk's expert stream (it stays charged until then); when the
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! DeviceBuffer host/device copies (whole buffer and ranges, staged through a host Vec, synchronised) and its release
|
||||
//! and Drop, plus the LELEKA_CHAIN 'mtp' selector chain_selects_mtp with its no-driver stubs.
|
||||
|
||||
use super::{DeviceBuffer, Error, Result, combine, compute_drain, vram_ledger};
|
||||
#[cfg(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
|
||||
use super::original_verify;
|
||||
use super::{DeviceBuffer, Error, Result, combine, compute_drain, vram_ledger};
|
||||
|
||||
impl DeviceBuffer<'_> {
|
||||
pub fn len(&self) -> usize {
|
||||
@@ -153,7 +153,7 @@ impl DeviceBuffer<'_> {
|
||||
if self.borrowed {
|
||||
return Ok(());
|
||||
}
|
||||
vram_ledger::note_free(handle.qwen_address());
|
||||
vram_ledger::note_free(&self.context.retained.vram_ledger, handle.qwen_address());
|
||||
if compute_drain::background() != 0 && !self.context.poisoned.get() {
|
||||
// a chunk expert stream is in flight: do not synchronize the device now
|
||||
return self.context.defer_free(handle, self.bytes);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#[cfg(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
|
||||
use super::original_verify;
|
||||
use super::{Cell, Rc, RefCell, ffi};
|
||||
use crate::vram_ledger::Ledger;
|
||||
use leleka_cuda_model::serving_context::ServingContext;
|
||||
|
||||
/// State that lives and dies with the context: its serving context, the lists (empty at the start) and the KV window
|
||||
@@ -20,6 +21,8 @@ pub(crate) struct Retained {
|
||||
pub(crate) deferred_frees: RefCell<Vec<(ffi::BufferHandle, usize)>>,
|
||||
/// The resident KV window of this context's streamed QSA owners, in cells; `None` keeps the default window.
|
||||
pub(crate) kv_resident: Cell<Option<usize>>,
|
||||
/// The device allocations of this context by call site (`LELEKA_VRAM_LEDGER=1`; empty while it is off).
|
||||
pub(crate) vram_ledger: RefCell<Ledger>,
|
||||
}
|
||||
impl Retained {
|
||||
pub(crate) fn new(serving: ServingContext) -> Self {
|
||||
@@ -28,6 +31,7 @@ impl Retained {
|
||||
window_modules: RefCell::default(),
|
||||
deferred_frees: RefCell::default(),
|
||||
kv_resident: Cell::new(None),
|
||||
vram_ledger: RefCell::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
//! estimated to reserve: on the WDDM laptop probe (`scripts/dev/alloc-granularity-probe.py`) an allocation above 1 MiB
|
||||
//! is rounded up to a multiple of 2 MiB (3 MiB takes 4 MiB, 2.5 MiB takes 4 MiB) while smaller ones are packed; the
|
||||
//! 5090 load ledger agrees (the 322.1 MiB token table takes 324 MiB). The estimate is the "granularity waste" column.
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
const MIB: f64 = 1_048_576.0;
|
||||
const GRANULE: usize = 2 << 20;
|
||||
@@ -33,7 +33,7 @@ struct Site {
|
||||
reported_n: usize,
|
||||
}
|
||||
|
||||
/// The accounting itself, free of the process-global state so that tests can drive it.
|
||||
/// The accounting of one `Context`, driven by the tests as well.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Ledger {
|
||||
sites: Vec<Site>,
|
||||
@@ -176,38 +176,33 @@ fn strip_hash(symbol: &str) -> &str {
|
||||
}
|
||||
}
|
||||
|
||||
fn state() -> &'static Mutex<Ledger> {
|
||||
static STATE: OnceLock<Mutex<Ledger>> = OnceLock::new();
|
||||
STATE.get_or_init(|| Mutex::new(Ledger::default()))
|
||||
}
|
||||
|
||||
/// `LELEKA_VRAM_LEDGER=1`.
|
||||
pub fn enabled() -> bool {
|
||||
crate::settings::VRAM_LEDGER.get()
|
||||
}
|
||||
|
||||
pub(crate) fn note_alloc(address: u64, bytes: usize) {
|
||||
pub(crate) fn note_alloc(ledger: &RefCell<Ledger>, address: u64, bytes: usize) {
|
||||
if !enabled() {
|
||||
return;
|
||||
}
|
||||
let trace = std::backtrace::Backtrace::force_capture().to_string();
|
||||
let key = site_key(&trace);
|
||||
state().lock().unwrap_or_else(|e| e.into_inner()).alloc(address, bytes, &key);
|
||||
ledger.borrow_mut().alloc(address, bytes, &key);
|
||||
}
|
||||
|
||||
pub(crate) fn note_free(address: u64) {
|
||||
pub(crate) fn note_free(ledger: &RefCell<Ledger>, address: u64) {
|
||||
if !enabled() {
|
||||
return;
|
||||
}
|
||||
state().lock().unwrap_or_else(|e| e.into_inner()).free(address);
|
||||
ledger.borrow_mut().free(address);
|
||||
}
|
||||
|
||||
/// Print the sites that changed since the last report to stderr (nothing when the ledger is off).
|
||||
pub fn report(label: &str) {
|
||||
pub(crate) fn report(ledger: &RefCell<Ledger>, label: &str) {
|
||||
if !enabled() {
|
||||
return;
|
||||
}
|
||||
let text = state().lock().unwrap_or_else(|e| e.into_inner()).report(label);
|
||||
let text = ledger.borrow_mut().report(label);
|
||||
eprintln!("{text}");
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ fn forward_outcome<P>(pending: Result<P>, cancel: &mut impl FnMut() -> bool) ->
|
||||
pub(super) fn vram_note(context: &Context<'_>, label: &str) {
|
||||
// LELEKA_VRAM_LEDGER=1: the device allocations that appeared since the previous note, per call site (nothing when
|
||||
// off)
|
||||
leleka_cuda_driver::vram_ledger::report(label);
|
||||
context.report_vram_ledger(label);
|
||||
if profile::enabled() {
|
||||
if let Ok((free, total)) = context.memory_info() {
|
||||
// the low-water mark of the card's free VRAM over every note of this process: the number the reserve must
|
||||
|
||||
Reference in New Issue
Block a user