|
| 1 | +# ExecuTorch Wasm Build |
| 2 | + |
| 3 | +This guide describes how to build ExecuTorch for WebAssembly (Wasm). |
| 4 | + |
| 5 | +## Directory Structure |
| 6 | + |
| 7 | +``` |
| 8 | +examples/wasm |
| 9 | +└── README.md # This file |
| 10 | +``` |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- [emscripten](https://emscripten.org/docs/getting_started/Tutorial.html) |
| 15 | +- [Node.js](https://nodejs.org/en/) (Optional) |
| 16 | + |
| 17 | +## Generate Models |
| 18 | + |
| 19 | +JavaScript does not have access to the filesystem. To load a model, it needs to be preloaded or embedded into the virtual filesystem. In this example, models in the `./models/` directory are embedded by default. We will then build `executorch_runner` in Wasm. |
| 20 | + |
| 21 | +1. Following the setup guide in [Setting up ExecuTorch](https://pytorch.org/executorch/main/getting-started-setup) |
| 22 | +you should be able to get the basic development environment for ExecuTorch working. |
| 23 | + |
| 24 | +2. Using the script `portable/scripts/export.py` generate a model binary file by selecting a |
| 25 | +model name from the list of available models in the `examples/models` dir. |
| 26 | + |
| 27 | +```bash |
| 28 | +cd executorch # To the top level dir |
| 29 | + |
| 30 | +mkdir models |
| 31 | + |
| 32 | +# To get a list of example models |
| 33 | +python3 -m examples.portable.script.export -h |
| 34 | + |
| 35 | +# To generate a specific pte model into the models/ directory |
| 36 | +python3 -m examples.portable.scripts.export --model_name="mv2" --output_dir="models/" # for MobileNetv2 |
| 37 | + |
| 38 | +# This should generate ./models/mv2.pte file, if successful. |
| 39 | +``` |
| 40 | + |
| 41 | +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. |
| 42 | + |
| 43 | +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/`. |
| 44 | + |
| 45 | +```bash |
| 46 | +./install_executorch.sh --clean |
| 47 | +(mkdir cmake-out-wasm \ |
| 48 | + && cd cmake-out-wasm \ |
| 49 | + && emcmake cmake -DEXECUTORCH_PAL_DEFAULT=posix ..) \ |
| 50 | + && cmake --build cmake-out-wasm -j32 --target executor_runner |
| 51 | +``` |
| 52 | + |
| 53 | +If you need to rebuild `executor_runner` after modifying the contents of `./models/`, you can run the following command |
| 54 | + |
| 55 | +```bash |
| 56 | +cmake --build cmake-out-wasm -j32 --target executor_runner --clean-first |
| 57 | +``` |
| 58 | + |
| 59 | +4. Run the model with Node.js. |
| 60 | + |
| 61 | +```bash |
| 62 | +# Run the tool on the generated model. |
| 63 | +node cmake-out-wasm/executor_runner.js --model_path mv2.pte |
| 64 | +``` |
| 65 | + |
| 66 | +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: |
| 67 | + |
| 68 | +```bash |
| 69 | +python3 -m http.server --directory cmake-out-wasm |
| 70 | +``` |
| 71 | + |
| 72 | +The page will be available at http://localhost:8000/executor_runner.html. |
0 commit comments