Repository navigation
perf(markov): normalize tmatrix_cov in place -- 49% less peak memory - #347
Merged
clonker merged 1 commit intoNov 13, 2026
Merged
Conversation
tmatrix_cov built the non-normalized (M, M, M) tensor Z and then made a second (M, M, M) array with cov = Z / norm. Divide Z by the norm in place and return it, so only one tensor is alive at the peak. tracemalloc peak for a 200 x 200 count matrix: 128.33 MB -> 65.33 MB (49.1% less). Values, shape and dtype of the result are unchanged. Co-Authored-By: Aiden <aiden@weco.ai>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #347 +/- ##
=======================================
Coverage 94.03% 94.03%
=======================================
Files 141 141
Lines 11199 11199
=======================================
Hits 10531 10531
Misses 668 668
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
clonker
approved these changes
Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
tmatrix_covinmarkov/tools/estimation/dense/covariance.pywhen it returns the full covariance tensor.(M, M, M)array, not two.tracemallocpeak of one call on a 200 x 200 count matrix, 128.33 MB -> 65.33 MB, 49.1% less.python -m pytest tests/markov/tools/estimation/impl/dense/covariance_test.py::TestCovariance::test_tmatrix_cov -q --tb=shortpasses (1 passed).What & Why
Before, the full-tensor branch built the non-normalized tensor
Zand then rancov = Z / norm[:, None, None]. That division made a second(M, M, M)array whileZwas still alive. For M = 200 each array is 64 MB, so the call needed about 128 MB.Now the code runs
Z /= norm[:, None, None]and returnsZ.Zis a new array that only this function owns, and it always has a float dtype becausealpha = C + 1.0. The division is the same operation on the same values, so the output stays identical. The count matrixCis not changed.Results
k=7), M = 200Each row is the
tracemallocpeak of one call on a random dense float64 count matrix. The best time of five calls for M = 200 went from 14.4 ms to 10.2 ms. The one-row call usesdirichlet_covariance, which this PR does not touch.Why this is safe and easy to maintain
The change is two lines in one function and adds no new code path. The returned tensor has the same values, shape and dtype as before. The test
test_tmatrix_covintests/markov/tools/estimation/impl/dense/covariance_test.pycompares the full tensor and a single row with reference values.Validation
covariance_test.pyalso passes (4 passed).Checklist
test_tmatrix_covalready covers this function; no new test added)AUTHORSAI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/UBiNlGOILh2wSMbn76AW3iUVKTEl5RkR