Skip to content

Commit b6ada3d

Browse files
ConarnarConan Jeffrey Truong
andauthored
Wasm build demo (#45)
* Copied demo files * Added more instructions * Moved error checking to separate function * Clarified the readme * Simplified installation instructions * Removed unnecessary options --------- Co-authored-by: Conan Jeffrey Truong <[email protected]>
1 parent 1135dbb commit b6ada3d

File tree

8 files changed

+1348
-0
lines changed

8 files changed

+1348
-0
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
path = program-data-separation/cpp/executorch
88
url = https://github.com/pytorch/executorch.git
99
branch = release/0.7
10+
11+
[submodule "mv2/wasm/executorch"]
12+
path = mv2/wasm/executorch
13+
url = https://github.com/pytorch/executorch.git

mv2/wasm/CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Please this file formatted by running:
8+
# ~~~
9+
# cmake-format -i CMakeLists.txt
10+
# ~~~
11+
12+
add_subdirectory("executorch")
13+
14+
add_executable(executorch_wasm_demo_lib)
15+
target_link_libraries(executorch_wasm_demo_lib PUBLIC executorch_wasm)
16+
target_link_options(
17+
executorch_wasm_demo_lib PUBLIC -sALLOW_MEMORY_GROWTH
18+
)
19+
20+
add_custom_command(
21+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/demo.js
22+
COMMAND
23+
${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/demo.js
24+
${CMAKE_CURRENT_BINARY_DIR}/demo.js
25+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/demo.js
26+
COMMENT "Copying demo.js to build output directory"
27+
)
28+
29+
add_custom_command(
30+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/classes.js
31+
COMMAND
32+
${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/classes.js
33+
${CMAKE_CURRENT_BINARY_DIR}/classes.js
34+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/classes.js
35+
COMMENT "Copying classes.js to build output directory"
36+
)
37+
38+
add_custom_command(
39+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/demo.html
40+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/demo.html
41+
${CMAKE_CURRENT_BINARY_DIR}/demo.html
42+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/demo.html
43+
COMMENT "Copying demo.html to build output directory"
44+
)
45+
46+
add_custom_target(
47+
executorch_wasm_demo
48+
DEPENDS executorch_wasm_demo_lib
49+
${CMAKE_CURRENT_BINARY_DIR}/classes.js
50+
${CMAKE_CURRENT_BINARY_DIR}/demo.js
51+
${CMAKE_CURRENT_BINARY_DIR}/demo.html
52+
)

mv2/wasm/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# ExecuTorch JavaScript Bindings Demo
2+
3+
This demo showcases the capabilities of ExecuTorch's JavaScript bindings. It is able to load a model, run inference, and classify an image natively in the browser.
4+
5+
## Installing Emscripten
6+
7+
[Emscripten](https://emscripten.org/index.html) is necessary to compile ExecuTorch for Wasm. You can install Emscripten with these commands:
8+
9+
```bash
10+
# Clone the emsdk repository
11+
git clone https://github.com/emscripten-core/emsdk.git
12+
cd emsdk
13+
14+
# Download and install version 4.0.10 of the SDK
15+
./emsdk install 4.0.10
16+
./emsdk activate 4.0.10
17+
18+
# Add the Emscripten environment variables to your shell
19+
source ./emsdk_env.sh
20+
```
21+
22+
## Setting up ExecuTorch and Generating the Model File
23+
24+
Make sure you have the system requirements listed in the [Getting Started Guide](https://docs.pytorch.org/executorch/main/getting-started.html#system-requirements) before continuing.
25+
26+
1. Install ExecuTorch from PyPI.
27+
```bash
28+
pip3 install executorch
29+
```
30+
31+
2. Update the ExecuTorch submodule.
32+
```bash
33+
git submodule update --init --recursive executorch
34+
```
35+
36+
3. Using the script `examples/portable/scripts/export.py` generate the MobileNetV2 binary file for this demo.
37+
38+
```bash
39+
cd executorch # To the root of the executorch repo
40+
41+
# Export the model file for the demo
42+
python3 -m examples.portable.scripts.export --model_name="mv2"
43+
```
44+
45+
## Building and Running
46+
47+
Once you have Emscripten installed, ExecuTorch set up, and the model file generated, you can build and run the demo.
48+
49+
```bash
50+
cd mv2/wasm # The directory containing this README
51+
52+
# Build the demo
53+
bash build.sh
54+
55+
# Run the demo
56+
python3 -m http.server --directory build/
57+
```
58+
59+
The page will be available at http://localhost:8000/demo.html.
60+
61+
## Demo Features
62+
63+
- Load a model from a file
64+
- It currently only supports the MobileNetV2 model. Passing in a model with different input/output shapes will result in an error.
65+
- Run inference on an image
66+
- Supported formats: `.png`, `.gif`, `.jpeg`, `.jpg`

mv2/wasm/build.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
CMAKE_OUT=build
9+
10+
emcmake cmake . -DEXECUTORCH_BUILD_WASM=ON \
11+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
12+
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
13+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
14+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
15+
-DCMAKE_BUILD_TYPE=Release \
16+
-B"${CMAKE_OUT}"
17+
18+
if [ "$(uname)" == "Darwin" ]; then
19+
CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))
20+
else
21+
CMAKE_JOBS=$(( $(nproc) - 1 ))
22+
fi
23+
24+
cmake --build ${CMAKE_OUT} --target executorch_wasm_demo -j ${CMAKE_JOBS}

0 commit comments

Comments
 (0)