u99127 commented on a change in pull request #4274: [µTVM] Enable AutoTVM for
ARM STM32F746XX Boards
URL: https://github.com/apache/incubator-tvm/pull/4274#discussion_r349747601
##########
File path: python/tvm/micro/base.py
##########
@@ -14,129 +14,157 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-
-"""Base definitions for micro."""
+"""Base definitions for MicroTVM"""
from __future__ import absolute_import
-import logging
import os
import sys
+from enum import Enum
+import tvm
from tvm.contrib import util as _util
from tvm.contrib import cc as _cc
-
from .._ffi.function import _init_api
-from .._ffi.libinfo import find_include_path
-SUPPORTED_DEVICE_TYPES = ["host", "openocd"]
+class LibType(Enum):
+ """Enumeration of library types that can be compiled and loaded onto a
device"""
+ # library to be used as a MicroTVM runtime
+ RUNTIME = 0
+ # library to be used as an operator
+ OPERATOR = 1
+
class Session:
"""MicroTVM Device Session
Parameters
----------
- device_type : str
- type of low-level device
-
- toolchain_prefix : str
- toolchain prefix to be used. For example, a prefix of
- "riscv64-unknown-elf-" means "riscv64-unknown-elf-gcc" is used as
- the compiler and "riscv64-unknown-elf-ld" is used as the linker,
- etc.
+ config : dict
+ configuration for this session (as generated by
+ `tvm.micro.device.host.default_config()`, for example)
Example
--------
.. code-block:: python
c_mod = ... # some module generated with "c" as the target
- device_type = "openocd"
- toolchain_prefix = "riscv64-unknown-elf-"
- with tvm.micro.Session(device_type,
- toolchain_prefix,
- base_addr=0x10010000,
- server_addr="127.0.0.1",
- port=6666):
- c_mod.export_library(lib_obj_path,
fcompile=tvm.micro.cross_compiler(toolchain_prefix))
- micro_mod = tvm.module.load(lib_obj_path, "micro_dev")
+ dev_config = micro.device.arm.stm32f746xx.default_config("127.0.0.1",
6666)
+ with tvm.micro.Session(dev_config) as sess:
+ micro_mod = sess.create_micro_mod(c_mod)
"""
- def __init__(self, device_type, toolchain_prefix, **kwargs):
- if device_type not in SUPPORTED_DEVICE_TYPES:
- raise RuntimeError("unknown micro device type
\"{}\"".format(device_type))
+ def __init__(self, config):
self._check_system()
- self._check_args(device_type, kwargs)
+ # TODO(weberlo): add config validation
+
+ # grab a binutil instance from the ID in the config
+ dev_funcs = tvm.micro.device.get_device_funcs(config["device_id"])
+ self.create_micro_lib = dev_funcs["create_micro_lib"]
+ self.toolchain_prefix = config["toolchain_prefix"]
+ self.mem_layout = config["mem_layout"]
+ self.word_size = config["word_size"]
+ self.thumb_mode = config["thumb_mode"]
+ self.comms_method = config["comms_method"]
# First, find and compile runtime library.
- runtime_src_path = os.path.join(_get_micro_device_dir(),
"utvm_runtime.c")
+ runtime_src_path = os.path.join(get_micro_host_driven_dir(),
"utvm_runtime.c")
tmp_dir = _util.tempdir()
runtime_obj_path = tmp_dir.relpath("utvm_runtime.obj")
- create_micro_lib(
- runtime_obj_path, runtime_src_path, toolchain_prefix,
include_dev_lib_header=False)
+ self.create_micro_lib(runtime_obj_path, runtime_src_path,
LibType.RUNTIME)
+ #input(f"check {runtime_obj_path}: ")
+
+ comms_method = config["comms_method"]
+ if comms_method == "openocd":
+ server_addr = config["server_addr"]
+ server_port = config["server_port"]
+ elif comms_method == "host":
+ server_addr = ""
+ server_port = 0
+ else:
+ raise RuntimeError(f"unknown communication method:
f{self.comms_method}")
- base_addr = kwargs.get("base_addr", 0)
- server_addr = kwargs.get("server_addr", "")
- port = kwargs.get("port", 0)
self.module = _CreateSession(
- device_type, runtime_obj_path, toolchain_prefix, base_addr,
server_addr, port)
+ comms_method,
+ runtime_obj_path,
+ self.toolchain_prefix,
+ self.mem_layout["text"].get("start", 0),
+ self.mem_layout["text"]["size"],
+ self.mem_layout["rodata"].get("start", 0),
+ self.mem_layout["rodata"]["size"],
+ self.mem_layout["data"].get("start", 0),
+ self.mem_layout["data"]["size"],
+ self.mem_layout["bss"].get("start", 0),
+ self.mem_layout["bss"]["size"],
+ self.mem_layout["args"].get("start", 0),
+ self.mem_layout["args"]["size"],
+ self.mem_layout["heap"].get("start", 0),
+ self.mem_layout["heap"]["size"],
+ self.mem_layout["workspace"].get("start", 0),
+ self.mem_layout["workspace"]["size"],
+ self.mem_layout["stack"].get("start", 0),
+ self.mem_layout["stack"]["size"],
+ self.word_size,
+ self.thumb_mode,
+ server_addr,
+ server_port)
self._enter = self.module["enter"]
self._exit = self.module["exit"]
+ def create_micro_mod(self, c_mod):
+ """Produces a micro module from a given module.
+
+ Parameters
+ ----------
+ c_mod : tvm.module.Module
+ module with "c" as its target backend
+
+ Return
+ ------
+ micro_mod : tvm.module.Module
+ micro module for the target device
+ """
+ temp_dir = _util.tempdir()
+ lib_obj_path = temp_dir.relpath("dev_lib.obj")
+ c_mod.export_library(
+ lib_obj_path,
+ fcompile=cross_compiler(self.create_micro_lib, LibType.OPERATOR))
+ micro_mod = tvm.module.load(lib_obj_path)
+ return micro_mod
+
def _check_system(self):
"""Check if the user's system is supported by MicroTVM.
Raises error if not supported.
"""
if not sys.platform.startswith("linux"):
- raise RuntimeError("microTVM is currently only supported on Linux")
+ raise RuntimeError("MicroTVM is currently only supported on Linux")
# TODO(weberlo): Add 32-bit support.
# It's primarily the compilation pipeline that isn't compatible.
if sys.maxsize <= 2**32:
- raise RuntimeError("microTVM is currently only supported on 64-bit
platforms")
-
- def _check_args(self, device_type, args):
- """Check if the given configuration is valid."""
- if device_type == "host":
- pass
- elif device_type == "openocd":
- assert "base_addr" in args
- assert "server_addr" in args
- assert "port" in args
+ raise RuntimeError("MicroTVM is currently only supported on 64-bit
platforms")
Review comment:
On 64 bit host platforms and that's really because of the "host emulation" ?
----------------------------------------------------------------
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