From 59c41cb613a56362ec89de0db246d1393642b6dd Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Tue, 1 Dec 2026 18:56:50 +0100 Subject: [PATCH] [HIP][GW] Support large context: global flash scores + raise MAX_CONTEXT to 32768 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- crates/vetta-infer-gw/src/serve.rs | 8 ++++++-- crates/vetta-infer-hip/src/lib.rs | 20 ++++++++++++++++---- kernels/hip/vetta_kernels.h | 4 ++-- kernels/hip/vetta_kernels.hip | 26 ++++++++++++++------------ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/crates/vetta-infer-gw/src/serve.rs b/crates/vetta-infer-gw/src/serve.rs index 6854f6e..1c5a3dc 100644 --- a/crates/vetta-infer-gw/src/serve.rs +++ b/crates/vetta-infer-gw/src/serve.rs @@ -43,8 +43,12 @@ const REL_CLIP: f32 = 8.0; const SITU_BETA: f32 = 4.0; const SITU_LB: f32 = 25.0; /// Declared resident context window advertised in `READY` (SWA window is 1024; -/// full-attn layers attend globally up to this cap). -const MAX_CONTEXT: usize = 8192; +/// full-attn layers attend globally up to this cap). Sized to accommodate the +/// consumer's default generation budget (strata-ctl max_new=16384) plus a large +/// agent prompt; KV is allocated per request at prompt+max_new, well within the +/// 48 GiB HIP-visible VRAM. Flash scores live in global memory, so full-attn is +/// not limited by the 64 KB LDS cap. +const MAX_CONTEXT: usize = 32768; /// A loaded Inkling model ready to run autoregressive forwards. pub struct InklingEngine { diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 726dc87..2dd0f67 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -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 { + 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(()) diff --git a/kernels/hip/vetta_kernels.h b/kernels/hip/vetta_kernels.h index ee45be5..b9741ca 100644 --- a/kernels/hip/vetta_kernels.h +++ b/kernels/hip/vetta_kernels.h @@ -62,7 +62,7 @@ int vetta_flash_swa_gqa_decode(const float* q, const float* k, const float* v, 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); // k_flash_swa_gqa_decode_f16: FP16 KV cache variant (strata default FP16 KV). int vetta_flash_swa_gqa_decode_f16(const float* q, const uint16_t* k, const uint16_t* v, @@ -70,7 +70,7 @@ int vetta_flash_swa_gqa_decode_f16(const float* q, const uint16_t* k, const uint 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); // k_moe_router_topk: Inkling exact sigmoid router (top-K, route_scale norm). int vetta_moe_router_topk(const float* logits, const float* bias, diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index ffd1abd..f5f2064 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -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<<>>(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<<>>(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; }