Skip to content

Commit a2c3fe1

Browse files
Conarnarfacebook-github-bot
authored andcommitted
Executorch Wasm Internal Build
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. Differential Revision: D81276482
1 parent 2261848 commit a2c3fe1

File tree

7 files changed

+2448
-4
lines changed

7 files changed

+2448
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.genrule(
12+
name = "add_mul.pte",
13+
outs = {
14+
"add_mul.pte": ["add_mul.pte"],
15+
},
16+
cmd = " ".join(export_genrule_cmd).format(model_name="add_mul"),
17+
default_outs = ["."],
18+
)
19+
20+
runtime.genrule(
21+
name = "add.pte",
22+
outs = {
23+
"add.pte": ["add.pte"],
24+
},
25+
cmd = " ".join(export_genrule_cmd).format(model_name="add"),
26+
default_outs = ["."],
27+
)
28+
29+
runtime.python_binary(
30+
name = "test_model",
31+
srcs = [
32+
"test_model.py",
33+
],
34+
main_module = "executorch.extension.wasm.test.test_model",
35+
deps = [
36+
"//executorch/extension/export_util:export_util",
37+
],
38+
)
39+
40+
test_pte_genrule_cmd = [
41+
"$(exe :test_model)",
42+
"$OUT/test.pte",
43+
]
44+
45+
runtime.genrule(
46+
name = "test.pte",
47+
outs = {
48+
"test.pte": ["test.pte"],
49+
},
50+
cmd = " ".join(test_pte_genrule_cmd),
51+
default_outs = ["."],
52+
)
53+
54+
models_genrule_cmd = [
55+
"mkdir -p $OUT &&",
56+
"cp $(location :add.pte)/add.pte $OUT/add.pte &&",
57+
"cp $(location :add_mul.pte)/add_mul.pte $OUT/add_mul.pte &&",
58+
"cp $(location :test.pte)/test.pte $OUT/test.pte",
59+
]
60+
61+
runtime.genrule(
62+
name = "models",
63+
outs = {
64+
"add.pte": ["add.pte"],
65+
"add_mul.pte": ["add_mul.pte"],
66+
"test.pte": ["test.pte"],
67+
},
68+
cmd = " ".join(models_genrule_cmd),
69+
)

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
}

extension/wasm/test/test_models/dummy

Whitespace-only changes.

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
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)