-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[EP ABI] Add CreateCustomOpDomains() API for plugin EP to register custom ops #26759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
0b5b4d7
80561db
6bd316f
ad0a023
5e398d4
aeb2386
3849cd3
9c987be
fbe2434
40fa8fe
c7a0491
4787c3f
632ce31
5905434
6017c00
47bb4dc
6721a98
1ab246d
3478732
a1d36af
3065e9d
d340de5
ee8851b
15f5baf
6b01e7f
adf565e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1413,6 +1413,49 @@ struct OrtEpFactory { | |
| * \since Version 1.24. | ||
| */ | ||
| ORT_API2_STATUS(SetEnvironmentOptions, _In_ OrtEpFactory* this_ptr, _In_ const OrtKeyValuePairs* options); | ||
|
|
||
| /** \brief Returns the number of OrtCustomOpDomains that this factory creates. | ||
| * | ||
| * \param[in] this_ptr The OrtEpFactory instance. | ||
| * \param[out] num_domains Output parameter set to the number of created 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 Creates the EP-specific OrtCustomOpDomains. | ||
| * | ||
| * 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. It happens | ||
|
||
| * automatically if using ORT C++ api. | ||
|
||
| * | ||
| * \param[in] this_ptr The OrtEpFactory instance. | ||
| * \param[out] domains Pre-allocated array of `num_domains` elements by ORT that should be filled with | ||
| OrtCustomOpDomain created by the EP. | ||
| * \param[in] num_domains The size of the `domains` array pre-allocated by ORT. | ||
| * | ||
| * \snippet{doc} snippets.dox OrtStatus Return Value | ||
adrianlizarraga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * \since Version 1.24. | ||
| */ | ||
| ORT_API2_STATUS(CreateCustomOpDomains, _In_ OrtEpFactory* this_ptr, | ||
| _Outptr_result_maybenull_ OrtCustomOpDomain** domains, _Out_ size_t num_domains); | ||
|
||
| }; | ||
|
|
||
| #ifdef __cplusplus | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -482,7 +482,8 @@ Status CreateIExecutionProviderFactoryForEpDevices(const Environment& env, | |
| Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this not called during auto ep selection path, when the session_options.set_provider_selection_policy(ort.OrtExecutionProviderDevicePolicy.PREFER_GPU) is set?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of EP using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Registering custom ops should be independent of EP , so it should be available in auto ep selectin path as well |
||
| 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, | ||
|
|
@@ -505,6 +506,44 @@ Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devic | |
|
|
||
| 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::GetNumCustomOpDomains and OrtEpFactory::CreateCustomOpDomains | ||
| // were added in ORT 1.24. | ||
| OrtEpFactory* ep_factory = ep_device->ep_factory; | ||
| if (ep_factory && | ||
| ep_factory->ort_version_supported >= 24 && | ||
| ep_factory->CreateCustomOpDomains != nullptr) { | ||
|
||
| auto is_already_in_domains = [&](std::string& domain_name, std::vector<OrtCustomOpDomain*>& domains) { | ||
chilo-ms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (auto ptr : domains) { | ||
| if (domain_name == ptr->domain_) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| size_t num_domains = 0; | ||
| ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_factory->GetNumCustomOpDomains(ep_factory, &num_domains))); | ||
|
|
||
| InlinedVector<OrtCustomOpDomain*> domains; | ||
| domains.resize(num_domains); | ||
|
|
||
| ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_factory->CreateCustomOpDomains(ep_factory, | ||
| domains.data(), | ||
| num_domains))); | ||
|
|
||
| const auto domains_span = gsl::span<OrtCustomOpDomain*>(domains.data(), domains.size()); | ||
| for (auto domain : 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(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we renamed it to
GetCustomOpDomains, maybe replace "creates" with "provides" or "supplies".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced with "provides"