Commit fd6dbd5e by Joshua Z. Zhang Committed by Tianqi Chen

fix js test load module example (#3556)

parent 6d702ea8
...@@ -30,7 +30,9 @@ def prepare_test_libs(base_path): ...@@ -30,7 +30,9 @@ def prepare_test_libs(base_path):
fadd1 = tvm.build(s, [A, B], target, name="add_one") fadd1 = tvm.build(s, [A, B], target, name="add_one")
obj_path = os.path.join(base_path, "test_add_one.bc") obj_path = os.path.join(base_path, "test_add_one.bc")
fadd1.save(obj_path) fadd1.save(obj_path)
emscripten.create_js(os.path.join(base_path, "test_module.js"), obj_path) emscripten.create_js(os.path.join(base_path, "test_module.js"), obj_path,
options=["-s", "WASM=0", "-s", "USE_GLFW=3", "-s",
"USE_WEBGL2=1", "-lglfw"])
if __name__ == "__main__": if __name__ == "__main__":
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
......
...@@ -37,15 +37,18 @@ function randomArray(length, max) { ...@@ -37,15 +37,18 @@ function randomArray(length, max) {
function testAddOne() { function testAddOne() {
// grab pre-loaded function // grab pre-loaded function
var faddOne = sysLib.getFunction("add_one"); var faddOne = sysLib.getFunction("add_one");
var assert = require('assert');
tvm.assert(tvm.isPackedFunc(faddOne)); tvm.assert(tvm.isPackedFunc(faddOne));
var n = 124; var n = 124;
var A = tvm.empty(n).copyFrom(randomArray(n, 1)); var A = tvm.empty(n).copyFrom(randomArray(n, 1));
var B = tvm.empty(n); var B = tvm.empty(n);
// call the function. // call the function.
faddOne(A, B); faddOne(A, B);
AA = A.asArray(); // retrieve values in js array
BB = B.asArray(); // retrieve values in js array
// verify // verify
for (var i = 0; i < B.length; ++i) { for (var i = 0; i < BB.length; ++i) {
tvm.assert(B[i] == A[i] + 1); assert(Math.abs(BB[i] - (AA[i] + 1)) < 1e-5);
} }
faddOne.release(); faddOne.release();
} }
......
...@@ -120,13 +120,13 @@ how to run the compiled library. ...@@ -120,13 +120,13 @@ how to run the compiled library.
```js ```js
// Load Emscripten Module, need to change path to root/build // Load Emscripten Module, need to change path to root/build
const path = require("path"); const path = require("path");
process.chdir(path.join(__dirname, "../../buld")); process.chdir(path.join(__dirname, "../../build"));
var Module = require("../../build/test_module.js"); var Module = require("../../build/test_module.js");
// Bootstrap TVMruntime with emscripten module. // Bootstrap TVMruntime with emscripten module.
const tvm_runtime = require("../../web/tvm_runtime.js"); const tvm_runtime = require("../../web/tvm_runtime.js");
const tvm = tvm_runtime.create(Module); const tvm = tvm_runtime.create(Module);
// Load system library, the compiled functions is registered in sysLib. // Load system library, the compiled function is registered in sysLib.
var sysLib = tvm.systemLib(); var sysLib = tvm.systemLib();
function randomArray(length, max) { function randomArray(length, max) {
...@@ -138,22 +138,25 @@ function randomArray(length, max) { ...@@ -138,22 +138,25 @@ function randomArray(length, max) {
function testAddOne() { function testAddOne() {
// grab pre-loaded function // grab pre-loaded function
var faddOne = sysLib.getFunction("add_one"); var faddOne = sysLib.getFunction("add_one");
var assert = require('assert');
tvm.assert(tvm.isPackedFunc(faddOne)); tvm.assert(tvm.isPackedFunc(faddOne));
var n = 124; var n = 124;
var A = tvm.empty(n).copyFrom(randomArray(n, 1)); var A = tvm.empty(n).copyFrom(randomArray(n, 1));
var B = tvm.empty(n); var B = tvm.empty(n);
// call the function. // call the function.
faddOne(A, B); faddOne(A, B);
AA = A.asArray(); // retrieve values in js array
BB = B.asArray(); // retrieve values in js array
// verify // verify
for (var i = 0; i < B.length; ++i) { for (var i = 0; i < BB.length; ++i) {
tvm.assert(B[i] == A[i] + 1); assert(Math.abs(BB[i] - (AA[i] + 1)) < 1e-5);
} }
faddOne.release(); faddOne.release();
} }
testAddOne(); testAddOne();
sysLib.release(); sysLib.release();
console.log("Finish verifying test_module_load");
``` ```
Current example supports static linking, which is the preferred way to get more efficiency Current example supports static linking, which is the preferred way to get more efficiency
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment