Fix local_sqrt_sqr rewrite logic bug#1952
Open
WHOIM1205 wants to merge 3 commits intopymc-devs:mainfrom
Open
Conversation
Contributor
Author
|
pre-commit.ci autofix |
Contributor
Author
|
heyy @ricardoV94 is there anything i can improve in this pr |
Signed-off-by: WHOIM1205 <rathourprateek8@gmail.com>
for more information, see https://pre-commit.ci
0caa51e to
2e4175f
Compare
ricardoV94
approved these changes
Mar 12, 2026
Member
ricardoV94
left a comment
There was a problem hiding this comment.
Rebased and removed a redundant test.
Thanks for the bugfix
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.
Fix swapped conditions in local_sqrt_sqr rewrite
The rewrite rule
local_sqrt_sqrhad the conditions forsqrt(sqr(x))and
sqr(sqrt(x))reversed. Sinceprev_oprepresents the inneroperation and
node_opthe outer operation, theisinstancecheckswere matching the wrong patterns.
Because of this,
sqrt(sqr(x))was rewritten toswitch(x >= 0, x, nan)instead ofabs(x), which caused negativeinputs to return
NaN. This silently breaks the mathematical identitysqrt(x^2) = |x| and produces incorrect gradients.
This PR swaps the two
isinstancechecks so the rewrites match thecorrect patterns:
sqrt(sqr(x)) -> abs(x)sqr(sqrt(x)) -> switch(x >= 0, x, nan)Tests were also updated to reflect the correct behavior and now include
numerical checks with negative inputs.