Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

ONNX specification prohibits graph inputs from being directly used as graph outputs without intermediate nodes. This creates invalid graphs that fail validation.

Changes

  • New pass: IdentityFixPass in src/onnx_ir/passes/common/identity_fix.py

    • Detects outputs that are direct graph inputs (via is_graph_input())
    • Inserts Identity nodes between them
    • Recursively processes subgraphs and functions
    • Preserves metadata, shapes, types, and doc strings
  • Tests: 12 test cases in identity_fix_test.py covering:

    • Single and multiple direct connections
    • Subgraphs (If/Loop nodes)
    • Functions
    • Metadata preservation
  • Export: Added to __init__.py for public API

Usage

from onnx_ir.passes.common import IdentityFixPass

# Invalid: input directly used as output
graph = ir.Graph(inputs=[x], outputs=[x], nodes=[])

# Apply pass to fix
result = IdentityFixPass()(ir.Model(graph, ir_version=10))
# Now contains Identity node: input -> Identity -> output

Ref: pytorch/pytorch#167848

Original prompt

This section details on the original issue you should resolve

<issue_title>[Passes] Add identity fix</issue_title>
<issue_description>Create a pass that adds an Identity node if an input of a graph is directly set as the output of the graph. This will turn an invalid graph into a valid one.

Ref: pytorch/pytorch#167848</issue_description>

<agent_instructions>Add the pass in src/onnx_ir/passes/common</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@codecov
Copy link

codecov bot commented Dec 11, 2025

Codecov Report

❌ Patch coverage is 97.22222% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 77.64%. Comparing base (ac8138e) to head (d247885).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/onnx_ir/passes/common/output_fix.py 97.14% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #268      +/-   ##
==========================================
+ Coverage   77.33%   77.64%   +0.31%     
==========================================
  Files          40       41       +1     
  Lines        5042     5078      +36     
  Branches     1009     1016       +7     
==========================================
+ Hits         3899     3943      +44     
+ Misses        853      843      -10     
- Partials      290      292       +2     

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

Copilot AI changed the title [WIP] Add pass to include identity node for graph validation Add IdentityFixPass to fix invalid graphs with direct input-output connections Dec 11, 2025
Copilot AI requested a review from justinchuby December 11, 2025 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Passes] Add identity fix

2 participants