Skip to content

Commit 04321f7

Browse files
fs-eirePrathik Rao
authored and
Prathik Rao
committed
upgrade emsdk to 3.1.37 (#15817)
### Description upgrade emsdk to 3.1.37 WIP branch to debug the mystery memory issue in web assembly multi-thread build.
1 parent ec84831 commit 04321f7

File tree

10 files changed

+95
-19
lines changed

10 files changed

+95
-19
lines changed

Diff for: .gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[submodule "cmake/external/emsdk"]
1111
path = cmake/external/emsdk
1212
url = https://github.com/emscripten-core/emsdk.git
13-
branch = 3.1.32
13+
branch = 3.1.37
1414
[submodule "cmake/external/onnxruntime-extensions"]
1515
path = cmake/external/onnxruntime-extensions
1616
url = https://github.com/microsoft/onnxruntime-extensions.git

Diff for: cgmanifests/generated/cgmanifest.json

+50
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,56 @@
7878
"comments": "manylinux dependency LIBXCRYPT"
7979
}
8080
},
81+
{
82+
"component": {
83+
"type": "git",
84+
"git": {
85+
"commitHash": "d10b27fe37736d2944630ecd7557cefa95cf87c9",
86+
"repositoryUrl": "https://gitlab.com/libeigen/eigen.git"
87+
},
88+
"comments": "git submodule at cmake/external/eigen"
89+
}
90+
},
91+
{
92+
"component": {
93+
"type": "git",
94+
"git": {
95+
"commitHash": "b113f24842c6e97fe3e352084db09a6e278593ae",
96+
"repositoryUrl": "https://github.com/emscripten-core/emsdk.git"
97+
},
98+
"comments": "git submodule at cmake/external/emsdk"
99+
}
100+
},
101+
{
102+
"component": {
103+
"type": "git",
104+
"git": {
105+
"commitHash": "7a2ed51a6b682a83e345ff49fc4cfd7ca47550db",
106+
"repositoryUrl": "https://github.com/google/libprotobuf-mutator.git"
107+
},
108+
"comments": "git submodule at cmake/external/libprotobuf-mutator"
109+
}
110+
},
111+
{
112+
"component": {
113+
"type": "git",
114+
"git": {
115+
"commitHash": "9b7bca2a723ff94edcd007d93b5d0cf1838591dc",
116+
"repositoryUrl": "https://github.com/onnx/onnx.git"
117+
},
118+
"comments": "git submodule at cmake/external/onnx"
119+
}
120+
},
121+
{
122+
"component": {
123+
"type": "git",
124+
"git": {
125+
"commitHash": "81e7799c69044c745239202085eb0a98f102937b",
126+
"repositoryUrl": "https://github.com/microsoft/onnxruntime-extensions.git"
127+
},
128+
"comments": "git submodule at cmake/external/onnxruntime-extensions"
129+
}
130+
},
81131
{
82132
"component": {
83133
"type": "git",

Diff for: cmake/adjust_global_compile_flags.cmake

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ if (onnxruntime_BUILD_WEBASSEMBLY)
5151

5252
# Build WebAssembly with multi-threads support.
5353
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
54-
string(APPEND CMAKE_C_FLAGS " -pthread")
55-
string(APPEND CMAKE_CXX_FLAGS " -pthread")
56-
string(APPEND CMAKE_C_FLAGS " -s USE_PTHREADS=1 -Wno-pthreads-mem-growth")
57-
string(APPEND CMAKE_CXX_FLAGS " -s USE_PTHREADS=1 -Wno-pthreads-mem-growth")
54+
string(APPEND CMAKE_C_FLAGS " -pthread -Wno-pthreads-mem-growth")
55+
string(APPEND CMAKE_CXX_FLAGS " -pthread -Wno-pthreads-mem-growth")
5856
endif()
5957
endif()
6058

Diff for: cmake/onnxruntime_unittests.cmake

+11-4
Original file line numberDiff line numberDiff line change
@@ -848,13 +848,20 @@ if (onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
848848
endif()
849849
if (onnxruntime_BUILD_WEBASSEMBLY)
850850
set_target_properties(onnxruntime_test_all PROPERTIES LINK_DEPENDS ${TEST_SRC_DIR}/wasm/onnxruntime_test_all_adapter.js)
851-
set_target_properties(onnxruntime_test_all PROPERTIES LINK_FLAGS "-s ALLOW_MEMORY_GROWTH=1 --pre-js \"${TEST_SRC_DIR}/wasm/onnxruntime_test_all_adapter.js\" -s \"EXPORTED_RUNTIME_METHODS=['FS']\" --preload-file ${CMAKE_CURRENT_BINARY_DIR}/testdata@/testdata -s EXIT_RUNTIME=1")
851+
set_target_properties(onnxruntime_test_all PROPERTIES LINK_FLAGS "-s STACK_SIZE=5242880 -s ALLOW_MEMORY_GROWTH=1 --pre-js \"${TEST_SRC_DIR}/wasm/onnxruntime_test_all_adapter.js\" -s \"EXPORTED_RUNTIME_METHODS=['FS']\" --preload-file ${CMAKE_CURRENT_BINARY_DIR}/testdata@/testdata -s EXIT_RUNTIME=1 -s DEMANGLE_SUPPORT=1")
852852
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
853-
set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " -s USE_PTHREADS=1 -s PROXY_TO_PTHREAD=1")
853+
set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " -s DEFAULT_PTHREAD_STACK_SIZE=131072 -s PROXY_TO_PTHREAD=1")
854854
endif()
855855
if (onnxruntime_USE_JSEP)
856856
set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " --pre-js \"${ONNXRUNTIME_ROOT}/wasm/js_internal_api.js\"")
857857
endif()
858+
859+
###
860+
### if you want to investigate or debug a test failure in onnxruntime_test_all, replace the following line.
861+
### those flags slow down the CI test significantly, so we don't use them by default.
862+
###
863+
# set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=2 -s SAFE_HEAP=1 -s STACK_OVERFLOW_CHECK=2")
864+
set_property(TARGET onnxruntime_test_all APPEND_STRING PROPERTY LINK_FLAGS " -s ASSERTIONS=0 -s SAFE_HEAP=0 -s STACK_OVERFLOW_CHECK=1")
858865
endif()
859866

