Skip to content

Create a name fix pass to ensure unique names for all values and nodes #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 11, 2025

This PR implements a NameFixPass in src/onnx_ir/passes/common/naming.py to ensure that all values and nodes in an ONNX IR model have unique names according to the specified policy:

Key Features

  1. Graph inputs and outputs have precedence - Their names are preserved when possible, and only renamed if duplicates exist
  2. Intermediate values get unique names - Unnamed values are assigned names using the pattern val_{counter}
  3. Global uniqueness across subgraphs - The pass recursively processes all subgraphs to ensure names are unique across the entire model
  4. Node naming - All nodes get unique names, with unnamed nodes assigned names using the pattern node_{counter}

Implementation Details

The pass inherits from InPlacePass and processes the model in the following order:

  1. Graph inputs (highest precedence)
  2. Graph outputs (highest precedence)
  3. Initializers
  4. All nodes and their values recursively through subgraphs

The implementation carefully handles the IR's automatic naming system by tracking already-processed values to avoid duplicate processing and unwanted modifications.

Example Usage

import onnx_ir as ir
from onnx_ir.passes.common import NameFixPass

# Create model with duplicate names
input1 = ir.Input("same_name", ...)
input2 = ir.Input("same_name", ...)  # Duplicate!

# ... create graph and model ...

# Apply the name fix pass
pass_instance = NameFixPass()
result = pass_instance(model)

# Now input1.name == "same_name", input2.name == "same_name_1" 

Testing

Comprehensive tests cover:

  • Auto-assigned name preservation when already unique
  • Duplicate name resolution with precedence rules
  • Unnamed value/node assignment
  • Global uniqueness across subgraphs
  • No modification when all names are already unique

Fixes #123.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@justinchuby
Copy link
Member

You should maintain a ir.Value to assigned name dictionary to ensure that a value is only assigned a name once. Alternatively maintain a set of seen values.

@Copilot Copilot AI changed the title [WIP] Create a name fix pass Create a name fix pass to ensure unique names for all values and nodes Jul 11, 2025
@Copilot Copilot AI requested a review from justinchuby July 11, 2025 22:14
Copilot finished work on behalf of justinchuby July 11, 2025 22:14
Copy link

codecov bot commented Jul 11, 2025

Codecov Report

Attention: Patch coverage is 71.53846% with 37 lines in your changes missing coverage. Please review.

Project coverage is 74.71%. Comparing base (383cb03) to head (3cbe736).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/onnx_ir/passes/common/naming.py 71.31% 28 Missing and 9 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #124      +/-   ##
==========================================
- Coverage   74.80%   74.71%   -0.09%     
==========================================
  Files          39       40       +1     
  Lines        4723     4853     +130     
  Branches      966     1008      +42     
==========================================
+ Hits         3533     3626      +93     
- Misses        840      868      +28     
- Partials      350      359       +9     

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

Copy link
Contributor Author

Copilot AI commented Jul 11, 2025

You should maintain a ir.Value to assigned name dictionary to ensure that a value is only assigned a name once. Alternatively maintain a set of seen values.

I've updated the implementation to use a dict[ir.Value, str] mapping instead of a set to track processed values. This ensures each value is assigned a name exactly once and makes the intent clearer. The dictionary explicitly maps each ir.Value to its final assigned name. Commit: b979a3d

Copilot finished work on behalf of justinchuby July 11, 2025 22:23
Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lintrunner found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Signed-off-by: Justin Chu <[email protected]>
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.

Create a name fix pass
2 participants