diff --git a/crates/deploy/src/reconfigure.rs b/crates/deploy/src/reconfigure.rs index 65ea1ec6..1214df5f 100644 --- a/crates/deploy/src/reconfigure.rs +++ b/crates/deploy/src/reconfigure.rs @@ -908,13 +908,13 @@ mod tests { #[test] fn the_resident_archive_is_not_charged_twice_to_the_host() { - // 121 GiB host, 100 GiB available *with* the 55 GB memfd already resident: fits the dedicated preset + // 121 GiB host, 66 GiB available *with* the 55 GB memfd already resident (what the supervisor sees): fits the dedicated preset let config = server_config(); assert!( plan( &config, &request(Some(20), None, json!({})), - &host(32, 121, 40, vec![gpu(32607, 32100)]), + &host(32, 121, 66, vec![gpu(32607, 32100)]), true ) .is_ok() diff --git a/crates/deploy/src/resources.rs b/crates/deploy/src/resources.rs index cdfef18e..844cd87d 100644 --- a/crates/deploy/src/resources.rs +++ b/crates/deploy/src/resources.rs @@ -28,6 +28,14 @@ const MAX_USEFUL_WORKERS: u32 = 15; const WORKSTATION_VRAM_MARGIN_MIB: u64 = 2048; /// Resident host memory of a memfd-backed engine besides the archive (tokenizer, pinned buffers, CUDA host state). const ENGINE_EXTRA_BYTES: u64 = 3 * GIB; +/// Host memory of an engine with the resident expert source: the page-locked RAM copy of the routed experts plus rings, staging pools and pins +/// (53.54 GiB, the fixed footprint the engine's own budget reported in the live restart check plat-reconf-live; while it runs the 121 GiB server has only +/// 12-13 GiB available besides the 51.5 GiB sealed archive). It is what makes a higher free-RAM target unreachable on that host. +const ENGINE_FIXED_RESIDENT_BYTES: u64 = 54 * GIB; +/// The memory governor keeps this much beyond its soft floor (`memory.reserve_mib`, 2 GiB in the server profile). +const DEFAULT_RESERVE_BYTES: u64 = 2 * GIB; +/// Page-locked share of the routed experts in the mapped form never needs to exceed the bank itself. +const RESIDENT_BANK_GIB: u64 = 46; /// Host memory the mapped form keeps for the engine itself, the tokenizer and the working set of the page cache. const MAPPED_OVERHEAD_BYTES: u64 = 8 * GIB; /// What the mapped form still needs available to start. @@ -171,14 +179,16 @@ pub fn compute(facts: &Facts, gpu: Option<&Gpu>, profile: &Profile, preset: Pres plan.ram_floors_mib = Some((soft, soft / 2)); } if profile.kar_source == KarSource::Memfd { - let footprint = ARCHIVE_BYTES + ENGINE_EXTRA_BYTES; + let engine = if profile.expert_source == ExpertSource::Resident { ENGINE_FIXED_RESIDENT_BYTES } else { ENGINE_EXTRA_BYTES }; + let footprint = ARCHIVE_BYTES + engine; let after = ram.available.saturating_sub(footprint); - if after >= target { + let reserve = profile.memory.reserve_mib.map_or(DEFAULT_RESERVE_BYTES, |mib| mib * MIB); + if after >= target + reserve { plan.ram_expected_free_after_bytes = Some(after); } else { // Not enough RAM for the pinned form: map the archive and the experts from disk instead of failing. - let resident = ram.available.saturating_sub(target + MAPPED_OVERHEAD_BYTES) / GIB; - let resident = resident.min(u64::from(u32::MAX)) as u32; + let resident = ram.available.saturating_sub(target + reserve + MAPPED_OVERHEAD_BYTES) / GIB; + let resident = resident.min(RESIDENT_BANK_GIB) as u32; plan.mapped_resident_gib = Some(resident); plan.ram_expected_free_after_bytes = Some(ram.available.saturating_sub(u64::from(resident) * GIB + MAPPED_OVERHEAD_BYTES)); plan.warnings.push(format!( @@ -349,12 +359,12 @@ mod tests { #[test] fn a_resident_sealed_archive_is_not_charged_to_the_footprint_twice() { - // the server with the 51.5 GiB memfd already resident: 121 GiB total, 9 GiB available (no other consumer left) - let resident = host(32, 121, 9); + // the server with the 51.5 GiB memfd already resident: 121 GiB total, 66 GiB available (what the supervisor sees before the engine starts) + let resident = host(32, 121, 66); let charged_twice = compute(&resident, Some(&gpu(32607, 32100)), &server(), Preset::Dedicated); assert!(charged_twice.mapped_resident_gib.is_some(), "without the correction the pinned form looks impossible"); let corrected = compute(&with_resident_archive(&resident), Some(&gpu(32607, 32100)), &server(), Preset::Dedicated); - assert_eq!(corrected.mapped_resident_gib, None, "the archive is already paid for: only the engine's 3 GiB remain"); + assert_eq!(corrected.mapped_resident_gib, None, "the archive is already paid for: the engine's 54 GiB fit next to the 4 GiB target and the 2 GiB reserve"); assert!(corrected.ram_expected_free_after_bytes.is_some()); // a host that really lacks the engine's own memory is still sent to the mapped form let tiny = host(32, 121, 2); @@ -371,9 +381,9 @@ mod tests { #[test] fn not_enough_ram_for_the_pinned_form_switches_to_mapped_files_instead_of_failing() { - // 121 GiB host with 70 GiB available: 58 GiB of pinned archive leaves 12 GiB, below the 16 GiB workstation target + // 121 GiB host with 70 GiB available: archive and resident experts (105.5 GiB) do not fit at all let plan = compute(&host(32, 121, 70), Some(&gpu(32607, 32100)), &server(), Preset::Workstation); - assert_eq!(plan.mapped_resident_gib, Some(46), "70 - 16 target - 8 overhead"); + assert_eq!(plan.mapped_resident_gib, Some(44), "70 - 16 target - 2 reserve - 8 overhead"); assert_eq!(plan.warnings.len(), 1, "{:?}", plan.warnings); assert!(plan.render(&host(32, 121, 70)).contains("served from disk")); let mut profile = server(); @@ -381,7 +391,7 @@ mod tests { apply(&plan, &mut profile, &mut env, false); assert_eq!(profile.kar_source, KarSource::File); assert_eq!(profile.expert_source, ExpertSource::Mapped); - assert_eq!(profile.memory.resident_budget_gib, Some(46)); + assert_eq!(profile.memory.resident_budget_gib, Some(44)); assert_eq!(profile.memory.lock.as_deref(), Some("off")); assert!(profile.min_ram_available_bytes <= 10 * GIB && profile.min_ram_total_bytes <= 16 * GIB); profile.validate().unwrap(); @@ -391,7 +401,7 @@ mod tests { fn a_small_host_gets_zero_resident_experts_and_dedicated_switches_too() { let plan = compute(&host(16, 24, 12), Some(&gpu(16384, 15500)), &server(), Preset::Dedicated); assert_eq!(plan.mapped_resident_gib, Some(0), "12 GiB available is below the 4 GiB target plus 8 GiB overhead"); - let ample = compute(&host(32, 121, 100), Some(&gpu(32607, 32100)), &server(), Preset::Workstation); + let ample = compute(&host(64, 256, 240), Some(&gpu(32607, 32100)), &server(), Preset::Workstation); assert_eq!(ample.mapped_resident_gib, None); assert!(ample.warnings.is_empty(), "{:?}", ample.warnings); } diff --git a/docs/deploy.md b/docs/deploy.md index f0f5a5d0..8b700545 100644 --- a/docs/deploy.md +++ b/docs/deploy.md @@ -129,7 +129,7 @@ binary built without `sm86` on such a card. All PTX is embedded in the binaries | host RAM kept free | the profile's governor floors (server: soft 4 GiB, hard 2 GiB) | soft floor 8 / 12 / 16 GiB (hosts under 32 / under 64 / from 64 GiB RAM), hard floor half of it; the profile's floors are only raised. The governor sheds the prefix cache first, then refuses new requests with 503 `memory_pressure` | | VRAM | the profile budget | `min(profile, free - 2 GiB)` rounded down to GiB; the expert tier is sized from what remains | -The native expert pool takes at most 15 workers. **Measured cost of the preset on the 5090** (job 9p0, tic-tac-toe replay, 16 requests, 2 repeats, fixed window): default (30 GiB, 15 workers) 139.8 tok/s; 8 workers alone 136.4 (-2.4 %); 8 workers and a 27 GiB budget 119.6 (-14.5 %). The VRAM taken from the expert tier is what costs (about 4 % per GiB), so the workstation margin is 2 GiB, not more, and the workers are capped at 15 rather than 8. If the workstation target cannot be met (less than half of the profile's VRAM budget would remain) the preflight fails with the numbers; a RAM shortfall does not fail: when the pinned form (sealed archive plus engine, about 58 GiB on the server) would leave less than the free target, `up` switches the profile to the mapped form of the laptop profile: the archive stays a read-only file and the experts are memory-mapped from SSD/HDD, with `available - target - 8 GiB` of experts page-locked (`resident_budget_gib`, 0 on a small host). It prints a warning, because decode is slower than with everything in RAM. The same fallback applies to `dedicated` when the host cannot hold the pinned form at all. `--gpu-budget-gib` and explicit profile fields win over the preset. +The native expert pool takes at most 15 workers. **Measured cost of the preset on the 5090** (job 9p0, tic-tac-toe replay, 16 requests, 2 repeats, fixed window): default (30 GiB, 15 workers) 139.8 tok/s; 8 workers alone 136.4 (-2.4 %); 8 workers and a 27 GiB budget 119.6 (-14.5 %). **Host RAM of the server profile (live check plat-reconf-live):** the 121 GiB server holds the 51.5 GiB sealed archive and an engine with a fixed footprint of 53.5 GiB (the page-locked copy of the routed experts and the pools), which leaves 12-13 GiB available while it runs. The `workstation` preset asks the governor for 16 GiB plus its 2 GiB reserve, so on that host it is not reachable in the resident form: the planner now says so (and `leleka up` falls back to the mapped form, `leleka reconfigure --preset workstation` refuses and points to `leleka up`). A host with 192 GiB or more has room for it. The VRAM taken from the expert tier is what costs (about 4 % per GiB), so the workstation margin is 2 GiB, not more, and the workers are capped at 15 rather than 8. If the workstation target cannot be met (less than half of the profile's VRAM budget would remain) the preflight fails with the numbers; a RAM shortfall does not fail: when the pinned form (sealed archive plus engine, about 58 GiB on the server) would leave less than the free target, `up` switches the profile to the mapped form of the laptop profile: the archive stays a read-only file and the experts are memory-mapped from SSD/HDD, with `available - target - 8 GiB` of experts page-locked (`resident_budget_gib`, 0 on a small host). It prints a warning, because decode is slower than with everything in RAM. The same fallback applies to `dedicated` when the host cannot hold the pinned form at all. `--gpu-budget-gib` and explicit profile fields win over the preset. ## 4c. What the runtime reports (`leleka status`)