[HIP] add synchronize + vram_used profiling bindings (hipDeviceSynchronize / hipMemGetInfo) for batched-forward phase + VRAM instrumentation

This commit is contained in:
adm committed 2026-12-02 00:55:31 +01:00
1 parent c059a20aa9
commit 66a40631ee
2 files changed
+32

No files matched your search

+19
View File
@@ -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_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_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_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( pub fn vetta_rms_norm_f32(
x: *const c_float, w: *const c_float, y: *mut c_float, 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, 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); 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")] #[cfg(feature = "hip")]
fn pool_enabled() -> bool { fn pool_enabled() -> bool {
POOL_ENABLED.load(std::sync::atomic::Ordering::Relaxed) POOL_ENABLED.load(std::sync::atomic::Ordering::Relaxed)
+13
View File
@@ -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)); HIP_CHECK(hipMemcpy2D(dst, dpitch, src, spitch, width, height, hipMemcpyDeviceToDevice));
return 0; 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 -------------- // ---- 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. // One block per row; block-wide reduction over D. f32 in/out.