perf(csr): avoid CSR copies and full-grid scaling when freezing constraints - #987
Merged
Merged
Conversation
…raints (#977) Share the lhs matrix when every row stays active, copy only before eliminate_zeros, and pick mask and scaling values at the active rows without expanding broadcast views. Scaling is still validated in full.
Merging this PR will not alter performance
Comparing Footnotes
|
Build cost — v1 vs legacyv1 build peak & time relative to legacy, on this commit — not a comparison against master (that is CodSpeed).
Full table (time + peak, mean)📊 Interactive plots + CSV: download the semantics-report-v1-vs-legacy artifact from this run. Report-only · not a gate · refreshed on every push · obsolete once legacy is dropped. |
4 of 5 tasks
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.
Closes #977.
Note
The following content was generated by AI.
Changes proposed in this Pull Request
Adding a frozen constraint from a sparse expression copied the lhs CSR matrix two to three times. It also broadcast the scaling and the mask over the full coordinate grid before it picked the active rows. This PR removes these copies.
CSRConstraint.from_csrand_keptskip the row slice when every row stays active. The constraint then shares the matrix with the expression. This is safe, because all code that changes a constraint's matrix replaces it and does not write to it in place.eliminate_zeros.assign_labelscopies the matrix only when it contains explicit zeros and is still shared, so the user's expression is never changed.CSRConstraint._active_values, reads a broadcast view per dimension instead of expanding it to the full grid. A scalar scaling (the default1) needs no grid array at all.validate_scalingnow also accepts a plainnp.ndarray.Peak memory of
add_constraints(..., freeze=True), measured with the reproducer from the issue:With a 90 % mask, one copy of the kept rows is still needed. With a 1 % mask, most of the remaining peak comes from broadcasting the rhs over the grid, which this PR does not change.
Tests in
test/test_csr.py:test_zero_coefficient_rows_stay_activenow also checks that the lhs expression is not changed.Checklist
AGENTS.md).doc.doc/release_notes.rstof the upcoming release is included.