Commit 383cb03
Create a identity-elimination pass (onnx#121)
This PR implements a new `IdentityEliminationPass` that removes
redundant Identity nodes from ONNX graphs according to the rules
specified in the issue.
## Implementation
The pass handles three distinct scenarios for Identity nodes of the form
`y = Identity(x)`:
1. **Case 1**: When `y` is not a graph output → Replace all uses of `y`
with `x` and remove the node
2. **Case 2**: When `y` is a graph output but `x` is not a graph input →
Eliminate the node but rename `x` to `y`
3. **Case 3**: When both `y` is a graph output and `x` is a graph input
→ Keep the node (cannot eliminate)
## Example
```python
import onnx_ir as ir
from onnx_ir.passes.common import IdentityEliminationPass
# Before: input1 -> Identity -> Add -> Identity -> output
# After: input1 -> Add -> output (with proper renaming)
pass_instance = IdentityEliminationPass()
result = pass_instance(model)
```
## Key Features
- **Comprehensive elimination**: Handles all three scenarios correctly
- **Subgraph support**: Processes Identity nodes in functions and node
attributes
- **Chain elimination**: Can eliminate chains of Identity nodes in a
single pass
- **Safe operation**: Uses `graph.remove(node, safe=True)` to ensure
graph consistency
- **Edge case handling**: Properly handles invalid nodes, None inputs,
and empty graphs
## Testing
Added comprehensive test suite with 9 test cases covering:
- All three elimination scenarios
- Multiple Identity nodes in complex graphs
- Chain of Identity nodes
- Edge cases (invalid nodes, None inputs, empty graphs)
- Integration with PassManager
All tests pass and the implementation follows established patterns in
the codebase.
Fixes onnx#120.
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Co-authored-by: titaiwangms <18010845+titaiwangms@users.noreply.github.com>1 parent 2481fdd commit 383cb03
File tree
3 files changed
+731
-0
lines changed- src/onnx_ir/passes/common
3 files changed
+731
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
33 | 37 | | |
34 | 38 | | |
35 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
0 commit comments