From 3a2ad4da49733671cedca433441d8165e062eac4 Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Wed, 2 Dec 2026 00:00:23 +0100 Subject: [PATCH] [HIP] copy_f32: device-side f32 copy for depth-block snapshot / bank rollback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit k_copy_f32 (dst=src, n elements): enables device-side depth-block snapshot and core-loop bank rollback in the batched forward, eliminating the M=1 host D2H/H2D round-trips (layer.rs block snapshot + bank rollback). Exact (test copy_f32_exact). Additive — decode default unchanged. Foundation for the device-resident depth-block arena [n_blocks,B,h] that attn_res_mix reads directly (already M>1). Verified: hip GPU 32 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx. --- crates/vetta-infer-hip/src/lib.rs | 22 ++++++++++++++++++++++ kernels/hip/vetta_kernels.hip | 17 +++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 710f9d5..65fdb36 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -76,6 +76,7 @@ mod ffi { m: c_int, n_experts: c_int, k_top: c_int, route_scale: c_float, stream: *mut c_void, ) -> c_int; pub fn vetta_vec_add(a: *const c_float, b: *const c_float, y: *mut c_float, n: usize, stream: *mut c_void) -> c_int; + pub fn vetta_copy_f32(src: *const c_float, dst: *mut c_float, n: usize, stream: *mut c_void) -> c_int; pub fn vetta_cast_f32_bf16(x: *const c_float, y: *mut u16, n: usize, stream: *mut c_void) -> c_int; pub fn vetta_cast_f32_f16(x: *const c_float, y: *mut u16, n: usize, stream: *mut c_void) -> c_int; pub fn vetta_nvfp4_gemv( @@ -452,6 +453,14 @@ pub fn vec_add(a: &DeviceBuf, b: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result< Ok(()) } +/// Device-side f32 copy (dst=src, n elements): depth-block snapshot / bank rollback. +#[cfg(feature = "hip")] +pub fn copy_f32(src: &DeviceBuf, dst: &DeviceBuf, n: usize) -> Result<(), String> { + let rc = unsafe { ffi::vetta_copy_f32(src.ptr as *const f32, dst.ptr as *mut f32, n, std::ptr::null_mut()) }; + if rc != 0 { return Err("copy_f32 launch failed".into()); } + Ok(()) +} + /// Launch k_cast_f32_bf16: f32 activation -> BF16 (round-to-nearest-even). #[cfg(feature = "hip")] pub fn cast_f32_bf16(x: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<(), String> { @@ -1682,6 +1691,19 @@ mod gpu_tests { eprintln!("ctrl_pack_batched M={m} D={d}: byte-identical to sequential"); } + /// copy_f32 device-side copy is exact (dst == src). + #[test] + #[ignore] + fn copy_f32_exact() { + let n = 1024usize; + let src: Vec = (0..n).map(|i| (i as f32 - 512.0) * 0.013).collect(); + let ds = DeviceBuf::alloc(n * 4).unwrap(); ds.h2d(&src).unwrap(); + let dd = DeviceBuf::alloc(n * 4).unwrap(); + copy_f32(&ds, &dd, n).unwrap(); + let mut got = vec![0f32; n]; dd.d2h(&mut got).unwrap(); + assert_eq!(got, src, "copy_f32 must be exact"); + } + #[test] #[ignore] fn shortconv4_multistep_parity() { diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index 48826dd..15c974f 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -664,6 +664,23 @@ extern "C" int vetta_vec_add(const float* a, const float* b, float* y, size_t n, return 0; } +// ---- k_copy_f32: device-side f32 copy (dst=src), for depth-block snapshot ---- +// and bank rollback in the batched forward (no host D2H/H2D round-trip). +__global__ void k_copy_f32(const float* __restrict__ src, float* __restrict__ dst, size_t n) { + size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x; + if (i < n) dst[i] = src[i]; +} + +extern "C" int vetta_copy_f32(const float* src, float* dst, size_t n, vetta_stream_t stream) { + if (n == 0) return 0; + const int nt = 256; + const size_t grid = (n + nt - 1) / nt; + hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0; + k_copy_f32<<>>(src, dst, n); + HIP_CHECK(hipGetLastError()); + return 0; +} + // ---- k_router_gemv: y[N] = x_f32[K] @ w_bf16[N,K]^T, f32 accumulate -------- // Exact strata B=1 router (launch_f32_linear): the router reads the F32 mlpin // (NOT bf16-rounded) against the bf16 gate weight promoted to f32. Rounding the