Skip to content
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
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
56 changes: 56 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_ep_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,62 @@ 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.
Copy link
Contributor

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".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced with "provides"

*
* \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 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 implements this function to supply a list of OrtCustomOpDomain instances.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should clarify that the custom op domains are provided by the EP factory, not the EP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, modified to EP factory

* 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.
*
* Plugin EPs can provide two types of custom ops:
* 1. A full OrtCustomOp with a concrete kernel implementation
* - This Example EP demonstrates this approach.
* - 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: EP has the responsibility to release OrtCustomOpDomain instances it creates. It happens
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be the EP factory and not the EP that has this responsibility, right? and the instances must be valid while any session is using them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

* automatically if using ORT C++ api.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, with a name like "CreateCustomOpDomains", there's some expectation of ownership transfer. E.g., how OrtApi::CreateCustomOpDomain() creates a new domain that the user is responsible for releasing.

since this function does not transfer ownership to the caller, maybe a name like GetCustomOpDomains() would be better?

Copy link
Contributor Author

@chilo-ms chilo-ms Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to me to have a name GetCustomOpDomains(). Changed.

*
* \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
*
* \since Version 1.24.
*/
ORT_API2_STATUS(GetCustomOpDomains, _In_ OrtEpFactory* this_ptr,
_Outptr_ OrtCustomOpDomain** domains, _In_ size_t num_domains);
};

#ifdef __cplusplus
Expand Down
71 changes: 71 additions & 0 deletions onnxruntime/core/providers/tensorrt/utilities/common/exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#pragma once

#include <algorithm>
#include <exception>
#include <iterator>
#include <stdexcept>
#include <string>
#include <vector>

#include "core/common/common.h"
#include "core/common/code_location.h"

namespace onnxruntime {

class NotImplementedException : public std::logic_error {
public:
explicit NotImplementedException(const char* _Message = "Function not yet implemented") noexcept : std::logic_error(_Message) {};

Check warning on line 20 in onnxruntime/core/providers/tensorrt/utilities/common/exceptions.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 You don't need a ; after a } [readability/braces] [4] Raw Output: onnxruntime/core/providers/tensorrt/utilities/common/exceptions.h:20: You don't need a ; after a } [readability/braces] [4]
explicit NotImplementedException(const std::string& _Message = "Function not yet implemented") noexcept : std::logic_error(_Message) {};

Check warning on line 21 in onnxruntime/core/providers/tensorrt/utilities/common/exceptions.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 You don't need a ; after a } [readability/braces] [4] Raw Output: onnxruntime/core/providers/tensorrt/utilities/common/exceptions.h:21: You don't need a ; after a } [readability/braces] [4]
};

class TypeMismatchException : public std::logic_error {
public:
TypeMismatchException() noexcept : logic_error("Type mismatch") {};

Check warning on line 26 in onnxruntime/core/providers/tensorrt/utilities/common/exceptions.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 You don't need a ; after a } [readability/braces] [4] Raw Output: onnxruntime/core/providers/tensorrt/utilities/common/exceptions.h:26: You don't need a ; after a } [readability/braces] [4]
};

class OnnxRuntimeException : public std::exception {
public:
OnnxRuntimeException(const CodeLocation& location, const std::string& msg) noexcept
: OnnxRuntimeException(location, nullptr, msg) {
}

/**
Create a new exception that captures the location it was thrown from.
@param location Location in the source code the exception is being thrown from
@param failed_condition Optional string containing the condition that failed.
e.g. "tensor.Size() == input.Size()". May be nullptr.
@param msg Message containing additional information about the exception cause.
*/
OnnxRuntimeException(const CodeLocation& location, const char* failed_condition, const std::string& msg)
: location_{location} {
std::ostringstream ss;

ss << location.ToString(CodeLocation::kFilenameAndPath); // output full path in case just the filename is ambiguous
if (failed_condition != nullptr) {
ss << " " << failed_condition << " was false.";
}

ss << " " << msg << "\n";
if (!location.stacktrace.empty()) {
ss << "Stacktrace:\n";
// skip the first entry in the stacktrace as we have that information from location.ToString()
std::copy(std::next(location.stacktrace.begin()), location.stacktrace.end(), std::ostream_iterator<std::string>(ss, "\n"));
}

what_ = ss.str();
}

const char* what() const noexcept override {
return what_.c_str();
}

private:
const CodeLocation location_;
const std::vector<std::string> stacktrace_;
std::string what_;
};

} // namespace onnxruntime
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
10 changes: 10 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,16 @@ class EpFactoryInternalImpl {
return nullptr;
}

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

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

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

Expand Down
43 changes: 42 additions & 1 deletion onnxruntime/core/session/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@
Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices,

Choose a reason for hiding this comment

The 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?
For auto ep path when it creates plugin ep it goes for inference_session.cc - RegisterExecutionProvider() where it calls GetCustomOpDomainList? Is this understanding correct?

Copy link
Contributor Author

@chilo-ms chilo-ms Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of EP using GetCustomOpDomains to register custom op, only this EP can run the model contains that custom op.
IMO, the application should explicitly call SessionOptionsAppendExecutionProvider_V2() and specify that ep device, rather than using auto ep selection as other devices might not be able to run that custom op.

Copy link

Choose a reason for hiding this comment

The 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,
Expand All @@ -542,6 +543,46 @@

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::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) {
auto is_already_in_domains =
[&](const std::string& domain_name, const std::vector<OrtCustomOpDomain*>& domains) {

Check warning on line 556 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:556: Add #include <vector> for vector<> [build/include_what_you_use] [4]

Check warning on line 556 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:556: Add #include <string> for string [build/include_what_you_use] [4]
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->GetCustomOpDomains(ep_factory,
domains.data(),
domains.size())));

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();
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)
2 changes: 1 addition & 1 deletion onnxruntime/python/onnxruntime_pybind_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ static Status AddEpFactoryFromEpDevices(PySessionOptions& py_sess_options,
ORT_RETURN_IF_ERROR(AddEpOptionsToSessionOptions(ep_devices,
ep_option_keys,
ep_option_vals,
py_sess_options.value));
py_sess_options));

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