Skip to content

Commit

Permalink
wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
decahedron1 committed May 25, 2024
1 parent 7e07c81 commit 8ec9fbc
Show file tree
Hide file tree
Showing 10 changed files with 987 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ jobs:
static: true
feature-set: none
runs-on: windows-2022
- target: wasm32-unknown-unknown
args: "-W --xnnpack"
static: true
feature-set: none
runs-on: ubuntu-22.04
steps:
- name: Install cross-compile tools
if: matrix.target == 'aarch64-unknown-linux-gnu'
Expand Down
88 changes: 72 additions & 16 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,60 @@ await new Command()

$.cd(onnxruntimeRoot);

await $`git reset --hard HEAD`;
await $`git clean -fd`;
if (options.wasm) {
const patchDir = join(root, 'src', 'patches', 'wasm');
for await (const patchFile of Deno.readDir(patchDir)) {
if (!patchFile.isFile) {
continue;
}

await $`git apply ${join(patchDir, patchFile.name)} --ignore-whitespace --recount --verbose`;
console.log(`applied ${patchFile.name}`);
}

// there's no WAY im gonna try to wrestle with CMake on this one
await $`./build.bat --build_wasm_static_lib --disable_rtti --disable_exceptions --disable_wasm_exception_catching --skip_tests --config Release --parallel --minimal_build`;

const buildRoot = join(onnxruntimeRoot, 'build', 'Linux', 'Release');
let originalArDesc = await Deno.readTextFile(join(buildRoot, 'onnxruntime_webassembly.ar')).then(c => c.trim().split('\n'));
// slice off SAVE, END lines
originalArDesc = originalArDesc.slice(0, originalArDesc.length - 2);

const addArLine = async (path: string) => {
if (path.includes('+')) {
// ar, in its infinite wisdom, still lives in last century and can't handle such modern delicacies
// as a plus symbol in a pathname
const oldStylePath = path.replace(/\+/g, 'x');
await Deno.rename(path, oldStylePath);
path = oldStylePath;
}
originalArDesc.push(`ADDLIB ${path}`);
};

const emscriptenSysootLibs = join(onnxruntimeRoot, 'cmake', 'external', 'emsdk', 'upstream', 'emscripten', 'cache', 'sysroot', 'lib', 'wasm32-emscripten');
await addArLine(join(emscriptenSysootLibs, 'libc.a'));
await addArLine(join(emscriptenSysootLibs, 'libc++-noexcept.a'));
await addArLine(join(emscriptenSysootLibs, 'libc++abi-noexcept.a'));
await addArLine(join(emscriptenSysootLibs, 'libcompiler_rt.a'));
await addArLine(join(emscriptenSysootLibs, 'libstubs.a'));

originalArDesc.push('SAVE', 'END');

await $`ar -M`.stdinText(originalArDesc.join('\n'));

const artifactOutDir = join(root, 'artifact');
await Deno.mkdir(artifactOutDir);

const artifactLibDir = join(artifactOutDir, 'onnxruntime', 'lib');
await Deno.mkdir(artifactLibDir, { recursive: true });

await Deno.copyFile(join(buildRoot, 'libonnxruntime_webassembly.a'), join(artifactLibDir, 'libonnxruntime.a'));

return;
}

const args = [];
if (options.cuda) {
args.push('-Donnxruntime_USE_CUDA=ON');
Expand Down Expand Up @@ -99,23 +153,25 @@ await new Command()
args.push('-Donnxruntime_USE_XNNPACK=ON');
}

if (platform === 'darwin') {
if (options.arch === 'aarch64') {
args.push('-DCMAKE_OSX_ARCHITECTURES=arm64');
if (!options.wasm) {
if (platform === 'darwin') {
if (options.arch === 'aarch64') {
args.push('-DCMAKE_OSX_ARCHITECTURES=arm64');
} else {
args.push('-DCMAKE_OSX_ARCHITECTURES=x86_64');
}
} else {
args.push('-DCMAKE_OSX_ARCHITECTURES=x86_64');
}
} else {
if (options.arch === 'aarch64' && arch !== 'arm64') {
args.push('-Donnxruntime_CROSS_COMPILING=ON');
switch (platform) {
case 'win32':
args.push('-A', 'ARM64');
args.push('-DCMAKE_CXX_FLAGS=-D_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS');
break;
case 'linux':
args.push(`-DCMAKE_TOOLCHAIN_FILE=${join(root, 'toolchains', 'aarch64-unknown-linux-gnu.cmake')}`);
break;
if (options.arch === 'aarch64' && arch !== 'arm64') {
args.push('-Donnxruntime_CROSS_COMPILING=ON');
switch (platform) {
case 'win32':
args.push('-A', 'ARM64');
args.push('-DCMAKE_CXX_FLAGS=-D_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS');
break;
case 'linux':
args.push(`-DCMAKE_TOOLCHAIN_FILE=${join(root, 'toolchains', 'aarch64-unknown-linux-gnu.cmake')}`);
break;
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/patches/wasm/0001-disable-streams.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
From 0000000000000000000000000000000000000000 Mon Dec 21 00:00:00 2000
Date: Mon, 21 Dec 2000 00:00:00 -0600
Subject: Disable stream for all WASM builds.

diff --git a/cmake/adjust_global_compile_flags.cmake b/cmake/adjust_global_compile_flags.cmake
index 690b6d4..aaa458f 100644
--- a/cmake/adjust_global_compile_flags.cmake
+++ b/cmake/adjust_global_compile_flags.cmake
@@ -102,7 +102,7 @@ if (onnxruntime_MINIMAL_BUILD)
endif()

# Enable stream for all the non-minimal build
-if (NOT onnxruntime_MINIMAL_BUILD)
+if (NOT onnxruntime_MINIMAL_BUILD AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
add_compile_definitions(ORT_ENABLE_STREAM)
endif()

26 changes: 26 additions & 0 deletions src/patches/wasm/0002-override-time-diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
From 0000000000000000000000000000000000000000 Mon Dec 21 00:00:00 2000
Date: Mon, 21 Dec 2000 00:00:00 -0600
Subject: Override `TimeDiffMicroSeconds` to always return 0.

diff --git a/include/onnxruntime/core/common/common.h b/include/onnxruntime/core/common/common.h
index 0822eba..7e684d3 100644
--- a/include/onnxruntime/core/common/common.h
+++ b/include/onnxruntime/core/common/common.h
@@ -249,13 +249,12 @@ void LogRuntimeError(uint32_t session_id, const common::Status& status, const ch
} \
} while (0)

-inline long long TimeDiffMicroSeconds(TimePoint start_time) {
- auto end_time = std::chrono::high_resolution_clock::now();
- return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
+inline long long TimeDiffMicroSeconds(TimePoint /*start_time*/) {
+ return 0;
}

-inline long long TimeDiffMicroSeconds(TimePoint start_time, TimePoint end_time) {
- return std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
+inline long long TimeDiffMicroSeconds(TimePoint /*start_time*/, TimePoint /*end_time*/) {
+ return 0;
}

struct null_type {};
Loading

0 comments on commit 8ec9fbc

Please sign in to comment.