Repository navigation
diskgraph: compute offset chunk positions instead of a per-process directory - #93
Conversation
…rectory ShardReader::open scanned every offsets.bin chunk into a heap Vec<OffsetChunk> (24 B per 64 offsets). On a 50B-node index that is ~19 GiB of private memory per sampler process, which capped the number of processes a host could run. Chunks are fixed-size (64 offsets, last one partial), so a chunk's byte position is header_len + chunk_idx * (10 + 63 * delta_bytes). Look it up directly and keep the same per-lookup bounds and range checks. No change to the on-disk format. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Change map
|
There was a problem hiding this comment.
Note
Quiet mode is enabled, so only the most important comments were posted inline. Other review comments are grouped below.
🟡 Other comments: 1
packages/diskgraph/diskgraph-index/src/reader.rs: 1
248-251: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick winReject persisted chunk widths that arithmetic lookup cannot decode.
OffsetFileHeader::delta_chunkis persisted, butlocate_chunkalways usesDELTA_CHUNK(64). A file encoded with another chunk width can passShardReader::open; the previous reader used each stored chunk count and could decode it, while the current reader can compute later chunk positions incorrectly.Suggested fix
if !matches!(header.node_data_len_bytes, 2..=4) { return Err( DiskgraphError::InvalidMetadata("node data len width invalid".into()).into(), ); } + if header.delta_chunk as usize != DELTA_CHUNK { + return Err( + DiskgraphError::InvalidMetadata("offset delta chunk size invalid".into()).into(), + ); + } let edge_count = header.edge_count;Do not add exact mapped-file length rejection as part of this fix. The previous reader allowed truncated files to open, and the truncation tests preserve lookup-time errors.
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository: NVIDIA/kumo-engine/.coderabbit.yaml
- Review profile: QUIET
- Plan: Enterprise
- Run ID:
eb648338-5a6b-4d6b-9fb1-05fb1d2d4cfd
📒 Files selected for processing: 4
packages/diskgraph/diskgraph-index/src/lib.rspackages/diskgraph/diskgraph-index/src/reader.rspackages/diskgraph/diskgraph-index/src/tests_reader.rspackages/diskgraph/diskgraph-pybind/python/tests/test_offset_chunks.py
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.
Victsm
left a comment
There was a problem hiding this comment.
Reviewed the fixed-size offset lookup against the existing writers and legacy reader. No correctness blocker found; 57 Rust index tests passed. The remaining connector CI failure is due to missing restricted BigQuery test identities. Large-scale memory/throughput A/B validation remains outstanding.
## Summary This PR makes **DiskGraph sampling output reproducible**: the same request with the same `random_seed` now returns the same local node ids and the same edge order every time. **Problem.** - Two runs of the same sampling request return the same set of nodes and edges but in a different order, with different local ids. A run cannot be compared row by row with an earlier one, and a regression test cannot pin an exact output. - At 50B (#93), 2 of 7 sampled tables changed row order between two runs of the same build. **Cause.** In each layer, `process_layer` builds its work queue by iterating `SamplingState::type_states`, a `gxhash` map; the fused path's final feature flush iterates the same map. Its hasher is randomly seeded per map instance, so each pooled or fresh `SamplingState` iterates node types in a different order. When more than one node type is on the frontier, as in every layer after the first in a heterogeneous graph, the type processed first gets the lower local ids for the destinations they share, and its edges are emitted first. **Change.** The work queue and the fused path's final feature flush are both processed in node type id order. Nothing else changes. ## Back-compat | Situation | Behaviour | |---|---| | Same request, same seed, repeated | Identical output (previously varied between states). | | Output compared with a run of an older build | Same nodes and edges per seed; local ids and edge order match one of the orders the older build could already produce. | | Single node type on the frontier | Unchanged. | | Fused feature-fetching path | Sampling and the final feature flush in type-id order; fused feature batches arrive in the same order every run. | ## Testing Done - New `repeated_runs_give_identical_output`: two node types that both reach the same destination type in one hop; 16 fresh states per run, fused and non-fused; edges, node records and fused feature batches must be identical. It fails on `main`, and also fails if only the flush is left unsorted. - On 100M- and 1B-row synthetic indexes, 6 batches of 512 seeds at 2–6 hops gave identical edges, nodes, frontiers and stats across fresh states (part of the stacked PR's real-graph test). - `cargo test -p diskgraph-engine --lib` passes; clippy is clean on the changed code. ## Checklist - [x] No breaking changes. - [x] Unit tests added. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
…uest (#102) Stacked on #101 (`vedaant/dg-per-seed-recording`). ## Summary This PR **runs the per-seed deduplication on the sampler's thread pool** and **resolves seed anchor times once per request**. Together with the previous PR, multi-threaded sampling is 2–3.3× faster than `main` at 3–6 hops, with identical output. **Problem.** After the previous PR, a process with 8 sampler threads still spent most of a 5–6-hop request in serial work, and one per-edge lookup remained in the parallel phase: - With 8 threads at 5 hops, the commit phase took 6.6 s of a 7.3 s run (100M graph, 5,000 seeds), with about 2.3 cores busy. - Every sampled (node, relation) pair looked up its seed's anchor time by hashing the seed type's name string, about 8% of single-thread CPU. **Cause.** The per-seed step only touches one seed's map and edges, but it ran on one thread. The anchor lookup went `seed_type_id → type name → HashMap<String, Vec<i64>>` on every call to `sample_segment_edges`. **Change.** - `commit_edges` splits the seeds into contiguous ranges of roughly equal edge counts (4 per thread) and runs the deduplication and the final map update on the engine's pool. Local id assignment and edge output remain one in-order pass, so output is unchanged. The parallel path runs only when the pool has more than one thread and the layer has at least `DiskGraphEngine::parallel_commit_min_edges` edges (default 32,768); single-thread processes run the same code inline. The default comes from forcing the threshold to 1 and to infinity at 8 threads: layers of a few thousand to ~25k edges ran 10–50% slower on the pool, layers of 100k+ edges ran faster. Cuts fall on seed boundaries, so ranges are uneven; 4 ranges per thread let the pool balance them. - The in-order pass keeps each destination type's state at hand instead of looking it up in `type_states` for every new node. - `sample_with_cutoff` resolves each seed type's anchor slice once; `sample_segment_edges` takes that slice table instead of the request. ## Back-compat | Situation | Behaviour | |---|---| | Standalone sampling, any thread count | Same local ids, edges, frontiers and stats as the previous path (tested at 4 threads). | | 1-thread process | No parallel dispatch; same code inline. | | Fused feature-fetching sampling | Anchor change only; output unchanged. | | `legacy_recording = true` | Previous path. | | `parallel_commit_min_edges` | New public engine field; only changes when the parallel dedup runs, never the output. | ## Measured results Same indexes, workload, ctxbench-style driver and machine as the previous PR. Sampling seeds/s, better of two interleaved rounds: | graph | threads | hops | `main` | previous PR | this PR | this PR / `main` | with feature fetch, `main` → this PR | cores busy, `main` → this PR | p99 per batch, `main` → this PR | |---|---|---|---|---|---|---|---|---|---| | 100M | 1 | 2 | 36,496 | 31,250 | 35,211 | 0.96× | 18,587 → 17,794 | 1.0 → 1.0 | 30 → 29 ms | | 100M | 1 | 3 | 7,899 | 9,960 | 10,846 | 1.37× | 5,123 → 6,090 | 1.0 → 1.0 | 107 → 85 ms | | 100M | 1 | 4 | 2,836 | 3,213 | 4,340 | 1.53× | 2,094 → 2,769 | 1.0 → 1.0 | 250 → 191 ms | | 100M | 1 | 5 | 331 | 578 | 648 | 1.96× | 270 → 435 | 1.0 → 1.0 | 1.95 → 1.23 s | | 100M | 1 | 6 | 154 | 226 | 269 | 1.74× | 138 → 222 | 1.0 → 1.0 | 3.82 → 2.44 s | | 100M | 8 | 2 | 51,546 | 53,191 | 56,180 | 1.09× | 27,778 → 28,902 | 3.1 → 3.2 | 19 → 18 ms | | 100M | 8 | 3 | 9,804 | 12,987 | 19,685 | 2.01× | 7,728 → 13,021 | 2.5 → 3.2 | 67 → 41 ms | | 100M | 8 | 4 | 4,444 | 5,359 | 8,865 | 1.99× | 3,771 → 6,831 | 2.6 → 3.5 | 139 → 82 ms | | 100M | 8 | 5 | 421 | 704 | 1,268 | 3.01× | 393 → 1,041 | 1.8 → 3.2 | 1.34 → 0.52 s | | 100M | 8 | 6 | 235 | 397 | 630 | 2.68× | 225 → 563 | 1.9 → 3.1 | 2.38 → 1.00 s | | 1B | 1 | 2 | 30,675 | 27,473 | 30,303 | 0.99× | 15,385 → 15,773 | 1.0 → 1.0 | 34 → 33 ms | | 1B | 1 | 3 | 6,821 | 8,913 | 9,980 | 1.46× | 4,355 → 5,556 | 1.0 → 1.0 | 125 → 99 ms | | 1B | 1 | 4 | 2,900 | 3,183 | 3,843 | 1.33× | 2,033 → 2,449 | 1.0 → 1.0 | 266 → 224 ms | | 1B | 1 | 5 | 327 | 554 | 612 | 1.87× | 262 → 418 | 1.0 → 1.0 | 2.12 → 1.33 s | | 1B | 1 | 6 | 162 | 236 | 282 | 1.74× | 142 → 228 | 1.0 → 1.0 | 3.85 → 2.39 s | | 1B | 8 | 2 | 48,544 | 48,077 | 53,191 | 1.10× | 25,253 → 26,455 | 3.7 → 3.8 | 21 → 20 ms | | 1B | 8 | 3 | 9,634 | 13,587 | 19,455 | 2.02× | 7,396 → 12,255 | 2.8 → 3.6 | 73 → 46 ms | | 1B | 8 | 4 | 4,651 | 5,525 | 8,000 | 1.72× | 3,975 → 5,995 | 2.5 → 3.8 | 133 → 89 ms | | 1B | 8 | 5 | 402 | 716 | 1,334 | 3.32× | 374 → 1,066 | 1.8 → 3.3 | 1.42 → 0.51 s | | 1B | 8 | 6 | 233 | 407 | 680 | 2.91× | 223 → 593 | 1.9 → 3.5 | 2.39 → 0.92 s | Four concurrent processes of 8 threads each (aggregate sampling seeds/s; a scaled-down version of the 16×8 layout used on large hosts, limited by this shared machine): | graph | hops | `main` | this PR | × | with feature fetch × | cores busy, `main` → this PR | |---|---|---|---|---|---|---| | 100M | 2 | 197,466 | 241,907 | 1.23× | 1.14× | 12.5 → 12.6 | | 100M | 3 | 39,897 | 74,313 | 1.86× | 1.60× | 9.6 → 12.7 | | 100M | 4 | 17,195 | 31,178 | 1.81× | 1.62× | 9.7 → 14.1 | | 100M | 5 | 1,409 | 4,261 | 3.02× | 2.62× | 6.4 → 10.9 | | 100M | 6 | 877 | 2,338 | 2.67× | 2.48× | 7.0 → 12.5 | | 1B | 2 | 191,409 | 218,250 | 1.14× | 1.06× | 13.7 → 14.0 | | 1B | 3 | 38,287 | 74,560 | 1.95× | 1.58× | 10.3 → 14.2 | | 1B | 4 | 17,512 | 33,131 | 1.89× | 1.64× | 10.1 → 15.2 | | 1B | 5 | 1,349 | 4,406 | 3.27× | 2.82× | 6.7 → 13.1 | | 1B | 6 | 748 | 2,218 | 2.96× | 2.72× | 7.1 → 13.7 | Peak RSS per process rises by 0.0–0.3 GB over `main`. ### Training path (`sample_runtime_batch`, link prediction) Same setup as the training-path table in #101: `sample_runtime_batch` with a fresh `SamplingState` per batch, 1B index, two-tower link-prediction stage table, b=512 with one engine negative per row (three seed groups of 512), features skipped unless noted. Per-batch time, previous path (`legacy_recording = true`) → this PR, better of three interleaved rounds of 4 batches; identical node counts in every cell. | threads | 2 hops | 3 hops | 4 hops | 5 hops | 6 hops | |---|---|---|---|---|---| | 2 | 96 → 87 ms (1.10×) | 714 → 470 ms (1.52×) | 1.64 → 1.11 s (1.48×) | 7.23 → 3.90 s (1.86×) | 16.5 → 9.31 s (1.78×) | | 8 | 79 → 67 ms (1.17×) | 676 → 371 ms (1.82×) | 1.46 → 0.88 s (1.66×) | 6.74 → 3.01 s (2.24×) | 14.9 → 7.02 s (2.12×) | With feature fetch for every sampled node (8 threads): 114 → 101 ms (1.13×) at 2 hops, 1.89 → 1.28 s (1.48×) at 4 hops. ### 50B index | Hops | 2 | 3 | 4 | 5 | 6 | |---|---|---|---|---|---| | seeds/s (best config, #102) | 392k | 169k | 98k | 13.8k | 9.3k | | vs main (best config) | 1.10× | 1.16× | 1.19× | 1.6× | 1.4× | 50B commerce, i7ie.48xlarge, warm, b=512, numa_balancing=0, identical row counts. Same published synthetic graph as #93 (49,997M rows, 7 node types × 171 shards, 2.6 TB index). Machine: 192 vCPU, 1.5 TiB RAM, local NVMe, `kernel.numa_balancing=0`. Harness: P processes × T threads, sampling plus feature fetch for every sampled node, b=512, warm (a warm-up pass, then the same seeds measured). `main` and this PR return identical row counts in every cell. Seeds/s, `main` → this PR: | hops | 64×3 | 16×8 | 96×2 | |---|---|---|---| | 2 | 273,570 → 374,560 (1.37×)¹ | 200,983 → 219,036 (1.09×) | 356,736 → 392,243 (1.10×) | | 3 | 118,877 → 161,784 (1.36×) | 61,169 → 84,639 (1.38×) | 146,194 → 169,197 (1.16×) | | 4 | 70,050 → 92,251 (1.32×) | 29,632 → 44,783 (1.51×) | 81,754 → 97,583 (1.19×) | | 5 | 7,850 → 13,750 (1.75×) | 3,619 → 6,713 (1.86×) | 8,560 → 11,687 (1.37×) | | 6 | 5,729 → 8,740 (1.53×) | 2,114 → 4,146 (1.96×) | 6,777 → 9,319 (1.38×) | Best config per hop: | hops | `main` | this PR | #93 run, b=512 | #93 run, best config | DuckDB 2.0 native 8×16 / 8×24 | |---|---|---|---|---|---| | 2 | 356,736 | 392,243 | 387,683 (64×3) | 421,042 (64×3) | 37,332 / 42,651 | | 3 | 146,194 | 169,197 | —² | 143,935 (96×2) | 24,549 / 27,605 | | 4 | 81,754 | 97,583 | 77,214 (96×2) | 77,316 (96×2) | 15,867 / 15,045 | | 5 | 8,560 | 13,750 | —² | 10,919 (96×2) | 4,184 / 5,002 | | 6 | 6,777 | 9,319 | 6,774 (96×2) | 6,582 (96×2) | 3,020 / 3,138 | ¹ `main`'s measured pass still read 6.7 GB from disk in this cell, so part of the gain is warm-up; 96×2 is the cleaner 2-hop comparison. ² The #93 run measured b=512 only at 2, 4 and 6 hops. Its best-config column is the 192-vCPU row of #93's table, at batch sizes tuned per hop. At 6 hops, p99 per batch at 16×8 drops from 4.2 s to 2.1 s. Each cell above was measured once; repeated runs on this machine type have shown about 15% run-to-run spread, so the 1.10–1.19× best-config gains at 2–4 hops are within noise; the 1.4–1.6× gains at 5–6 hops are clearly real. The gains are smaller than on the 64-vCPU machine because the best 50B configs run many processes with 2–3 threads each: there the commit phase is a smaller share and page faults dominate. The 16×8 layout, where commit work is concentrated in fewer processes, gains 1.4–2.0× at 3–6 hops. ## Known limitations - One seed that holds most of a layer's edges (a hub seed) lands in a single range, so its dedup runs on one thread and caps the parallel speedup for that request. - With 8 threads at 5–6 hops, the remaining serial work is the in-order id assignment and edge output (about 60% of commit time) and the counting sort (about 20%). ## Testing Done - Both equivalence tests from #101 also run with `parallel_commit_min_edges = 1`, so at 4 threads every multi-seed layer takes the parallel branch, and compare that run with the previous path. - `tests/per_seed_recording_real_graph.rs` (4 threads) passes on both indexes at 2–6 hops. - `cargo test -p diskgraph-engine`, `-p diskgraph-ingest --test seed_table_ingest --test resolution_runtime_batch` and `-p diskgraph-pybind` pass; `cargo clippy --workspace --all-targets -- -D warnings` is clean. ## Checklist - [x] No breaking changes. - [x] Unit tests added. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
This PR removes the per-process offset chunk directory from DiskGraph's
ShardReader. Each sampler process used to build a private heap copy of it; on a 50B-node index that copy was ~19 GiB per process.Problem. On large indexes, the number of sampler processes a host can run is limited by memory, not by cores:
SamplerEngineprocesses fit on one host, and can sampling use all of its cores?On a 50B-node index (7 node types × 171 shards, 101 GiB of
offsets.bin), each process held ~20 GiB of anonymous memory after open. On a 128-vCPU / 991 GiB host, 32 processes ran out of memory at 2 hops. Threads inside one process do not add throughput past ~1.5 busy cores, so the host could not be filled.Cause.
offsets.binstores node offsets in delta-packed chunks:[u16 count][u64 base][(count-1) × delta_bytes], 64 offsets per chunk.ShardReader::openscanned every chunk and pushed anOffsetChunk { base_idx: u64, count: u16, data_offset: usize }(24 B) into aVecper shard, to find a node's chunk later. That is one 24-byte entry per 64 nodes, or 50B / 64 × 24 B ≈ 18.7 GB per process, and every process built its own copy. Open also read every page ofoffsets.binto build it.Change. The writer always emits full 64-offset chunks; only the last one can be partial. The reader already relied on this (it indexed the directory with
idx / 64). A chunk's byte position is therefore fixed:decode_offsetnow computes the position instead of looking it up, then decodes exactly as before. The directory, its build loop and its memory are gone. Open no longer scansoffsets.bin.The per-lookup checks are kept: a chunk past the end of the file is still
offset chunk missing, an index outside the chunk's storedcountis stilloffset chunk range mismatch, and every delta read is still bounds-checked.No file changes on disk: same
offsets.bin, same layout version, no new files, nothing new in_MANIFEST.json, no re-ingest.Alternatives considered
Vec<OffsetChunk>u32/u64positions only)offsets.chunksfile, mmapped and sharedBack-compat
offsets.binoffsets.binwhose chunks aren't 64 offsets (no DiskGraph writer produces this)count, but no longer against a start index found by scanning.Measured results
Index: a synthetic relational graph with 50B nodes in 7 node types, 171 shards per type, 2.6 TiB on disk, of which
offsets.binis 101 GiB. Workload: temporal most-recent sampling for 2 to 6 hops plus a feature fetch for every sampled node, run byPindependent processes ofTsampler threads each. Old build and this PR are the same commit apart from this change.Machine A: 128 vCPU, 991 GiB RAM, 8 local NVMe SSDs striped (28 TB). Machine B: 192 vCPU, 1.5 TiB RAM, local NVMe.
Per process, after
SamplerEngineopen (machine A)offsets.bin)How many processes fit. With the old build, 32 processes at 2 hops ran the 991 GiB machine out of memory, and 32 processes at 2–4 hops evicted the index from the page cache (10–17 GB re-read per warm pass). Its best configs used 24–32 processes. With this PR, 128 processes ran at 6 hops with ~320 GiB used.
Correctness. 300 seeds at 2 and 6 hops, sampled rows plus fetched keys, old build vs this PR: every table has the same rows. Tables whose row order is stable from run to run (5 of 7) are byte-identical. In the other two the row order differs between old and new, but it differs just as much between two runs of the old build, and between two runs of this PR. That ordering is pre-existing nondeterminism, not this change.
Warm throughput (seeds/s), best config per hop. Each cell is a warm-up pass and then a measured pass over the same seeds; measured passes read 0 bytes from disk. Latency is per batch within one process.
The speedup (1.3× at 2 hops, 1.65–1.8× at 3–6 hops on machine A) comes from running more processes, which memory no longer prevents. Per-batch latency rises because the best configs use fewer threads per process. At 6 hops with batch 64 this PR does 4,302 seeds/s at p50 1.22 s / p99 1.67 s. The old build's config shapes still run with their old latency.
Cold reads. With the page cache dropped, a 1M-seed 2-hop pass reads 300 GB with this PR and 200 GB with the old build. The difference is
offsets.bin: the old build read all of it during open, outside the timed pass. Withoffsets.binpre-read, this PR's cold pass reads 200.4 GB, the same as the old build. The process count does not change it: 16×8 and 64×2 both read 299.6 GB. During cold passes the SSDs serve ~0.75M reads/s at 0.16 ms, with outstanding I/O roughly equal to the number of sampler threads. Cold sampling is bound by synchronous page-fault latency, not by device bandwidth (a 4 KiB random-read benchmark on the same volume reaches 2.3M IOPS).Known limitations
Testing Done
diskgraph-index/src/tests_reader.rs, which keeps the previous directory-based decoder as a reference:offsets.bin(cut inside the header region, inside a chunk header, at a chunk boundary, mid-file, one byte short): the same indexes succeed or fail as with the reference;ShardReader::edges_for_nodereturns the same edge-blob slice as the source offsets for every node, for each width.test_offset_chunks.py: ingests a 1,000-node table (16 chunks, the last one partial) at delta widths 2, 3 and 4, thenfetch_featuresfor every node and checks each row's values.cargo test -p diskgraph-index,-p diskgraph-engine,-p diskgraph-gatewaypass.cargo clippy -p diskgraph-index -p diskgraph-engine --all-targets -D warningsis clean. The full pybind suite against the built manylinux wheel passes (260 passed, 3 skipped).Change map: see the
kumo-pr-change-mapcomment.Checklist
🤖 Generated with Claude Code