Repository navigation
perf(markov): vectorize PCCA+ inner simplex search -- 78x faster - #345
Merged
Merged
Conversation
_pcca_connected_isa looped over every state in Python to find the most outlying state and, for each later representative, to project each row and compute its norm. Use whole-array row norms, one np.outer rank-one update and a shared masked argmax helper instead. The same states are selected in the same order, so chi and the rotation matrix are unchanged. Benchmark (6000 states, 10 eigenvectors, plus smaller calls): 228.81 ms -> 2.92 ms. 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 #345 +/- ##
==========================================
- Coverage 94.03% 94.03% -0.01%
==========================================
Files 141 141
Lines 11202 11200 -2
==========================================
- Hits 10534 10532 -2
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 9, 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
_pcca_connected_isa, the inner simplex step of PCCA+ inmarkov/tools/analysis/dense/_pcca.py.argmax.python -m pytest tests/markov/tools/analysis/impl/dense/pcca_test.pypasses (10 passed).What & Why
Before, the function visited each state in a Python loop. The first loop computed the norm of each row twice to find the most outlying state. For each later representative, a second loop projected each row in turn, computed its norm and checked
i not in ind[0:k].Now one small helper,
farthest, computes all row norms at once. It masks the states that are already representatives and returns the first largest positive norm. It returns 0 when no norm is positive, as the old loop did. The projection of all rows is onenp.outerupdate. The projection after the last representative is skipped because no later step uses it. The first representative is the origin, so its projection was always zero and is not computed. The same states are selected in the same order, sochiand the rotation matrix stay identical.Results
ValueError)Why this is safe and easy to maintain
The change is small and only touches
_pcca_connected_isa. One helper does the representative search for every step, so the selection rule is in one place. The chosen states,chi, the rotation matrix and the error messages stay the same. The tests intests/markov/tools/analysis/impl/dense/pcca_test.py, such astest_pcca_1andtest_pcca_2, cover this function throughpcca.Validation
python -m pytest tests/markov/tools/analysis/impl/dense/pcca_test.py: 10 passed with and without the change.Checklist
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/soFJ60nnElI_gJ0oasAkfKw4hTI-wya2