-
Notifications
You must be signed in to change notification settings - Fork 698
Add example for building executor_runner in Wasm #12303
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6c7666
README and CMake changes
Conarnar b322cb8
Added build test script for wasm example
Conarnar 43561d7
Added CI tests
Conarnar d19c6b4
Emscripten automatically installs node.js
Conarnar 4b0e1af
Use Emscripten provided version of Node.js
Conarnar a21942d
Applied suggestions
Conarnar 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 hidden or 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,13 @@ | ||
|
|
||
| set -ex | ||
|
|
||
| install_emscripten() { | ||
| git clone https://github.com/emscripten-core/emsdk.git | ||
| pushd emsdk || return | ||
| ./emsdk install 4.0.10 | ||
| ./emsdk activate 4.0.10 | ||
| source ./emsdk_env.sh | ||
| popd || return | ||
| } | ||
|
|
||
| install_emscripten |
This file contains hidden or 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 hidden or 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 hidden or 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,64 @@ | ||
| # ExecuTorch Wasm Build | ||
|
|
||
| This guide describes how to build ExecuTorch for WebAssembly (Wasm). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [Emscripten](https://emscripten.org/docs/getting_started/Tutorial.html) | ||
|
|
||
Conarnar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Generate Models | ||
|
|
||
| JavaScript does not have direct access to the host file system. To load a model, it needs to be preloaded or embedded into the virtual file system. In this example, models in the `./models/` directory are embedded by default. We will then build `executorch_runner` in Wasm. | ||
|
|
||
| 1. Following the setup guide in [Setting up ExecuTorch](https://pytorch.org/executorch/main/getting-started-setup) | ||
| you should be able to get the basic development environment for ExecuTorch working. | ||
|
|
||
| 2. Using the script `portable/scripts/export.py` generate a model binary file by selecting a | ||
| model name from the list of available models in the `examples/models` dir. | ||
|
|
||
| ```bash | ||
| cd executorch # To the top level dir | ||
|
|
||
| mkdir models | ||
|
|
||
| # To get a list of example models | ||
| python3 -m examples.portable.script.export -h | ||
|
|
||
| # To generate a specific pte model into the models/ directory | ||
| python3 -m examples.portable.scripts.export --model_name="mv2" --output_dir="models/" # for MobileNetv2 | ||
|
|
||
| # This should generate ./models/mv2.pte file, if successful. | ||
| ``` | ||
|
|
||
| Use -h (or --help) to see all the supported models. For the browser example, make sure you have a model with the file name `model.pte` in the `./models/` directory. | ||
|
|
||
| 3. Once we have the model binaries (.pte) in `./models/`, we can build `executor_runner` in Wasm with Emscripten. When calling `emcmake cmake`, you can pass the `-DWASM_MODEL_DIR=<path>` option to specify the directory containing the model files instead of `./models/`. | ||
|
|
||
| ```bash | ||
| ./install_executorch.sh --clean | ||
| (mkdir cmake-out-wasm \ | ||
| && cd cmake-out-wasm \ | ||
| && emcmake cmake -DEXECUTORCH_PAL_DEFAULT=posix ..) \ | ||
| && cmake --build cmake-out-wasm -j32 --target executor_runner | ||
| ``` | ||
|
|
||
| If you need to rebuild `executor_runner` after modifying the contents of `./models/`, you can run the following command | ||
|
|
||
| ```bash | ||
| cmake --build cmake-out-wasm -j32 --target executor_runner --clean-first | ||
| ``` | ||
|
|
||
| 4. Run the model with Node.js. Emscripten should come preinstalled with a compatible version of Node.js. If you have an incompatible version of Node.js installed, you can use the Emscripten-provided version by running `$EMSDK_NODE` instead of `node`. | ||
|
|
||
| ```bash | ||
| # Run the tool on the generated model. | ||
| node cmake-out-wasm/executor_runner.js --model_path mv2.pte | ||
| ``` | ||
|
|
||
| 5. You can also run the model in the browser. Note that you cannot pass command line arguments to the browser version of the tool. By default, the program will load the model `model.pte` and run it. Several browsers do not support `file://` XHR requests to load the Wasm file. To get around this, you can use a local web server. For example, with Python: | ||
|
|
||
| ```bash | ||
| python3 -m http.server --directory cmake-out-wasm | ||
| ``` | ||
|
|
||
| The page will be available at http://localhost:8000/executor_runner.html. | ||
Conarnar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,37 @@ | ||
|
|
||
| set -e | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/../../.ci/scripts/utils.sh" | ||
|
|
||
| test_build_wasm() { | ||
| local model_name=$1 | ||
| local model_export_name="${model_name}.pte" | ||
| local model_dir_name="./models_test/" | ||
| echo "Exporting ${model_name}" | ||
| mkdir -p "${model_dir_name}" | ||
| ${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="${model_name}" --output_dir="$model_dir_name" | ||
|
|
||
| local example_dir=examples/wasm | ||
| local build_dir=cmake-out/${example_dir} | ||
| rm -rf ${build_dir} | ||
| retry emcmake cmake -DWASM_MODEL_DIR="$(realpath "${model_dir_name}")" -B${build_dir} . | ||
|
|
||
| echo "Building ${example_dir}" | ||
| cmake --build ${build_dir} -j9 --target executor_runner | ||
|
|
||
| echo "Removing ${model_dir_name}" | ||
| rm -rf "${model_dir_name}" | ||
|
|
||
| echo 'Running wasm build test' | ||
| $EMSDK_NODE ${build_dir}/executor_runner.js --model_path="${model_export_name}" | ||
| } | ||
|
|
||
| if [[ -z $PYTHON_EXECUTABLE ]]; | ||
| then | ||
| PYTHON_EXECUTABLE=python3 | ||
| fi | ||
|
|
||
| cmake_install_executorch_lib | ||
|
|
||
| test_build_wasm add_mul | ||
| test_build_wasm mv2 |
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.
Uh oh!
There was an error while loading. Please reload this page.