Skip to content

[WIP] Out-Tree EP feature#21450

Closed
jslhcl wants to merge 84 commits intomainfrom
leca/outOfTreeEP
Closed

[WIP] Out-Tree EP feature#21450
jslhcl wants to merge 84 commits intomainfrom
leca/outOfTreeEP

Conversation

@jslhcl
Copy link
Contributor

@jslhcl jslhcl commented Jul 23, 2024

Description

Out-Tree EP feature.

Motivation and Context

  1. Decouple ExecutionProvider from ONNXRuntime and make it a standalone class which will benefit 3rd party EP authors to write their own EP.
  2. Binary compatibility is required for this feature.

@@ -0,0 +1,35 @@
#pragma once

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
#ifdef __cplusplus
extern "C" {
#endif
OrtExecutionProviderFactory* RegisterCustomEp() {
Copy link
Contributor Author

@jslhcl jslhcl Jul 23, 2024

Choose a reason for hiding this comment

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

return Status instead #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we have to do this? This function will new a factory object by invoking its constructor which has no return type

… EP as graph API is not exported by ORT. Need to put these graph API into ortapi structure
} OrtMetaDef;

typedef struct OrtIndexedSubGraph {
OrtMetaDef* meta_def; // TODO(leca): how to define a nested structure pointer?
Copy link
Contributor

@adrianlizarraga adrianlizarraga Jul 30, 2024

Choose a reason for hiding this comment

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

Does this have to be a pointer to an OrtMetaDef? It may be simpler if this meta_def is contained by value instead. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks we will check the pointer is null or not to distinguish between single node mode and fused node mode (See base class IExecutionProvider::GetCapability() which does not set this pointer and TryAssignSingleNode() which will check this pointer)


OutTreeEp::OutTreeEp(const char* ep_type, const OutTreeEpInfo& ep_info) : info(ep_info) {
type = ep_type;
OrtExecutionProvider::GetCapability = [](const OrtExecutionProvider* this_, const OrtGraphViewer* graph, size_t* cnt, OrtIndexedSubGraph*** indexed_sub_graph) {
Copy link
Contributor

@adrianlizarraga adrianlizarraga Jul 30, 2024

Choose a reason for hiding this comment

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

If I'm understanding correctly, the type of the OrtIndexedSubGraph*** indexed_sub_graph parameter is essentially asking the EP to fill out an array of pointers to OrtIndexedSubGraph objects.

Would it be simpler to change this to OrtIndexedSubgraph** indexed_sub_graph so that the EP fills out an array of OrtIndexedSubGraph objects directly? Each OrtIndexedSubgraph struct is a simple POD that can be created on the stack and copied around. It seems like it would result in less pointer tracking. #Resolved

Copy link
Contributor

@adrianlizarraga adrianlizarraga Jul 30, 2024

Choose a reason for hiding this comment

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

Also, who is responsible for freeing this memory and when? If the EP allocates an array, then the EP should free it. The currently example leaks the allocations.

Edit: one possibility is to have onnxruntime call a new EP function (e.g., ReleaseOrtIndexedSubGraph()) so the the EP can free the memory. onnxruntime would call this once it is done using the indexed_sub_graph.

Copy link
Contributor Author

@jslhcl jslhcl Jul 31, 2024

Choose a reason for hiding this comment

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

The problem is that we don't know how many OrtIndexedSubGraph would be before we call GetCapability() function. I will fix the leak issue in the coming commits

@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,89 @@
#include "kernel_ep.h"

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,36 @@
#pragma once

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,627 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,285 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,266 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,147 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,557 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,110 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,54 @@
#include "qnn_execution_provider.h"

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
@@ -0,0 +1,33 @@
#pragma once

Check warning

Code scanning / lintrunner

CLANGFORMAT/format

See https://clang.llvm.org/docs/ClangFormat.html. Run `lintrunner -a` to apply this patch.
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(SessionOptionsAppendPluginExecutionProvider, _In_ OrtSessionOptions* options, _In_ const char* ep_name, _In_ OrtEnv* env,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this can be removed? We may be able to use the existing C API function called SessionOptionsAppendExecutionProvider. The existing C API does not take an OrtEnv parameter, but we can just get the default OrtEnv since there is only one per process.

@yuslepukhin
Copy link
Member

General comment, please, re-format so the lines do not exceed 120 chars limit.

return InlinedVector<const Node*>();
}

bool IsBuiltInEp() const { return builtin_ep_; }
Copy link
Member

Choose a reason for hiding this comment

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

const

noexcept

#include <string>
#include <set>

struct OrtTypeConstraints {
Copy link
Member

Choose a reason for hiding this comment

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

truct OrtTypeConstraints {

Please, add documentation to all of the classes

#include "core/common/logging/logging.h"
#include "core/framework/allocator.h"
#include "core/session/onnxruntime_c_api_ep.h"

Copy link
Member

Choose a reason for hiding this comment

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

Can we forward declare the types and avoid public header inclusion in the header?


#include <atomic>
#include <memory>
#include <unordered_set>
Copy link
Member

@yuslepukhin yuslepukhin Dec 4, 2024

Choose a reason for hiding this comment

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

<unordered_set>

Please, use inlined containers

*
* \param[in] kernel_registry Opaque pointer of KernelRegistry object
* \param[in] custom_op Custom Op where the kernel compute function is defined
* \param[in] type_constraints
Copy link
Member

Choose a reason for hiding this comment

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

param[in] type_constraints

Given the fact that type_constraing pointer is not-const, does it mean it gets modified or the ownership is taken by the registry? Please, add this to the documentation.

OrtExecutionProvider*(ORT_API_CALL* CreateExecutionProvider)(OrtExecutionProviderFactory* this_, const char* const* ep_option_keys, const char* const* ep_option_values, size_t option_size);
} OrtExecutionProviderFactory;

struct OrtGraphApi {
Copy link
Member

@yuslepukhin yuslepukhin Dec 4, 2024

Choose a reason for hiding this comment

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

This is really a read-only graph viewer.
Suggest to name it appropriatly
Perhaps, the name should reflect the fact that this API is specifically for EP interaction.

ONNXTensorElementDataType data_type;
const char* data;
size_t data_len;
} OrtTensorRef;
Copy link
Member

Choose a reason for hiding this comment

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

This seems to be a repeat of the existing API such as TensorTypeAndShape. Can we re-use that part?

size_t num_external_initializer_files);
};

/** \brief Create OrtDevice object.
Copy link
Member

Choose a reason for hiding this comment

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

/** \brief Create OrtDevice object.

Suggestion is to create a table that is separate from
the main API. Reasons:
Most of the clients do not need that code.
But it does affect language bindings.
For example, we now need to pad C# imported API structure, although it is unlikely we would ever need that in the C#, but if we do we can add that separate.

ORT_API2_STATUS(OrtGraph_IsConstantInitializer, const OrtGraphViewer* graph, const char* name, bool check_outer_scope, _Out_ bool* out);

/** \brief Get the NodeIndex values of the graph nodes sorted in topological order
*
Copy link
Member

@yuslepukhin yuslepukhin Dec 4, 2024

Choose a reason for hiding this comment

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

It looks like the output ptr data is const, please, specify that it is not to be freed by the client.

size_t node_index_len;
} OrtIndexedSubGraph;

typedef struct OrtComputeContext {
Copy link
Member

Choose a reason for hiding this comment

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

Suggest adding a constructor for _cplusplus

/** \brief Gets the path of the owning model if any
*
* \param[in] graph The graph to query
* \param[out] model_path The path of the owning model if any
Copy link
Member

Choose a reason for hiding this comment

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

model_path The path of the owning model if any

Should this be an ORTTCHAR?

* \param[out] out True if the graph is a subgraph
*
*/
ORT_API2_STATUS(OrtGraph_IsSubgraph, const OrtGraphViewer* graph, _Out_ bool* out);
Copy link
Member

Choose a reason for hiding this comment

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

ORT_API2_STATUS

Suggest this returns a bool

* \remarks The caller is responsible for freeing the byte array using OrtFreeMem.
*
*/
ORT_API2_STATUS(OrtGraph_SerializeToArray, const OrtGraphViewer* graph, _Out_ void** data, _Out_ size_t* data_size); // TODO(leca): review and discuss
Copy link
Member

@yuslepukhin yuslepukhin Dec 4, 2024

Choose a reason for hiding this comment

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

oid** data

suggest this to be an array of bytes such as unsigned char

* \param[in] onnx_model_path The file path to save to
*
*/
ORT_API2_STATUS(OrtGraph_DumpOnnxModel, const OrtGraphViewer* graph, const char* onnx_model_path);
Copy link
Member

Choose a reason for hiding this comment

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

const char* onnx_model_path)

ORTTCHAR

auto& eps = GetExecutionProviders();
for (auto& ep : eps) {
ep->RegisterStreamHandlers(GetStreamHandleRegistryInstance(), *allocators_);
std::string register_resource_after = "";
Copy link
Member

Choose a reason for hiding this comment

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

= "";

redundant

std::string register_resource_after = "";
IExecutionProvider* plugin_ep = nullptr;
for (auto& ep : execution_providers_) {
if (register_resource_after == "") {
Copy link
Member

Choose a reason for hiding this comment

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

== ""

.empty()

std::unordered_map<std::string, std::set<ONNXTensorElementDataType>>::iterator iter = type_constraints_.find(type_symbol);
if (iter == type_constraints_.end()) {
std::set<ONNXTensorElementDataType> types{type};
type_constraints_[type_symbol] = types;
Copy link
Member

Choose a reason for hiding this comment

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

types;

std::move()

Copy link
Contributor

@adrianlizarraga adrianlizarraga left a comment

Choose a reason for hiding this comment

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

Some memory/lifetime comments for OrtIndexedSubGraph in GetCapability()

} OrtMetaDef;

typedef struct OrtIndexedSubGraph {
OrtMetaDef* meta_def; // TODO(leca): how to define a nested structure pointer?
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I don't think it's necessary for this to be a separate memory allocation. I see that it was based on our internal IndexedSubGraph implementation, but in my view reducing the number of memory allocation makes things simpler. Perhaps meta_def can be stored inline and can add a boolean to indicate if the meta_def is valid.

typedef struct OrtMetaDef {
  bool is_valid;
  // ...
} OrtMetaDef;

typedef struct OrtIndexedSubGraph {
  OrtMetaDef meta_def;
  // ...
} OrtIndexedSubGraph;

virtual std::vector<std::unique_ptr<ComputeCapability>> GetCapability(const GraphViewer& graph_viewer, const IKernelLookup& kernel_lookup) const override {
size_t cnt = 0;
OrtIndexedSubGraph** indexed_subgraph = nullptr;
if (ep_impl_->GetCapability) ep_impl_->GetCapability(ep_impl_, reinterpret_cast<const OrtGraphViewer*>(&graph_viewer), &cnt, &indexed_subgraph);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is currently passing an OrtIndexedSubGraph*** to the EP's GetCapability() function and asking the EP to allocate an array of pointers to OrtIndexSubGraph objects. Because the EP is allocating the memory, we currently have a separate API to allow the EP to delete this memory.

I wonder if it would be simpler to allow ORT to pass in an OrtAllocator to the EP. The EP would use this ORT-owned allocator to allocate memory for the array. This woud remove the need for a separate C API to clean up the memory. Also, it would allow the parameter to be a OrtIndexedSubGraph** instead of OrtIndexedSubGraph***.

adrianlizarraga added a commit that referenced this pull request Jun 19, 2025
### Description
This PRs sets the foundation for the EP ABI, which allows plugin-EPs to
interface with ORT using a binary stable interface. A plugin-EP can be
built separately from ORT and is not tied to a specific commit of ORT.

Currently, this PR adds basic APIs necessary to allow an example
plugin-EP to compile and run a simple model with a single `Mul` node.

- Example plugin-EP implementation:
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/onnxruntime/test/autoep/library/example_plugin_ep.cc
- APIs: 
- Graph IR:
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/include/onnxruntime/core/session/onnxruntime_c_api.h#L5290-L5439
- Plugin EP:
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/include/onnxruntime/core/session/onnxruntime_c_api.h#L6177-L6481
- Example app code (from unit tests):
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/onnxruntime/test/autoep/test_autoep_selection.cc#L614

### Motivation and Context
Based on #21450

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
@snnn
Copy link
Contributor

snnn commented Jul 3, 2025

This pull request has been automatically closed because it has merge conflicts and has been inactive for more than 30 days. Please rebase on the target branch and open a new PR.

@snnn snnn closed this Jul 3, 2025
quic-ankus pushed a commit to CodeLinaro/onnxruntime that referenced this pull request Nov 25, 2025
### Description
This PRs sets the foundation for the EP ABI, which allows plugin-EPs to
interface with ORT using a binary stable interface. A plugin-EP can be
built separately from ORT and is not tied to a specific commit of ORT.

Currently, this PR adds basic APIs necessary to allow an example
plugin-EP to compile and run a simple model with a single `Mul` node.

- Example plugin-EP implementation:
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/onnxruntime/test/autoep/library/example_plugin_ep.cc
- APIs: 
- Graph IR:
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/include/onnxruntime/core/session/onnxruntime_c_api.h#L5290-L5439
- Plugin EP:
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/include/onnxruntime/core/session/onnxruntime_c_api.h#L6177-L6481
- Example app code (from unit tests):
https://github.com/microsoft/onnxruntime/blob/adrianl/ep-abi/onnxruntime/test/autoep/test_autoep_selection.cc#L614

### Motivation and Context
Based on microsoft#21450

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants