Repository navigation
fix(rewriter): match arithmetic identity constants exactly - #3066
Open
Ti-Tai Wang (titaiwangms) wants to merge 6 commits into
Open
Ti-Tai Wang (titaiwangms) wants to merge 6 commits into
Ti-Tai Wang (titaiwangms) wants to merge 6 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c63f5f94-f5d2-4f18-b06f-a39096ba80f6 Signed-off-by: titaiwangms <titaiwang@microsoft.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3066 +/- ##
==========================================
+ Coverage 73.01% 73.03% +0.02%
==========================================
Files 267 267
Lines 32641 32668 +27
Branches 3101 3104 +3
==========================================
+ Hits 23832 23859 +27
Misses 7767 7767
Partials 1042 1042 ☔ View full report in Codecov by Harness. |
Copilot started reviewing on behalf of
G. Ramalingam (gramalingam)
October 6, 2026 23:30
View session
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused fix is correct and has comprehensive regression coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes arithmetic no-op rewrites that incorrectly removed near-identity constants, addressing pytorch/pytorch#199915.
Changes:
- Requires exact matching for arithmetic identity constants.
- Adds direct rule and optimizer-level regressions.
| File | Description |
|---|---|
_no_op.py |
Uses zero tolerances for identity constants. |
_no_op_test.py |
Covers near-identity constants and operand orders. |
_optimizer_test.py |
Verifies magnitude stabilizer preservation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c63f5f94-f5d2-4f18-b06f-a39096ba80f6 Signed-off-by: titaiwangms <titaiwang@microsoft.com>
Ti-Tai Wang (titaiwangms)
marked this pull request as ready for review
October 7, 2026 00:17
G. Ramalingam (gramalingam)
approved these changes
Oct 7, 2026
G. Ramalingam (gramalingam)
enabled auto-merge (squash)
October 7, 2026 00:21
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c63f5f94-f5d2-4f18-b06f-a39096ba80f6 Signed-off-by: titaiwangms <titaiwang@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c63f5f94-f5d2-4f18-b06f-a39096ba80f6 Signed-off-by: titaiwangms <titaiwang@microsoft.com>
Contributor
Author
This branch has not been deployed
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.
Summary
Related to pytorch/pytorch#199915.
Require exact identity constants in the arithmetic no-op rewrite rules:
Add(x, 0)andSub(x, 0)now match zero with both tolerances set to zero.Mul(x, 1)andDiv(x, 1)now match one with both tolerances set to zero.Problem
Constant patterns default to
rel_tol=1e-5andabs_tol=1e-8. Consequently, theAdd(x, 0)rule matches a nonzero float32 constant such as1e-12and replacesthe addition with
Identity. In the reported magnitude computation,sqrt(real**2 + imag**2 + 1e-12), this changes the zero-input output fromapproximately
1e-6to zero.The same matching issue affects subtraction of small nonzero constants and
multiplication/division by constants close to, but different from, one. This
fix addresses the arithmetic identity patterns rather than special-casing
Sqrtor changing global matching tolerances.Regression coverage
using exact output comparison so a small absolute tolerance cannot hide
removal of the stabilizer.
Validation
Run from the isolated worktree, with its source directory on
PYTHONPATH:python -m pytest onnxscript/rewriter/rules/common/_no_op_test.py onnxscript/optimizer/_optimizer_test.py -qlintrunner -alintrunnergit diff --checkThe new regressions were also run before the production change and failed for
the expected erroneous rewrite counts and magnitude outputs.
Additionally, the exact PyTorch issue reproducer was exported in memory with
both
optimize=Falseandoptimize=True, then executed with ONNX Runtime graphoptimizations disabled. Both exports passed
onnx.checker.check_model(..., full_check=True)and exactly matched PyTorch for zero and1e-8inputs:approximately
1e-6and1.00010004e-6, respectively.Integration environment: PyTorch
2.13.0+cu130, ONNX Script from this branch,ONNX
1.23.0, ONNX Runtime1.30.0, NumPy2.5.1; CPU execution.Review and scope
Static review of the changed rules, constant matcher, commutation/cloning, and
default optimizer wiring found no Critical/Major findings or overruled findings.
Non-blocking review questions and exclusions:
swapped subtraction/division are not identity operations.
environment; compatibility across other evaluator versions was not tested.
unchanged and outside this fix.
matching policy; this is not changed here.
were not run.