Skip to content

Commit

Permalink
[REFACTOR][PY-API] Polish tvm.runtime, tvm.runtime.module API update
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tqchen committed Feb 7, 2020
1 parent 62543d4 commit 5cf1fd8
Show file tree
Hide file tree
Showing 169 changed files with 818 additions and 802 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
7 changes: 7 additions & 0 deletions include/tvm/runtime/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ class TVM_DLL ModuleNode : public Object {
std::shared_ptr<PackedFunc> > import_cache_;
};

/*!
* \brief Check if runtime module is enabled for target.
* \param target The target module name.
* \return Whether runtime is enabled.
*/
TVM_DLL bool RuntimeEnabled(const std::string& target);

/*! \brief namespace for constant symbols */
namespace symbol {
/*! \brief Global variable to store module context. */
Expand Down
2 changes: 1 addition & 1 deletion jvm/core/src/test/scripts/test_add_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from tvm.contrib import cc, util

def test_add(target_dir):
if not tvm.module.enabled("cuda"):
if not tvm.runtime.enabled("cuda"):
print("skip %s because cuda is not enabled..." % __file__)
return
n = tvm.var("n")
Expand Down
4 changes: 0 additions & 4 deletions python/tvm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@
# top-level alias
# tvm.runtime
from .runtime.object import Object
from .runtime.packed_func import PackedFunc as Function
from .runtime.ndarray import context, cpu, gpu, opencl, cl, vulkan, metal, mtl
from .runtime.ndarray import vpi, rocm, opengl, ext_dev, micro_dev
from .runtime import module
from .runtime import ndarray
# pylint: disable=reimported
from .runtime import ndarray as nd

# others
Expand Down
19 changes: 19 additions & 0 deletions python/tvm/_ffi/_ctypes/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,22 @@ def __init_handle_by_constructor__(self, fconstructor, *args):
if not isinstance(handle, ObjectHandle):
handle = ObjectHandle(handle)
self.handle = handle

def same_as(self, other):
"""Check object identity.
Parameters
----------
other : object
The other object to compare against.
Returns
-------
result : bool
The comparison result.
"""
if not isinstance(other, ObjectBase):
return False
if self.handle is None:
return other.handle is None
return self.handle.value == other.handle.value
17 changes: 17 additions & 0 deletions python/tvm/_ffi/_cython/object.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,20 @@ cdef class ObjectBase:
(<PackedFuncBase>fconstructor).chandle,
kTVMObjectHandle, args, &chandle)
self.chandle = chandle

def same_as(self, other):
"""Check object identity.
Parameters
----------
other : object
The other object to compare against.
Returns
-------
result : bool
The comparison result.
"""
if not isinstance(other, ObjectBase):
return False
return self.chandle == (<ObjectBase>other).chandle
Loading

0 comments on commit 5cf1fd8

Please sign in to comment.