Repository navigation
Add EP device memory capacity query API with CUDA support #33447
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
Open
Selena Yang (selenayang888)
wants to merge
8
commits into
main
Choose a base branch
from
syang/ep-device-memory-capacity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+441
−2
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
38c2f75
Add EP device memory capacity query API for total and available memory
selenayang888 7192904
Merge remote-tracking branch 'origin/main' into syang/ep-device-memor…
selenayang888 668a565
Resolve the copilot comments
selenayang888 fd8afc5
Merge remote-tracking branch 'origin/main' into syang/ep-device-memor…
selenayang888 d9ba475
fix CI failure
selenayang888 7b7fce9
Merge remote-tracking branch 'origin/main' into syang/ep-device-memor…
selenayang888 0173484
Move GetMemoryCapacity below CreateSyncStream in C++ wrappers
selenayang888 87f2354
Merge remote-tracking branch 'origin/main' into syang/ep-device-memor…
selenayang888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
27 changes: 27 additions & 0 deletions
27
onnxruntime/core/providers/cuda/shared_inc/cuda_memory_capacity.h
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #pragma once | ||
|
|
||
| #include <cuda.h> | ||
|
|
||
| #include "device_memory_capacity.h" | ||
|
Check warning on line 5 in onnxruntime/core/providers/cuda/shared_inc/cuda_memory_capacity.h
|
||
|
|
||
| namespace onnxruntime { | ||
| namespace cuda { | ||
|
|
||
| struct MemoryCapacityDriverApi { | ||
| using Device = CUdevice; | ||
| using Context = CUcontext; | ||
|
|
||
| bool Init() noexcept { return cuInit(0) == CUDA_SUCCESS; } | ||
| bool DeviceGet(Device* device, int ordinal) noexcept { return cuDeviceGet(device, ordinal) == CUDA_SUCCESS; } | ||
| bool DeviceTotalMem(size_t* total, Device device) noexcept { return cuDeviceTotalMem(total, device) == CUDA_SUCCESS; } | ||
| bool PrimaryCtxRetain(Context* context, Device device) noexcept { | ||
| return cuDevicePrimaryCtxRetain(context, device) == CUDA_SUCCESS; | ||
| } | ||
| bool CtxPushCurrent(Context context) noexcept { return cuCtxPushCurrent(context) == CUDA_SUCCESS; } | ||
| bool MemGetInfo(size_t* available, size_t* total) noexcept { return cuMemGetInfo(available, total) == CUDA_SUCCESS; } | ||
| bool CtxPopCurrent(Context* context) noexcept { return cuCtxPopCurrent(context) == CUDA_SUCCESS; } | ||
| bool PrimaryCtxRelease(Device device) noexcept { return cuDevicePrimaryCtxRelease(device) == CUDA_SUCCESS; } | ||
| }; | ||
|
|
||
| } // namespace cuda | ||
| } // namespace onnxruntime | ||
55 changes: 55 additions & 0 deletions
55
onnxruntime/core/providers/cuda/shared_inc/device_memory_capacity.h
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| namespace onnxruntime { | ||
| namespace cuda { | ||
|
|
||
| template <typename DriverApi> | ||
| const char* QueryDeviceMemoryCapacity(DriverApi& driver, int device_id, | ||
| uint64_t& total_memory_bytes, | ||
| uint64_t& available_memory_bytes) noexcept { | ||
| typename DriverApi::Device device{}; | ||
| typename DriverApi::Context context{}; | ||
| if (!driver.Init()) { | ||
| return "cuInit failed"; | ||
| } | ||
| if (!driver.DeviceGet(&device, device_id)) { | ||
| return "cuDeviceGet failed"; | ||
| } | ||
| size_t total{}; | ||
| if (!driver.DeviceTotalMem(&total, device)) { | ||
| return "cuDeviceTotalMem failed"; | ||
| } | ||
| if (!driver.PrimaryCtxRetain(&context, device)) { | ||
| return "cuDevicePrimaryCtxRetain failed"; | ||
| } | ||
| if (!driver.CtxPushCurrent(context)) { | ||
| driver.PrimaryCtxRelease(device); | ||
| return "cuCtxPushCurrent failed"; | ||
| } | ||
|
|
||
| size_t available{}; | ||
| size_t context_total{}; | ||
| const bool query_ok = driver.MemGetInfo(&available, &context_total); | ||
| typename DriverApi::Context popped_context{}; | ||
| const bool restore_ok = driver.CtxPopCurrent(&popped_context); | ||
| const bool release_ok = driver.PrimaryCtxRelease(device); | ||
| if (!restore_ok) { | ||
| return "cuCtxPopCurrent failed to restore the calling thread's CUDA context"; | ||
| } | ||
| if (!release_ok) { | ||
| return "cuDevicePrimaryCtxRelease failed"; | ||
| } | ||
| if (!query_ok) { | ||
| return "cuMemGetInfo failed"; | ||
| } | ||
|
|
||
| total_memory_bytes = static_cast<uint64_t>(total); | ||
| available_memory_bytes = static_cast<uint64_t>(available); | ||
| return nullptr; | ||
| } | ||
|
|
||
| } // namespace cuda | ||
| } // namespace onnxruntime |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.