Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions docs/prefix-cache-offload.md
Original file line number Diff line number Diff line change
Expand Up @@ -1592,18 +1592,21 @@ local disks, the manager can restore records it retained before shutdown.

With only a pool configured and all ranks in one process, Flare registers a
CPU-readable capture buffer. Its slot count is the larger of automatic intake
and the largest GPU page capacity plus one row's slots, bounded by the host
budget. All slots are available for capture. For GLM-5.3-Flash NVFP4 at TP4
with 32 concurrent requests, automatic intake sets it to 11.0 GiB (1,664 slots);
the full-chain floor is 7.1 GiB. Helios limits GPU page capacity so the buffer
fits the host budget. If even the minimum page capacity plus one row does not
fit, startup fails with `host budget cannot hold a full pool capture`. Configure `--prefix-cache-memory` to choose
the host memory explicitly. While the manager can accept
records, each poll selects completed leaves for retirement, with up to four
selected at once. If a capture has no resident ancestor, it copies the full
GPU chain and offers its ancestors for linking at the manager. While the manager
is unavailable, the buffer retains records under the normal DRAM eviction policy.
With separate rank processes, a pool without a local disk tier only restores.
and one full single-sequence capture chain
(`min(GPU page capacity, ceil(max_sequence_tokens / page_tokens))` plus one
row's slots), bounded by the host budget. All slots are available for capture.
For GLM-5.3-Flash NVFP4 at TP4 with 32 concurrent requests, automatic intake
sets it to 11.0 GiB (1,664 slots); the full-chain floor is 7.1 GiB. Helios
limits GPU page capacity so one sequence chain plus one row fits the host
budget. If even the capped minimum page capacity plus one row does not fit,
startup fails with `host budget cannot hold a full pool capture`. Configure
`--prefix-cache-memory` to choose the host memory explicitly. While the manager
can accept records, each poll selects completed leaves for retirement, with up
to four selected at once. If a capture has no resident ancestor, it copies the
full GPU chain and offers its ancestors for linking at the manager. While the
manager is unavailable, the buffer retains records under the normal DRAM
eviction policy. With separate rank processes, a pool without a local disk tier
only restores.

When DRAM feeds the pool directly, the shutdown drain continues copying,
checksumming and retiring records that have not yet completed pool handover at
Expand Down
1 change: 1 addition & 0 deletions helios/src/offload/pool/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn peer_registration_budget_fails_startup() {
input_staging: 0,
shard: Some((1, 2)),
integrated: false,
max_sequence_pages: None,
},
)
.unwrap();
Expand Down
62 changes: 59 additions & 3 deletions helios/src/runtime_core/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! Choose physical cache capacities within reserved virtual address ranges.

use std::collections::HashMap;
use std::num::NonZeroU64;

use crate::model::contract::CodecLayout;
use flare_selene::{CacheGeometry, Plane, geometry::CodecField};
Expand All @@ -31,6 +32,7 @@ const ASYNC_TRANSFER_DEVICE_MARGIN_BYTES: u64 = 256 << 20;
/// never exceed the device page-table headroom reserved for them.
pub(super) struct PlannedHostBudgets {
pub offload_host: Option<u64>,
pub max_sequence_pages: NonZeroU64,
pub async_transfer_host_caps: Vec<u64>,
}

Expand Down Expand Up @@ -558,6 +560,13 @@ struct PageLimit {
maximum: u64,
}

fn max_sequence_pages(context: NonZeroU64, page_tokens: usize) -> Result<NonZeroU64> {
let page_tokens = NonZeroU64::new(page_tokens as u64)
.ok_or_else(|| invalid("model requires a positive cache page size"))?;
Ok(NonZeroU64::new(context.get().div_ceil(page_tokens.get()))
.expect("positive context divided by positive page size is non-zero"))
}

fn page_ceiling(
mode: KvCacheLimit,
context: u64,
Expand Down Expand Up @@ -663,6 +672,11 @@ fn planned_host_future(offload_host: u64, input_window: u64, source_window: u64)
.ok_or_else(|| invalid("CPU image input windows overflow host memory"))
}

/// Effective chain page count for pool-only host capture sizing.
fn capture_chain_pages(pages: u64, max_sequence_pages: NonZeroU64) -> u64 {
pages.min(max_sequence_pages.get())
}

