Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
0b5b4d7
update
chilo-ms Dec 8, 2025
80561db
update
chilo-ms Dec 9, 2025
6bd316f
add API summary
chilo-ms Dec 9, 2025
ad0a023
update
chilo-ms Dec 9, 2025
5e398d4
address reviewer's comments and add GetNumCustomOpDomains
chilo-ms Dec 10, 2025
aeb2386
update example ep to run Custom_Mul op
chilo-ms Dec 10, 2025
3849cd3
address reviewr's comment
chilo-ms Dec 10, 2025
9c987be
lintrunner -a
chilo-ms Dec 10, 2025
fbe2434
update example ep GetCapability()
chilo-ms Dec 10, 2025
40fa8fe
update Example EP
chilo-ms Dec 11, 2025
c7a0491
add more comments in API summary
chilo-ms Dec 12, 2025
4787c3f
address reviewer's comments
chilo-ms Dec 18, 2025
632ce31
lintrunner -a
chilo-ms Dec 18, 2025
5905434
Merge branch 'main' into chi/custom_op_for_ep
chilo-ms Jan 7, 2026
6017c00
Use CreateKernelV2 and ComputeKernelV2
chilo-ms Jan 8, 2026
47bb4dc
address reviewer's comments
chilo-ms Jan 8, 2026
6721a98
lintrunner -a
chilo-ms Jan 8, 2026
1ab246d
update
chilo-ms Jan 8, 2026
3478732
Remove accidentally added file
chilo-ms Jan 8, 2026
a1d36af
address reviewer's comments
chilo-ms Jan 9, 2026
3065e9d
address reviewer's comment
chilo-ms Jan 9, 2026
d340de5
address reveiwer's comment
chilo-ms Jan 9, 2026
ee8851b
Merge branch 'main' into chi/custom_op_for_ep
chilo-ms Jan 9, 2026
15f5baf
update
chilo-ms Jan 9, 2026
6b01e7f
lintrunner -a
chilo-ms Jan 9, 2026
adf565e
fix bug when merging main
chilo-ms Jan 9, 2026
062280e
Make auto ep selection be able to register custom op
chilo-ms Jan 13, 2026
ff58721
Merge branch 'main' into chi/custom_op_for_ep
chilo-ms Jan 13, 2026
002fcdc
add comments
chilo-ms Jan 13, 2026
bb7e082
Make code be able to get model_metadata from model during auto ep sel…
chilo-ms Jan 13, 2026
953dbd3
Use Model::Load
chilo-ms Jan 14, 2026
cf5948a
revert unnecessary change
chilo-ms Jan 14, 2026
cc31408
update API comment
chilo-ms Jan 14, 2026
e2604b9
fix build issue for minimal build
chilo-ms Jan 14, 2026
27cb17a
address reviewer's comments
chilo-ms Jan 14, 2026
f77e5f7
Merge branch 'main' into chi/custom_op_for_ep
chilo-ms Jan 14, 2026
2e91855
lintrunner -a
chilo-ms Jan 14, 2026
1f448a6
fix compile warning for minimal build
chilo-ms Jan 14, 2026
debdc79
address reviewer's comment
chilo-ms Jan 14, 2026
452bb26
Add AddEpCustomDomainsToSessionOptions()
chilo-ms Jan 15, 2026
96d42fb
clean up code
chilo-ms Jan 15, 2026
04b75e8
clean up code and fix compile error
chilo-ms Jan 15, 2026
84cff1f
revert auto ep selection
chilo-ms Jan 15, 2026
4d9849f
Merge branch 'main' into chi/custom_op_for_ep
chilo-ms Jan 15, 2026
32e2e57
add back accidentaly removed code
chilo-ms Jan 15, 2026
b80b451
address reviewer's comments
chilo-ms Jan 16, 2026
0b7302e
update
chilo-ms Jan 16, 2026
be94f18
fix compile error for onnxruntime_pybind_state.cc
chilo-ms Jan 16, 2026
1841117
address reveiwer's comment
chilo-ms Jan 16, 2026
6a571ef
address reviewer's comments
chilo-ms Jan 16, 2026
3b2e5a5
address Copilot comment
chilo-ms Jan 17, 2026
5d0b15b
address Copilot comment
chilo-ms Jan 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_ep_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,32 @@ struct OrtEpFactory {
* \since Version 1.24.
*/
ORT_API2_STATUS(SetEnvironmentOptions, _In_ OrtEpFactory* this_ptr, _In_ const OrtKeyValuePairs* options);

/** \brief Create the EP-specific custom op domain list.
*
* This function is used when running inference on a model that contains EP-specific custom operations.
* For compile-based EPs, the EP does not need to provide a concrete kernel implementation for each custom op.
* Instead, it may provide only placeholder custom ops with the correct names so they can be recognized
* during model loading.
*
* Workflow:
* 1. The EP implements this function to supply a list of OrtCustomOpDomain instances.
* 2. The application calls SessionOptionsAppendExecutionProvider_V2() with an OrtEpDevice containing
* the plugin EP's factory.
* 3. SessionOptionsAppendExecutionProvider_V2() appends the provided OrtCustomOpDomain list to the
* session options.
*
* As a result, any session created from these session options will have these custom op domains registered
* in ORT, ensuring that the custom ops are properly recognized and validated when the model is loaded.
*
* Note: EP has the responsibility to release OrtCustomOpDomain instances it creates.
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.24.
*/
ORT_API2_STATUS(CreateCustomOpDomains, _In_ OrtEpFactory* this_ptr,
_Outptr_result_maybenull_ OrtCustomOpDomain** out, _Out_ size_t* num_domains);
};

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/session/onnxruntime_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3332,7 +3332,7 @@ ORT_API_STATUS_IMPL(OrtApis::SessionOptionsAppendExecutionProvider_V2, _In_ OrtS
ep_devices_span,
ep_option_keys_span,
ep_option_vals_span,
session_options->value));
*session_options));

