[HIP][LD][GW] Bound VRAM across requests: unpooled KV + FP16 serve KV + bucketed flash scratch
Federico saw VRAM grow ~7 GiB per agent step (44.9/48 GiB, near OOM). Root cause: the KV cache scales with max_seq=prompt+max_new (varies per request) and the exact-size DeviceBuf pool retained every distinct KV size, so a multi-turn chat accumulated KV without bound. The full-attn flash scores scratch (n_win grows per token) leaked similarly. - DeviceBuf gains a 'pooled' flag + alloc_unpooled(): always hipFree on drop, never pooled. KV (k_cache/v_cache) uses it -> freed at request end, no accumulation. Per-token fixed-size intermediates stay pooled (decode speed). - flash_scores_scratch buckets the allocation to a power of two so the pool reuses it as cur_pos grows (kernel still strides by actual n_win <= bucket). - KV dtype: FP16 for the resident serve (exact strata KvDtype::FP16 default, parity-verified EXACT, half the VRAM of F32). Safe process-wide switch set_kv_f16(); InklingEngine::new opts in. The ld CPU-oracle parity tests keep the F32 default (VETTA_F32_KV=1 also forces F32). Measured (new binary, 3 requests of 200/400/600 prompt tokens): VRAM 7.66 -> 8.07 -> 8.33 GiB (small bounded expert lazy-load growth, NOT +7 GiB/step); clean release to 0.34 GiB on QUIT. Parity EXACT (16 tok); 18 ld + 18 hip + 5 gw GPU green; workspace -D warnings green; 5 Lean 0 sorryAx.
This commit is contained in:
1 parent
b1f65942c8
commit
c9713c8bc1
3 files changed
+68
-22
No files matched your search
@@ -136,6 +136,10 @@ pub fn query_device(dev_id: i32) -> Option<DeviceInfo> {
|
||||
pub struct DeviceBuf {
|
||||
pub ptr: *mut std::os::raw::c_void,
|
||||
pub bytes: usize,
|
||||
/// Whether drop returns this buffer to the size-bucketed pool. Large
|
||||
/// per-request buffers whose size varies (KV cache) are unpooled so they are
|
||||
/// truly freed instead of accumulating every distinct size in the pool.
|
||||
pooled: bool,
|
||||
}
|
||||
|
||||
// Size-bucketed free list of device buffers. The layer forward allocates and
|
||||
@@ -171,29 +175,46 @@ fn pool_enabled() -> bool {
|
||||
&& std::env::var("VETTA_NO_BUF_POOL").is_err()
|
||||
}
|
||||
|
||||
/// Set the blocking host-wait schedule once, before the first hipMalloc creates
|
||||
/// the device context, so GPU waits sleep instead of spinning at 100% CPU (exact
|
||||
/// strata --hip-sync blocking). Escape hatch VETTA_NO_BLOCKING_SYNC=1 keeps the
|
||||
/// ROCm default spin schedule (for A/B CPU measurement).
|
||||
#[cfg(feature = "hip")]
|
||||
fn ensure_sync_init() {
|
||||
static SYNC_INIT: std::sync::Once = std::sync::Once::new();
|
||||
SYNC_INIT.call_once(|| unsafe {
|
||||
if std::env::var("VETTA_NO_BLOCKING_SYNC").is_err() {
|
||||
ffi::vetta_hip_init_blocking_sync();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
fn raw_alloc(bytes: usize) -> Result<*mut std::os::raw::c_void, String> {
|
||||
let mut ptr: *mut std::os::raw::c_void = std::ptr::null_mut();
|
||||
let rc = unsafe { ffi::vetta_buf_alloc(&mut ptr, bytes) };
|
||||
if rc != 0 || ptr.is_null() { return Err(format!("hipMalloc({bytes}) failed")); }
|
||||
Ok(ptr)
|
||||
}
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
impl DeviceBuf {
|
||||
pub fn alloc(bytes: usize) -> Result<Self, String> {
|
||||
// Set the blocking host-wait schedule once, before the first hipMalloc
|
||||
// creates the device context, so GPU waits sleep instead of spinning at
|
||||
// 100% CPU (exact strata --hip-sync blocking).
|
||||
static SYNC_INIT: std::sync::Once = std::sync::Once::new();
|
||||
SYNC_INIT.call_once(|| unsafe {
|
||||
// Escape hatch VETTA_NO_BLOCKING_SYNC=1 keeps the ROCm default spin
|
||||
// schedule (for A/B CPU measurement).
|
||||
if std::env::var("VETTA_NO_BLOCKING_SYNC").is_err() {
|
||||
ffi::vetta_hip_init_blocking_sync();
|
||||
}
|
||||
});
|
||||
ensure_sync_init();
|
||||
if pool_enabled() {
|
||||
if let Some(ptr) = BUF_POOL.with(|p| p.borrow_mut().get_mut(&bytes).and_then(|v| v.pop())) {
|
||||
return Ok(Self { ptr, bytes });
|
||||
return Ok(Self { ptr, bytes, pooled: true });
|
||||
}
|
||||
}
|
||||
let mut ptr: *mut std::os::raw::c_void = std::ptr::null_mut();
|
||||
let rc = unsafe { ffi::vetta_buf_alloc(&mut ptr, bytes) };
|
||||
if rc != 0 || ptr.is_null() { return Err(format!("hipMalloc({bytes}) failed")); }
|
||||
Ok(Self { ptr, bytes })
|
||||
Ok(Self { ptr: raw_alloc(bytes)?, bytes, pooled: true })
|
||||
}
|
||||
/// Allocate a buffer that is ALWAYS `hipFree`'d on drop (never pooled). For
|
||||
/// large per-request buffers whose size varies between requests (the KV
|
||||
/// cache scales with prompt+max_new): the exact-size pool would retain every
|
||||
/// distinct size and grow VRAM without bound across a multi-turn chat.
|
||||
pub fn alloc_unpooled(bytes: usize) -> Result<Self, String> {
|
||||
ensure_sync_init();
|
||||
Ok(Self { ptr: raw_alloc(bytes)?, bytes, pooled: false })
|
||||
}
|
||||
pub fn h2d(&self, src: &[f32]) -> Result<(), String> {
|
||||
let bytes = src.len() * 4;
|
||||
@@ -226,7 +247,7 @@ impl DeviceBuf {
|
||||
#[cfg(feature = "hip")]
|
||||
impl Drop for DeviceBuf {
|
||||
fn drop(&mut self) {
|
||||
if pool_enabled() && !self.ptr.is_null() {
|
||||
if self.pooled && pool_enabled() && !self.ptr.is_null() {
|
||||
BUF_POOL.with(|p| p.borrow_mut().entry(self.bytes).or_default().push(self.ptr));
|
||||
} else {
|
||||
unsafe { ffi::vetta_buf_free(self.ptr); }
|
||||
@@ -292,7 +313,12 @@ pub fn flash_gqa_decode(q: &DeviceBuf, k: &DeviceBuf, v: &DeviceBuf, out: &Devic
|
||||
fn flash_scores_scratch(n_head: i32, swa_window: i32, cur_pos: i32) -> Result<DeviceBuf, String> {
|
||||
let k_start = if swa_window > 0 && cur_pos >= swa_window { cur_pos - swa_window + 1 } else { 0 };
|
||||
let n_win = (cur_pos - k_start + 1).max(1) as usize;
|
||||
DeviceBuf::alloc(n_head as usize * n_win * 4)
|
||||
// Bucket the allocation to a power of two so the pool reuses it as cur_pos
|
||||
// grows (full-attn n_win increases every token); otherwise every distinct
|
||||
// size is retained and VRAM creeps up over a long generation. The kernel
|
||||
// still strides by the actual n_win (<= bucket), so the extra slack is safe.
|
||||
let cap = n_win.next_power_of_two();
|
||||
DeviceBuf::alloc(n_head as usize * cap * 4)
|
||||
}
|
||||
|
||||
/// Launch k_flash_swa_gqa_decode: Inkling SWA window + relpos + sink + gate.
|
||||
|
||||
Reference in new issue
Block a user