[HIP] Batched causal flash SWA GQA M=B (FP16 KV): the hardest prefill attention kernel
k_flash_swa_gqa_prefill_f16: same two-pass softmax semantics as the decode f16 kernel extended to M tokens (grid n_head x M). Each query token m uses cur_pos=pos[m] (causal: attends keys <= pos[m]); KV cache [n_kv,seq,hd] must hold all B positions first. q/out/gate [M,n_head,hd], rel_bias [M,n_head,max_rel], sink [M,n_head], scores_scratch [M,n_head,n_win_max] (alloc_unpooled, varies per batch). SWA window + rel_bias + sink + sigmoid gate all preserved. Parity vs B sequential decode flash f16 calls: B=20 n_head=8 win=16 base=10 (windowed + unwindowed tokens), BIT-EXACT (0.0). Additive — decode default unchanged. Batched attention kernels now landed: ShortConv prefill_par + causal flash M=B + attn_res_mix (already M>1). Remaining: kv_store B positions + batched depth-blocks/controllers/bank + SiTU/MXFP8 batched, then forward_batch orchestration + serve wiring + EXACT parity + measure turn-1. Verified: hip GPU 25 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
1 parent
ec2ad3fdc5
commit
a7a1ab11b0
2 files changed
+183
No files matched your search
@@ -478,6 +478,112 @@ extern "C" int vetta_flash_swa_gqa_decode_f16(const float* q, const uint16_t* k,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_flash_swa_gqa_prefill_f16: batched causal flash SWA GQA (M=B) --------
|
||||
// Same two-pass softmax semantics as the decode f16 kernel, extended to M tokens:
|
||||
// grid (n_head, M); each query token m uses cur_pos=pos[m] (causal: attends keys
|
||||
// <= pos[m]), so the KV cache [n_kv,seq,hd] must already hold all B positions.
|
||||
// q/out/gate [M,n_head,hd]; rel_bias [M,n_head,max_rel]; sink [M,n_head];
|
||||
// scores_scratch [M,n_head,n_win_max] (n_win_max = max window over the batch).
|
||||
__global__ void k_flash_swa_gqa_prefill_f16(const float* __restrict__ q,
|
||||
const uint16_t* __restrict__ k,
|
||||
const uint16_t* __restrict__ v,
|
||||
float* __restrict__ out,
|
||||
int n_head, int n_kv, int seq, int hd,
|
||||
float scale, int swa_window,
|
||||
const int32_t* __restrict__ pos,
|
||||
const float* __restrict__ rel_bias, int max_rel,
|
||||
const float* __restrict__ sink_logit,
|
||||
const float* __restrict__ gate,
|
||||
float* __restrict__ scores_scratch,
|
||||
int n_win_max, int M) {
|
||||
const int h = blockIdx.x;
|
||||
const int m = blockIdx.y;
|
||||
if (h >= n_head || m >= M) return;
|
||||
const int kvh = h / (n_head / n_kv);
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const int cur_pos = pos[m];
|
||||
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
|
||||
const int n_win = cur_pos - k_start + 1;
|
||||
extern __shared__ float smem[];
|
||||
float* scores = scores_scratch + ((size_t)m * n_head + h) * n_win_max;
|
||||
float* red = smem;
|
||||
const float* qh = q + ((size_t)m * n_head + h) * hd;
|
||||
const uint16_t* kh = k + (size_t)kvh * seq * hd;
|
||||
const uint16_t* vh = v + (size_t)kvh * seq * hd;
|
||||
const float* rb = rel_bias ? (rel_bias + ((size_t)m * n_head + h) * max_rel) : nullptr;
|
||||
const float sink = sink_logit ? sink_logit[(size_t)m * n_head + h] : -INFINITY;
|
||||
|
||||
float lmax = sink;
|
||||
for (int p = tid; p < n_win; p += nt) {
|
||||
const int s = k_start + p;
|
||||
const uint16_t* ks = kh + (size_t)s * hd;
|
||||
float dot = 0.f;
|
||||
for (int d = 0; d < hd; ++d) dot += qh[d] * f16_to_f32(ks[d]);
|
||||
dot *= scale;
|
||||
const int dist = cur_pos - s;
|
||||
if (rb && dist < max_rel) dot += rb[dist];
|
||||
scores[p] = 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 p = tid; p < n_win; p += nt) {
|
||||
float e = __expf(scores[p] - mx);
|
||||
scores[p] = 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 total = red[0] + (sink_logit ? __expf(sink - mx) : 0.f);
|
||||
const float inv = 1.f / fmaxf(total, 1e-30f);
|
||||
__syncthreads();
|
||||
|
||||
float* oh = out + ((size_t)m * n_head + h) * hd;
|
||||
const float* gh = gate ? (gate + ((size_t)m * n_head + h) * hd) : nullptr;
|
||||
for (int d = tid; d < hd; d += nt) {
|
||||
float acc = 0.f;
|
||||
for (int p = 0; p < n_win; ++p) {
|
||||
const int s = k_start + p;
|
||||
acc += scores[p] * inv * f16_to_f32(vh[(size_t)s * hd + d]);
|
||||
}
|
||||
if (gh) acc *= 1.f / (1.f + __expf(-gh[d]));
|
||||
oh[d] = acc;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_flash_swa_gqa_prefill_f16(const float* q, const uint16_t* k, const uint16_t* v,
|
||||
float* out, int n_head, int n_kv, int seq, int hd,
|
||||
float scale, int swa_window, const int32_t* pos,
|
||||
const float* rel_bias, int max_rel,
|
||||
const float* sink_logit, const float* gate,
|
||||
float* scores_scratch, int n_win_max, int M,
|
||||
vetta_stream_t stream) {
|
||||
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd <= 0 || (n_head % n_kv) != 0 || M <= 0) return 1;
|
||||
if (n_win_max <= 0) return 1;
|
||||
const int nt = 256;
|
||||
const size_t shmem = (size_t)nt * sizeof(float);
|
||||
dim3 grid(n_head, M);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_flash_swa_gqa_prefill_f16<<<grid, nt, shmem, s>>>(q, k, v, out, n_head, n_kv, seq, hd,
|
||||
scale, swa_window, pos, rel_bias, max_rel,
|
||||
sink_logit, gate, scores_scratch, n_win_max, M);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_moe_router_topk (kernel #11): Inkling exact sigmoid router ----------
|
||||
// Semantics verified against strata k4_moe_router_topk:
|
||||
// p[e] = sigmoid(logits[m,e]) (NaN->0); score[e] = p[e] + bias[e] (NaN->-1e29);
|
||||
|
||||
Reference in new issue
Block a user