Skip to content

Commit ef31409

Browse files
Conarnarfacebook-github-bot
authored andcommitted
Executorch Wasm Internal Build (#14015)
Summary: Added internal build scripts for Executorch Wasm and unit tests. Due to some intricacies the following things were done: - Emscripten build options don't work properly on `fbcode`, so only `xplat` targets were made. - Export scripts are only supported on `fbcode`, but do not work properly with the Emscripten build mode. Because of this, the test models for unit testing need to be built separately and copied into `xplat`. - Jest is configured to not search for unit tests within `buck-out`, so they must also be copied into `xplat`. - These are handled by `xplat_build.sh`, which can be run with `yarn build`. These files can also be deleted with `yarn clean`. - Unit tests won't build without `test_models` directory so dummy file is necessary to pass build rule CI. Reviewed By: lucylq Differential Revision: D81276482
1 parent 41c299f commit ef31409

File tree

7 files changed

+2424
-4
lines changed

7 files changed

+2424
-4
lines changed

extension/wasm/test/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Internal build generated files
2+
__tests__/
3+
test_models/*.pte

extension/wasm/test/TARGETS

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
3+
oncall("executorch")
4+
5+
export_genrule_cmd = [
6+
"$(exe //executorch/examples/portable/scripts:export)",
7+
"--model_name={model_name}",
8+
"--output_dir=$OUT",
9+
]
10+
11+
runtime.python_binary(
12+
name = "test_model",
13+
srcs = [
14+
"test_model.py",
15+
],
16+
main_module = "executorch.extension.wasm.test.test_model",
17+
deps = [
18+
"//executorch/extension/export_util:export_util",
19+
],
20+
)
21+
22+
test_pte_genrule_cmd = [
23+
"$(exe :test_model)",
24+
"$OUT/test.pte",
25+
]
26+
27+
models_genrule_cmd = [
28+
"mkdir -p $OUT &&",
29+
" ".join(export_genrule_cmd).format(model_name="add_mul"),
30+
"&&",
31+
" ".join(export_genrule_cmd).format(model_name="add"),
32+
"&&",
33+
" ".join(test_pte_genrule_cmd),
34+
]
35+
36+
runtime.genrule(
37+
name = "models",
38+
outs = {
39+
"add.pte": ["add.pte"],
40+
"add_mul.pte": ["add_mul.pte"],
41+
"test.pte": ["test.pte"],
42+
},
43+
cmd = " ".join(models_genrule_cmd),
44+
)

extension/wasm/test/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
2+
"name": "wasm-test",
3+
"private": true,
24
"scripts": {
3-
"test": "jest"
5+
"build": "bash xplat_build.sh",
6+
"test": "jest --verbose",
7+
"clean": "rm -rf __tests__ node_modules test_models/*.pte"
8+
},
9+
"devDependencies": {
10+
"jest": "^30.1.3"
411
}
512
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unit tests will not build without a file in this directory. Normally, running the build script should add the model files to this directory, but the Build Rule CIs will attempt and fail to build the unit tests directly without this file present.

extension/wasm/test/xplat_build.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
# This script is used to build unit tests internally. It is not intended to be used in OSS.
9+
10+
set -xueo pipefail
11+
12+
THIS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13+
OUTPUT_DIR="${THIS_DIR}/__tests__"
14+
TEST_MODEL_DIR="${THIS_DIR}/test_models/"
15+
16+
if [[ "${THIS_DIR}" != *"xplat"* ]]; then
17+
echo "This script must be run from xplat, not fbcode or oss"
18+
exit 1
19+
fi
20+
21+
ENABLE_ETDUMP=0
22+
for arg in "$@"; do
23+
if [[ "$arg" == "etdump" ]]; then
24+
ENABLE_ETDUMP=1
25+
else
26+
echo "Unknown argument: $arg"
27+
exit 1
28+
fi
29+
done
30+
31+
# Build the models
32+
# Using fbcode because the Python scripts are only supported in fbcode
33+
MODEL_TARGET_DIR=$(buck2 build fbcode//executorch/extension/wasm/test:models --show-full-output | awk '{print $2}')
34+
35+
mkdir -p "${TEST_MODEL_DIR}"
36+
cp ${MODEL_TARGET_DIR}/*.pte ${TEST_MODEL_DIR}
37+
38+
if (( ENABLE_ETDUMP != 0 )); then
39+
ETDUMP_OPTIONS="-DET_EVENT_TRACER_ENABLED=1"
40+
WASM_TARGET_NAME="wasm_etdump.test"
41+
else
42+
ETDUMP_OPTIONS=""
43+
WASM_TARGET_NAME="wasm.test"
44+
fi
45+
46+
# Emscripten build options don't work properly on fbcode; copy test artifacts to xplat and run the test in xplat.
47+
BUILD_TARGET_DIR=$(dirname $(buck2 build :${WASM_TARGET_NAME}.js --show-full-output -c "cxx.extra_cxxflags=-DET_LOG_ENABLED=0 $ETDUMP_OPTIONS" | awk '{print $2}'))
48+
49+
mkdir -p "${OUTPUT_DIR}"
50+
cp ${BUILD_TARGET_DIR}/${WASM_TARGET_NAME}.js ${OUTPUT_DIR}
51+
cp ${BUILD_TARGET_DIR}/${WASM_TARGET_NAME}.wasm ${OUTPUT_DIR}
52+
53+
yarn install

0 commit comments

Comments
 (0)