[LD][GW] Resident WeightCache + eager full-model boot load (eliminate per-request NVMe re-read)

Bug: serve_gen called WeightCache::new(&eng.ar) PER REQUEST, creating empty
memoization maps each turn -> every request re-read ~15-23 GiB of weights from
NVMe and re-uploaded to device (the 'resident' comment was contradicted by the
per-request instantiation). Federico spotted the symptom: model only loaded at
the first message, unlike the reference which loads at boot.

Fix:
- WeightCache now owns Arc<SafetensorsArchive> (was &'a borrow) so ONE cache lives
  resident in InklingEngine across all requests; loaded once, never re-read.
- WeightCache::prewarm(d, n_layers) eagerly loads the whole model (trunk + every
  SWA/full layer + core controllers + all expert matrices). run_serve calls it at
  boot by DEFAULT (model ~23 GiB <= 80 GiB full-UMA-residency policy); READY is
  emitted only after residency. VETTA_LAZY_LOAD=1 defers to first use (still
  resident across requests once loaded).
- Arc deref coercion keeps all CPU-oracle test reads (&ar) unchanged.

Verified (gfx1150, Inkling-2-Mini, FP16 KV): eager VRAM 23.60 GiB at READY before
any message, boot 3.6s, STABLE across 3 requests (no reload/growth), clean release
0.34 GiB on QUIT; lazy option 0.34 GiB at boot. Parity EXACT preserved: CPU 91 +
ld GPU 18 + gw GPU 5 green.
This commit is contained in:
adm committed 2026-12-01 21:46:42 +01:00
1 parent 20e152640e
commit 58940840d6
3 files changed
+78 -43

No files matched your search

+14 -10
View File
@@ -139,7 +139,7 @@ impl PrefixCache {
/// A loaded Inkling model ready to run autoregressive forwards.
pub struct InklingEngine {
ar: SafetensorsArchive,
cache: WeightCache,
d: Dims,
vocab: usize,
n_layers: usize,
@@ -155,7 +155,7 @@ impl InklingEngine {
// half the VRAM of the F32 CPU-oracle baseline). The ld parity tests keep
// the F32 default; the resident serve opts in here.
vetta_infer_ld::layer::set_kv_f16(true);
let ar = SafetensorsArchive::open(dir)?;
let ar = Arc::new(SafetensorsArchive::open(dir)?);
let cfg_path = ar.dir.join("config.json");
let raw = std::fs::read_to_string(&cfg_path)
.map_err(|e| format!("read {}: {e}", cfg_path.display()))?;
@@ -181,7 +181,7 @@ impl InklingEngine {
swa_win: t.swa_win as i32,
};
Ok(Self {
ar,
cache: WeightCache::new(ar),
d,
// The served/logit vocab is the UNPADDED size (exact strata
// k_unembed_softcap V = vocab_unpadded): rows >= unpadded are padding
@@ -206,13 +206,12 @@ impl InklingEngine {
fn forward(
&self,
cache: &WeightCache,
token: i32,
pos: i32,
max_seq: usize,
states: &mut [Option<LayerState>],
) -> Result<DeviceBuf, String> {
forward_inkling_with_state(cache, &self.d, token, self.vocab, max_seq, pos, self.cap, states)
forward_inkling_with_state(&self.cache, &self.d, token, self.vocab, max_seq, pos, self.cap, states)
}
}
@@ -277,9 +276,8 @@ pub fn serve_gen(
eprintln!("[prefix] prompt_len={n_prompt} restored={start}");
}
// Resident weight cache for this request: weights upload once and stay on
// the GPU across prefill + decode (no per-token NVMe expert re-reads).
let cache = WeightCache::new(&eng.ar);
// Weights are resident in eng.cache (loaded once at boot, reused across all
// requests): no per-request NVMe re-read or re-upload.
// --- Prefill: run the forward over the NEW prompt tokens, growing KV/history. ---
let t0 = Instant::now();
@@ -295,7 +293,7 @@ pub fn serve_gen(
stop.store(false, Ordering::Relaxed);
return;
}
match eng.forward(&cache, tok, pos, max_seq, &mut states) {
match eng.forward(tok, pos, max_seq, &mut states) {
Ok(lg) => logits = Some(lg),
Err(e) => {
emit_all(emit, sess.on_error(&e));
@@ -400,7 +398,7 @@ pub fn serve_gen(
finish = "length";
break;
}
match eng.forward(&cache, rep.token, pos, max_seq, &mut states) {
match eng.forward(rep.token, pos, max_seq, &mut states) {
Ok(l) => logits = l,
Err(e) => {
emit_all(emit, sess.on_error(&e));
@@ -439,6 +437,12 @@ enum Msg {
/// drains to DONE). One request at a time (FIFO), matching the consumer.
pub fn run_serve(dir: &str) -> Result<(), String> {
let eng = InklingEngine::new(dir)?;
// Default: eager full-model residency at boot — VRAM is populated immediately
// (like the reference) and no request pays NVMe reads/uploads. The cache is
// resident across requests either way. VETTA_LAZY_LOAD=1 defers to first use.
if std::env::var("VETTA_LAZY_LOAD").is_err() {
eng.cache.prewarm(&eng.d, eng.n_layers)?;
}
let vocab = eng.vocab();
let stop = Arc::new(AtomicBool::new(false));
let quit = Arc::new(AtomicBool::new(false));