From 43d059de1f727287da9e617536848d6bf703d28d Mon Sep 17 00:00:00 2001 From: Jason Cook Date: Sat, 14 Nov 2026 23:12:21 -0500 Subject: [PATCH] Release the page cache of rank cores sent to peer ranks When `flare run` starts a multi-host core, rank zero reads every peer's rank core through the page cache and sends it over TCP. On a host whose GPU allocates from system memory, such as a DGX Spark (GB10), those cached pages are not free memory for CUDA, so the pages of several rank cores can keep rank zero from creating its CUDA context. Helios already releases the page cache of a rank's own core files before it creates the context (Files::release_page_cache in helios/src/runtime_core/model.rs), and a receiving rank syncs the files it writes; the files rank zero reads on behalf of other ranks were never released. Releasing them where the distribution source stops closes that gap with the same eos operation and needs no change to loading or to the transfer protocol. Before this change the distribution source in flare-cli/src/distribution.rs returned when its serving threads had joined and left the pages it had read in the cache. Now release_served_pages calls flare_eos::release_page_cache (POSIX_FADV_DONTNEED on Linux, a no-op on Windows) on every file of ranks one and above when the source thread finishes, whether distribution succeeded or failed. Rank zero's own files are left to the existing release in Helios. flare-cli gains a path dependency on the in-tree eos crate, which the lock file records. The result is that a rank-zero host returns the memory used to read peer cores before GPU initialization, without a manual page-cache drop. Evidence, on four DGX Sparks (121 GiB unified memory each, one rank per host) serving a 351 GB four-rank Inkling 2 Small NVFP4 core. Before: after rank zero distributed three rank cores, its host showed 114 GiB of page cache and 2 GiB free, and rank zero failed at "Initialize GPU" with CudaError { operation: "cuCtxCreate_v2", code: 2, name: "CUDA_ERROR_OUT_OF_MEMORY" }. The failure was intermittent: the first launch of a smaller core went through the same distribution without it. After, with rebuilt binaries on all four hosts, the peers' rank cores removed and no manual cache drop: three rank cores were sent in 70.2 s, /proc/meminfo on rank zero at the end of distribution showed MemAvailable 115.9 GiB and Cached 0.12 GiB, and GPU initialization succeeded. The Linux test source_releases_the_page_cache_of_peer_core_files_when_it_finishes serves a 4 MiB on-disk file to one peer and checks with mincore that the file has resident pages after the transfer and none after the source finishes. cargo test --release --manifest-path flare-cli/Cargo.toml distribution: 30 passed, 0 failed, 2 ignored (they need external fixtures). Not run: other models, two- and three-rank cores, and hosts with discrete GPUs, where the release changes only the page cache. Design-Signed-Off-By: flare-02; design-review=ed22 --- flare-cli/Cargo.lock | 1 + flare-cli/Cargo.toml | 1 + flare-cli/src/distribution.rs | 82 ++++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/flare-cli/Cargo.lock b/flare-cli/Cargo.lock index 14f081e4d..94a252e72 100644 --- a/flare-cli/Cargo.lock +++ b/flare-cli/Cargo.lock @@ -989,6 +989,7 @@ version = "0.1.0" dependencies = [ "clap", "flare", + "flare-eos", "flare-helios", "libc", "libloading", diff --git a/flare-cli/Cargo.toml b/flare-cli/Cargo.toml index 82e64ac0b..10be10791 100644 --- a/flare-cli/Cargo.toml +++ b/flare-cli/Cargo.toml @@ -24,6 +24,7 @@ flare = { path = "../flare" } serde = { version = "1", features = ["derive"] } toml = "0.8" clap = { version = "4.6", features = ["derive"] } +flare-eos = { path = "../eos" } flare-helios = { path = "../helios", features = ["runtime-core", "server"] } libc = "0.2.189" serde_json = "1.0" diff --git a/flare-cli/src/distribution.rs b/flare-cli/src/distribution.rs index 4edb8129f..6cc2d74ba 100644 --- a/flare-cli/src/distribution.rs +++ b/flare-cli/src/distribution.rs @@ -1004,6 +1004,17 @@ impl Drop for Payload<'_> { } } +/// Drop the clean page-cache pages of every core file the source read for a peer rank. +/// +/// Rank zero reads each peer's core ranges through the page cache to send them. An integrated +/// GPU such as GB10 allocates from system memory, where cached file pages are not free, so the +/// pages of several rank cores can keep rank zero from creating its CUDA context. +fn release_served_pages(files: &[Vec>]) { + for file in files.iter().skip(1).flatten() { + flare_eos::release_page_cache(file); + } +} + struct Source { port: u16, stopping: Arc, @@ -1024,7 +1035,7 @@ impl Source { let (stop, held) = (stopping.clone(), channels.clone()); let worker = thread::spawn(move || { let slots = Mutex::new(std::collections::HashSet::new()); - thread::scope(|scope| -> Result<(), Failure> { + let result = thread::scope(|scope| -> Result<(), Failure> { let mut workers = Vec::new(); let (sender, receiver) = std::sync::mpsc::channel(); let mut error = None; @@ -1066,7 +1077,9 @@ impl Source { } } error.map_or(Ok(()), Err) - }) + }); + release_served_pages(&files); + result }); Ok(Self { port, @@ -1395,6 +1408,71 @@ mod tests { source.finish().unwrap(); } + /// Pages of `file` that the page cache holds. + #[cfg(target_os = "linux")] + fn resident_pages(file: &File, size: usize) -> usize { + use std::os::fd::AsRawFd; + + let page = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize; + let mut resident = vec![0u8; size.div_ceil(page)]; + unsafe { + let map = libc::mmap( + std::ptr::null_mut(), + size, + libc::PROT_READ, + libc::MAP_SHARED, + file.as_raw_fd(), + 0, + ); + assert_ne!(map, libc::MAP_FAILED); + assert_eq!(libc::mincore(map, size, resident.as_mut_ptr()), 0); + libc::munmap(map, size); + } + resident.iter().filter(|&&page| page & 1 != 0).count() + } + + #[cfg(target_os = "linux")] + #[test] + fn source_releases_the_page_cache_of_peer_core_files_when_it_finishes() { + use std::os::unix::fs::MetadataExt; + + // A memory-backed file system keeps its pages; the crate directory is on disk. + let directory = tempfile::tempdir_in(env!("CARGO_MANIFEST_DIR")).unwrap(); + let size = 4 << 20; + let path = directory.path().join("core"); + fs::write(&path, vec![7u8; size]).unwrap(); + let file = File::open(&path).unwrap(); + file.sync_all().unwrap(); + assert_ne!(file.metadata().unwrap().blocks(), 0); + let observer = File::open(&path).unwrap(); + let spec = || { + vec![FileSpec { + name: "core".into(), + size: size as u64, + ranges: vec![(0, size as u64)], + }] + }; + let source = Source::start(vec![Vec::new(), vec![Arc::new(file)]], vec![spec(), spec()], 1).unwrap(); + let mut socket = TcpStream::connect(("127.0.0.1", source.port)).unwrap(); + configure(&socket, IO_TIMEOUT).unwrap(); + socket.write_all(&[1, 0, 0, 0, 0, 0, 0, 0]).unwrap(); + write( + &mut socket, + &Request { + file: 0, + ranges: vec![(0, size as u64)], + }, + ) + .unwrap(); + let mut bytes = vec![0; 8 + size]; + socket.read_exact(&mut bytes).unwrap(); + assert!(bytes[8..].iter().all(|&byte| byte == 7)); + assert_ne!(resident_pages(&observer, size), 0); + socket.shutdown(Shutdown::Both).unwrap(); + source.finish().unwrap(); + assert_eq!(resident_pages(&observer, size), 0); + } + #[test] fn sparse_rank_distribution_reuses_channels_across_files_and_cached_launches() { for (world, ring) in [(2, false), (4, false), (4, true)] {