Unverified Commit ff5dffa4 by Tianqi Chen Committed by GitHub

[APPS] add an external dll call example (#2156)

parent 2bd02e4e
...@@ -8,7 +8,9 @@ import tvm ...@@ -8,7 +8,9 @@ import tvm
def load_lib(): def load_lib():
"""Load library, the functions will be registered into TVM""" """Load library, the functions will be registered into TVM"""
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
lib = ctypes.CDLL(os.path.join(curr_path, "../../lib/libtvm_ext.so")) # load in as global so the global extern symbol is visible to other dll.
lib = ctypes.CDLL(
os.path.join(curr_path, "../../lib/libtvm_ext.so"), ctypes.RTLD_GLOBAL)
return lib return lib
_LIB = load_lib() _LIB = load_lib()
......
...@@ -66,6 +66,11 @@ TVM_REGISTER_GLOBAL("device_api.ext_dev") ...@@ -66,6 +66,11 @@ TVM_REGISTER_GLOBAL("device_api.ext_dev")
}); });
} // namespace tvm_ext } // namespace tvm_ext
// External function exposed to runtime.
extern "C" float TVMTestAddOne(float y) {
return y + 1;
}
// This callback approach allows extension allows tvm to extract // This callback approach allows extension allows tvm to extract
// This way can be helpful when we want to use a header only // This way can be helpful when we want to use a header only
// minimum version of TVM Runtime. // minimum version of TVM Runtime.
......
...@@ -49,7 +49,27 @@ def test_extract_ext(): ...@@ -49,7 +49,27 @@ def test_extract_ext():
assert fdict["mul"](3, 4) == 12 assert fdict["mul"](3, 4) == 12
def test_extern_call():
n = 10
A = tvm.placeholder((n,), name='A')
B = tvm.compute((n,), lambda *i: tvm.call_extern("float32", "TVMTestAddOne", A(*i)), name='B')
s = tvm.create_schedule(B.op)
def check_llvm():
if not tvm.module.enabled("llvm"):
return
f = tvm.build(s, [A, B], "llvm")
ctx = tvm.cpu(0)
# launch the kernel.
a = tvm.nd.array(np.random.uniform(size=n).astype(A.dtype), ctx)
b = tvm.nd.array(np.zeros(n, dtype=B.dtype), ctx)
f(a, b)
tvm.testing.assert_allclose(b.asnumpy(), a.asnumpy() + 1)
check_llvm()
if __name__ == "__main__": if __name__ == "__main__":
test_extern_call()
test_ext_dev() test_ext_dev()
test_ext_vec() test_ext_vec()
test_bind_add() test_bind_add()
......
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