Files
leleka/crates/deploy/src/resources/sizing.rs
T

364 lines
15 KiB
Rust

//! Sizing the resource plan from the host facts and applying a preset to a profile.
use super::*;
/// The largest prompt chunk an expert cache of this GPU budget can lend to the prompt path (measured on the 3080 laptop
/// with the pinned Qwen3.8 IQ3_S pack: a budget of 13 GiB holds 2,234 expert slots of 1.97 MiB, about 520 more per GiB;
/// the loan of a chunk plus the 128-slot floor is 1,282 slots at 2048, 1,497 at 3072, 1,713 at 4096, 2,144 at 6144 and
/// about 2,575 at 8192; a chunk that does not fit stops the engine from opening). Whole GiB, rounded down, with the
/// margin the 1-GiB steps leave: 14+ GiB carry 8192, 13 carry 6144, 12 carry 3072, less keeps 2048.
pub fn prefill_capacity_for_budget(gpu_budget_bytes: u64) -> u32 {
match gpu_budget_bytes / GIB {
14.. => 8192,
13 => 6144,
12 => 3072,
_ => 2048,
}
}
/// RAM the workstation preset keeps free: 8 GiB on small hosts up to 16 GiB from 64 GiB of RAM.
pub fn workstation_ram_target(total: u64) -> u64 {
if total >= 64 * GIB {
16 * GIB
} else if total >= 32 * GIB {
12 * GIB
} else {
8 * GIB
}
}
/// The host as the planner should see it when the sealed archive memfd is already resident (an inherited `--kar-fd`, or
/// the descriptor a running supervisor holds): its pages are already missing from `MemAvailable`, and `compute` charges
/// the archive to the footprint of the pinned form, so without this the archive is paid for twice and a host that fits
/// is told it does not.
pub fn with_resident_archive(facts: &Facts) -> Facts {
with_resident_bytes(facts, ARCHIVE_BYTES)
}
/// [`with_resident_archive`] for a memfd of `bytes` (a `ple_only` memfd holds about 16 GB, not 55).
pub fn with_resident_bytes(facts: &Facts, bytes: u64) -> Facts {
let mut facts = facts.clone();
facts.memory.available = facts.memory.available.saturating_add(bytes);
facts
}
pub fn compute(
facts: &Facts,
gpu: Option<&Gpu>,
profile: &Profile,
preset: Preset,
) -> ResourcePlan {
let mut plan = ResourcePlan {
preset,
cores: facts.cpu_cores,
cpu_workers: None,
gpu_budget_bytes: profile.gpu_budget_bytes,
min_vram_free_mib: profile.min_vram_free_mib,
vram_free_after_mib: None,
ram_floors_mib: None,
ram_expected_free_after_bytes: None,
mapped_resident_gib: None,
resident_budget_gib: None,
windows_floors_mib: None,
mapped_working_set_gib: None,
problems: Vec::new(),
warnings: Vec::new(),
};
plan_cpu(&mut plan, facts, preset);
if let Some(gpu) = gpu {
plan_vram(&mut plan, gpu, profile, preset);
}
plan_host_ram(&mut plan, facts, profile, preset);
plan_windows_mapped(&mut plan, facts, profile, preset);
plan
}
fn plan_cpu(plan: &mut ResourcePlan, facts: &Facts, preset: Preset) {
if facts.cpu_cores == 0 {
return;
}
let cores = facts.cpu_cores;
let leave = match preset {
Preset::Dedicated => 2,
Preset::Workstation if cores <= 16 => 4,
Preset::Workstation => 8,
};
let mut workers = cores.saturating_sub(leave).clamp(1, MAX_USEFUL_WORKERS);
// The pool's workers spin while they wait: one without a core of its own steals cycles from one that has it, so a
// bigger count is slower, not just useless (docs/upstream-port-plan.md row 2.3). Where the platform reports the
// physical cores (Windows), the count never exceeds every physical core but the host thread's: 7 on the 8-core
// laptop instead of 12 (the workstation preset's logical-core count); the 16-core server is unchanged (15).
if facts.cpu_physical_cores > 0 {
workers = workers.min(facts.cpu_physical_cores.saturating_sub(1).max(1));
}
plan.cpu_workers = Some(workers);
if preset == Preset::Workstation && cores < leave + 1 {
plan.warnings.push(format!(
"only {cores} CPU cores: the workstation preset cannot leave {leave} of them free (1 worker)"
));
}
}
fn plan_vram(plan: &mut ResourcePlan, gpu: &Gpu, profile: &Profile, preset: Preset) {
let budget_mib = profile.gpu_budget_bytes / MIB;
if preset == Preset::Workstation {
let cap_mib = (gpu
.vram_free_mib
.saturating_sub(WORKSTATION_VRAM_MARGIN_MIB)
/ 1024)
* 1024;
if cap_mib < budget_mib {
cap_vram(plan, profile, cap_mib);
}
}
let used = (plan.gpu_budget_bytes / MIB).max(plan.min_vram_free_mib.unwrap_or(0));
plan.vram_free_after_mib = Some(gpu.vram_free_mib.saturating_sub(used));
}
/// The workstation preset lowers the VRAM budget to `cap_mib`, and reports a profile that cannot live with it.
fn cap_vram(plan: &mut ResourcePlan, profile: &Profile, cap_mib: u64) {
let budget_mib = profile.gpu_budget_bytes / MIB;
plan.gpu_budget_bytes = cap_mib * MIB;
let need = profile.min_vram_free_mib.unwrap_or(budget_mib).min(cap_mib);
plan.min_vram_free_mib = Some(need);
// Below half of the profile budget the expert tier and KV window shrink past what was measured.
if cap_mib * 2 < budget_mib {
plan.problems.push(format!(
"the workstation preset leaves {} MiB of VRAM free, which gives the engine {} MiB; \
the profile {} needs about {} MiB (free the GPU or use --preset dedicated)",
WORKSTATION_VRAM_MARGIN_MIB, cap_mib, profile.name, budget_mib
));
}
}
fn plan_host_ram(plan: &mut ResourcePlan, facts: &Facts, profile: &Profile, preset: Preset) {
let ram = &facts.memory;
if ram.total == 0 {
return;
}
let target = free_ram_target(facts, profile, preset);
if preset == Preset::Workstation {
let soft = target / MIB;
plan.ram_floors_mib = Some((soft, soft / 2));
}
// The sealed memfd holds the whole archive; a file-backed archive with a resident expert bank (rtx5090-32g-file)
// holds only the bank and the engine's pins, its archive pages are page cache that the kernel gives back. The
// mapped file form (laptop) is already the low tier. `kar_source: none` has no archive at all (the PLE table is
// read from the shard through the page cache): the same footprint.
let resident_file = matches!(profile.kar_source, KarSource::File | KarSource::None)
&& profile.expert_source == ExpertSource::Resident;
if profile.kar_source == KarSource::Memfd || resident_file {
plan_pinned_form(plan, facts, profile, target, resident_file);
}
}
/// The free RAM the preset wants to keep after the engine is up.
fn free_ram_target(facts: &Facts, profile: &Profile, preset: Preset) -> u64 {
match preset {
// Windows reclaims standby and trimmed mapped pages on its own (the engine trims its working set under
// pressure), so a free-RAM target above 8 GiB only starves the prefix cache: live 48 GB run 2026-10-26,
// 12 GiB left it a budget of 0.
Preset::Workstation if facts.os == "windows" => {
workstation_ram_target(facts.memory.total).min(WINDOWS_WORKSTATION_RAM_TARGET_BYTES)
}
Preset::Workstation => workstation_ram_target(facts.memory.total),
Preset::Dedicated => profile
.memory
.soft_floor_mib
.map_or(4 * GIB, |mib| mib * MIB),
}
}
/// The pinned form (sealed memfd or resident bank) against the RAM that is available.
fn plan_pinned_form(
plan: &mut ResourcePlan,
facts: &Facts,
profile: &Profile,
target: u64,
resident_file: bool,
) {
let ram = &facts.memory;
let engine = if profile.expert_source == ExpertSource::Resident {
ENGINE_FIXED_RESIDENT_BYTES
} else {
ENGINE_EXTRA_BYTES
};
let footprint = if resident_file {
engine
} else {
profile.memfd_bytes() + engine
};
let after = ram.available.saturating_sub(footprint);
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);
return;
}
plan_mapped_fallback(plan, facts, (target, reserve, footprint), resident_file);
}
/// Not enough RAM for the pinned form: map the archive and the experts from disk instead of failing.
/// `sizes` is (the free RAM to keep, the reserve, the pinned footprint).
fn plan_mapped_fallback(
plan: &mut ResourcePlan,
facts: &Facts,
sizes: (u64, u64, u64),
resident_file: bool,
) {
let (target, reserve, footprint) = sizes;
let available = facts.memory.available;
let resident = 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(available.saturating_sub(u64::from(resident) * GIB + MAPPED_OVERHEAD_BYTES));
plan.warnings.push(format!(
"{} of host RAM available, the pinned form needs about {} plus {} free: {} served from disk (memory-mapped), \
{} GiB of experts stay page-locked; decode is slower than with everything in RAM",
gib_text(available),
gib_text(footprint),
gib_text(target),
if resident_file { "the experts are" } else { "the archive and the experts are" },
resident
));
}
/// The Windows file-backed form with mapped experts and no explicit resident budget.
fn windows_mapped_applies(facts: &Facts, profile: &Profile) -> bool {
facts.os == "windows"
&& profile.kar_source == KarSource::File
&& profile.expert_source == ExpertSource::Mapped
&& profile.memory.resident_budget_gib.is_none()
&& facts.memory.total > 0
}
/// The Windows file-backed form with mapped experts: how much of the free RAM may be page-locked.
fn plan_windows_mapped(plan: &mut ResourcePlan, facts: &Facts, profile: &Profile, preset: Preset) {
if !windows_mapped_applies(facts, profile) {
return;
}
let ram = &facts.memory;
let workstation = preset == Preset::Workstation;
let desktop = if workstation {
WINDOWS_WORKSTATION_DESKTOP_RESERVE_BYTES
} else {
WINDOWS_DESKTOP_RESERVE_BYTES
};
let engine_fixed = MAPPED_ENGINE_FIXED_BYTES;
let room = ram.available.saturating_sub(desktop + engine_fixed) / GIB;
plan.resident_budget_gib = Some(room.min(24) as u32);
if workstation {
// the workstation floors (already raised above) stay: the desktop is the point of the preset
plan.mapped_working_set_gib = Some(WINDOWS_WORKSTATION_WORKING_SET_GIB);
} else {
plan.windows_floors_mib = Some((3072, 1536));
}
plan.warnings.push(format!(
"Windows: the engine may page-lock {room} GiB of experts ({} available, {} left to the desktop, \
{} for the engine's own pools); close heavy applications for a larger share",
gib_text(ram.available),
gib_text(desktop),
gib_text(engine_fixed)
));
}
/// Write the plan into the profile and the engine environment. Values the profile or the command line already set win.
pub fn apply(
plan: &ResourcePlan,
profile: &mut Profile,
engine_env: &mut BTreeMap<String, String>,
budget_overridden: bool,
) {
if !budget_overridden {
profile.gpu_budget_bytes = plan.gpu_budget_bytes;
profile.min_vram_free_mib = plan.min_vram_free_mib;
}
if let Some((soft, hard)) = plan.ram_floors_mib {
// The profile's floors are its dedicated-host values; the workstation preset only raises them.
let raised =
|current: Option<u64>, wanted: u64| Some(current.map_or(wanted, |c| c.max(wanted)));
profile.memory.soft_floor_mib = raised(profile.memory.soft_floor_mib, soft);
profile.memory.hard_floor_mib = raised(profile.memory.hard_floor_mib, hard);
}
if let Some(resident) = plan.mapped_resident_gib {
apply_mapped_form(profile, resident);
}
if let Some(resident) = plan.resident_budget_gib {
profile.memory.resident_budget_gib.get_or_insert(resident);
}
fit_prefill_chunk(profile);
if let Some((soft, hard)) = plan.windows_floors_mib {
lower_windows_floors(profile, soft, hard);
}
apply_environment(plan, profile, engine_env);
}
/// The file-backed form: the archive is mapped, the experts are mapped, the RAM floors of a host that cannot hold both.
fn apply_mapped_form(profile: &mut Profile, resident: u32) {
// `kar_source: none` has no archive to map: it stays none (the PLE table is read from the shard either way)
if profile.kar_source != KarSource::None {
profile.kar_source = KarSource::File;
profile.memfd_extents = crate::profile::MemfdExtents::Full;
}
profile.expert_source = ExpertSource::Mapped;
profile.memory.resident_budget_gib.get_or_insert(resident);
profile.memory.lock = Some("off".into());
profile.memory.heal = Some(false);
profile.min_ram_total_bytes = profile.min_ram_total_bytes.min(16 * GIB);
profile.min_ram_available_bytes = profile
.min_ram_available_bytes
.min(MAPPED_MIN_AVAILABLE_BYTES);
}
/// The prompt chunk borrows its buffers from the expert cache, so a smaller VRAM budget (the workstation preset's, or
/// `--gpu-budget-gib`)
/// that cannot hold the profile's chunk would stop the engine from starting: lower the chunk (and the grid with it)
/// to what the budget carries.
fn fit_prefill_chunk(profile: &mut Profile) {
let Some(chunk) = profile.prefill_capacity else {
return;
};
let fits = prefill_capacity_for_budget(profile.gpu_budget_bytes);
if chunk > fits {
profile.prefill_capacity = Some(fits);
if let Some(block) = profile.memory.prefix_cache_block
&& block > fits
{
profile.memory.prefix_cache_block = Some(fits);
}
}
}
/// The working-set and worker count the plan asks for, unless the profile already names them.
fn apply_environment(
plan: &ResourcePlan,
profile: &Profile,
engine_env: &mut BTreeMap<String, String>,
) {
if let Some(gib) = plan.mapped_working_set_gib
&& !profile.engine_env.contains_key(MAPPED_WORKING_SET_ENV)
{
engine_env.insert(MAPPED_WORKING_SET_ENV.to_owned(), gib.to_string());
}
if let Some(workers) = plan.cpu_workers
&& !profile.engine_env.contains_key(CPU_WORKERS_ENV)
{
engine_env.insert(CPU_WORKERS_ENV.to_owned(), workers.to_string());
}
}
/// The Windows RAM floors: only ever lowered, and only when the profile asks for more than the Windows values.
fn lower_windows_floors(profile: &mut Profile, soft: u64, hard: u64) {
if profile
.memory
.soft_floor_mib
.is_none_or(|current| current > soft)
{
profile.memory.soft_floor_mib = Some(soft);
profile.memory.hard_floor_mib = Some(hard);
}
}