Skip to content

Conversation

@yuslepukhin
Copy link
Member

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.

  call Graph::ToGraphProto() const so it does process all of the
  subgraphs recursively and removes all the in-memory references.
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 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
skottmckay previously approved these changes Aug 6, 2025
Copy link
Contributor

@skottmckay skottmckay left a comment

Choose a reason for hiding this comment

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

:shipit:

@gedoensmax
Copy link
Contributor

Ok thank you, that does the trick for NV TRT EP.

skottmckay
skottmckay previously approved these changes Aug 7, 2025
@gaugarg-nv
Copy link
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.

@yuslepukhin yuslepukhin merged commit 3ca49d8 into main Aug 7, 2025
96 of 98 checks passed
@yuslepukhin yuslepukhin deleted the yuslepukhin/node_to_proto_subgraph_fix branch August 7, 2025 22:32
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 <[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]>
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.
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.

7 participants