maia: the weights move into the network (no copy), the file's bytes go once read
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run

Peak memory while loading: 79M 940 → 636 MB, 5M 72 → 52 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-26 00:09:21 +03:00
parent 0fd40f01b4
commit a94de00199
3 changed files with 20 additions and 11 deletions

View file

@ -26,14 +26,23 @@ pub struct Maia {
impl Maia { impl Maia {
/// Reads the checkpoint (`maia3-5m.pt`). /// Reads the checkpoint (`maia3-5m.pt`).
pub fn load(path: &std::path::Path) -> Result<Maia, WeightsError> { pub fn load(path: &std::path::Path) -> Result<Maia, WeightsError> {
let bytes = std::fs::read(path).map_err(WeightsError::Io)?; // The file's bytes are let go once read into tensors, and the tensors move into
Maia::from_bytes(&bytes) // the network: at most two copies of the weights at once (the large model: its
// 316 MB twice, not three times).
let w = {
let bytes = std::fs::read(path).map_err(WeightsError::Io)?;
weights::Weights::parse(&bytes)?
};
Maia::from_weights(w)
} }
pub fn from_bytes(bytes: &[u8]) -> Result<Maia, WeightsError> { pub fn from_bytes(bytes: &[u8]) -> Result<Maia, WeightsError> {
let w = weights::Weights::parse(bytes)?; Maia::from_weights(weights::Weights::parse(bytes)?)
}
fn from_weights(mut w: weights::Weights) -> Result<Maia, WeightsError> {
Ok(Maia { Ok(Maia {
net: net::Net::new(&w)?, net: net::Net::new(&mut w)?,
}) })
} }

View file

@ -65,7 +65,7 @@ struct Linear {
impl Linear { impl Linear {
fn load( fn load(
w: &Weights, w: &mut Weights,
name: &str, name: &str,
out: usize, out: usize,
inp: usize, inp: usize,
@ -152,7 +152,7 @@ struct LayerNorm {
} }
impl LayerNorm { impl LayerNorm {
fn load(w: &Weights, name: &str, n: usize) -> Result<LayerNorm, WeightsError> { fn load(w: &mut Weights, name: &str, n: usize) -> Result<LayerNorm, WeightsError> {
Ok(LayerNorm { Ok(LayerNorm {
w: w.get(&format!("{name}.weight"), &[n])?, w: w.get(&format!("{name}.weight"), &[n])?,
b: w.get(&format!("{name}.bias"), &[n])?, b: w.get(&format!("{name}.bias"), &[n])?,
@ -246,7 +246,7 @@ pub struct Prediction {
} }
impl Net { impl Net {
pub fn new(w: &Weights) -> Result<Net, WeightsError> { pub fn new(w: &mut Weights) -> Result<Net, WeightsError> {
let dims = Dims::read(w)?; let dims = Dims::read(w)?;
let Dims { let Dims {
d, d,

View file

@ -44,11 +44,11 @@ impl Weights {
shape.get(axis).copied().ok_or(WeightsError::Format) shape.get(axis).copied().ok_or(WeightsError::Format)
} }
/// The tensor `name` of this shape. /// The tensor `name` of this shape, taken out (each is used once: no copy).
pub fn get(&self, name: &str, shape: &[usize]) -> Result<Vec<f32>, WeightsError> { pub fn get(&mut self, name: &str, shape: &[usize]) -> Result<Vec<f32>, WeightsError> {
let t = self let t = self
.tensors .tensors
.get(name) .remove(name)
.ok_or_else(|| WeightsError::Missing(name.to_owned()))?; .ok_or_else(|| WeightsError::Missing(name.to_owned()))?;
if t.shape != shape { if t.shape != shape {
return Err(WeightsError::Shape( return Err(WeightsError::Shape(
@ -57,6 +57,6 @@ impl Weights {
shape.to_vec(), shape.to_vec(),
)); ));
} }
Ok(t.data.clone()) Ok(t.data)
} }
} }