[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

+16 -4
View File
@@ -52,13 +52,13 @@ mod ffi {
q: *const c_float, k: *const c_float, v: *const c_float, out: *mut c_float,
n_head: c_int, n_kv: c_int, seq: c_int, hd: c_int, scale: c_float,
swa_window: c_int, cur_pos: c_int, rel_bias: *const c_float, max_rel: c_int,
sink_logit: *const c_float, gate: *const c_float, stream: *mut c_void,
sink_logit: *const c_float, gate: *const c_float, scores_scratch: *mut c_float, stream: *mut c_void,
) -> c_int;
pub fn vetta_flash_swa_gqa_decode_f16(
q: *const c_float, k: *const u16, v: *const u16, out: *mut c_float,
n_head: c_int, n_kv: c_int, seq: c_int, hd: c_int, scale: c_float,
swa_window: c_int, cur_pos: c_int, rel_bias: *const c_float, max_rel: c_int,
sink_logit: *const c_float, gate: *const c_float, stream: *mut c_void,
sink_logit: *const c_float, gate: *const c_float, scores_scratch: *mut c_float, stream: *mut c_void,
) -> c_int;
pub fn vetta_moe_router_topk(
logits: *const c_float, bias: *const c_float, topk_ids: *mut i32, topk_weights: *mut c_float,
@@ -273,6 +273,16 @@ pub fn flash_gqa_decode(q: &DeviceBuf, k: &DeviceBuf, v: &DeviceBuf, out: &Devic
Ok(())
}
/// Global scratch for the flash-attention per-head scores `[n_head, n_win]`.
/// Scores live in global memory (not LDS) so the full-attn window is not capped
/// by the 64 KB shared-memory limit; only the `[nt]` reduction stays in shared.
#[cfg(feature = "hip")]
fn flash_scores_scratch(n_head: i32, swa_window: i32, cur_pos: i32) -> Result<DeviceBuf, String> {
let k_start = if swa_window > 0 && cur_pos >= swa_window { cur_pos - swa_window + 1 } else { 0 };
let n_win = (cur_pos - k_start + 1).max(1) as usize;
DeviceBuf::alloc(n_head as usize * n_win * 4)
}
/// Launch k_flash_swa_gqa_decode: Inkling SWA window + relpos + sink + gate.
/// `rel_bias`, `sink_logit`, `gate` are optional (None -> NULL).
#[cfg(feature = "hip")]
@@ -281,8 +291,9 @@ pub fn flash_swa_gqa_decode(q: &DeviceBuf, k: &DeviceBuf, v: &DeviceBuf, out: &D
let rb = rel_bias.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let sk = sink_logit.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let gt = gate.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let scratch = flash_scores_scratch(n_head, swa_window, cur_pos)?;
let rc = unsafe {
ffi::vetta_flash_swa_gqa_decode(q.ptr as *const f32, k.ptr as *const f32, v.ptr as *const f32, out.ptr as *mut f32, n_head, n_kv, seq, hd, scale, swa_window, cur_pos, rb, max_rel, sk, gt, std::ptr::null_mut())
ffi::vetta_flash_swa_gqa_decode(q.ptr as *const f32, k.ptr as *const f32, v.ptr as *const f32, out.ptr as *mut f32, n_head, n_kv, seq, hd, scale, swa_window, cur_pos, rb, max_rel, sk, gt, scratch.ptr as *mut f32, std::ptr::null_mut())
};
if rc != 0 { return Err("flash_swa_gqa launch failed".into()); }
Ok(())
@@ -296,8 +307,9 @@ pub fn flash_swa_gqa_decode_f16(q: &DeviceBuf, k: &DeviceBuf, v: &DeviceBuf, out
let rb = rel_bias.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let sk = sink_logit.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let gt = gate.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let scratch = flash_scores_scratch(n_head, swa_window, cur_pos)?;
let rc = unsafe {
ffi::vetta_flash_swa_gqa_decode_f16(q.ptr as *const f32, k.ptr as *const u16, v.ptr as *const u16, out.ptr as *mut f32, n_head, n_kv, seq, hd, scale, swa_window, cur_pos, rb, max_rel, sk, gt, std::ptr::null_mut())
ffi::vetta_flash_swa_gqa_decode_f16(q.ptr as *const f32, k.ptr as *const u16, v.ptr as *const u16, out.ptr as *mut f32, n_head, n_kv, seq, hd, scale, swa_window, cur_pos, rb, max_rel, sk, gt, scratch.ptr as *mut f32, std::ptr::null_mut())
};
if rc != 0 { return Err("flash_swa_gqa_f16 launch failed".into()); }
Ok(())