From 25200e2e7982dcde175a0764497fad67970bb285 Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Tue, 1 Dec 2026 19:06:33 +0100 Subject: [PATCH] [HIP] Blocking host-wait schedule: kill the 100% CPU spin during GPU inference MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- crates/vetta-infer-hip/src/lib.rs | 12 ++++++++++++ kernels/hip/vetta_kernels.h | 3 +++ kernels/hip/vetta_kernels.hip | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 2dd0f67..01b001c 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -24,6 +24,7 @@ mod ffi { unsafe extern "C" { 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_free(p: *mut c_void) -> 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")] impl DeviceBuf { pub fn alloc(bytes: usize) -> Result { + // 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 let Some(ptr) = BUF_POOL.with(|p| p.borrow_mut().get_mut(&bytes).and_then(|v| v.pop())) { return Ok(Self { ptr, bytes }); diff --git a/kernels/hip/vetta_kernels.h b/kernels/hip/vetta_kernels.h index b9741ca..1ebcfe7 100644 --- a/kernels/hip/vetta_kernels.h +++ b/kernels/hip/vetta_kernels.h @@ -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); // 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_free(void* p); int vetta_buf_h2d(void* dst, const void* src, size_t bytes); diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index f5f2064..7db14c0 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -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) --------------------------- +// 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) { if (!out) return 1; HIP_CHECK(hipMalloc(out, bytes));