860867
if (onnxruntime_ENABLE_ATEN)
@@ -1005,7 +1012,7 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
10051012
endif()
10061013
if (onnxruntime_BUILD_WEBASSEMBLY)
10071014
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
1008-
set_target_properties(onnx_test_runner PROPERTIES LINK_FLAGS "-s NODERAWFS=1 -s ALLOW_MEMORY_GROWTH=1 -s USE_PTHREADS=1 -s PROXY_TO_PTHREAD=1 -s EXIT_RUNTIME=1")
1015+
set_target_properties(onnx_test_runner PROPERTIES LINK_FLAGS "-s NODERAWFS=1 -s ALLOW_MEMORY_GROWTH=1 -s PROXY_TO_PTHREAD=1 -s EXIT_RUNTIME=1")
10091016
else()
10101017
set_target_properties(onnx_test_runner PROPERTIES LINK_FLAGS "-s NODERAWFS=1 -s ALLOW_MEMORY_GROWTH=1")
10111018
endif()
@@ -1349,7 +1356,7 @@ if (NOT onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)
13491356
set_target_properties(onnxruntime_mlas_test PROPERTIES FOLDER "ONNXRuntimeTest")
13501357
if (onnxruntime_BUILD_WEBASSEMBLY)
13511358
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
1352-
set_target_properties(onnxruntime_mlas_test PROPERTIES LINK_FLAGS "-s ALLOW_MEMORY_GROWTH=1 -s USE_PTHREADS=1 -s PROXY_TO_PTHREAD=1 -s EXIT_RUNTIME=1")
1359+
set_target_properties(onnxruntime_mlas_test PROPERTIES LINK_FLAGS "-s ALLOW_MEMORY_GROWTH=1 -s PROXY_TO_PTHREAD=1 -s EXIT_RUNTIME=1")
13531360
else()
13541361
set_target_properties(onnxruntime_mlas_test PROPERTIES LINK_FLAGS "-s ALLOW_MEMORY_GROWTH=1")
13551362
endif()

Diff for: cmake/onnxruntime_webassembly.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ else()
242242
target_link_options(onnxruntime_webassembly PRIVATE
243243
"SHELL:-s ASSERTIONS=2"
244244
"SHELL:-s SAFE_HEAP=1"
245-
"SHELL:-s STACK_OVERFLOW_CHECK=1"
245+
"SHELL:-s STACK_OVERFLOW_CHECK=2"
246246
"SHELL:-s DEMANGLE_SUPPORT=1"
247247
)
248248
else()
@@ -265,7 +265,7 @@ else()
265265
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
266266
target_link_options(onnxruntime_webassembly PRIVATE
267267
"SHELL:-s EXPORT_NAME=ortWasmThreaded"
268-
"SHELL:-s USE_PTHREADS=1"
268+
"SHELL:-s DEFAULT_PTHREAD_STACK_SIZE=131072"
269269
)
270270
if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
271271
set_target_properties(onnxruntime_webassembly PROPERTIES OUTPUT_NAME "ort-wasm-simd-threaded")

Diff for: onnxruntime/core/platform/posix/env.cc

