[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
@@ -147,6 +147,111 @@ extern "C" int vetta_rms_norm_f32(const float* x, const float* w, float* y,
|
||||
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 ----------------------------------------------------------
|
||||
|
||||
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
|
||||
|
||||
Reference in new issue
Block a user