[HIP][Core] Zero-VRAM PrefixCache in pinned host RAM (PinnedBuf) + DSpark SWA ring

Close Punto 3 of the flat-VRAM plan:
- Add PinnedBuf (hipHostMallocDefault / hipHostFree with stream 0 sync on drop) and pipelined async 1D/2D D2H/H2D copy helpers in vetta_kernels.{h,hip} and vetta-infer-hip.
- Migrate LayerSnapshot (KV + ShortConv hist) and DsparkSnapshot (drafter KV + frnn_last) from VRAM DeviceBuf to host PinnedBuf (0 bytes VRAM per checkpoint).
- Size DSpark SWA layers 0/2 in DsparkContext to DS_SWA_RING=1280 (-258 MiB VRAM at 32k context).
- Add GPU regression tests: pinned_buf_zero_vram_and_roundtrip_2d (+0.00 MiB VRAM on 64 MiB alloc) and serve_gen_pinned_prefix_cache_vram_flat (4 turns up to 2100 tok, 8/8 checkpoints full, VETTA_SPEC=1: 20674->20672->20676->20676 MiB, +2.00 MiB delta).
- Update README.md, docs/ORIENTATION.md, and review/2026-12-02-fix-drafter-e-ottimizzazioni.md.
This commit is contained in:
adm committed 2026-12-02 23:23:07 +01:00
1 parent c1c5221642
commit 1772050087
9 files changed
+370 -88

No files matched your search

+55
View File
@@ -1098,4 +1098,59 @@ mod tests {
assert_eq!(on_t2, off_t2, "Turn 2 tokens after PrefixCache+DsparkSnapshot restore must match OFF exactly");
});
}
/// GPU: verify that saving multiple large PrefixCache checkpoints (trunk KV +
/// ShortConv + DsparkSnapshot across grid boundaries 512, 1024, 1536, 2048)
/// keeps GPU VRAM completely flat (0 bytes added per checkpoint in VRAM,
/// since all snapshots live in pinned host RAM via `PinnedBuf`).
#[test]
#[ignore]
fn serve_gen_pinned_prefix_cache_vram_flat() {
crate::gpu_isolated(|| {
unsafe { std::env::set_var("VETTA_SPEC", "1"); }
let eng = InklingEngine::new("/mnt/models/Inkling/safetensors").unwrap();
let stop = AtomicBool::new(false);
let mut prompt: Vec<i32> = Vec::with_capacity(2200);
let seed = [200002, 200004, 34182, 279, 154554, 150628, 25, 220, 15, 200010, 200000, 200004];
prompt.extend_from_slice(&seed);
let mut vram_samples: Vec<(usize, usize, usize)> = Vec::new();
for target_len in [600usize, 1100, 1600, 2100] {
while prompt.len() < target_len {
prompt.push(10970 + ((prompt.len() * 17) % 500) as i32);
}
let ids_str = prompt.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(",");
let g = match parse_serve_line(&format!("GEN 8 temperature=0 {ids_str}"), eng.vocab()).unwrap() {
ServeCommand::Gen(g) => g,
_ => panic!("expected Gen"),
};
let mut lines = Vec::new();
serve_gen(&eng, &g, &stop, &mut |l| lines.push(l));
let resumed: usize = lines.iter()
.find(|l| l.starts_with("RESUME "))
.and_then(|l| l[7..].trim().parse().ok())
.unwrap_or(0);
let ckpts = eng.prefix.borrow().pool.len();
let vram = vetta_infer_hip::vram_used();
eprintln!(
"pinned_ckpt_vram: prompt={} resumed={} ckpts={} vram={:.2} MiB",
target_len, resumed, ckpts, vram as f64 / (1024.0 * 1024.0)
);
vram_samples.push((target_len, resumed, vram));
}
unsafe { std::env::remove_var("VETTA_SPEC"); }
assert!(vram_samples[1].1 >= 512, "Turn 2 must hit >=512 checkpoint, got {}", vram_samples[1].1);
assert!(vram_samples[2].1 >= 1024, "Turn 3 must hit >=1024 checkpoint, got {}", vram_samples[2].1);
assert!(vram_samples[3].1 >= 1536, "Turn 4 must hit >=1536 checkpoint, got {}", vram_samples[3].1);
// Compare VRAM after Turn 1 (once KV + DsparkContext + BUF_POOL are warm)
// against Turn 4 (after filling 8 checkpoints up to 2100 tokens).
let v1 = vram_samples[0].2;
let v4 = vram_samples[3].2;
let delta_mb = (v4 as isize - v1 as isize) as f64 / (1024.0 * 1024.0);
eprintln!("pinned_ckpt_vram: Turn1->Turn4 delta = {delta_mb:+.2} MiB (expected ~0 MiB)");
assert!(
delta_mb.abs() < 32.0,
"PrefixCache in pinned RAM must keep VRAM flat across checkpoints, got {delta_mb:+.2} MiB"
);
});
}
}