forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial WASM support (VowpalWabbit#4562)
Co-authored-by: olgavrou <[email protected]>
- Loading branch information
1 parent
eb15bf9
commit 71d3d41
Showing
15 changed files
with
657 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Wasm | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- 'releases/**' | ||
pull_request: | ||
branches: | ||
- '*' | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.sha }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
job: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
submodules: true | ||
- uses: lukka/get-cmake@latest | ||
- uses: mymindstorm/setup-emsdk@v11 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
- name: Configure | ||
run: emcmake cmake --preset wasm -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=$(pwd)/ext_libs/vcpkg/scripts/buildsystems/vcpkg.cmake | ||
- name: Build | ||
run: cmake --build build --target vw-wasm | ||
- uses: actions/upload-artifact@v3 | ||
with: | ||
path: wasm/out | ||
- name: Test | ||
working-directory: wasm | ||
run: | | ||
npm install | ||
npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule vcpkg
updated
2506 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
if(VW_INSTALL) | ||
message(FATAL_ERROR "Install not supported for WASM build" ) | ||
endif() | ||
|
||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/") | ||
|
||
add_executable(vw-wasm wasm_wrapper.cc) | ||
set_target_properties(vw-wasm PROPERTIES LINK_FLAGS "-fexceptions -s WASM=1 -s NO_DYNAMIC_EXECUTION=1 --bind -s ALLOW_MEMORY_GROWTH=1 -s EXPORTED_FUNCTIONS=\"['_malloc', '_free']\"") | ||
target_link_libraries(vw-wasm PUBLIC vw_explore vw_core "-fexceptions") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
const VWModule = require('./out/vw-wasm.js'); | ||
|
||
// You must wait for the WASM runtime to be ready before using the module. | ||
VWModule['onRuntimeInitialized'] = () => { | ||
try { | ||
// Create a model with default options | ||
let model = new VWModule.VWModel(""); | ||
|
||
let example_line = "0 | price:.23 sqft:.25 age:.05 2006"; | ||
// For multi_ex learners, the input to parse should have newlines in it. | ||
// One call to parse should only ever get input for either a single | ||
// example or a single multi_ex grouping. | ||
let parsedExample = model.parse(example_line); | ||
|
||
// Prediction returns a normal javascript value which corresponds to the | ||
// expected prediction type. Here is just a number. | ||
console.log(`prediction: ${model.predict(parsedExample)}`); | ||
|
||
model.learn(parsedExample); | ||
|
||
// Any examples which were given to either learn and/or predict must be | ||
// given to finishExample. | ||
model.finishExample(parsedExample); | ||
|
||
// Every object returned by parse must be deleted manually. | ||
// Additionally, shallow or deep example copies must be deleted. | ||
// let shallowCopyExample = parsedExample; | ||
// shallowCopyExample.delete(); | ||
// | ||
// let deepCopyExample = parsedExample.clone(model); | ||
// deepCopyExample.delete(); | ||
parsedExample.delete(); | ||
|
||
// VWModel must also always be manually deleted. | ||
model.delete(); | ||
} | ||
catch (e) { | ||
// Exceptions that are produced by the module must be passed through | ||
// this transformation function to get the error info. | ||
console.error(VWModule.getExceptionMessage(e)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"devDependencies": { | ||
"mocha": "^9.1.2" | ||
}, | ||
"scripts": { | ||
"test1": "mocha --delay", | ||
"test": "node --experimental-wasm-threads ./node_modules/mocha/bin/mocha --delay" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# VW - WASM | ||
|
||
This module is currently very experimental. It is not currently built standalone and still requires the glue JS to function. Ideally it will be standalone WASM module eventually. | ||
|
||
## Use docker container | ||
### Build | ||
```sh | ||
# Run in VW root directory | ||
docker run --rm -v $(pwd):/src -it emscripten/emsdk emcmake cmake -G "Unix Makefiles" --preset wasm -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=/src/ext_libs/vcpkg/scripts/buildsystems/vcpkg.cmake | ||
docker run --rm -v $(pwd):/src -it emscripten/emsdk emcmake cmake --build /src/build --target vw-wasm -j $(nproc) | ||
``` | ||
|
||
Artifacts are placed in `wasm/out` | ||
|
||
### Test | ||
Assumes required build artifacts are in `wasm/out` | ||
|
||
```sh | ||
# Run in VW root directory | ||
docker run --workdir="/src/wasm" --rm -v $(pwd):/src -t node:16-alpine npm install | ||
docker run --workdir="/src/wasm" --rm -v $(pwd):/src -t node:16-alpine npm test | ||
``` | ||
|
||
## Or, setup local environment to build | ||
|
||
### Install Emscripten and activate environment | ||
Instructions here: https://emscripten.org/docs/getting_started/downloads.html | ||
```sh | ||
git clone https://github.com/emscripten-core/emsdk.git | ||
cd emsdk | ||
./emsdk install latest | ||
./emsdk activate latest | ||
source ./emsdk_env.sh | ||
``` | ||
|
||
### Build | ||
Make sure Emscripten is activated. | ||
```sh | ||
emcmake cmake --preset wasm -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=$(pwd)/ext_libs/vcpkg/scripts/buildsystems/vcpkg.cmake | ||
cmake --build build --target vw-wasm | ||
``` | ||
|
||
### Test | ||
```sh | ||
npm test | ||
``` |
Oops, something went wrong.