[HIP] DSpark brick 3a: mix_taps + per-head RMSNorm/NeoX-RoPE kernels + parity
Exact port of strata k_dspark_mix_taps (softmax(fuse_w[row]) weighted sum of 4 target taps + RMSNorm) and k_dspark_rmsnorm_rope (per-head RMSNorm + NeoX half-split RoPE pairs (d,d+hd/2)). f32 activations; bf16 store is the caller's. RoPE freq/trig computed in f64 internally (f32 angle) for tight parity at large pos; the downstream bf16 ctx store makes the extra precision moot. Host wrappers dspark_mix_taps / dspark_rmsnorm_rope + CPU-oracle parity tests (both < 1e-4). hip 39/39; CPU -D warnings green; speculative_draft.lean 0 err.
This commit is contained in:
1 parent
3a9533afd0
commit
7e74db7ba5
2 files changed
+224
No files matched your search
@@ -38,6 +38,15 @@ mod ffi {
|
|||||||
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,
|
||||||
) -> c_int;
|
) -> c_int;
|
||||||
|
pub fn vetta_dspark_mix_taps(
|
||||||
|
t0: *const c_float, t1: *const c_float, t2: *const c_float, t3: *const c_float,
|
||||||
|
fuse_w: *const c_float, row: c_int, norm_w: *const c_float, out: *mut c_float,
|
||||||
|
m: c_int, d: c_int, eps: c_float, stream: *mut c_void,
|
||||||
|
) -> c_int;
|
||||||
|
pub fn vetta_dspark_rmsnorm_rope(
|
||||||
|
x: *mut c_float, nw: *const c_float, pos: *const c_int, theta: c_float,
|
||||||
|
m: c_int, h: c_int, hd: c_int, eps: c_float, stream: *mut c_void,
|
||||||
|
) -> c_int;
|
||||||
pub fn vetta_embed_bf16_gather(
|
pub fn vetta_embed_bf16_gather(
|
||||||
table: *const u16, tokens: *const i32, out: *mut c_float,
|
table: *const u16, tokens: *const i32, out: *mut c_float,
|
||||||
b: c_int, d: c_int, vocab: c_int, stream: *mut c_void,
|
b: c_int, d: c_int, vocab: c_int, stream: *mut c_void,
|
||||||
@@ -411,6 +420,33 @@ pub fn rms_norm_f32(x: &DeviceBuf, w: Option<&DeviceBuf>, y: &DeviceBuf, rows: i
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// DSpark: mix 4 tap buffers [M,D] with softmax(fuse_w[row]) + RMSNorm(norm_w).
|
||||||
|
#[cfg(feature = "hip")]
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn dspark_mix_taps(taps: [&DeviceBuf; 4], fuse_w: &DeviceBuf, row: i32, norm_w: &DeviceBuf, out: &DeviceBuf, m: i32, d: i32, eps: f32) -> Result<(), String> {
|
||||||
|
let rc = unsafe {
|
||||||
|
ffi::vetta_dspark_mix_taps(
|
||||||
|
taps[0].ptr as *const f32, taps[1].ptr as *const f32,
|
||||||
|
taps[2].ptr as *const f32, taps[3].ptr as *const f32,
|
||||||
|
fuse_w.ptr as *const f32, row, norm_w.ptr as *const f32,
|
||||||
|
out.ptr as *mut f32, m, d, eps, std::ptr::null_mut())
|
||||||
|
};
|
||||||
|
if rc != 0 { return Err("dspark_mix_taps launch failed".into()); }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// DSpark: in-place per-head RMSNorm(nw) + NeoX half-split RoPE(theta) on x
|
||||||
|
/// [M,H,hd]; pos[M] gives each row's absolute position.
|
||||||
|
#[cfg(feature = "hip")]
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn dspark_rmsnorm_rope(x: &DeviceBuf, nw: &DeviceBuf, pos: &DeviceBuf, theta: f32, m: i32, h: i32, hd: i32, eps: f32) -> Result<(), String> {
|
||||||
|
let rc = unsafe {
|
||||||
|
ffi::vetta_dspark_rmsnorm_rope(x.ptr as *mut f32, nw.ptr as *const f32, pos.ptr as *const i32, theta, m, h, hd, eps, std::ptr::null_mut())
|
||||||
|
};
|
||||||
|
if rc != 0 { return Err("dspark_rmsnorm_rope launch failed".into()); }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Launch k_embed_bf16_gather: `b` token rows from BF16 table `[vocab, d]` → f32.
|
/// Launch k_embed_bf16_gather: `b` token rows from BF16 table `[vocab, d]` → f32.
|
||||||
#[cfg(feature = "hip")]
|
#[cfg(feature = "hip")]
|
||||||
pub fn embed_bf16_gather(table: &DeviceBuf, tokens: &DeviceBuf, out: &DeviceBuf, b: i32, d: i32, vocab: i32) -> Result<(), String> {
|
pub fn embed_bf16_gather(table: &DeviceBuf, tokens: &DeviceBuf, out: &DeviceBuf, b: i32, d: i32, vocab: i32) -> Result<(), String> {
|
||||||
@@ -1009,6 +1045,89 @@ mod gpu_tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// DSpark mix_taps: softmax(fuse_w[row]) weighted sum of 4 taps + RMSNorm.
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn dspark_mix_taps_parity() {
|
||||||
|
let (m, d) = (3usize, 2304usize);
|
||||||
|
let eps = 1e-6f32;
|
||||||
|
let row = 1i32;
|
||||||
|
let taps: Vec<Vec<f32>> = (0..4)
|
||||||
|
.map(|t| (0..m * d).map(|i| ((i % 41) as f32 - 20.0) * 0.05 + t as f32 * 0.1).collect())
|
||||||
|
.collect();
|
||||||
|
let fuse: Vec<f32> = (0..16).map(|i| ((i % 7) as f32 - 3.0) * 0.3).collect();
|
||||||
|
let nw: Vec<f32> = (0..d).map(|i| 0.5 + (i % 13) as f32 * 0.05).collect();
|
||||||
|
// oracle
|
||||||
|
let fw = &fuse[row as usize * 4..row as usize * 4 + 4];
|
||||||
|
let mx = fw.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
|
||||||
|
let e: Vec<f32> = fw.iter().map(|v| (v - mx).exp()).collect();
|
||||||
|
let es: f32 = e.iter().sum();
|
||||||
|
let sw: Vec<f32> = e.iter().map(|v| v / es).collect();
|
||||||
|
let mut want = vec![0f32; m * d];
|
||||||
|
for mi in 0..m {
|
||||||
|
let mut ss = 0f32;
|
||||||
|
for i in 0..d {
|
||||||
|
let mut v = 0f32;
|
||||||
|
for t in 0..4 { v += sw[t] * taps[t][mi * d + i]; }
|
||||||
|
want[mi * d + i] = v;
|
||||||
|
ss += v * v;
|
||||||
|
}
|
||||||
|
let inv = 1.0f32 / (ss / d as f32 + eps).sqrt();
|
||||||
|
for i in 0..d { want[mi * d + i] *= inv * nw[i]; }
|
||||||
|
}
|
||||||
|
let dt: Vec<DeviceBuf> = taps.iter().map(|t| { let b = DeviceBuf::alloc(t.len() * 4).unwrap(); b.h2d(t).unwrap(); b }).collect();
|
||||||
|
let df = DeviceBuf::alloc(fuse.len() * 4).unwrap(); df.h2d(&fuse).unwrap();
|
||||||
|
let dn = DeviceBuf::alloc(nw.len() * 4).unwrap(); dn.h2d(&nw).unwrap();
|
||||||
|
let dy = DeviceBuf::alloc(want.len() * 4).unwrap();
|
||||||
|
dspark_mix_taps([&dt[0], &dt[1], &dt[2], &dt[3]], &df, row, &dn, &dy, m as i32, d as i32, eps).unwrap();
|
||||||
|
let mut got = vec![0f32; want.len()];
|
||||||
|
dy.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!("dspark_mix_taps max_rel_err={max_rel:.3e}");
|
||||||
|
assert!(max_rel < 1e-4, "dspark_mix_taps parity failed: {max_rel}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// DSpark per-head RMSNorm + NeoX half-split RoPE (theta=1e6), in-place.
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn dspark_rmsnorm_rope_parity() {
|
||||||
|
let (m, h, hd) = (2usize, 8usize, 128usize);
|
||||||
|
let eps = 1e-6f32;
|
||||||
|
let theta = 1e6f32;
|
||||||
|
let pos: Vec<i32> = vec![5, 1024];
|
||||||
|
let nw: Vec<f32> = (0..hd).map(|i| 0.5 + (i % 11) as f32 * 0.06).collect();
|
||||||
|
let x: Vec<f32> = (0..m * h * hd).map(|i| ((i % 37) as f32 - 18.0) * 0.08).collect();
|
||||||
|
let mut want = x.clone();
|
||||||
|
for mi in 0..m {
|
||||||
|
for hi in 0..h {
|
||||||
|
let base = (mi * h + hi) * hd;
|
||||||
|
let ss: f32 = (0..hd).map(|i| x[base + i] * x[base + i]).sum();
|
||||||
|
let inv = 1.0f32 / (ss / hd as f32 + eps).sqrt();
|
||||||
|
let t: Vec<f32> = (0..hd).map(|i| x[base + i] * inv * nw[i]).collect();
|
||||||
|
for i in 0..hd / 2 {
|
||||||
|
let freq = (1.0f64 / (theta as f64).powf((2 * i) as f64 / hd as f64)) as f32;
|
||||||
|
let ang = pos[mi] as f32 * freq;
|
||||||
|
let s = (ang as f64).sin() as f32;
|
||||||
|
let c = (ang as f64).cos() as f32;
|
||||||
|
want[base + i] = t[i] * c - t[i + hd / 2] * s;
|
||||||
|
want[base + i + hd / 2] = t[i] * s + t[i + hd / 2] * c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let dx = DeviceBuf::alloc(x.len() * 4).unwrap(); dx.h2d(&x).unwrap();
|
||||||
|
let dn = DeviceBuf::alloc(nw.len() * 4).unwrap(); dn.h2d(&nw).unwrap();
|
||||||
|
let dp = DeviceBuf::alloc(pos.len() * 4).unwrap();
|
||||||
|
dp.h2d_bytes(&pos.iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>()).unwrap();
|
||||||
|
dspark_rmsnorm_rope(&dx, &dn, &dp, theta, m as i32, h as i32, hd as i32, eps).unwrap();
|
||||||
|
let mut got = vec![0f32; x.len()];
|
||||||
|
dx.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!("dspark_rmsnorm_rope max_rel_err={max_rel:.3e}");
|
||||||
|
assert!(max_rel < 1e-4, "dspark_rmsnorm_rope parity failed: {max_rel}");
|
||||||
|
}
|
||||||
|
|
||||||
fn f32_to_bf16(v: f32) -> u16 { let u = v.to_bits(); ((u + 0x7FFFu32 + ((u >> 16) & 1)) >> 16) as u16 }
|
fn f32_to_bf16(v: f32) -> u16 { let u = v.to_bits(); ((u + 0x7FFFu32 + ((u >> 16) & 1)) >> 16) as u16 }
|
||||||
fn bf16_to_f32(h: u16) -> f32 { f32::from_bits((h as u32) << 16) }
|
fn bf16_to_f32(h: u16) -> f32 { f32::from_bits((h as u32) << 16) }
|
||||||
|
|
||||||
|
|||||||
@@ -147,6 +147,111 @@ extern "C" int vetta_rms_norm_f32(const float* x, const float* w, float* y,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- DSpark drafter: mix 4 taps + RMSNorm, and per-head RMSNorm + NeoX RoPE --
|
||||||
|
// Exact port of strata k_dspark_mix_taps / k_dspark_rmsnorm_rope (f32 throughout;
|
||||||
|
// the bf16 store happens in the caller). NeoX half-split pairs (d, d+hd/2).
|
||||||
|
|
||||||
|
__global__ void k_dspark_mix_taps(const float* __restrict__ tap0,
|
||||||
|
const float* __restrict__ tap1,
|
||||||
|
const float* __restrict__ tap2,
|
||||||
|
const float* __restrict__ tap3,
|
||||||
|
const float* __restrict__ fuse_w, int row,
|
||||||
|
const float* __restrict__ norm_w,
|
||||||
|
float* __restrict__ out, int M, int D,
|
||||||
|
float eps) {
|
||||||
|
const int m = blockIdx.x;
|
||||||
|
if (m >= M) return;
|
||||||
|
const int tid = threadIdx.x;
|
||||||
|
const int nt = blockDim.x;
|
||||||
|
__shared__ float sw[4];
|
||||||
|
__shared__ float red[256];
|
||||||
|
if (tid == 0) {
|
||||||
|
const float* fw = fuse_w + row * 4;
|
||||||
|
float mx = fmaxf(fmaxf(fw[0], fw[1]), fmaxf(fw[2], fw[3]));
|
||||||
|
float e0 = expf(fw[0] - mx), e1 = expf(fw[1] - mx);
|
||||||
|
float e2 = expf(fw[2] - mx), e3 = expf(fw[3] - mx);
|
||||||
|
float inv = 1.f / (e0 + e1 + e2 + e3);
|
||||||
|
sw[0] = e0 * inv; sw[1] = e1 * inv; sw[2] = e2 * inv; sw[3] = e3 * inv;
|
||||||
|
}
|
||||||
|
__syncthreads();
|
||||||
|
const size_t off = (size_t)m * D;
|
||||||
|
float* dst = out + off;
|
||||||
|
float ss = 0.f;
|
||||||
|
for (int d = tid; d < D; d += nt) {
|
||||||
|
float v = sw[0] * tap0[off + d] + sw[1] * tap1[off + d]
|
||||||
|
+ sw[2] * tap2[off + d] + sw[3] * tap3[off + d];
|
||||||
|
dst[d] = v;
|
||||||
|
ss += v * v;
|
||||||
|
}
|
||||||
|
red[tid] = ss;
|
||||||
|
__syncthreads();
|
||||||
|
for (int s = nt / 2; s > 0; s >>= 1) {
|
||||||
|
if (tid < s) red[tid] += red[tid + s];
|
||||||
|
__syncthreads();
|
||||||
|
}
|
||||||
|
const float inv = rsqrtf(red[0] / (float)D + eps);
|
||||||
|
for (int d = tid; d < D; d += nt) dst[d] = dst[d] * inv * norm_w[d];
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int vetta_dspark_mix_taps(const float* t0, const float* t1,
|
||||||
|
const float* t2, const float* t3,
|
||||||
|
const float* fuse_w, int row,
|
||||||
|
const float* norm_w, float* out,
|
||||||
|
int M, int D, float eps,
|
||||||
|
vetta_stream_t stream) {
|
||||||
|
if (M <= 0 || D <= 0) return 1;
|
||||||
|
int threads = D < 256 ? ((D + 31) / 32) * 32 : 256;
|
||||||
|
if (threads < 32) threads = 32;
|
||||||
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
|
k_dspark_mix_taps<<<M, threads, 0, s>>>(t0, t1, t2, t3, fuse_w, row, norm_w,
|
||||||
|
out, M, D, eps);
|
||||||
|
HIP_CHECK(hipGetLastError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
__global__ void k_dspark_rmsnorm_rope(float* __restrict__ x,
|
||||||
|
const float* __restrict__ nw,
|
||||||
|
const int* __restrict__ pos, float theta,
|
||||||
|
int M, int H, int hd, float eps) {
|
||||||
|
const int m = blockIdx.y;
|
||||||
|
const int h = blockIdx.x;
|
||||||
|
const int d = threadIdx.x;
|
||||||
|
if (m >= M || h >= H || d >= hd) return;
|
||||||
|
__shared__ float s_x[128];
|
||||||
|
__shared__ float red[128];
|
||||||
|
float* xp = x + ((size_t)m * H + h) * hd;
|
||||||
|
float v = xp[d];
|
||||||
|
red[d] = v * v;
|
||||||
|
__syncthreads();
|
||||||
|
for (int s = hd / 2; s > 0; s >>= 1) {
|
||||||
|
if (d < s) red[d] += red[d + s];
|
||||||
|
__syncthreads();
|
||||||
|
}
|
||||||
|
const float inv = rsqrtf(red[0] / (float)hd + eps);
|
||||||
|
s_x[d] = v * inv * nw[d];
|
||||||
|
__syncthreads();
|
||||||
|
if (d < hd / 2) {
|
||||||
|
const float freq = (float)(1.0 / pow((double)theta, (double)(2 * d) / (double)hd));
|
||||||
|
const float ang = (float)pos[m] * freq;
|
||||||
|
const float sv = (float)sin((double)ang), cv = (float)cos((double)ang);
|
||||||
|
const float x0 = s_x[d], x1 = s_x[d + hd / 2];
|
||||||
|
xp[d] = x0 * cv - x1 * sv;
|
||||||
|
xp[d + hd / 2] = x0 * sv + x1 * cv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int vetta_dspark_rmsnorm_rope(float* x, const float* nw,
|
||||||
|
const int* pos, float theta,
|
||||||
|
int M, int H, int hd, float eps,
|
||||||
|
vetta_stream_t stream) {
|
||||||
|
if (M <= 0 || H <= 0 || hd <= 0 || hd > 128) return 1;
|
||||||
|
dim3 grid(H, M);
|
||||||
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
|
k_dspark_rmsnorm_rope<<<grid, hd, 0, s>>>(x, nw, pos, theta, M, H, hd, eps);
|
||||||
|
HIP_CHECK(hipGetLastError());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// ---- BF16 helpers ----------------------------------------------------------
|
// ---- BF16 helpers ----------------------------------------------------------
|
||||||
|
|
||||||
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
|
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
|
||||||
|
|||||||
Reference in new issue
Block a user