[翻译]交叉编译和RPC — tvm 0.7.dev1文档
#
**
git clone —recursive https://github.com/apache/incubator-tvm tvm
cd tvm
make runtime -j2
~/.bashrc``~/.bashrc``vi ~/.bashrc``~/tvm
export PYTHONPATH=$PYTHONPATH:~/tvm/python
source ~/.bashrc
python -m tvm.exec.rpc_server —host 0.0.0.0 —port=9090
INFO:root:RPCServer: bind to 0.0.0.0:9090
import numpy as np
import tvm from tvm import te from tvm import rpc from tvm.contrib import util
n = tvm.runtime.convert(1024) A = te.placeholder((n,), name=’A’) B = te.compute((n,), lambda i: A[i] + 1.0, name=’B’) s = te.create_schedule(B.op)
local_demo = True
if local_demo: target = ‘llvm’ else: target = ‘llvm -target=armv7l-linux-gnueabihf’
func = tvm.build(s, [A, B], target=target, name=’add_one’)
save the lib at a local temp folder
temp = util.tempdir()
path = temp.relpath(‘lib.tar’)
func.export_library(path)
local_demo``target``build``'llvm -target=armv7l-linux-gnueabihf'``'llvm -target=aarch64-linux-gnu'
gcc -v``Target:
-target
llc -mtriple=
-mattr=help
if local_demo: remote = rpc.LocalSession() else:
# The following is my environment, change this to the IP address of your target device
host = '10.77.1.162'
port = 9090
remote = [rpc.connect](https://tvm.apache.org/docs/api/python/rpc.html#tvm.rpc.connect)(host, port)
remote.upload(path) func = remote.load_module(‘lib.tar’)
create arrays on the remote device
ctx = remote.cpu() a = tvm.nd.array(np.random.uniform(size=1024).astype(A.dtype), ctx) b = tvm.nd.array(np.zeros(1024, dtype=A.dtype), ctx)
the function will run on the remote device
func(a, b)
np.testing.assert_equal(b.asnumpy(), a.asnumpy() + 1)
time_evaluator
time_f = func.time_evaluator(func.entry_name, ctx, number=10)
cost = time_f(a, b).mean
print(‘%g secs/op’ % cost)
1.292e-07 secs/op
cp cmake/config.cmake .
sed -i “s/USE_OPENCL OFF/USE_OPENCL ON/“ config.cmake
make runtime -j4
def run_opencl():
# NOTE: This is the setting for my rk3399 board. You need to modify
# them according to your environment.
target_host = "llvm -target=aarch64-linux-gnu"
opencl_device_host = '10.77.1.145'
opencl_device_port = 9090
# create schedule for the above "add one" compute declaration
s = [te.create_schedule](https://tvm.apache.org/docs/api/python/te.html#tvm.te.create_schedule)(B.op)
xo, xi = s[B].split(B.op.axis[0], factor=32)
s[B].bind(xo, [te.thread_axis](https://tvm.apache.org/docs/api/python/te.html#tvm.te.thread_axis)("blockIdx.x"))
s[B].bind(xi, [te.thread_axis](https://tvm.apache.org/docs/api/python/te.html#tvm.te.thread_axis)("threadIdx.x"))
func = [tvm.build](https://tvm.apache.org/docs/api/python/driver.html#tvm.build)(s, [A, B], "opencl", target_host=target_host)
remote = [rpc.connect](https://tvm.apache.org/docs/api/python/rpc.html#tvm.rpc.connect)(opencl_device_host, opencl_device_port)
# export and upload
path = temp.relpath('lib_cl.tar')
func.export_library(path)
remote.upload(path)
func = remote.load_module('lib_cl.tar')
# run
ctx = remote.cl()
a = [tvm.nd.array](https://tvm.apache.org/docs/api/python/ndarray.html#tvm.nd.array)(np.random.uniform(size=1024).astype(A.dtype), ctx)
b = [tvm.nd.array](https://tvm.apache.org/docs/api/python/ndarray.html#tvm.nd.array)([np.zeros](https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html#numpy.zeros)(1024, dtype=A.dtype), ctx)
func(a, b)
[np.testing.assert_equal](https://docs.scipy.org/doc/numpy/reference/generated/numpy.testing.assert_equal.html#numpy.testing.assert_equal)(b.asnumpy(), a.asnumpy() + 1)
print("OpenCL test passed!")