Skip to content

Commit

Permalink
[REFACTOR][PY][API-Change] Polish tvm.runtime, tvm.runtime.module API…
Browse files Browse the repository at this point in the history
… update (apache#4837)

* [REFACTOR][PY-API] Polish tvm.runtime, tvm.runtime.module API update

This PR updates the tvm.runtime to use the new FFI style.

- Remove top-level tvm.module to avoid confusion between runtime.Module and IRModule
- API changes wrt to runtime.Module
  - tvm.module.load -> tvm.runtime.load_module
  - tvm.module.enabled -> tvm.runtime.enabled
  - tvm.module.system_lib -> tvm.runtime.system_lib
- Remove dep on api_internal from runtime.

* Update module.load in the latest API
  • Loading branch information
tqchen authored and Ubuntu committed Feb 10, 2020
1 parent 7984f77 commit 6819d9e
Show file tree
Hide file tree
Showing 174 changed files with 835 additions and 819 deletions.
2 changes: 1 addition & 1 deletion apps/bundle_deploy/bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TVM_BUNDLE_FUNCTION void *tvm_runtime_create() {
const std::string json_data(&build_graph_json[0],
&build_graph_json[0] + build_graph_json_len);
tvm::runtime::Module mod_syslib =
(*tvm::runtime::Registry::Get("module._GetSystemLib"))();
(*tvm::runtime::Registry::Get("runtime.SystemLib"))();
int device_type = kDLCPU;
int device_id = 0;
tvm::runtime::Module mod =
Expand Down
2 changes: 1 addition & 1 deletion apps/dso_plugin_module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Example Plugin Module
=====================
This folder contains an example that implements a C++ module
that can be directly loaded as TVM's DSOModule (via tvm.module.load)
that can be directly loaded as TVM's DSOModule (via tvm.runtime.load_module)

## Guideline

Expand Down
2 changes: 1 addition & 1 deletion apps/dso_plugin_module/test_plugin_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

def test_plugin_module():
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
mod = tvm.module.load(os.path.join(curr_path, "lib", "plugin_module.so"))
mod = tvm.runtime.load_module(os.path.join(curr_path, "lib", "plugin_module.so"))
# NOTE: we need to make sure all managed resources returned
# from mod get destructed before mod get unloaded.
#
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_ext_dev():
B = tvm.compute((n,), lambda *i: A(*i) + 1.0, name='B')
s = tvm.create_schedule(B.op)
def check_llvm():
if not tvm.module.enabled("llvm"):
if not tvm.runtime.enabled("llvm"):
return
f = tvm.build(s, [A, B], "ext_dev", "llvm")
ctx = tvm.ext_dev(0)
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_extern_call():
s = tvm.create_schedule(B.op)

def check_llvm():
if not tvm.module.enabled("llvm"):
if not tvm.runtime.enabled("llvm"):
return
f = tvm.build(s, [A, B], "llvm")
ctx = tvm.cpu(0)
Expand Down
6 changes: 3 additions & 3 deletions apps/howto_deploy/cpp_deploy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down Expand Up @@ -79,7 +79,7 @@ int main(void) {
// For libraries that are directly packed as system lib and linked together with the app
// We can directly use GetSystemLib to get the system wide library.
LOG(INFO) << "Verify load function from system lib";
tvm::runtime::Module mod_syslib = (*tvm::runtime::Registry::Get("module._GetSystemLib"))();
tvm::runtime::Module mod_syslib = (*tvm::runtime::Registry::Get("runtime.SystemLib"))();
Verify(mod_syslib, "addonesys");
return 0;
}
14 changes: 7 additions & 7 deletions apps/howto_deploy/python_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# 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
Expand All @@ -26,21 +26,21 @@ def verify(mod, fname):
f = mod.get_function(fname)
# Use tvm.nd.array to convert numpy ndarray to tvm
# NDArray type, so that function can be invoked normally
N = 10
N = 10
x = tvm.nd.array(np.arange(N, dtype=np.float32))
y = tvm.nd.array(np.zeros(N, dtype=np.float32))
# Invoke the function
f(x, y)
np_x = x.asnumpy()
np_y = y.asnumpy()
np_x = x.asnumpy()
np_y = y.asnumpy()
# Verify correctness of function
assert(np.all([xi+1 == yi for xi, yi in zip(np_x, np_y)]))
print("Finish verification...")


if __name__ == "__main__":
# The normal dynamic loading method for deployment
mod_dylib = tvm.module.load("lib/test_addone_dll.so")
mod_dylib = tvm.runtime.load_module("lib/test_addone_dll.so")
print("Verify dynamic loading from test_addone_dll.so")
verify(mod_dylib, "addone")
# There might be methods to use the system lib way in
Expand Down
2 changes: 1 addition & 1 deletion apps/sgx/run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

def main():
ctx = tvm.context('cpu', 0)
model = tvm.module.load(osp.join(CWD, 'build', 'enclave.signed.so'))
model = tvm.runtime.load_module(osp.join(CWD, 'build', 'enclave.signed.so'))
inp = tvm.nd.array(np.ones((1, 3, 224, 224), dtype='float32'), ctx)
out = tvm.nd.array(np.empty((1, 1000), dtype='float32'), ctx)
model(inp, out)
Expand Down
24 changes: 0 additions & 24 deletions docs/api/python/bridge.rst

This file was deleted.

15 changes: 12 additions & 3 deletions docs/api/python/contrib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
specific language governing permissions and limitations
under the License.
Additional Contrib APIs
-----------------------
tvm.contrib
-----------
.. automodule:: tvm.contrib

tvm.contrib.cblas
Expand All @@ -43,6 +43,11 @@ tvm.contrib.cublas
:members:


tvm.contrib.dlpack
~~~~~~~~~~~~~~~~~~
.. automodule:: tvm.contrib.dlpack
:members:

tvm.contrib.emscripten
~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: tvm.contrib.emscripten
Expand All @@ -53,6 +58,11 @@ tvm.contrib.miopen
.. automodule:: tvm.contrib.miopen
:members:

tvm.contrib.mxnet
~~~~~~~~~~~~~~~~~
.. automodule:: tvm.contrib.mxnet
:members:

tvm.contrib.ndk
~~~~~~~~~~~~~~~
.. automodule:: tvm.contrib.ndk
Expand Down Expand Up @@ -118,7 +128,6 @@ tvm.contrib.util
:members:



tvm.contrib.xcode
~~~~~~~~~~~~~~~~~
.. automodule:: tvm.contrib.xcode
Expand Down
7 changes: 0 additions & 7 deletions docs/api/python/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ Developer API
This page contains modules that are used by developers of TVM.
Many of these APIs are PackedFunc registered in C++ backend.

tvm.object
~~~~~~~~~~
.. automodule:: tvm.object

.. autoclass:: tvm.object.Object
:members:

.. autofunction:: tvm.register_object

tvm.expr
~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion docs/api/python/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ Python API
:maxdepth: 2

tvm
runtime
ndarray
intrin
tensor
schedule
target
build
module
error
ndarray
container
function
autotvm
graph_runtime
rpc
bridge
contrib
ffi
dev
topi
vta/index
Expand Down
21 changes: 0 additions & 21 deletions docs/api/python/module.rst

This file was deleted.

20 changes: 10 additions & 10 deletions docs/api/python/ndarray.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
specific language governing permissions and limitations
under the License.
tvm.ndarray
-----------
.. automodule:: tvm.ndarray
tvm.runtime.ndarray
-------------------
.. automodule:: tvm.runtime.ndarray

.. autoclass:: tvm.ndarray.TVMContext
.. autoclass:: tvm.nd.NDArray
:members:
:inherited-members:


.. autoclass:: tvm.ndarray.NDArray
.. autoclass:: tvm.runtime.TVMContext
:members:
:inherited-members:

.. autofunction:: tvm.context
.. autofunction:: tvm.cpu
.. autofunction:: tvm.gpu
.. autofunction:: tvm.opencl
.. autofunction:: tvm.metal
.. autofunction:: tvm.ndarray.array
.. autofunction:: tvm.ndarray.empty

.. autofunction:: tvm.register_extension
.. autofunction:: tvm.nd.array
.. autofunction:: tvm.nd.empty
27 changes: 24 additions & 3 deletions docs/api/python/function.rst → docs/api/python/runtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,31 @@
specific language governing permissions and limitations
under the License.
tvm.Function
------------
.. autoclass:: tvm.Function
tvm.runtime
-----------

.. automodule:: tvm.runtime


.. autoclass:: tvm.runtime.PackedFunc
:members:

.. autofunction:: tvm.register_func

.. autofunction:: tvm.get_global_func


.. autoclass:: tvm.runtime.Module
:members:

.. autofunction:: tvm.runtime.load_module

.. autofunction:: tvm.runtime.system_lib

.. autofunction:: tvm.runtime.enabled


.. autoclass:: tvm.runtime.Object
:members:

.. autofunction:: tvm.register_object
4 changes: 2 additions & 2 deletions docs/deploy/aocl_fpga.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ import os
tgt="aocl_sw_emu"
fadd = tvm.module.load("myadd.so")
fadd_dev = tvm.module.load("myadd.aocx")
fadd = tvm.runtime.load("myadd.so")
fadd_dev = tvm.runtime.load("myadd.aocx")
fadd.import_module(fadd_dev)
ctx = tvm.context(tgt, 0)
Expand Down
6 changes: 3 additions & 3 deletions docs/deploy/aws_fpga.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ import os

tgt="sdaccel"

fadd = tvm.module.load("myadd.so")
fadd = tvm.runtime.load("myadd.so")
if os.environ.get("XCL_EMULATION_MODE"):
fadd_dev = tvm.module.load("myadd.xclbin")
fadd_dev = tvm.runtime.load("myadd.xclbin")
else:
fadd_dev = tvm.module.load("myadd.awsxclbin")
fadd_dev = tvm.runtime.load("myadd.awsxclbin")
fadd.import_module(fadd_dev)

ctx = tvm.context(tgt, 0)
Expand Down
4 changes: 2 additions & 2 deletions docs/dev/introduction_to_module_serialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Let us build one ResNet-18 workload for GPU as an example first.
resnet18_lib.export_library(path_lib)
# load it back
loaded_lib = tvm.module.load(path_lib)
loaded_lib = tvm.runtime.load(path_lib)
assert loaded_lib.type_key == "library"
assert loaded_lib.imported_modules[0].type_key == "cuda"
Expand Down Expand Up @@ -177,7 +177,7 @@ support arbitrary modules to import ideally.
Deserialization
****************

The entrance API is ``tvm.module.load``. This function
The entrance API is ``tvm.runtime.load``. This function
is to call ``_LoadFromFile`` in fact. If we dig it a little deeper, this is
``Module::LoadFromFile``. In our example, the file is ``deploy.so``,
according to the function logic, we will call ``module.loadfile_so`` in
Expand Down
Loading

0 comments on commit 6819d9e

Please sign in to comment.