[HIP] copy_f32: device-side f32 copy for depth-block snapshot / bank rollback

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.
This commit is contained in:
adm committed 2026-12-02 00:00:23 +01:00
1 parent 48de65d395
commit 3a2ad4da49
2 files changed
+39

No files matched your search

+22
View File
@@ -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, m: c_int, n_experts: c_int, k_top: c_int, route_scale: c_float, stream: *mut c_void,
) -> c_int; ) -> 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_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_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_cast_f32_f16(x: *const c_float, y: *mut u16, n: usize, stream: *mut c_void) -> c_int;
pub fn vetta_nvfp4_gemv( pub fn vetta_nvfp4_gemv(
@@ -452,6 +453,14 @@ pub fn vec_add(a: &DeviceBuf, b: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<
Ok(()) 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). /// Launch k_cast_f32_bf16: f32 activation -> BF16 (round-to-nearest-even).
#[cfg(feature = "hip")] #[cfg(feature = "hip")]
pub fn cast_f32_bf16(x: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<(), String> { 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"); 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<f32> = (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] #[test]
#[ignore] #[ignore]
fn shortconv4_multistep_parity() { fn shortconv4_multistep_parity() {
+17
View File
@@ -664,6 +664,23 @@ extern "C" int vetta_vec_add(const float* a, const float* b, float* y, size_t n,
return 0; 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<<<grid, nt, 0, s>>>(src, dst, n);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_router_gemv: y[N] = x_f32[K] @ w_bf16[N,K]^T, f32 accumulate -------- // ---- 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 // 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 // (NOT bf16-rounded) against the bf16 gate weight promoted to f32. Rounding the