[HIP] Bound BUF_POOL with a 512MiB cap + LRU hipFree; guard moe_scatter_buckets

The exact-size thread-local BUF_POOL never called hipFree: every distinct
batched tail size (bu = suffix % chunk, varies per turn) parked ~100-160MiB
forever => unbounded multi-turn VRAM leak (audit 2026-12-02 §2.1). Track
POOL_BYTES and hipFree a dropped buffer once the pool exceeds MAX_POOL_BYTES
(512MiB); hot fixed-size intermediates still recycle. Also add the symmetric
e<0||e>=128 guard to k_moe_scatter_buckets (count_buckets had it) so a corrupt
router id cannot write exp_cur out of bounds.
This commit is contained in:
adm committed 2026-12-02 02:28:45 +01:00
1 parent 088ac1f66d
commit ac1b55425c
2 files changed
+24

No files matched your search

+23
View File
@@ -198,8 +198,19 @@ pub struct DeviceBuf {
thread_local! {
static BUF_POOL: std::cell::RefCell<std::collections::HashMap<usize, Vec<*mut std::os::raw::c_void>>> =
std::cell::RefCell::new(std::collections::HashMap::new());
// Total bytes currently retained (free) in the pool. Capped so the exact-size
// pool cannot accumulate every distinct batched-tail size (bu = suffix % chunk
// varies per turn) forever: beyond MAX_POOL_BYTES a dropped buffer is hipFree'd
// instead of pooled, bounding the multi-turn VRAM leak (audit 2026-12-02 §2.1).
static POOL_BYTES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Upper bound on bytes retained in the thread-local buffer pool. Hot fixed-size
/// intermediates (chunk=256 batched + M=1 decode) fit well under this; excess
/// distinct sizes are freed rather than retained without bound.
#[cfg(feature = "hip")]
const MAX_POOL_BYTES: usize = 512 * 1024 * 1024;
#[cfg(feature = "hip")]
static POOL_ENABLED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
@@ -264,6 +275,7 @@ impl DeviceBuf {
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())) {
POOL_BYTES.with(|c| c.set(c.get().saturating_sub(bytes)));
return Ok(Self { ptr, bytes, pooled: true });
}
}
@@ -331,10 +343,21 @@ impl DeviceBuf {
impl Drop for DeviceBuf {
fn drop(&mut self) {
if self.pooled && pool_enabled() && !self.ptr.is_null() {
// Retain only while under the global pool budget; otherwise hipFree so
// distinct one-off sizes (batched tail chunks) cannot leak unbounded.
let keep = POOL_BYTES.with(|c| {
if c.get().saturating_add(self.bytes) <= MAX_POOL_BYTES {
c.set(c.get() + self.bytes); true
} else { false }
});
if keep {
BUF_POOL.with(|p| p.borrow_mut().entry(self.bytes).or_default().push(self.ptr));
} else {
unsafe { ffi::vetta_buf_free(self.ptr); }
}
} else {
unsafe { ffi::vetta_buf_free(self.ptr); }
}
}
}
+1
View File
@@ -2120,6 +2120,7 @@ __global__ void __launch_bounds__(256) k_moe_scatter_buckets(
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= total) return;
const int e = topk_ids[i];
if (e < 0 || e >= 128) return; // symmetric with k_moe_count_buckets: never OOB exp_cur[e]
const int pos = atomicAdd(&exp_cur[e], 1);
bucket_tok[pos] = i / top_k;
slotk_to_bidx[i] = pos;