[HIP] DSpark brick 4a: non-causal block attention + SwiGLU kernels + parity
- k_dspark_block_attn: each of B draft query slots attends to the committed ctx window (bf16 ring, slot=pos%cap) PLUS ALL B draft k/v (f32) — non-causal block, GQA n_rep=H/KV, two-pass softmax in dynamic shared. Exact reference port. - k_dspark_swiglu: silu(g)*u (drafter MLP activation). - Host wrappers + CPU-oracle parity: block_attn 5.3e-7 (wrapping ring cap=64, window 69..99), swiglu 9.0e-8. hip 41/41; CPU -D warnings green.
This commit is contained in:
1 parent
b6ed89b2d2
commit
1b10258e6e
2 files changed
+226
No files matched your search
@@ -47,6 +47,16 @@ mod ffi {
|
||||
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_dspark_block_attn(
|
||||
q: *const c_float, kd: *const c_float, vd: *const c_float,
|
||||
ck: *const u16, cv: *const u16, out: *mut c_float,
|
||||
b: c_int, h: c_int, kv: c_int, hd: c_int, c0: c_int, c_len: c_int,
|
||||
cap: c_int, scale: c_float, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_dspark_swiglu(
|
||||
g: *const c_float, u: *const c_float, out: *mut c_float, n: usize,
|
||||
stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_embed_bf16_gather(
|
||||
table: *const u16, tokens: *const i32, out: *mut c_float,
|
||||
b: c_int, d: c_int, vocab: c_int, stream: *mut c_void,
|
||||
@@ -447,6 +457,27 @@ pub fn dspark_rmsnorm_rope(x: &DeviceBuf, nw: &DeviceBuf, pos: &DeviceBuf, theta
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// DSpark non-causal block attention: B draft queries attend to the committed
|
||||
/// ctx window (bf16 ring `ck`/`cv`, slot=pos%cap) + all B draft k/v (f32).
|
||||
/// q [B,H,hd], kd/vd [B,KV,hd], out [B,H,hd]. c_len = committed window length.
|
||||
#[cfg(feature = "hip")]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn dspark_block_attn(q: &DeviceBuf, kd: &DeviceBuf, vd: &DeviceBuf, ck: &DeviceBuf, cv: &DeviceBuf, out: &DeviceBuf, b: i32, h: i32, kv: i32, hd: i32, c0: i32, c_len: i32, cap: i32, scale: f32) -> Result<(), String> {
|
||||
let rc = unsafe {
|
||||
ffi::vetta_dspark_block_attn(q.ptr as *const f32, kd.ptr as *const f32, vd.ptr as *const f32, ck.ptr as *const u16, cv.ptr as *const u16, out.ptr as *mut f32, b, h, kv, hd, c0, c_len, cap, scale, std::ptr::null_mut())
|
||||
};
|
||||
if rc != 0 { return Err("dspark_block_attn launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// DSpark SwiGLU: out = silu(g) * u (elementwise, n entries).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn dspark_swiglu(g: &DeviceBuf, u: &DeviceBuf, out: &DeviceBuf, n: usize) -> Result<(), String> {
|
||||
let rc = unsafe { ffi::vetta_dspark_swiglu(g.ptr as *const f32, u.ptr as *const f32, out.ptr as *mut f32, n, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("dspark_swiglu launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_embed_bf16_gather: `b` token rows from BF16 table `[vocab, d]` → f32.
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn embed_bf16_gather(table: &DeviceBuf, tokens: &DeviceBuf, out: &DeviceBuf, b: i32, d: i32, vocab: i32) -> Result<(), String> {
|
||||
@@ -1128,6 +1159,91 @@ mod gpu_tests {
|
||||
assert!(max_rel < 1e-4, "dspark_rmsnorm_rope parity failed: {max_rel}");
|
||||
}
|
||||
|
||||
/// DSpark non-causal block attention vs CPU oracle, with a WRAPPING ctx ring
|
||||
/// (cap=64, window pos 69..99) + all B draft keys/values.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn dspark_block_attn_parity() {
|
||||
let (b, h, kv, hd, cap) = (8usize, 8usize, 2usize, 128usize, 64usize);
|
||||
let (anchor_pos, win) = (100i32, 32i32);
|
||||
let c0 = (anchor_pos - win + 1).max(0);
|
||||
let c_len = (anchor_pos - c0) as usize;
|
||||
let scale = 1.0f32 / (hd as f32).sqrt();
|
||||
let n_rep = h / kv;
|
||||
let ck: Vec<u16> = (0..cap * kv * hd).map(|i| f32_to_bf16(((i % 29) as f32 - 14.0) * 0.03)).collect();
|
||||
let cv: Vec<u16> = (0..cap * kv * hd).map(|i| f32_to_bf16(((i % 17) as f32 - 8.0) * 0.05)).collect();
|
||||
let q: Vec<f32> = (0..b * h * hd).map(|i| ((i % 23) as f32 - 11.0) * 0.04).collect();
|
||||
let kd: Vec<f32> = (0..b * kv * hd).map(|i| ((i % 31) as f32 - 15.0) * 0.03).collect();
|
||||
let vd: Vec<f32> = (0..b * kv * hd).map(|i| ((i % 13) as f32 - 6.0) * 0.06).collect();
|
||||
let tot_k = c_len + b;
|
||||
let mut want = vec![0f32; b * h * hd];
|
||||
for qi in 0..b {
|
||||
for hh in 0..h {
|
||||
let kvh = hh / n_rep;
|
||||
let qv = &q[(qi * h + hh) * hd..(qi * h + hh + 1) * hd];
|
||||
let mut sc = vec![0f32; tot_k];
|
||||
let mut mx = f32::NEG_INFINITY;
|
||||
for t in 0..tot_k {
|
||||
let mut dot = 0f32;
|
||||
if t < c_len {
|
||||
let slot = ((c0 + t as i32) as usize) % cap;
|
||||
for j in 0..hd { dot += qv[j] * bf16_to_f32(ck[(slot * kv + kvh) * hd + j]); }
|
||||
} else {
|
||||
let di = t - c_len;
|
||||
for j in 0..hd { dot += qv[j] * kd[(di * kv + kvh) * hd + j]; }
|
||||
}
|
||||
dot *= scale; sc[t] = dot; mx = mx.max(dot);
|
||||
}
|
||||
let mut sum = 0f32;
|
||||
for t in 0..tot_k { sc[t] = (sc[t] - mx).exp(); sum += sc[t]; }
|
||||
let inv = 1.0f32 / sum;
|
||||
for d in 0..hd {
|
||||
let mut acc = 0f32;
|
||||
for t in 0..c_len {
|
||||
let slot = ((c0 + t as i32) as usize) % cap;
|
||||
acc += sc[t] * inv * bf16_to_f32(cv[(slot * kv + kvh) * hd + d]);
|
||||
}
|
||||
for di in 0..b { acc += sc[c_len + di] * inv * vd[(di * kv + kvh) * hd + d]; }
|
||||
want[(qi * h + hh) * hd + d] = acc;
|
||||
}
|
||||
}
|
||||
}
|
||||
let dq = DeviceBuf::alloc(q.len() * 4).unwrap(); dq.h2d(&q).unwrap();
|
||||
let dkd = DeviceBuf::alloc(kd.len() * 4).unwrap(); dkd.h2d(&kd).unwrap();
|
||||
let dvd = DeviceBuf::alloc(vd.len() * 4).unwrap(); dvd.h2d(&vd).unwrap();
|
||||
let dck = DeviceBuf::alloc(ck.len() * 2).unwrap(); dck.h2d_bytes(bytemuck_u16(&ck)).unwrap();
|
||||
let dcv = DeviceBuf::alloc(cv.len() * 2).unwrap(); dcv.h2d_bytes(bytemuck_u16(&cv)).unwrap();
|
||||
let dy = DeviceBuf::alloc(want.len() * 4).unwrap();
|
||||
dspark_block_attn(&dq, &dkd, &dvd, &dck, &dcv, &dy, b as i32, h as i32, kv as i32, hd as i32, c0, c_len as i32, cap as i32, scale).unwrap();
|
||||
let mut got = vec![0f32; want.len()];
|
||||
dy.d2h(&mut got).unwrap();
|
||||
let gscale = want.iter().fold(0f32, |a, v| a.max(v.abs())).max(1e-6);
|
||||
let mut max_rel = 0f32;
|
||||
for (g, w) in got.iter().zip(want.iter()) { max_rel = max_rel.max((g - w).abs() / gscale); }
|
||||
eprintln!("dspark_block_attn max_rel_err={max_rel:.3e}");
|
||||
assert!(max_rel < 1e-4, "dspark_block_attn parity failed: {max_rel}");
|
||||
}
|
||||
|
||||
/// DSpark SwiGLU vs CPU oracle (silu(g)*u).
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn dspark_swiglu_parity() {
|
||||
let n = 4096usize;
|
||||
let g: Vec<f32> = (0..n).map(|i| ((i % 53) as f32 - 26.0) * 0.2).collect();
|
||||
let u: Vec<f32> = (0..n).map(|i| ((i % 37) as f32 - 18.0) * 0.1).collect();
|
||||
let want: Vec<f32> = (0..n).map(|i| (g[i] / (1.0f32 + (-g[i]).exp())) * u[i]).collect();
|
||||
let dg = DeviceBuf::alloc(n * 4).unwrap(); dg.h2d(&g).unwrap();
|
||||
let du = DeviceBuf::alloc(n * 4).unwrap(); du.h2d(&u).unwrap();
|
||||
let dy = DeviceBuf::alloc(n * 4).unwrap();
|
||||
dspark_swiglu(&dg, &du, &dy, n).unwrap();
|
||||
let mut got = vec![0f32; n];
|
||||
dy.d2h(&mut got).unwrap();
|
||||
let mut max_rel = 0f32;
|
||||
for (a, b) in got.iter().zip(want.iter()) { max_rel = max_rel.max((a - b).abs() / b.abs().max(1e-3)); }
|
||||
eprintln!("dspark_swiglu max_rel_err={max_rel:.3e}");
|
||||
assert!(max_rel < 1e-5, "dspark_swiglu 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 bf16_to_f32(h: u16) -> f32 { f32::from_bits((h as u32) << 16) }
|
||||
|
||||
|
||||
@@ -252,6 +252,116 @@ extern "C" int vetta_dspark_rmsnorm_rope(float* x, const float* nw,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Non-causal DSpark block attention: each of the B draft query slots attends to
|
||||
// the committed ctx window [c0, c0+c_len) (bf16 ring, slot=pos%cap) PLUS ALL B
|
||||
// draft keys/values (f32). GQA n_rep=H/KV. One block per (qi,hh); dynamic shared
|
||||
// holds tot_k scores + hd reduction scratch. Exact port of the reference loop.
|
||||
__device__ __forceinline__ float bf16_to_f32(uint16_t h); // defined below
|
||||
__global__ void k_dspark_block_attn(
|
||||
const float* __restrict__ q, const float* __restrict__ kd,
|
||||
const float* __restrict__ vd, const uint16_t* __restrict__ ck,
|
||||
const uint16_t* __restrict__ cv, float* __restrict__ out,
|
||||
int B, int H, int KV, int hd, int c0, int c_len, int cap,
|
||||
float scale, int n_rep) {
|
||||
const int qi = blockIdx.x / H;
|
||||
const int hh = blockIdx.x % H;
|
||||
const int kvh = hh / n_rep;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const int tot_k = c_len + B;
|
||||
extern __shared__ float sm[];
|
||||
float* sc = sm;
|
||||
float* red = sm + tot_k;
|
||||
const float* qv = q + ((size_t)qi * H + hh) * hd;
|
||||
float lmax = -INFINITY;
|
||||
for (int t = tid; t < tot_k; t += nt) {
|
||||
float dot = 0.f;
|
||||
if (t < c_len) {
|
||||
const uint16_t* kp = ck + (((size_t)((c0 + t) % cap) * KV + kvh) * hd);
|
||||
for (int j = 0; j < hd; ++j) dot += qv[j] * bf16_to_f32(kp[j]);
|
||||
} else {
|
||||
const float* kp = kd + (((size_t)(t - c_len) * KV + kvh) * hd);
|
||||
for (int j = 0; j < hd; ++j) dot += qv[j] * kp[j];
|
||||
}
|
||||
dot *= scale;
|
||||
sc[t] = dot;
|
||||
lmax = fmaxf(lmax, dot);
|
||||
}
|
||||
red[tid] = lmax;
|
||||
__syncthreads();
|
||||
for (int s = nt / 2; s > 0; s >>= 1) {
|
||||
if (tid < s) red[tid] = fmaxf(red[tid], red[tid + s]);
|
||||
__syncthreads();
|
||||
}
|
||||
const float mx = red[0];
|
||||
__syncthreads();
|
||||
float lsum = 0.f;
|
||||
for (int t = tid; t < tot_k; t += nt) {
|
||||
float e = expf(sc[t] - mx);
|
||||
sc[t] = e;
|
||||
lsum += e;
|
||||
}
|
||||
red[tid] = lsum;
|
||||
__syncthreads();
|
||||
for (int s = nt / 2; s > 0; s >>= 1) {
|
||||
if (tid < s) red[tid] += red[tid + s];
|
||||
__syncthreads();
|
||||
}
|
||||
const float inv = 1.f / red[0];
|
||||
__syncthreads();
|
||||
float* ov = out + ((size_t)qi * H + hh) * hd;
|
||||
for (int d = tid; d < hd; d += nt) {
|
||||
float acc = 0.f;
|
||||
for (int t = 0; t < c_len; ++t) {
|
||||
const uint16_t* vp = cv + (((size_t)((c0 + t) % cap) * KV + kvh) * hd);
|
||||
acc += sc[t] * inv * bf16_to_f32(vp[d]);
|
||||
}
|
||||
for (int di = 0; di < B; ++di) {
|
||||
const float* vp = vd + (((size_t)di * KV + kvh) * hd);
|
||||
acc += sc[c_len + di] * inv * vp[d];
|
||||
}
|
||||
ov[d] = acc;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_dspark_block_attn(
|
||||
const float* q, const float* kd, const float* vd,
|
||||
const uint16_t* ck, const uint16_t* cv, float* out,
|
||||
int B, int H, int KV, int hd, int c0, int c_len, int cap,
|
||||
float scale, vetta_stream_t stream) {
|
||||
if (B <= 0 || H <= 0 || KV <= 0 || hd <= 0 || hd > 128 || H % KV != 0) return 1;
|
||||
const int n_rep = H / KV;
|
||||
const int tot_k = c_len + B;
|
||||
const size_t sh = (size_t)(tot_k + hd) * 4;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_dspark_block_attn<<<B * H, hd, sh, s>>>(q, kd, vd, ck, cv, out, B, H, KV,
|
||||
hd, c0, c_len, cap, scale, n_rep);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// SwiGLU: out = silu(g) * u = (g / (1 + exp(-g))) * u (drafter MLP activation).
|
||||
__global__ void k_dspark_swiglu(const float* __restrict__ g,
|
||||
const float* __restrict__ u,
|
||||
float* __restrict__ out, size_t n) {
|
||||
const size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i < n) {
|
||||
const float gv = g[i];
|
||||
out[i] = (gv / (1.f + expf(-gv))) * u[i];
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_dspark_swiglu(const float* g, const float* u, float* out,
|
||||
size_t n, vetta_stream_t stream) {
|
||||
if (n == 0) return 1;
|
||||
const int t = 256;
|
||||
const size_t b = (n + t - 1) / t;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_dspark_swiglu<<<b, t, 0, s>>>(g, u, out, n);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- BF16 helpers ----------------------------------------------------------
|
||||
|
||||
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
|
||||
|
||||
Reference in new issue
Block a user