test_rpc_exec.py 1.37 KB
Newer Older
1
import tvm
2 3
from tvm import rpc
from tvm.contrib import util, graph_runtime
4 5 6
import nnvm.symbol as sym
import nnvm.compiler
import numpy as np
Tianqi Chen committed
7
import time
8 9 10

def test_rpc_executor():
    host = "localhost"
Tianqi Chen committed
11 12 13
    port = 9021
    server = rpc.Server(host, port, use_popen=True)
    time.sleep(1)
14 15 16 17 18 19 20 21 22
    x = sym.Variable("x")
    y = sym.Variable("y")
    z = sym.exp(y + x)
    shape = (10, 128)
    dtype = tvm.float32
    shape_dict = {"x": shape, "y": shape}
    tmp = util.tempdir()
    lib_name  = tmp.relpath("net.o")

23
    graph, lib, _ = nnvm.compiler.build(z, "llvm", shape_dict)
24 25
    # save module
    lib.save(lib_name)
Tianqi Chen committed
26
    remote = rpc.connect(host, port)
27 28 29 30 31 32
    remote.upload(lib_name)
    ctx = remote.cpu(0)
    # load remote
    rlib = remote.load_module("net.o")

    # Create remotemodule
33
    m = graph_runtime.create(graph, rlib, remote.cpu(0))
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    # get member functions
    set_input, run, get_output = m["set_input"], m["run"], m["get_output"]
    na = tvm.nd.array(np.ones(shape).astype(dtype), ctx)
    nb = tvm.nd.array(np.ones(shape).astype(dtype), ctx)
    # set inputs
    set_input("x", na)
    set_input("y", nb)
    # execute
    run()
    # get outputs
    out = tvm.nd.empty(shape, dtype, ctx)
    get_output(0, out)
    np.testing.assert_allclose(
        out.asnumpy(), np.exp(na.asnumpy() + nb.asnumpy()))
    server.terminate()

if __name__ == "__main__":
    test_rpc_executor()