Skip to content

merge Feat/corrfunc kdtree#43

Merged
TobiBu merged 2 commits into
paper/differentiable-applicationsfrom
feat/corrfunc-kdtree
Jul 17, 2026
Merged

merge Feat/corrfunc kdtree#43
TobiBu merged 2 commits into
paper/differentiable-applicationsfrom
feat/corrfunc-kdtree

Conversation

@TobiBu

@TobiBu TobiBu commented Jul 16, 2026

Copy link
Copy Markdown
Owner

This pull request introduces significant improvements to the memory efficiency and scalability of the pair-counting estimator, especially for large particle counts. The main changes include chunked accumulation of near-field pairwise interactions to avoid out-of-memory (OOM) errors, configurable chunk sizing, and improved benchmarking and validation scripts to handle larger problem sizes and edge cases. It also adds clearer documentation and configuration options to both the estimator and its benchmarks.

Estimator memory scaling and chunking:

  • Added _accumulate_block_pairs to perform chunked, differentiable reduction of soft-bin weights over leaf pairs, dramatically reducing memory usage for large N by bounding intermediates to O(chunk_size * max_leaf^2 * nbins) instead of O(P * max_leaf^2 * nbins).
  • Updated soft_pair_counts_from_topology and soft_pair_counts to use chunked accumulation, add a configurable chunk_size parameter, and propagate it through the call stack [1] [2] [3].
  • Introduced _DEFAULT_NEAR_CHUNK (default 1024) as the chunk size for near-field reductions, with documentation on its effect on memory and speed.

Benchmark and validation improvements:

  • In bench/corrfunc/scaling.py, reduced preallocated capacities for far/near lists to prevent OOM during topology build, and improved timing methodology for topology construction [1] [2].
  • In bench/corrfunc/validate_vs_baseline.py, added --brute-force-max-n to switch to a tree-based exact solution as the reference when brute-force is infeasible, and restructured reference computation logic accordingly [1] [2] [3].
  • Increased max_pair_queue in validation to support larger N at tight theta.

Documentation and configuration:

  • Added detailed docstrings to new and updated functions to clarify memory scaling, differentiability, and usage of chunking [1] [2] [3].
  • Updated benchmark record outputs to include more timing and configuration details for better analysis.

These changes collectively make the estimator and its benchmarks robust to much larger problem sizes, prevent OOM errors, and provide clear configuration and diagnostics for users.

TobiBu and others added 2 commits July 16, 2026 22:58
…ling

soft_pair_counts_from_topology materialised the full per-pair soft-bin
weight tensors at once -- w_cross of shape (P, max_leaf, max_leaf, nbins)
and w_within of shape (L, ...) -- so peak memory scaled as
P*max_leaf^2*nbins and OOM'd above N~1e4-2e4 (value_and_grad ~doubled it).

Reduce the near field in fixed-size chunks over the leaf-pair axis via a
rematerialised jax.lax.scan (new _accumulate_block_pairs helper),
accumulating into the (nbins,) output so the (chunk, ml, ml, nbins)
weight tensor is the only large live intermediate; memory is now
O(chunk_size*max_leaf^2*nbins). jax.checkpoint on the scan body is
essential -- without it the reverse pass would stack every chunk's
residual and restore the O(P) ceiling this removes.

chunk_size is a new keyword-only parameter (default 1024) on
soft_pair_counts_from_topology and soft_pair_counts; a single-chunk fast
path keeps small-N results bit-identical to the previous code.
build_pair_topology and the far-field monopole path are unchanged.

Validated: forward + jax.grad bit-identical at N=1000 (theta in
{0,0.3,0.6}); tests/applications all pass; on an A100-40GB the estimator
now runs at N=1e5 (acc 0.5s / vgrad 0.9s) and N=1e6 (acc 7.7s / vgrad
15.7s) with no OOM, and validation errors match the committed 40GB run
at the overlapping sizes (estimator counts within 6e-16 relative).

Bench harness (no estimator-code change): validate_vs_baseline gains
--brute-force-max-n so the O(N^2) reference is skipped where it would
itself OOM (above it the theta=0 tree run, whose near field is exact, is
used as the reference); scaling right-sizes the oversized traversal
capacity buffers (1<<15 -> 1<<13, which preallocated ~8.6GiB and OOM'd
the *build* at 1e6) and warms the build_s timing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
perf(corrfunc): chunk the near-field reduction to lift the memory ceiling
@TobiBu
TobiBu merged commit f6eb701 into paper/differentiable-applications Jul 17, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant