test_rpc_exec.py 2.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17
import tvm
18 19
from tvm import rpc
from tvm.contrib import util, graph_runtime
20 21 22
import nnvm.symbol as sym
import nnvm.compiler
import numpy as np
Tianqi Chen committed
23
import time
24 25 26

def test_rpc_executor():
    host = "localhost"
Tianqi Chen committed
27 28 29
    port = 9021
    server = rpc.Server(host, port, use_popen=True)
    time.sleep(1)
30 31 32 33 34 35 36 37 38
    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")

39
    graph, lib, _ = nnvm.compiler.build(z, "llvm", shape_dict)
40 41
    # save module
    lib.save(lib_name)
Tianqi Chen committed
42
    remote = rpc.connect(host, port)
43 44 45 46 47 48
    remote.upload(lib_name)
    ctx = remote.cpu(0)
    # load remote
    rlib = remote.load_module("net.o")

    # Create remotemodule
49
    m = graph_runtime.create(graph, rlib, remote.cpu(0))
50 51 52 53 54 55 56 57 58 59 60 61
    # 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)
62
    tvm.testing.assert_allclose(
63 64 65 66 67
        out.asnumpy(), np.exp(na.asnumpy() + nb.asnumpy()))
    server.terminate()

if __name__ == "__main__":
    test_rpc_executor()