Skip to content
Draft
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: 2 additions & 0 deletions helios/src/runtime_core/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub const CU_STREAM_WRITE_VALUE_DEFAULT: c_uint = 0x0;
pub const CU_MEMHOSTREGISTER_PORTABLE: c_uint = 0x1;
/// `CU_MEMHOSTREGISTER_DEVICEMAP`: map registered host memory into the device address space.
pub const CU_MEMHOSTREGISTER_DEVICEMAP: c_uint = 0x2;
/// `CUDA_ERROR_INVALID_VALUE`: parameter lies outside the device or driver limit.
pub const CUDA_ERROR_INVALID_VALUE: c_int = 1;
/// `CUDA_ERROR_NOT_READY`: asynchronous stream or event operation has not completed.
pub const CUDA_ERROR_NOT_READY: c_int = 600;
/// `CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE`: primary context flags were already fixed by an active user.
Expand Down
210 changes: 204 additions & 6 deletions helios/src/runtime_core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,40 @@ impl NativeResources {
Ok(data)
}

fn trimmed_reservation_bytes<'a>(
reservation: &format::Reservation,
allocations: impl IntoIterator<Item = &'a format::Allocation>,
granularity: usize,
) -> Option<usize> {
let start = reservation.address;
let full_end = start.checked_add(reservation.nbytes as u64)?;
let mut required_end = start;
for allocation in allocations {
if matches!(allocation.memory, Memory::Device)
&& let Some(addr) = allocation.address
&& (start..full_end).contains(&addr)
{
required_end = required_end.max(addr.checked_add(allocation.extent() as u64)?);
}
}
let used = usize::try_from(required_end - start).ok()?;
let aligned = used.checked_next_multiple_of(granularity)?;
(aligned > 0 && aligned < reservation.nbytes).then_some(aligned)
}

unsafe fn reserve(&mut self, core: &Core) -> Result<()> {
unsafe { self.reserve_for(std::slice::from_ref(core)) }
}

