tqchen commented on a change in pull request #4855: [Refactor] move vm.py under runtime and adt to runtime.container.py URL: https://github.com/apache/incubator-tvm/pull/4855#discussion_r377780840
########## File path: python/tvm/runtime/vm.py ########## @@ -0,0 +1,357 @@ +# License .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. +# pylint: disable=no-else-return, unidiomatic-typecheck, undefined-variable, invalid-name, redefined-builtin +""" +The Relay Virtual Machine runtime. + +Implements a Python interface to executing the compiled VM object. +""" +import numpy as np + +import tvm +from tvm._ffi.runtime_ctypes import TVMByteArray +from tvm._ffi import base as _base +from .object import Object +from . import _ffi_api, container + +def _convert(arg, cargs): + if isinstance(arg, Object): + cargs.append(arg) + elif isinstance(arg, np.ndarray): + nd_arr = tvm.nd.array(arg, ctx=tvm.cpu(0)) + cargs.append(nd_arr) + elif isinstance(arg, tvm.nd.NDArray): + cargs.append(arg) + elif isinstance(arg, (tuple, list)): + field_args = [] + for field in arg: + _convert(field, field_args) + cargs.append(container.tuple_object(field_args)) + elif isinstance(arg, (_base.numeric_types, bool)): + dtype = "int32" if isinstance(arg, (int, bool)) else "float32" + value = tvm.nd.array(np.array(arg, dtype=dtype), ctx=tvm.cpu(0)) + cargs.append(value) + else: + raise TypeError("Unsupported type: %s" % (type(arg))) + + +def convert(args): + cargs = [] + for arg in args: + _convert(arg, cargs) + + return cargs + + +class Executable(object): + """Relay VM executable""" + def __init__(self, mod): + self.mod = mod + self._function_params = {} + self._save = self.mod["save"] + self._get_lib = self.mod["get_lib"] + self._get_bytecode = self.mod["get_bytecode"] + self._get_stats = self.mod["get_stats"] + self._get_function_arity = self.mod["get_function_arity"] + self._get_function_param_name = self.mod["get_function_param_name"] + + def save(self): + """Save the Relay VM Executable. + + Returns + ------- + code : bytearray + The binary blob representing a serialized Relay VM executable. It + can then be saved to disk and later deserialized into a new + Executable. + + lib : :py:class:`~tvm.runtime.Module` + The runtime module that contains the generated code. It is + basically a library that is composed of hardware dependent code. + + Notes + ----- + The returned code is organized with the following sections in order. + - Global section. This section contains the globals used by the Review comment: possibly need a new line here(due to rst restriction) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
