[HIP] flash SWA GQA decode kernel — Inkling attention parity (window+relpos+sink+gate)

- k_flash_swa_gqa_decode / vetta_flash_swa_gqa_decode: exact strata
  k3_flash_swa_gqa semantics on a contiguous f32 KV cache:
  * SWA window: k_start = (window>0 && cur_pos>=window) ? cur_pos-window+1 : 0;
  * relpos bias rel_bias[h,dist] added to the score (dist=cur_pos-s, <max_rel);
  * attention sink_logit[h]: virtual sink token (value 0) contributing
    exp(sink-m) to the softmax denominator only;
  * sigmoid gate_attn[h,d] applied per output dim.
  Two-pass softmax over the windowed range, f32 throughout. rel_bias /
  sink_logit / gate are optional (NULL disables each).
- Parity test vs a CPU oracle across 4 configs (window on/off x relpos x sink
  x gate): max_rel_err 9.4e-7 .. 3.6e-6. The Inkling-specific attention math
  (local window, relative position, sink, gate) is verified.

With ShortConv4, this covers the defining Inkling attention mechanisms; the
AttnRes block-mix deep residual is the remaining parity piece.

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (4 files, 42 theorems) green; GPU 12/12 (--features hip).
This commit is contained in:
adm committed 2026-12-01 14:28:54 +01:00
1 parent e060862660
commit aea1c437d4
3 files changed
+201

No files matched your search

+9
View File
@@ -50,6 +50,15 @@ int vetta_flash_gqa_decode(const float* q, const float* k, const float* v,
float* out, int n_head, int n_kv, int seq,
int hd, float scale, vetta_stream_t stream);
// k_flash_swa_gqa_decode: Inkling flash SWA GQA — SWA window + relpos bias +
// attention sink + sigmoid gate. rel_bias/sink_logit/gate may be NULL.
int vetta_flash_swa_gqa_decode(const float* q, const float* k, const float* v,
float* out, int n_head, int n_kv, int seq, int hd,
float scale, int swa_window, int cur_pos,
const float* rel_bias, int max_rel,
const float* sink_logit, const float* gate,
vetta_stream_t stream);
// k_moe_router_topk: Inkling exact sigmoid router (top-K, route_scale norm).
int vetta_moe_router_topk(const float* logits, const float* bias,
int32_t* topk_ids, float* topk_weights,
+102
View File
@@ -243,6 +243,108 @@ extern "C" int vetta_flash_gqa_decode(const float* q, const float* k, const floa
return 0;
}
// ---- k_flash_swa_gqa_decode: Inkling flash SWA GQA (parity) ----------------
// Exact strata k3_flash_swa_gqa semantics on a contiguous f32 KV cache:
// SWA window: k_start = (window>0 && cur_pos>=window) ? cur_pos-window+1 : 0;
// rel_bias[h, dist] added to score (dist = cur_pos - s, if dist < max_rel);
// sink_logit[h]: virtual sink token (logit=sink, value=0) -> adds exp(sink-m)
// to the softmax denominator only;
// gate_attn[h, d]: output *= sigmoid(gate) per dim.
// Two-pass softmax over the windowed range, f32 throughout.
__global__ void k_flash_swa_gqa_decode(const float* __restrict__ q,
const float* __restrict__ k,
const float* __restrict__ v,
float* __restrict__ out,
int n_head, int n_kv, int seq, int hd,
float scale, int swa_window, int cur_pos,
const float* __restrict__ rel_bias, int max_rel,
const float* __restrict__ sink_logit,
const float* __restrict__ gate) {
const int h = blockIdx.x;
const int kvh = h / (n_head / n_kv);
const int tid = threadIdx.x;
const int nt = blockDim.x;
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 = smem; // [n_win]
float* red = smem + n_win; // [nt]
const float* qh = q + (size_t)h * hd;
const float* kh = k + (size_t)kvh * seq * hd;
const float* vh = v + (size_t)kvh * seq * hd;
const float* rb = rel_bias ? (rel_bias + (size_t)h * max_rel) : nullptr;
const float sink = sink_logit ? sink_logit[h] : -INFINITY;
float lmax = sink;
for (int p = tid; p < n_win; p += nt) {
const int s = k_start + p;
const float* ks = kh + (size_t)s * hd;
float dot = 0.f;
for (int d = 0; d < hd; ++d) dot += qh[d] * 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 m = red[0];
__syncthreads();
float lsum = 0.f;
for (int p = tid; p < n_win; p += nt) {
float e = __expf(scores[p] - m);
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 - m) : 0.f);
const float inv = 1.f / fmaxf(total, 1e-30f);
__syncthreads();
float* oh = out + (size_t)h * hd;
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 * vh[(size_t)s * hd + d];
}
if (gate) acc *= 1.f / (1.f + __expf(-gate[(size_t)h * hd + d]));
oh[d] = acc;
}
}
extern "C" int vetta_flash_swa_gqa_decode(const float* q, const float* k, const float* v,
float* out, int n_head, int n_kv, int seq, int hd,
float scale, int swa_window, int cur_pos,
const float* rel_bias, int max_rel,
const float* sink_logit, const float* gate,
vetta_stream_t stream) {
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd <= 0 || (n_head % n_kv) != 0) return 1;
if (cur_pos < 0 || cur_pos >= seq) return 1;
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;
const int nt = 256;
const size_t shmem = ((size_t)n_win + nt) * sizeof(float);
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_flash_swa_gqa_decode<<<n_head, nt, shmem, s>>>(q, k, v, out, n_head, n_kv, seq, hd,
scale, swa_window, cur_pos, rel_bias, max_rel,
sink_logit, gate);
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);