[HIP] FP16 expert-path kernels (strata B=1 parity), opt-in; refactor run_experts

Add the exact strata B=1 FP16 expert path as verified kernels: k_cast_f32_f16 (RNE), k_nvfp4_gemv_f16 / k_mxfp8_gemv_f16 (FP16 activations, group partial dot then scale-fma, fdot2 order), k_situ_f16 (SiTU f32 -> stored FP16). Rust wrappers + expert_gemv_f16 dispatch.

Refactor the duplicated SWA/full expert loops into ld::layer::run_experts. Default stays all-f32 (verified parity baseline); VETTA_F16_EXPERT=1 selects the FP16 path.

MEASURED A/B (tok100): f32 argmax 220 (23.074) / 68014 22.643; fp16 argmax 220 (23.069) / 68014 22.632. FP16 shifts logits ~0.01 but does NOT flip the near-tie: the residual gap vs reference is the core-loop pass 1/2, NOT expert precision. FP16 kept opt-in for once the loop is fixed.

New real_expert_nvfp4_gemv_f16_parity (GPU fp16 vs CPU oracle over same fp16-rounded x) green. Verified: workspace -D warnings, 18 ld GPU + 17 hip GPU green.
This commit is contained in:
adm committed 2026-12-01 16:53:18 +01:00
1 parent 26bff57645
commit e4dcc5e015
5 files changed
+316 -31

No files matched your search

+44 -1
View File
@@ -60,12 +60,19 @@ mod ffi {
) -> 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_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(
packed: *const u8, scales: *const u8, outer_scale: c_float, x: *const c_float, y: *mut c_float,
n: c_int, k: c_int, stream: *mut c_void,
) -> c_int;
pub fn vetta_nvfp4_gemv_f16(
packed: *const u8, scales: *const u8, outer_scale: c_float, x: *const u16, y: *mut c_float,
n: c_int, k: c_int, stream: *mut c_void,
) -> c_int;
pub fn vetta_mxfp8_gemv(weights: *const u8, scales: *const u8, x: *const c_float, y: *mut c_float, n: c_int, k: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_mxfp8_gemv_f16(weights: *const u8, scales: *const u8, x: *const u16, y: *mut c_float, n: c_int, k: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_situ(gu: *const c_float, act: *mut c_float, inter: c_int, beta: c_float, lb: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_situ_f16(gu: *const c_float, act: *mut u16, inter: c_int, beta: c_float, lb: c_float, stream: *mut c_void) -> c_int;
pub fn vetta_moe_reduce(y_part: *const c_float, tk_w: *const c_float, out: *mut c_float, top_k: c_int, hidden: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_shortconv4_decode(cur: *const c_float, static_w: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_shortconv4_dyn_decode(cur: *const c_float, static_w: *const c_float, dyn_taps: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, n_heads: c_int, stream: *mut c_void) -> c_int;
@@ -279,7 +286,7 @@ pub fn mxfp8_gemv(weights: &DeviceBuf, scales: &DeviceBuf, x: &DeviceBuf, y: &De
Ok(())
}
/// Launch k_situ: SiTU double-softcapped activation (gu=[gate;up] -> act).
/// Launch k_situ: SiTU double-softcapped activation (gu INTERLEAVED gate/up -> act f32).
#[cfg(feature = "hip")]
pub fn situ(gu: &DeviceBuf, act: &DeviceBuf, inter: i32, beta: f32, lb: f32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_situ(gu.ptr as *const f32, act.ptr as *mut f32, inter, beta, lb, std::ptr::null_mut()) };
@@ -287,6 +294,42 @@ pub fn situ(gu: &DeviceBuf, act: &DeviceBuf, inter: i32, beta: f32, lb: f32) ->
Ok(())
}
/// Launch k_cast_f32_f16: f32 activation -> FP16 (RNE), exact strata k_f32_to_f16.
#[cfg(feature = "hip")]
pub fn cast_f32_f16(x: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<(), String> {
let rc = unsafe { ffi::vetta_cast_f32_f16(x.ptr as *const f32, y.ptr as *mut u16, n, std::ptr::null_mut()) };
if rc != 0 { return Err("cast_f32_f16 launch failed".into()); }
Ok(())
}
/// Launch k_nvfp4_gemv_f16: NVFP4 GEMV reading FP16 activations (strata B=1 path).
#[cfg(feature = "hip")]
pub fn nvfp4_gemv_f16(packed: &DeviceBuf, scales: &DeviceBuf, outer_scale: f32, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32) -> Result<(), String> {
let rc = unsafe {
ffi::vetta_nvfp4_gemv_f16(packed.ptr as *const u8, scales.ptr as *const u8, outer_scale, x.ptr as *const u16, y.ptr as *mut f32, n, k, std::ptr::null_mut())
};
if rc != 0 { return Err("nvfp4_gemv_f16 launch failed".into()); }
Ok(())
}
/// Launch k_mxfp8_gemv_f16: MXFP8 GEMV reading FP16 activations (strata B=1 path).
#[cfg(feature = "hip")]
pub fn mxfp8_gemv_f16(weights: &DeviceBuf, scales: &DeviceBuf, x: &DeviceBuf, y: &DeviceBuf, n: i32, k: i32) -> Result<(), String> {
let rc = unsafe {
ffi::vetta_mxfp8_gemv_f16(weights.ptr as *const u8, scales.ptr as *const u8, x.ptr as *const u16, y.ptr as *mut f32, n, k, std::ptr::null_mut())
};
if rc != 0 { return Err("mxfp8_gemv_f16 launch failed".into()); }
Ok(())
}
/// Launch k_situ_f16: SiTU computed in f32, stored FP16 (exact strata act_out).
#[cfg(feature = "hip")]
pub fn situ_f16(gu: &DeviceBuf, act: &DeviceBuf, inter: i32, beta: f32, lb: f32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_situ_f16(gu.ptr as *const f32, act.ptr as *mut u16, inter, beta, lb, std::ptr::null_mut()) };
if rc != 0 { return Err("situ_f16 launch failed".into()); }
Ok(())
}
/// Launch k_moe_reduce: out[d] = sum_e tk_w[e]*y_part[e,d].
#[cfg(feature = "hip")]
pub fn moe_reduce(y_part: &DeviceBuf, tk_w: &DeviceBuf, out: &DeviceBuf, top_k: i32, hidden: i32) -> Result<(), String> {