-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: initial WASM support #4562
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
132b06f
feat: initial WASM support
jackgerrits fc2416d
remove packagelock
jackgerrits f141aea
remove option and try use ninja
jackgerrits 09df3b4
decrease string size in test
olgavrou 8d2fd95
Merge branch 'master' into wasm_2023
olgavrou d67477e
clang
olgavrou c1d7040
merge from master
olgavrou e8948d4
turn search and networking off for wasm
olgavrou 57e63e8
add compile time flags to the preset instead
olgavrou 629b22f
trigger build
olgavrou 7657854
trigger build
olgavrou 4190681
restore string and handle text parsing for long strings
olgavrou 1fdabf7
restore code and use latest std
olgavrou 41f9a11
correct spanning tree target name
olgavrou da9884d
spanning tree bin target
olgavrou 886aae4
restore ninja
olgavrou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build the wasm blob standalone