[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
@@ -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