Skip to content

Commit 293334a

Browse files
javieratomicclaude
andcommitted
Hash Engram planes on parallel readers when assembling a core
When a core is assembled, `core_engram` resolves each Engram plane's payload source in turn, and `payload_source` hashes each plane, or copies and hashes it when it spans several files, with one synchronous reader. On DeepSeek V4.1 Flash the two weight planes are about 98 GB each, so "Assemble core payloads" spends most of its time hashing one plane at a time while one reader at a time cannot keep the NVMe device busy. Before this change `core_engram` validated each table and resolved its planes one after another. Now it first validates every table and lists its planes (shared weights and scales, or each shard's weights and scales) with the same checks as before; the new `plane_sources` then resolves those planes with `payload_source` on up to eight workers, largest first, stops scheduling at the first error and returns it once running planes finish. The sources come back in declared order, and the tables, shards and installed payloads are built from them in the same order as before, so the published layout does not change. Measured result, on an earlier base: on a TP3 export on three DGX Spark GB10 hosts, "Assemble package payloads" (now "Assemble core payloads") took 169.1 s instead of 262.0 s. Evidence: helios `cargo test --release --features server,runtime-core --lib`: 1334 passed, 0 failed, 26 ignored. heliopause `cargo test --release`: 190 passed, 0 failed, 3 ignored, including `core_engram_preserves_shared_and_partitioned_plane_layouts`. The new test `plane_sources_keep_declared_order_and_report_failures` resolves three planes of different sizes, one of them spanning two source files, checks that they come back in declared order with the right identities and sizes, and checks that a plane beyond the payload fails. The measurement used full exports with the same model and recipe flags, one per configuration, on builds based on `a161b17f` that also contained earlier versions of NVIDIA#122, NVIDIA#124 and NVIDIA#125 and the local checkpoint reading proposed in NVIDIA#126, none of which change this phase in those versions; the export completed and committed the core, including the data of all three ranks. This model has four planes, so four readers run at once; the other two planes are about 3 GB each, so most of the remaining time is the two weight planes, each still hashed by one reader. The change was not measured on `bbad44fe`, nor together with the read-ahead digest of NVIDIA#124 (`50c664a3`), which also shortens this phase. Design-Signed-Off-By: Claude Opus 5.5; design-review=cfcf Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Javier López <59578422+javieratomic@users.noreply.github.com>
1 parent bbad44f commit 293334a

1 file changed

Lines changed: 155 additions & 42 deletions

File tree

‎heliopause/src/export/core.rs‎

