Preserve output names for returned input aliases - #3018
Conversation
|
Justin Chu (@justinchuby) gentle ping on this PR when you have a chance. It addresses #2714 and is ready for review. Happy to address any feedback — thanks! |
Check the resolved ONNX value rather than looking its name up in the mutable Python symbol table. Cover input rebinding and collisions with existing input names, including duplicate returns and numerical parity. Assisted-by: ChatGPT
| @script(default_opset=op) | ||
| def returned_alias(X: FLOAT[2]) -> FLOAT[2]: | ||
| Y = X | ||
| X = op.Neg(X) |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3018 +/- ##
==========================================
+ Coverage 72.64% 72.70% +0.05%
==========================================
Files 265 265
Lines 32251 32339 +88
Branches 3050 3059 +9
==========================================
+ Hits 23429 23512 +83
- Misses 7786 7788 +2
- Partials 1036 1039 +3 ☔ View full report in Codecov by Harness. |
There was a problem hiding this comment.
🟢 Approval recommended
The change is narrowly scoped, corrects the rebinding misclassification by relying on IR state, and includes comprehensive regression tests for the reported and related edge cases.
Pull request overview
Updates the ONNXScript converter to preserve Python alias names when returning a graph input through a local alias, by using the alias name for the inserted Identity output (while keeping the existing return_val... behavior for direct return X of an input). This addresses the rebinding regression described in #2714 by checking the resolved IR value (ir.Value.is_graph_input()) instead of consulting the mutable Python symbol table.
Changes:
- In
Converter._translate_return_stmt, detect returned graph inputs viareturn_var.is_graph_input()and name the insertedIdentityusing the return expression’sast.Name.idwhen it differs from the resolved ONNX value name. - Add regression tests covering alias-preserved naming, input rebinding, name collisions with an existing input, and duplicate alias returns (with ONNX checker + runtime parity where relevant).
File summaries
| File | Description |
|---|---|
| onnxscript/_internal/converter.py | Fixes output naming for returned input aliases by deriving the copy name from the returned ast.Name when appropriate and using IR-based graph-input detection. |
| onnxscript/_internal/converter_test.py | Adds targeted regression tests for alias naming preservation, rebinding correctness, collision handling, and duplicate alias return behavior. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Fixes #2714.
When a graph input is returned through a local alias, use the alias name for the inserted
Identityoutput instead of the genericreturn_valname. Directly returning an unchanged graph input keeps the existing naming behavior. Generated names still go through the existing uniqueness allocator.The method audit found a missed case:
Y = X; X = op.Neg(X); return Yreturned the original input under the nameX, notY. Looking up the resolved ONNX name in the mutable Python symbol table incorrectly classified the value afterXwas rebound. The converter now checks the resolved IR value directly.Regression coverage includes input-name rebinding, collisions with an existing input name, duplicate alias returns, ONNX checker validation, and runtime output parity. The rebinding regression fails before the follow-up fix (
'X' != 'Y') and passes afterward.Tests (2026-09-07):
python -m pytest onnxscript/_internal/converter_test.py -q: 56 passed, 1 skipped, 1 xfailed, 3 xpassed; 160 subtests passed, both with ONNX 1.22.0 / ONNX Runtime 1.29.0 and with ONNX 1.18.0 / ONNX Runtime 1.23.0 / ONNX IR 0.1.16.lintrunner -a: no lint issues.git diff --check: clean.Revalidation on 2026-09-08 at unchanged head
cbcda16aa93281d0c5e5dcb50baa96951e3a2f08: both converter-suite results above were reproduced.AI assistance was used for the source audit, patch preparation, and validation. The existing expected-failure / unexpected-pass results are reported above, not counted as ordinary passes.