Skip to content

perf(covariance): weighted moments_XX peak memory 128 MB -> 0.8 MB - #346

Merged
clonker merged 2 commits into
deeptime-ml:mainfrom
dexhunter:perf/moments-xx-weighted-peak-memory
Nov 14, 2026
Merged

clonker merged 2 commits into
deeptime-ml:mainfrom
dexhunter:perf/moments-xx-weighted-peak-memory

Conversation

@dexhunter

Copy link
Copy Markdown
Contributor

TL;DR

  • This PR lowers the peak memory of weighted moments_XX calls in deeptime.covariance.util.
  • _sum now gets weighted column sums from weights.dot(X), and _M2_dense adds up the weighted product over blocks of 1024 frames.
  • Peak allocation for a 200000 x 80 float64 block, measured with tracemalloc: 128.07 MB -> 0.78 MB, 99.4% lower.
  • Tested with python -m pytest tests/covariance -q: 67 passed, including the 22 tests in test_moments.py.

What & Why

Before, a weighted call built two full copies of the data, one after the other. _sum computed weights[:, None] * X only to take column sums. _M2_dense computed the same product again for np.dot((weights[:, None] * X).T, Y). Each copy is as large as the input, but the returned moment matrix is only M x M.

Now _sum uses a vector-matrix product, which needs no copy. _M2_dense weights one block of 1024 frames at a time in a reused buffer and adds each block product to the result. The math is the same sum in a different order, so results agree to floating-point rounding. Unweighted calls and the diag_only paths use the same code as before.

Situation (float64, dense mode) Before After Change
weighted, 200000 x 80 128.07 MB 0.78 MB -99.4%
weighted, 20000 x 80 12.87 MB 0.78 MB -94.0%
weighted, 1000000 x 10 80.07 MB 0.15 MB -99.8%
weighted, 200000 x 80, diag_only=True 128.07 MB 3.7 kB -99.99%
weighted, 200000 x 80, remove_mean=True 256.07 MB 128.78 MB -49.7%
weighted, 500 x 10 (small input, no gain) 81.7 kB 82.9 kB +1.5%
unweighted, 200000 x 80 (code path not changed) 52.9 kB 52.9 kB 0%

The numbers are the peak of traced allocations (tracemalloc) during one call, with the input allocated before. With remove_mean=True the remaining 128 MB is the centered copy of the data, which this PR does not touch.

Why this is safe and easy to maintain

The change is small: about 20 lines in _sum and _M2_dense, with no new helper, argument or dependency. Return types, dtypes and shapes stay the same, and values differ only by floating-point rounding. The existing tests in tests/covariance compare weighted and unweighted moments with reference values and cover both functions.

Validation

  • python -m pytest tests/covariance/test_moments.py -q --tb=short: 22 passed.
  • python -m pytest tests/covariance -q --tb=short: 67 passed (_sum and _M2_dense are shared with moments_XXXY and moments_block).
  • Resident memory (rise in VmHWM over the 200000 x 80 weighted call, three fresh processes each): 128.4-128.6 MB before, 1.56 MB after.
  • Run time, best of 7 calls, three processes each, two BLAS threads: 200000 x 80 weighted 117-119 ms before and 115-141 ms after. With remove_mean=True it was 138-162 ms before and 148-211 ms after, so that case can be a little slower. Unweighted: 104 ms and 106 ms.
  • The outputs were also compared with the unchanged code at rtol=1e-9 for weighted, unweighted, remove_mean, column_selection, diag_only, sparse, boolean and float32 inputs.

Reproduce the first table row:

import tracemalloc
import numpy as np
from deeptime.covariance.util import moments_XX

rng = np.random.default_rng(1)
X = rng.standard_normal((200_000, 80))
w = rng.random(200_000) + 0.1
tracemalloc.start()
moments_XX(X, weights=w, sparse_mode="dense")
print(tracemalloc.get_traced_memory()[1])

PR checklist

  • Make sure to include one or more tests for your change (no new test: the existing tests/covariance tests cover the changed functions)
  • Add yourself to AUTHORS

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/Gh2FBnhVjIAP-u5ARXToJoX_pjCWkTj6

A weighted moments_XX call built two full weighted copies of the data:
one in _sum to take column sums, one in _M2_dense for the second moment.

_sum now uses weights.dot(X) for the weighted column sums. _M2_dense
weights one block of 1024 frames at a time in a reused buffer and adds
up the block products. Results agree to floating-point rounding.

Peak traced allocation, 200000 x 80 float64 with weights, dense mode:
128.07 MB -> 0.78 MB. Unweighted calls are unchanged.

Co-Authored-By: Aiden <aiden@weco.ai>
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.03%. Comparing base (b1b7006) to head (7fe357b).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #346   +/-   ##
=======================================
  Coverage   94.03%   94.03%           
=======================================
  Files         141      141           
  Lines       11199    11207    +8     
=======================================
+ Hits        10531    10539    +8     
  Misses        668      668           
Flag Coverage Δ
decomposition 48.88% <100.00%> (+0.03%) ⬆️
markov 69.14% <6.25%> (-0.05%) ⬇️
misc 65.64% <18.75%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@clonker clonker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks! i didn't 100%ly like the tradeoff in the block size so I made it adaptive. sweet improvement otherwise :)

@clonker
clonker enabled auto-merge November 13, 2026 11:29
@clonker
clonker merged commit 3a2f205 into deeptime-ml:main Nov 14, 2026
148 of 150 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.

2 participants