From ac1b55425c6bbe6680895fb6c2910cb1d01bff7f Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Wed, 2 Dec 2026 02:28:45 +0100 Subject: [PATCH] [HIP] Bound BUF_POOL with a 512MiB cap + LRU hipFree; guard moe_scatter_buckets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- crates/vetta-infer-hip/src/lib.rs | 25 ++++++++++++++++++++++++- kernels/hip/vetta_kernels.hip | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index b59be33..2e892e1 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -198,8 +198,19 @@ pub struct DeviceBuf { thread_local! { static BUF_POOL: std::cell::RefCell>> = 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 = 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,7 +343,18 @@ impl DeviceBuf { impl Drop for DeviceBuf { fn drop(&mut self) { if self.pooled && pool_enabled() && !self.ptr.is_null() { - BUF_POOL.with(|p| p.borrow_mut().entry(self.bytes).or_default().push(self.ptr)); + // 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); } } diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index 5c0c21f..7d0f881 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -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;