microsoft / microsoft/onnxruntime
C API question - Custom allocator
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 21.9k
- Forks
- 4.2k
- Avg merge
- 4d 11h
- Merged PRs (30d)
- 184
Description
Hi
Is there an option to create a custom allocator using the c API, to be shared across all sessions?
From `C_API.md`:
* **Share allocator(s) between sessions:** Allow multiple sessions in the same process to use the same allocator(s). This
allocator is first registered in the env and then reused by all sessions that use the same env instance unless a session
chooses to override this by setting ```session_state.use_env_allocators``` to "0". Usage of this feature is as follows
* Register an allocator created by ORT using the ```CreateAndRegisterAllocator``` API.
* Set ```session.use_env_allocators``` to "1" for each session that wants to use the env registered allocators.
* See test ```TestSharedAllocatorUsingCreateAndRegisterAllocator``` in
onnxruntime/test/shared_lib/test_inference.cc for an example.
So I wonder if it is possible to override the `OrtAllocator` struct `Alloc` and `Free` functions
```c
typedef struct OrtAllocator {
uint32_t version; // Initialize to ORT_API_VERSION
void*(ORT_API_CALL* Alloc)(struct OrtAllocator* this_, size_t size);
void(ORT_API_CALL* Free)(struct OrtAllocator* this_, void* p);
const struct OrtMemoryInfo*(ORT_API_CALL* Info)(const struct OrtAllocator* this_);
} OrtAllocator;
```
Thanks
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with C_API.md and the TestSharedAllocatorUsingCreateAndRegisterAllocator test in onnxruntime/test/shared_lib/test_inference.cc. Then inspect the OrtAllocator definition and the documented session and environment allocator settings. Done should establish whether overriding Alloc and Free is supported and, if not, define the needed API or documentation change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, cpp
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100