Skip to content

Conversation

@justinchuby
Copy link
Collaborator

Clear unused initializers on the fly to prevent memory usage jump due to intermediate folded tensors.

Clear unused initializers on the fly to prevent memory usage jump due to intermediate folded tensors.

Signed-off-by: Justin Chu <[email protected]>
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 adds automatic cleanup of unused initializers during constant folding and bumps the package version to 0.5.6.

  • Adds functionality to detect and remove unused initializers when nodes are removed during constant folding
  • Updates package version from 0.5.5 to 0.5.6

Reviewed Changes

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

File Description
onnxscript/optimizer/_constant_folding.py Adds _clear_unused_initializers helper function and integrates it into the replace_node method to clean up unused initializers after node replacement
VERSION Bumps package version from 0.5.5 to 0.5.6

Signed-off-by: Justin Chu <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
@codecov
Copy link

codecov bot commented Oct 31, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.45%. Comparing base (ee9a6e8) to head (289a8ba).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2668      +/-   ##
==========================================
+ Coverage   70.42%   70.45%   +0.02%     
==========================================
  Files         224      224              
  Lines       26729    26751      +22     
  Branches     2673     2678       +5     
==========================================
+ Hits        18825    18847      +22     
  Misses       6973     6973              
  Partials      931      931              

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

Signed-off-by: Justin Chu <[email protected]>
@justinchuby
Copy link
Collaborator Author

@titaiwangms fixed tests. PTAL. Also cc @gramalingam


if isinstance(root, ir.Graph):
# The old node should now be detached from the graph
assert node.graph is None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this assertion? This might be true in the current implementation ... but just wondering if we might ever want to replace [oldnode] by [newnode1, oldnode, newnode2], for example ... not sure of replace_nodes_and_values supports such a scenario ... but the logic here seems to be robust even in that case. But the assert would fail in that case. Anyway ... just a minor suggestion.

Also: in principle, this logic could go into replace_nodes_and_values, can't it?

Copy link
Collaborator Author

@justinchuby justinchuby Oct 31, 2025

Choose a reason for hiding this comment

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

this logic could go into replace_nodes_and_values, can't it?

Good point - I agree. But also replace_nodes_and_values is more general. So checking value users may be more expensive there.

replace [oldnode] by [newnode1, oldnode, newnode2]

Not sure? replace_nodes_and_values will disconnect the old nodes (which breaks all the value connections) so I don't think it will work?

@justinchuby justinchuby enabled auto-merge (squash) October 31, 2025 01:58
@justinchuby justinchuby merged commit 3846705 into main Oct 31, 2025
33 checks passed
@justinchuby justinchuby deleted the justinchu/clean-initializers branch October 31, 2025 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

4 participants