session_options->provider_factories.push_back(std::move(provider_factory));

Expand Down
7 changes: 7 additions & 0 deletions onnxruntime/core/session/plugin_ep/ep_factory_internal_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class EpFactoryInternalImpl {
return nullptr;
}

virtual OrtStatus* CreateCustomOpDomains(_Outptr_result_maybenull_ OrtCustomOpDomain** out,
_Out_ size_t* num_domains) const noexcept {
*out = nullptr;
*num_domains = 0;
return nullptr;
}

// Function ORT calls to release an EP instance.
void ReleaseEp(OrtEp* ep);

Expand Down
35 changes: 34 additions & 1 deletion onnxruntime/core/session/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@
Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices,
gsl::span<const char* const> ep_option_keys,
gsl::span<const char* const> ep_option_vals,
SessionOptions& session_options) {
OrtSessionOptions& ort_session_options) {
SessionOptions& session_options = ort_session_options.value;
const size_t num_ep_options = ep_option_keys.size();
if (ep_option_vals.size() != num_ep_options) {
return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT,
Expand All @@ -505,6 +506,38 @@

ORT_RETURN_IF_ERROR(config_options.AddConfigEntry((prefix + ep_option_keys[j]).c_str(), ep_option_vals[j]));
}

// add custom op domain provided by EP to the session options if any
OrtEpFactory* ep_factory = ep_device->ep_factory;
if (ep_factory) {
auto is_already_in_domains = [&](std::string& domain_name, std::vector<OrtCustomOpDomain*>& domains) {

Check warning on line 513 in onnxruntime/core/session/utils.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <vector> for vector<> [build/include_what_you_use] [4] Raw Output: onnxruntime/core/session/utils.cc:513: Add #include <vector> for vector<> [build/include_what_you_use] [4]

Check warning on line 513 in onnxruntime/core/session/utils.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <string> for string [build/include_what_you_use] [4] Raw Output: onnxruntime/core/session/utils.cc:513: Add #include <string> for string [build/include_what_you_use] [4]
for (auto ptr : domains) {
if (domain_name == ptr->domain_) {
return true;
}
}
return false;
};

if (ep_factory->CreateCustomOpDomains == nullptr) {
continue;
}

size_t num_domains = 0;
OrtCustomOpDomain* domain_ptrs = nullptr;
ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_factory->CreateCustomOpDomains(ep_factory, &domain_ptrs, &num_domains)));

const auto custom_op_domains_span = gsl::span<OrtCustomOpDomain*>(&domain_ptrs, num_domains);
for (auto domain : custom_op_domains_span) {
if (!is_already_in_domains(domain->domain_, ort_session_options.custom_op_domains_) &&
domain->custom_ops_.size() > 0) {
ort_session_options.custom_op_domains_.push_back(domain);
} else {
LOGS_DEFAULT(WARNING) << "The custom op domain name "
<< domain->domain_ << " is already in the session option. Skip it.";
}
}
}
}

