Skip to content

Commit 9e7a667

Browse files
authored
[DOCS] How to deploy TVM Modules (#499)
* [DOCS] How to deploy TVM Modules * More comments
1 parent 5d9647e commit 9e7a667

File tree

14 files changed

+183
-45
lines changed

14 files changed

+183
-45
lines changed

apps/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ If you are interested in writing optimized kernels with TVM, checkout [TOPI: TVM
88
- [graph_executor](graph_executor) Build nnvm graph executor with TVM.
99
- [ios_rpc](ios_rpc) iOS RPC server.
1010
- [android_rpc](android_rpc) Android RPC server.
11-
- [cpp_deploy](cpp_deploy) Example to deploy with C++ runtime.
12-
11+
- [howto_deploy](howto_depploy) Tutorial on how to deploy TVM with minimum code dependency.

apps/cpp_deploy/Makefile

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

apps/cpp_deploy/README.md

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

apps/cpp_deploy/run_example.sh

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

apps/howto_deploy/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Makefile Example to deploy TVM modules.
2+
TVM_ROOT=$(shell cd ../..; pwd)
3+
NNVM_PATH=nnvm
4+
DMLC_CORE=${TVM_ROOT}/dmlc-core
5+
6+
PKG_CFLAGS = -std=c++11 -O2 -fPIC\
7+
-I${TVM_ROOT}/include\
8+
-I${DMLC_CORE}/include\
9+
-I${TVM_ROOT}/dlpack/include\
10+
11+
PKG_LDFLAGS = -L${TVM_ROOT}/lib -ldl -lpthread
12+
13+
.PHONY: clean all
14+
15+
all: lib/cpp_deploy_pack lib/cpp_deploy_normal
16+
17+
# Build rule for all in one TVM package library
18+
lib/libtvm_runtime_pack.o: tvm_runtime_pack.cc
19+
@mkdir -p $(@D)
20+
$(CXX) -c $(PKG_CFLAGS) -o $@ $^
21+
22+
# The code library built by TVM
23+
lib/test_addone_sys.o: prepare_test_libs.py
24+
python prepare_test_libs.py
25+
26+
# Deploy using the all in one TVM package library
27+
lib/cpp_deploy_pack: cpp_deploy.cc lib/test_addone_sys.o lib/libtvm_runtime_pack.o
28+
@mkdir -p $(@D)
29+
$(CXX) $(PKG_CFLAGS) -o $@ $^ $(PKG_LDFLAGS)
30+
31+
# Deploy using pre-built libtvm_runtime.so
32+
lib/cpp_deploy_normal: cpp_deploy.cc lib/test_addone_sys.o
33+
@mkdir -p $(@D)
34+
$(CXX) $(PKG_CFLAGS) -o $@ $^ $(PKG_LDFLAGS) -ltvm_runtime

apps/howto_deploy/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
How to Deploy TVM Modules
2+
=========================
3+
This folder contains an example on how to deploy TVM modules.
4+
It also contains an example code to deploy with C++.
5+
6+
Type the following command to run the sample code under the current folder(need to build TVM first).
7+
```bash
8+
./run_example.sh
9+
```
10+
11+
Checkout [How to Deploy TVM Modules](http://docs.tvmlang.org/how_to/deploy.html) for more information.
File renamed without changes.

apps/cpp_deploy/prepare_test_libs.py renamed to apps/howto_deploy/prepare_test_libs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ def prepare_test_libs(base_path):
77
A = tvm.placeholder((n,), name='A')
88
B = tvm.compute(A.shape, lambda *i: A(*i) + 1.0, name='B')
99
s = tvm.create_schedule(B.op)
10+
# Compile library as dynamic library
1011
fadd_dylib = tvm.build(s, [A, B], "llvm", name="addone")
11-
fadd_syslib = tvm.build(s, [A, B], "llvm --system-lib", name="addonesys")
1212
dylib_path = os.path.join(base_path, "test_addone_dll.so")
13-
syslib_path = os.path.join(base_path, "test_addone_sys.o")
1413
fadd_dylib.export_library(dylib_path)
14+
15+
# Compile library in system library mode
16+
fadd_syslib = tvm.build(s, [A, B], "llvm --system-lib", name="addonesys")
17+
syslib_path = os.path.join(base_path, "test_addone_sys.o")
1518
fadd_syslib.save(syslib_path)
1619

1720
if __name__ == "__main__":

apps/howto_deploy/run_example.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
echo "Build the libraries.."
3+
mkdir -p lib
4+
make
5+
echo "Run the example"
6+
export LD_LIBRARY_PATH=../../lib:${LD_LIBRARY_PATH}
7+
export DYLD_LIBRARY_PATH=../../lib:${DYLD_LIBRARY_PATH}
8+
9+
echo "Run the deployment with all in one packed library..."
10+
lib/cpp_deploy_pack
11+
12+
echo "Run the deployment with all in normal library..."
13+
lib/cpp_deploy_normal
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*!
2+
* \brief This is an all in one TVM runtime file.
3+
*
4+
* You only have to use this file to compile libtvm_runtime to
5+
* include in your project.
6+
*
7+
* - Copy this file into your project which depends on tvm runtime.
8+
* - Compile with -std=c++11
9+
* - Add the following include path
10+
* - /path/to/tvm/include/
11+
* - /path/to/tvm/dmlc-core/include/
12+
* - /path/to/tvm/dlpack/include/
13+
* - Add -lpthread -ldl to the linked library.
14+
* - You are good to go.
15+
* - See the Makefile in the same folder for example.
16+
*
17+
* The include files here are presented with relative path
18+
* You need to remember to change it to point to the right file.
19+
*
20+
*/
21+
#include "../../src/runtime/c_runtime_api.cc"
22+
#include "../../src/runtime/cpu_device_api.cc"
23+
#include "../../src/runtime/workspace_pool.cc"
24+
#include "../../src/runtime/module_util.cc"
25+
#include "../../src/runtime/module.cc"
26+
#include "../../src/runtime/registry.cc"
27+
#include "../../src/runtime/file_util.cc"
28+
#include "../../src/runtime/thread_pool.cc"
29+
30+
// NOTE: all the files after this are optional modules
31+
// that you can include remove, depending on how much feature you use.
32+
33+
// Likely we only need to enable one of the following
34+
// If you use Module::Load, use dso_module
35+
// For system packed library, use system_lib_module
36+
#include "../../src/runtime/dso_module.cc"
37+
#include "../../src/runtime/system_lib_module.cc"
38+
39+
// Graph runtime
40+
#include "../../src/runtime/graph/graph_runtime.cc"
41+
42+
// Uncomment the following lines to enable RPC
43+
// #include "../../src/runtime/rpc/rpc_session.cc"
44+
// #include "../../src/runtime/rpc/rpc_event_impl.cc"
45+
// #include "../../src/runtime/rpc/rpc_server_env.cc"
46+
47+
// Uncomment the following lines to enable Metal
48+
// #include "../../src/runtime/metal/metal_device_api.mm"
49+
// #include "../../src/runtime/metal/metal_module.mm"
50+
51+
// Uncomment the following lines to enable OpenCL
52+
// #include "../../src/runtime/opencl/opencl_device_api.cc"
53+
// #include "../../src/runtime/opencl/opencl_module.cc"

0 commit comments

Comments
 (0)