From f65c28614395a6be268de4db9c41110729483b3f Mon Sep 17 00:00:00 2001 From: Dixing Xu Date: Tue, 10 Nov 2026 19:07:43 +0000 Subject: [PATCH 1/2] perf(covariance): weighted moments_XX peak memory 128 MB -> 0.8 MB 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 --- src/deeptime/covariance/util/_moments.py | 25 +++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/deeptime/covariance/util/_moments.py b/src/deeptime/covariance/util/_moments.py index 08df0b8a..8dbb511f 100644 --- a/src/deeptime/covariance/util/_moments.py +++ b/src/deeptime/covariance/util/_moments.py @@ -257,14 +257,13 @@ def _sum(X, xmask=None, xconst=None, Y=None, ymask=None, yconst=None, symmetric= T = X.shape[0] # Check if weights are given: if weights is not None: - X = weights[:, None] * X - if Y is not None: - Y = weights[:, None] * Y - # compute raw sums on variable data - sx_raw = X.sum(axis=0) # this is the mean before subtracting it. - sy_raw = 0 - if Y is not None: - sy_raw = Y.sum(axis=0) + # weighted column sums as vector-matrix products, without a weighted copy of the data + sx_raw = weights.dot(X) + sy_raw = weights.dot(Y) if Y is not None else 0 + else: + # compute raw sums on variable data + sx_raw = X.sum(axis=0) # this is the mean before subtracting it. + sy_raw = Y.sum(axis=0) if Y is not None else 0 # expand raw sums to full data if xmask is not None: @@ -385,7 +384,15 @@ def _M2_dense(X, Y, weights=None, diag_only=False): if diag_only: return np.einsum('i,ij,ij->j', weights, X, Y) else: - return np.dot((weights[:, None] * X).T, Y) + # accumulate over blocks of frames, so that only one block of X is weighted at a time + blocksize = 1024 + M2 = np.zeros((X.shape[1], Y.shape[1]), dtype=np.result_type(weights, X, Y)) + buf = np.empty((min(blocksize, X.shape[0]), X.shape[1]), dtype=np.result_type(weights, X)) + for start in range(0, X.shape[0], blocksize): + block = slice(start, start + blocksize) + wX = np.multiply(weights[block, None], X[block], out=buf[:len(X[block])]) + M2 += np.dot(wX.T, Y[block]) + return M2 else: if diag_only: # contract column-wise without materializing the elementwise product From 7fe357b60f23eac06a395428370d8d0bc8cb09c9 Mon Sep 17 00:00:00 2001 From: clonker <1685266+clonker@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:27:42 +0100 Subject: [PATCH 2/2] covariance: adaptive weighted block size in moments_XX --- src/deeptime/covariance/util/_moments.py | 13 ++++++++--- tests/covariance/test_moments.py | 28 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/deeptime/covariance/util/_moments.py b/src/deeptime/covariance/util/_moments.py index 8dbb511f..9ecaffc0 100644 --- a/src/deeptime/covariance/util/_moments.py +++ b/src/deeptime/covariance/util/_moments.py @@ -374,6 +374,10 @@ def _filter_variable_indices(mask, column_selection): # SECOND MOMENT MATRICES / COVARIANCES # ==================================================================================== +# size of the weighted copy of a block of frames in the weighted second moment +_WEIGHTED_BLOCK_BYTES = 1 << 24 + + def _M2_dense(X, Y, weights=None, diag_only=False): """ 2nd moment matrix using dense matrix computations. @@ -385,9 +389,12 @@ def _M2_dense(X, Y, weights=None, diag_only=False): return np.einsum('i,ij,ij->j', weights, X, Y) else: # accumulate over blocks of frames, so that only one block of X is weighted at a time - blocksize = 1024 - M2 = np.zeros((X.shape[1], Y.shape[1]), dtype=np.result_type(weights, X, Y)) - buf = np.empty((min(blocksize, X.shape[0]), X.shape[1]), dtype=np.result_type(weights, X)) + dtype = np.result_type(weights, X) + blocksize = max(_WEIGHTED_BLOCK_BYTES // (dtype.itemsize * max(X.shape[1], 1)), 4 * X.shape[1]) + if X.shape[0] <= blocksize: + return np.dot((weights[:, None] * X).T, Y) + M2 = np.zeros((X.shape[1], Y.shape[1]), dtype=np.result_type(dtype, Y)) + buf = np.empty((blocksize, X.shape[1]), dtype=dtype) for start in range(0, X.shape[0], blocksize): block = slice(start, start + blocksize) wX = np.multiply(weights[block, None], X[block], out=buf[:len(X[block])]) diff --git a/tests/covariance/test_moments.py b/tests/covariance/test_moments.py index 28a1c897..205df4a4 100644 --- a/tests/covariance/test_moments.py +++ b/tests/covariance/test_moments.py @@ -4,6 +4,7 @@ __author__ = 'noe' from deeptime.covariance import moments_XX, moments_XXXY, moments_block +from deeptime.covariance.util import _moments class TestMoments(unittest.TestCase): @@ -602,3 +603,30 @@ def test_moments_block_weighted_sparseconst(self): self._test_moments_block_weighted(self.X_100_sparseconst, self.Y_100_sparseconst, self.cols_100, remove_mean=True, sparse_mode='sparse', sparse_tol=self.sparse_tol, weights=self.weights) + + +N_COLS = 16 +FRAMES_PER_BLOCK = _moments._WEIGHTED_BLOCK_BYTES // (8 * N_COLS) + + +def _check_weighted_moments(n_frames): + rng = np.random.default_rng(42) + X, Y = rng.standard_normal((n_frames, N_COLS)), rng.standard_normal((n_frames, N_COLS)) + weights = rng.random(n_frames) + w, sx, sy, Cxx, Cxy = moments_XXXY(X, Y, weights=weights, sparse_mode='dense') + wX = weights[:, None] * X + np.testing.assert_allclose(w, weights.sum()) + np.testing.assert_allclose(sx, wX.sum(axis=0), atol=1e-8) + np.testing.assert_allclose(sy, (weights[:, None] * Y).sum(axis=0), atol=1e-8) + np.testing.assert_allclose(Cxx, wX.T @ X, atol=1e-8) + np.testing.assert_allclose(Cxy, wX.T @ Y, atol=1e-8) + + +def test_weighted_moments_single_block(): + # largest input that is still weighted in one piece + _check_weighted_moments(FRAMES_PER_BLOCK) + + +def test_weighted_moments_blocked(): + # two full blocks and a ragged one + _check_weighted_moments(2 * FRAMES_PER_BLOCK + 1000)