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
11 changes: 8 additions & 3 deletions docs/runtime-core-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,12 @@ by CUDA's device total. CPU allocations, registered buffers, and GPU allocations
share physical DRAM; reclaimable page cache contributes to available memory,
while swap does not. Discrete GPUs use CUDA's free device-memory query.
Dedicated rows comprise one null row and the selected request capacity times
the recipe's execution rows per request. KV pages receive the remaining budget,
the recipe's execution rows per request. When one continuation row spans more
than one VMM allocation granule (`row_bytes >= 2 * alignment`), Helios aliases
the `floor(row_bytes / alignment)` full leading granules of null row zero onto
one shared zeroed physical granule while keeping row zero's partial tail and
all live rows uniquely backed, saving `(floor(row_bytes / alignment) - 1) *
alignment` physical bytes per rank. KV pages receive the remaining budget,
within their virtual maximum. The planner accounts for mapping alignment and
requires at least 8,192 cached tokens per active request.

Expand Down Expand Up @@ -836,8 +841,8 @@ as an enclosing page range. Wider bands can exclude more pages while a snapshot
is retained. Both policies keep the planned number of KV pages, snapshot slots
and physical bytes. The RM estimate covers the model's VMM objects; other driver
objects share the same 524,288-handle range, so deployments near the limit
(such as the 518,232-handle 3,103-page DCP16 layout, leaving 6,056 handles /
1.2%) rely on that remaining headroom.
(such as the 518,504-handle 3,103-page DCP16 layout, leaving 5,784 handles /
1.1%) rely on that remaining headroom.

