[HIP] SiTU activation + MoE reduce kernels — full MoE primitive set verified

- k_situ: SiTU double-softcapped MoE activation, exact strata situ_f32 —
  g_act = beta*tanh(g/beta)*sigmoid(g), u_act = lb*tanh(u/lb), act = g_act*u_act
  (beta=4, lb=25). gu=[gate;up] from w13 -> act[inter].
- k_moe_reduce: out[d] = sum_e tk_w[e]*y_part[e,d] (weighted expert sum).
- C ABI + safe wrappers (situ, moe_reduce) + CPU-oracle parity tests.

With nvfp4_gemv (w13/w2) + moe_router_topk, the full MoE compute primitive
set is now verified on gfx1150: router -> per-expert nvfp4 GEMV -> SiTU ->
nvfp4 GEMV -> weighted reduce. Parity: SiTU ~4e-7 (inter 1152/64/256),
moe_reduce ~3e-6 (top-8, hidden 2304). Ten GPU kernels verified.

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (4 files, 42 theorems) green; GPU parity 10/10 (--features hip).
This commit is contained in:
adm committed 2026-12-01 14:13:30 +01:00
1 parent bd49da5636
commit 529709cc64
3 files changed
+123

No files matched your search

+65
View File
@@ -54,6 +54,8 @@ mod ffi {
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_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_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;
}
}
@@ -222,6 +224,22 @@ pub fn nvfp4_gemv(packed: &DeviceBuf, scales: &DeviceBuf, outer_scale: f32, x: &
Ok(())
}
/// Launch k_situ: SiTU double-softcapped activation (gu=[gate;up] -> act).
#[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()) };
if rc != 0 { return Err("situ 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> {
let rc = unsafe { ffi::vetta_moe_reduce(y_part.ptr as *const f32, tk_w.ptr as *const f32, out.ptr as *mut f32, top_k, hidden, std::ptr::null_mut()) };
if rc != 0 { return Err("moe_reduce launch failed".into()); }
Ok(())
}
#[cfg(all(test, feature = "hip"))]
mod gpu_tests {
use super::*;
@@ -501,6 +519,53 @@ mod gpu_tests {
}
}
#[test]
#[ignore]
fn situ_parity_vs_cpu() {
let (beta, lb) = (4.0f32, 25.0f32);
for &inter in &[1152usize, 64, 256] {
let gu: Vec<f32> = (0..2 * inter).map(|i| ((i % 61) as f32 - 30.0) * 0.3).collect();
let want: Vec<f32> = (0..inter).map(|j| {
let g = gu[j];
let u = gu[inter + j];
let sig = 1.0 / (1.0 + (-g).exp());
let ga = beta * (g / beta).tanh() * sig;
let ua = lb * (u / lb).tanh();
ga * ua
}).collect();
let dgu = DeviceBuf::alloc(gu.len() * 4).unwrap();
let da = DeviceBuf::alloc(inter * 4).unwrap();
dgu.h2d(&gu).unwrap();
situ(&dgu, &da, inter as i32, beta, lb).unwrap();
let mut got = vec![0f32; inter];
da.d2h(&mut got).unwrap();
let mut max_rel = 0f32;
for (g, w) in got.iter().zip(want.iter()) { max_rel = max_rel.max((g - w).abs() / w.abs().max(1e-3)); }
eprintln!("situ inter={inter} max_rel_err={max_rel:.3e}");
assert!(max_rel < 1e-5, "situ parity failed: {max_rel}");
}
}
#[test]
#[ignore]
fn moe_reduce_parity_vs_cpu() {
let (top_k, hidden) = (8usize, 2304usize);
let y_part: Vec<f32> = (0..top_k * hidden).map(|i| ((i % 43) as f32 - 21.0) * 0.02).collect();
let tk_w: Vec<f32> = (0..top_k).map(|e| 0.5 + (e as f32) * 0.1).collect();
let want: Vec<f32> = (0..hidden).map(|d| (0..top_k).map(|e| tk_w[e] * y_part[e * hidden + d]).sum()).collect();
let dy = DeviceBuf::alloc(y_part.len() * 4).unwrap();
let dw = DeviceBuf::alloc(tk_w.len() * 4).unwrap();
let dout = DeviceBuf::alloc(hidden * 4).unwrap();
dy.h2d(&y_part).unwrap(); dw.h2d(&tk_w).unwrap();
moe_reduce(&dy, &dw, &dout, top_k as i32, hidden as i32).unwrap();
let mut got = vec![0f32; hidden];
dout.d2h(&mut got).unwrap();
let mut max_rel = 0f32;
for (g, w) in got.iter().zip(want.iter()) { max_rel = max_rel.max((g - w).abs() / w.abs().max(1e-3)); }
eprintln!("moe_reduce max_rel_err={max_rel:.3e}");
assert!(max_rel < 1e-5, "moe_reduce parity failed: {max_rel}");
}
fn bytemuck_u16(v: &[u16]) -> &[u8] {
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2) }
}
+8
View File
@@ -70,6 +70,14 @@ int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
float outer_scale, const float* x, float* y,
int N, int K, vetta_stream_t stream);
// k_situ: SiTU double-softcapped MoE activation. gu=[gate;up] (2*inter) -> act.
int vetta_situ(const float* gu, float* act, int inter, float beta, float lb,
vetta_stream_t stream);
// k_moe_reduce: out[d] = sum_e tk_w[e]*y_part[e,d] (weighted expert sum).
int vetta_moe_reduce(const float* y_part, const float* tk_w, float* out,
int top_k, int hidden, vetta_stream_t stream);
// Core <15 microkernel entry points (implemented progressively in M1 & M2):
// 1. k_embed_bf16
// 2. k_rms_norm
+50
View File
@@ -419,3 +419,53 @@ extern "C" int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- SiTU activation (exact strata situ_f32, double-softcapped) ------------
// gu = [gate(inter); up(inter)] from w13. act[j] = g_act * u_act where
// g_act = beta*tanh(g/beta)*sigmoid(g), u_act = lb*tanh(u/lb).
__device__ __forceinline__ float situ_f32(float g, float u, float beta, float lb) {
const float sig = 1.f / (1.f + __expf(-g));
const float g_act = beta * tanhf(g / beta) * sig;
const float u_act = lb * tanhf(u / lb);
return g_act * u_act;
}
__global__ void k_situ(const float* __restrict__ gu, float* __restrict__ act,
int inter, float beta, float lb) {
int j = blockIdx.x * blockDim.x + threadIdx.x;
if (j < inter) act[j] = situ_f32(gu[j], gu[inter + j], beta, lb);
}
extern "C" int vetta_situ(const float* gu, float* act, int inter, float beta,
float lb, vetta_stream_t stream) {
if (inter <= 0) return 1;
const int nt = 256;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_situ<<<(inter + nt - 1) / nt, nt, 0, s>>>(gu, act, inter, beta, lb);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_moe_reduce: out[d] = sum_e tk_w[e] * y_part[e, d] -------------------
__global__ void k_moe_reduce(const float* __restrict__ y_part,
const float* __restrict__ tk_w, float* __restrict__ out,
int top_k, int hidden) {
int d = blockIdx.x * blockDim.x + threadIdx.x;
if (d < hidden) {
float acc = 0.f;
for (int e = 0; e < top_k; ++e) acc += tk_w[e] * y_part[(size_t)e * hidden + d];
out[d] = acc;
}
}
extern "C" int vetta_moe_reduce(const float* y_part, const float* tk_w, float* out,
int top_k, int hidden, vetta_stream_t stream) {
if (top_k <= 0 || hidden <= 0) return 1;
const int nt = 256;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_moe_reduce<<<(hidden + nt - 1) / nt, nt, 0, s>>>(y_part, tk_w, out, top_k, hidden);
HIP_CHECK(hipGetLastError());
return 0;
}