Properly remove in-memory references#25652
Merged
yuslepukhin merged 10 commits intomainfrom Aug 7, 2025
Merged
Conversation
call Graph::ToGraphProto() const so it does process all of the subgraphs recursively and removes all the in-memory references.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a constness violation in Node::ToProto() that was preventing proper removal of in-memory references from subgraphs. The changes ensure that the const version of Graph::ToGraphProto() is called, which properly processes subgraph initializers and removes in-memory references recursively.
- Modifies Node::ToProto() to force calling the const version of Graph::ToGraphProto()
- Refactors ProcessSubgraphsInMemoryData() to accept a main_graph parameter for better comparison logic
- Updates documentation to clarify deprecated usage and parameter meanings
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| onnxruntime/core/graph/graph.cc | Implements constness fix in Node::ToProto() and refactors ProcessSubgraphsInMemoryData() parameter handling |
| include/onnxruntime/core/graph/graph.h | Updates method signature and adds deprecation documentation |
skottmckay
reviewed
Aug 5, 2025
2 tasks
Contributor
|
Ok thank you, that does the trick for NV TRT EP. |
skottmckay
reviewed
Aug 6, 2025
skottmckay
previously approved these changes
Aug 7, 2025
Contributor
|
@skottmckay Can you please tag this PR for cherry-picking into the 1.23.0 release? This is a blocker to run Phi models with TRT-RTX EP. |
adrianlizarraga
approved these changes
Aug 7, 2025
skottmckay
reviewed
Aug 7, 2025
adrianlizarraga
pushed a commit
that referenced
this pull request
Aug 7, 2025
<!-- Describe your changes. --> Make Node::ToProto() const call Graph::ToGraphProto() const so it does process all of the subgraphs recursively and removes all the in-memory references. <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Node::ToProto() const does not respect constness of subgraph attributes and calls non-const version of Graph::ToGraphProto() which does not process subgraph initializers and does not remove in-memory references.
adrianlizarraga
pushed a commit
that referenced
this pull request
Aug 8, 2025
<!-- Describe your changes. --> Make Node::ToProto() const call Graph::ToGraphProto() const so it does process all of the subgraphs recursively and removes all the in-memory references. <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Node::ToProto() const does not respect constness of subgraph attributes and calls non-const version of Graph::ToGraphProto() which does not process subgraph initializers and does not remove in-memory references.
adrianlizarraga
added a commit
that referenced
this pull request
Aug 8, 2025
…5, 25652 (#25701) ### Description Cherry-pick the following PRs into the `rel-1.23.0` branch: - #25391 - #25611 - #25656 - #25346 - #25374 - #25664 - #25675 - #25652 ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> --------- Co-authored-by: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Co-authored-by: Ishwar Raut <iraut@nvidia.com> Co-authored-by: Maximilian Müller <44298237+gedoensmax@users.noreply.github.com> Co-authored-by: Gaurav Garg <gaugarg@nvidia.com> Co-authored-by: Scott McKay <skottmckay@gmail.com> Co-authored-by: Chi Lo <54722500+chilo-ms@users.noreply.github.com> Co-authored-by: Abhishek Jindal <abjindal@microsoft.com> Co-authored-by: Dmitri Smirnov <yuslepukhin@users.noreply.github.com>
sanketkaleoss
pushed a commit
to sanketkaleoss/onnxruntime
that referenced
this pull request
Aug 11, 2025
### Description <!-- Describe your changes. --> Make Node::ToProto() const call Graph::ToGraphProto() const so it does process all of the subgraphs recursively and removes all the in-memory references. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Node::ToProto() const does not respect constness of subgraph attributes and calls non-const version of Graph::ToGraphProto() which does not process subgraph initializers and does not remove in-memory references.
gedoensmax
pushed a commit
to gedoensmax/onnxruntime
that referenced
this pull request
Sep 2, 2025
### Description <!-- Describe your changes. --> Make Node::ToProto() const call Graph::ToGraphProto() const so it does process all of the subgraphs recursively and removes all the in-memory references. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Node::ToProto() const does not respect constness of subgraph attributes and calls non-const version of Graph::ToGraphProto() which does not process subgraph initializers and does not remove in-memory references.
yuslepukhin
added a commit
that referenced
this pull request
Oct 16, 2025
This reverts commit 3ca49d8. It also makes adjustments for the current source code state.
yuslepukhin
added a commit
that referenced
this pull request
Oct 30, 2025
…lues early (#26345) ### Description Converts weights early and revert "Properly remove in-memory references (#25652)" This reverts commit 3ca49d8 and makes appropriate adjustments for the current state of the code. This PR is made possible and on the heels of: #26263 #25833. Previous history: #23979 #25320 #25626 #25652 The first change (#26263) allows us to convert initializers to OrtValues early and save lots of memory at model loading time. Specifically, for Phi-4-mini-instruct-INT4 model before and after looks like this: **Before** <img width="1204" height="124" alt="Before change DEBUG 2025-10-16 144819" src="https://github.com/user-attachments/assets/674ff75b-057f-498a-a906-0140d59d46e6" /> **After** <img width="997" height="114" alt="After change DEBUG 2025-10-16 144819" src="https://github.com/user-attachments/assets/df1783af-7f50-4cd2-b3ad-6868f23be53f" /> The two peaks represent memory usage at optimization time (8.1Gb before) and after weights memory mapping (6.5Gb) After this change corresponding numbers look 3.5Gb and 4.7Gb respectively. Most of the savings during optimization phase come from `ConstantFolding` where we are able to reuse the resulting OrtValues directly for the new initializers. This PR concludes a series of PRs converting initializers to OrtValues. Memory consumption before the conversion began was 9.3Gb and 6.7Gb respectively. We are saving almost 6Gb during optimization and 2Gb for the steady state. <img width="1175" height="139" alt="image" src="https://github.com/user-attachments/assets/80e7d228-8a8e-4316-8e04-b02c2be30f04" /> The model also loads about 12 seconds faster. Example of ConstantFolding being one of the top contributors where we duplicate memory for higher peak before Resolve takes care of no longer used initializers. <img width="1100" height="558" alt="Sanpshot 3 Peak on ConstantFolding Transpose Optimizer" src="https://github.com/user-attachments/assets/95545abd-3f99-46d9-862e-bbf27cbb5b40" /> <img width="1060" height="600" alt="Snapshot 4 Peak AddInitializer from ConstantFolding" src="https://github.com/user-attachments/assets/dd457ec6-23ee-4efd-8c60-625d5faad61e" /> <img width="325" height="160" alt="image" src="https://github.com/user-attachments/assets/37c1194d-f683-49a7-afb1-073dfbb9bbfc" /> ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Reduce memory usage.
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.
Description
Make Node::ToProto() const
call Graph::ToGraphProto() const so it does process all of the
subgraphs recursively and removes all the in-memory references.
Motivation and Context
Node::ToProto() const does not respect constness of subgraph attributes
and calls non-const version of Graph::ToGraphProto() which does not process
subgraph initializers and does not remove in-memory references.