From 2b921335f9a718ef7c15c196fe72289150a17613 Mon Sep 17 00:00:00 2001 From: Zhi Chen Date: Tue, 11 Feb 2020 00:17:59 +0000 Subject: [PATCH] use _ffi_api --- python/tvm/relay/backend/vm.py | 2 +- python/tvm/runtime/_vm.py | 21 ------ python/tvm/runtime/profiler_vm.py | 7 +- python/tvm/runtime/vm.py | 64 +++---------------- src/runtime/vm/executable.cc | 10 +-- src/runtime/vm/profiler/vm.cc | 2 +- src/runtime/vm/vm.cc | 2 +- .../unittest/test_runtime_vm_profiler.py | 2 + 8 files changed, 24 insertions(+), 86 deletions(-) delete mode 100644 python/tvm/runtime/_vm.py diff --git a/python/tvm/relay/backend/vm.py b/python/tvm/relay/backend/vm.py index 1c635a40de66..b61fafda1d1d 100644 --- a/python/tvm/relay/backend/vm.py +++ b/python/tvm/relay/backend/vm.py @@ -59,7 +59,7 @@ def compile(mod, target=None, target_host=None, params=None): Returns ------- - exec : tvm.runtime.vmf.Executable + exec : tvm.runtime.vm.Executable The VM executable that contains both library code and bytecode. """ compiler = VMCompiler() diff --git a/python/tvm/runtime/_vm.py b/python/tvm/runtime/_vm.py deleted file mode 100644 index 939d3e12c93c..000000000000 --- a/python/tvm/runtime/_vm.py +++ /dev/null @@ -1,21 +0,0 @@ -# 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. -"""The Relay virtual machine FFI namespace. -""" -import tvm._ffi - -tvm._ffi._init_api("runtime._vm", __name__) diff --git a/python/tvm/runtime/profiler_vm.py b/python/tvm/runtime/profiler_vm.py index fa0326edb95b..11cedd5fa200 100644 --- a/python/tvm/runtime/profiler_vm.py +++ b/python/tvm/runtime/profiler_vm.py @@ -20,18 +20,19 @@ Provides extra APIs for profiling vm execution. """ -from . import vm, _vm +from tvm.runtime import _ffi_api +from . import vm def enabled(): """Whether vm profiler is enabled.""" - return hasattr(_vm, "_VirtualMachineDebug") + return hasattr(_ffi_api, "_VirtualMachineDebug") class VirtualMachineProfiler(vm.VirtualMachine): """Relay profile VM runtime.""" def __init__(self, mod): super(VirtualMachineProfiler, self).__init__(mod) m = mod.module if isinstance(mod, vm.Executable) else mod - self.mod = _vm._VirtualMachineDebug(m) + self.mod = _ffi_api._VirtualMachineDebug(m) self._init = self.mod["init"] self._invoke = self.mod["invoke"] self._get_stat = self.mod["get_stat"] diff --git a/python/tvm/runtime/vm.py b/python/tvm/runtime/vm.py index aa2ea0f586b1..b6de7f36f42a 100644 --- a/python/tvm/runtime/vm.py +++ b/python/tvm/runtime/vm.py @@ -16,24 +16,20 @@ # under the License. # pylint: disable=no-else-return, unidiomatic-typecheck, undefined-variable, invalid-name, redefined-builtin """ -The Relay Virtual Machine. +The Relay Virtual Machine runtime. -Implements a Python interface to compiling and executing on the Relay VM. +Implements a Python interface to executing the compiled VM object. """ import numpy as np import tvm from tvm.runtime import Object, container -from tvm.relay import expr as _expr +from tvm.runtime import _ffi_api from tvm._ffi.runtime_ctypes import TVMByteArray from tvm._ffi import base as _base -from tvm.relay.backend.interpreter import Executor -from . import _vm def _convert(arg, cargs): - if isinstance(arg, _expr.Constant): - cargs.append(arg.data) - elif isinstance(arg, Object): + if isinstance(arg, Object): cargs.append(arg) elif isinstance(arg, np.ndarray): nd_arr = tvm.nd.array(arg, ctx=tvm.cpu(0)) @@ -163,7 +159,7 @@ def load_exec(bytecode, lib): raise TypeError("lib is expected to be the type of tvm.runtime.Module" + ", but received {}".format(type(lib))) - return Executable(_vm.Load_Executable(bytecode, lib)) + return Executable(_ffi_api.Load_Executable(bytecode, lib)) @property def lib(self): @@ -197,9 +193,9 @@ def primitive_ops(self): The list of primitive ops. """ ret = [] - num_primitives = _vm.GetNumOfPrimitives(self.module) + num_primitives = _ffi_api.GetNumOfPrimitives(self.module) for i in range(num_primitives): - ret.append(_vm.GetPrimitiveFields(self.module, i)) + ret.append(_ffi_api.GetPrimitiveFields(self.module, i)) return ret @property @@ -240,9 +236,9 @@ def globals(self): The globals contained in the executable. """ ret = [] - num_globals = _vm.GetNumOfGlobals(self.module) + num_globals = _ffi_api.GetNumOfGlobals(self.module) for i in range(num_globals): - ret.append(_vm.GetGlobalFields(self.module, i)) + ret.append(_ffi_api.GetGlobalFields(self.module, i)) return ret @property @@ -272,7 +268,7 @@ def __init__(self, mod): raise TypeError("mod is expected to be the type of Executable or " + "tvm.Module, but received {}".format(type(mod))) m = mod.module if isinstance(mod, Executable) else mod - self.mod = _vm._VirtualMachine(m) + self.mod = _ffi_api._VirtualMachine(m) self._exec = mod self._init = self.mod["init"] self._invoke = self.mod["invoke"] @@ -359,43 +355,3 @@ def run(self, *args, **kwargs): The output. """ return self.invoke("main", *args, **kwargs) - - -class VMExecutor(Executor): - """ - An implementation of the executor interface for - the Relay VM. - - Useful interface for experimentation and debugging - the VM can also be used directly from the API. - supported by `tvm.runtime.vm`. - - Parameters - ---------- - mod : :py:class:`~tvm.relay.module.Module` - The module to support the execution. - - ctx : :py:class:`~tvm.TVMContext` - The runtime context to run the code on. - - target : :py:class:`Target` - The target option to build the function. - """ - def __init__(self, mod, ctx, target): - if mod is None: - raise RuntimeError("Must provide module to get VM executor.") - self.mod = mod - self.ctx = ctx - self.target = target - self.executable = compile(mod, target) - self.vm = VirtualMachine(self.executable) - self.vm.init(ctx) - - def _make_executor(self, expr=None): - main = self.mod["main"] - - def _vm_wrapper(*args, **kwargs): - args = self._convert_args(main, args, kwargs) - return self.vm.run(*args) - - return _vm_wrapper diff --git a/src/runtime/vm/executable.cc b/src/runtime/vm/executable.cc index 624bf9d4c242..c2036da46e09 100644 --- a/src/runtime/vm/executable.cc +++ b/src/runtime/vm/executable.cc @@ -738,7 +738,7 @@ void Executable::LoadCodeSection(dmlc::Stream* strm) { } } -TVM_REGISTER_GLOBAL("runtime._vm.GetNumOfGlobals") +TVM_REGISTER_GLOBAL("runtime.GetNumOfGlobals") .set_body([](TVMArgs args, TVMRetValue* rv) { runtime::Module mod = args[0]; const auto* exec = dynamic_cast(mod.operator->()); @@ -746,7 +746,7 @@ TVM_REGISTER_GLOBAL("runtime._vm.GetNumOfGlobals") *rv = static_cast(exec->global_map.size()); }); -TVM_REGISTER_GLOBAL("runtime._vm.GetGlobalFields") +TVM_REGISTER_GLOBAL("runtime.GetGlobalFields") .set_body([](TVMArgs args, TVMRetValue* rv) { runtime::Module mod = args[0]; const auto* exec = dynamic_cast(mod.operator->()); @@ -763,7 +763,7 @@ TVM_REGISTER_GLOBAL("runtime._vm.GetGlobalFields") *rv = globals[idx].first; }); -TVM_REGISTER_GLOBAL("runtime._vm.GetNumOfPrimitives") +TVM_REGISTER_GLOBAL("runtime.GetNumOfPrimitives") .set_body([](TVMArgs args, TVMRetValue* rv) { runtime::Module mod = args[0]; const auto* exec = dynamic_cast(mod.operator->()); @@ -772,7 +772,7 @@ TVM_REGISTER_GLOBAL("runtime._vm.GetNumOfPrimitives") }); -TVM_REGISTER_GLOBAL("runtime._vm.GetPrimitiveFields") +TVM_REGISTER_GLOBAL("runtime.GetPrimitiveFields") .set_body([](TVMArgs args, TVMRetValue* rv) { runtime::Module mod = args[0]; const auto* exec = dynamic_cast(mod.operator->()); @@ -789,7 +789,7 @@ TVM_REGISTER_GLOBAL("runtime._vm.GetPrimitiveFields") } }); -TVM_REGISTER_GLOBAL("runtime._vm.Load_Executable") +TVM_REGISTER_GLOBAL("runtime.Load_Executable") .set_body_typed([]( std::string code, runtime::Module lib) { diff --git a/src/runtime/vm/profiler/vm.cc b/src/runtime/vm/profiler/vm.cc index 3b7b7aa8e73e..4dac66e50a82 100644 --- a/src/runtime/vm/profiler/vm.cc +++ b/src/runtime/vm/profiler/vm.cc @@ -133,7 +133,7 @@ runtime::Module CreateVirtualMachineDebug(const Executable* exec) { return runtime::Module(vm); } -TVM_REGISTER_GLOBAL("relay._vm._VirtualMachineDebug") +TVM_REGISTER_GLOBAL("runtime._VirtualMachineDebug") .set_body([](TVMArgs args, TVMRetValue* rv) { runtime::Module mod = args[0]; const auto* exec = dynamic_cast(mod.operator->()); diff --git a/src/runtime/vm/vm.cc b/src/runtime/vm/vm.cc index ced16d799509..af3a86cac17f 100644 --- a/src/runtime/vm/vm.cc +++ b/src/runtime/vm/vm.cc @@ -1057,7 +1057,7 @@ runtime::Module CreateVirtualMachine(const Executable* exec) { return runtime::Module(vm); } -TVM_REGISTER_GLOBAL("runtime._vm._VirtualMachine") +TVM_REGISTER_GLOBAL("runtime._VirtualMachine") .set_body([](TVMArgs args, TVMRetValue* rv) { runtime::Module mod = args[0]; const auto* exec = dynamic_cast(mod.operator->()); diff --git a/tests/python/unittest/test_runtime_vm_profiler.py b/tests/python/unittest/test_runtime_vm_profiler.py index 6de94f18f7e0..849a9ef3f823 100644 --- a/tests/python/unittest/test_runtime_vm_profiler.py +++ b/tests/python/unittest/test_runtime_vm_profiler.py @@ -14,6 +14,8 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import numpy as np + import tvm from tvm.runtime import profiler_vm from tvm import relay