/// Additional per-GPU cost above the capture buffer reserved before pool registration.
fn capture_growth(
layout: &flare_selene::Layout,
Expand Down Expand Up @@ -989,10 +1003,13 @@ impl NativeModel {
.find(|buffer| buffer.name == name)
.map(|buffer| buffer.nbytes)
})?;
let context = self.server_config["max_sequence_tokens"]
let context_limit = self.server_config["max_sequence_tokens"]
.as_u64()
.and_then(NonZeroU64::new)
.ok_or_else(|| invalid("model requires a prepared context ceiling"))?;
let context = context_limit.get();
let ceiling = page_ceiling(options.kv_cache_limit, context, active as u64, &contract)?;
let max_sequence_pages = max_sequence_pages(context_limit, contract.cache.page_tokens)?;
let minimum_pages = ceiling.map_or(active as u64 * 8192u64.div_ceil(page_tokens), |limit| limit.minimum);
let capture = options.prefix_cache.pool_only() && self.cluster().is_none();
let cfg = &options.prefix_cache;
Expand Down Expand Up @@ -1126,6 +1143,7 @@ impl NativeModel {
input_staging: staging,
shard: self.cluster().map(|cluster| (cluster.rank, cluster.world)),
integrated: integrated > 0,
max_sequence_pages: Some(max_sequence_pages),
},
capture.then_some(minimum_pages),
initial_budget,
Expand Down Expand Up @@ -1481,7 +1499,7 @@ impl NativeModel {
return Some(0);
};
let shared = if self.ranks[rank].integrated { integrated } else { 0 };
capture_growth(layout, pages, tier_host, host_growth, shared)
capture_growth(layout, capture_chain_pages(pages, max_sequence_pages), tier_host, host_growth, shared)
},
)?;
// Fewer pages only shrink KV, and fewer rows only the snapshot slots aliased into it, so
Expand Down Expand Up @@ -1547,7 +1565,7 @@ impl NativeModel {
}
let planned_offload_host = if capture {
let layout = capture_layout.as_ref().unwrap();
let chain_bytes = pages
let chain_bytes = capture_chain_pages(pages, max_sequence_pages)
.saturating_add(layout.row_slots() as u64)
.saturating_mul(layout.page_record_bytes);
Some(tier_host.max(chain_bytes))
Expand Down Expand Up @@ -1585,6 +1603,7 @@ impl NativeModel {
}
Ok(PlannedHostBudgets {
offload_host: planned_offload_host,
max_sequence_pages,
async_transfer_host_caps,
})
}
Expand Down Expand Up @@ -2025,6 +2044,7 @@ mod tests {
input_staging: 0,
shard: None,
integrated: true,
max_sequence_pages: None,
};
let disk_only = crate::offload::PrefixCacheOffload {
memory_bytes: Some(0),
Expand Down Expand Up @@ -2433,6 +2453,42 @@ mod tests {
assert_eq!(pages, 131);
}

#[test]
fn pool_capture_growth_caps_at_max_sequence_pages_without_throttling_gpu_pages() {
assert!(max_sequence_pages(NonZeroU64::new(1024).unwrap(), 0).is_err());
assert_eq!(
max_sequence_pages(NonZeroU64::new(1025).unwrap(), 64)
.unwrap()
.get(),
17
);
assert_eq!(
max_sequence_pages(NonZeroU64::new(1_048_576).unwrap(), 64)
.unwrap()
.get(),
16_384
);
let layout =
flare_selene::Layout::new(&[flare_selene::testing::geometry(64, &[("kv", 4096)], 4096)])
.unwrap();
let max_pages = NonZeroU64::new(128).unwrap();
let reserved = (max_pages.get() + layout.row_slots() as u64) * layout.page_record_bytes;
assert_eq!(capture_growth(&layout, 512, reserved, 0, 1), None);
assert_eq!(
capture_growth(&layout, capture_chain_pages(512, max_pages), reserved, 0, 1),
Some(0)
);
let cores = [core(4096, 4096, 1024)];
let (_, uncapped_pages, _) =
capacities(&cores, &[1 << 30], 1, 1, 64, None, |_, _| Some(0)).unwrap();
let (_, pages, _) = capacities(&cores, &[1 << 30], 1, 1, 64, None, |_, pages| {
capture_growth(&layout, capture_chain_pages(pages, max_pages), reserved, 0, 1)
})
.unwrap();
assert_eq!(pages, uncapped_pages);
assert!(pages > max_pages.get());
}

#[test]
fn lower_concurrency_and_verification_move_physical_budget_to_kv() {
let p = [core(100 << 20, 2 << 20, 100000)];
Expand Down
17 changes: 13 additions & 4 deletions helios/src/runtime_core/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
};
use crate::{Result, execution::ExecutionManifest, startup::StartupProgress};
use blake2::{Blake2b, Digest, digest::consts::U32};
use std::{collections::HashSet, thread};
use std::{collections::HashSet, num::NonZeroU64, thread};

pub struct NativeModel {
pub(super) ranks: Vec<NativeResources>,
Expand Down Expand Up @@ -1044,7 +1044,13 @@ impl NativeModel {
&initial,
embedding_host_bytes,
)?;
model.prepare_offload(options, progress, active, planned.offload_host)?;
model.prepare_offload(
options,
progress,
active,
planned.offload_host,
planned.max_sequence_pages,
)?;
model.prepare_async_transfers(
&files,
&cores,
Expand Down Expand Up @@ -1116,6 +1122,7 @@ impl NativeModel {
progress: &StartupProgress,
max_active: usize,
planned_host_budget: Option<u64>,
max_sequence_pages: NonZeroU64,
) -> Result<()> {
if !options.prefix_cache.enabled() {
return Ok(());
Expand Down Expand Up @@ -1162,6 +1169,7 @@ impl NativeModel {
input_staging: staging,
shard,
integrated,
max_sequence_pages: Some(max_sequence_pages),
},
planned_host_budget,
|step| phase.detail(step),
Expand Down Expand Up @@ -1551,7 +1559,7 @@ mod tests {
..Default::default()
};
unsafe {
model.prepare_offload(&options, &progress, 1, None).unwrap();
model.prepare_offload(&options, &progress, 1, None, NonZeroU64::MIN).unwrap();
}
assert!(model.offload.shared().is_none());
}
Expand All @@ -1562,7 +1570,8 @@ mod tests {
},
..Default::default()
};
let error = unsafe { model.prepare_offload(&options, &progress, 1, None) }.unwrap_err();
let error =
unsafe { model.prepare_offload(&options, &progress, 1, None, NonZeroU64::MIN) }.unwrap_err();
assert!(error.to_string().contains("lacks an identity"));
}

Expand Down
2 changes: 2 additions & 0 deletions selene-manager/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3252,6 +3252,7 @@ fn offload_startup_and_restore_cover_every_tier_combination() {
input_staging: 0,
shard: None,
integrated: false,
max_sequence_pages: None,
},
registrar.as_ref(),
)
Expand Down Expand Up @@ -3505,6 +3506,7 @@ fn prepared_pool_store_with_geometry(
input_staging: 0,
shard: None,
integrated: false,
max_sequence_pages: None,
},
&OkRegistrar,
)
Expand Down
1 change: 1 addition & 0 deletions selene-manager/tests/offload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fn startup_keeps_registration_through_attachment_and_store_drain() {
input_staging: 0,
shard: None,
integrated: false,
max_sequence_pages: None,
},
&flare_selene::testing::IdentityRegistrar,
)
Expand Down
11 changes: 10 additions & 1 deletion selene/src/offload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
pool, pool::PoolOptions, record, shard, slab,
};
use std::{
num::NonZeroU64,
path::PathBuf,
time::{Duration, Instant},
};
Expand Down Expand Up @@ -162,7 +163,10 @@ impl PrefixCacheOffload {
let disk = self.disk_enabled();
let capture = local_pages.filter(|_| self.pool_only());
let intake = if let Some(pages) = capture {
let chain = pages
let chain_pages = inputs
.max_sequence_pages
.map_or(pages, |limit| pages.min(limit.get()));
let chain = chain_pages
.checked_add(layout.row_slots() as u64)
.ok_or("pool capture slot count overflow")?;
let budget = match budget_override {
Expand Down Expand Up @@ -336,6 +340,10 @@ pub struct PrepareInputs {
/// Whether any local rank's device reports integrated memory (`CU_DEVICE_ATTRIBUTE_INTEGRATED`);
/// bounds disk-only intake on shared-memory GPUs.
pub integrated: bool,
/// Optional single-sequence page ceiling (`max_sequence_tokens.div_ceil(page_tokens)`).
/// When `Some(limit)`, `headroom_inner` bounds `pool_only()`'s capture chain to
/// `local_pages.min(limit.get())` because no single sequence chain exceeds `limit` pages.
pub max_sequence_pages: Option<NonZeroU64>,
}

/// Elapsed durations for host prefix-cache startup sub-steps.
Expand Down Expand Up @@ -617,6 +625,7 @@ pub fn prepare_with_inputs_budget_with_progress(
input_staging,
shard,
integrated,
..
} = inputs;
options.validate_compression()?;
if !options.enabled() {
Expand Down
4 changes: 4 additions & 0 deletions selene/src/offload/sharded/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn sharded_export_and_restore_use_each_ranks_real_record_file() {
input_staging: 0,
shard: Some((rank, 2)),
integrated: false,
max_sequence_pages: None,
},
&IdentityRegistrar,
)
Expand Down Expand Up @@ -257,6 +258,7 @@ fn sharded_join_rejects_mismatched_compression_modes() {
input_staging: 0,
shard: Some((rank, 2)),
integrated: false,
max_sequence_pages: None,
},
&IdentityRegistrar,
)
Expand Down Expand Up @@ -387,6 +389,7 @@ fn sharded_compressed_v4_store_fetch_truncate_and_restart_agree() {
input_staging: 0,
shard: Some((rank, 2)),
integrated: false,
max_sequence_pages: None,
},
&IdentityRegistrar,
)
Expand Down Expand Up @@ -552,6 +555,7 @@ fn sharded_pool_with_compression_rejected_at_startup() {
input_staging: 0,
shard: Some((0, 2)),
integrated: false,
max_sequence_pages: None,
},
&IdentityRegistrar,
)
Expand Down
Loading