tvm.rpc

Lightweight TVM RPC module.

RPC enables connect to a remote server, upload and launch functions. This is useful to for cross-compile and remote testing, The compiler stack runs on local server, while we use RPC server to run on remote runtime which don’t have a compiler available.

The test program compiles the program on local server, upload and run remote RPC server, get the result back to verify correctness.

Classes

LocalSession()

RPCSession interface backed by local environment.

RPCSession(sess)

RPC Client session module

Server(host[, port, port_end, is_proxy, …])

Start RPC server on a separate process.

TrackerSession(addr)

Tracker client session.

Functions

connect(url, port[, key, session_timeout])

Connect to RPC Server

connect_tracker(url, port)

Connect to a RPC tracker

class tvm.rpc.Server(host, port=9091, port_end=9199, is_proxy=False, use_popen=False, tracker_addr=None, key='', load_library=None, custom_addr=None, silent=False)

Start RPC server on a separate process.

This is a simple python implementation based on multi-processing. It is also possible to implement a similar C based server with TVM runtime which does not depend on the python.

Parameters
  • host (str) – The host url of the server.

  • port (int) – The port to be bind to

  • port_end (int, optional) – The end port to search

  • is_proxy (bool, optional) – Whether the address specified is a proxy. If this is true, the host and port actually corresponds to the address of the proxy server.

  • use_popen (bool, optional) – Whether to use Popen to start a fresh new process instead of fork. This is recommended to switch on if we want to do local RPC demonstration for GPU devices to avoid fork safety issues.

  • tracker_addr (Tuple (str, int) , optional) – The address of RPC Tracker in tuple(host, ip) format. If is not None, the server will register itself to the tracker.

  • key (str, optional) – The key used to identify the device type in tracker.

  • load_library (str, optional) – List of additional libraries to be loaded during execution.

  • custom_addr (str, optional) – Custom IP Address to Report to RPC Tracker

  • silent (bool, optional) – Whether run this server in silent mode.

Methods

terminate()

Terminate the server process

terminate()

Terminate the server process

class tvm.rpc.RPCSession(sess)

RPC Client session module

Do not directly create the obhect, call connect

Methods

cl([dev_id])

Construct OpenCL device.

context(dev_type[, dev_id])

Construct a remote context.

cpu([dev_id])

Construct CPU device.

download(path)

Download file from remote temp folder.

ext_dev([dev_id])

Construct extension device.

get_function(name)

Get function from the session.

gpu([dev_id])

Construct GPU device.

load_module(path)

Load a remote module, the file need to be uploaded first.

metal([dev_id])

Construct Metal device.

opengl([dev_id])

Construct OpenGL device.

remove(path)

Remove file from remote temp folder.

upload(data[, target])

Upload file to remote runtime temp folder

vulkan([dev_id])

Construct Vulkan device.

get_function(name)

Get function from the session.

Parameters

name (str) – The name of the function

Returns

f – The result function.

Return type

Function

context(dev_type, dev_id=0)

Construct a remote context.

Parameters
  • dev_type (int or str) –

  • dev_id (int, optional) –

Returns

ctx – The corresponding encoded remote context.

Return type

TVMContext

upload(data, target=None)

Upload file to remote runtime temp folder

Parameters
  • data (str or bytearray) – The file name or binary in local to upload.

  • target (str, optional) – The path in remote

download(path)

Download file from remote temp folder.

Parameters

path (str) – The relative location to remote temp folder.

Returns

blob – The result blob from the file.

Return type

bytearray

remove(path)

Remove file from remote temp folder.

Parameters

path (str) – The relative location to remote temp folder.

load_module(path)

Load a remote module, the file need to be uploaded first.

Parameters

path (str) – The relative location to remote temp folder.

Returns

m – The remote module containing remote function.

Return type

Module

cpu(dev_id=0)

Construct CPU device.

gpu(dev_id=0)

Construct GPU device.

cl(dev_id=0)

Construct OpenCL device.

vulkan(dev_id=0)

Construct Vulkan device.

metal(dev_id=0)

Construct Metal device.

opengl(dev_id=0)

Construct OpenGL device.

ext_dev(dev_id=0)

Construct extension device.

class tvm.rpc.LocalSession

RPCSession interface backed by local environment.

This class can be used to implement functions that need to be ran both locally and remotely.

Methods

download(path)

Download file from remote temp folder.

load_module(path)

Load a remote module, the file need to be uploaded first.

upload(data[, target])

Upload file to remote runtime temp folder

upload(data, target=None)

Upload file to remote runtime temp folder

Parameters
  • data (str or bytearray) – The file name or binary in local to upload.

  • target (str, optional) – The path in remote

download(path)

Download file from remote temp folder.

Parameters

path (str) – The relative location to remote temp folder.

Returns

blob – The result blob from the file.

Return type

bytearray

load_module(path)

Load a remote module, the file need to be uploaded first.

Parameters

path (str) – The relative location to remote temp folder.

Returns

m – The remote module containing remote function.

Return type

Module

class tvm.rpc.TrackerSession(addr)

Tracker client session.

Parameters

addr (tuple) – The address tuple

Methods

close()

Close the tracker connection.

request(key[, priority, session_timeout, …])

Request a new connection from the tracker.

request_and_run(key, func[, priority, …])

Request a resource from tracker and run the func.

summary()

Get the summary dict of the tracker.

text_summary()

Get a text summary of the tracker.

close()

Close the tracker connection.

summary()

Get the summary dict of the tracker.

text_summary()

Get a text summary of the tracker.

request(key, priority=1, session_timeout=0, max_retry=5)

Request a new connection from the tracker.

Parameters
  • key (str) – The type key of the device.

  • priority (int, optional) – The priority of the request.

  • session_timeout (float, optional) – The duration of the session, allows server to kill the connection when duration is longer than this value. When duration is zero, it means the request must always be kept alive.

  • max_retry (int, optional) – Maximum number of times to retry before give up.

request_and_run(key, func, priority=1, session_timeout=0, max_retry=2)

Request a resource from tracker and run the func.

This function safe-guard rare server node dropout during execution. In such case, a new resource will be requested and func will be ran again.

Parameters
  • key (str) – The type key of the device.

  • func (function of session -> value) – A stateless function

  • priority (int, optional) – The priority of the request.

  • session_timeout (float, optional) – The duration of the session, allows server to kill the connection when duration is longer than this value. When duration is zero, it means the request must always be kept alive.

  • max_retry (int, optional) – Maximum number of times to retry the function before give up.

tvm.rpc.connect(url, port, key='', session_timeout=0)

Connect to RPC Server

Parameters
  • url (str) – The url of the host

  • port (int) – The port to connect to

  • key (str, optional) – Additional key to match server

  • session_timeout (float, optional) – The duration of the session, allows server to kill the connection when duration is longer than this value. When duration is zero, it means the request must always be kept alive.

Returns

sess – The connected session.

Return type

RPCSession

tvm.rpc.connect_tracker(url, port)

Connect to a RPC tracker

Parameters
  • url (str) – The url of the host

  • port (int) – The port to connect to

Returns

sess – The connected tracker session.

Return type

TrackerSession