Lines changed: 155 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,66 @@ pub(crate) fn publish_payload(
10841084

10851085
type Materialized = (helios_core::core::PayloadId, u64, PathBuf);
10861086

1087+
const MAX_PLANE_WORKERS: usize = 8;
1088+
1089+
/// Resolve each `(span, staging name)` plane with [`payload_source`] on parallel readers, largest
1090+
/// first. Each plane is hashed by one synchronous reader, so hashing the planes one at a time
1091+
/// leaves the device mostly idle. Returns the sources in the order of `planes`; the first error
1092+
/// stops scheduling and is returned once running planes finish.
1093+
fn plane_sources(
1094+
payload: &[FilePart],
1095+
planes: &[(&wire::Span, String)],
1096+
staging: &Path,
1097+
) -> Result<
1098+
Vec<(
1099+
helios_core::core::PayloadId,
1100+
u64,
1101+
helios_core::core::container::Slice,
1102+
)>,
1103+
> {
1104+
use std::sync::Mutex;
1105+
use std::sync::atomic::{AtomicUsize, Ordering};
1106+
1107+
let workers = std::thread::available_parallelism()
1108+
.map_or(1, |workers| workers.get())
1109+
.min(MAX_PLANE_WORKERS)
1110+
.min(planes.len());
1111+
let mut order: Vec<usize> = (0..planes.len()).collect();
1112+
order.sort_by_key(|&index| std::cmp::Reverse(planes[index].0.nbytes));
1113+
let cursor = AtomicUsize::new(0);
1114+
let failure = Mutex::new(None);
1115+
let resolved = Mutex::new((0..planes.len()).map(|_| None).collect::<Vec<_>>());
1116+
std::thread::scope(|scope| {
1117+
for _ in 0..workers {
1118+
scope.spawn(|| loop {
1119+
if failure.lock().unwrap().is_some() {
1120+
break;
1121+
}
1122+
let Some(&index) = order.get(cursor.fetch_add(1, Ordering::Relaxed)) else {
1123+
break;
1124+
};
1125+
let (span, name) = &planes[index];
1126+
match payload_source(payload, span, staging, name) {
1127+
Ok(source) => resolved.lock().unwrap()[index] = Some(source),
1128+
Err(error) => {
1129+
failure.lock().unwrap().get_or_insert(error);
1130+
break;
1131+
}
1132+
}
1133+
});
1134+
}
1135+
});
1136+
if let Some(error) = failure.into_inner().unwrap() {
1137+
return Err(error);
1138+
}
1139+
Ok(resolved
1140+
.into_inner()
1141+
.unwrap()
1142+
.into_iter()
1143+
.map(|source| source.expect("every plane resolved"))
1144+
.collect())
1145+
}
1146+
10871147
fn payload_source(
10881148
payload: &[FilePart],
10891149
span: &wire::Span,
@@ -1352,27 +1412,62 @@ fn core_engram(
13521412
}),
13531413
role: pwire::PayloadRole::Weights as i32,
13541414
};
1355-
let mut tables = Vec::new();
1415+
// Validate every table and list its planes first, so the planes can be hashed together.
1416+
let mut planes = Vec::new();
13561417
for (index, table) in engram.tables.iter().enumerate() {
1357-
let (weights, scales, shards) = if table.shards.is_empty() {
1358-
let weights = payload_source(
1359-
payload,
1418+
if table.shards.is_empty() {
1419+
planes.push((
13601420
table
13611421
.weights
13621422
.as_ref()
13631423
.ok_or_else(|| invalid("engram table lacks weights"))?,
1364-
staging,
1365-
&format!("engram-{index}-w.payload"),
1366-
)?;
1367-
let scales = payload_source(
1368-
payload,
1424+
format!("engram-{index}-w.payload"),
1425+
));
1426+
planes.push((
13691427
table
13701428
.scales
13711429
.as_ref()
13721430
.ok_or_else(|| invalid("engram table lacks scales"))?,
1373-
staging,
1374-
&format!("engram-{index}-s.payload"),
1375-
)?;
1431+
format!("engram-{index}-s.payload"),
1432+
));
1433+
continue;
1434+
}
1435+
if table.weights.is_some() || table.scales.is_some() {
1436+
return Err(invalid(
1437+
"partitioned engram table cannot also carry shared planes",
1438+
));
1439+
}
1440+
let mut shard_ranks = BTreeSet::new();
1441+
for (position, shard) in table.shards.iter().enumerate() {
1442+
if !shard_ranks.insert(shard.rank) {
1443+
return Err(invalid(format!(
1444+
"engram table repeats shard rank {}",
1445+
shard.rank
1446+
)));
1447+
}
1448+
planes.push((
1449+
shard
1450+
.weights
1451+
.as_ref()
1452+
.ok_or_else(|| invalid("engram shard lacks weights"))?,
1453+
format!("engram-{index}-shard-{position}-w.payload"),
1454+
));
1455+
planes.push((
1456+
shard
1457+
.scales
1458+
.as_ref()
1459+
.ok_or_else(|| invalid("engram shard lacks scales"))?,
1460+
format!("engram-{index}-shard-{position}-s.payload"),
1461+
));
1462+
}
1463+
}
1464+
let mut sources = plane_sources(payload, &planes, staging)?.into_iter();
1465+
let mut next = || sources.next().expect("one source per listed plane");
1466+
let mut tables = Vec::new();
1467+
for (index, table) in engram.tables.iter().enumerate() {
1468+
let (weights, scales, shards) = if table.shards.is_empty() {
1469+
let weights = next();
1470+
let scales = next();
13761471
for (plane, materialized) in [("weights", &weights), ("scales", &scales)] {
13771472
installed.push(helios_core::core::export::PublishPayload {
13781473
role: pwire::PayloadRole::Weights,
@@ -1388,38 +1483,10 @@ fn core_engram(
13881483
Vec::new(),
13891484
)
13901485
} else {
1391-
if table.weights.is_some() || table.scales.is_some() {
1392-
return Err(invalid(
1393-
"partitioned engram table cannot also carry shared planes",
1394-
));
1395-
}
13961486
let mut shards = Vec::with_capacity(table.shards.len());
1397-
let mut shard_ranks = BTreeSet::new();
13981487
for (position, shard) in table.shards.iter().enumerate() {
1399-
if !shard_ranks.insert(shard.rank) {
1400-
return Err(invalid(format!(
1401-
"engram table repeats shard rank {}",
1402-
shard.rank
1403-
)));
1404-
}
1405-
let weights = payload_source(
1406-
payload,
1407-
shard
1408-
.weights
1409-
.as_ref()
1410-
.ok_or_else(|| invalid("engram shard lacks weights"))?,
1411-
staging,
1412-
&format!("engram-{index}-shard-{position}-w.payload"),
1413-
)?;
1414-
let scales = payload_source(
1415-
payload,
1416-
shard
1417-
.scales
1418-
.as_ref()
1419-
.ok_or_else(|| invalid("engram shard lacks scales"))?,
1420-
staging,
1421-
&format!("engram-{index}-shard-{position}-s.payload"),
1422-
)?;
1488+
let weights = next();
1489+
let scales = next();
14231490
for (plane, materialized) in [("weights", &weights), ("scales", &scales)] {
14241491
installed.push(helios_core::core::export::PublishPayload {
14251492
role: pwire::PayloadRole::Weights,
@@ -3833,6 +3900,52 @@ mod payload_export_tests {
38333900
assert_eq!(&bytes, b"cdef");
38343901
}
38353902

3903+
#[test]
3904+
fn plane_sources_keep_declared_order_and_report_failures() {
3905+
use helios_core::core::PayloadId;
3906+
let base = std::env::temp_dir().join(format!("flare-plane-sources-{}", std::process::id()));
3907+
let _ = fs::remove_dir_all(&base);
3908+
let staging = base.join("staging");
3909+
fs::create_dir_all(&staging).unwrap();
3910+
let small = vec![0x01; 16];
3911+
let large = vec![0x02; 4096];
3912+
let split_a = vec![0x03; 100];
3913+
let split_b = vec![0x04; 50];
3914+
let mut payload = Vec::new();
3915+
let mut cursor = 0u64;
3916+
for (name, bytes) in [("small", &small), ("large", &large), ("split-a", &split_a), ("split-b", &split_b)] {
3917+
let path = base.join(name);
3918+
fs::write(&path, bytes).unwrap();
3919+
payload.push(FilePart {
3920+
offset: cursor,
3921+
source: FileSource { path, offset: 0, nbytes: bytes.len() as u64 },
3922+
});
3923+
cursor += bytes.len() as u64;
3924+
}
3925+
let span = |offset, nbytes| wire::Span { offset, nbytes, filename: String::new() };
3926+
let small_span = span(0, 16);
3927+
let large_span = span(16, 4096);
3928+
// Crosses two source files, so it is materialized into staging rather than pinned.
3929+
let split_span = span(16 + 4096, 150);
3930+
let planes = vec![
3931+
(&small_span, "small.payload".to_string()),
3932+
(&split_span, "split.payload".to_string()),
3933+
(&large_span, "large.payload".to_string()),
3934+
];
3935+
// Planes are scheduled largest first but returned in declared order.
3936+
let sources = plane_sources(&payload, &planes, &staging).unwrap();
3937+
let split = [split_a, split_b].concat();
3938+
assert_eq!(sources.len(), 3);
3939+
for ((id, nbytes, _), bytes) in sources.iter().zip([&small, &split, &large]) {
3940+
assert_eq!(*id, PayloadId::of(bytes));
3941+
assert_eq!(*nbytes, bytes.len() as u64);
3942+
}
3943+
let beyond = span(cursor, 8);
3944+
let planes = vec![(&large_span, "a.payload".to_string()), (&beyond, "b.payload".to_string())];
3945+
assert!(plane_sources(&payload, &planes, &staging).is_err());
3946+
let _ = fs::remove_dir_all(&base);
3947+
}
3948+
38363949
#[test]
38373950
fn core_engram_preserves_shared_and_partitioned_plane_layouts() {
38383951
use helios_core::core::schema::wire as pwire;

0 commit comments

Comments
 (0)