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
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ in the same change that adds, removes or renames a folder it lists.
node, which unified GPUs share.
- **occupancy**: what is in use in a memory pool: measured use per deployment, other use, and
claims. A **claim** is a starting model deployment's estimate, counted until it reports measured
use; advisory, never a reservation.
use, or for its whole life when it is claimed while running (growing engines); advisory, never a
reservation.
- **demand**: a configuration's estimated bytes per memory pool on one target.
- **placement preview**: the capacity component's answer for one configuration on one target: each
pool's occupancy with the demand and the fit line.
Expand Down
1 change: 1 addition & 0 deletions backend/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ backend/
- Keep one inherent impl directly after its type unless responsibility modules justify a split. Put exactly one blank line between functions, impl blocks and adjacent enum variants (the blank line goes before a variant's attributes and docs).
- Every authored module opens with a `//!` doc a junior developer can read first: a one-line summary, a blank line, then two or three plain sentences on what it does for its callers and its guarantees, naming no internal items. Library crates name the component or capability. Major components also state their boundary. Model: `backend/catalog/src/sizing/expression.rs`.
- Give public value and error types and non-trivial public APIs short capability-focused docs.
- An engine that reads its own variant of a per-engine enum (configuration, resolved state) matches every other variant with `_`, so adding an engine doesn't touch the others. Keep matches exhaustive where each engine needs its own answer, such as dispatch and the engine list.
- Prefer root-cause fixes over lint suppressions. Every suppression gives `reason = "..."`, with `#[expect]` when the lint reliably fires; generated bindings may use one justified crate-level suppression.
- `backend/proto` holds only generated bindings and generic protobuf utilities; [`proto/AGENTS.md`](../proto/AGENTS.md) owns generation.

Expand Down
1 change: 1 addition & 0 deletions backend/core/src/application/workloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub(crate) fn node_workloads(
memory_architecture,
device_ids,
estimate: Estimate::default(),
claim_while_running: false,
attributed: attributed.filter(|attributed| !attributed.is_empty()),
});
}
Expand Down
80 changes: 70 additions & 10 deletions backend/core/src/hardware/capacity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ impl TargetCapacity {
}

