Skip to content

Reduce memory use and processing time for similarity preparation - #38

Draft
weitse-hsu wants to merge 14 commits into
kalininalab:mainfrom
weitse-hsu:scalability
Draft

weitse-hsu wants to merge 14 commits into
kalininalab:mainfrom
weitse-hsu:scalability

Conversation

@weitse-hsu

Copy link
Copy Markdown

Hi, I ran into high memory use and a suspected out-of-memory failure during similarity preparation when using S2 with Foldseek on a larger dataset. This PR reduces memory use and speeds up preparation while preserving the numerical results for identical inputs.

This builds on the fixes proposed in #36. I’ve kept the performance work in a separate PR to make it easier to review. If both are accepted, merging #36 first would keep the changes separate. I can update this branch once those fixes are in main and mark this draft ready for review.

Changes

  • Sort Foldseek alignments using temporary files instead of loading the full table into RAM, and allow saved alignments to be reused (b2fa592d).
  • Read the alignment table once and cache repeated identifier and numeric conversions (d7821ffd).
  • Load precomputed matrices row by row into a single array (de5e5a50).
  • Calculate similarities one cluster pair at a time instead of storing all pairwise contributions in Python lists, preserving calculation order (372c7949).
  • Avoid unnecessary matrix copies during clustering and duplicate removal (68d7f455).
  • Add --foldseek-sort-memory to configure the sorting buffer, defaulting to 256M (9a2ade5).

Foldseek alignment processing now requires GNU sort and trades additional temporary disk space for lower RAM use.

Performance comparison

I compared the version containing the fixes proposed in #36 (65ce26a) with the version containing those fixes plus the performance changes in this PR (9a2ade5). Both processed the same 2048 interaction records and 10.35 million saved Foldseek alignment rows. Each version was run in three replicates.

Times below are medians. Memory is the median sampled process-tree peak RSS, including the external sorter.

Operation Time (s) Peak RSS (GiB) Relevant commits
Foldseek saved-alignment processing 461.51 → 50.19 (↑9.19×) 4.399 → 0.625 (↓85.8%) b2fa592d, d7821ffd, 9a2ade5
Precomputed matrix loading, both axes 2.06 → 1.61 (↑1.28×) 0.449 → 0.254 (↓43.5%) de5e5a50
Intercluster aggregation, both axes 2.42 → 1.16 (↑2.09×) 0.471 → 0.325 (↓31.0%) 372c7949
Combined preparation 466.85 → 54.66 (↑8.54×) 4.399 → 0.625 (↓85.8%) All above, plus 68d7f455

Commit links identify the relevant changes, not separately measured contributions from each commit. Later commits on this branch only updated tests and fixture names.

Overall, the combined preparation took 461.68–469.66 s for the baseline and 53.29–55.22 s for the candidate. The reduced memory use comes with more temporary disk use, increasing from 1.01 to 2.41 GiB.

These measurements exclude native Foldseek search/export and final optimization, so they do not represent an end-to-end split speedup. Loading and aggregation were measured separately in fresh processes and should not be added to the preparation time.

Validation

All repetitions passed exact comparisons of the relevant matrix bytes, ordered identifiers and mappings, cluster memberships and weights. The complete preparations also produced identical canonical optimization-model data, without numerical tolerances.

Regression tests cover external-sort spilling, numeric edge cases, identifier handling, matrix loading, aggregation and matrix-copy behavior. The existing chain-selection rules are preserved, including their handling of tied hits.

I’ve attached a script that generates a small synthetic example with 64 entities and checks that the two versions produce identical similarity matrices and optimization-model data. (See compare_scalability.py and
README.md.) It does not require the benchmark dataset or run Foldseek searches or split optimization. Its timings are illustrative and do not reproduce the workload measured above.

I’d be happy to adjust the implementation based on your feedback. Thanks for taking a look!

@Old-Shatterhand Old-Shatterhand self-assigned this Nov 26, 2026
@Old-Shatterhand Old-Shatterhand added the enhancement New feature or request label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants