From ff66cfdfa6e53203af11043db742f290bd7e89f7 Mon Sep 17 00:00:00 2001 From: Zuwei Zhao <4123666+Zuwei-Zhao@users.noreply.github.com> Date: Fri, 10 Sep 2021 09:09:16 -0500 Subject: [PATCH] Enable linking in exception throwing support library when build onnxruntime wasm. (#8973) * Enable linking in exception throwing support library when build onnxruntime webassembly containing onnxruntime-extensions. * Add flag in build.py to enable linking exceptions throwing library. * Update onnxruntime-extensions document and bind custom_ops build flag with use_extensions. * Update doc. * Update cgmanifest.json. Co-authored-by: Zuwei Zhao --- cgmanifests/submodules/cgmanifest.json | 2 +- cmake/CMakeLists.txt | 1 + cmake/external/onnxruntime-extensions | 2 +- cmake/onnxruntime_webassembly.cmake | 5 +++++ docs/onnxruntime_extensions.md | 9 ++++++++- tools/ci_build/build.py | 9 ++++++++- 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cgmanifests/submodules/cgmanifest.json b/cgmanifests/submodules/cgmanifest.json index 40a52aca8e..eb914f26df 100644 --- a/cgmanifests/submodules/cgmanifest.json +++ b/cgmanifests/submodules/cgmanifest.json @@ -374,7 +374,7 @@ "component": { "type": "git", "git": { - "commitHash": "97ec95075187b3e6bfbe2820572f9edc93e6ac5b", + "commitHash": "d4b2aff0c890ae38bad87c20f5731333db2a2cc1", "repositoryUrl": "https://github.com/microsoft/onnxruntime-extensions.git" }, "comments": "git submodule at cmake/external/onnxruntime-extensions" diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index d02fc96641..a025135d0f 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -148,6 +148,7 @@ option(onnxruntime_USE_MPI "Build with MPI support" OFF) option(onnxruntime_BUILD_WEBASSEMBLY "Enable this option to create WebAssembly byte codes" OFF) option(onnxruntime_ENABLE_WEBASSEMBLY_THREADS "Enable this option to create WebAssembly byte codes with multi-threads support" OFF) option(onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING "Enable this option to turn on exception catching" OFF) +option(onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_THROWING "Enable this option to turn on exception throwing even if the build disabled exceptions support" OFF) option(onnxruntime_ENABLE_WEBASSEMBLY_DEBUG_INFO "Enable this option to turn on DWARF format debug info" OFF) # Enable bitcode for iOS diff --git a/cmake/external/onnxruntime-extensions b/cmake/external/onnxruntime-extensions index 97ec950751..d4b2aff0c8 160000 --- a/cmake/external/onnxruntime-extensions +++ b/cmake/external/onnxruntime-extensions @@ -1 +1 @@ -Subproject commit 97ec95075187b3e6bfbe2820572f9edc93e6ac5b +Subproject commit d4b2aff0c890ae38bad87c20f5731333db2a2cc1 diff --git a/cmake/onnxruntime_webassembly.cmake b/cmake/onnxruntime_webassembly.cmake index 0a44fd82fe..0a53844162 100644 --- a/cmake/onnxruntime_webassembly.cmake +++ b/cmake/onnxruntime_webassembly.cmake @@ -61,6 +61,11 @@ else() set_property(TARGET onnxruntime_webassembly APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=0 -s SAFE_HEAP=0 -s STACK_OVERFLOW_CHECK=0 -s DEMANGLE_SUPPORT=0") endif() +# Set link flag to enable exceptions support, this will override default disabling exception throwing behavior when disable exceptions. +if (onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_THROWING) + set_property(TARGET onnxruntime_webassembly APPEND_STRING PROPERTY LINK_FLAGS " -s DISABLE_EXCEPTION_THROWING=0") +endif() + if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS) if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD) set_property(TARGET onnxruntime_webassembly APPEND_STRING PROPERTY LINK_FLAGS " -s EXPORT_NAME=ortWasmSimdThreaded -s USE_PTHREADS=1") diff --git a/docs/onnxruntime_extensions.md b/docs/onnxruntime_extensions.md index 4961089c82..189108daa4 100644 --- a/docs/onnxruntime_extensions.md +++ b/docs/onnxruntime_extensions.md @@ -31,7 +31,14 @@ If your model contains operators from onnxruntime-extensions, please add argumen You could even manually edit the **required_operators.config** if you know the custom operators required and don't want to build the shared library. ### Build and Disable Exceptions -You could add argument `--disable_exceptions` to disable exceptions in both onnxruntime and onnxruntime-extensions. However, if the custom operators you used in onnxruntime-extensions (such as BlingFireTokenizer) use c++ exceptions, then you cannot disable it. +You could add argument `--disable_exceptions` to disable exceptions in both onnxruntime and onnxruntime-extensions. + +However, if the custom operators you used in onnxruntime-extensions (such as BlingFireTokenizer) use c++ exceptions, then you will also need to add argument `--enable_wasm_exception_throwing_override` to enable **Emscripten** to link in exception throwing support library. If this argument is not set, Emscripten will throw linking errors. + +### Example Build Command +```console +D:\onnxruntime> build.bat --config Release --build_wasm --enable_wasm_threads --enable_wasm_simd --skip_tests --disable_exceptions --disable_wasm_exception_catching --enable_wasm_exception_throwing_override --disable_rtti --use_extensions --parallel --minimal_build custom_ops --include_ops_by_config D:\required_operators.config +``` ## E2E Example using Custom Operators A common NLP task would probably contain several steps, including pre-processing, DL model and post-processing. It would be very efficient and productive to convert the pre/post processing code snippets into ONNX model since ONNX graph is actually a computation graph, and it can represent the most programming code, theoretically. diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index f31fd02f3f..b44902f7c1 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -345,6 +345,10 @@ def parse_arguments(): parser.add_argument( "--disable_wasm_exception_catching", action='store_true', help="Disable exception catching in WebAssembly.") + parser.add_argument( + "--enable_wasm_exception_throwing_override", action='store_true', + help="Enable exception throwing in WebAssembly, this will override default disabling exception throwing " + "behavior when disable exceptions.") parser.add_argument( "--enable_wasm_threads", action='store_true', help="Enable WebAssembly multi-threads support") @@ -756,7 +760,8 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home "-Donnxruntime_MINIMAL_BUILD=" + ("ON" if args.minimal_build is not None else "OFF"), "-Donnxruntime_EXTENDED_MINIMAL_BUILD=" + ("ON" if args.minimal_build and 'extended' in args.minimal_build else "OFF"), - "-Donnxruntime_MINIMAL_BUILD_CUSTOM_OPS=" + ("ON" if args.minimal_build and 'custom_ops' in args.minimal_build + "-Donnxruntime_MINIMAL_BUILD_CUSTOM_OPS=" + ("ON" if (args.minimal_build is not None and ('custom_ops' in + args.minimal_build or args.use_extensions)) else "OFF"), "-Donnxruntime_REDUCED_OPS_BUILD=" + ("ON" if is_reduced_ops_build(args) else "OFF"), # enable pyop if it is nightly build @@ -794,6 +799,8 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home "-Donnxruntime_ENABLE_WEBASSEMBLY_SIMD=" + ("ON" if args.enable_wasm_simd else "OFF"), "-Donnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING=" + ("OFF" if args.disable_wasm_exception_catching else "ON"), + "-Donnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_THROWING=" + ("ON" if args.enable_wasm_exception_throwing_override + else "OFF"), "-Donnxruntime_ENABLE_WEBASSEMBLY_THREADS=" + ("ON" if args.enable_wasm_threads else "OFF"), "-Donnxruntime_ENABLE_WEBASSEMBLY_DEBUG_INFO=" + ("ON" if args.enable_wasm_debug_info else "OFF"), "-Donnxruntime_WEBASSEMBLY_MALLOC=" + args.wasm_malloc,