Skip to content

Commit 405ca49

Browse files
fs-eirehanbitmythszhanghuanrongwschintianleiwu
authored
build ONNXRuntime into WebAssembly (microsoft#6478)
* Simplified version of WebAssembly support to keep most of existing data structures and add cmake using Ninja and emcmake * Clean up CMakeLists.txt and add an example to create and compute a kernel * Load a model from bytes and remove graph building steps * Add all cpu and contrib ops with mlas library * WebAssembly build with Onnxruntime C/CXX API * Use protobuf cmakefile directory instead of adding every necessary source file * Fix invalid output at example * add missing files * Change an example to use Teams model and support ort mobile format * add API for javascript * fix input releasing in _ort_run() * update API * Let onnxruntime cmake build WebAssembly with option '--wasm' * allow one-step building for wasm * Make build script working on Linux and MacOS * Fix broken build from Windows command * Enable unit test on building WebAssembly * Resolve comments * update build flags * wasm conv improvement from: 1) GemmV; 2) Depthwise direct convolution 3x3; 3) Direct convolution 3x3 * Cleaned mlas unittest. * use glob * update comments * Update baseline due to loss scale fix (microsoft#6948) * fix stream sync issue (microsoft#6954) * Enable type reduction in EyeLike, Mod, random.cc CPU kernels. (microsoft#6960) * Update EyeLike CPU kernel. * Update Mod CPU kernel. * Update Multinomial CPU kernel. * Slight improvement to Pad CPU kernel binary size. * Update RandomNormal[Like], RandomUniform[Like] CPU kernels. * Fix warning from setting multiple MSVC warning level options. (microsoft#6917) Fix warning from setting multiple MSVC warning level options. Replace an existing /Wn flag instead of always appending a new one. * MLAS: quantized GEMM update (microsoft#6916) Various updates to the int8_t GEMMs: 1) Add ARM64 udot kernel to take advantage of dot product instructions available in newer cores. Some models run 4x faster than the stock implementation we used before. 2) Refactor the x64 kernels to share common code for AVX2(u8u8/u8s8/avxvnni) vs AVX512(u8u8/u8s8/avx512vnni) to reduce binary size. 3) Extend kernels to support per-column zero points for matrix B. This is not currently wired to an operator. * Implement QLinearAveragePool with unit tests. (microsoft#6896) Implement QLinearAveragePool with unit tests. * Attention fusion detect num_heads and hidden_size automatically (microsoft#6920) * fixed type to experimental session constructor (microsoft#6950) * fixed type to experimental session constructor Co-authored-by: David Medine <[email protected]> * Update onnxruntime_perf_test.exe to accept free dimension overrides (microsoft#6962) Co-authored-by: Ori Levari <[email protected]> * Fix possible fd leak in NNAPI (microsoft#6966) * Release buffers for prepacked tensors (microsoft#6820) Unsolved problems: 1. One test failure was caused by a bug in Cudnn rnn kernels, when they can allocate a buffer and partially initialize it, the garbage data near tail of the buffer caused problem in some of the hardware. To attack this problem in a broader sense, should we add code in our allocators, and during a memory fuzzing test, fill an allocated buffer with garbage before returning to the caller? 2. Prepacking is used more widely than we know. For instance, Cudnn rnn kernels also cache their weights. They mix several weight tensors together into a single buffer, and never touch the original weight tensor anymore. This is the same idea with pre-pack, but they didn't override the virtual function, and they never tried to release those weight tensors, leading to memory waste. It also seems to me that there are some other kernels have similar behavior. Wonder how much memory we can save if we try to cleanup those too. 3. Turning off memory pattern planning does increase memory fragmentation, leading to out of memory error in some training test cases. Perhaps we can revisit the idea of pushing kernels-creation stage earlier, and then during initializer deserialization, we only avoid tracing those that will be prepacked. * Enable type reduction for Range, ReverseSequence, ScatterND, Split, and Unique CPU kernels. (microsoft#6963) * add CI * fix test in ci * fix flags for nsync in wasm build * add copyright banner * fix wasm source glob * add missing exports * resolve comments * Perf gain by make packb wide to 4 from 16 on GEMM for WASM. Remove no need direct conv in previous perf tuning. * fix buildbreak introduced from latest master merge * fix buildbreak in mlasi.h * resolve all comments except MLAS * rewrite packb related 3 functions for WASM_SCALAR seperately rather than using #ifdef in each. and other changes according to PR feedback in mlas. * More complete scalar path in sgemm from Tracy. * Fix edge case handling in depthwise conv2d kernel 3x3. where: *) support input W==1 and H==1 *) recalc in accurate pad_right and pad_bottom *) support hidden pad_right == 2 or pad_bottom == 2 when W == 1 or H==1 and no pad left/top * Add more test coverage for conv depthwise from Tracy. Fix one typo according to PR. * resolve comments * replace typedef by using * do not use throw in OrtRun() * output error message Co-authored-by: Sunghoon <[email protected]> Co-authored-by: Lei Zhang <[email protected]> Co-authored-by: Wei-Sheng Chin <[email protected]> Co-authored-by: Tianlei Wu <[email protected]> Co-authored-by: Edward Chen <[email protected]> Co-authored-by: Tracy Sharpe <[email protected]> Co-authored-by: David Medine <[email protected]> Co-authored-by: David Medine <[email protected]> Co-authored-by: Ori Levari <[email protected]> Co-authored-by: Ori Levari <[email protected]> Co-authored-by: Guoyu Wang <[email protected]> Co-authored-by: Chen Fu <[email protected]>
1 parent 2aa8998 commit 405ca49

34 files changed

+3562
-142
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,7 @@
7171
[submodule "cmake/external/dlpack"]
7272
path = cmake/external/dlpack
7373
url = https://github.com/dmlc/dlpack.git
74+
[submodule "cmake/external/emsdk"]
75+
path = cmake/external/emsdk
76+
url = https://github.com/emscripten-core/emsdk.git
77+
branch = 2.0.13

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Common use cases for ONNX Runtime:
3636
|Mac|[![Build Status](https://dev.azure.com/onnxruntime/onnxruntime/_apis/build/status/MacOS%20CI%20Pipeline?label=MacOS+CPU)](https://dev.azure.com/onnxruntime/onnxruntime/_build/latest?definitionId=13)<br>[![Build Status](https://dev.azure.com/onnxruntime/onnxruntime/_apis/build/status/MacOS%20NoContribops%20CI%20Pipeline?label=MacOS+NoContribops)](https://dev.azure.com/onnxruntime/onnxruntime/_build/latest?definitionId=65)|||
3737
|Android|||[![Build Status](https://dev.azure.com/onnxruntime/onnxruntime/_apis/build/status/Android%20CI%20Pipeline?label=Android)](https://dev.azure.com/onnxruntime/onnxruntime/_build/latest?definitionId=53)|
3838
|iOS|||[![Build Status](https://dev.azure.com/onnxruntime/onnxruntime/_apis/build/status/iOS%20CI%20Pipeline?label=iOS)](https://dev.azure.com/onnxruntime/onnxruntime/_build/latest?definitionId=134)|
39+
|WebAssembly|||[![Build Status](https://dev.azure.com/onnxruntime/onnxruntime/_apis/build/status/Windows%20WebAssembly%20CI%20Pipeline?label=WASM)](https://dev.azure.com/onnxruntime/onnxruntime/_build/latest?definitionId=161)|
3940

4041

4142
## Data/Telemetry

ThirdPartyNotices.txt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4627,5 +4627,40 @@ https://github.com/dmlc/dlpack
46274627
See the License for the specific language governing permissions and
46284628
limitations under the License.
46294629

4630-
_____
4631-
4630+
_____
4631+
4632+
emsdk
4633+
4634+
MIT/Expat license
4635+
4636+
https://github.com/emscripten-core/emsdk
4637+
4638+
Copyright (c) 2018 Emscripten authors (see AUTHORS in Emscripten)
4639+
4640+
Permission is hereby granted, free of charge, to any person obtaining a copy
4641+
of this software and associated documentation files (the "Software"), to deal
4642+
in the Software without restriction, including without limitation the rights
4643+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
4644+
copies of the Software, and to permit persons to whom the Software is
4645+
furnished to do so, subject to the following conditions:
4646+
4647+
The above copyright notice and this permission notice shall be included in all
4648+
copies or substantial portions of the Software.
4649+
4650+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4651+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4652+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4653+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4654+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4655+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
4656+
SOFTWARE.
4657+
4658+
----------------------------------------------------------------------------
4659+
4660+
This is the MIT/Expat Licence. For more information see:
4661+
4662+
1. http://www.opensource.org/licenses/mit-license.php
4663+
4664+
2. http://en.wikipedia.org/wiki/MIT_License
4665+
4666+
_____

cmake/CMakeLists.txt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ option(onnxruntime_ENABLE_CPU_FP16_OPS "Build with advanced instruction sets" ON
144144
option(onnxruntime_USE_NCCL "Build with NCCL support" OFF)
145145
option(onnxruntime_USE_MPI "Build with MPI support" OFF)
146146

147+
# build WebAssembly
148+
option(onnxruntime_BUILD_WEBASSEMBLY "Enable this option to create WebAssembly byte codes" OFF)
149+
option(onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING "Enable this option to turn on exception catching" OFF)
150+
147151
# Enable bitcode for iOS
148152
option(onnxruntime_ENABLE_BITCODE "Enable bitcode for iOS only" OFF)
149153

@@ -557,6 +561,12 @@ if (onnxruntime_BUILD_UNIT_TESTS)
557561
endif()
558562
if(NOT TARGET GTest::gtest)
559563
message("Use gtest from submodule")
564+
565+
# WebAssembly doesn't support threading properly yet.
566+
if (onnxruntime_BUILD_WEBASSEMBLY)
567+
set(gtest_disable_pthreads ON)
568+
endif()
569+
560570
# gtest and gmock
561571
set_msvc_c_cpp_compiler_warning_level(4)
562572
add_subdirectory(${PROJECT_SOURCE_DIR}/external/googletest EXCLUDE_FROM_ALL)
@@ -607,7 +617,7 @@ if(onnxruntime_BUILD_BENCHMARKS)
607617
endif()
608618
endif()
609619

610-
if(NOT WIN32 AND NOT onnxruntime_PREFER_SYSTEM_LIB)
620+
if(NOT WIN32 AND NOT onnxruntime_PREFER_SYSTEM_LIB AND NOT onnxruntime_BUILD_WEBASSEMBLY)
611621
add_subdirectory(${PROJECT_SOURCE_DIR}/external/nsync EXCLUDE_FROM_ALL)
612622
endif()
613623
# External dependencies
@@ -763,7 +773,7 @@ function(onnxruntime_add_shared_library target_name)
763773
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
764774
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /sdl>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/sdl>")
765775
set_target_properties(${target_name} PROPERTIES VS_CA_EXCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
766-
else()
776+
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
767777
target_compile_definitions(${target_name} PUBLIC -DNSYNC_ATOMIC_CPP11)
768778
target_include_directories(${target_name} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/external/nsync/public")
769779
endif()
@@ -783,7 +793,7 @@ function(onnxruntime_add_shared_library_module target_name)
783793
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
784794
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /sdl>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/sdl>")
785795
set_target_properties(${target_name} PROPERTIES VS_CA_EXCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
786-
else()
796+
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
787797
target_compile_definitions(${target_name} PUBLIC -DNSYNC_ATOMIC_CPP11)
788798
target_include_directories(${target_name} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/external/nsync/public")
789799
endif()
@@ -803,7 +813,7 @@ function(onnxruntime_add_executable target_name)
803813
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
804814
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /sdl>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/sdl>")
805815
set_target_properties(${target_name} PROPERTIES VS_CA_EXCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
806-
else()
816+
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
807817
target_compile_definitions(${target_name} PUBLIC -DNSYNC_ATOMIC_CPP11)
808818
target_include_directories(${target_name} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/external/nsync/public")
809819
endif()
@@ -984,7 +994,7 @@ endif()
984994

985995
# Flatbuffers
986996
# We do not need to build flatc for iOS or Android Cross Compile
987-
if (CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
997+
if (CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "Android" OR onnxruntime_BUILD_WEBASSEMBLY)
988998
set(FLATBUFFERS_BUILD_FLATC OFF CACHE BOOL "FLATBUFFERS_BUILD_FLATC" FORCE)
989999
endif()
9901000
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "FLATBUFFERS_BUILD_TESTS" FORCE)
@@ -1451,7 +1461,7 @@ foreach(target_name onnxruntime_common onnxruntime_graph onnxruntime_framework o
14511461
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
14521462
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /sdl>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/sdl>")
14531463
set_target_properties(${target_name} PROPERTIES VS_CA_EXCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
1454-
else()
1464+
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
14551465
target_compile_definitions(${target_name} PUBLIC -DNSYNC_ATOMIC_CPP11)
14561466
target_include_directories(${target_name} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/external/nsync/public")
14571467
endif()
@@ -1463,7 +1473,7 @@ foreach(provider_name ${ONNXRUNTIME_PROVIDER_NAMES})
14631473
if (MSVC)
14641474
target_compile_options(onnxruntime_providers_${provider_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /utf-8>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/utf-8>")
14651475
target_compile_options(onnxruntime_providers_${provider_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options /sdl>" "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:/sdl>")
1466-
else()
1476+
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
14671477
target_compile_definitions(onnxruntime_providers_${provider_name} PUBLIC -DNSYNC_ATOMIC_CPP11)
14681478
target_include_directories(onnxruntime_providers_${provider_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT} "${CMAKE_CURRENT_SOURCE_DIR}/external/nsync/public")
14691479
endif()
@@ -1483,7 +1493,7 @@ endif()
14831493
if(WIN32)
14841494
list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${SYS_PATH_LIB} Shlwapi)
14851495
list(APPEND onnxruntime_EXTERNAL_LIBRARIES debug Dbghelp)
1486-
else()
1496+
elseif(NOT onnxruntime_BUILD_WEBASSEMBLY)
14871497
list(APPEND onnxruntime_EXTERNAL_LIBRARIES nsync_cpp)
14881498
list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${CMAKE_DL_LIBS} Threads::Threads)
14891499
endif()
@@ -1566,6 +1576,15 @@ if (onnxruntime_BUILD_CSHARP)
15661576
include(onnxruntime_csharp.cmake)
15671577
endif()
15681578

1579+
if (onnxruntime_BUILD_WEBASSEMBLY)
1580+
if (onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING)
1581+
string(APPEND CMAKE_CXX_FLAGS " -s DISABLE_EXCEPTION_CATCHING=0")
1582+
endif()
1583+
1584+
message(STATUS "WebAssembly Build is enabled")
1585+
include(onnxruntime_webassembly.cmake)
1586+
endif()
1587+
15691588
if (WINDOWS_STORE)
15701589
target_link_options(onnxruntime PRIVATE /DYNAMICBASE /NXCOMPAT /APPCONTAINER)
15711590
target_link_options(winml_dll PRIVATE /DYNAMICBASE /NXCOMPAT /APPCONTAINER)

cmake/external/emsdk

Submodule emsdk added at 8b32b7d

cmake/onnxruntime.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ if (NOT WIN32)
7979
else()
8080
set_target_properties(onnxruntime PROPERTIES INSTALL_RPATH "@loader_path")
8181
endif()
82-
else()
82+
elseif (NOT onnxruntime_BUILD_WEBASSEMBLY)
8383
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")
8484
endif()
8585
endif()

cmake/onnxruntime_common.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ file(GLOB onnxruntime_common_src CONFIGURE_DEPENDS
8686
${onnxruntime_common_src_patterns}
8787
)
8888

89+
if (onnxruntime_BUILD_WEBASSEMBLY)
90+
list(REMOVE_ITEM onnxruntime_common_src
91+
"${ONNXRUNTIME_ROOT}/core/platform/posix/ort_mutex.cc"
92+
)
93+
endif()
94+
8995
source_group(TREE ${REPO_ROOT} FILES ${onnxruntime_common_src})
9096

9197
add_library(onnxruntime_common ${onnxruntime_common_src})
@@ -124,7 +130,7 @@ target_include_directories(onnxruntime_common
124130

125131
target_link_libraries(onnxruntime_common Boost::mp11)
126132

127-
if(NOT WIN32)
133+
if(NOT WIN32 AND NOT onnxruntime_BUILD_WEBASSEMBLY)
128134
target_include_directories(onnxruntime_common PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/external/nsync/public")
129135
endif()
130136

cmake/onnxruntime_framework.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ add_dependencies(onnxruntime_framework ${onnxruntime_EXTERNAL_DEPENDENCIES})
4646
# For the shared onnxruntime library, this is set in onnxruntime.cmake through CMAKE_SHARED_LINKER_FLAGS
4747
# But our test files don't use the shared library so this must be set for them.
4848
# For Win32 it generates an absolute path for shared providers based on the location of the executable/onnxruntime.dll
49-
if (UNIX AND NOT APPLE AND NOT onnxruntime_MINIMAL_BUILD)
49+
if (UNIX AND NOT APPLE AND NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_BUILD_WEBASSEMBLY)
5050
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")
5151
endif()
5252

cmake/onnxruntime_mlas.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ set(mlas_common_srcs
2424
${ONNXRUNTIME_ROOT}/core/mlas/lib/qlgavgpool.cpp
2525
)
2626

27-
if(MSVC)
27+
if (onnxruntime_BUILD_WEBASSEMBLY)
28+
file(GLOB_RECURSE mlas_platform_srcs
29+
"${ONNXRUNTIME_ROOT}/core/mlas/lib/wasm/*.cpp"
30+
)
31+
elseif(MSVC)
2832
if(onnxruntime_target_platform STREQUAL "ARM64")
2933
set(mlas_platform_preprocess_srcs
3034
${ONNXRUNTIME_ROOT}/core/mlas/lib/arm64/QgemmU8X8KernelNeon.asm

0 commit comments

Comments
 (0)