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
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ local store's measured restore rate.

When a loaded core declares `Execution.async_transfers` channels, `/metrics`
also exports lock-free per-channel gauges and counters labeled by `channel` and
`rank`: `flare_async_transfer_device_pool_bytes`,
the global `rank` ID: `flare_async_transfer_device_pool_bytes`,
`flare_async_transfer_host_pool_bytes`,
`flare_async_transfer_submitted_cmds_total`,
`flare_async_transfer_completed_cmds_total`,
Expand Down
17 changes: 10 additions & 7 deletions docs/prefix-cache-offload.md
Original file line number Diff line number Diff line change
Expand Up @@ -994,13 +994,16 @@ Offload limits interference through bounded work and explicit ownership:
stream uses the default priority, which CUDA defines as the least priority
as well, so stream priority does not separate offload work from model work.
When a loaded model contains CUDA graphs with in-graph peer-to-peer Copy Engine
nodes (`Node::Copy` referencing `Memory::Peer`), Helios coordinates offload
batches against those graphs through a per-model `CopyEngineGate`: new offload
batches pause between 16 MiB windows while a peer-copy graph executes, waiting
peer-copy graphs bound their drain wait at 120 seconds, and continuous pipelined
peer-copy execution yields bounded fairness turns to waiting offload workers.
Models without peer-copy graphs omit the gate and keep one synchronization per
transfer window. Creating the model and copy streams at a greater priority
nodes (`Node::Copy` referencing `Memory::Peer`), or runs on a single-host PCIe
multi-rank topology (without all-pairs NVLink peer links) with graphs whose
kernels, memsets, module globals, or indirect pointer tables access
`Memory::Peer`, Helios coordinates offload batches against those graphs through
a per-model `CopyEngineGate`: new offload batches pause between 16 MiB windows
while a gated graph executes, waiting gated graphs bound their drain wait at
120 seconds, and continuous pipelined execution yields bounded fairness turns
to waiting offload workers. Models without gated graphs omit the gate and keep
one synchronization per transfer window. Creating the model and copy streams at
a greater priority
requires the section 4 measurements to show a benefit. Measure delays to
input uploads, completion downloads, and Engram row copies on shared copy
engines through per-token latency. Batch size and stream priority alone do
Expand Down
20 changes: 13 additions & 7 deletions docs/runtime-core-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,19 @@ serving dashboard without synchronizing GPU streams. Shutdown drains compute and
copy streams first, writes `1u32` (release) to the mapped-host `stop_flag`
reference, and polls `cuStreamQuery` on the worker stream against a bounded
drain timeout with context save/restore (returning `ExecutionError::Drain` and
retaining GPU resources on timeout). Resource teardown stops any still-running
worker, synchronizes the primary context, drains active telemetry counter
readers before unmapping allocations, and only after context synchronization
succeeds destroys worker and compute streams, executables, modules, mappings,
all created green contexts (worker, compute, and optional codec, clearing rather
than restoring the caller's current context if it referred to any destroyed
green context), virtual-address reservations, and the primary context.
retaining GPU resources on timeout). Multi-rank model teardown executes a
three-phase cross-rank barrier: first, every rank stops both persistent workers
and async-transfer workers without short-circuiting and synchronizes its CUDA
context before any rank frees shared memory; second, while `Memory::MappedHost`
mailboxes and CUDA contexts remain live, Engram background workers and prefix
offload workers join, after which every rank destroys its streams, executables,
modules, and allocations (draining active telemetry counter readers before
unmapping); third, every rank destroys all created green contexts (worker,
compute, and optional codec, clearing rather than restoring the caller's current
context if it referred to any destroyed green context), virtual-address
reservations, and primary CUDA contexts in reverse rank order. If any rank fails
drain, all ranks retain their CUDA and host resources until process
exit.

Before CUDA initialization or serving worker threads, Flare restricts
`CUDA_VISIBLE_DEVICES` to `--gpus`, resolving indices within an inherited mask.
Expand Down
114 changes: 104 additions & 10 deletions helios/src/async_transfer/io_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub trait IoBackend: Send {
/// Flush any locally staged submission entries to the kernel without blocking.
fn flush(&mut self) -> io::Result<()>;

/// Number of staged submission entries not yet accepted by the kernel.
fn pending_submissions(&self) -> usize {
0
}

/// Harvest all currently completed I/O operations without blocking.
fn poll_completions(&mut self, out: &mut Vec<IoCompletion>) -> io::Result<()>;

Expand Down Expand Up @@ -138,6 +143,32 @@ pub mod linux_uring {
}
}

const MAX_EINTR_RETRIES: usize = 16;

pub(super) fn flush_queued_sqes(
queued_sqes: &mut usize,
mut submit: impl FnMut() -> io::Result<usize>,
) -> io::Result<()> {
let mut eintr_retries = 0usize;
while *queued_sqes > 0 {
match submit() {
Ok(0) => break,
Ok(submitted) => {
*queued_sqes = queued_sqes.saturating_sub(submitted);
eintr_retries = 0;
}
Err(err) if err.raw_os_error() == Some(libc::EINTR) => {
eintr_retries += 1;
if eintr_retries >= MAX_EINTR_RETRIES {
return Err(err);
}
}
Err(err) => return Err(err),
}
}
Ok(())
}

