aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-inference-abi / examples / load_rss.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Host-only measurement of the checkpoint load path (no GPU, no chip): peak and final resident |
| 17 | //! memory of the process for the unchanged host f32 load and for the GB10 resident load |
| 18 | //! (sovereign-core #277 cut C) with a stub uploader that only checksums the bf16 bits. |
| 19 | //! |
| 20 | //! load_rss host|resident <model_dir> |
| 21 | use aien_inference_abi::{ |
| 22 | load_model_config, load_resident_weights, ResidentHandle, ResidentUploader, ResidentWeight, |
| 23 | TransformerWeights, |
| 24 | }; |
| 25 | use std::sync::{Arc, Mutex}; |
| 26 | |
| 27 | fn status_kib(key: &str) -> u64 { |
| 28 | std::fs::read_to_string("/proc/self/status") |
| 29 | .unwrap() |
| 30 | .lines() |
| 31 | .find_map(|l| l.strip_prefix(key)) |
| 32 | .and_then(|v| v.trim().trim_end_matches("kB").trim().parse().ok()) |
| 33 | .unwrap_or(0) |
| 34 | } |
| 35 | |
| 36 | fn gib(kib: u64) -> f64 { |
| 37 | kib as f64 / 1024.0 / 1024.0 |
| 38 | } |
| 39 | |
| 40 | #[derive(Debug)] |
| 41 | struct Stub; |
| 42 | impl ResidentHandle for Stub { |
| 43 | fn matmul_f32(&self, _: usize, _: &[f32], _: &mut [f32]) -> Result<u64, String> { |
| 44 | Err("stub".into()) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | struct StubUploader { |
| 49 | log: Mutex<Vec<(usize, u64)>>, |
| 50 | bytes: Mutex<u64>, |
| 51 | } |
| 52 | impl ResidentUploader for StubUploader { |
| 53 | fn upload_bf16(&self, k: usize, n: usize, bits: &[u16]) -> Result<ResidentWeight, String> { |
| 54 | let sum = bits.iter().fold(0u64, |a, b| a.wrapping_add(*b as u64)); |
| 55 | std::hint::black_box(sum); |
| 56 | *self.bytes.lock().unwrap() += (k * n * 2) as u64; |
| 57 | self.log.lock().unwrap().push((k * n, status_kib("VmRSS:"))); |
| 58 | Ok(ResidentWeight::new(k, n, Arc::new(Stub))) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn main() { |
| 63 | let args: Vec<String> = std::env::args().collect(); |
| 64 | let (mode, dir) = (args[1].as_str(), std::path::PathBuf::from(&args[2])); |
| 65 | let config = load_model_config(&dir).expect("config.json"); |
| 66 | let t = std::time::Instant::now(); |
| 67 | let weights = match mode { |
| 68 | "host" => TransformerWeights::load_from_safetensors(&dir, &config).expect("host load"), |
| 69 | "resident" => { |
| 70 | let up = StubUploader { |
| 71 | log: Mutex::new(Vec::new()), |
| 72 | bytes: Mutex::new(0), |
| 73 | }; |
| 74 | let w = load_resident_weights(&dir, &config, &up, &mut |_| {}).expect("resident load"); |
| 75 | let log = up.log.lock().unwrap(); |
| 76 | let max_during_uploads = log.iter().map(|x| x.1).max().unwrap_or(0); |
| 77 | println!( |
| 78 | "uploads={} bf16_uploaded={:.3} GiB max_VmRSS_during_uploads={:.3} GiB", |
| 79 | log.len(), |
| 80 | gib(*up.bytes.lock().unwrap() / 1024), |
| 81 | gib(max_during_uploads) |
| 82 | ); |
| 83 | w |
| 84 | } |
| 85 | other => panic!("mode must be host or resident, got {other}"), |
| 86 | }; |
| 87 | let resident = weights |
| 88 | .layers |
| 89 | .iter() |
| 90 | .filter(|l| l.q_proj.is_resident()) |
| 91 | .count(); |
| 92 | println!( |
| 93 | "mode={mode} load_secs={:.1} VmHWM(peak)={:.3} GiB VmRSS(after load)={:.3} GiB resident_layers={resident}/{}", |
| 94 | t.elapsed().as_secs_f64(), |
| 95 | gib(status_kib("VmHWM:")), |
| 96 | gib(status_kib("VmRSS:")), |
| 97 | weights.layers.len() |
| 98 | ); |
| 99 | } |