Fix prepacked weight reference lifetime - #32040
Merged
Akshay Sonawane (apsonawane) merged 1 commit intoAug 13, 2026
Merged
Conversation
Copilot started reviewing on behalf of
Akshay Sonawane (apsonawane)
August 12, 2026 21:35
View session
Akshay Sonawane (apsonawane)
enabled auto-merge (squash)
August 12, 2026 21:35
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a lifetime issue in prepacked-weight handling by introducing a new PrepackedWeightsForGraph API that can safely discard an existing per-graph entry and (when saving) replace it with a non-owning reference to a stable, shared prepacked weight.
Changes:
- Added
PrepackedWeightsForGraph::DiscardAndReplaceWithReferenceIfSavingto discard existing entries and, in save mode, store a referring copy to a provided weight. - Updated
SessionState::PrepackConstantInitializedTensorsto use the new API when a shared prepacked weight already exists. - Added unit tests for the new discard/replace behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| onnxruntime/core/framework/prepacked_weights_container.h | Declares the new discard/replace API for save-mode reference handling. |
| onnxruntime/core/framework/prepacked_weights_container.cc | Implements discard + replace-with-reference (save mode) or erase (non-save mode). |
| onnxruntime/core/framework/session_state.cc | Switches a call site to the new API to avoid creating references to soon-to-be-freed weights. |
| onnxruntime/test/framework/tensorutils_test.cc | Adds unit tests validating discard + reference replacement and non-save erasure behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Ti-Tai Wang (titaiwangms)
approved these changes
Aug 13, 2026
Akshay Sonawane (apsonawane)
deleted the
fix/prepacked-weights-reference-lifetime
branch
August 13, 2026 22:53
This was referenced Sep 10, 2026
Open
This was referenced Sep 14, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a new method to the
PrepackedWeightsForGraphclass to improve how prepacked weights are managed, particularly when saving models. The main change is the addition of aDiscardAndReplaceWithReferenceIfSavingmethod, which either replaces a prepacked weight with a reference or removes it, depending on whether the save mode is enabled. Corresponding updates are made to usage sites and comprehensive unit tests are added to verify the new behavior.Enhancements to prepacked weights management:
DiscardAndReplaceWithReferenceIfSavingmethod to thePrepackedWeightsForGraphclass. This method replaces an existing entry with a reference to another weight when in save mode, or removes it from the container otherwise. [1] [2]SessionState::PrepackConstantInitializedTensorsto use the new method, ensuring correct handling of prepacked weights during model save operations.Testing improvements:
tensorutils_test.ccto verify the behavior ofDiscardAndReplaceWithReferenceIfSavingin both save mode and non-save mode, ensuring correct reference replacement and removal of weights as appropriate.