Skip to content

fix(rewriter): match arithmetic identity constants exactly - #3066

Open
Ti-Tai Wang (titaiwangms) wants to merge 6 commits into
microsoft:mainfrom
titaiwangms:fix/exact-no-op-constants
Open

Ti-Tai Wang (titaiwangms) wants to merge 6 commits into
microsoft:mainfrom
titaiwangms:fix/exact-no-op-constants

Conversation

@titaiwangms

Copy link
Copy Markdown
Contributor

Summary

Related to pytorch/pytorch#199915.

Require exact identity constants in the arithmetic no-op rewrite rules:

  • Add(x, 0) and Sub(x, 0) now match zero with both tolerances set to zero.
  • Mul(x, 1) and Div(x, 1) now match one with both tolerances set to zero.
  • Other rewrite rules and the default approximate constant-matching API remain unchanged.

Problem

Constant patterns default to rel_tol=1e-5 and abs_tol=1e-8. Consequently, the
Add(x, 0) rule matches a nonzero float32 constant such as 1e-12 and replaces
the addition with Identity. In the reported magnitude computation,
sqrt(real**2 + imag**2 + 1e-12), this changes the zero-input output from
approximately 1e-6 to 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
Sqrt or changing global matching tolerances.

Regression coverage

  • Positive and negative near-zero constants for Add/Sub.
  • Constants above and below one for Mul/Div.
  • Both operand orders for Add/Mul.
  • Float32 and float64, with Constant nodes and scalar initializers.
  • Full optimizer magnitude regression at zero, near-zero, and ordinary inputs,
    using exact output comparison so a small absolute tolerance cannot hide
    removal of the stabilizer.
  • Existing exact-zero/one and broadcast-preservation tests remain unchanged.

Validation

Run from the isolated worktree, with its source directory on PYTHONPATH:

Command Result
python -m pytest onnxscript/rewriter/rules/common/_no_op_test.py onnxscript/optimizer/_optimizer_test.py -q Passed: 45 tests and 48 subtests
lintrunner -a Passed
lintrunner Passed
git diff --check Passed

The 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=False and optimize=True, then executed with ONNX Runtime graph
optimizations disabled. Both exports passed onnx.checker.check_model(..., full_check=True) and exactly matched PyTorch for zero and 1e-8 inputs:
approximately 1e-6 and 1.00010004e-6, respectively.

Integration environment: PyTorch 2.13.0+cu130, ONNX Script from this branch,
ONNX 1.23.0, ONNX Runtime 1.30.0, NumPy 2.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:

  • Readability review: asymmetric Sub/Div operand coverage is intentional;
    swapped subtraction/division are not identity operations.
  • Readability review: exact ReferenceEvaluator comparison passed in the local
    environment; compatibility across other evaluator versions was not tested.
  • Deep review: pre-existing signed-zero behavior of exact identity rewrites is
    unchanged and outside this fix.
  • Deep review: rank-one identity constants remain outside the existing scalar
    matching policy; this is not changed here.
  • Deep review: the full suite and other runtime/provider/platform combinations
    were not run.

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

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.03%. Comparing base (1cd0878) to head (f4b4001).

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.
📢 Have feedback on the report? Share it here.

Comment thread onnxscript/rewriter/rules/common/_no_op.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@titaiwangms
Ti-Tai Wang (titaiwangms) marked this pull request as ready for review October 7, 2026 00:17
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>
@titaiwangms

Copy link
Copy Markdown
Contributor Author

cc G. Ramalingam (@gramalingam)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

4 participants