Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
path = program-data-separation/cpp/executorch
url = https://github.com/pytorch/executorch.git
branch = release/0.7

[submodule "mv2/wasm/executorch"]
path = mv2/wasm/executorch
url = https://github.com/pytorch/executorch.git
52 changes: 52 additions & 0 deletions mv2/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# Please this file formatted by running:
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~

add_subdirectory("executorch")

add_executable(executorch_wasm_demo_lib)
target_link_libraries(executorch_wasm_demo_lib PUBLIC executorch_wasm)
target_link_options(
executorch_wasm_demo_lib PUBLIC -sALLOW_MEMORY_GROWTH
)

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/demo.js
COMMAND
${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/demo.js
${CMAKE_CURRENT_BINARY_DIR}/demo.js
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/demo.js
COMMENT "Copying demo.js to build output directory"
)

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/classes.js
COMMAND
${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/classes.js
${CMAKE_CURRENT_BINARY_DIR}/classes.js
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/classes.js
COMMENT "Copying classes.js to build output directory"
)

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/demo.html
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/demo.html
${CMAKE_CURRENT_BINARY_DIR}/demo.html
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/demo.html
COMMENT "Copying demo.html to build output directory"
)

add_custom_target(
executorch_wasm_demo
DEPENDS executorch_wasm_demo_lib
${CMAKE_CURRENT_BINARY_DIR}/classes.js
${CMAKE_CURRENT_BINARY_DIR}/demo.js
${CMAKE_CURRENT_BINARY_DIR}/demo.html
)
37 changes: 37 additions & 0 deletions mv2/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ExecuTorch JavaScript Bindings Demo

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.

## Prerequisites

- [Emscripten](https://emscripten.org/docs/getting_started/Tutorial.html)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a link to your readme?

'Refer to the wasm readme for setup guide.'

- Refer to the [Wasm example Readme](https://github.com/pytorch/executorch/blob/main/examples/wasm/README.md) for a quick setup guide.

## Building and Running

```
# Clone executorch submodule
git submodule update --init

# Set up Executorch
cd executorch
./install_executorch.sh
./install_executorch.sh --clean

cd ..

# Build the demo
bash build.sh

# Run the demo
python3 -m http.server --directory build/
```

The page will be available at http://localhost:8000/demo.html.

## Demo Features

- Load a model from a file
- It currently only supports the MobileNetv2 model. Passing in a model with different input/output shapes will result in an error.
- You can generate the model file by following the instructions in the [Portable Mode Readme](https://github.com/pytorch/executorch/blob/main/examples/portable/README.md).
- Run inference on an image
27 changes: 27 additions & 0 deletions mv2/wasm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

CMAKE_OUT=build

emcmake cmake . -DEXECUTORCH_BUILD_WASM=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
-DEXECUTORCH_BUILD_DEVTOOLS=ON \
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
-DEXECUTORCH_BUILD_WASM_DEMO=ON \
-DCMAKE_BUILD_TYPE=Release \
-B"${CMAKE_OUT}"

if [ "$(uname)" == "Darwin" ]; then
CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))
else
CMAKE_JOBS=$(( $(nproc) - 1 ))
fi

cmake --build ${CMAKE_OUT} --target executorch_wasm_demo -j ${CMAKE_JOBS}
Loading