Skip to content

Preserve output names for returned input aliases - #3018

Open
TANGBUDU wants to merge 2 commits into
microsoft:mainfrom
TANGBUDU:fix/return-alias-output-name-2714
Open

Preserve output names for returned input aliases#3018
TANGBUDU wants to merge 2 commits into
microsoft:mainfrom
TANGBUDU:fix/return-alias-output-name-2714

Conversation

@TANGBUDU

@TANGBUDU TANGBUDU commented Aug 28, 2026

Copy link
Copy Markdown

Fixes #2714.

When a graph input is returned through a local alias, use the alias name for the inserted Identity output instead of the generic return_val name. 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 Y returned the original input under the name X, not Y. Looking up the resolved ONNX name in the mutable Python symbol table incorrectly classified the value after X was 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.

@TANGBUDU

TANGBUDU commented Sep 7, 2026

Copy link
Copy Markdown
Author

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

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.22222% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.70%. Comparing base (3ba2bf7) to head (cbcda16).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
onnxscript/_internal/converter_test.py 80.95% 8 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

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.

🟢 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 via return_var.is_graph_input() and name the inserted Identity using the return expression’s ast.Name.id when 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.

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.

Unintuitive behavior: Returned input aliases generate Identity nodes with generic names instead of preserving Python variable names

3 participants