return Status::OK();
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/session/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Status CreateIExecutionProviderFactoryForEpDevices(const Environment& env,
Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices,
gsl::span<const char* const> ep_options_keys,
gsl::span<const char* const> ep_options_vals,
SessionOptions& session_options);
OrtSessionOptions& session_options);

} // namespace onnxruntime
#endif // !defined(ORT_MINIMAL_BUILD)
24 changes: 24 additions & 0 deletions onnxruntime/test/autoep/library/example_plugin_ep/ep_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ ExampleEpFactory::ExampleEpFactory(const char* ep_name, ApiPtrs apis, const OrtL
IsStreamAware = IsStreamAwareImpl;
CreateSyncStreamForDevice = CreateSyncStreamForDeviceImpl;

CreateCustomOpDomains = CreateCustomOpDomainsImpl;

// setup the OrtMemoryInfo instances required by the EP.
// We pretend the device the EP is running on is GPU.
default_memory_info_ = Ort::MemoryInfo{"ExampleEP GPU",
Expand Down Expand Up @@ -68,6 +70,9 @@ ExampleEpFactory::ExampleEpFactory(const char* ep_name, ApiPtrs apis, const OrtL
OrtDeviceMemoryType_HOST_ACCESSIBLE,
/*alignment*/ 0,
OrtAllocatorType::OrtDeviceAllocator};

// Custom Op Domain
custom_op_domain_ = Ort::CustomOpDomain{"test"};
}

/*static*/
Expand Down Expand Up @@ -308,3 +313,22 @@ OrtStatus* ORT_API_CALL ExampleEpFactory::CreateSyncStreamForDeviceImpl(OrtEpFac

return nullptr;
}

/*static*/
OrtStatus* ORT_API_CALL ExampleEpFactory::CreateCustomOpDomainsImpl(OrtEpFactory* this_ptr,
_Outptr_result_maybenull_ OrtCustomOpDomain** out,
_Out_ size_t* num_domains) noexcept {
auto* factory = static_cast<ExampleEpFactory*>(this_ptr);

std::vector<std::unique_ptr<PluginEpCustomOp>> created_custom_op_list;
created_custom_op_list.push_back(std::make_unique<PluginEpCustomOp>(factory->ep_name_.c_str(), nullptr));
created_custom_op_list.back().get()->SetName("VariadicNode");
factory->custom_op_domain_.Add(created_custom_op_list.back().get());

*out = factory->custom_op_domain_;
*num_domains = 1;

factory->created_custom_op_list_ = std::move(created_custom_op_list);

return nullptr;
}
75 changes: 75 additions & 0 deletions onnxruntime/test/autoep/library/example_plugin_ep/ep_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,77 @@

#include <mutex>

#include "onnxruntime_c_api.h"
#include "ep_arena.h"
#include "ep_data_transfer.h"
#include "../plugin_ep_utils.h"

// This is a placeholder for "compile-based" plugin EP to provide custom op domains to ORT.
// Please note that this is not for "kernel registration" plugin EP to register kernels.
struct PluginEpCustomKernel {
PluginEpCustomKernel(const OrtKernelInfo* /*info*/, void* compute_stream)
: compute_stream_(compute_stream) {
}

void Compute(OrtKernelContext* /*context*/) {
// The implementation is in plugin EP's compiled bits. No need to implement it here.
};

private:
void* compute_stream_;
};

struct PluginEpCustomOp : Ort::CustomOpBase<PluginEpCustomOp, PluginEpCustomKernel> {
explicit PluginEpCustomOp(const char* provider, void* compute_stream) : provider_(provider),
compute_stream_(compute_stream) {
}

void* CreateKernel(const OrtApi& /* api */, const OrtKernelInfo* info) const {
return new PluginEpCustomKernel(info, compute_stream_);
};

const char* GetName() const { return name_; };

void SetName(const char* name) { name_ = name; };

const char* GetExecutionProviderType() const { return provider_; };

size_t GetInputTypeCount() const { return num_inputs_; };

void SetInputTypeCount(size_t num) { num_inputs_ = num; };

ONNXTensorElementDataType GetInputType(size_t /*index*/) const { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED; };

OrtCustomOpInputOutputCharacteristic GetInputCharacteristic(size_t) const {
return OrtCustomOpInputOutputCharacteristic::INPUT_OUTPUT_VARIADIC;
};

size_t GetOutputTypeCount() const { return num_outputs_; };

void SetOutputTypeCount(size_t num) { num_outputs_ = num; };

ONNXTensorElementDataType GetOutputType(size_t /*index*/) const { return ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED; };

OrtCustomOpInputOutputCharacteristic GetOutputCharacteristic(size_t) const {
return OrtCustomOpInputOutputCharacteristic::INPUT_OUTPUT_VARIADIC;
};

bool GetVariadicInputHomogeneity() const {
return false; // heterogenous
}

bool GetVariadicOutputHomogeneity() const {
return false; // heterogeneous
}

private:
const char* provider_ = nullptr;
void* compute_stream_ = nullptr;
const char* name_ = nullptr;
size_t num_inputs_ = 1; // set to 1 to match with default min_arity for variadic input
size_t num_outputs_ = 1; // set to 1 to match with default min_arity for variadic output
};

/// <summary>
/// Example EP factory that can create an OrtEp and return information about the supported hardware devices.
/// </summary>
Expand Down Expand Up @@ -67,6 +134,10 @@ class ExampleEpFactory : public OrtEpFactory, public ApiPtrs {
const OrtKeyValuePairs* stream_options,
OrtSyncStreamImpl** stream) noexcept;

static OrtStatus* ORT_API_CALL CreateCustomOpDomainsImpl(OrtEpFactory* this_ptr,
_Outptr_result_maybenull_ OrtCustomOpDomain** out,
_Out_ size_t* num_domains) noexcept;

const OrtLogger& default_logger_; // default logger for the EP factory
const std::string ep_name_; // EP name
const std::string vendor_{"Contoso"}; // EP vendor name
Expand All @@ -83,4 +154,8 @@ class ExampleEpFactory : public OrtEpFactory, public ApiPtrs {
std::mutex mutex_; // mutex to protect arena_allocator_ and num_arena_users_

std::unique_ptr<ExampleDataTransfer> data_transfer_impl_; // data transfer implementation for this factory

// std::unique_ptr<OrtCustomOpDomain, std::function<void(OrtCustomOpDomain*)>> custom_op_domain_;
Ort::CustomOpDomain custom_op_domain_;
std::vector<std::unique_ptr<PluginEpCustomOp>> created_custom_op_list_;
};
25 changes: 25 additions & 0 deletions onnxruntime/test/autoep/test_execution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,30 @@ TEST(OrtEpLibrary, KernelPluginEp_Inference) {
gsl::span<const float> output_span(output_data, 6);
EXPECT_THAT(output_span, ::testing::ElementsAre(4, 0, 24, 0, 0, 84));
}

// Creates a session with the example plugin EP and runs a model with a single Mul node.
// Uses AppendExecutionProvider_V2 to append the example plugin EP to the session.
TEST(OrtEpLibrary, PluginEp_Create_OrtCustomOpDomain) {
RegisteredEpDeviceUniquePtr example_ep;
ASSERT_NO_FATAL_FAILURE(Utils::RegisterAndGetExampleEp(*ort_env, Utils::example_ep_info, example_ep));
Ort::ConstEpDevice plugin_ep_device(example_ep.get());

// Create session with example plugin EP
Ort::SessionOptions session_options;
std::unordered_map<std::string, std::string> ep_options;
session_options.AppendExecutionProvider_V2(*ort_env, {plugin_ep_device}, ep_options);

try {
Ort::Session session(*ort_env, ORT_TSTR("testdata/custom_op_variadic_io.onnx"), session_options);
FAIL();
} catch (const Ort::Exception& excpt) {
// The session is expected to pass the custom onnx model validation as example plugin EP provides the custom op domain for VariadicNode op"
// If the custom op domain is not provided, the error message should be:
// "Load model from testdata/custom_op_variadic_io.onnx failed:Fatal error: test:VariadicNode(-1) is not a registered function/op"
ASSERT_THAT(excpt.what(), testing::Not(testing::HasSubstr("test:VariadicNode(-1) is not a registered function/op")));

// But still, session creation is expected to fail as example plugin EP is not able to run VariadicNode op.
}
}
} // namespace test
} // namespace onnxruntime
2 changes: 1 addition & 1 deletion onnxruntime/test/unittest_util/test_dynamic_plugin_ep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ std::unique_ptr<IExecutionProvider> MakeEp(const logging::Logger* logger) {
ORT_THROW_IF_ERROR(AddEpOptionsToSessionOptions(state.selected_c_ep_devices,
default_ep_option_key_cstrs,
default_ep_option_value_cstrs,
ort_session_options.value));
ort_session_options));

return state.ep_factory->CreateProvider(ort_session_options, *logger->ToExternal());
}
Expand Down
Loading