Skip to content

Use optimized reference ops in constant folding evaluator - #3162

Open
Santosh Todkar (todkarsant) wants to merge 5 commits into
microsoft:mainfrom
todkarsant:fix/constant-folding-use-optimized-ops
Open

Santosh Todkar (todkarsant) wants to merge 5 commits into
microsoft:mainfrom
todkarsant:fix/constant-folding-use-optimized-ops

Conversation

@todkarsant

@todkarsant Santosh Todkar (todkarsant) commented Nov 5, 2026 •

Copy link
Copy Markdown

Fixes #3003.

  • Index ONNX's optimized operator-class list by domain/name so constant folding selects optimized Conv. Retry plain evaluation when optimized kernels reject inputs, including empty batches/channels.
  • Keep SAME_UPPER, SAME_LOWER, and VALID Conv nodes unfolded on legacy ONNX: both reference kernels can silently compute incorrect automatic padding. Default/NOTSET and explicit-padding Conv retain the optimized path.
  • Newer ONNX builds use corrected plain ops after upstream vectorization in perf(reference): vectorize Conv implementation onnx/onnx#8366. Custom domains and evaluator precedence remain unchanged.

Independent NumPy numerical/shape regressions cover all three automatic-padding modes across opsets 1/11/21, including unchanged-graph checks on legacy ONNX. Other coverage includes 1D/2D/3D/grouped Conv, explicit pads/strides/dilations, bias, float16/32/64, and graph folding.

Optimizer suite: 106 passed on ONNX 1.22; 105 passed, 1 intentional skip on development 1.24; 105 passed on ONNX 1.17 excluding one confirmed pre-existing IR-version checker incompatibility. lintrunner -a passes.

Windows AMD64, Python 3.11.18, ONNX 1.22, NumPy 2.4.6; reporter's NOTSET Conv, X=(1,2,192,192), W=(32,2,1,1): median 10.469 s → 6.253 ms (1,674×) over 3 plain / 7 guarded optimized runs, with identical float32 values.

The constant folding ReferenceEvaluator always used onnx.reference.ops.load_op()
which only returns plain reference implementations. ONNX's own ReferenceEvaluator
defaults to optimized=True and prefers ops from onnx.reference.ops_optimized
(e.g., Conv uses im2col+GEMM instead of a slow Python loop nest).

This change makes constant folding try onnx.reference.ops_optimized.optimized_operators
first, falling back to the plain ops. The import is guarded with a try/except so older
onnx versions without the module still work.

Fixes microsoft#3003

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@todkarsant

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Index ONNX's operator-class list by domain/name and preserve plain evaluation when optimized kernels reject inputs. Cover Conv parity and constant-folding integration across supported ONNX versions.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.29032% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.83%. Comparing base (f51083c) to head (09c448d).

Files with missing lines Patch % Lines
onnxscript/optimizer/_constant_folding_test.py 90.47% 7 Missing and 3 partials ⚠️
onnxscript/optimizer/_constant_folding.py 63.15% 6 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3162      +/-   ##
==========================================
+ Coverage   73.77%   73.83%   +0.05%     
==========================================
  Files         270      270              
  Lines       33876    34000     +124     
  Branches     3243     3259      +16     
==========================================
+ Hits        24993    25104     +111     
- Misses       7829     7839      +10     
- Partials     1054     1057       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Decline automatic-padding Conv evaluation when legacy ONNX reference kernels are present: both plain and optimized implementations can return incorrect values without raising. Preserve the Conv node and optimized default/explicit-padding paths. Replace circular parity coverage with independent numerical and shape regressions.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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.

Constant folding uses onnx's plain reference ops, never onnx.reference.ops_optimized

2 participants