[Core] fix 2x expert VRAM: prewarm loads device-MoE arenas, not per-expert copies

prewarm eagerly loaded all per-expert ExpertMatrix tensors (host run_experts
path, ~16 GiB), but the production serve uses device-routed MoE which loads a
SECOND contiguous ExpertArena copy of the same expert bytes lazily on the first
forward. Measured: 25 GiB after prewarm climbing to 42 GiB during the first
prefill chunk (~0.6-0.9 GiB/layer = arena load), near the 48 GiB HIP aperture.

Fix: prewarm now loads the expert ARENAS (the layout device MoE actually reads);
the forward reuses the memoized arenas. VRAM 42 -> 20 GiB (-22), prefill 60 -> 56 s
(no lazy arena NVMe load mid-forward). Parity EXACT (serve GPU 4/4, ld 23, hip 33).
Also adds VETTA_PROF batched phase+VRAM profiling (BPROF) used to diagnose this.
This commit is contained in:
adm committed 2026-12-02 00:55:31 +01:00
1 parent 66a40631ee
commit 01d8a48d51
1 file changed
+27 -10
+27 -10
View File
@@ -164,12 +164,19 @@ impl WeightCache {
/// Eagerly load the WHOLE model to device: trunk (embed/norms/unembed/output /// Eagerly load the WHOLE model to device: trunk (embed/norms/unembed/output
/// query/cross-pass queries), every attention layer (SWA or full), the core /// query/cross-pass queries), every attention layer (SWA or full), the core
/// loop controllers, and all expert matrices. After this the cache is fully /// loop controllers, and all expert ARENAS (the contiguous device-MoE layout).
/// resident: VRAM is populated at boot and NO request pays NVMe reads or /// After this the cache is fully resident: VRAM is populated at boot and NO
/// uploads (the model is ~18 GiB, within the <=80 GiB full-UMA-residency /// request pays NVMe reads or uploads (the model is ~18 GiB, within the <=80
/// policy). This is the serve default; `VETTA_LAZY_LOAD=1` defers to first use /// GiB full-UMA-residency policy). This is the serve default; `VETTA_LAZY_LOAD=1`
/// (still resident across requests once loaded). Idempotent: each loader /// defers to first use (still resident across requests once loaded). Idempotent:
/// memoizes, so calling it twice uploads nothing new. /// each loader memoizes, so calling it twice uploads nothing new.
///
/// Loads the expert ARENAS (not the per-expert `ExpertMatrix` copies): the
/// production serve uses the device-routed MoE (`VETTA_DEVICE_MOE`), which
/// reads the arena. Prewarming the per-expert matrices too would hold a SECOND
/// full copy of the ~16 GiB expert weights (the arena loads lazily on the first
/// forward), nearly doubling VRAM. The legacy host `run_experts` path loads
/// per-expert matrices lazily on demand if ever selected.
pub fn prewarm(&self, d: &Dims, n_layers: usize) -> Result<(), String> { pub fn prewarm(&self, d: &Dims, n_layers: usize) -> Result<(), String> {
self.raw("model.llm.embed.weight")?; self.raw("model.llm.embed.weight")?;
self.bf16_f32("model.llm.embed_norm.weight")?; self.bf16_f32("model.llm.embed_norm.weight")?;
@@ -183,10 +190,8 @@ impl WeightCache {
if (6..18).contains(&ip) { self.controllers(ip)?; } if (6..18).contains(&ip) { self.controllers(ip)?; }
} }
for ip in 0..n_layers { for ip in 0..n_layers {
for e in 0..d.n_experts { self.expert_arena(ip, "w13")?;
self.expert(ip, e, "w13")?; self.expert_arena(ip, "w2")?;
self.expert(ip, e, "w2")?;
}
} }
Ok(()) Ok(())
} }
@@ -954,6 +959,10 @@ pub fn forward_batch(
if bu == 0 { return Err("forward_batch: empty tokens".into()); } if bu == 0 { return Err("forward_batch: empty tokens".into()); }
let b = bu as i32; let b = bu as i32;
let max_pos = base_pos + b - 1; let max_pos = base_pos + b - 1;
let prof = std::env::var("VETTA_PROF").is_ok();
let tp0 = std::time::Instant::now();
let gb = |x: usize| x as f64 / 1e9;
if prof { eprintln!("[BPROF] B={bu} base_pos={base_pos} vram_start={:.2}GB", gb(vetta_infer_hip::vram_used())); }
// Embed B tokens (gather + RMS-norm rows=B). // Embed B tokens (gather + RMS-norm rows=B).
let table = cache.raw("model.llm.embed.weight")?; let table = cache.raw("model.llm.embed.weight")?;
let tok_bytes: Vec<u8> = tokens.iter().flat_map(|t| t.to_le_bytes()).collect(); let tok_bytes: Vec<u8> = tokens.iter().flat_map(|t| t.to_le_bytes()).collect();
@@ -968,6 +977,7 @@ pub fn forward_batch(
let mut blocks = BatchBlocks::new(bu, h, 8)?; let mut blocks = BatchBlocks::new(bu, h, 8)?;
// Prelude. // Prelude.
for ip in 0..6 { run_layer_batched(cache, d, ip, &cur, &mut blocks, states, max_seq, &dpos, max_pos, 0, b, None)?; } for ip in 0..6 { run_layer_batched(cache, d, ip, &cur, &mut blocks, states, max_seq, &dpos, max_pos, 0, b, None)?; }
if prof { vetta_infer_hip::synchronize()?; eprintln!("[BPROF] prelude {:.1}ms vram={:.2}GB", tp0.elapsed().as_secs_f64()*1e3, gb(vetta_infer_hip::vram_used())); }
let n_prelude_blocks = blocks.count; let n_prelude_blocks = blocks.count;
let prelude = DeviceBuf::alloc(bu * h * 4)?; vetta_infer_hip::copy_f32(&cur, &prelude, bu * h)?; let prelude = DeviceBuf::alloc(bu * h * 4)?; vetta_infer_hip::copy_f32(&cur, &prelude, bu * h)?;
// Core: 3 passes with device-side bank rollback. // Core: 3 passes with device-side bank rollback.
@@ -998,7 +1008,9 @@ pub fn forward_batch(
} }
} }
// Coda. // Coda.
if prof { vetta_infer_hip::synchronize()?; eprintln!("[BPROF] core {:.1}ms vram={:.2}GB", tp0.elapsed().as_secs_f64()*1e3, gb(vetta_infer_hip::vram_used())); }
for ip in 18..24 { run_layer_batched(cache, d, ip, &cur, &mut blocks, states, max_seq, &dpos, max_pos, 0, b, None)?; } for ip in 18..24 { run_layer_batched(cache, d, ip, &cur, &mut blocks, states, max_seq, &dpos, max_pos, 0, b, None)?; }
if prof { vetta_infer_hip::synchronize()?; eprintln!("[BPROF] coda {:.1}ms vram={:.2}GB", tp0.elapsed().as_secs_f64()*1e3, gb(vetta_infer_hip::vram_used())); }
// Final depth mix + unembed the LAST token. // Final depth mix + unembed the LAST token.
let oq = cache.raw("model.llm.attn_res.output_query")?; let oq = cache.raw("model.llm.attn_res.output_query")?;
let mix = DeviceBuf::alloc(bu * h * 4)?; let mix = DeviceBuf::alloc(bu * h * 4)?;
@@ -1006,6 +1018,11 @@ pub fn forward_batch(
let last = DeviceBuf::alloc(h * 4)?; vetta_infer_hip::copy_f32_sub(&mix, (bu - 1) * h, &last, h)?; let last = DeviceBuf::alloc(h * 4)?; vetta_infer_hip::copy_f32_sub(&mix, (bu - 1) * h, &last, h)?;
let logits = unembed_logits(cache, &last, h, vocab, d.eps)?; let logits = unembed_logits(cache, &last, h, vocab, d.eps)?;
if cap > 0.0 { vetta_infer_hip::softcap(&logits, &logits, vocab, cap)?; } if cap > 0.0 { vetta_infer_hip::softcap(&logits, &logits, vocab, cap)?; }
if prof {
vetta_infer_hip::synchronize()?;
let exp = EXPERT_NS.with(|c| c.replace(0));
eprintln!("[BPROF] total {:.1}ms moe_launch={:.1}ms vram_end={:.2}GB", tp0.elapsed().as_secs_f64()*1e3, exp as f64/1e6, gb(vetta_infer_hip::vram_used()));
}
Ok(logits) Ok(logits)
} }