Skip to content

Commit f236768

Browse files
authored
[ios] Enable --use_extensions with custom built iOS pod (microsoft#16711)
- Fix link errors by including the needed onnxruntime-extensions libraries in the static framework. - Add Objective-C API to register custom ops from embedded onnxruntime-extensions. Caveat: Not all onnxruntime-extensions build options are working yet. E.g., building with the onnxruntime-extensions OpenCV dependency does not work.
1 parent 4faee2e commit f236768

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

cmake/onnxruntime.cmake

+7
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS)
228228
)
229229
endif()
230230

231+
if (onnxruntime_USE_EXTENSIONS)
232+
list(APPEND onnxruntime_INTERNAL_LIBRARIES
233+
onnxruntime_extensions
234+
ocos_operators
235+
)
236+
endif()
237+
231238
# If you are linking a new library, please add it to the list onnxruntime_INTERNAL_LIBRARIES or onnxruntime_EXTERNAL_LIBRARIES,
232239
# Please do not add a library directly to the target_link_libraries command
233240
target_link_libraries(onnxruntime PRIVATE

objectivec/include/ort_session.h

+14
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ NS_ASSUME_NONNULL_BEGIN
243243
- (BOOL)registerCustomOpsUsingFunctionPointer:(ORTCAPIRegisterCustomOpsFnPtr)registerCustomOpsFn
244244
error:(NSError**)error;
245245

246+
/**
247+
* Registers ONNX Runtime Extensions custom ops that have been built in to ONNX Runtime.
248+
*
249+
* Available since 1.16.
250+
*
251+
* @note ONNX Runtime must have been built with the `--use_extensions` flag for the ONNX Runtime Extensions custom ops
252+
* to be able to be registered with this method. When using a separate ONNX Runtime Extensions library, use
253+
* `registerCustomOpsUsingFunctionPointer:error:` instead.
254+
*
255+
* @param error Optional error information set if an error occurs.
256+
* @return Whether the ONNX Runtime Extensions custom ops were successfully registered.
257+
*/
258+
- (BOOL)enableOrtExtensionsCustomOpsWithError:(NSError**)error;
259+
246260
@end
247261

248262
/**

objectivec/ort_session.mm

+8
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ - (BOOL)registerCustomOpsUsingFunctionPointer:(ORTCAPIRegisterCustomOpsFnPtr)reg
322322
ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
323323
}
324324

325+
- (BOOL)enableOrtExtensionsCustomOpsWithError:(NSError**)error {
326+
try {
327+
_sessionOptions->EnableOrtCustomOps();
328+
return YES;
329+
}
330+
ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error)
331+
}
332+
325333
#pragma mark - Internal
326334

327335
- (Ort::SessionOptions&)CXXAPIOrtSessionOptions {

0 commit comments

Comments
 (0)