107 lines
4.6 KiB
Rust
107 lines
4.6 KiB
Rust
//! `engine/strata/setup.py`'s pure RAM / VRAM / context sizing rules, ported function for function.
|
|
//!
|
|
//! The rope half of `setup.py` (`derived_factor`, `resolve_rope`) lives in [`crate::rope_scaling`]
|
|
//! (`derived_factor`, `setup_resolve_rope`); this module holds the rest: which contexts the RAM allows
|
|
//! ([`ram_ctx`], [`ctx_ram_need`]), how many GiB of experts to keep resident ([`resident_budget_gib`]) and the
|
|
//! low-RAM mode's arithmetic (`low_ram_*`). Every function is checked against `import setup` over a grid by
|
|
//! `tests/setup_policy_oracle.rs` (golden from `scripts/offline/gen_goldens.py setup_policy`).
|
|
//!
|
|
//! Floating point is IEEE double on both sides, evaluated in the same order; Python's `round()` is round-half-even
|
|
//! (`f64::round_ties_even`).
|
|
|
|
/// `setup.py` `CONTEXTS`: the context sizes the setup offers.
|
|
pub const CONTEXTS: [u64; 7] = [8192, 32768, 65536, 131072, 262144, 393216, 524288];
|
|
/// `UNSLOTH_RAM_LEFT_GB`: RAM beside the resident budget (OS, engine, file cache).
|
|
pub const UNSLOTH_RAM_LEFT_GB: i64 = 24;
|
|
/// `LOW_RAM_HEADROOM_GB`: RAM beside the experts in the low-RAM mode.
|
|
pub const LOW_RAM_HEADROOM_GB: f64 = 10.0;
|
|
|
|
/// One entry of `setup.py` `MODELS` (only the sizing fields).
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Model {
|
|
pub name: &'static str,
|
|
/// RAM a PC needs for the model (GB).
|
|
pub ram_gb: u32,
|
|
/// The routed experts' size (GB).
|
|
pub arena_gb: f64,
|
|
}
|
|
|
|
/// `setup.py` `MODELS`, in file order.
|
|
pub const MODELS: [Model; 6] = [
|
|
Model { name: "Q2_0", ram_gb: 48, arena_gb: 34.0 },
|
|
Model { name: "IQ2_XS", ram_gb: 48, arena_gb: 35.5 },
|
|
Model { name: "IQ3_XXS", ram_gb: 60, arena_gb: 42.9 },
|
|
Model { name: "IQ3_S", ram_gb: 62, arena_gb: 50.3 },
|
|
Model { name: "IQ1_M", ram_gb: 32, arena_gb: 23.4 },
|
|
Model { name: "UD-Q4_K_XL", ram_gb: 48, arena_gb: 77.0 },
|
|
];
|
|
|
|
pub fn model(name: &str) -> Option<&'static Model> {
|
|
MODELS.iter().find(|m| m.name == name)
|
|
}
|
|
|
|
/// KV cache element format for [`low_ram_gpu_gb`]: `"q4_0"` costs 576 bytes per cell and layer, everything else 1056.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Kv {
|
|
Int8,
|
|
Q4_0,
|
|
}
|
|
|
|
impl Kv {
|
|
pub fn parse(name: &str) -> Self {
|
|
if name == "q4_0" { Self::Q4_0 } else { Self::Int8 }
|
|
}
|
|
}
|
|
|
|
/// `resident_budget_gib`: the GiB of experts the engine keeps in RAM (UD-Q4_K_XL): the RAM less 24 for the OS, the
|
|
/// engine and the file cache, less a KV cache streamed to RAM; at most all of them, at least 8.
|
|
pub fn resident_budget_gib(model: &Model, ram: f64, kv_ram_gb: f64) -> i64 {
|
|
let gib = ram.round_ties_even() as i64 - UNSLOTH_RAM_LEFT_GB - kv_ram_gb.ceil() as i64;
|
|
gib.min((model.arena_gb / 1.073741824) as i64).max(8)
|
|
}
|
|
|
|
/// `low_ram_needed`: the experts do not fit the PC's RAM with room left, so they are mapped from `experts.bin`.
|
|
pub fn low_ram_needed(model: &Model, ram: f64) -> bool {
|
|
ram < model.arena_gb + LOW_RAM_HEADROOM_GB
|
|
}
|
|
|
|
/// `low_ram_gpu_gb`: about how many GB of the experts the GPU's cache holds.
|
|
pub fn low_ram_gpu_gb(model: &Model, vram_gb: f64, ctx: u64, kv: Kv) -> f64 {
|
|
let bytes_per_token = 13.0 * if kv == Kv::Q4_0 { 576.0 } else { 1056.0 };
|
|
let longer = ctx.saturating_sub(32768) as f64 * bytes_per_token / 1e9;
|
|
model.arena_gb.min(vram_gb - 5.0 - longer).max(0.0)
|
|
}
|
|
|
|
pub fn low_ram_gpu_share(model: &Model, vram_gb: f64, ctx: u64, kv: Kv) -> f64 {
|
|
low_ram_gpu_gb(model, vram_gb, ctx, kv) / model.arena_gb
|
|
}
|
|
|
|
/// `low_ram_resident`: the experts the GPU does not hold fit the RAM with the usual room, so they are copied once.
|
|
pub fn low_ram_resident(model: &Model, ram: f64, vram_gb: f64, ctx: u64, kv: Kv) -> bool {
|
|
let rest = model.arena_gb - low_ram_gpu_gb(model, vram_gb, ctx, kv);
|
|
ram >= rest + LOW_RAM_HEADROOM_GB
|
|
}
|
|
|
|
pub fn low_ram_fits(model: &Model, ram: f64, vram_gb: f64) -> bool {
|
|
ram - 6.0 + (vram_gb - 5.0).max(0.0) >= model.arena_gb
|
|
}
|
|
|
|
/// `ctx_ram_need`: the RAM (GB) estimated for a long context with IQ3_XXS / IQ3_S - their experts, the context's 8-bit
|
|
/// KV cache and 24 GB of room. `None` where the rule does not apply: the other sizes and the low-RAM mode.
|
|
pub fn ctx_ram_need(model: &Model, ctx: u64, low_ram: bool) -> Option<f64> {
|
|
if !matches!(model.name, "IQ3_XXS" | "IQ3_S") || low_ram {
|
|
return None;
|
|
}
|
|
Some(model.arena_gb + ctx as f64 * 13.0 * 1056.0 / 1e9 + 24.0)
|
|
}
|
|
|
|
/// `ram_ctx`: the longest context the RAM rule recommends: 128K, or longer where the estimate fits this PC's RAM.
|
|
pub fn ram_ctx(model: &Model, ram: f64, low_ram: bool) -> u64 {
|
|
CONTEXTS
|
|
.iter()
|
|
.copied()
|
|
.filter(|&c| c <= 131072 || ctx_ram_need(model, c, low_ram).unwrap_or(0.0) <= ram)
|
|
.max()
|
|
.expect("CONTEXTS contains 8192")
|
|
}
|