Multi-host cache agreement exchanges page and row counts. Each host uses the
same canonical layout and includes its snapshot conflict ranges in the
Expand Down
4 changes: 2 additions & 2 deletions helios/src/offload/gpu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ mod disk_store {
let layout = Layout::new(&geometry).unwrap();
let fingerprint = fingerprint(&geometry);
let page_tokens = rank.page_tokens;
assert!(rank.page_capacity >= 400 && rank.row_capacity >= 4);
assert!(rank.page_capacity >= 400 && rank.row_capacity >= 5);

const SOURCE: u32 = 100;
const RESTORED: u32 = 200;
Expand All @@ -1475,7 +1475,7 @@ mod disk_store {
let extra_pages = pages(SOURCE + PAGES..SOURCE + PAGES + EXTRA);
let (source_slot, child_slot, restored_slot, again_slot) = (
StateSlotId(1),
StateSlotId(0),
StateSlotId(4),
StateSlotId(2),
StateSlotId(3),
);
Expand Down
38 changes: 34 additions & 4 deletions helios/src/offload/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ pub(crate) fn expand(
}
if let Some((slot, slots)) = &job.row {
let index = u64::from(slot.0);
if index >= geometry.row_capacity {
if index == 0 || index >= geometry.row_capacity {
return Err(invalid(format!(
"state slot {index} is outside the cache's {} rows",
"state slot {index} is outside the cache's live rows 1..{}",
geometry.row_capacity
)));
}
Expand Down Expand Up @@ -1168,6 +1168,29 @@ mod tests {
)
.is_err()
);
let mut null_slot = job(Direction::Out);
null_slot.row = Some((StateSlotId(0), vec![0x30000]));
assert!(
expand(
&ranks[0],
&layout.ranks[0],
None,
&null_slot,
&mut descriptors
)
.is_err()
);
null_slot.direction = Direction::In;
assert!(
expand(
&ranks[0],
&layout.ranks[0],
None,
&null_slot,
&mut descriptors
)
.is_err()
);
let mut null_slab = job(Direction::In);
null_slab.pages[0].1 = 0;
assert!(
Expand Down Expand Up @@ -1692,12 +1715,19 @@ mod tests {
"row {target_slot} differs from row {source_slot}"
);

for (id, slot) in [(capacity, 0), (0, rank.row_capacity as u32)] {
for (index, (id, slot)) in [
(capacity, 1),
(0, 0),
(0, rank.row_capacity as u32),
]
.into_iter()
.enumerate()
{
transfers
.submit(Job {
codec: None,
replication: Default::default(),
id: JobId(3 + u64::from(slot != 0)),
id: JobId(3 + index as u64),
window: 0,
direction: Direction::Out,
pages: vec![(PageId(id), page_slabs[0])],
Expand Down
79 changes: 54 additions & 25 deletions helios/src/offload/transfer/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ fn device_spans(
} else {
geometry.page_capacity
};
if id >= capacity {
if (row && id == 0) || id >= capacity {
return Err(invalid("codec destination id outside cache"));
}
if row {
Expand Down Expand Up @@ -1052,7 +1052,7 @@ mod tests {
let (page_bytes, row_bytes) = (4096usize, 65536usize);
let n_pages = 4usize;
let mut page_mem = stream.mapped_host_buffer(page_bytes * n_pages * 2).unwrap();
let mut row_mem = stream.mapped_host_buffer(row_bytes * 2).unwrap();
let mut row_mem = stream.mapped_host_buffer(row_bytes * 3).unwrap();
let page_addr = stream.mapped_address(&page_mem).unwrap() as u64;
let row_addr = stream.mapped_address(&row_mem).unwrap() as u64;
let mut geometry = CacheGeometry {
Expand All @@ -1074,7 +1074,7 @@ mod tests {
snapshot_pages: Vec::new(),
page_tokens: 64,
page_capacity: (n_pages * 2) as u64,
row_capacity: 2,
row_capacity: 3,
planes: vec![Plane {
name: "kv".into(),
address: page_addr,
Expand All @@ -1087,24 +1087,25 @@ mod tests {
},
};

// Populate source pages 0..n_pages and row 0 in device-visible memory.
// Populate source pages 0..n_pages and row 1 in device-visible memory (row 0 is null).
let src_pages: Vec<Vec<u8>> = (0..n_pages)
.map(|p| (0..page_bytes).map(|i| (((i + p * 17) / 7) % 13) as u8).collect())
.collect();
let mut row0 = vec![0u8; row_bytes];
let mut row1 = vec![0u8; row_bytes];
for c in 0..128 {
for r in 0..128 {
let v = ((r as f32 * 0.01).sin() * (c as f32 * 0.02).cos()).to_bits();
let idx = (c * 128 + r) * 4;
row0[idx..idx + 4].copy_from_slice(&v.to_le_bytes());
row1[idx..idx + 4].copy_from_slice(&v.to_le_bytes());
}
}
for (p, data) in src_pages.iter().enumerate() {
page_mem.bytes_mut()[p * page_bytes..(p + 1) * page_bytes].copy_from_slice(data);
}
row_mem.bytes_mut()[..row_bytes].copy_from_slice(&row0);
row_mem.bytes_mut()[..row_bytes].fill(0);
row_mem.bytes_mut()[row_bytes..row_bytes * 2].copy_from_slice(&row1);
page_mem.bytes_mut()[n_pages * page_bytes..].fill(0xa5);
row_mem.bytes_mut()[row_bytes..].fill(0xa5);
row_mem.bytes_mut()[row_bytes * 2..].fill(0xa5);

let mut transfers = if geometry.placement == KvCompression::Gpu {
let path = std::env::var("FLARE_TEST_KV_CODEC_CUBIN")
Expand All @@ -1128,7 +1129,7 @@ mod tests {
window: 0,
direction: Direction::Out,
pages: (0..n_pages as u32).map(|p| (PageId(p), p as usize)).collect(),
row: Some((StateSlotId(0), vec![0])),
row: Some((StateSlotId(1), vec![0])),
replication: Default::default(),
codec: Some(cap_win.clone()),
})
Expand Down Expand Up @@ -1163,7 +1164,7 @@ mod tests {
assert_eq!(slots[n_pages].unit.representation, Representation::Packet as i32);
assert!(slots[n_pages].bytes.len() < row_bytes);

// 2. Restore packed host -> VRAM pages n_pages..2*n_pages and row 1 (Direction::In)
// 2. Restore packed host -> VRAM pages n_pages..2*n_pages and row 2 (Direction::In)
let restore_win = Window::restore_pinned(
0,
slots
Expand All @@ -1184,7 +1185,7 @@ mod tests {
pages: (0..n_pages as u32)
.map(|p| (PageId(n_pages as u32 + p), p as usize))
.collect(),
row: Some((StateSlotId(1), vec![0])),
row: Some((StateSlotId(2), vec![0])),
replication: Default::default(),
codec: Some(restore_win),
})
Expand All @@ -1202,7 +1203,7 @@ mod tests {
let dst_off = (n_pages + p) * page_bytes;
assert_eq!(&page_mem.bytes()[dst_off..dst_off + page_bytes], data.as_slice());
}
assert_eq!(&row_mem.bytes()[row_bytes..row_bytes * 2], &row0);
assert_eq!(&row_mem.bytes()[row_bytes * 2..row_bytes * 3], &row1);

// 3. Corrupt stored CRC must fail cleanly and count a stored verification failure.
let mut bad_u = slots[0].unit.clone();
Expand Down Expand Up @@ -1296,7 +1297,7 @@ mod tests {
let stream = Stream::open_lowest_priority(context, 0).unwrap();
let (page_bytes, row_bytes) = (4096usize, 65536usize);
let mut page_mem = stream.mapped_host_buffer(page_bytes * 2).unwrap();
let mut row_mem = stream.mapped_host_buffer(row_bytes * 2).unwrap();
let mut row_mem = stream.mapped_host_buffer(row_bytes * 3).unwrap();
let page_addr = stream.mapped_address(&page_mem).unwrap() as u64;
let row_addr = stream.mapped_address(&row_mem).unwrap() as u64;
let mut base_geo = CacheGeometry {
Expand All @@ -1318,7 +1319,7 @@ mod tests {
snapshot_pages: Vec::new(),
page_tokens: 64,
page_capacity: 2,
row_capacity: 2,
row_capacity: 3,
planes: vec![Plane {
name: "kv".into(),
address: page_addr,
Expand All @@ -1331,16 +1332,17 @@ mod tests {
},
};
let page0: Vec<u8> = (0..page_bytes).map(|i| ((i / 7) % 13) as u8).collect();
let mut row0 = vec![0u8; row_bytes];
let mut row1 = vec![0u8; row_bytes];
for c in 0..128 {
for r in 0..128 {
let v = ((r as f32 * 0.01).sin() * (c as f32 * 0.02).cos()).to_bits();
let idx = (c * 128 + r) * 4;
row0[idx..idx + 4].copy_from_slice(&v.to_le_bytes());
row1[idx..idx + 4].copy_from_slice(&v.to_le_bytes());
}
}
page_mem.bytes_mut()[..page_bytes].copy_from_slice(&page0);
row_mem.bytes_mut()[..row_bytes].copy_from_slice(&row0);
row_mem.bytes_mut()[..row_bytes].fill(0);
row_mem.bytes_mut()[row_bytes..row_bytes * 2].copy_from_slice(&row1);

let cpu_profile = flare_selene::shared::codec_layout::profile(
&base_geo,
Expand Down Expand Up @@ -1392,7 +1394,7 @@ mod tests {
window: 0,
direction: Direction::Out,
pages: vec![(PageId(0), 0)],
row: Some((StateSlotId(0), vec![0])),
row: Some((StateSlotId(1), vec![0])),
replication: Default::default(),
codec: Some(cpu_cap.clone()),
},
Expand All @@ -1402,7 +1404,7 @@ mod tests {

// 2. Restore CPU ZKV1 packets using GPU-mode Transfers (decodes on CPU -> VRAM)
page_mem.bytes_mut()[page_bytes..].fill(0xaa);
row_mem.bytes_mut()[row_bytes..].fill(0xaa);
row_mem.bytes_mut()[row_bytes * 2..].fill(0xaa);
let zkv_win = Window::restore_pinned(
0,
cpu_slots
Expand All @@ -1422,13 +1424,13 @@ mod tests {
window: 0,
direction: Direction::In,
pages: vec![(PageId(1), 0)],
row: Some((StateSlotId(1), vec![0])),
row: Some((StateSlotId(2), vec![0])),
replication: Default::default(),
codec: Some(zkv_win),
},
);
assert_eq!(&page_mem.bytes()[page_bytes..], &page0);
assert_eq!(&row_mem.bytes()[row_bytes..], &row0);
assert_eq!(&row_mem.bytes()[row_bytes * 2..], &row1);

// 3. Re-capture in GPU mode (writes GKV1 / gpu_profile)
let gpu_cap = Window::capture(0);
Expand All @@ -1439,7 +1441,7 @@ mod tests {
window: 0,
direction: Direction::Out,
pages: vec![(PageId(1), 0)],
row: Some((StateSlotId(1), vec![0])),
row: Some((StateSlotId(2), vec![0])),
replication: Default::default(),
codec: Some(gpu_cap.clone()),
},
Expand All @@ -1449,7 +1451,7 @@ mod tests {

// 4. CPU-mode Transfers natively restores GKV1/v2 packets on CPU with zero GPU VRAM!
page_mem.bytes_mut()[page_bytes..].fill(0x55);
row_mem.bytes_mut()[row_bytes..].fill(0x55);
row_mem.bytes_mut()[row_bytes * 2..].fill(0x55);
let gkv_win = Window::restore_pinned(
0,
gpu_slots
Expand All @@ -1469,15 +1471,42 @@ mod tests {
window: 0,
direction: Direction::In,
pages: vec![(PageId(1), 0)],
row: Some((StateSlotId(1), vec![0])),
row: Some((StateSlotId(2), vec![0])),
replication: Default::default(),
codec: Some(gkv_win),
},
);
assert_eq!(&page_mem.bytes()[page_bytes..], &page0);
assert_eq!(&row_mem.bytes()[row_bytes..], &row0);
assert_eq!(&row_mem.bytes()[row_bytes * 2..], &row1);

cpu_xfer.close().unwrap();
gpu_xfer.close().unwrap();
}

#[test]
fn device_spans_rejects_null_state_slot_zero() {
let geometry = CacheGeometry {
placement: KvCompression::Cpu,
codec_fields: Vec::new(),
snapshot_pages: Vec::new(),
page_tokens: 64,
page_capacity: 4,
row_capacity: 4,
planes: vec![Plane {
name: "kv".into(),
address: 0x1000,
unit_bytes: 4096,
}],
row: Plane {
name: "state".into(),
address: 0x10000,
unit_bytes: 8192,
},
};
let layout = flare_selene::Layout::new(std::slice::from_ref(&geometry)).unwrap();
assert!(device_spans(&geometry, &layout.ranks[0], false, 0).is_ok());
assert!(device_spans(&geometry, &layout.ranks[0], true, 0).is_err());
assert!(device_spans(&geometry, &layout.ranks[0], true, 1).is_ok());
assert!(device_spans(&geometry, &layout.ranks[0], true, 4).is_err());
}
}
Loading
Loading