Skip to content

Fix initializer-safe bulk renaming and NameFixPass initializer iteration#369

Merged
justinchuby merged 11 commits intoonnx:mainfrom
enpasos:main
Apr 9, 2026
Merged

Fix initializer-safe bulk renaming and NameFixPass initializer iteration#369
justinchuby merged 11 commits intoonnx:mainfrom
enpasos:main

Conversation

@enpasos
Copy link
Copy Markdown
Contributor

@enpasos enpasos commented Mar 13, 2026

Summary

Fixes #353.

This PR addresses two related rename failures:

  1. Direct rename loops fail for initializer-backed values in swap/permutation cases.
  2. NameFixPass can raise RuntimeError: dictionary keys changed during iteration when a rename changes initializer dict keys while the pass is iterating them.

Changes

  • add onnx_ir.convenience.rename_values() for bulk value renaming
  • make initializer-backed multi-value renames safe by using temporary names during swaps/permutations
  • keep the existing initializer collision checks for names outside the rename set
  • make NameFixPass iterate over a snapshot of initializer values instead of the live dict view
  • add regression tests for:
    • swapping two initializer-backed outputs
    • renaming to an existing initializer name before running NameFixPass

Why

A single value.name = ... rename is not enough for safe initializer swaps, because the initializer name guard correctly rejects taking another live initializer name. The new bulk helper provides the missing multi-rename operation, while the NameFixPass change makes renaming safe when initializer dict keys are updated during the pass.

Validation

  • pytest -q src/onnx_ir/_convenience/_convenience_test.py -k rename_values
  • pytest -q src/onnx_ir/passes/common/naming_test.py -k initializer_collision

Reported from downstream integration work in jax2onnx.

@enpasos enpasos requested review from a team and titaiwangms as code owners March 13, 2026 04:09
Copilot AI review requested due to automatic review settings March 13, 2026 04:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes initializer-safe renaming in the ONNX IR by introducing a bulk rename helper that can handle swaps/permutations of initializer-backed values, and by making NameFixPass robust to initializer dict-key mutation during renames.

Changes:

  • Add onnx_ir.convenience.rename_values() (implemented in _convenience) to perform bulk renames safely for initializer-backed values via temporary names.
  • Update NameFixPass to iterate over a snapshot of initializer values to avoid RuntimeError when initializer dict keys change during the pass.
  • Add regression tests covering initializer swaps and initializer-name collisions during NameFixPass.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/onnx_ir/passes/common/naming_test.py Adds regression test ensuring initializer collisions don’t mutate the initializer dict during iteration.
src/onnx_ir/passes/common/naming.py Snapshots initializers.values() to avoid dict mutation during iteration.
src/onnx_ir/convenience.py Re-exports rename_values as part of the public convenience API.
src/onnx_ir/_convenience/_convenience_test.py Adds test verifying initializer-backed value swaps work with bulk renaming.
src/onnx_ir/_convenience/init.py Implements rename_values bulk helper with initializer-safe temporary naming.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/onnx_ir/_convenience/__init__.py
Comment thread src/onnx_ir/passes/common/naming.py Outdated
Comment thread src/onnx_ir/_convenience/__init__.py Outdated
Comment thread src/onnx_ir/_convenience/__init__.py Outdated
Copy link
Copy Markdown
Member

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

To rename initializers, it is possible to simply pop the initializers, rename, and put them back. Renaming other values is quite straightforward (just set the name field). I think the logic can be simplified.

@enpasos
Copy link
Copy Markdown
Contributor Author

enpasos commented Mar 16, 2026

I accidentally opened #370; it is closed now. The review fixes are pushed here.

@enpasos
Copy link
Copy Markdown
Contributor Author

enpasos commented Mar 16, 2026

To rename initializers, it is possible to simply pop the initializers, rename, and put them back. Renaming other values is quite straightforward (just set the name field). I think the logic can be simplified.

Applied in ca7dcb3.

I simplified rename_values() by removing affected initializers from graph.initializers, renaming the values, and then adding them back. Non-initializer values are renamed directly via value.name = ....

This also removed the temporary-name permutation logic and the id(...) bookkeeping.

@justinchuby
Copy link
Copy Markdown
Member

Could you fix DCO? Thanks

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 75.47170% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.07%. Comparing base (9fe0960) to head (bde0eed).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/onnx_ir/_convenience/__init__.py 75.00% 7 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #369      +/-   ##
==========================================
- Coverage   80.08%   80.07%   -0.01%     
==========================================
  Files          52       52              
  Lines        6412     6464      +52     
  Branches     1297     1318      +21     
==========================================
+ Hits         5135     5176      +41     
- Misses        913      918       +5     
- Partials      364      370       +6     

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

enpasos and others added 5 commits March 17, 2026 06:26
Signed-off-by: enpasos <matthias.unverzagt@enpasos.com>
Signed-off-by: enpasos <matthias.unverzagt@enpasos.com>
Agree.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: enpasos <matthias.unverzagt@enpasos.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: enpasos <matthias.unverzagt@enpasos.com>
Signed-off-by: enpasos <matthias.unverzagt@enpasos.com>
@enpasos
Copy link
Copy Markdown
Contributor Author

enpasos commented Mar 17, 2026

Could you fix DCO? Thanks

Done.

Comment thread src/onnx_ir/_convenience/__init__.py
Signed-off-by: enpasos <matthias.unverzagt@enpasos.com>
@justinchuby
Copy link
Copy Markdown
Member

Sorry for the delay. Reviewing this week

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes two rename-related failures in the IR: (1) safe bulk renaming for initializer-backed values in swap/permutation scenarios, and (2) preventing NameFixPass from mutating the initializers dictionary while iterating it.

Changes:

  • Add onnx_ir.convenience.rename_values() (implemented in _convenience) to support bulk renaming, including initializer-backed swaps.
  • Make NameFixPass iterate over a snapshot of initializer values to avoid RuntimeError from dict key mutation during iteration.
  • Add regression tests covering initializer swaps and initializer-name collisions during NameFixPass.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/onnx_ir/passes/common/naming.py Iterates over a snapshot of initializers to avoid dict-mutation runtime errors during renames.
src/onnx_ir/passes/common/naming_test.py Adds a regression test ensuring initializer collisions don’t mutate the dict during iteration.
src/onnx_ir/convenience.py Re-exports rename_values in the public convenience module.
src/onnx_ir/_convenience/init.py Implements rename_values with initializer-aware bulk renaming behavior.
src/onnx_ir/_convenience/_convenience_test.py Adds tests for initializer swaps and error cases (None name, collisions, empty initializer name).

@justinchuby justinchuby added this to the 0.2.1 milestone Apr 2, 2026
@justinchuby justinchuby enabled auto-merge (squash) April 3, 2026 18:00
@justinchuby justinchuby disabled auto-merge April 8, 2026 16:44
@justinchuby
Copy link
Copy Markdown
Member

@enpasos could you fix lint?

enpasos and others added 3 commits April 8, 2026 21:15
@justinchuby justinchuby enabled auto-merge (squash) April 9, 2026 13:51
@justinchuby justinchuby merged commit 5291b8b into onnx:main Apr 9, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with renaming

3 participants