Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
59 changes: 59 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_ep_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,65 @@ struct OrtEpFactory {
ORT_API2_STATUS(CreateExternalResourceImporterForDevice, _In_ OrtEpFactory* this_ptr,
_In_ const OrtEpDevice* ep_device,
_Outptr_result_maybenull_ OrtExternalResourceImporterImpl** out_importer);

/** \brief Returns the number of OrtCustomOpDomains that this factory provides.
*
* \param[in] this_ptr The OrtEpFactory instance.
* \param[out] num_domains Output parameter set to the number of provided OrtCustomOpDomain instances.
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.24.
*/
ORT_API2_STATUS(GetNumCustomOpDomains, _In_ OrtEpFactory* this_ptr, _Out_ size_t* num_domains);

/** \brief Gets the EP-specific OrtCustomOpDomains.
*
* This function is used when running inference on a model that contains EP-specific custom operations.
*
* Workflow:
* 1. The EP factory implements this function to supply a list of OrtCustomOpDomain instances.
* 2. The application either 1) calls SessionOptionsAppendExecutionProvider_V2() with an OrtEpDevice containing
* the plugin EP's factory or 2) enables auto ep selection.
* 3. 1) SessionOptionsAppendExecutionProvider_V2() appends the provided OrtCustomOpDomains to the
* session options or 2) ORT registers the OrtCustomOpDomains provided by the EP devices
* that could be potentially selected.
*
* 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.
*
* Plugin EPs can provide two types of custom ops:
* 1. A full OrtCustomOp with a concrete kernel implementation
* - A Plugin EP can supply an OrtCustomOp and a corresponding CustomKernel::Compute() implementation.
* - In GetCapability(), it calls EpGraphSupportInfo_AddSingleNode() to inform ORT
* that the custom node should NOT be fused or compiled. Instead, ORT should invoke
* the custom node's Compute() function at runtime.
*
* 2. A "placeholder" OrtCustomOp with an empty kernel implementation
* - A compile-based Plugin EP can supply an OrtCustomOp whose CustomKernel::Compute()
* does nothing. The purpose is to satisfy model validation during model loading by
* registering the custom op as a valid operator in the session.
* - In GetCapability(), the EP should call EpGraphSupportInfo_AddNodesToFuse() to
* notify ORT that this custom node should be fused and compiled by the EP.
* - In Compile(), the EP executes its compiled bits to perform inference for
* the fused custom node.
*
* Note: The OrtCustomOpDomain instances must be valid while any session is using them.
EP factory has the responsibility to release OrtCustomOpDomain instances it creates. It happens
* automatically if using the C++ Ort::CustomOpDomain class.
*
* \param[in] this_ptr The OrtEpFactory instance.
* \param[out] domains Array of `num_domains` elements pre-allocated by ORT that should be filled with
OrtCustomOpDomain instances created by the EP. The `num_domains` is the value returned by
GetNumCustomOpDomains().
* \param[in] num_domains The size of the `domains` array pre-allocated by ORT.
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*
* \since Version 1.24.
*/
ORT_API2_STATUS(GetCustomOpDomains, _In_ OrtEpFactory* this_ptr,
_Out_writes_all_(num_domains) OrtCustomOpDomain** domains, _In_ size_t num_domains);
};

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions onnxruntime/core/session/onnxruntime_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3390,6 +3390,10 @@ ORT_API_STATUS_IMPL(OrtApis::SessionOptionsAppendExecutionProvider_V2, _In_ OrtS
ep_option_vals_span,
session_options->value));

ORT_API_RETURN_IF_STATUS_NOT_OK(AddEpCustomDomainsToSessionOptions(
ep_devices_span,
*session_options));

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

return nullptr;
Expand Down
12 changes: 12 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 @@ -97,6 +97,18 @@ class EpFactoryInternalImpl {
return nullptr;
}

virtual OrtStatus* GetNumCustomOpDomains(_Out_ size_t* num_domains) const noexcept {
*num_domains = 0;
return nullptr;
}

virtual OrtStatus* GetCustomOpDomains(_Out_writes_all_(num_domains) OrtCustomOpDomain** domains,
_In_ size_t num_domains) const noexcept {
ORT_UNUSED_PARAMETER(domains);
ORT_UNUSED_PARAMETER(num_domains);
return nullptr;
}

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

Expand Down
94 changes: 94 additions & 0 deletions onnxruntime/core/session/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,58 @@ Status TestAutoSelectEPsImpl(const Environment& env, InferenceSession& sess, con

return Status::OK();
}

