Skip to content

Commit 5378272

Browse files
committed
Module test working
1 parent 7d15296 commit 5378272

File tree

5 files changed

+482
-0
lines changed

5 files changed

+482
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ if(EXECUTORCH_BUILD_PYBIND)
673673
)
674674
endif()
675675

676+
if(EXECUTORCH_BUILD_WASM)
677+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/wasm)
678+
endif()
679+
676680
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
677681
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/training)
678682
endif()

extension/wasm/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
cmake_minimum_required(VERSION 3.24) # 3.24 is required for WHOLE_ARCHIVE
3+
4+
project(executorch_wasm)
5+
6+
if(NOT CMAKE_CXX_STANDARD)
7+
set(CMAKE_CXX_STANDARD 17)
8+
endif()
9+
10+
if(NOT EMSCRIPTEN)
11+
message(FATAL_ERROR "Emscripten is required to build this target")
12+
endif()
13+
14+
# Source root directory for executorch.
15+
if(NOT EXECUTORCH_ROOT)
16+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
17+
endif()
18+
19+
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
20+
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
21+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
22+
23+
set(link_libraries)
24+
list(
25+
APPEND
26+
link_libraries
27+
embind
28+
executorch_core
29+
extension_data_loader
30+
portable_ops_lib
31+
extension_module_static
32+
extension_tensor
33+
extension_runner_util
34+
)
35+
36+
add_executable(executorch_wasm wasm_bindings.cpp)
37+
38+
target_compile_options(executorch_wasm PUBLIC ${_common_compile_options})
39+
target_include_directories(executorch_wasm PUBLIC ${_common_include_directories})
40+
target_link_libraries(executorch_wasm PUBLIC ${link_libraries})
41+
target_link_options(executorch_wasm PUBLIC -sALLOW_MEMORY_GROWTH=1 --embed-file=${CMAKE_CURRENT_SOURCE_DIR}/[email protected])
42+
43+
add_custom_target(executorch_wasm_test ALL
44+
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/test_module.js ${CMAKE_CURRENT_BINARY_DIR}/test_module.js
45+
DEPENDS executorch_wasm
46+
COMMENT "Copying test_module.js to build output directory"
47+
)

extension/wasm/build_wasm.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
cd "$(dirname "${BASH_SOURCE[0]}")/../../"
3+
mkdir -p cmake-out-wasm
4+
cd cmake-out-wasm
5+
emcmake cmake -DEXECUTORCH_BUILD_WASM=ON \
6+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
7+
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
8+
-DEXECUTORCH_BUILD_DEVTOOLS=ON \
9+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
10+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
11+
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON ..
12+
make executorch_wasm_test -j32

extension/wasm/test_module.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
const et = require("./executorch_wasm");
3+
et.onRuntimeInitialized = () => {
4+
module = et.Module.load("model.pte");
5+
var methods = module.getMethods();
6+
console.log(methods);
7+
var method = methods[0];
8+
var methodMeta = module.getMethodMeta(method);
9+
var inputs = [];
10+
for (var i = 0; i < methodMeta.numInputs; i++) {
11+
var tensor = et.FloatTensor.ones(methodMeta.inputTensorMeta(i).sizes);
12+
console.log("input", i, tensor.getData(), tensor.getSizes());
13+
inputs.push(tensor);
14+
}
15+
var output = module.execute(method, inputs);
16+
17+
for (var i = 0; i < inputs.length; i++) {
18+
inputs[i].delete();
19+
}
20+
21+
for (var i = 0; i < output.length; i++) {
22+
console.log("output", i, output[i].getData(), output[i].getSizes());
23+
output[i].delete();
24+
}
25+
26+
module.delete();
27+
}

0 commit comments

Comments
 (0)