/// Counts in the target's pools what the workloads of its node hold there, each in the pools
/// it shares with the target: its own devices and the node's host pool. Starting workloads
/// claim memory beyond what is measured of them, and a claim whose size or devices aren't known
/// makes the pools it may use unknown. Other workloads only split the measured use: what the
/// node attributes to them, or a ready workload's estimate where nothing is attributed.
/// it shares with the target: its own devices and the node's host pool. Starting workloads,
/// and ready ones claimed while running, claim memory beyond what is measured of them, and a
/// claim whose size or devices aren't known makes the pools it may use unknown. Other
/// workloads only split the measured use: what the node attributes to them, or a ready
/// workload's estimate where nothing is attributed.
pub(crate) fn with_workloads(mut self, workloads: &[Workload]) -> Self {
occupy(&mut self.pools, workloads);
self
Expand Down Expand Up @@ -236,9 +237,9 @@ pub(crate) struct Occupancy {
/// it moves with every observation and is never retained.
pub(crate) measured_bytes: Option<u64>,

/// Bytes starting workloads are expected to take beyond what is measured of them: their
/// claims. Absent when a starting workload that may use the pool has an unknown claim there.
/// Advisory, never a reservation.
/// Bytes starting workloads, and ready ones claimed while running, are expected to take beyond
/// what is measured of them: their claims. Absent when a claiming workload that may use the
/// pool has an unknown claim there. Advisory, never a reservation.
pub(crate) claimed_bytes: Option<u64>,

/// What each workload holds of the measured and claimed bytes, in workload order; the rest of
Expand Down Expand Up @@ -290,7 +291,7 @@ pub(crate) enum SegmentSource {
/// A ready workload's estimate standing for measured use the node doesn't attribute to it.
Estimated,

/// A starting workload's estimate beyond what is measured of it.
/// A claiming workload's estimate beyond what is measured of it.
Claimed,

/// Measured use no workload accounts for.
Expand All @@ -305,7 +306,8 @@ pub(crate) enum SegmentSource {
/// estimate: its accelerator estimate counts beyond what the node already attributes to it, while
/// its host estimate counts whole, since attribution sees only accelerator memory and host memory
/// loads through the whole start. Once ready and measured, its claim ends and it holds only
/// measured use.
/// measured use, unless it is claimed while running: it then holds the larger of its estimate and
/// its measured use in each pool for its whole life.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Workload {
/// Identity of the workload, which a pool's segments name.
Expand All @@ -326,6 +328,10 @@ pub(crate) struct Workload {

pub(crate) estimate: Estimate,

/// Whether the workload keeps claiming its estimate once ready, because it may take that
/// memory at any point of its life.
pub(crate) claim_while_running: bool,

/// Accelerator memory the node attributes to the workload's processes, by device id; absent
/// until the node attributes any.
pub(crate) attributed: Option<BTreeMap<String, u64>>,
Expand Down Expand Up @@ -451,6 +457,45 @@ impl Workload {
(SegmentSource::Estimated, estimated)
}

/// Whether the workload is ready and still claims its estimate beyond what is measured of it.
fn claims_while_ready(&self) -> bool {
self.claim_while_running && self.phase == WorkloadPhase::Ready
}

/// The workload's estimate in `pool` while it runs: its accelerator share on one of its
/// devices, its accelerator and host estimates together in a unified target's host pool, and
/// nothing elsewhere. Absent when it may use the pool but that estimate or its devices aren't
/// known.
///
/// The host estimate counts on unified memory because, on macOS, what the node attributes there
/// is the process's whole footprint, host memory included, and an engine that grows after ready
/// fills its host estimate after ready too. Where attribution covers only accelerator memory,
/// such as a GB10, this counts host use twice; no engine that grows after ready runs there.
fn running_estimate_in(&self, pool: &Pool) -> Option<u64> {
match (self.memory_architecture, pool.kind) {
(MemoryArchitecture::Dedicated, PoolKind::Device) => {
if self.device_ids.is_empty() {
return None;
}
let Some(index) = self.device_index(pool) else {
return Some(0);
};
Some(device_share(
self.estimate.accelerator_bytes?,
self.device_ids.len(),
index,
))
}
(MemoryArchitecture::Unified, PoolKind::Host) => Some(
self.estimate
.accelerator_bytes?
.saturating_add(self.estimate.host_bytes.unwrap_or_default()),
),
(MemoryArchitecture::Dedicated, PoolKind::Host)
| (MemoryArchitecture::Unified, PoolKind::Device) => Some(0),
}
}

/// Whether the workload may use `pool`: a device it holds, or any device when its devices
/// don't resolve; the host pool when its devices share host memory, or when it has a host
/// estimate beside dedicated devices.
Expand Down Expand Up @@ -620,7 +665,8 @@ struct HostMemory {
}

/// Counts in each pool what the workloads hold there: claims of starting workloads first, then
/// what the others hold of the measured use, each segment in workload order.
/// what the others hold of the measured use, each segment in workload order. A ready workload
/// claimed while running also claims what its estimate leaves above its share of that use.
fn occupy(pools: &mut [Pool], workloads: &[Workload]) {
for pool in pools {
// Attribution can be newer than the pool's reading, so what it accounts for never
Expand Down Expand Up @@ -668,6 +714,20 @@ fn occupy(pools: &mut [Pool], workloads: &[Workload]) {
let bytes = bytes.min(measured_left);
measured_left -= bytes;
segments.push((index, workload.segment(source, bytes)));
if !workload.claims_while_ready() {
continue;
}
match workload.running_estimate_in(pool) {
Some(estimate) => {
let claimed = estimate.saturating_sub(bytes);
pool.occupancy.claimed_bytes = pool
.occupancy
.claimed_bytes
.map(|total| total.saturating_add(claimed));
segments.push((index, workload.segment(SegmentSource::Claimed, claimed)));
}
None => pool.occupancy.claimed_bytes = None,
}
}
segments.sort_by_key(|(index, _)| *index);
pool.occupancy.workloads.extend(
Expand Down
1 change: 1 addition & 0 deletions backend/core/src/hardware/capacity/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ mod tests {
key,
phase: WorkloadPhase::Ready,
expects_use: true,
claim_while_running: false,
memory_architecture: Dedicated,
device_ids: vec![gpu.to_owned()],
estimate,
Expand Down
Loading
Loading