Skip to content

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

Open
enpasos wants to merge 6 commits intoonnx:mainfrom
enpasos:main
Open

Fix initializer-safe bulk renaming and NameFixPass initializer iteration#369
enpasos wants to merge 6 commits intoonnx:mainfrom
enpasos:main

Conversation

@enpasos
Copy link

@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
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.

Copy link
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
Author

enpasos commented Mar 16, 2026

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

@enpasos
Copy link
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
Member

Could you fix DCO? Thanks

@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 74.50980% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.02%. Comparing base (f80b605) to head (e18a862).

Files with missing lines Patch % Lines
src/onnx_ir/_convenience/__init__.py 74.00% 7 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #369      +/-   ##
==========================================
- Coverage   80.04%   80.02%   -0.02%     
==========================================
  Files          52       52              
  Lines        6394     6444      +50     
  Branches     1294     1314      +20     
==========================================
+ Hits         5118     5157      +39     
- Misses        912      917       +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
Author

enpasos commented Mar 17, 2026

Could you fix DCO? Thanks

Done.

Signed-off-by: enpasos <matthias.unverzagt@enpasos.com>
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.

Issue with renaming

3 participants