use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Declared {
pub model: String,
pub weights_bytes: u64,
pub kv_bytes: u64,
}
impl Declared {
pub fn total_bytes(&self) -> u64 {
self.weights_bytes.saturating_add(self.kv_bytes)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Budget {
pub total_bytes: u64,
pub reserve_bytes: u64,
}
pub const GIB: u64 = 1024 * 1024 * 1024;
impl Budget {
pub fn with_reserve_pct(total_bytes: u64, pct: u8) -> Budget {
let pct = pct.min(100) as u64;
let proportional = total_bytes / 100 * pct;
Budget {
total_bytes,
reserve_bytes: proportional.max(GIB).min(total_bytes),
}
}
pub fn usable_bytes(&self) -> u64 {
self.total_bytes.saturating_sub(self.reserve_bytes)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Plan {
pub admitted: Vec<String>,
pub rejected: Vec<Rejection>,
pub committed_bytes: u64,
pub usable_bytes: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Rejection {
pub model: String,
pub needed_bytes: u64,
pub free_bytes: u64,
}
impl Rejection {
pub fn short_bytes(&self) -> u64 {
self.needed_bytes.saturating_sub(self.free_bytes)
}
}
impl Plan {
pub fn fits(&self) -> bool {
self.rejected.is_empty()
}
pub fn headroom_bytes(&self) -> u64 {
self.usable_bytes.saturating_sub(self.committed_bytes)
}
pub fn explain(&self) -> String {
let mut out = format!(
"{} of {} admitted, {:.1} GiB committed of {:.1} GiB usable",
self.admitted.len(),
self.admitted.len() + self.rejected.len(),
gib(self.committed_bytes),
gib(self.usable_bytes),
);
for r in &self.rejected {
out.push_str(&format!(
"\n REJECTED {} — needs {:.1} GiB, {:.1} GiB free, short by {:.1} GiB",
r.model,
gib(r.needed_bytes),
gib(r.free_bytes),
gib(r.short_bytes()),
));
}
out
}
}
pub fn gib(bytes: u64) -> f64 {
bytes as f64 / GIB as f64
}
pub fn plan(budget: Budget, declared: &[Declared]) -> Plan {
let usable = budget.usable_bytes();
let mut committed: u64 = 0;
let mut admitted = Vec::new();
let mut rejected = Vec::new();
for d in declared {
let need = d.total_bytes();
let free = usable.saturating_sub(committed);
if need <= free {
committed += need;
admitted.push(d.model.clone());
} else {
rejected.push(Rejection {
model: d.model.clone(),
needed_bytes: need,
free_bytes: free,
});
}
}
Plan {
admitted,
rejected,
committed_bytes: committed,
usable_bytes: usable,
}
}
pub fn declared_from_json(v: &serde_json::Value) -> Result<Vec<Declared>, Vec<String>> {
let Some(items) = v.as_array() else {
return Err(vec!["expected an array of declared models".into()]);
};
let mut out = Vec::with_capacity(items.len());
let mut errors = Vec::new();
for (i, item) in items.iter().enumerate() {
match one_declared(item) {
Ok(d) => out.push(d),
Err(why) => errors.push(format!("entry {i}: {why}")),
}
}
if errors.is_empty() {
Ok(out)
} else {
Err(errors)
}
}
pub fn whole_bytes(v: &serde_json::Value) -> Option<u64> {
size_of(Some(v))
}
fn size_of(v: Option<&serde_json::Value>) -> Option<u64> {
let v = v?;
if let Some(n) = v.as_u64() {
return Some(n);
}
let f = v.as_f64()?;
if f.is_finite() && f >= 0.0 && f.fract() == 0.0 {
Some(f as u64)
} else {
None
}
}
fn one_declared(v: &serde_json::Value) -> Result<Declared, String> {
let model = v
.get("model")
.and_then(|m| m.as_str())
.ok_or("missing \"model\"")?;
if model.is_empty() {
return Err("\"model\" is empty".into());
}
let weights = size_of(v.get("weightsBytes").or_else(|| v.get("weights_bytes")))
.ok_or_else(|| format!("{model}: \"weightsBytes\" missing or not a whole byte count"))?;
let kv = match v.get("kvBytes").or_else(|| v.get("kv_bytes")) {
None | Some(serde_json::Value::Null) => 0,
some => size_of(some)
.ok_or_else(|| format!("{model}: \"kvBytes\" is not a whole byte count"))?,
};
Ok(Declared {
model: model.to_string(),
weights_bytes: weights,
kv_bytes: kv,
})
}