Repository navigation
Opt-in direct-I/O reader for HDF5 inputs (--io, ADATA_IO) - #150
Conversation
The Lustre probe found every library held to 260-500 MB/s on an uncompressed h5ad, fetching one chunk of a few hundred KB at a time through the page cache, where plain O_DIRECT reads of the same file in 16 MiB requests, 16-32 in flight, reached 4-5 GB/s. storage/rawio.py reads those datasets itself: each dataset's chunks are mapped once (chunk_iter / get_offset), a slice of rows is coalesced into runs of the file and read in request_bytes requests on a module-level thread pool with os.preadv, into 4 KiB-aligned buffers, past the page cache where the system allows (a buffered descriptor otherwise). Rows that are one run of the file are read into the result; tiles and chunks out of order are copied into place as they arrive. Consecutive slices are read ahead. Its own memory, read-ahead included, is one budget for the process (ADATA_IO_BUFFER, 256M). Anything else - filters, text, compound, fancy indexing, a file opened r+ - goes to h5py. storage/fsinfo.py says which filesystem a path is on, from /proc/self/mountinfo, and the settings for Lustre and NFS. open_store(path, "r") and the new open_hdf5 (the converters' sources) return a DirectFile when the reader applies: an h5py.File whose groups hand out DirectDatasets, so no call site changes. --io auto|direct|h5py (and ADATA_IO) on every command that reads a store; h5py stays the default until the reader is measured on the farm. While it is on, a sparse block reads at least half the buffer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
os.preadv leaves a descriptor's offset alone, so the reader can share HDF5's: concat holds up to 64 inputs open, a quarter of macOS's limit, and each input cost the reader one more descriptor than O_DIRECT needs. The O_DIRECT descriptor is checked against HDF5's for the same file. A platform without os.preadv is left to h5py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 282e9bc232
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A chunk map held an int64 for every chunk a dataset could have, written or not: 8 GB for a (10**12,) dataset in chunks of 1,000, to read a few rows. It now holds runs of chunks that lie one after another in the file, within MAP_BYTES (64 MiB) for every open file together; a dataset whose map would not fit is left to h5py. Chunks out of order, and tiles, were read whole into one buffer: with a 1 MiB buffer and 2 MiB chunks each buffer was 2 MiB while the budget counted 512 KiB. Only the rows of each chunk a block holds are read now, in segments of at most a request, so what the budget counts is what is held. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Why
The December Lustre probe (
benchmarks/lustre/, Performance on cluster storage) found every library — h5py, zarr over HDF5 chunks, DuckDB h5db, obstore — held to 260-500 MB/s on an uncompressed h5ad, because each fetches one 164-415 KB chunk at a time through the page cache. Plain byte ranges of the same file, read with O_DIRECT in 16 MiB requests with 16-32 in flight from Python threads, reached 4-5 GB/s at a third of the CPU on every node. This PR adds that reader.Opt-in.
--io h5pystays the default, and nothing changes unless--io direct|autoorADATA_IOasks.autobecomes the default on Lustre and NFS only after farm measurements of this build (a later step, another session).Design
storage/fsinfo.pyfilesystem(path): the fstype of the longest mount-point prefix of the path's realpath, from/proc/self/mountinfo.\040) are decoded.autofs)."unknown".io_profile(fstype)→ frozenIOProfile(direct, request_bytes, in_flight, buffer_bytes), orNonefor h5py.lustre: O_DIRECT, 16 MiB requests, 16 in flight, 256 MiB buffer.nfs/nfs4: buffered, 4 MiB, 32 in flight, 128 MiB (provisional).None.storage/rawio.pyChunkMapis built once per dataset, cached per open file. It holds runs of chunks that lie one after another in the file, 24 bytes each. Its size follows how the file was written, not the dataset's size: a dataset that was never written has no runs. All open files' maps share a 64 MiBMAP_BYTEScap, and a dataset whose map wouldn't fit goes to h5py. The map applies only to:"r"with the sec2 driver and no userblock;b/i/u/f, no fields, and a file type equal topy_create(dtype). That last check excludes int24s, custom floats and non-bool enums, where HDF5 converts on read;get_offset) or chunked (chunk_iter) layout, with 1 or 2 dimensions.Unallocated chunks read as the fill value. Anything else raises
Unsupported, and h5py reads it.Reading.
DirectDataset.__getitem__sends a step-1 row slice with every other axis whole (ds[a:b],ds[a:b, ...],ds[a:b, :],ds[:],ds[...],ds[()]) toread_rows, the one entry point. Everything else goes tosuper().os.preadv, into 4 KiB-aligned buffers at 4 KiB-aligned offsets and lengths.EINVAL), the same requests go through a buffered descriptor.OSErrorreaches the caller, with the path and offset, once every request it started has finished.=for native order, as h5py has it), shape and values. A result at an unaligned file offset is copied, so the array comes back aligned.Read-ahead. After consecutive slices
[a,b)then[b,c), the reader reads[c, c+(c-b))in the background. The next call is served from it when it falls inside, and discarded otherwise. A closed file waits for its read-ahead to finish.Budget. The reader's own memory is one budget for the process,
ADATA_IO_BUFFER: reads in flight, pieces waiting for their copy, and read-ahead. It doesn't depend on the input's size.Descriptors. Buffered reads go through HDF5's own descriptor (
preadvdoesn't move its offset). The reader opens a descriptor only for O_DIRECT, and checks it against HDF5's for the same inode.concatholds up to 64 inputs, a quarter of macOS's limit.Wiring (no call-site changes)
open_store(path, "r")returns aDirectFile(anh5py.Filesubclass) when the reader applies._get(behind__getitem__,get,values,itemsandvisititems) wraps members asDirectGrouporDirectDataset.isinstance(x, h5py.Dataset)still holds.storage.open_hdf5(path)does the same for converter sources. The bulk-read opens in tenx, visium, xenium, cellbender, loom and hdf5 now use it. Sniffers keep plainh5py.File. mudata had none: it reads throughopen_store. tenx's_CheckedIndicesand_Widenedstay on h5py.subset's sparse block floor rises tomax(TARGET_READ_BYTES, buffer_bytes // 2)(rawio.block_bytes).Knobs
--io auto|direct|h5py: a shared_io_optionwithenvvar="ADATA_IO", on subset, split, concat, convert, recast, everyexportand everyimport.viewdoesn't get it because it reads no data.ADATA_IO_BUFFER(256M,1G…, at least 1M) overridesbuffer_bytes. Requests are capped at half of it.Reading HDF5 with adata's direct reader: O_DIRECT, 16 MiB requests, 16 in flight, 256 MiB buffer (filesystem: lustre).Tests
tests/test_rawio.py(117 tests) compares against h5py byte for byte, with dtype and byte order:r+, a userblock;preadvreturns, a file truncated under the reader, anOSErroron a worker, an error while placing a chunk;ADATA_IO_BUFFER;MAP_BYTESfalls back to h5py; with a 1 MiB buffer, 2 MiB tiles and out-of-order chunks are read with no allocation above 512 KiB.tests/test_fsinfo.py(30 tests): mountinfo with escapes, nested mounts, autofs with nfs4 over it, Lustre, prefix-not-folder, symlinks, paths that don't exist yet, off-Linux, an unreadable table, and theio_profiletable.tests/test_direct_io_cli.py(45 tests) runs each command withADATA_IO=directand with h5py on h5ad, Zarr v2 and Zarr v3 inputs: subset--obs/--var, split, convert to Zarr and to a 10x.h5, export sparse, export dataframe obs, concat, and recast csc/float64/dense. Outputs are identical between the two readers and equal to anndata's result. Blocks are kept small so matrices span several blocks and read-ahead is used. It also checks the status line,autoon ext4 vs Lustre,--iooverADATA_IO, and malformed values refused before anything is written.tests/perf_counters.pyhooksrawio.read_rows, counted as reads like h5py's, andrawio._read_request, givingdirect_requestsanddirect_request_bytes.ADATA_IO=h5py. Three new ones check that:ceil((block + 2 chunks) / request_bytes)for one run of the file, and pieces of whole chunks otherwise;X/data,X/indicesor a dense layer while the reader is on.The request guard caught tiles being asked for one run at a time across HDF5's B-tree nodes; that's fixed by reading through small gaps.
Local results (macOS, so the buffered path):
pytest -m "not integration": 4247 passed, 11 skipped.-m perf: 212 passed, on the final code (5db0476).-m "not integration and not perf" --cov=adata --cov-fail-under=90: 4041 passed at 95.80% total, on the final code.rawio.pyis at 97.44% andfsinfo.pyat 96.88%.main's 0.17.9: 805 passed, including 0.17.9'stest_zarr_safety.py.On Linux,
test_on_linux_requests_go_past_the_page_cacheasserts that requests really went through the O_DIRECT descriptor; it skips where O_DIRECT is missing or refused. It passed on every Linux leg of CI (ext4 runners). Docker wasn't available locally, and nothing has run on Lustre yet. macOS covers the buffered path, and the fallback tests force it on Linux too.Notes
direct-io, notdev.direct-iois its own branch, cut frommainat 0.17.10. It keeps this work apart from the audit work landing indevuntil a farm measurement shows the reader pays off. Nothing has timed it yet; the tests above show correctness and request patterns, not speed. Where it lands after that (devonce the audit settles, or a release of its own) is still to be decided.mainat 0.17.8 (2077928).git merge-treeshows it merges cleanly withmainat 0.17.10 and withdev.docs/reference/cluster-storage.mdandbenchmarks/lustre/are untouched. The changelog has no Unreleased section, so it's left to the release.🤖 Generated with Claude Code