From f236768d5c4bfd3c378400dc86fb6ee760cbaa35 Mon Sep 17 00:00:00 2001 From: Edward Chen <18449977+edgchen1@users.noreply.github.com> Date: Fri, 14 Jul 2023 15:37:16 -0700 Subject: [PATCH] [ios] Enable `--use_extensions` with custom built iOS pod (#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. --- cmake/onnxruntime.cmake | 7 +++++++ objectivec/include/ort_session.h | 14 ++++++++++++++ objectivec/ort_session.mm | 8 ++++++++ 3 files changed, 29 insertions(+) diff --git a/cmake/onnxruntime.cmake b/cmake/onnxruntime.cmake index 07ee8e313f..59ebf8eca4 100644 --- a/cmake/onnxruntime.cmake +++ b/cmake/onnxruntime.cmake @@ -228,6 +228,13 @@ if (onnxruntime_ENABLE_LANGUAGE_INTEROP_OPS) ) endif() +if (onnxruntime_USE_EXTENSIONS) + list(APPEND onnxruntime_INTERNAL_LIBRARIES + onnxruntime_extensions + ocos_operators + ) +endif() + # If you are linking a new library, please add it to the list onnxruntime_INTERNAL_LIBRARIES or onnxruntime_EXTERNAL_LIBRARIES, # Please do not add a library directly to the target_link_libraries command target_link_libraries(onnxruntime PRIVATE diff --git a/objectivec/include/ort_session.h b/objectivec/include/ort_session.h index cfdb3bf822..1b0c47de22 100644 --- a/objectivec/include/ort_session.h +++ b/objectivec/include/ort_session.h @@ -243,6 +243,20 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)registerCustomOpsUsingFunctionPointer:(ORTCAPIRegisterCustomOpsFnPtr)registerCustomOpsFn error:(NSError**)error; +/** + * Registers ONNX Runtime Extensions custom ops that have been built in to ONNX Runtime. + * + * Available since 1.16. + * + * @note ONNX Runtime must have been built with the `--use_extensions` flag for the ONNX Runtime Extensions custom ops + * to be able to be registered with this method. When using a separate ONNX Runtime Extensions library, use + * `registerCustomOpsUsingFunctionPointer:error:` instead. + * + * @param error Optional error information set if an error occurs. + * @return Whether the ONNX Runtime Extensions custom ops were successfully registered. + */ +- (BOOL)enableOrtExtensionsCustomOpsWithError:(NSError**)error; + @end /** diff --git a/objectivec/ort_session.mm b/objectivec/ort_session.mm index 0255e7b093..d27c3e2cef 100644 --- a/objectivec/ort_session.mm +++ b/objectivec/ort_session.mm @@ -322,6 +322,14 @@ NS_ASSUME_NONNULL_BEGIN ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error) } +- (BOOL)enableOrtExtensionsCustomOpsWithError:(NSError**)error { + try { + _sessionOptions->EnableOrtCustomOps(); + return YES; + } + ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error) +} + #pragma mark - Internal - (Ort::SessionOptions&)CXXAPIOrtSessionOptions {