[Core] flat VRAM: SWA KV ring + fixed global KV + bounded scratch bucketing (reviewer p1+p2)
Punto 2: the 40 SWA pools now store a circular ring of swa_win+SWA_CHUNK_CAP(512)
=1536 positions (slot=pos%seq) instead of the whole 32768 context (~5GiB wasted);
the 8 global pools keep a linear max_context cache allocated ONCE. KV capacities
are fixed per layer kind => no drop+realloc when a session crosses a 4096 multiple.
kv_store{,_f16,_f16_batched} write pos%seq; flash decode/prefill/prefill_wmma read
p%seq (global seq>pos => identity). snapshot/restore map the window onto ring slots
with 1 or 2 pitched copies (wrap). serve clamps prefill chunk to SWA_CHUNK_CAP so
ring>=swa_win+chunk holds. proofs/prefix_cache.lean Part E (0 sorryAx): slot_inj,
window_slots_distinct, ring_no_clobber, global_slot_id. New GPU swa_ring_kv_parity
(R=1536, P=2000) vs an independent host LINEAR oracle: 1.48e-5.
Punto 1: BUF_POOL buckets pooled allocs in (4KiB,64MiB] to a power of two so variable
batch tails / future speculative B converge to a closed size set (no hipMalloc/Free
churn); larger allocs stay exact. Weight uploads (load_raw/load_bf16_as_f32/expert
matrix/arena) switched to alloc_unpooled: resident, exact, never pooled (rounding
them had added ~11GiB).
MEASURED gfx1150 FP16 KV chunk256: READY ~21.0GB; multi-turn 2048->13000->2048->5000
vram flat ~23.1-23.7GB (only bounded cap-8 checkpoint turnover, no KV resize);
prefill 2048 ~12.2s, 13000 ~78.6s (~6ms/tok). Suite GREEN hip37 ld23 gw4, CPU23, Lean6.
This commit is contained in:
1 parent
a4752953fb
commit
ca3a6b0e15
6 files changed
+210
-55
No files matched your search
@@ -297,11 +297,19 @@ impl LayerState {
|
||||
let es = if f16_kv() { 2 } else { 4 };
|
||||
let win = if swa { seq_len.min(d.swa_win.max(0) as usize) } else { seq_len };
|
||||
let kv_start = seq_len - win;
|
||||
// Ring-aware source slots: the window [kv_start, seq_len) maps to
|
||||
// [kv_start % r, ..) in the SWA ring (r = self.max_seq); it wraps at most
|
||||
// once because win <= swa_win < r. Global pools have kv_start = 0, no wrap.
|
||||
let r = self.max_seq;
|
||||
let s0 = kv_start % r;
|
||||
let first = win.min(r - s0);
|
||||
let row = win * d.hd * es;
|
||||
let src_off = kv_start * d.hd * es;
|
||||
let snap_kv = |src: &DeviceBuf| -> Result<DeviceBuf, String> {
|
||||
let b = DeviceBuf::alloc_unpooled(d.n_kv * row)?;
|
||||
b.copy2d_at(0, src, src_off, row, self.max_seq * d.hd * es, row, d.n_kv)?;
|
||||
b.copy2d_at(0, src, s0 * d.hd * es, row, r * d.hd * es, first * d.hd * es, d.n_kv)?;
|
||||
if win > first {
|
||||
b.copy2d_at(first * d.hd * es, src, 0, row, r * d.hd * es, (win - first) * d.hd * es, d.n_kv)?;
|
||||
}
|
||||
Ok(b)
|
||||
};
|
||||
let k_cache = self.k_cache.iter().map(snap_kv).collect::<Result<Vec<_>, _>>()?;
|
||||
@@ -326,13 +334,21 @@ impl LayerState {
|
||||
pub fn restore(&mut self, d: &Dims, snap: &LayerSnapshot) -> Result<(), String> {
|
||||
let es = if f16_kv() { 2 } else { 4 };
|
||||
let win = snap.seq_len - snap.kv_start;
|
||||
let r = self.max_seq;
|
||||
let s0 = snap.kv_start % r;
|
||||
let first = win.min(r - s0);
|
||||
let row = win * d.hd * es;
|
||||
let dst_off = snap.kv_start * d.hd * es;
|
||||
for (dst, src) in self.k_cache.iter().zip(&snap.k_cache) {
|
||||
dst.copy2d_at(dst_off, src, 0, self.max_seq * d.hd * es, row, row, d.n_kv)?;
|
||||
dst.copy2d_at(s0 * d.hd * es, src, 0, r * d.hd * es, row, first * d.hd * es, d.n_kv)?;
|
||||
if win > first {
|
||||
dst.copy2d_at(0, src, first * d.hd * es, r * d.hd * es, row, (win - first) * d.hd * es, d.n_kv)?;
|
||||
}
|
||||
}
|
||||
for (dst, src) in self.v_cache.iter().zip(&snap.v_cache) {
|
||||
dst.copy2d_at(dst_off, src, 0, self.max_seq * d.hd * es, row, row, d.n_kv)?;
|
||||
dst.copy2d_at(s0 * d.hd * es, src, 0, r * d.hd * es, row, first * d.hd * es, d.n_kv)?;
|
||||
if win > first {
|
||||
dst.copy2d_at(0, src, first * d.hd * es, r * d.hd * es, row, (win - first) * d.hd * es, d.n_kv)?;
|
||||
}
|
||||
}
|
||||
for i in 0..3 {
|
||||
self.hist_k[i].copy_from(&snap.hist_k[i], snap.hist_k[i].bytes)?;
|
||||
@@ -693,7 +709,7 @@ pub fn forward_swa_layers(
|
||||
let mut blocks: Vec<DeviceBuf> = Vec::new();
|
||||
for &ip in schedule {
|
||||
let w = cache.swa_layer(ip)?;
|
||||
let mut st = new_state(d, max_seq, 1)?;
|
||||
let mut st = new_state(d, swa_ring(d), 1)?;
|
||||
layer_forward_swa(cache, &w, &mut st, d, &cur, &mut blocks, pos, 0, None)?;
|
||||
}
|
||||
Ok((cur, blocks))
|
||||
@@ -702,6 +718,15 @@ pub fn forward_swa_layers(
|
||||
/// Full-attention physical layers (the rest are sliding-window).
|
||||
pub fn is_swa(ip: usize) -> bool { !matches!(ip, 5 | 11 | 17 | 23) }
|
||||
|
||||
/// Max batched-prefill chunk the SWA ring is sized for. The ring invariant
|
||||
/// (proofs/prefix_cache.lean Part E `ring_no_clobber`) requires
|
||||
/// `ring >= swa_win + chunk`; serve clamps the chunk to this cap.
|
||||
pub const SWA_CHUNK_CAP: usize = 512;
|
||||
/// Circular KV capacity of a SWA pool: `swa_win + SWA_CHUNK_CAP` positions instead
|
||||
/// of the whole context (the causal window never reads older rows). Global pools
|
||||
/// keep a linear `max_context` cache. Sized once, never resized across requests.
|
||||
pub fn swa_ring(d: &Dims) -> usize { d.swa_win.max(0) as usize + SWA_CHUNK_CAP }
|
||||
|
||||
/// Build the stacked cross-pass msg_queries [4, hidden] for a core layer ip:
|
||||
/// [q_cp[2i], kv_cp[2i], kv_cp[2i+1], q_cp[2i+1]] with i = ip - core_start.
|
||||
fn load_msg_queries(cache: &WeightCache, ip: usize, hidden: usize) -> Result<DeviceBuf, String> {
|
||||
@@ -853,7 +878,7 @@ fn run_layer(
|
||||
if states[ip].is_none() {
|
||||
// Core layers run 3 loop passes (3 distinct KV pools); prelude/coda run 1.
|
||||
let n_passes = if (6..18).contains(&ip) { 3 } else { 1 };
|
||||
states[ip] = Some(new_state(d, max_seq, n_passes)?);
|
||||
states[ip] = Some(new_state(d, if is_swa(ip) { swa_ring(d) } else { max_seq }, n_passes)?);
|
||||
}
|
||||
let st = states[ip].as_mut().unwrap();
|
||||
let r = if is_swa(ip) {
|
||||
@@ -976,7 +1001,7 @@ fn run_layer_batched(
|
||||
) -> Result<(), String> {
|
||||
if states[ip].is_none() {
|
||||
let n_passes = if (6..18).contains(&ip) { 3 } else { 1 };
|
||||
states[ip] = Some(new_state(d, max_seq, n_passes)?);
|
||||
states[ip] = Some(new_state(d, if is_swa(ip) { swa_ring(d) } else { max_seq }, n_passes)?);
|
||||
}
|
||||
let st = states[ip].as_mut().unwrap();
|
||||
if is_swa(ip) {
|
||||
|
||||
Reference in new issue
Block a user