Status GetCustomOpDomainsFromEpDevice(const OrtEpDevice& ep_device, InlinedVector<OrtCustomOpDomain*>& domains_out) {
InlinedVector<OrtCustomOpDomain*> domains{};

// Get custom op domain provided by EP factory if any.
// OrtEpFactory::GetNumCustomOpDomains and OrtEpFactory::GetCustomOpDomains were added in ORT 1.24.
OrtEpFactory* ep_factory = ep_device.ep_factory;
if (ep_factory &&
ep_factory->ort_version_supported >= 24 &&
ep_factory->GetNumCustomOpDomains != nullptr &&
ep_factory->GetCustomOpDomains != nullptr) {
size_t num_domains = 0;
ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_factory->GetNumCustomOpDomains(ep_factory, &num_domains)));

domains.resize(num_domains);
ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_factory->GetCustomOpDomains(ep_factory, domains.data(),
domains.size())));
}

domains_out = std::move(domains);
return Status::OK();
}

bool DoesDomainWithNameExist(const std::string& domain_name, gsl::span<const OrtCustomOpDomain* const> domains) {
for (auto ptr : domains) {
if (domain_name == ptr->domain_) {
return true;
}
}
return false;
}

bool ShouldAddDomain(const OrtCustomOpDomain* domain_to_add,
gsl::span<const OrtCustomOpDomain* const> existing_domains) {
if (!domain_to_add) {
return false;
}

if (domain_to_add->custom_ops_.size() == 0) {
LOGS_DEFAULT(WARNING) << "Skipping custom op domain '" << domain_to_add->domain_
<< "': custom ops is empty.";
return false;
}

if (DoesDomainWithNameExist(domain_to_add->domain_, existing_domains)) {
LOGS_DEFAULT(WARNING) << "Skipping custom op domain '" << domain_to_add->domain_
<< "': domain already exists in session options.";
return false;
}

return true;
}
} // namespace
#endif // !defined(ORT_MINIMAL_BUILD)

Expand Down Expand Up @@ -195,6 +247,31 @@ static OrtStatus* CreateSessionAndLoadModelImpl(_In_ const OrtSessionOptions* op
}
#endif

#if !defined(ORT_MINIMAL_BUILD)
// Add custom domains for all OrtEpDevice instances to inference session.
// The custom domains should be registered before model load for ORT to validate the custom ops.
if (options != nullptr &&
options->provider_factories.empty() &&
options->value.ep_selection_policy.enable) {
InlinedVector<OrtCustomOpDomain*> all_ep_custom_op_domains;

for (const OrtEpDevice* ep_device : env.GetOrtEpDevices()) {
InlinedVector<OrtCustomOpDomain*> domains;
ORT_API_RETURN_IF_STATUS_NOT_OK(GetCustomOpDomainsFromEpDevice(*ep_device, domains));

for (auto domain : domains) {
if (ShouldAddDomain(domain, options->custom_op_domains_)) {
all_ep_custom_op_domains.push_back(domain);
}
}
}

if (!all_ep_custom_op_domains.empty()) {
ORT_API_RETURN_IF_STATUS_NOT_OK(sess->AddCustomOpDomains(all_ep_custom_op_domains));
}
}
#endif

// Finish load
if (load_config_from_model) {
#if !defined(ORT_MINIMAL_BUILD)
Expand Down Expand Up @@ -546,5 +623,22 @@ Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devic

return Status::OK();
}

Status AddEpCustomDomainsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices,
OrtSessionOptions& ort_session_options) {
for (const OrtEpDevice* ep_device : ep_devices) {
// Add custom domains if EP factory has any.
InlinedVector<OrtCustomOpDomain*> domains;
ORT_RETURN_IF_ERROR(GetCustomOpDomainsFromEpDevice(*ep_device, domains));

for (auto domain : domains) {
if (ShouldAddDomain(domain, ort_session_options.custom_op_domains_)) {
ort_session_options.custom_op_domains_.push_back(domain);
}
}
}

return Status::OK();
}
#endif // !defined(ORT_MINIMAL_BUILD)
} // namespace onnxruntime
4 changes: 4 additions & 0 deletions onnxruntime/core/session/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@ Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devic
gsl::span<const char* const> ep_options_vals,
SessionOptions& session_options);

// Adss EP specific custom domains to the OrtSessionOptions configuration.
Status AddEpCustomDomainsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices,
OrtSessionOptions& ort_session_options);

} // namespace onnxruntime
#endif // !defined(ORT_MINIMAL_BUILD)
3 changes: 3 additions & 0 deletions onnxruntime/python/onnxruntime_pybind_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,9 @@ static Status AddEpFactoryFromEpDevices(PySessionOptions& py_sess_options,
ep_option_vals,
py_sess_options.value));

ORT_RETURN_IF_ERROR(AddEpCustomDomainsToSessionOptions(ep_devices,
py_sess_options));

py_sess_options.provider_factories.push_back(std::move(provider_factory));
return Status::OK();
}
Expand Down
Loading
Loading