-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Properly remove in-memory references #25652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
call Graph::ToGraphProto() const so it does process all of the subgraphs recursively and removes all the in-memory references.
There was a problem hiding this 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 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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![]()
|
Ok thank you, that does the trick for NV TRT EP. |
|
@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. |
<!-- 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.
<!-- 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.
…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 <[email protected]> Co-authored-by: Ishwar Raut <[email protected]> Co-authored-by: Maximilian Müller <[email protected]> Co-authored-by: Gaurav Garg <[email protected]> Co-authored-by: Scott McKay <[email protected]> Co-authored-by: Chi Lo <[email protected]> Co-authored-by: Abhishek Jindal <[email protected]> Co-authored-by: Dmitri Smirnov <[email protected]>
### 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.
### 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.
This reverts commit 3ca49d8. It also makes adjustments for the current source code state.
…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.
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.