[HIP] k2 attention-prep kernels: L2 clip, relpos bias projection, KV store
- k_l2_clip / vetta_l2_clip: exact strata Q/R/K per-head L2 clip — v = x*scale (scale nullable), fac = min(1, clip/(sqrt(sum v^2)+1e-6)), optional tau fac *= 1 + 0.1*log(max(1,(pos+1)/8192)) (kLogScaleAlpha=0.1, floor=8192); x = v*fac, in place. Config: qk_clip=22.627, rel_clip=8.0; tau=!is_swa. - k_relpos_bias / vetta_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc] * rel_proj[rc,dist] for dist < lim=min(pos+1,rel_extent). R pre-clipped [n_head,d_rel=16], rel_proj [d_rel,rel_extent=1024] (rel_logits_proj.proj). - k_kv_store / vetta_kv_store: K (pre-clipped) stored as-is, V RMS-normalized (rsqrt(mean v^2 + eps)), into a contiguous f32 KV cache [n_kv,seq,hd] at pos. (Paged FP16/FP8 KV pool is a later perf refinement; contiguous f32 first.) - Combined parity test vs CPU oracles: l2_clip (scale+tau and plain), relpos bias, kv_store (K exact, V <1e-5). These complete the k2 attention prep; with gemm_bf16 (projections) + shortconv4 + flash_swa_gqa the full attention block can now be composed. Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; lean proofs (4 files, 42 theorems) green; GPU 14/14 (--features hip).
This commit is contained in:
1 parent
a5ee804641
commit
b5d6d7cee3
3 files changed
+220
No files matched your search
@@ -99,6 +99,20 @@ int vetta_attn_res_mix(const float* blocks, const float* cur, const float* queri
|
||||
float* out, int n_blocks, int M, int n_queries, int D,
|
||||
float eps, vetta_stream_t stream);
|
||||
|
||||
// k_l2_clip: per-head L2 clip + optional per-dim scale + optional tau (in place).
|
||||
int vetta_l2_clip(float* x, const float* scale, int rows, int hd, float clip,
|
||||
int apply_tau, int pos, vetta_stream_t stream);
|
||||
|
||||
// k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist], dist<lim.
|
||||
int vetta_relpos_bias(const float* r, const float* rel_proj, float* rel_bias,
|
||||
int n_head, int d_rel, int rel_extent, int lim,
|
||||
vetta_stream_t stream);
|
||||
|
||||
// k_kv_store: K (pre-clipped) as-is + V RMS-normalized -> contiguous f32 cache.
|
||||
int vetta_kv_store(const float* k_in, const float* v_in, float* k_cache,
|
||||
float* v_cache, int pos, int n_kv, int hd, int seq,
|
||||
float rms_eps, vetta_stream_t stream);
|
||||
|
||||
// Core <15 microkernel entry points (implemented progressively in M1 & M2):
|
||||
// 1. k_embed_bf16
|
||||
// 2. k_rms_norm
|
||||
|
||||
@@ -687,4 +687,111 @@ extern "C" int vetta_attn_res_mix(const float* blocks, const float* cur, const f
|
||||
k_attn_res_mix<<<grid, 256, 0, s>>>(blocks, cur, queries, out, n_blocks, M, n_queries, D, eps);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_l2_clip: per-head L2 clip + optional per-dim scale + optional tau ----
|
||||
// Exact strata k2 Q/R/K clip: v = x * scale (scale nullable); ss = sum(v^2);
|
||||
// fac = min(1, clip/(sqrt(ss)+1e-6)); if apply_tau: fac *= 1 + 0.1*log(max(1,
|
||||
// (pos+1)/8192)); x = v*fac. One block per row (head), in place.
|
||||
|
||||
__global__ void k_l2_clip(float* __restrict__ x, const float* __restrict__ scale,
|
||||
int rows, int hd, float clip, int apply_tau, int pos) {
|
||||
const int r = blockIdx.x;
|
||||
if (r >= rows) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
float* xv = x + (size_t)r * hd;
|
||||
const float* sc = scale ? (scale + (size_t)r * hd) : nullptr;
|
||||
__shared__ float red[256];
|
||||
__shared__ float fac_s;
|
||||
const float v = (tid < hd) ? (xv[tid] * (sc ? sc[tid] : 1.f)) : 0.f;
|
||||
red[tid] = v * v;
|
||||
__syncthreads();
|
||||
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
|
||||
if (tid == 0) {
|
||||
float fac = fminf(1.f, clip / (sqrtf(red[0]) + 1e-6f));
|
||||
if (apply_tau) {
|
||||
const float rr = (float)(pos + 1) / 8192.f;
|
||||
fac *= 1.f + 0.1f * logf(fmaxf(1.f, rr));
|
||||
}
|
||||
fac_s = fac;
|
||||
}
|
||||
__syncthreads();
|
||||
if (tid < hd) xv[tid] = v * fac_s;
|
||||
}
|
||||
|
||||
extern "C" int vetta_l2_clip(float* x, const float* scale, int rows, int hd,
|
||||
float clip, int apply_tau, int pos, vetta_stream_t stream) {
|
||||
if (rows <= 0 || hd <= 0 || hd > 256) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_l2_clip<<<rows, 256, 0, s>>>(x, scale, rows, hd, clip, apply_tau, pos);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist] ----
|
||||
// R already L2-clipped [n_head,d_rel]; rel_proj [d_rel,rel_extent]; output
|
||||
// rel_bias [n_head,rel_extent] for dist < lim = min(pos+1, rel_extent).
|
||||
|
||||
__global__ void k_relpos_bias(const float* __restrict__ r, const float* __restrict__ rel_proj,
|
||||
float* __restrict__ rel_bias, int n_head, int d_rel,
|
||||
int rel_extent, int lim) {
|
||||
const int h = blockIdx.x;
|
||||
if (h >= n_head) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const float* rh = r + (size_t)h * d_rel;
|
||||
float* dst = rel_bias + (size_t)h * rel_extent;
|
||||
for (int dist = tid; dist < lim; dist += nt) {
|
||||
float acc = 0.f;
|
||||
for (int rc = 0; rc < d_rel; ++rc) acc += rh[rc] * rel_proj[(size_t)rc * rel_extent + dist];
|
||||
dst[dist] = acc;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_relpos_bias(const float* r, const float* rel_proj, float* rel_bias,
|
||||
int n_head, int d_rel, int rel_extent, int lim,
|
||||
vetta_stream_t stream) {
|
||||
if (n_head <= 0 || d_rel <= 0 || rel_extent <= 0 || lim < 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_relpos_bias<<<n_head, 256, 0, s>>>(r, rel_proj, rel_bias, n_head, d_rel, rel_extent, lim);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_kv_store: K (pre-clipped) stored as-is; V RMS-normalized; contiguous
|
||||
// f32 cache [n_kv, seq, hd] at position pos. Exact strata K-clip/V-rmsnorm.
|
||||
|
||||
__global__ void k_kv_store(const float* __restrict__ k_in, const float* __restrict__ v_in,
|
||||
float* __restrict__ k_cache, float* __restrict__ v_cache,
|
||||
int pos, int n_kv, int hd, int seq, float rms_eps) {
|
||||
const int h = blockIdx.x;
|
||||
if (h >= n_kv) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const float* kv = k_in + (size_t)h * hd;
|
||||
const float* vv = v_in + (size_t)h * hd;
|
||||
__shared__ float red[256];
|
||||
__shared__ float fac_s;
|
||||
const float v = (tid < hd) ? vv[tid] : 0.f;
|
||||
red[tid] = v * v;
|
||||
__syncthreads();
|
||||
for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); }
|
||||
if (tid == 0) fac_s = rsqrtf(red[0] / (float)hd + rms_eps);
|
||||
__syncthreads();
|
||||
if (tid < hd) {
|
||||
const size_t off = (size_t)h * seq * hd + (size_t)pos * hd + tid;
|
||||
k_cache[off] = kv[tid];
|
||||
v_cache[off] = v * fac_s;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_kv_store(const float* k_in, const float* v_in, float* k_cache,
|
||||
float* v_cache, int pos, int n_kv, int hd, int seq,
|
||||
float rms_eps, vetta_stream_t stream) {
|
||||
if (n_kv <= 0 || hd <= 0 || hd > 256 || seq <= 0 || pos < 0 || pos >= seq) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_kv_store<<<n_kv, 256, 0, s>>>(k_in, v_in, k_cache, v_cache, pos, n_kv, hd, seq, rms_eps);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user