+20-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,26 @@ class PosixThread : public EnvThread {
191191
auto [err_no, err_msg] = GetSystemError();
192192
ORT_THROW("pthread_attr_init failed, error code: ", err_no, " error msg: ", err_msg);
193193
}
194-
if (thread_options.stack_size > 0) {
195-
s = pthread_attr_setstacksize(&attr, thread_options.stack_size);
194+
195+
size_t stack_size = thread_options.stack_size;
196+
#if defined(__wasm__)
197+
// emscripten 3.1.37 has a bug which does not take build flags 'STACK_SIZE' or 'DEFAULT_PTHREAD_STACK_SIZE'.
198+
// the pthread stack size will always be 64kB, which is insufficient to run some kernels.
199+
// we set the stack_size to a bigger value
200+
//
201+
// https://github.com/emscripten-core/emscripten/issues/19302
202+
//
203+
// TODO: once this issue is fixed by emscripten's new release, remove this code.
204+
// future changes to DEFAULT_PTHREAD_STACK_SIZE will be in the following files
205+
// - cmake/onnxruntime_unittests.cmake (target onnxruntime_test_all)
206+
// - cmake/onnxruntime_webassembly.cmake (target onnxruntime_webassembly)
207+
//
208+
if (stack_size == 0) {
209+
stack_size = 131072;
210+
}
211+
#endif
212+
if (stack_size > 0) {
213+
s = pthread_attr_setstacksize(&attr, stack_size);
196214
if (s != 0) {
197215
auto [err_no, err_msg] = GetSystemError();
198216
ORT_THROW("pthread_attr_setstacksize failed, error code: ", err_no, " error msg: ", err_msg);

Diff for: tools/ci_build/build.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def convert_arg_line_to_args(self, arg_line):
396396
# WebAssembly build
397397
parser.add_argument("--build_wasm", action="store_true", help="Build for WebAssembly")
398398
parser.add_argument("--build_wasm_static_lib", action="store_true", help="Build for WebAssembly static library")
399-
parser.add_argument("--emsdk_version", default="3.1.19", help="Specify version of emsdk")
399+
parser.add_argument("--emsdk_version", default="3.1.37", help="Specify version of emsdk")
400400

401401
parser.add_argument("--enable_wasm_simd", action="store_true", help="Enable WebAssembly SIMD")
402402
parser.add_argument("--enable_wasm_threads", action="store_true", help="Enable WebAssembly multi-threads support")
@@ -1245,6 +1245,9 @@ def generate_build_tree(
12451245
add_default_definition(emscripten_settings, "MALLOC", args.wasm_malloc)
12461246
add_default_definition(emscripten_settings, "MALLOC", "dlmalloc")
12471247

1248+
# set -s STACK_SIZE=5242880
1249+
add_default_definition(emscripten_settings, "STACK_SIZE", "5242880")
1250+
12481251
if emscripten_settings:
12491252
cmake_args += [f"-Donnxruntime_EMSCRIPTEN_SETTINGS={';'.join(emscripten_settings)}"]
12501253

Diff for: tools/ci_build/github/azure-pipelines/templates/web-ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ stages:
8181
parameters:
8282
CommitOverride: true
8383
BuildConfig: 'Release'
84-
ExtraBuildArgs: '--skip_tests --enable_wasm_api_exception_catching --disable_rtti --use_extensions --cmake_extra_defines onnxruntime_WEBASSEMBLY_DEFAULT_EXTENSION_FLAGS=ON ${{ parameters.ExtraBuildArgs }}'
84+
ExtraBuildArgs: '--target onnxruntime_webassembly --skip_tests --enable_wasm_api_exception_catching --disable_rtti --use_extensions --cmake_extra_defines onnxruntime_WEBASSEMBLY_DEFAULT_EXTENSION_FLAGS=ON ${{ parameters.ExtraBuildArgs }}'
8585
PoolName: ${{ parameters.PoolName }}
8686
BuildJsep: ${{ parameters.BuildJsep }}
8787

Diff for: tools/ci_build/github/azure-pipelines/templates/win-wasm-ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
variables:
3434
EnvSetupScript: setup_env.bat
3535
buildArch: x64
36-
CommonBuildArgs: '--parallel --config ${{ parameters.BuildConfig }} --skip_submodule_sync --cmake_generator "MinGW Makefiles" --build_wasm --use_xnnpack --emsdk_version releases-upstream-4c3772879a04140298c3abde90962d5567b5e2fc-64bit ${{ parameters.ExtraBuildArgs }}'
36+
CommonBuildArgs: '--parallel --config ${{ parameters.BuildConfig }} --skip_submodule_sync --cmake_generator "MinGW Makefiles" --build_wasm --use_xnnpack --emsdk_version releases-7c905cfc1ca6699f6ccb288ae174902cfbdcf0a2-64bit ${{ parameters.ExtraBuildArgs }}'
3737
runCodesignValidationInjection: false
3838
timeoutInMinutes: ${{ parameters.TimeoutInMinutes }}
3939
workspace:
@@ -88,10 +88,10 @@ jobs:
8888
arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)\wasm_threads --enable_wasm_threads'
8989
workingDirectory: '$(Build.BinariesDirectory)'
9090
- task: PythonScript@0
91-
displayName: 'Build and test (node) (simd + threads)'
91+
displayName: 'Build and test (browser) (simd + threads)'
9292
inputs:
9393
scriptPath: '$(Build.SourcesDirectory)\tools\ci_build\build.py'
94-
arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)\wasm_simd_threads --enable_wasm_simd --enable_wasm_threads'
94+
arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)\wasm_simd_threads --enable_wasm_simd --enable_wasm_threads --wasm_run_tests_in_browser'
9595
workingDirectory: '$(Build.BinariesDirectory)'
9696
- task: PythonScript@0
9797
displayName: 'Build and test (node) (simd)'

0 commit comments

Comments
 (0)