impl IoBackend for IoUringBackend {
unsafe fn submit_read(
&mut self,
Expand All @@ -157,12 +188,12 @@ pub mod linux_uring {
}

fn flush(&mut self) -> io::Result<()> {
if self.queued_sqes == 0 {
return Ok(());
}
let submitted = self.ring.submit()?;
self.queued_sqes = self.queued_sqes.saturating_sub(submitted);
Ok(())
let ring = &mut self.ring;
flush_queued_sqes(&mut self.queued_sqes, || ring.submit())
}

fn pending_submissions(&self) -> usize {
self.queued_sqes
}

fn poll_completions(&mut self, out: &mut Vec<IoCompletion>) -> io::Result<()> {
Expand Down Expand Up @@ -193,12 +224,16 @@ pub mod linux_uring {
for op_id in pending_ids {
let cancel = opcode::AsyncCancel::new(op_id).build().user_data(0);
if self.ring.submission().is_full() {
let _ = self.ring.submit();
if let Ok(submitted) = self.ring.submit() {
self.queued_sqes = self.queued_sqes.saturating_sub(submitted);
}
}
// SAFETY: AsyncCancel references no caller buffer.
let _ = unsafe { self.ring.submission().push(&cancel) };
}
let _ = self.ring.submit();
if let Ok(submitted) = self.ring.submit() {
self.queued_sqes = self.queued_sqes.saturating_sub(submitted);
}
}
let deadline = Instant::now() + timeout;
while !self.expected.is_empty() {
Expand All @@ -215,14 +250,18 @@ pub mod linux_uring {
let ts = types::Timespec::from(remaining);
let args = types::SubmitArgs::new().timespec(&ts);
match self.ring.submitter().submit_with_args(1, &args) {
Ok(_) => {}
Ok(submitted) => {
self.queued_sqes = self.queued_sqes.saturating_sub(submitted);
}
Err(err) if err.raw_os_error() == Some(libc::ETIME) => {
let _ = self.poll_completions(out);
return self.expected.is_empty();
}
Err(err) if err.raw_os_error() == Some(libc::EINTR) => continue,
Err(_) => {
let _ = self.ring.submit();
if let Ok(submitted) = self.ring.submit() {
self.queued_sqes = self.queued_sqes.saturating_sub(submitted);
}
let _ = self.poll_completions(out);
if self.expected.is_empty() {
break;
Expand Down Expand Up @@ -342,7 +381,9 @@ mod tests {
.submit_read(7, 0, 0, buf.as_mut_ptr(), 64)
.expect("submit pipe read");
}
assert_eq!(pipe_backend.pending_submissions(), 1);
pipe_backend.flush().expect("flush pipe read");
assert_eq!(pipe_backend.pending_submissions(), 0);
let mut out: Vec<IoCompletion> = Vec::new();
let start = Instant::now();
// Cancelable pipe read must drain promptly and return true.
Expand Down Expand Up @@ -376,4 +417,57 @@ mod tests {
assert_eq!(stuck.in_flight(), 1);
drop(w2);
}

#[test]
fn flush_queued_sqes_retries_eintr_handles_partial_submit_and_preserves_unsubmitted_count() {
use super::linux_uring::flush_queued_sqes;
use std::collections::VecDeque;

// EINTR retries and partial submissions drain all queued SQEs cleanly.
let mut queued = 3usize;
let mut responses = VecDeque::from([
Err(std::io::Error::from_raw_os_error(libc::EINTR)),
Ok(1),
Err(std::io::Error::from_raw_os_error(libc::EINTR)),
Ok(2),
]);
flush_queued_sqes(&mut queued, || responses.pop_front().unwrap()).expect("flush succeeds");
assert_eq!(queued, 0);
assert!(responses.is_empty());

// Returning Ok(0) stops the inner loop without spinning while retaining queued > 0
// so the worker retries flushing on the next iteration.
let mut queued = 2usize;
let mut calls = 0usize;
flush_queued_sqes(&mut queued, || {
calls += 1;
Ok(0)
})
.expect("Ok(0) returns cleanly");
assert_eq!(calls, 1);
assert_eq!(queued, 2);

// Non-EINTR errors propagate immediately while keeping unsubmitted SQEs accounted for.
let mut queued = 2usize;
let mut responses = VecDeque::from([
Ok(1),
Err(std::io::Error::from_raw_os_error(libc::EBUSY)),
]);
let err = flush_queued_sqes(&mut queued, || responses.pop_front().unwrap())
.expect_err("EBUSY propagates");
assert_eq!(err.raw_os_error(), Some(libc::EBUSY));
assert_eq!(queued, 1);

// Consecutive EINTR retries are bounded so the worker loop still polls shutdown state.
let mut queued = 2usize;
let mut eintr_calls = 0usize;
let err = flush_queued_sqes(&mut queued, || {
eintr_calls += 1;
Err(std::io::Error::from_raw_os_error(libc::EINTR))
})
.expect_err("bounded EINTR retry returns EINTR");
assert_eq!(err.raw_os_error(), Some(libc::EINTR));
assert_eq!(eintr_calls, 16);
assert_eq!(queued, 2);
}
}
Loading
Loading