microsoft / microsoft/onnxruntime

C API question - Custom allocator

Open
#6,143 6 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api feature request
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.