unsafe fn reserve_for(&mut self, cores: &[Core]) -> Result<()> {
let Some(first) = cores.first() else {
return Ok(());
};
let driver = &self.driver;
unsafe {
driver.check("cuCtxSetCurrent", (driver.context_set)(self.context))?;
let bits = address_space::virtual_address_bits(self.device);
for reservation in &core.reservations {
for reservation in &first.reservations {
address_space::check_fits(
"core reservation",
reservation.address,
Expand All @@ -524,11 +552,37 @@ impl NativeResources {
)
.map_err(|error| invalid(format!("{error}; re-export the core for this GPU")))?;
let mut address = 0;
driver.check(
"cuMemAddressReserve",
(driver.reserve)(&mut address, reservation.nbytes, 0, reservation.address, 0),
)?;
self.reservations.push((address, reservation.nbytes));
let mut reserved_bytes = reservation.nbytes;
let mut status =
(driver.reserve)(&mut address, reserved_bytes, 0, reservation.address, 0);
// When GiB-rounded trailing slack overhangs a tighter GPU VMM ceiling,
// retry trimmed to the granule-aligned extent of all declared device allocations.
if status == driver::CUDA_ERROR_INVALID_VALUE {
let properties = AllocationProperties {
kind: 1,
handles: 0,
location: Location {
kind: 1,
device: self.device,
},
windows: null_mut(),
flags: [0; 8],
};
let mut granularity = 0usize;
if (driver.granularity)(&mut granularity, &properties, 0) == 0
&& let Some(trimmed) = Self::trimmed_reservation_bytes(
reservation,
cores.iter().flat_map(|core| core.allocations.iter()),
granularity,
)
&& (driver.reserve)(&mut address, trimmed, 0, reservation.address, 0) == 0
{
reserved_bytes = trimmed;
status = 0;
}
}
driver.check("cuMemAddressReserve", status)?;
self.reservations.push((address, reserved_bytes));
if address != reservation.address {
return Err(invalid(format!(
"CUDA address collision: requested {:#x}, reserved {address:#x}",
Expand Down Expand Up @@ -1936,3 +1990,147 @@ unsafe fn apply_context_settings(
configured
}
}

#[cfg(test)]
mod tests {
use super::*;
use format::{Allocation, Reservation};
use protobuf::wire::Sizing;

fn allocation(
name: &str,
address: Option<u64>,
nbytes: usize,
memory: Memory,
sizing: Option<Sizing>,
) -> Allocation {
Allocation {
name: name.into(),
nbytes,
alignment: 256,
file: None,
data_spans: vec![],
sources: vec![],
relocations: vec![],
address,
memory,
sizing,
}
}

#[test]
fn trimmed_reservation_bytes_rounds_highest_device_and_runtime_extent_to_granularity() {
const MIB: usize = 1 << 20;
let reservation = Reservation {
address: 0x10_0000_0000,
nbytes: 1024 * MIB,
};
let allocations = [
allocation("weights", Some(0x10_0000_0000), 64 * MIB, Memory::Device, None),
allocation(
"state_pool",
Some(0x10_0000_0000 + (64 * MIB) as u64),
0,
Memory::Device,
Some(Sizing {
capacity: 0,
unit_bytes: 300,
maximum: 100,
}),
),
// MappedHost, Peer, unaddressed, and out-of-range allocations must be ignored.
allocation("mailbox", Some(0x10_0000_0000 + (900 * MIB) as u64), 4096, Memory::MappedHost, None),
allocation("peer", Some(0x10_0000_0000 + (950 * MIB) as u64), 4 * MIB, Memory::Peer, None),
allocation("floating", None, 4 * MIB, Memory::Device, None),
allocation("before_window", Some(0x0f_f000_0000), 16 * MIB, Memory::Device, None),
allocation("at_window_end", Some(0x10_0000_0000 + (1024 * MIB) as u64), 16 * MIB, Memory::Device, None),
allocation("other_window", Some(0x20_0000_0000), 16 * MIB, Memory::Device, None),
];

assert_eq!(
NativeResources::trimmed_reservation_bytes(&reservation, &allocations, 2 * MIB),
Some(66 * MIB)
);
assert_eq!(
NativeResources::trimmed_reservation_bytes(&reservation, &allocations, 64 * MIB),
Some(128 * MIB)
);
}

#[test]
fn trimmed_reservation_bytes_preserves_allocations_across_all_ranks() {
const MIB: usize = 1 << 20;
let reservation = Reservation {
address: 0x10_0000_0000,
nbytes: 1024 * MIB,
};
let rank0 = [allocation(
"rank0_weights",
Some(0x10_0000_0000),
128 * MIB,
Memory::Device,
None,
)];
let rank1 = [allocation(
"rank1_weights",
Some(0x10_0000_0000 + (512 * MIB) as u64),
129 * MIB,
Memory::Device,
None,
)];

assert_eq!(
NativeResources::trimmed_reservation_bytes(
&reservation,
rank0.iter().chain(rank1.iter()),
2 * MIB,
),
Some(642 * MIB)
);
}

#[test]
fn trimmed_reservation_bytes_rejects_zero_granularity_empty_full_and_overflow() {
const MIB: usize = 1 << 20;
let reservation = Reservation {
address: 0x10_0000_0000,
nbytes: 64 * MIB,
};
let partial = [allocation(
"weights",
Some(0x10_0000_0000),
32 * MIB,
Memory::Device,
None,
)];
let full = [allocation(
"weights",
Some(0x10_0000_0000),
63 * MIB,
Memory::Device,
None,
)];

assert_eq!(
NativeResources::trimmed_reservation_bytes(&reservation, &partial, 0),
None
);
assert_eq!(
NativeResources::trimmed_reservation_bytes(&reservation, &[], 2 * MIB),
None
);
assert_eq!(
NativeResources::trimmed_reservation_bytes(&reservation, &full, 2 * MIB),
None
);

let overflowing = Reservation {
address: u64::MAX - 4095,
nbytes: 8192,
};
assert_eq!(
NativeResources::trimmed_reservation_bytes(&overflowing, &partial, 2 * MIB),
None
);
}
}
2 changes: 1 addition & 1 deletion helios/src/runtime_core/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ impl NativeModel {
}
let phase = progress.phase("Allocate model memory", Some(gpu_bytes + host_bytes));
// Reservations cover all ranks and must exist before parallel mapping.
model.ranks[0].reserve(&cores[0])?;
model.ranks[0].reserve_for(&cores)?;
model.parallel(&cores, &files, |rank, core, _| {
rank.allocate(core, &phase)
})?;
Expand Down
Loading