[HIP][GW] Support large context: global flash scores + raise MAX_CONTEXT to 32768

The GUI (strata-ctl agent session) sends a ~2648-token prompt plus max_new=16384;
the old MAX_CONTEXT=8192 refused it ('prompt + max_new exceeds context window'),
so the chat returned 0 tokens. Two fixes:

- Flash attention stored per-head scores[n_win] in dynamic shared memory. SWA is
  window-capped (1024) but full-attn is global, so beyond ~16K positions it
  exceeded the 64 KB LDS limit. Scores now live in a global scratch buffer
  ([n_head, n_win], allocated by the Rust wrapper, pool-reused); only the [nt]
  reduction stays in shared. Math unchanged -> parity EXACT (16 tok) preserved.
- MAX_CONTEXT 8192 -> 32768, sized for the consumer's max_new budget plus a large
  agent prompt; per-request KV (prompt+max_new) fits the 48 GiB HIP VRAM.

Verified live: 3113-token prompt -> correct 'Parigi' (finish stop). NOTE: prefill
is still sequential (~140 ms/token), so a large agent prompt takes minutes to
first token — batched prefill / prefix cache / expert-GEMV speedup is the next
(perf) step. Small prompts are fast.

Suite: workspace -D warnings green; 18 ld + 18 hip + 5 gw GPU; 5 Lean 0 sorryAx.
This commit is contained in:
adm committed 2026-12-01 18:56:50 +01:00
1 parent 7790f48c27
commit 59c41cb613
4 files changed
+38 -20

No files matched your search

+14 -12
View File
@@ -273,7 +273,8 @@ __global__ void k_flash_swa_gqa_decode(const float* __restrict__ q,
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 float* __restrict__ gate,
float* __restrict__ scores_scratch) {
const int h = blockIdx.x;
const int kvh = h / (n_head / n_kv);
const int tid = threadIdx.x;
@@ -281,8 +282,8 @@ __global__ void k_flash_swa_gqa_decode(const float* __restrict__ q,
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]
float* scores = scores_scratch + (size_t)h * n_win; // [n_win] global (no LDS cap)
float* red = smem; // [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;
@@ -343,17 +344,17 @@ extern "C" int vetta_flash_swa_gqa_decode(const float* q, const float* k, const
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) {
float* scores_scratch, 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);
const size_t shmem = (size_t)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);
sink_logit, gate, scores_scratch);
HIP_CHECK(hipGetLastError());
return 0;
}
@@ -370,7 +371,8 @@ __global__ void k_flash_swa_gqa_decode_f16(const float* __restrict__ q,
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 float* __restrict__ gate,
float* __restrict__ scores_scratch) {
const int h = blockIdx.x;
const int kvh = h / (n_head / n_kv);
const int tid = threadIdx.x;
@@ -378,8 +380,8 @@ __global__ void k_flash_swa_gqa_decode_f16(const float* __restrict__ q,
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;
float* red = smem + n_win;
float* scores = scores_scratch + (size_t)h * n_win;
float* red = smem;
const float* qh = q + (size_t)h * hd;
const uint16_t* kh = k + (size_t)kvh * seq * hd;
const uint16_t* vh = v + (size_t)kvh * seq * hd;
@@ -440,17 +442,17 @@ extern "C" int vetta_flash_swa_gqa_decode_f16(const float* q, const uint16_t* k,
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) {
float* scores_scratch, 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);
const size_t shmem = (size_t)nt * sizeof(float);
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_flash_swa_gqa_decode_f16<<<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);
sink_logit, gate, scores_scratch);
HIP_CHECK(hipGetLastError());
return 0;
}