Skip to content
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

avoid redundant memory allocation for external initializers #21682

Merged
merged 7 commits into from
Aug 14, 2024

Conversation

frank-dong-ms
Copy link
Contributor

@frank-dong-ms frank-dong-ms commented Aug 8, 2024

Description

avoid redundant memory allocation for external initializers, we will use mmap for external initializers later so no point to allocate memory in advance then release them later.

Motivation and Context

In current implementation, we will:

  1. Allocate memory (with desired size of current initializer) for initializer first: https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/core/framework/session_state_utils.cc#L131
  2. For external initializer, we will point initializer to mmaped object in memory and release previously allocated tensor: https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/core/framework/session_state_utils.cc#L89

For large models, we are keep allocating and release memory for external initializers which seems unnecessary.

For phi silica model, with this change we can reduce transient memory usage from 4,566MB to 2,724MB. Since these redundant memory is released quickly when we mmap external initializers so this change has no much impact on peak memory usage.

pranavsharma
pranavsharma previously approved these changes Aug 13, 2024
guschmue
guschmue previously approved these changes Aug 13, 2024
@frank-dong-ms frank-dong-ms dismissed stale reviews from guschmue and pranavsharma via 4398895 August 13, 2024 22:03
@frank-dong-ms frank-dong-ms requested a review from fs-eire August 13, 2024 22:33
@frank-dong-ms frank-dong-ms changed the title avoid redcurrant memory allocation for external initializers avoid redundant memory allocation for external initializers Aug 14, 2024
@frank-dong-ms frank-dong-ms merged commit a0708a0 into main Aug 14, 2024
96 of 97 checks passed
@frank-dong-ms frank-dong-ms deleted the frdong/mem_opt branch August 14, 2024 06:13
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.

4 participants