[Runtime] Allow for parameter sharing in GraphRuntime (#3384)
authorAndrew Tulloch <andrew@tullo.ch>
Tue, 25 Jun 2019 04:06:20 +0000 (21:06 -0700)
committerTianqi Chen <tqchen@users.noreply.github.com>
Tue, 25 Jun 2019 04:06:20 +0000 (21:06 -0700)
commit32be34a07f72e1ac008b385a07ca1cb66ec59e6e
tree97f51ffedadc414e719e678f7f7b3ff6d0f29dbc
parente97c01012d3a04c1f2a39abbc810c34395c74080
[Runtime] Allow for parameter sharing in GraphRuntime (#3384)

Summary:

In multi-threaded applications where we have multiple inferences on the
same model in parallel (consider e.g. a TTS system handling multiple
requests), it can be useful to share the parameters of a model amongst
these multiple instances. This improves the cache utilization behaviour
of the system, as multiple cores can use the same set of weights instead
of evicting the identical copies of weights in a shared cache.

As the underlying `NDArray` instances in `data_entry_` implement a
ref-counted based sharing system, this is a simple modification of the
`GraphRuntime::LoadParams` logic to instead copy parameters from an
existing GraphRuntime instance. This is a little ugly in that we need
both the pre-existing GraphRuntime instance, as well as the 'serialized'
params (since we need to know the set of names we should copy), but
without imposing additional assumptions (i.e. storing the set of param
names in GraphRuntime, and enforcing that shared param names are
identical to the parameters set in the preceding `LoadParams` call),
this seems unavoidable.

Test Plan:

Unit test added.
python/tvm/contrib/graph_runtime.py
src/runtime/graph/graph_runtime.cc
src/runtime/graph/graph_runtime.h
tests/python/unittest/test_runtime_graph.py