microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.4.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

ci_build/onnxruntime_integration/onnxruntime_v1.8.patch

244lines · modecode

1diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
2index 95bd1416c4..8c736bd69f 100644
3--- a/cmake/CMakeLists.txt
4+++ b/cmake/CMakeLists.txt
5@@ -1195,6 +1195,15 @@ if (onnxruntime_USE_TVM)
6 list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES tvm nnvm_compiler)
7 endif()
8
9+# ONNXRuntime-CustomOps
10+set(OCOS_ENABLE_CTEST OFF CACHE INTERNAL "")
11+set(OCOS_ENABLE_STATIC_LIB ON CACHE INTERNAL "")
12+set(OCOS_ENABLE_SPM_TOKENIZER OFF CACHE INTERNAL "")
13+add_subdirectory(external/onnxruntime-extensions EXCLUDE_FROM_ALL)
14+# target library or executable are defined in CMakeLists.txt of onnxruntime-extensions
15+target_include_directories(ocos_operators PRIVATE ${RE2_INCLUDE_DIR} external/json/include)
16+target_include_directories(ortcustomops PUBLIC external/onnxruntime-extensions/shared)
17+
18 if (APPLE OR CMAKE_SYSTEM_NAME STREQUAL "Android")
19 #onnx/onnx/proto_utils.h:34:16: error: 'SetTotalBytesLimit' is deprecated: Please use the single
20 #parameter version of SetTotalBytesLimit(). The second parameter is ignored.
21diff --git a/cmake/onnxruntime_session.cmake b/cmake/onnxruntime_session.cmake
22index bb5eb0d166..bbc7a954e9 100644
23--- a/cmake/onnxruntime_session.cmake
24+++ b/cmake/onnxruntime_session.cmake
25@@ -16,7 +16,7 @@ if(onnxruntime_ENABLE_INSTRUMENT)
26 target_compile_definitions(onnxruntime_session PUBLIC ONNXRUNTIME_ENABLE_INSTRUMENT)
27 endif()
28 target_include_directories(onnxruntime_session PRIVATE ${ONNXRUNTIME_ROOT} ${eigen_INCLUDE_DIRS})
29-target_link_libraries(onnxruntime_session PRIVATE nlohmann_json::nlohmann_json)
30+target_link_libraries(onnxruntime_session PRIVATE nlohmann_json::nlohmann_json ortcustomops)
31 add_dependencies(onnxruntime_session ${onnxruntime_EXTERNAL_DEPENDENCIES})
32 set_target_properties(onnxruntime_session PROPERTIES FOLDER "ONNXRuntime")
33 if (onnxruntime_USE_CUDA)
34diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h
35index 838d202c28..e6db1b6eaa 100644
36--- a/include/onnxruntime/core/session/onnxruntime_c_api.h
37+++ b/include/onnxruntime/core/session/onnxruntime_c_api.h
38@@ -1382,6 +1382,11 @@ struct OrtApi {
39 _In_ const void* model_data, size_t model_data_length,
40 _In_ const OrtSessionOptions* options, _Inout_ OrtPrepackedWeightsContainer* prepacked_weights_container,
41 _Outptr_ OrtSession** out);
42+
43+ /**
44+ * Enable custom operators in ORT CustomOps: https://github.com/microsoft/onnxruntime-extensions.git
45+ */
46+ ORT_API2_STATUS(EnableOrtCustomOps, _Inout_ OrtSessionOptions* options);
47 };
48
49 /*
50diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h
51index 4bd07b47c6..0fa74bcf38 100644
52--- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h
53+++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h
54@@ -310,6 +310,7 @@ struct SessionOptions : Base<OrtSessionOptions> {
55
56 SessionOptions& EnableProfiling(const ORTCHAR_T* profile_file_prefix);
57 SessionOptions& DisableProfiling();
58+ SessionOptions& EnableOrtCustomOps();
59
60 SessionOptions& EnableMemPattern();
61 SessionOptions& DisableMemPattern();
62diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h
63index d08b890e80..f4b78b562f 100644
64--- a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h
65+++ b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h
66@@ -440,6 +440,11 @@ inline SessionOptions& SessionOptions::DisableProfiling() {
67 return *this;
68 }
69
70+inline SessionOptions& SessionOptions::EnableOrtCustomOps() {
71+ ThrowOnError(GetApi().EnableOrtCustomOps(p_));
72+ return *this;
73+}
74+
75 inline SessionOptions& SessionOptions::EnableMemPattern() {
76 ThrowOnError(GetApi().EnableMemPattern(p_));
77 return *this;
78diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc
79index e73bdbe18a..46238f02df 100644
80--- a/onnxruntime/core/session/onnxruntime_c_api.cc
81+++ b/onnxruntime/core/session/onnxruntime_c_api.cc
82@@ -35,6 +35,9 @@
83 #include "abi_session_options_impl.h"
84 #include "core/framework/TensorSeq.h"
85 #include "core/platform/ort_mutex.h"
86+
87+#include "ortcustomops.h"
88+
89 #ifdef USE_CUDA
90 #include "core/providers/cuda/cuda_provider_factory.h"
91 #endif
92@@ -403,6 +406,13 @@ ORT_API_STATUS_IMPL(OrtApis::RegisterCustomOpsLibrary, _Inout_ OrtSessionOptions
93 API_IMPL_END
94 }
95
96+ORT_API_STATUS_IMPL(OrtApis::EnableOrtCustomOps, _Inout_ OrtSessionOptions* options) {
97+ API_IMPL_BEGIN
98+
99+ return RegisterCustomOps(options, OrtGetApiBase());
100+ API_IMPL_END
101+}
102+
103 namespace {
104 // provider either model_path, or modal_data + model_data_length.
105 static ORT_STATUS_PTR CreateSessionAndLoadModel(_In_ const OrtSessionOptions* options,
106@@ -2228,6 +2238,7 @@ static constexpr OrtApi ort_api_1_to_8 = {
107 // End of Version 8 - DO NOT MODIFY ABOVE (see above text for more information)
108
109 // Version 9 - In development, feel free to add/remove/rearrange here
110+ &OrtApis::EnableOrtCustomOps,
111 };
112
113 // Assert to do a limited check to ensure Version 1 of OrtApi never changes (will detect an addition or deletion but not if they cancel out each other)
114diff --git a/onnxruntime/core/session/ort_apis.h b/onnxruntime/core/session/ort_apis.h
115index 2d99582724..0588e5af42 100644
116--- a/onnxruntime/core/session/ort_apis.h
117+++ b/onnxruntime/core/session/ort_apis.h
118@@ -277,4 +277,5 @@ ORT_API_STATUS_IMPL(CreateSessionFromArrayWithPrepackedWeightsContainer, _In_ co
119 _In_ const OrtSessionOptions* options, _Inout_ OrtPrepackedWeightsContainer* prepacked_weights_container,
120 _Outptr_ OrtSession** out);
121
122+ORT_API_STATUS_IMPL(EnableOrtCustomOps, _Inout_ OrtSessionOptions* options);
123 } // namespace OrtApis
124diff --git a/onnxruntime/test/shared_lib/test_inference.cc b/onnxruntime/test/shared_lib/test_inference.cc
125index 4f04588d93..d5c287893b 100644
126--- a/onnxruntime/test/shared_lib/test_inference.cc
127+++ b/onnxruntime/test/shared_lib/test_inference.cc
128@@ -173,6 +173,8 @@ static constexpr PATH_TYPE VARIED_INPUT_CUSTOM_OP_MODEL_URI_2 = TSTR("testdata/f
129 static constexpr PATH_TYPE OPTIONAL_INPUT_OUTPUT_CUSTOM_OP_MODEL_URI = TSTR("testdata/foo_bar_1.onnx");
130 static constexpr PATH_TYPE OPTIONAL_INPUT_OUTPUT_CUSTOM_OP_MODEL_URI_2 = TSTR("testdata/foo_bar_2.onnx");
131 static constexpr PATH_TYPE CUSTOM_OP_MODEL_WITH_ATTRIBUTES_URI = TSTR("testdata/foo_bar_3.onnx");
132+static constexpr PATH_TYPE ORT_CUSTOM_OPS_MODEL_URI = TSTR("testdata/custom_op_string_lower.onnx");
133+static constexpr PATH_TYPE ORT_CUSTOM_OPS_MODEL_URI_2 = TSTR("testdata/custom_op_negpos.onnx");
134
135 #ifdef ENABLE_LANGUAGE_INTEROP_OPS
136 static constexpr PATH_TYPE PYOP_FLOAT_MODEL_URI = TSTR("testdata/pyop_1.onnx");
137@@ -266,6 +268,91 @@ TEST(CApiTest, custom_op_handler) {
138 #endif
139 }
140
141+TEST(CApiTest, test_enable_ort_customops_negpos) {
142+
143+ Ort::MemoryInfo info("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault);
144+ auto allocator = onnxruntime::make_unique<MockedOrtAllocator>();
145+
146+ // Create Inputs
147+ std::vector<Ort::Value> ort_inputs;
148+ std::vector<float> input_data = {-1.1f, 2.2f, 4.4f, -5.5f};
149+ std::vector<int64_t> input_dims = {2, 2};
150+ ort_inputs.emplace_back(Ort::Value::CreateTensor<float>(info, const_cast<float*>(input_data.data()), input_data.size(), input_dims.data(), input_dims.size()));
151+
152+ // Create Session with ORT CustomOps
153+ Ort::SessionOptions session_options;
154+ session_options.EnableOrtCustomOps();
155+ Ort::Session session(*ort_env, ORT_CUSTOM_OPS_MODEL_URI_2, session_options);
156+
157+ // Create Input and Output Names
158+ std::vector<const char*> input_names = {"X"};
159+ const char* output_names[] = {"out0", "out1"};
160+
161+ // Run Session
162+ std::vector<Ort::Value> ort_outputs = session.Run(Ort::RunOptions{}, input_names.data(), ort_inputs.data(), ort_inputs.size(), output_names, countof(output_names));
163+
164+ // Validate Results
165+ ASSERT_EQ(ort_outputs.size(), 2u);
166+
167+ std::vector<int64_t> out_dims = {2, 2};
168+ std::vector<float> values_out0 = {-1.1f, 0.0f, 0.0f, -5.5f};
169+ auto type_info = ort_outputs[0].GetTensorTypeAndShapeInfo();
170+ ASSERT_EQ(type_info.GetShape(), out_dims);
171+ size_t total_len = type_info.GetElementCount();
172+ ASSERT_EQ(values_out0.size(), total_len);
173+
174+ float* f = ort_outputs[0].GetTensorMutableData<float>();
175+ for (size_t i = 0; i != total_len; ++i) {
176+ ASSERT_EQ(values_out0[i], f[i]);
177+ }
178+}
179+
180+TEST(CApiTest, test_enable_ort_customops_stringlower) {
181+
182+ auto allocator = onnxruntime::make_unique<MockedOrtAllocator>();
183+
184+ // Create Inputs
185+ std::vector<Ort::Value> ort_inputs;
186+ std::string input_data{"HI, This is ENGINEER from Microsoft."};
187+ const char* const input_strings[] = {input_data.c_str()};
188+ std::vector<int64_t> input_dims = {1, 1};
189+
190+ Ort::Value input_tensor = Ort::Value::CreateTensor(allocator.get(), input_dims.data(), input_dims.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING);
191+ input_tensor.FillStringTensor(input_strings, 1U);
192+ ort_inputs.push_back(std::move(input_tensor));
193+
194+ // Create Session with ORT CustomOps
195+ Ort::SessionOptions session_options;
196+ session_options.EnableOrtCustomOps();
197+ Ort::Session session(*ort_env, ORT_CUSTOM_OPS_MODEL_URI, session_options);
198+
199+ // Create Input and Output Names
200+ std::vector<const char*> input_names = {"input_1"};
201+ const char* output_names[] = {"customout"};
202+
203+ // Run Session
204+ std::vector<Ort::Value> ort_outputs = session.Run(Ort::RunOptions{nullptr}, input_names.data(), ort_inputs.data(), ort_inputs.size(), output_names, countof(output_names));
205+
206+ // Validate Results
207+ ASSERT_EQ(ort_outputs.size(), 1u);
208+
209+ std::vector<int64_t> out_dims = {1, 1};
210+ auto type_info = ort_outputs[0].GetTensorTypeAndShapeInfo();
211+ ASSERT_EQ(type_info.GetShape(), out_dims);
212+ ASSERT_EQ(type_info.GetElementType(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING);
213+
214+ std::string output_data{"hi, this is engineer from microsoft."};
215+ auto expected_string = output_data.c_str();
216+ size_t expected_string_len = strlen(expected_string);
217+ auto data_length = ort_outputs[0].GetStringTensorDataLength();
218+ ASSERT_EQ(expected_string_len, data_length);
219+
220+ std::string result(data_length, '\0');
221+ std::vector<size_t> offsets(type_info.GetElementCount());
222+ ort_outputs[0].GetStringTensorContent((void*)result.data(), data_length, offsets.data(), offsets.size());
223+ ASSERT_STREQ(result.c_str(), expected_string);
224+}
225+
226 //test custom op which accepts float and double as inputs
227 TEST(CApiTest, varied_input_custom_op_handler) {
228 std::vector<Input> inputs(2);
229diff --git a/onnxruntime/wasm/api.cc b/onnxruntime/wasm/api.cc
230index 8413197086..16e8479d35 100644
231--- a/onnxruntime/wasm/api.cc
232+++ b/onnxruntime/wasm/api.cc
233@@ -122,6 +122,11 @@ OrtSession* OrtCreateSession(void* data, size_t data_length, OrtSessionOptions*
234 return nullptr;
235 }
236
237+ // Enable ORT CustomOps
238+ // TODO: add condition check here to enable ORT CustomOps
239+ // session_options->EnableOrtCustomOps();
240+ RETURN_NULLPTR_IF_ERROR(EnableOrtCustomOps, session_options);
241+
242 #if defined(__EMSCRIPTEN_PTHREADS__)
243 RETURN_NULLPTR_IF_ERROR(DisablePerSessionThreads, session_options);
244 #else
245