[HIP] Device buffer pool + right-sized GEMV blocks (decode perf)

DeviceBuf::alloc/Drop now reuse freed buffers via a thread-local size-bucketed
free list instead of hipMalloc/hipFree per intermediate (~1200 alloc/free per
token). Safe: all launches share the default stream and host copies are blocking,
so a recycled buffer's prior use is ordered before its next launch. Escape hatch
VETTA_NO_BUF_POOL=1. nvfp4_gemv 256->128 threads (K/16=144 groups: half the waves
were idle), mxfp8_gemv 256->64 (K/32<=72 groups); cheaper block reduction.

MEASURED (gfx1150 release, GEN 64): decode 269ms/tok -> 234ms/tok (3.72->4.27
tok/s, ~15%). Parity unchanged: 18 ld GPU + 17 hip GPU green. Profiling shows the
remaining hotspot is the scalar-dequant expert GEMV (~87ms/tok) — the fdot2
hardware-dot + device-resident blocks + hipGraph path is the next big lever.
This commit is contained in:
adm committed 2026-12-01 17:27:07 +01:00
1 parent 0a46c4dd7b
commit 4bc268f69c
2 files changed
+30 -2

No files matched your search

+28
View File
@@ -130,9 +130,33 @@ pub struct DeviceBuf {
pub bytes: usize,
}
/// Size-bucketed free list of device buffers. The layer forward allocates and
/// frees ~25 short-lived intermediates per visit (~1200 hipMalloc/hipFree per
/// token); hipMalloc/hipFree each synchronize the device, dominating decode
/// latency. Reusing freed buffers by exact size removes that churn. Safe because
/// all launches share the default stream and every host copy (hipMemcpy) is
/// blocking: a buffer is only recycled after its last use is ordered before the
/// next launch that reuses it. Thread-local (cargo test runs tests in parallel).
/// Escape hatch: VETTA_NO_BUF_POOL=1 forces raw hipMalloc/hipFree.
#[cfg(feature = "hip")]
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());
}
#[cfg(feature = "hip")]
fn pool_enabled() -> bool {
std::env::var("VETTA_NO_BUF_POOL").is_err()
}
#[cfg(feature = "hip")]
impl DeviceBuf {
pub fn alloc(bytes: usize) -> Result<Self, String> {
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 });
}
}
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")); }
@@ -169,9 +193,13 @@ impl DeviceBuf {
#[cfg(feature = "hip")]
impl Drop for DeviceBuf {
fn drop(&mut self) {
if 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); }
}
}
}
/// Launch k_rms_norm over `rows` rows of width `d` (device pointers).
#[cfg(feature = "hip")]
+2 -2
View File
@@ -586,7 +586,7 @@ extern "C" int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
int N, int K, vetta_stream_t stream) {
if (N <= 0 || K <= 0 || (K % 16) != 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_nvfp4_gemv<<<N, 256, 0, s>>>(packed, scales, outer_scale, x, y, N, K);
k_nvfp4_gemv<<<N, 128, 0, s>>>(packed, scales, outer_scale, x, y, N, K);
HIP_CHECK(hipGetLastError());
return 0;
}
@@ -683,7 +683,7 @@ extern "C" int vetta_mxfp8_gemv(const uint8_t* weights, const uint8_t* scales,
vetta_stream_t stream) {
if (N <= 0 || K <= 0 || (K % 32) != 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_mxfp8_gemv<<<N, 256, 0, s>>>(weights, scales, x, y, N, K);
k_mxfp8_gemv<<<N, 64, 0, s>>>(weights, scales, x, y, N, K);
HIP_CHECK(hipGetLastError());
return 0;
}