[HIP] Blocking host-wait schedule: kill the 100% CPU spin during GPU inference

Federico observed vetta-infer at 100% CPU while the GPU runs — the same busy-wait
the strata upstream had. Root cause: ROCm's default host-wait schedule spins on
every blocking synchronization (the per-token logits hipMemcpy D2H waits for the
whole 48-visit forward). The reference fixes it with --hip-sync blocking
(hipDeviceScheduleBlockingSync, ink_hip_backend.hip.cpp:607).

Apply the same: vetta_hip_init_blocking_sync() calls
hipSetDeviceFlags(hipDeviceScheduleBlockingSync), invoked once via std::sync::Once
at the top of DeviceBuf::alloc — before the first hipMalloc creates the device
context (tolerates hipErrorSetOnActiveProcess). Escape hatch
VETTA_NO_BLOCKING_SYNC=1 keeps the spin schedule for A/B measurement.

Measured A/B (220-token prefill, instantaneous /proc CPU):
  spin (VETTA_NO_BLOCKING_SYNC=1): 101.2%
  blocking (default):             54.2%
The ~47 pp of spin-wait is gone. The remaining ~54% is genuine kernel-launch
overhead (~1300 launches/token, no HIP graph yet) — collapsing launches via graph
capture is the deferred perf step that would bring host CPU near zero.

Parity EXACT (16 tok) unchanged; 18 hip + 5 gw GPU green; workspace -D warnings green.
This commit is contained in:
adm committed 2026-12-01 19:06:33 +01:00
1 parent 59c41cb613
commit 25200e2e79
3 files changed
+25

No files matched your search

+12
View File
@@ -24,6 +24,7 @@ mod ffi {
unsafe extern "C" { unsafe extern "C" {
pub fn vetta_hip_query_device(dev_id: c_int, out: *mut DeviceInfoRaw) -> c_int; pub fn vetta_hip_query_device(dev_id: c_int, out: *mut DeviceInfoRaw) -> c_int;
pub fn vetta_hip_init_blocking_sync() -> c_int;
pub fn vetta_buf_alloc(out: *mut *mut c_void, bytes: usize) -> c_int; pub fn vetta_buf_alloc(out: *mut *mut c_void, bytes: usize) -> c_int;
pub fn vetta_buf_free(p: *mut c_void) -> c_int; pub fn vetta_buf_free(p: *mut c_void) -> c_int;
pub fn vetta_buf_h2d(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int; pub fn vetta_buf_h2d(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int;
@@ -173,6 +174,17 @@ fn pool_enabled() -> bool {
#[cfg(feature = "hip")] #[cfg(feature = "hip")]
impl DeviceBuf { impl DeviceBuf {
pub fn alloc(bytes: usize) -> Result<Self, String> { 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();
}
});
if pool_enabled() { if pool_enabled() {
if let Some(ptr) = BUF_POOL.with(|p| p.borrow_mut().get_mut(&bytes).and_then(|v| v.pop())) { 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 });
+3
View File
@@ -27,6 +27,9 @@ int vetta_hip_register_uma_pages(void* host_aligned_ptr, size_t bytes, void** ou
int vetta_hip_unregister_uma_pages(void* host_aligned_ptr); int vetta_hip_unregister_uma_pages(void* host_aligned_ptr);
// Minimal device buffer lifecycle (executor foundation). // Minimal device buffer lifecycle (executor foundation).
// Set blocking host-wait schedule (no 100% CPU spin during GPU waits); call once
// before the first allocation creates the device context.
int vetta_hip_init_blocking_sync(void);
int vetta_buf_alloc(void** out, size_t bytes); int vetta_buf_alloc(void** out, size_t bytes);
int vetta_buf_free(void* p); int vetta_buf_free(void* p);
int vetta_buf_h2d(void* dst, const void* src, size_t bytes); int vetta_buf_h2d(void* dst, const void* src, size_t bytes);
+10
View File
@@ -43,6 +43,16 @@ extern "C" int vetta_hip_query_device(int dev_id, vetta_device_info_t* out) {
// ---- minimal device buffer lifecycle (UMA-aware) --------------------------- // ---- minimal device buffer lifecycle (UMA-aware) ---------------------------
// Host-wait schedule: BLOCKING so host synchronization (blocking hipMemcpy /
// hipStreamSynchronize) sleeps instead of spinning at 100% CPU while the GPU
// runs. Exact strata --hip-sync blocking (hipDeviceScheduleBlockingSync). Must
// run before the device context is created; tolerate hipErrorSetOnActiveProcess.
extern "C" int vetta_hip_init_blocking_sync(void) {
hipError_t e = hipSetDeviceFlags(hipDeviceScheduleBlockingSync);
if (e != hipSuccess && e != hipErrorSetOnActiveProcess) { (void)hipGetLastError(); return 1; }
return 0;
}
extern "C" int vetta_buf_alloc(void** out, size_t bytes) { extern "C" int vetta_buf_alloc(void** out, size_t bytes) {
if (!out) return 1; if (!out) return 1;
HIP_CHECK(hipMalloc(out, bytes)); HIP_CHECK(hipMalloc(out, bytes));