Skip to content
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

[WIP] Out-Tree EP feature #21450

Draft
wants to merge 79 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
0e6a80c
opaque pointer for graph
jslhcl Jul 17, 2024
c30a639
ORT C API RegisterOrtExecutionProviderLibrary work
jslhcl Jul 23, 2024
7bfe57e
ORT C-API SessionOptionsAppendOrtExecutionProvider work
jslhcl Jul 23, 2024
8e7d28d
Test Relu with compile based EP, build work, runtime error of loading…
jslhcl Jul 26, 2024
808bfc3
prototype works with hardcode node_compute_info's index in ExecutionP…
jslhcl Jul 29, 2024
49e396c
prototype works without hardcode
jslhcl Jul 29, 2024
e790105
fix comments for Compile function
jslhcl Jul 31, 2024
92f529d
add provider_factory_adapter.h
jslhcl Aug 1, 2024
3d83ed1
fix crash after introducing kernel based EP
jslhcl Aug 5, 2024
e29499a
kernel based EP work with type constraint check commented out
jslhcl Aug 6, 2024
f3678c4
add kernel type constraints from out tree EP
jslhcl Aug 7, 2024
ac5ae0a
add API ReleaseOrtTypeConstraints
jslhcl Aug 7, 2024
0cc78e8
introduce qnn ep
jslhcl Aug 12, 2024
740a687
more graph/node C API
jslhcl Aug 13, 2024
dad6397
stream support
jslhcl Aug 15, 2024
94e9cf7
support data transfer and OrtDevice in out tree EP API
jslhcl Aug 16, 2024
8698517
change compile return type from void to OrtStatusPtr
jslhcl Aug 20, 2024
3d5d2bf
add TensorRT dependency in tensorRT EP's CMakeLists.txt
jslhcl Aug 20, 2024
1f10c28
Add extra parameters in OrtExecutionProvider to avoid capture variabl…
jslhcl Aug 22, 2024
5e46d0f
add OrtGraph_SerializeToArray
jslhcl Aug 23, 2024
85c168d
finish Compile function
jslhcl Aug 24, 2024
7bdb36a
add override function implementation and cudart dependency for tensorrt
jslhcl Aug 26, 2024
7d915b7
add outOfTree tensorrt ep.1 (#21830)
guyang3532 Aug 27, 2024
4aea94b
GetSupportedList
jslhcl Aug 28, 2024
865a17f
GetSubGraph and TensorrtExecutionProviderInfo
jslhcl Aug 29, 2024
2811541
Add simple CUDA allocators for TRT EP (#21901)
chilo-ms Aug 29, 2024
c97b19f
add constructor for tensorrt ep and refine GetCapability (#21914)
guyang3532 Aug 29, 2024
36f97b5
relu can work on out tree TRT now
jslhcl Aug 29, 2024
2fc7aac
rebuild graph proto from scratch with the information needed from gra…
jslhcl Aug 31, 2024
4ad6993
complete the GetCapability (#21956)
guyang3532 Sep 2, 2024
53c736f
Chi's fix and reorder ep for registering shared resource
jslhcl Sep 4, 2024
5fcb972
complete the GetSubGraph (#21998)
guyang3532 Sep 5, 2024
c3bb437
run resnet18v1_7, crash on GetSubGraph()
jslhcl Sep 6, 2024
d1c657c
Merge branch 'leca/outOfTreeEP' of https://github.com/microsoft/onnxr…
jslhcl Sep 6, 2024
3efac97
resnet18-v1-7 works for TRT EP, with next_nodes_list assignment comme…
jslhcl Sep 6, 2024
766fec9
test cases for decoder and fast_rcnn, delete dynamic_cast in ShouldPo…
jslhcl Sep 9, 2024
ea2465c
add tensorrt home in CMakeLists, add trt and CUDA ep for test, change…
jslhcl Sep 11, 2024
76a9305
[WIP, DONT REVIEW] add initializer to graph proto (#22085)
jslhcl Sep 18, 2024
330cdb6
use parameter ExecutionOrder::PRIORITY_BASED for GraphViewerToProto()…
jslhcl Sep 19, 2024
6fd50f0
can create session with out tree trt ep now. Error:Name:'tensorrtEp_T…
jslhcl Sep 23, 2024
681585f
make trt_node_name_with_precision_ from string to map, to capture the…
jslhcl Sep 23, 2024
7db20cb
fix redundant inputs and outputs in GetSubgraph (#22201)
guyang3532 Sep 24, 2024
ff782e0
RunTinyYolov3()
jslhcl Sep 25, 2024
1d7b2df
fix bugs for run tinyYolo (#22233)
guyang3532 Sep 26, 2024
a407944
sample code to separate graph C API to different files
jslhcl Sep 26, 2024
f871b25
new test control_flow, error: ErrorMessage:Failed to find kernel for …
jslhcl Oct 2, 2024
e84f00c
control flow model works
jslhcl Oct 3, 2024
5b2de22
API refactor
jslhcl Oct 7, 2024
b1f8e2a
Python API
jslhcl Oct 14, 2024
7acaaab
fix memory leak (#22444)
guyang3532 Oct 15, 2024
d150a03
refactor all functions in onnxruntime_c_api_ep with status as return …
guyang3532 Oct 17, 2024
da5b6eb
resolve comments
jslhcl Oct 18, 2024
d280e59
add documents for all functions in c_api_ep (#22502)
guyang3532 Oct 18, 2024
cbe98e7
fix comments
jslhcl Oct 19, 2024
1529059
fix memory leak (#22522)
guyang3532 Oct 21, 2024
fa549f8
add mutex to plugin trt ep (#22581)
guyang3532 Oct 24, 2024
a28ad38
use std::mutex instead of OrtMutex and fix build error in Windows
jslhcl Oct 24, 2024
aa49805
openvino
jslhcl Oct 26, 2024
bc65613
openvino, GetCapability almost ready
jslhcl Oct 31, 2024
a1a3eea
openvino GetCapacity() is done. UnregisterPluginExecutionProviderLibrary
jslhcl Nov 1, 2024
0fe5f01
refine compile of openvino ep (#22689)
guyang3532 Nov 1, 2024
6bae1b9
Add utility files (#22650)
chilo-ms Nov 1, 2024
ab75d98
OpenVino, compile() is done
jslhcl Nov 2, 2024
c5510f2
Merge branch 'leca/outOfTreeEP' of https://github.com/microsoft/onnxr…
jslhcl Nov 2, 2024
08e3f20
Add unit test for TRT EP plugin (#22548)
chilo-ms Nov 2, 2024
b0b3123
add test for openvino plugin ep and fix bugs (#22734)
guyang3532 Nov 5, 2024
9dbb0b1
add missing mutex to plugin trt ep
chilo-ms Nov 6, 2024
5a59803
merge code
jslhcl Nov 6, 2024
999e7fd
Merge branch 'leca/outOfTreeEP' of https://github.com/microsoft/onnxr…
jslhcl Nov 6, 2024
084f735
fix bugs (#22744)
guyang3532 Nov 6, 2024
2b1cfdf
relu and resnet works in OpenVINO plugin
jslhcl Nov 7, 2024
e337d8f
Add OrtGraphApis::OrtNode_GetAttributeStrWithSize to handle case wher…
chilo-ms Nov 13, 2024
afe92e1
Make EP plugin be able to create and update EP Context graph (#22740)
chilo-ms Nov 13, 2024
63f8774
[TensorRT EP Plugin] use new graph api for ep context model generation
chilo-ms Nov 14, 2024
bf359a1
use cuda's preferred allocator for plugin trt and builtin cuda combin…
jslhcl Nov 16, 2024
c267ea5
[TensorRT EP Plugin] Add cuda::Impl_Cast (#22908)
chilo-ms Nov 20, 2024
72afdc4
fix build/compiler error for nvcc 11.8
chilo-ms Nov 22, 2024
6822206
Do not expose OrtGraph
jslhcl Dec 3, 2024
c8ddc73
initial commit for Graph C++ API
jslhcl Dec 3, 2024
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
6 changes: 5 additions & 1 deletion cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ if (NOT onnxruntime_MINIMAL_BUILD)
list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_inference.cc)
endif()

if (onnxruntime_TEST_TENSORRT_EP_PLUGIN)
list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_trt_ep_plugin.cc)
endif()

if(onnxruntime_RUN_ONNX_TESTS)
list(APPEND onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_io_types.cc)
endif()
Expand Down Expand Up @@ -1227,7 +1231,7 @@ if (NOT onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
endif()
if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
list(APPEND onnxruntime_perf_test_libs onnxruntime_graph onnxruntime_session onnxruntime_providers onnxruntime_framework onnxruntime_util onnxruntime_mlas onnxruntime_optimizer onnxruntime_flatbuffers iconv re2 gtest absl_failure_signal_handler absl_examine_stack absl_flags_parse absl_flags_usage absl_flags_usage_internal)
endif()
endif()
target_link_libraries(onnxruntime_perf_test PRIVATE ${onnxruntime_perf_test_libs} Threads::Threads)
if(WIN32)
target_link_libraries(onnxruntime_perf_test PRIVATE debug dbghelp advapi32)
Expand Down
4 changes: 4 additions & 0 deletions include/onnxruntime/core/framework/execution_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class IExecutionProvider {
*/
const OrtDevice default_device_;

bool builtin_ep_ = true;

public:
virtual ~IExecutionProvider() = default;

Expand Down Expand Up @@ -325,6 +327,8 @@ class IExecutionProvider {
return InlinedVector<const Node*>();
}

bool IsBuiltInEp() const { return builtin_ep_; }

private:
const std::string type_;

Expand Down
15 changes: 15 additions & 0 deletions include/onnxruntime/core/framework/ort_type_constraints.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format Warning

See https://clang.llvm.org/docs/ClangFormat.html.
Run lintrunner -a to apply this patch.
// Licensed under the MIT License.

#pragma once
#include "core/session/onnxruntime_c_api_ep.h"
#include <unordered_map>
#include <string>
#include <set>

struct OrtTypeConstraints {
bool AddTypeConstraint(const char* type_symbol, ONNXTensorElementDataType type);
inline const std::unordered_map<std::string, std::set<ONNXTensorElementDataType>>& GetTypeConstraints() const { return type_constraints_; };
private:
std::unordered_map<std::string, std::set<ONNXTensorElementDataType>> type_constraints_;
};
12 changes: 12 additions & 0 deletions include/onnxruntime/core/session/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

#include <atomic>
#include <memory>
#include <unordered_set>
#include "core/common/common.h"
#include "core/common/status.h"
#include "core/platform/threadpool.h"
#include "core/common/logging/logging.h"
#include "core/framework/allocator.h"
#include "core/session/onnxruntime_c_api_ep.h"

struct OrtThreadingOptions;
namespace onnxruntime {
Expand Down Expand Up @@ -88,6 +90,15 @@ class Environment {
*/
Status CreateAndRegisterAllocatorV2(const std::string& provider_type, const OrtMemoryInfo& mem_info, const std::unordered_map<std::string, std::string>& options, const OrtArenaCfg* arena_cfg = nullptr);

// TODO(leca): return Status to handle corner cases (plugin EP Factory already exists, etc.)
void InsertPluginEpFactory(const char* ep_name, OrtExecutionProviderFactory* ep_factory);

OrtExecutionProviderFactory* GetPluginExecutionProviderFactory(const std::string& ep_name);

Status DeletePluginEpFactory(const char* ep_name);

std::unordered_set<std::string> GetPluginEpFactoryNames();

private:
ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(Environment);
Status Initialize(std::unique_ptr<logging::LoggingManager> logging_manager,
Expand All @@ -99,5 +110,6 @@ class Environment {
std::unique_ptr<onnxruntime::concurrency::ThreadPool> inter_op_thread_pool_;
bool create_global_thread_pools_{false};
std::vector<AllocatorPtr> shared_allocators_;
std::unordered_map<std::string, std::unique_ptr<OrtExecutionProviderFactory>> plugin_ep_factories_;
};
} // namespace onnxruntime
158 changes: 147 additions & 11 deletions include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ ORT_RUNTIME_CLASS(Op);
ORT_RUNTIME_CLASS(OpAttr);
ORT_RUNTIME_CLASS(Logger);
ORT_RUNTIME_CLASS(ShapeInferContext);
ORT_RUNTIME_CLASS(KernelInfo);
ORT_RUNTIME_CLASS(KernelContext);
ORT_RUNTIME_CLASS(CustomOp);
ORT_RUNTIME_CLASS(KernelRegistry);
ORT_RUNTIME_CLASS(TypeConstraints);
ORT_RUNTIME_CLASS(Device);

#ifdef _WIN32
typedef _Return_type_success_(return == 0) OrtStatus* OrtStatusPtr;
Expand Down Expand Up @@ -364,13 +370,6 @@ typedef enum OrtLanguageProjection {
ORT_PROJECTION_NODEJS = 6,
} OrtLanguageProjection;

struct OrtKernelInfo;
typedef struct OrtKernelInfo OrtKernelInfo;
struct OrtKernelContext;
typedef struct OrtKernelContext OrtKernelContext;
struct OrtCustomOp;
typedef struct OrtCustomOp OrtCustomOp;

typedef enum OrtAllocatorType {
OrtInvalidAllocator = -1,
OrtDeviceAllocator = 0,
Expand All @@ -395,6 +394,13 @@ typedef enum OrtMemoryInfoDeviceType {
OrtMemoryInfoDeviceType_FPGA = 2
} OrtMemoryInfoDeviceType;

typedef enum OrtMemoryType {
OrtMemoryType_Default = 0,
OrtMemoryType_CUDA_PINNED = 1,
OrtMemoryType_HIP_PINNED = 2,
OrtMemoryType_CANN_PINNED = 3,
} OrtMemoryType;

Comment on lines +397 to +403
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we avoid having EP specific enum values in the public API? If 'pinned' equates to 'visible by device and host' there's hopefully a better place to plugin device specific info like CUDA/HIP/CANN than the high level memory type.

/** \brief Algorithm to use for cuDNN Convolution Op
*/
typedef enum OrtCudnnConvAlgoSearch {
Expand Down Expand Up @@ -658,6 +664,9 @@ typedef struct OrtApi OrtApi;
struct OrtTrainingApi;
typedef struct OrtTrainingApi OrtTrainingApi;

struct OrtGraphApi;
typedef struct OrtGraphApi OrtGraphApi;

/** \brief The helper interface to get the right version of OrtApi
*
* Get a pointer to this structure through ::OrtGetApiBase
Expand Down Expand Up @@ -4310,9 +4319,6 @@ struct OrtApi {
*/
const char*(ORT_API_CALL* GetBuildInfoString)(void);

/// \name OrtROCMProviderOptions
/// @{

/** \brief Create an OrtROCMProviderOptions
*
* \param[out] out Newly created ::OrtROCMProviderOptions. Must be released with OrtApi::ReleaseROCMProviderOptions
Expand Down Expand Up @@ -4665,7 +4671,128 @@ struct OrtApi {
_In_reads_(num_external_initializer_files) char* const* external_initializer_file_buffer_array,
_In_reads_(num_external_initializer_files) const size_t* external_initializer_file_lengths,
size_t num_external_initializer_files);
};

/** \brief Create OrtDevice object.
*
* \param[in] device_type
* \param[in] memory_type
* \param[in] device_id
* \param[out] out OrtDevice object
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(CreateDevice, _In_ enum OrtMemoryInfoDeviceType device_type, _In_ enum OrtMemoryType memory_type, _In_ int16_t device_id, _Outptr_ const OrtDevice** out);

/** \brief Get OrtMemoryInfoDeviceType property from OrtDevice object.
*
* \param[in] device OrtDevice object
* \param[out] out OrtMemoryInfoDeviceType property
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(DeviceGetType, _In_ const OrtDevice* device, _Out_ OrtMemoryInfoDeviceType* out);

/** \brief Get OrtMemoryType property from OrtDevice object.
*
* \param[in] device OrtDevice object
* \param[out] out OrtMemoryType property
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(DeviceGetMemoryType, _In_ const OrtDevice* device, _Out_ OrtMemoryType* out);

/** \brief Get device id property from OrtDevice object.
*
* \param[in] device OrtDevice object
* \param[out] out device id property
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(DeviceGetId, _In_ const OrtDevice* device, _Out_ int16_t* out);

/** \brief Release OrtDevice object.
*
* \since Version 1.xx.
*/
ORT_CLASS_RELEASE(Device);

/** \brief Register the plugin ExecutionProvider library
*
* The plugin ExecutionProvider library will be loaded and EP factory object will be created and saved in OrtEnv object
*
* \param[in] lib_path the path of the plugin ExecutionProvider library
* \param[in] env OrtEnv object
* \param[in] ep_name the plugin ExecutionProvider name
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(RegisterPluginExecutionProviderLibrary, _In_ const ORTCHAR_T* lib_path, _In_ OrtEnv* env, _In_ const char* ep_name);

/** \brief Unregister the plugin ExecutionProvider library
*
* The plugin ExecutionProvider factory will be removed from OrtEnv object
*
* \param[in] env OrtEnv object
* \param[in] ep_name the plugin ExecutionProvider name
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(UnregisterPluginExecutionProviderLibrary, _In_ OrtEnv* env, _In_ const char* ep_name);

/** \brief Append the plugin ExecutionProvider factory into the session option with provider options
*
* \param[in] options OrtSessionOptions object
* \param[in] ep_name the plugin ExecutionProvider name
* \param[in] env OrtEnv object
* \param[in] provider_options_keys provider options' keys
* \param[in] provider_options_values provider options' values
* \param[in] num_keys the number of the provider options' key-value pairs
*
* \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.

_In_reads_(num_keys) const char* const* provider_options_keys, _In_reads_(num_keys) const char* const* provider_options_values, _In_ size_t num_keys);

/** \brief Create OrtTypeConstraints object
*
* \param[out] OrtTypeConstraints object
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(CreateOrtTypeConstraints, _Outptr_ OrtTypeConstraints** type_constraints);

/** \brief Add a specific type constraint into OrtTypeConstraints object
*
* \param[in] type_constraints OrtTypeConstraints object
* \param[in] type_symbol symbol string to represent a specific type
* \param[in] type a specific type
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(AddTypeConstraint, _In_ OrtTypeConstraints* type_constraints, _In_ const char* type_symbol, ONNXTensorElementDataType type);

/** \brief Release OrtTypeConstraints object.
*
* \since Version 1.xx.
*/
ORT_CLASS_RELEASE(TypeConstraints);

/** \brief Create KernelCreateInfo with custom op and type constraints, and register it
*
* \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
*
* \since Version 1.xx.
*/
ORT_API2_STATUS(OrtKernelRegistry_RegisterKernel, _In_ OrtKernelRegistry* kernel_registry, _In_ OrtCustomOp* custom_op, _In_ OrtTypeConstraints* type_constraints);

/** \brief Get Graph API
*
* \since Version 1.xx.
*/
const OrtGraphApi*(ORT_API_CALL* GetGraphApi)(uint32_t version)NO_EXCEPTION;
}; // struct OrtApi

/*
* Steps to use a custom op:
Expand Down Expand Up @@ -4693,6 +4820,15 @@ typedef enum OrtCustomOpInputOutputCharacteristic {
* the implementor of the custom op.
*/
struct OrtCustomOp {
#ifdef __cplusplus
OrtCustomOp() : CreateKernel{nullptr}, GetName{nullptr}, GetExecutionProviderType{nullptr}, GetInputType{nullptr},
GetInputTypeCount{nullptr}, GetOutputType{nullptr}, GetOutputTypeCount{nullptr}, KernelCompute{nullptr},
KernelDestroy{nullptr}, GetInputCharacteristic{nullptr}, GetOutputCharacteristic{nullptr},
GetInputMemoryType{nullptr}, GetVariadicInputMinArity{nullptr}, GetVariadicInputHomogeneity{nullptr},
GetVariadicOutputMinArity{nullptr}, GetVariadicOutputHomogeneity{nullptr}, CreateKernelV2{nullptr},
KernelComputeV2{nullptr}, InferOutputShapeFn{nullptr}, GetStartVersion{nullptr}, GetEndVersion{nullptr},
GetMayInplace{nullptr}, ReleaseMayInplace{nullptr}, GetAliasMap{nullptr}, ReleaseAliasMap{nullptr} {}
#endif
uint32_t version; // Must be initialized to ORT_API_VERSION

// This callback creates the kernel, which is a user defined
Expand Down
Loading
Loading