-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add the option to create and register a custom allocator from C_API #6689
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
…vice and arena allocators)
Update onnx master
…vice and arena allocators)
Reset global env in testCustomArenaAllocator so won't have a registered allocator of type arena (from previous test)
|
@snnn @skottmckay can you please review this PR? |
|
|
/azp run Linux CPU CI Pipeline,Linux CPU Minimal Build E2E CI Pipeline,Linux CPU x64 NoContribops CI Pipeline,Linux GPU CI Pipeline,Linux GPU TensorRT CI Pipeline,Linux Nuphar CI Pipeline,Linux OpenVINO CI Pipeline,MacOS CI Pipeline,MacOS NoContribops CI Pipeline,Windows CPU CI Pipeline,Windows GPU CI Pipeline,Windows GPU TensorRT CI Pipeline,orttraining-amd-gpu-ci-pipeline,orttraining-distributed,orttraining-linux-ci-pipeline,orttraining-linux-gpu-ci-pipeline,orttraining-ortmodule,orttraining-ortmodule-distributed |
|
You have several pipelines (over 10) configured to build pull requests in this repository. Specify which pipelines you would like to run by using /azp run [pipelines] command. You can specify multiple pipelines using a comma separated list. |
|
/azp run Linux CPU CI Pipeline,Linux CPU Minimal Build E2E CI Pipeline,Linux CPU x64 NoContribops CI Pipeline,Linux GPU CI Pipeline,Linux GPU TensorRT CI Pipeline,Linux Nuphar CI Pipeline,Linux OpenVINO CI Pipeline,MacOS CI Pipeline,MacOS NoContribops CI Pipeline,Windows CPU CI Pipeline |
|
Azure Pipelines successfully started running 10 pipeline(s). |
|
/azp run Windows GPU CI Pipeline,Windows GPU TensorRT CI Pipeline,orttraining-amd-gpu-ci-pipeline,orttraining-distributed,orttraining-linux-ci-pipeline,orttraining-linux-gpu-ci-pipeline,orttraining-ortmodule,orttraining-ortmodule-distributed |
|
Azure Pipelines successfully started running 8 pipeline(s). |
|
@pranavsharma |
One of my team members is working on this feature. After some internal discussions, I don't think we'll be using the PR you created. |
|
@pranavsharma |
This PR extends the existing onnxruntime C_API so it will allow using an external allocator.
Motivation
#6143
When using onnxruntime from an application that has a limited memory consumption, it is essential for the app to monitor and control onnx memory usage. However, onnx is currently using it's own allocator which is separated from the app that is using it. Hence, we add the option to pass an external allocator, so that every memory allocation that occurs while creating and running inference sessions, will use the external allocator.
Description
This PR adds 4 functions to onnxruntime_c_api.h:
CreateCustomDeviceAllocator(uint32_t version, void* AllocFunc(OrtAllocator*, size_t), void FreeFunc(OrtAllocator*, void*), const OrtMemoryInfo* InfoFunc(const OrtAllocator*), OrtAllocator** out);RegisterCustomDeviceAllocator(OrtEnv* env, OrtAllocator *CustomAllocator);CreateCustomArenaAllocator(OrtAllocator* device_allocator, void* AllocFunc(size_t), void FreeFunc(void*), void* ReserveFunc(size_t), size_t UsedFunc(void), size_t MaxFunc(void), OrtAllocatorArena** out);RegisterCustomArenaAllocator(OrtEnv* env, OrtAllocatorArena *CustomArenaAllocator);The first 2 functions concern a custom device allocator. Function (1) allocates a new
OrtAllocator*whose internal fields are the ones that were sent as inputs. Note thatInfoFuncshould return anOrtMemoryInfo*withOrtAllocatorTypefield set toOrtDeviceAllocator. (2) Creates an instance ofAllocatorWrapperthat implements theIAllocatorinterface with theCustomAllocatorfunctionality, and registers this allocator to the givenenvby using the existingRegisterAllocatormechanism. This will guarantee that for every session that is created and associated with env, if it'ssession_optionsis configured to use theenvallocator (and arena is disabled), then the custom allocator will be used instead of the default one.As for (3) and (4), these enable to create and register a custom allocator that implements the arena functionality as defined in the
IArenaAllocatorinterface. An allocator of typeOrtArenaAllocatorshould have an underlinedevice_allocator, along with implementation for the following callbacks:Alloc, Free, Reserve, Used, Max. Therefore, we add the following struct to onnxtunrime_C_API.h:Function (3) allocates and returns the user an
OrtAllocatorArena*whose inner fields are the given inputs (Note that the underlinedevice_allocatorInfoFuncshould return anOrtMemoryInfo*withOrtAllocatorTypefield set toOrtArenaAllocator). For (4) we use a new class calledArenaAllocatorWrapperthat implementsIArenaAllocatoraccording to a givenOrtAllocatorArena*that encapsulate the required implementations. Calling (4) will create and registered the arena allocator to the givenenvby using the existingRegisterAllocatormechanism, similar to (2), so that the custom arena allocator will be used for managing inference sessions associated withenvand configured to use it's env allocator instead of the default one.