Make ~ in the mask DSL actually negate - #93
Open
cemde wants to merge 1 commit into
Open
Conversation
_M_not defined __call__, the tree-application entry point, instead of apply, the per-leaf predicate. Negation was therefore never applied by the &/| combinators and (~M(A))(model) returned a bare bool.
cemde
force-pushed
the
fix/68-mask-negation
branch
from
August 9, 2026 18:23
d25dde5 to
9e80b21
Compare
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.
Bug
Mhas two distinct entry points, and they do different jobs:apply(leaf)is the per-leaf predicate. Given oneParam, return True or False.__call__(pydag)is the tree walker. It reffs the tree, evaluatesapplyat everyParam, and returns a copy with rejected leaves replaced byNone.~M(A)constructs a_M_not, which needs to invert the predicate. It overrode__call__instead ofapply, so the negation was installed on the walker and never reached the predicate.Impact
Two silent failures.
(~M(A))(model)stopped walking the tree. It evaluated the predicate once against the whole model and returned a bareTruerather than a masked pytree.Worse,
_M_and.applyand_M_or.applyresolve their operands by calling.applyon each one._M_notnever overrodeapply, so it inheritedM's positive version.M(A) & ~M(B)therefore computedM(A) & M(B), selecting exactly the parameters the user asked to exclude, which trains the wrong parameters and freezes the ones meant to learn. That expression appears inM's own class docstring. De Morgan's laws did not hold either.Fix
_M_notnow definesapplyinstead of__call__:That matches
_M_or,_M_andand_M_hasattr, which all overrideapplyand none of which touch__call__. The negation now sits whereM._resolvelooks for it, so the combinators see it, and_M_notinherits the workingM.__call__for tree application.Also in this PR
Reviewing this branch surfaced a separate defect.
M.__call__callstree_reffirst, which replaces duplicateParamreferences with_BaseParamRefplaceholders holding integer indices. A positive mask rejects those because they are not the requested type; a negated mask selects them for the same reason, andOptim.apply_updatesthen adds the raw index to the weight. On a shared model, one step gives2.8where-0.2is correct.Filed as #88 and guarded by a
bug-marked test added here. Not fixed: it predates this change (M(None)selects them on main today), but~working is what makes the path reachable in practice.Closes #68