From 66a40631ee4fa5a45531f1eaf7b1951d4669626b Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Wed, 2 Dec 2026 00:55:31 +0100 Subject: [PATCH] [HIP] add synchronize + vram_used profiling bindings (hipDeviceSynchronize / hipMemGetInfo) for batched-forward phase + VRAM instrumentation --- crates/vetta-infer-hip/src/lib.rs | 19 +++++++++++++++++++ kernels/hip/vetta_kernels.hip | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 8d76f72..24cf6f6 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -31,6 +31,8 @@ mod ffi { pub fn vetta_buf_d2h(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int; pub fn vetta_buf_copy(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int; pub fn vetta_buf_copy2d(dst: *mut c_void, dpitch: usize, src: *const c_void, spitch: usize, width: usize, height: usize) -> c_int; + pub fn vetta_synchronize() -> c_int; + pub fn vetta_vram_used(out: *mut usize) -> c_int; pub fn vetta_rms_norm_f32( x: *const c_float, w: *const c_float, y: *mut c_float, rows: c_int, d: c_int, eps: c_float, stream: *mut c_void, @@ -210,6 +212,23 @@ pub fn set_buf_pool_enabled(on: bool) { POOL_ENABLED.store(on, std::sync::atomic::Ordering::Relaxed); } +/// Block until all queued GPU work completes. Used at profiling phase boundaries +/// (the batched forward is fully async otherwise); not on the hot path. +#[cfg(feature = "hip")] +pub fn synchronize() -> Result<(), String> { + let rc = unsafe { ffi::vetta_synchronize() }; + if rc != 0 { return Err("hipDeviceSynchronize failed".into()); } + Ok(()) +} + +/// VRAM used bytes (total - free via hipMemGetInfo), for allocation profiling. +#[cfg(feature = "hip")] +pub fn vram_used() -> usize { + let mut used = 0usize; + unsafe { ffi::vetta_vram_used(&mut used) }; + used +} + #[cfg(feature = "hip")] fn pool_enabled() -> bool { POOL_ENABLED.load(std::sync::atomic::Ordering::Relaxed) diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index 42eaba2..a92be5d 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -82,6 +82,19 @@ extern "C" int vetta_buf_copy2d(void* dst, size_t dpitch, const void* src, HIP_CHECK(hipMemcpy2D(dst, dpitch, src, spitch, width, height, hipMemcpyDeviceToDevice)); return 0; } +// Block until all queued GPU work completes (profiling phase boundaries; the +// batched forward is otherwise fully async with no host sync until final d2h). +extern "C" int vetta_synchronize(void) { + HIP_CHECK(hipDeviceSynchronize()); + return 0; +} +// VRAM used bytes (hipMemGetInfo: total-free), for allocation profiling. +extern "C" int vetta_vram_used(size_t* out) { + size_t free_b = 0, total_b = 0; + HIP_CHECK(hipMemGetInfo(&free_b, &total_b)); + if (out) *out = total_b - free_b; + return 0; +} // ---- k_rms_norm (kernel #2): y = x * rsqrt(mean(x^2)+eps) * w -------------- // One block per row; block-wide reduction over D. f32 in/out.