Skip to content

Read sparse blocks of at least 32 MiB; turn off numpy's huge pages - #129

Merged
Claptar merged 1 commit into
devfrom
perf/block-bytes-thp
Dec 3, 2026
Merged

Claptar merged 1 commit into
devfrom
perf/block-bytes-thp

Conversation

@Claptar

@Claptar Claptar commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Two fixes from a storage and engine probe run on the Sanger farm's Lustre, where subset of large h5ad files mostly sat waiting on the filesystem or in the kernel.

What changed

Sparse blocks read at least 32 MiB (SPARSE_BLOCK_BYTES in core/subset.py).

  • sparse_block_entries sized a block as --chunk rows' worth of entries. On a narrow matrix that is a small read: 1,024 rows of 480 genes is about 800 KB of data and indices, and on Lustre each read waits on the network.
  • A block now holds at least 32 MiB of entries, the same size _chunk_step already uses to copy a dense array.
  • Everything that reads through fan_out_sparse_matrix gets it: subset, split, and the 10x, Visium, Xenium and CellBender conversions.

main turns off numpy's requests for transparent huge pages for the run (_small_pages / _restore_pages in cli.py).

  • numpy asks for 2 MiB pages for every array of 4 MiB or more. The kernel's default defrag=madvise then makes each page fault compact memory directly. On a node whose memory is fragmented, every compaction fails.
  • A NUMPY_MADVISE_HUGEPAGE the user set is kept, and the earlier setting is restored after the run for a caller that runs on (a notebook, a test).

Measurements

subset --obs of half the cells of a 6.7 GB store (4.3M × 480, CSR), on Lustre, on one node, two rounds of each build:

build --chunk wall peak RSS
main 1,024 6.4 / 6.9 min 814–824 MB
this branch 1,024 1.5 / 2.1 min 821 MB
this branch 65,536 1.4 / 1.7 min 823 MB

The huge-page fix was measured on the node where it showed up, with a 2 GB write. The two runs were 10 minutes apart:

  • with numpy's default, nothing was written after 55 minutes, with 3,327 s of system time against 5 s of user time;
  • with NUMPY_MADVISE_HUGEPAGE=0, the write took 11 s.

The same effect probably explains the large node-to-node spread in earlier runs, for example 14 s against 64 s of system time for the same subset.

For reviewers

  • Memory. --chunk no longer lowers a sparse block below 32 MiB. The floor is a constant, so memory still does not follow the input. A block's temporaries in _SparseOutput.take are a few times the block, so peak memory can rise by roughly 100–200 MB on matrices where the floor binds. The spatial store showed no change in peak RSS. The release benchmarks will show it on the 50,000 × 20,000 store.
  • Tests.
    • New: test_a_sparse_block_reads_the_floor_whatever_the_input checks that the floor is reached and not exceeded as n_obs grows.
    • New: test_a_run_asks_numpy_for_no_huge_pages_unless_the_user_chose checks that huge pages are off during a run unless the user set them, and that the setting is restored afterwards.
    • Changed: six guards of what --chunk sets (the F032 block tests, the F091 memory grid, the streamed-growth guard, the write-unit test) now take the floor away with _no_sparse_floor. Their matrices are smaller than 32 MiB and would otherwise be read whole.
  • Private API. numpy._core.multiarray._set_madvise_hugepage is private but has been there since numpy 1.20. An ImportError leaves the setting alone.
  • Not covered yet, recorded in STATUS.md:
    • concat's sparse blocks (_entries_per_block, which also sizes its CSC buckets) have no floor yet;
    • recast's sparse blocks have no floor yet either.
  • Docs: the pipelines guide (the floor, and a new "Huge pages" section), AGENTS.md, docs/developers.md, STATUS.md. A changelog entry comes with the release.

Local runs: pytest -m perf 209 passed; pytest -m "not integration and not perf" 3,830 passed.

🤖 Generated with Claude Code

Two fixes from the storage and engine probe on the farm's Lustre.

- A sparse block reads at least 32 MiB of data and indices
  (SPARSE_BLOCK_BYTES), whatever --chunk says. At --chunk 1024 a narrow
  matrix (4.3M x 480) was read 800 KB at a time, and each read waited on
  the network. On one node, subset --obs of half its cells took 6.4 to
  6.9 minutes on main and 1.5 to 2.1 with the floor, at the same peak
  memory (820 MB). subset, split and the 10x, Visium, Xenium and
  CellBender conversions all read through fan_out_sparse_matrix.
- main turns off numpy's requests for transparent huge pages for the
  run, and restores them after. On a node with fragmented memory each
  fault of an array of 4 MiB or more waited on a failed compaction: a
  2 GB write had written nothing after 55 minutes, and took 11 seconds
  with the requests off. A NUMPY_MADVISE_HUGEPAGE the user set is kept.

Tests: the floor is read and not exceeded whatever the input; a run
turns huge pages off unless the user chose, and restores the setting.
The guards of what --chunk sets take the floor away, since their
matrices are smaller than it. Docs: the pipelines guide (the floor, a
Huge pages section), AGENTS.md, developers.md, STATUS.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 22:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T22:48:57.480422Z aad12e4 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Claptar
Claptar changed the base branch from main to dev December 3, 2026 22:51
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Test Results (py3.13)

3 839 tests   3 827 ✅  6m 7s ⏱️
    1 suites     12 💤
    1 files        0 ❌

Results for commit aad12e4.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Test Results (py3.12)

3 839 tests   3 827 ✅  9m 21s ⏱️
    1 suites     12 💤
    1 files        0 ❌

Results for commit aad12e4.

@Claptar
Claptar merged commit 06c6b9e into dev Dec 3, 2026
13 checks passed
@Claptar Claptar mentioned this pull request Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants