bench: world-model sample loading — clawhdf5 reads h5py files 7x faster
CI / test (push) Failing after 3s

than h5py (5e)

stable-worldmodel (arXiv 2605.21800, LeCun/Balestriero) supports HDF5 as
one of three native formats and measures generic HDF5 at 1,416-1,474
samples/s for per-frame sample loading. This measures clawhdf5 against
that shape, hardware-controlled: clawhdf5 and h5py reading the SAME file
on the SAME machine.

worldmodel_sampling example: mmap an (N,H,W,C) uint8 observation dataset,
read each frame once per pass in shuffled (dataloader) order. The file is
written by h5py (benchmarks/gen_worldmodel_frames.py) — clawhdf5 parsing
an externally-produced HDF5 file is itself the interop result — and read
by both clawhdf5 and the h5py counterpart (benchmarks/bench_worldmodel_h5py.py,
opening exactly stable-worldmodel's HDF5Dataset: swmr + 256 MB cache).

Results (tank, Ryzen 7 7800X3D, 20000x64x64x3 = 246 MB, in page cache,
median of 3):

  clawhdf5 zero-copy view        593k samples/sec   8.1x
  clawhdf5 materialised copy     518k samples/sec   7.1x
  h5py (swmr, 256 MB cache)       73k samples/sec   1.0x

The materialised-copy row is the fair equal-work comparison (to_vec per
frame, matching h5py's numpy materialisation) and is still 7.1x faster;
that the copy costs almost nothing shows the gap is h5py's per-frame call
overhead, not data movement. Honest caveats in BENCHMARKS.md: absolute
numbers are NOT comparable to the paper's (different hardware, smaller
frames, no torch/transform), only the same-machine ratio is; this is an
in-page-cache measurement isolating read-path overhead, not disk
bandwidth.

Adds only an example, two benchmark scripts, and a BENCHMARKS.md section —
no library code. (Workspace clippy has pre-existing toolchain drift
unrelated to this change; tracked separately.)
This commit is contained in:
osobh
2026-08-07 22:54:26 -07:00
parent 1537a9464a
commit b2dce41532
5 changed files with 211 additions and 0 deletions
+3
View File
@@ -1,3 +1,6 @@
/target /target
Cargo.lock Cargo.lock
benchmarks/longmemeval/*.json benchmarks/longmemeval/*.json
# Local model weights (MiniLM etc.) — large, not committed
weights/
+48
View File
@@ -544,6 +544,54 @@ No network hop, no serialization — direct HashMap operations.
--- ---
## World-Model Sample Loading (vs h5py / stable-worldmodel shape)
Reproduces the access pattern of `stable-worldmodel`'s HDF5 dataloader
([arXiv 2605.21800](https://arxiv.org/abs/2605.21800), LeCun/Balestriero
group), which supports HDF5 as one of three native formats and measures
generic HDF5 at **1,416-1,474 samples/s** (vs Lance 4,815) for per-frame
sample loading. This benchmark measures **clawhdf5 vs h5py on the same
machine and the same file**, so the comparison is hardware-controlled.
**Absolute numbers are not comparable to the paper's** - different hardware
(AMD Ryzen 7 7800X3D, local NVMe, warm page cache), smaller frames, and no
torch-tensor / transform step. Only the clawhdf5-vs-h5py ratio *here* is a
controlled result. The workload is the dataloader shape: a `(N, H, W, C)`
uint8 observation dataset (20,000 x 64x64x3 = 246 MB), each frame read once
per pass in a fixed shuffled (random-access) order, 10 passes.
Both read a **file written by h5py** - clawhdf5 parsing an
externally-produced HDF5 file is itself the interop result. h5py opens SWMR
with a 256 MB chunk cache, exactly `stable-worldmodel`'s `HDF5Dataset`; it
materialises each frame as a numpy array (`d[i]`) and sums it. clawhdf5
mmaps once, takes a zero-copy `&[u8]` over the contiguous dataset, and
indexes frame `i` as a subslice.
| Reader | samples/sec (median of 3) | vs h5py |
|--------|---------------------------|---------|
| **clawhdf5** (zero-copy view) | **593,000** | **8.1x** |
| **clawhdf5** (materialised copy per frame) | **518,000** | **7.1x** |
| h5py (swmr, 256 MB cache) | 73,000 | 1.0x |
The **materialised-copy row is the fair, equal-work comparison** - it
`to_vec()`s every frame so clawhdf5 pays the same per-frame allocation h5py
does, and it is still **7.1x faster**. That the copy costs almost nothing
(518k vs 593k) shows the h5py gap is **per-frame call overhead** (Python +
library dispatch), not data movement. This is an in-page-cache measurement:
it isolates the read-path overhead both libraries add on top of the OS,
which is the thing that differs - not disk bandwidth, which is shared.
Reproduce (`benchmarks/`):
```bash
python benchmarks/gen_worldmodel_frames.py /tmp/wm_frames.h5 20000
cargo run --release -p clawhdf5-bench --example worldmodel_sampling -- /tmp/wm_frames.h5 10
cargo run --release -p clawhdf5-bench --example worldmodel_sampling -- /tmp/wm_frames.h5 10 --copy
python benchmarks/bench_worldmodel_h5py.py /tmp/wm_frames.h5 10
```
Measured 2026-08-07 on tank (Ryzen 7 7800X3D, 246 MB dataset in page cache).
## Cross-Platform Notes ## Cross-Platform Notes
> **Run:** `./benchmarks/cross_platform.sh [--full] [--output results.json]` > **Run:** `./benchmarks/cross_platform.sh [--full] [--output results.json]`
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""h5py counterpart to worldmodel_sampling.rs — same file, same shuffled
per-frame access, same minimal touch (sum the frame bytes). Reports
samples/sec so the two sit side by side on one machine."""
import sys, time, numpy as np, h5py
path = sys.argv[1]
passes = int(sys.argv[2]) if len(sys.argv) > 2 else 5
def shuffled(n):
v = list(range(n))
state = 0x9E3779B97F4A7C15
for i in range(n - 1, 0, -1):
state = (state * 6364136223846793005 + 1442695040888963407) & 0xFFFFFFFFFFFFFFFF
j = (state >> 33) % (i + 1)
v[i], v[j] = v[j], v[i]
return v
# swmr + a 256 MB chunk cache: exactly stable-worldmodel's HDF5Dataset._open_h5.
f = h5py.File(path, "r", swmr=True, rdcc_nbytes=256 * 1024 * 1024)
d = f["observation"]
n = d.shape[0]
order = shuffled(n)
# warm
sink = 0
for i in order:
sink += int(d[i].sum())
t0 = time.perf_counter()
sink = 0
for _ in range(passes):
for i in order:
sink += int(d[i].sum())
elapsed = time.perf_counter() - t0
total = n * passes
print(f"h5py: {n} frames x {passes} passes = {total} reads in {elapsed:.3f}s")
print(f"h5py: {total/elapsed:.0f} samples/sec")
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
"""Generate a world-model-shaped dataset: N frames of HxWxC uint8 observations,
contiguous (N,H,W,C), matching stable-worldmodel's per-frame sample-loading
access pattern. Also emits ep_len/ep_offset like their format."""
import sys, time, numpy as np, h5py
path = sys.argv[1]
N = int(sys.argv[2]) if len(sys.argv) > 2 else 20000
H = W = 64
C = 3
rng = np.random.default_rng(0)
t0 = time.perf_counter()
with h5py.File(path, "w", libver="latest") as f:
# Contiguous (N,H,W,C) uint8 — the fair, both-APIs-support-it layout.
obs = f.create_dataset("observation", shape=(N, H, W, C), dtype=np.uint8)
# Write in blocks to bound memory.
B = 2000
for i in range(0, N, B):
n = min(B, N - i)
obs[i:i+n] = rng.integers(0, 256, size=(n, H, W, C), dtype=np.uint8)
# Episode metadata like their format: 100-step episodes.
ep = 100
n_ep = N // ep
f.create_dataset("ep_len", data=np.full(n_ep, ep, dtype=np.int32))
f.create_dataset("ep_offset", data=(np.arange(n_ep) * ep).astype(np.int64))
print(f"wrote {N} frames {H}x{W}x{C} to {path} in {time.perf_counter()-t0:.1f}s "
f"({N*H*W*C/1e6:.0f} MB)")
@@ -0,0 +1,95 @@
//! World-model sample-loading benchmark — clawhdf5 vs the h5py counterpart.
//!
//! Reproduces the access pattern of `stable-worldmodel`'s HDF5 dataloader
//! (arXiv 2605.21800): a dataset of `(N, H, W, C)` uint8 observation frames,
//! read one frame at a time in shuffled (dataloader) order. That paper
//! reports generic HDF5 at 1,4161,474 samples/s (vs Lance 4,815); this
//! measures clawhdf5 and h5py on the **same machine and file**, so the
//! comparison is hardware-controlled. Absolute numbers are not comparable to
//! the paper's (different box, smaller frames, no torch/transform) — only
//! clawhdf5-vs-h5py *here* is.
//!
//! clawhdf5 mmaps the file once and takes a zero-copy `&[u8]` over the
//! contiguous observation dataset; frame `i` is a subslice, and the OS pages
//! it in on access. Two modes, because fairness demands both:
//! * default: sum the frame bytes through the zero-copy view — clawhdf5's
//! real advantage, no per-frame allocation;
//! * `--copy`: `to_vec()` each frame first, matching h5py's unavoidable
//! per-frame numpy materialization, so the two do equal work.
//!
//! Usage: `... --example worldmodel_sampling -- <file.h5> [passes] [--copy]`
use std::hint::black_box;
use std::time::Instant;
use clawhdf5::MmapFile;
fn main() {
let args: Vec<String> = std::env::args().collect();
let path = args
.get(1)
.expect("usage: worldmodel_sampling <file.h5> [passes] [--copy]");
let passes: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(5);
let copy = args.iter().any(|a| a == "--copy");
let file = MmapFile::open(path).expect("open");
let ds = file.dataset("observation").expect("observation dataset");
let shape = ds.shape().expect("shape");
let n = shape[0] as usize;
let frame_bytes: usize = shape[1..].iter().map(|&d| d as usize).product();
let raw = ds
.read_raw_slice()
.expect("read_raw_slice")
.expect("contiguous zero-copy slice");
assert_eq!(raw.len(), n * frame_bytes, "unexpected dataset size");
let order = shuffled(n);
let touch = |slice: &[u8]| -> u64 {
if copy {
let owned = slice.to_vec();
owned.iter().map(|&b| u64::from(b)).sum()
} else {
slice.iter().map(|&b| u64::from(b)).sum()
}
};
// Warm one pass (page-in), then time.
let mut sink = 0u64;
for &i in &order {
sink = sink.wrapping_add(touch(&raw[i * frame_bytes..(i + 1) * frame_bytes]));
}
black_box(sink);
let t0 = Instant::now();
let mut sink = 0u64;
for _ in 0..passes {
for &i in &order {
sink = sink.wrapping_add(touch(&raw[i * frame_bytes..(i + 1) * frame_bytes]));
}
}
black_box(sink);
let elapsed = t0.elapsed().as_secs_f64();
let total = (n * passes) as f64;
let mode = if copy {
"materialized copy"
} else {
"zero-copy view"
};
println!("clawhdf5 ({mode}): {n} frames x {passes} passes in {elapsed:.3}s");
println!("clawhdf5 ({mode}): {:.0} samples/sec", total / elapsed);
}
fn shuffled(n: usize) -> Vec<usize> {
let mut v: Vec<usize> = (0..n).collect();
let mut state: u64 = 0x9E37_79B9_7F4A_7C15;
for i in (1..n).rev() {
state = state
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
let j = (state >> 33) as usize % (i + 1);
v.swap(i, j);
}
v
}