Save much memory at model loading time by converting weights to OrtValues early #26345
+153
−151
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
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

After
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.
The model also loads about 12 seconds faster.
Motivation and Context
Reduce memory usage.