Skip to content

Commit 160f15f

Browse files
committed
Applied suggestions
1 parent f3e3d54 commit 160f15f

File tree

6 files changed

+319
-130
lines changed

6 files changed

+319
-130
lines changed

extension/wasm/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
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+
# ~~~
111

212
cmake_minimum_required(VERSION 3.24)
313

@@ -36,7 +46,9 @@ list(
3646
add_library(executorch_wasm OBJECT wasm_bindings.cpp)
3747

3848
target_compile_options(executorch_wasm PUBLIC ${_common_compile_options})
39-
target_include_directories(executorch_wasm PUBLIC ${_common_include_directories})
49+
target_include_directories(
50+
executorch_wasm PUBLIC ${_common_include_directories}
51+
)
4052
target_link_libraries(executorch_wasm PUBLIC ${link_libraries})
4153

4254
if(BUILD_TESTING)

extension/wasm/build_wasm.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

extension/wasm/test/CMakeLists.txt

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
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+
# ~~~
111

212
set(MODELS_DIR ${CMAKE_CURRENT_BINARY_DIR}/models/)
313

414
add_custom_command(
5-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/models/add_mul.pte ${CMAKE_CURRENT_BINARY_DIR}/models/add.pte
15+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/models/add_mul.pte
16+
${CMAKE_CURRENT_BINARY_DIR}/models/add.pte
617
COMMAND ${CMAKE_COMMAND} -E make_directory "${MODELS_DIR}"
718
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../..
8-
COMMAND python3 -m examples.portable.scripts.export --model_name="add_mul" --output_dir="${MODELS_DIR}"
9-
COMMAND python3 -m examples.portable.scripts.export --model_name="add" --output_dir="${MODELS_DIR}"
19+
COMMAND python3 -m examples.portable.scripts.export --model_name="add_mul"
20+
--output_dir="${MODELS_DIR}"
21+
COMMAND python3 -m examples.portable.scripts.export --model_name="add"
22+
--output_dir="${MODELS_DIR}"
1023
)
1124

12-
add_custom_target(executorch_wasm_test_models DEPENDS ${MODELS_DIR}/add_mul.pte ${MODELS_DIR}/add.pte)
25+
add_custom_target(
26+
executorch_wasm_test_models DEPENDS ${MODELS_DIR}/add_mul.pte
27+
${MODELS_DIR}/add.pte
28+
)
1329

1430
add_executable(executorch_wasm_test_lib)
1531
target_link_libraries(executorch_wasm_test_lib PUBLIC executorch_wasm)
16-
target_link_options(executorch_wasm_test_lib PUBLIC --embed-file "${MODELS_DIR}@/")
32+
target_link_options(
33+
executorch_wasm_test_lib PUBLIC --embed-file "${MODELS_DIR}@/"
34+
)
1735
add_dependencies(executorch_wasm_test_lib executorch_wasm_test_models)
1836

1937
add_custom_command(
2038
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/executorch_wasm.test.js
21-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/executorch_wasm.test.js ${CMAKE_CURRENT_BINARY_DIR}/executorch_wasm.test.js
39+
COMMAND
40+
${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/executorch_wasm.test.js
41+
${CMAKE_CURRENT_BINARY_DIR}/executorch_wasm.test.js
2242
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/executorch_wasm.test.js
2343
COMMENT "Copying executorch_wasm.test.js to build output directory"
2444
)
2545

2646
add_custom_command(
2747
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/package.json
28-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/package.json ${CMAKE_CURRENT_BINARY_DIR}/package.json
48+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/package.json
49+
${CMAKE_CURRENT_BINARY_DIR}/package.json
2950
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/package.json
3051
COMMENT "Copying package.json to build output directory"
3152
)
3253

33-
add_custom_target(executorch_wasm_tests DEPENDS executorch_wasm_test_lib ${CMAKE_CURRENT_BINARY_DIR}/executorch_wasm.test.js ${CMAKE_CURRENT_BINARY_DIR}/package.json)
54+
add_custom_target(
55+
executorch_wasm_tests
56+
DEPENDS executorch_wasm_test_lib
57+
${CMAKE_CURRENT_BINARY_DIR}/executorch_wasm.test.js
58+
${CMAKE_CURRENT_BINARY_DIR}/package.json
59+
)

extension/wasm/test/executorch_wasm.test.js

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/*
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+
*/
18

29
let et;
310
beforeAll((done) => {
@@ -39,6 +46,23 @@ describe("Tensor", () => {
3946
expect(tensor.getSizes()).toEqual([2, 2]);
4047
tensor.delete();
4148
});
49+
50+
test("scalar type", () => {
51+
const tensor = et.FloatTensor.ones([2, 2]);
52+
// ScalarType can only be checked by strict equality.
53+
expect(tensor.scalarType).toBe(et.ScalarType.Float);
54+
tensor.delete();
55+
});
56+
57+
test("long tensor", () => {
58+
// Number cannot be converted to Long, so we use BigInt instead.
59+
const tensor = et.LongTensor.fromArray([1n, 2n, 3n, 4n], [2, 2]);
60+
expect(tensor.getData()).toEqual([1n, 2n, 3n, 4n]);
61+
expect(tensor.getSizes()).toEqual([2, 2]);
62+
// ScalarType can only be checked by strict equality.
63+
expect(tensor.scalarType).toBe(et.ScalarType.Long);
64+
tensor.delete();
65+
});
4266
});
4367

4468
describe("Module", () => {
@@ -66,15 +90,31 @@ describe("Module", () => {
6690
const module = et.Module.load("add_mul.pte");
6791
const methodMeta = module.getMethodMeta("forward");
6892
expect(methodMeta.name).toEqual("forward");
69-
methodMeta.delete();
7093
module.delete();
7194
});
7295

73-
test("numInputs is 3", () => {
96+
test("inputs are tensors", () => {
7497
const module = et.Module.load("add_mul.pte");
7598
const methodMeta = module.getMethodMeta("forward");
76-
expect(methodMeta.numInputs).toEqual(3);
77-
methodMeta.delete();
99+
expect(methodMeta.inputTags.length).toEqual(3);
100+
// Tags can only be checked by strict equality.
101+
methodMeta.inputTags.forEach((tag) => expect(tag).toBe(et.Tag.Tensor));
102+
module.delete();
103+
});
104+
105+
test("outputs are tensors", () => {
106+
const module = et.Module.load("add_mul.pte");
107+
const methodMeta = module.getMethodMeta("forward");
108+
expect(methodMeta.outputTags.length).toEqual(1);
109+
// Tags can only be checked by strict equality.
110+
expect(methodMeta.outputTags[0]).toBe(et.Tag.Tensor);
111+
module.delete();
112+
});
113+
114+
test("num instructions is 2", () => {
115+
const module = et.Module.load("add_mul.pte");
116+
const methodMeta = module.getMethodMeta("forward");
117+
expect(methodMeta.numInstructions).toEqual(2);
78118
module.delete();
79119
});
80120

@@ -85,23 +125,58 @@ describe("Module", () => {
85125
});
86126

87127
describe("TensorInfo", () => {
88-
test("sizes is 2x2", () => {
128+
test("input sizes is 2x2", () => {
89129
const module = et.Module.load("add_mul.pte");
90130
const methodMeta = module.getMethodMeta("forward");
91-
for (var i = 0; i < methodMeta.numInputs; i++) {
92-
const tensorInfo = methodMeta.inputTensorMeta(i);
131+
expect(methodMeta.inputTensorMeta.length).toEqual(3);
132+
methodMeta.inputTensorMeta.forEach((tensorInfo) => {
93133
expect(tensorInfo.sizes).toEqual([2, 2]);
94-
tensorInfo.delete();
95-
}
96-
methodMeta.delete();
134+
});
135+
module.delete();
136+
});
137+
138+
test("output sizes is 2x2", () => {
139+
const module = et.Module.load("add_mul.pte");
140+
const methodMeta = module.getMethodMeta("forward");
141+
expect(methodMeta.outputTensorMeta.length).toEqual(1);
142+
expect(methodMeta.outputTensorMeta[0].sizes).toEqual([2, 2]);
143+
module.delete();
144+
});
145+
146+
test("dim order is contiguous", () => {
147+
const module = et.Module.load("add_mul.pte");
148+
const methodMeta = module.getMethodMeta("forward");
149+
methodMeta.inputTensorMeta.forEach((tensorInfo) => {
150+
expect(tensorInfo.dimOrder).toEqual([0, 1]);
151+
});
152+
module.delete();
153+
});
154+
155+
test("scalar type is float", () => {
156+
const module = et.Module.load("add_mul.pte");
157+
const methodMeta = module.getMethodMeta("forward");
158+
methodMeta.inputTensorMeta.forEach((tensorInfo) => {
159+
// ScalarType can only be checked by strict equality.
160+
expect(tensorInfo.scalarType).toBe(et.ScalarType.Float);
161+
});
162+
module.delete();
163+
});
164+
165+
test("memory planned", () => {
166+
const module = et.Module.load("add_mul.pte");
167+
const methodMeta = module.getMethodMeta("forward");
168+
methodMeta.inputTensorMeta.forEach((tensorInfo) => {
169+
expect(tensorInfo.isMemoryPlanned).toBe(true);
170+
});
97171
module.delete();
98172
});
99173

100-
test("out of range", () => {
174+
test("nbytes is 16", () => {
101175
const module = et.Module.load("add_mul.pte");
102176
const methodMeta = module.getMethodMeta("forward");
103-
expect(() => methodMeta.inputTensorMeta(3)).toThrow();
104-
methodMeta.delete();
177+
methodMeta.inputTensorMeta.forEach((tensorInfo) => {
178+
expect(tensorInfo.nbytes).toEqual(16);
179+
});
105180
module.delete();
106181
});
107182
});
@@ -170,7 +245,7 @@ describe("Module", () => {
170245

171246
test("wrong input type", () => {
172247
const module = et.Module.load("add.pte");
173-
const inputs = [et.FloatTensor.ones([1]), et.IntTensor.ones([1])];
248+
const inputs = [et.FloatTensor.ones([1]), et.LongTensor.ones([1])];
174249
expect(() => module.execute("forward", inputs)).toThrow();
175250

176251
inputs.forEach((input) => input.delete());

0 commit comments

Comments
 (0)