Skip to content
Merged
Prev Previous commit
Next Next commit
merge
  • Loading branch information
skottmckay committed Aug 1, 2025
commit f512f2c240654ebc4572fefae87e51a9d78b96dd
38 changes: 36 additions & 2 deletions onnxruntime/test/autoep/library/ep_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,42 @@ OrtStatus* ORT_API_CALL ExampleEpFactory::CreateAllocatorImpl(OrtEpFactory* this
// NOTE: The factory implementation is free to return a shared OrtAllocator* instance instead of creating a new
// allocator on each call. To do this have an allocator instance as an OrtEpFactory class member and make
// ReleaseAllocatorImpl a no-op.
auto device_allocator = std::make_unique<CustomAllocator>(memory_info, factory);
*allocator = device_allocator.release();
//
// NOTE: EP should implement its own arena logic. ep_arena.cc/h is provided as a reference and we use it here for
// device memory. `allocator_options` can be used for arena configuration and there is a helper in ep_arena.h
// to convert from OrtKeyValuePairs to the same arena config settings that ORT uses.
// You are of course free to have completely different settings.

// the read-only allocator is used for initializers. we don't need an arena for that.
if (is_readonly_allocator) {
auto read_only_allocator = std::make_unique<CustomAllocator>(memory_info, factory);
*allocator = read_only_allocator.release();
return nullptr;
}

// create/use the shared arena based allocator
std::lock_guard<std::mutex> lock{factory.mutex_};

if (!factory.arena_allocator_) {
std::unique_ptr<OrtAllocator> ep_allocator = std::make_unique<CustomAllocator>(memory_info, factory);

// initial shared allocator in environment does not have allocator options.
// if the user calls CreateSharedAllocator they can provide options to configure the arena differently.
factory.arena_allocator_using_default_settings_ = allocator_options == nullptr;
RETURN_IF_ERROR(ArenaAllocator::CreateOrtArenaAllocator(std::move(ep_allocator), allocator_options,
factory.ort_api,
factory.default_logger_, factory.arena_allocator_));

} else {
if (factory.arena_allocator_using_default_settings_ && allocator_options) {
// potential change in arena settings. up to EP author to determine how to handle this.
// we should not get here if replacing the shared allocator in the environment, as we free the existing one
// before replacing it. i.e. ReleaseAllocatorImpl should have been called, and arena_allocator_ should be null.
}
}

++factory.num_arena_users_;
*allocator = factory.arena_allocator_.get();

return nullptr;
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.