fix: ignore masked variable labels when matching constraints - #895
Merged
Conversation
`Model.remove_variables` deleted every constraint whose `has_variable` matched the removed variable. For a masked variable that was every constraint with a padded term slot: `-1` marks a masked entry on the variable side and an empty term slot on the constraint side, and the two were compared without filtering the sentinel. Filter the -1 entries out of the variable labels before matching, via a new `common.assigned_labels` helper, in both `has_variable` implementations and in the objective cleanup of `remove_variables`. Only the dense `Constraint` path was actually affected -- a CSR matrix stores no empty slots -- but the same guard now holds for both. Fixes #883 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Filter masked labels once per removal instead of once per constraint, match via the contiguous label range, and test CSR positions instead of gathering labels. Reduce the objective term mask over the factor dimension so remove_variables works with a quadratic objective.
Merging this PR will degrade performance by 1.22%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Memory | test_to_lp[storage-n=250] |
28.2 MB | 36.3 MB | -22.27% |
| ❌ | Memory | test_to_lp[expression_arithmetic-n=250] |
41 MB | 46.9 MB | -12.4% |
| ⚡ | Memory | test_to_lp[knapsack-n=10000] |
2.7 MB | 2.3 MB | +21.4% |
| ⚡ | Memory | test_to_lp[storage-n=10] |
2.6 MB | 2.3 MB | +15.16% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing fix-remove-variables-masked (19288f0) with master (5d22e35)
Footnotes
-
175 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
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 #883. Builds on @YassineAbdelouadoud's #886, whose fix commit is included here unchanged.
Note
Everything below this line was generated by AI.
Changes proposed in this Pull Request
Model.remove_variablesremoved constraints that never referenced the removedvariable. A masked variable stores
-1in its label array, and-1is also themarker for an empty term slot in a constraint, so every masked variable looked
like it was used by every constraint that carried padded terms. Models built
with
mask=could silently lose constraints and solve to a wrong optimum. Thefix (from #886) filters the sentinel out before matching labels, in
Constraint.has_variable,CSRConstraint.has_variableand the objectiveupdate.
On top of that, this PR makes the matching faster and fixes a second, unrelated
failure in the same function.
Performance. The label matching runs once per constraint group for every
variable removal, so it is worth doing well.
ConstraintBase.has_variableis now concrete: it strips the sentinels onceand delegates to a new abstract
has_labels(labels).remove_variablescalls
has_labelsdirectly, so the filtering pass happens once per removalinstead of once per constraint group.
contains_labelshelper inlinopy/common.pyreplaces the plainisin. Labels are handed out in ascending blocks, so restricting the valuesto the label range is a cheap prefilter — and when the block turns out to be
gap-free it is already the complete answer, which reduces the test to two
comparisons instead of a sort-based membership check. Correctness was fuzzed
against
np.isinover 20k random cases.CSRConstraint.has_labelsmaps the variable's labels to dense positions andtests those against
csr.indices, rather than gatheringvlabels[csr.indices]first. That trades anO(nnz)materialisation perconstraint group for an
O(n_labels)gather. Masked labels are absent fromvlabelsentirely, so positions are gap-free even for a masked variable andthe fast path applies where the label-space one cannot.
Measured on 200k labels across 40 constraint groups,
remove_variablesend toend:
The frozen cases gain less because most of their remaining time is spent
rebuilding the label index, not scanning.
Quadratic objectives.
remove_variablesraisedIndexError: Boolean array size 2 is used to index array with shape (2, 6)forany model with a quadratic objective, because the term mask was built over the
factor dimension as well as the term dimension. The mask is now reduced with
any(FACTOR_DIM)when that dimension is present, so a quadratic term isdropped as soon as any of its factors references the removed variable. This
failed on
mastertoo and is independent of the masking bug.The regression test is parametrised over
freezeand over linear versusquadratic objectives.
Known limitation, not addressed here
A masked variable inside a quadratic term leaves a
[-1, other_label]factorpair, and the quadratic encoding reads a
-1factor as "this term is linear".So
(a * c).sum()withamasked silently contributes a linear cost onc.This is present on
masterbefore any removal happens and deserves its ownissue rather than a fix folded in here.
Checklist
AGENTS.md).doc.doc/release_notes.rstof the upcoming release is included.