From b1998294a407e83d61d533c00a289aabc504684b Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Wed, 24 Mar 2021 21:58:20 -0700 Subject: [PATCH 01/37] working on qemu --- tests/micro/qemu/test_zephyr.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/micro/qemu/test_zephyr.py b/tests/micro/qemu/test_zephyr.py index 4c8bd5f5dae8..0937342bf993 100644 --- a/tests/micro/qemu/test_zephyr.py +++ b/tests/micro/qemu/test_zephyr.py @@ -62,6 +62,7 @@ def _make_session(model, target, zephyr_board, west_cmd, mod): os.makedirs(workspace_parent) workspace = tvm.micro.Workspace(debug=True, root=workspace_root) + import pdb; pdb.set_trace() project_dir = os.path.join(os.path.dirname(__file__) or ".", "zephyr-runtime") compiler = zephyr.ZephyrCompiler( project_dir=project_dir, @@ -120,6 +121,7 @@ def _make_add_sess(model, zephyr_board, west_cmd): "host": ("host", "qemu_x86"), "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), + "riscv32_host": ("riscv32_host", "qemu_riscv32"), } @@ -184,12 +186,14 @@ def test_relay(platform, west_cmd): xx = relay.multiply(x, x) z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) func = relay.Function([x], z) - + import pdb + target = tvm.target.target.micro(model) with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): graph, mod, params = tvm.relay.build(func, target=target) with _make_session(model, target, zephyr_board, west_cmd, mod) as session: + pdb.set_trace() graph_mod = tvm.micro.create_local_graph_runtime( graph, session.get_system_lib(), session.context ) From 08ea3bad86677b0473b05d64f0dbe0e6fbc99bae Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Sun, 28 Mar 2021 21:18:14 -0700 Subject: [PATCH 02/37] debugging --- python/tvm/micro/contrib/zephyr.py | 19 ++++++++++++++++--- python/tvm/target/target.py | 2 ++ src/runtime/micro/micro_session.cc | 5 +++++ tests/micro/qemu/test_zephyr.py | 12 ++++++------ .../micro/qemu/zephyr-runtime/CMakeLists.txt | 3 +++ tests/micro/qemu/zephyr-runtime/src/main.c | 13 ++++++++++--- 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index cd9c23cd2f9d..a82d0a6af49a 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -531,7 +531,6 @@ def transport(self, micro_binary): serial_transport, ) - class QemuStartupFailureError(Exception): """Raised when the qemu pipe is not present within startup_timeout_sec.""" @@ -571,7 +570,7 @@ def write(self, data, timeout_sec): class ZephyrQemuTransport(Transport): """The user-facing Zephyr QEMU transport class.""" - def __init__(self, base_dir, startup_timeout_sec=5.0, **kwargs): + def __init__(self, base_dir, startup_timeout_sec=5.0, debugger=None, **kwargs): self.base_dir = base_dir self.startup_timeout_sec = startup_timeout_sec self.kwargs = kwargs @@ -588,15 +587,29 @@ def timeouts(self): def open(self): self.pipe_dir = tempfile.mkdtemp() + # self.pipe_dir = "/tmp/test123" self.pipe = os.path.join(self.pipe_dir, "fifo") self.write_pipe = os.path.join(self.pipe_dir, "fifo.in") self.read_pipe = os.path.join(self.pipe_dir, "fifo.out") + + # if os.path.exists(self.read_pipe): + # os.remove(self.read_pipe) + # if os.path.exists(self.write_pipe): + # os.remove(self.write_pipe) + os.mkfifo(self.write_pipe) os.mkfifo(self.read_pipe) + # log_path = "/Users/mhessar/work/tvm/proc.log" + # if not os.path.exists(log_path): + # os.mknod(log_path) + + # with open(log_path, "w") as f_log: self.proc = subprocess.Popen( ["make", "run", f"QEMU_PIPE={self.pipe}"], cwd=self.base_dir, **self.kwargs, + # stdout=f_log, + # stderr=f_log ) # NOTE: although each pipe is unidirectional, open both as RDWR to work around a select # limitation on linux. Without this, non-blocking I/O can't use timeouts because named @@ -622,7 +635,7 @@ def close(self): self.proc = None if self.pipe_dir is not None: - shutil.rmtree(self.pipe_dir) + # shutil.rmtree(self.pipe_dir) self.pipe_dir = None def read(self, n, timeout_sec): diff --git a/python/tvm/target/target.py b/python/tvm/target/target.py index 8c60260e640a..6dd692589f5b 100644 --- a/python/tvm/target/target.py +++ b/python/tvm/target/target.py @@ -252,6 +252,8 @@ def micro(model="unknown", options=None): "host": [], "stm32f746xx": ["-mcpu=cortex-m7", "-march=armv7e-m"], "nrf5340dk": ["-mcpu=cortex-m33"], + "host_riscv32": [], + "host_riscv64": [], } if model not in trans_table: raise ValueError(f"Model {model} not supported by tvm.target.micro.") diff --git a/src/runtime/micro/micro_session.cc b/src/runtime/micro/micro_session.cc index cd916d46971d..76d21cb4c539 100644 --- a/src/runtime/micro/micro_session.cc +++ b/src/runtime/micro/micro_session.cc @@ -331,13 +331,16 @@ class MicroTransportChannel : public RPCChannel { size_t message_size_bytes; switch (message_type) { case MessageType::kStartSessionInit: + LOG(ERROR) << "kStartSessionInit"; break; case MessageType::kStartSessionReply: + LOG(ERROR) << "kStartSessionReply"; state_ = State::kSessionEstablished; break; case MessageType::kTerminateSession: + LOG(ERROR) << "kTerminateSession"; if (state_ == State::kReset) { state_ = State::kSessionTerminated; } else if (state_ == State::kSessionTerminated) { @@ -349,6 +352,7 @@ class MicroTransportChannel : public RPCChannel { break; case MessageType::kLog: + LOG(ERROR) << "kLog"; uint8_t message[1024]; message_size_bytes = buf->ReadAvailable(); if (message_size_bytes == 0) { @@ -366,6 +370,7 @@ class MicroTransportChannel : public RPCChannel { return; case MessageType::kNormal: + LOG(ERROR) << "kNormal"; did_receive_message_ = true; message_buffer_ = buf; break; diff --git a/tests/micro/qemu/test_zephyr.py b/tests/micro/qemu/test_zephyr.py index 0937342bf993..7a762d5cc2b9 100644 --- a/tests/micro/qemu/test_zephyr.py +++ b/tests/micro/qemu/test_zephyr.py @@ -22,6 +22,7 @@ import os import subprocess import sys +import logging import pytest import numpy as np @@ -42,6 +43,7 @@ TARGET = None +_LOG = logging.getLogger(__name__) def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): target = tvm.target.target.micro(model) @@ -52,17 +54,16 @@ def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): def _make_session(model, target, zephyr_board, west_cmd, mod): - test_name = f"{os.path.splitext(os.path.abspath(__file__))[0]}-{model}" + test_name = f"{os.path.splitext(os.path.abspath(__file__))[0]}_{model}" prev_build = f"{test_name}-last-build.micro-binary" workspace_root = ( - f'{test_name}-workspace/{datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")}' + f'{test_name}_workspace/{datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")}' ) workspace_parent = os.path.dirname(workspace_root) if not os.path.exists(workspace_parent): os.makedirs(workspace_parent) workspace = tvm.micro.Workspace(debug=True, root=workspace_root) - import pdb; pdb.set_trace() project_dir = os.path.join(os.path.dirname(__file__) or ".", "zephyr-runtime") compiler = zephyr.ZephyrCompiler( project_dir=project_dir, @@ -119,9 +120,10 @@ def _make_add_sess(model, zephyr_board, west_cmd): # (model, zephyr_board). PLATFORMS = { "host": ("host", "qemu_x86"), + "host-riscv32": ("host_riscv32", "qemu_riscv32"), + "host-riscv64": ("host_riscv64", "qemu_riscv64"), "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), - "riscv32_host": ("riscv32_host", "qemu_riscv32"), } @@ -186,14 +188,12 @@ def test_relay(platform, west_cmd): xx = relay.multiply(x, x) z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) func = relay.Function([x], z) - import pdb target = tvm.target.target.micro(model) with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): graph, mod, params = tvm.relay.build(func, target=target) with _make_session(model, target, zephyr_board, west_cmd, mod) as session: - pdb.set_trace() graph_mod = tvm.micro.create_local_graph_runtime( graph, session.get_system_lib(), session.context ) diff --git a/tests/micro/qemu/zephyr-runtime/CMakeLists.txt b/tests/micro/qemu/zephyr-runtime/CMakeLists.txt index ce5605469fcb..d64cc33857b7 100644 --- a/tests/micro/qemu/zephyr-runtime/CMakeLists.txt +++ b/tests/micro/qemu/zephyr-runtime/CMakeLists.txt @@ -3,6 +3,9 @@ cmake_minimum_required(VERSION 3.13.1) set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack") +#set(ENV{QEMU_BIN_PATH} "/home/vagrant/zephyr-sdk/sysroots/x86_64-pokysdk-linux/usr/bin/") +#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv32") +#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv64") set(QEMU_PIPE "\${QEMU_PIPE}") # QEMU_PIPE is set by the calling TVM instance. diff --git a/tests/micro/qemu/zephyr-runtime/src/main.c b/tests/micro/qemu/zephyr-runtime/src/main.c index e04fc20508b4..08a9efe064cc 100644 --- a/tests/micro/qemu/zephyr-runtime/src/main.c +++ b/tests/micro/qemu/zephyr-runtime/src/main.c @@ -64,15 +64,18 @@ size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const } void TVMPlatformAbort(tvm_crt_error_t error) { - sys_reboot(SYS_REBOOT_COLD); + TVMLogf("TVMPlatformAbort: %x", error); + sys_reboot(SYS_REBOOT_WARM); for (;;) ; } -K_MEM_POOL_DEFINE(tvm_memory_pool, 64, 1024, 120, 4); +// K_MEM_POOL_DEFINE(tvm_memory_pool, 64, 1024, 120, 4); +K_HEAP_DEFINE(tvm_memory_pool, 120*1024); tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLContext ctx, void** out_ptr) { - *out_ptr = k_mem_pool_malloc(&tvm_memory_pool, num_bytes); + // *out_ptr = k_mem_pool_malloc(&tvm_memory_pool, num_bytes); + *out_ptr = k_heap_alloc(&tvm_memory_pool, num_bytes, K_NO_WAIT); return (*out_ptr == NULL) ? kTvmErrorPlatformNoMemory : kTvmErrorNoError; } @@ -248,11 +251,15 @@ void main(void) { utvm_rpc_server_t server = UTvmRpcServerInit(write_serial, NULL); TVMLogf("uTVM On-Device Runtime"); + TVMLogf("Mehrdad"); while (true) { uint8_t buf[256]; int bytes_read = uart_rx_buf_read(&uart_rx_buf, buf, sizeof(buf)); + // TVMLogf("mehrdad: loop r: %d", bytes_read); if (bytes_read > 0) { + // for (int i_read=0; i_read 0) { From 7844d012f251b82adc603bc6265c988cfed70554 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Sun, 28 Mar 2021 21:19:07 -0700 Subject: [PATCH 03/37] riscv hacks --- .../qemu-hack-riscv32/qemu-system-riscv32 | 33 +++++++++++++++++ .../qemu-hack-riscv64/qemu-system-riscv64 | 37 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 create mode 100755 tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 diff --git a/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 new file mode 100755 index 000000000000..a0bf0f2c4dee --- /dev/null +++ b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 @@ -0,0 +1,33 @@ +#!/bin/bash -e +# 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. + +# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to +# work given the way we've configured docker (the underlying filesystem doesn't suppor the +# file locking it needs to). This script strips any -pidfile option, then invokes qemu. + +ARGS=( "$(basename $0)" ) +while [ "$#" -gt 0 ]; do + if [ "$1" == "-pidfile" ]; then + shift + else + ARGS=( "${ARGS[@]}" "$1" ) + fi + shift +done + +"${ARGS[@]}" diff --git a/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 new file mode 100755 index 000000000000..eb4e12c4f2f8 --- /dev/null +++ b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 @@ -0,0 +1,37 @@ +#!/bin/bash -e +# 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. + +# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to +# work given the way we've configured docker (the underlying filesystem doesn't suppor the +# file locking it needs to). This script strips any -pidfile option, then invokes qemu. + +ARGS=( "$(basename $0)" ) +while [ "$#" -gt 0 ]; do + if [ "$1" == "-pidfile" ]; then + shift + else + ARGS=( "${ARGS[@]}" "$1" ) + fi + shift +done + +if [ "${TVM_QEMU_DEBUG}" != "" ]; then + ARGS=( "${ARGS[@]}" -s -S ) +fi + +"${ARGS[@]}" From da1390e1497bf1c4ef627411f75a987bf7680e65 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 00:11:01 -0700 Subject: [PATCH 04/37] config added --- .../zephyr/demo_runtime/CMakeLists.txt | 3 --- .../demo_runtime/boards/qemu_riscv32.conf | 22 +++++++++++++++++++ .../demo_runtime/boards/qemu_riscv64.conf | 22 +++++++++++++++++++ .../qemu-system-riscv32 | 0 .../qemu-system-riscv64 | 0 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf create mode 100644 apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf rename apps/microtvm/zephyr/demo_runtime/{qemu-hack-riscv32 => qemu-hack}/qemu-system-riscv32 (100%) rename apps/microtvm/zephyr/demo_runtime/{qemu-hack-riscv64 => qemu-hack}/qemu-system-riscv64 (100%) diff --git a/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt b/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt index 9ee5ff64f639..a99d5edb07e6 100644 --- a/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt +++ b/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt @@ -3,9 +3,6 @@ cmake_minimum_required(VERSION 3.13.1) set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack") -#set(ENV{QEMU_BIN_PATH} "/home/vagrant/zephyr-sdk/sysroots/x86_64-pokysdk-linux/usr/bin/") -#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv32") -#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv64") set(QEMU_PIPE "\${QEMU_PIPE}") # QEMU_PIPE is set by the calling TVM instance. diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf new file mode 100644 index 000000000000..01f66e65c8bb --- /dev/null +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf @@ -0,0 +1,22 @@ +# 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. + +# This file is specific to the QEMU-emulated microTVM board. + +# For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_TIMER_RANDOM_GENERATOR=y diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf new file mode 100644 index 000000000000..01f66e65c8bb --- /dev/null +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf @@ -0,0 +1,22 @@ +# 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. + +# This file is specific to the QEMU-emulated microTVM board. + +# For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_TIMER_RANDOM_GENERATOR=y diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack-riscv32/qemu-system-riscv32 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 similarity index 100% rename from apps/microtvm/zephyr/demo_runtime/qemu-hack-riscv32/qemu-system-riscv32 rename to apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack-riscv64/qemu-system-riscv64 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 similarity index 100% rename from apps/microtvm/zephyr/demo_runtime/qemu-hack-riscv64/qemu-system-riscv64 rename to apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 From ff3e99748df9ca6d327e9ef1f393cb4eaa60f50b Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 01:10:10 -0700 Subject: [PATCH 05/37] change target platforms --- python/tvm/target/target.py | 2 -- tests/micro/zephyr/conftest.py | 12 +++++++++++- tests/micro/zephyr/test_zephyr.py | 16 ++++------------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/python/tvm/target/target.py b/python/tvm/target/target.py index 4f502d439732..e3ef51158c5a 100644 --- a/python/tvm/target/target.py +++ b/python/tvm/target/target.py @@ -239,8 +239,6 @@ def intel_graphics(model="unknown", options=None): MICRO_SUPPORTED_MODELS = { "host": [], - "host_riscv32": [], - "host_riscv64": [], "stm32f746xx": ["-mcpu=cortex-m7", "-march=armv7e-m"], "nrf5340dk": ["-mcpu=cortex-m33"], } diff --git a/tests/micro/zephyr/conftest.py b/tests/micro/zephyr/conftest.py index e8ce443adfaf..eb9eb2928217 100644 --- a/tests/micro/zephyr/conftest.py +++ b/tests/micro/zephyr/conftest.py @@ -19,11 +19,21 @@ import tvm.target.target +# The models that should pass this configuration. Maps a short, identifying platform string to +# (model, zephyr_board). +PLATFORMS = { + "host": ("host", "qemu_x86"), + "host_riscv32": ("host", "qemu_riscv32"), + "host_riscv64": ("host", "qemu_riscv64"), + "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), + "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), +} + def pytest_addoption(parser): parser.addoption( "--microtvm-platforms", default="host", - choices=tvm.target.target.MICRO_SUPPORTED_MODELS.keys(), + choices=PLATFORMS.keys(), help=( "Specify a comma-separated list of test models (i.e. as passed to tvm.target.micro()) " "for microTVM tests." diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index f88e886d6dfa..1cb7c6575e2e 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -40,6 +40,8 @@ from tvm.relay.expr_functor import ExprMutator from tvm.relay.op.annotation import compiler_begin, compiler_end +import conftest + # If set, build the uTVM binary from scratch on each test. # Otherwise, reuses the build from the previous test run. BUILD = True @@ -51,6 +53,8 @@ _LOG = logging.getLogger(__name__) +PLATFORMS = conftest.PLATFORMS + def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): target = tvm.target.target.micro(model) with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): @@ -123,18 +127,6 @@ def _make_add_sess(model, zephyr_board, west_cmd): sched = tvm.te.create_schedule(C.op) return _make_sess_from_op(model, zephyr_board, west_cmd, "add", sched, [A, B, C]) - -# The models that should pass this configuration. Maps a short, identifying platform string to -# (model, zephyr_board). -PLATFORMS = { - "host": ("host", "qemu_x86"), - "host-riscv32": ("host_riscv32", "qemu_riscv32"), - "host-riscv64": ("host_riscv64", "qemu_riscv64"), - "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), - "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), -} - - # The same test code can be executed on both the QEMU simulation and on real hardware. def test_compile_runtime(platform, west_cmd): """Test compiling the on-device runtime.""" From 3876fae9cc5813dee9c5b16d8453756222076cae Mon Sep 17 00:00:00 2001 From: Andrew Reusch Date: Thu, 25 Mar 2021 17:53:33 -0700 Subject: [PATCH 06/37] fix merge --- apps/microtvm/pyproject.toml | 1 + .../demo_runtime/qemu-hack/qemu-system-i386 | 4 ++ apps/microtvm/zephyr/demo_runtime/src/main.c | 7 ++- python/tvm/micro/contrib/zephyr.py | 55 ++++++++++++++++++- tests/micro/zephyr/test_zephyr.py | 30 +++++++++- 5 files changed, 91 insertions(+), 6 deletions(-) diff --git a/apps/microtvm/pyproject.toml b/apps/microtvm/pyproject.toml index 8bfae0a157cd..20edf6553d7f 100644 --- a/apps/microtvm/pyproject.toml +++ b/apps/microtvm/pyproject.toml @@ -111,6 +111,7 @@ tensorflow-estimator = {version = "^2.1", optional = true} # TFLite frontend tflite = {version = "2.1.0", optional = true} wheel = "*" +colorama = "^0.4.4" [tool.poetry.extras] diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 index a0bf0f2c4dee..eb4e12c4f2f8 100755 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 @@ -30,4 +30,8 @@ while [ "$#" -gt 0 ]; do shift done +if [ "${TVM_QEMU_DEBUG}" != "" ]; then + ARGS=( "${ARGS[@]}" -s -S ) +fi + "${ARGS[@]}" diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index c5c596c2f162..7569aa32a9f6 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -99,10 +99,13 @@ size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const // Called by TVM when an internal invariant is violated, and execution cannot continue. void TVMPlatformAbort(tvm_crt_error_t error) { + // UtvmErrorReport(g_error); TVMLogf("TVMPlatformAbort: %x", error); sys_reboot(SYS_REBOOT_COLD); - for (;;) - ; +#ifdef CONFIG_LED + gpio_pin_set(led0_pin, LED0_PIN, 1); +#endif + for (;;) ; } // Called by TVM to generate random data. diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index 76c23c72ff73..91fdae8ff58d 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -18,6 +18,7 @@ """Defines a compiler integration that uses an externally-supplied Zephyr project.""" import collections +import copy import logging import multiprocessing import os @@ -428,12 +429,29 @@ def _get_device_args(self, cmake_entries): f"runner {flash_runner}" ) + def _zephyr_transport(self, micro_binary): + qemu_debugger = None + if self._debug_rpc_session: + qemu_debugger = debugger.RpcDebugger( + self._debug_rpc_session, + debugger.DebuggerFactory( + QemuGdbDebugger, + ( + micro_binary.abspath(micro_binary.debug_files[0]), + ), + {}, + ), + ) + + return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0, debugger=qemu_debugger) + + def flash(self, micro_binary): cmake_entries = read_cmake_cache( micro_binary.abspath(micro_binary.labelled_files["cmake_cache"][0]) ) if "qemu" in cmake_entries["BOARD"]: - return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0) + return self._zephyr_transport(micro_binary) build_dir = os.path.dirname( micro_binary.abspath(micro_binary.labelled_files["cmake_cache"][0]) @@ -531,6 +549,19 @@ def transport(self, micro_binary): serial_transport, ) + +class QemuGdbDebugger(debugger.GdbDebugger): + + def __init__(self, elf_file): + super(QemuGdbDebugger, self).__init__() + self._elf_file = elf_file + + def popen_kwargs(self): + return { + "args": ["gdb", "-ex", "target remote :1234", "-ex", f"file {self._elf_file}"], + } + + class QemuStartupFailureError(Exception): """Raised when the qemu pipe is not present within startup_timeout_sec.""" @@ -577,12 +608,13 @@ def __init__(self, base_dir, startup_timeout_sec=5.0, debugger=None, **kwargs): self.proc = None self.fd_transport = None self.pipe_dir = None + self.debugger = debugger def timeouts(self): return TransportTimeouts( session_start_retry_timeout_sec=2.0, session_start_timeout_sec=self.startup_timeout_sec, - session_established_timeout_sec=5.0, + session_established_timeout_sec=5.0 if self.debugger is None else 0, ) def open(self): @@ -604,13 +636,27 @@ def open(self): # os.mknod(log_path) # with open(log_path, "w") as f_log: + if self.debugger is not None: + if 'env' in self.kwargs: + self.kwargs["env"] = copy.copy(self.kwargs["env"]) + else: + self.kwargs["env"] = copy.copy(os.environ) + + self.kwargs["env"]["TVM_QEMU_DEBUG"] = "1" + self.proc = subprocess.Popen( - ["make", "run", f"QEMU_PIPE={self.pipe}"], + ["make", + "run", + f"QEMU_PIPE={self.pipe}"], cwd=self.base_dir, **self.kwargs, # stdout=f_log, # stderr=f_log ) + print('START DEBUG', self.debugger) + if self.debugger is not None: + self.debugger.start() + # NOTE: although each pipe is unidirectional, open both as RDWR to work around a select # limitation on linux. Without this, non-blocking I/O can't use timeouts because named # FIFO are always considered ready to read when no one has opened them for writing. @@ -625,6 +671,9 @@ def open(self): self.fd_transport.open() def close(self): + if self.debugger is not None: + self.debugger.stop() + if self.fd_transport is not None: self.fd_transport.child_transport.write_monitor_quit() self.proc.wait() diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index 1cb7c6575e2e..ab8bd9e34098 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -49,7 +49,7 @@ # If set, enable a debug session while the test is running. # Before running the test, in a separate shell, you should run: # python -m tvm.exec.microtvm_debug_shell -DEBUG = False +DEBUG = True _LOG = logging.getLogger(__name__) @@ -390,6 +390,34 @@ def test_byoc_utvm(platform, west_cmd): west_cmd=west_cmd, ) +def test_error_reporting(platform, west_cmd): + # Construct Relay program. + x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype)) + xx = relay.multiply(x, x) + z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) + func = relay.Function([x], z) + + target = tvm.target.target.micro(model) + with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): + graph, mod, params = tvm.relay.build(func, target=target) + + noinit_flag = "" + extra_opts = {"bin_opts": {"cflags": ["-DTEST_ERROR_MODULE"], "ldflags": noinit_flag} + # , + # "generated_lib_opts": {"cflags": noinit_flag, "ldflags": noinit_flag} + } + + with _make_session(model, target, zephyr_board, west_cmd, mod, extra_opts) as session: + graph_mod = tvm.micro.create_local_graph_runtime( + graph, session.get_system_lib(), session.context + ) + return + # graph_mod.set_input(**params) + # x_in = np.random.randint(10, size=shape[0], dtype=dtype) + # graph_mod.run(x=x_in) + # result = graph_mod.get_output(0).asnumpy() + # tvm.testing.assert_allclose(graph_mod.get_input(0).asnumpy(), x_in) + # tvm.testing.assert_allclose(result, x_in * x_in + 1) if __name__ == "__main__": sys.exit(pytest.main([os.path.dirname(__file__)] + sys.argv[1:])) From 25d3135873e32dff49848095c17f3f53c7c2e41b Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 15:14:16 -0700 Subject: [PATCH 07/37] debugging issue with zephyr 2.5 --- .../zephyr/demo_runtime/boards/qemu_x86.conf | 1 - apps/microtvm/zephyr/demo_runtime/prj.conf | 1 - .../demo_runtime/qemu-hack/qemu-system-riscv32 | 4 ++++ python/tvm/micro/contrib/zephyr.py | 14 +------------- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf index e0e4ae2fb2d3..01f66e65c8bb 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf @@ -20,4 +20,3 @@ # For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y - diff --git a/apps/microtvm/zephyr/demo_runtime/prj.conf b/apps/microtvm/zephyr/demo_runtime/prj.conf index bf2b330e35a6..5f4d7a0689dc 100644 --- a/apps/microtvm/zephyr/demo_runtime/prj.conf +++ b/apps/microtvm/zephyr/demo_runtime/prj.conf @@ -33,4 +33,3 @@ CONFIG_FPU=y # For TVMPlatformAbort(). CONFIG_REBOOT=y - diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 index a0bf0f2c4dee..eb4e12c4f2f8 100755 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 @@ -30,4 +30,8 @@ while [ "$#" -gt 0 ]; do shift done +if [ "${TVM_QEMU_DEBUG}" != "" ]; then + ARGS=( "${ARGS[@]}" -s -S ) +fi + "${ARGS[@]}" diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index 91fdae8ff58d..b43bcd003a82 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -619,23 +619,13 @@ def timeouts(self): def open(self): self.pipe_dir = tempfile.mkdtemp() - # self.pipe_dir = "/tmp/test123" self.pipe = os.path.join(self.pipe_dir, "fifo") self.write_pipe = os.path.join(self.pipe_dir, "fifo.in") self.read_pipe = os.path.join(self.pipe_dir, "fifo.out") - # if os.path.exists(self.read_pipe): - # os.remove(self.read_pipe) - # if os.path.exists(self.write_pipe): - # os.remove(self.write_pipe) - os.mkfifo(self.write_pipe) os.mkfifo(self.read_pipe) - # log_path = "/Users/mhessar/work/tvm/proc.log" - # if not os.path.exists(log_path): - # os.mknod(log_path) - # with open(log_path, "w") as f_log: if self.debugger is not None: if 'env' in self.kwargs: self.kwargs["env"] = copy.copy(self.kwargs["env"]) @@ -650,8 +640,6 @@ def open(self): f"QEMU_PIPE={self.pipe}"], cwd=self.base_dir, **self.kwargs, - # stdout=f_log, - # stderr=f_log ) print('START DEBUG', self.debugger) if self.debugger is not None: @@ -684,7 +672,7 @@ def close(self): self.proc = None if self.pipe_dir is not None: - # shutil.rmtree(self.pipe_dir) + shutil.rmtree(self.pipe_dir) self.pipe_dir = None def read(self, n, timeout_sec): From 91ca32159f5cb4dc04a6cd70cce45c384de4b096 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 17:54:55 -0700 Subject: [PATCH 08/37] cleanup --- tests/micro/zephyr/test_zephyr.py | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index ab8bd9e34098..b7132d9a63b6 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -49,7 +49,7 @@ # If set, enable a debug session while the test is running. # Before running the test, in a separate shell, you should run: # python -m tvm.exec.microtvm_debug_shell -DEBUG = True +DEBUG = False _LOG = logging.getLogger(__name__) @@ -390,34 +390,5 @@ def test_byoc_utvm(platform, west_cmd): west_cmd=west_cmd, ) -def test_error_reporting(platform, west_cmd): - # Construct Relay program. - x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype)) - xx = relay.multiply(x, x) - z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) - func = relay.Function([x], z) - - target = tvm.target.target.micro(model) - with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): - graph, mod, params = tvm.relay.build(func, target=target) - - noinit_flag = "" - extra_opts = {"bin_opts": {"cflags": ["-DTEST_ERROR_MODULE"], "ldflags": noinit_flag} - # , - # "generated_lib_opts": {"cflags": noinit_flag, "ldflags": noinit_flag} - } - - with _make_session(model, target, zephyr_board, west_cmd, mod, extra_opts) as session: - graph_mod = tvm.micro.create_local_graph_runtime( - graph, session.get_system_lib(), session.context - ) - return - # graph_mod.set_input(**params) - # x_in = np.random.randint(10, size=shape[0], dtype=dtype) - # graph_mod.run(x=x_in) - # result = graph_mod.get_output(0).asnumpy() - # tvm.testing.assert_allclose(graph_mod.get_input(0).asnumpy(), x_in) - # tvm.testing.assert_allclose(result, x_in * x_in + 1) - if __name__ == "__main__": sys.exit(pytest.main([os.path.dirname(__file__)] + sys.argv[1:])) From 8a92af8591358607598ca6b8a766d3031e32fdc3 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Wed, 24 Mar 2021 21:58:20 -0700 Subject: [PATCH 09/37] working on qemu --- tests/micro/zephyr/test_zephyr.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index 1db3d505f490..b400a89da084 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -129,6 +129,7 @@ def _make_add_sess(model, zephyr_board, west_cmd): "host": ("host", "qemu_x86"), "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), + "riscv32_host": ("riscv32_host", "qemu_riscv32"), } @@ -193,7 +194,8 @@ def test_relay(platform, west_cmd): xx = relay.multiply(x, x) z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) func = relay.Function([x], z) - + import pdb + target = tvm.target.target.micro(model) with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): graph, mod, params = tvm.relay.build(func, target=target) From 223bc04f05cfde8f9897b93cd22360806c6c06c4 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Sun, 28 Mar 2021 21:18:14 -0700 Subject: [PATCH 10/37] debugging --- .../zephyr/demo_runtime/CMakeLists.txt | 3 +++ apps/microtvm/zephyr/demo_runtime/src/main.c | 19 +++++++++---------- python/tvm/micro/contrib/zephyr.py | 19 ++++++++++++++++--- python/tvm/target/target.py | 9 ++++++++- src/runtime/micro/micro_session.cc | 5 +++++ tests/micro/zephyr/test_zephyr.py | 10 ++++++---- 6 files changed, 47 insertions(+), 18 deletions(-) diff --git a/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt b/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt index a99d5edb07e6..9ee5ff64f639 100644 --- a/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt +++ b/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt @@ -3,6 +3,9 @@ cmake_minimum_required(VERSION 3.13.1) set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack") +#set(ENV{QEMU_BIN_PATH} "/home/vagrant/zephyr-sdk/sysroots/x86_64-pokysdk-linux/usr/bin/") +#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv32") +#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv64") set(QEMU_PIPE "\${QEMU_PIPE}") # QEMU_PIPE is set by the calling TVM instance. diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index e2aa59af7ad9..f44ba40aa4ad 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -99,14 +99,11 @@ size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const // Called by TVM when an internal invariant is violated, and execution cannot continue. void TVMPlatformAbort(tvm_crt_error_t error) { - sys_reboot(SYS_REBOOT_COLD); -#ifdef CONFIG_LED - gpio_pin_set(led0_pin, LED0_PIN, 1); -#endif + TVMLogf("TVMPlatformAbort: %x", error); + sys_reboot(SYS_REBOOT_WARM); for (;;) ; } - // Called by TVM to generate random data. tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) { uint32_t random; // one unit of random data. @@ -127,12 +124,12 @@ tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) { return kTvmErrorNoError; } -// Memory pool for use by TVMPlatformMemoryAllocate. -K_MEM_POOL_DEFINE(tvm_memory_pool, 64, 1024, 216, 4); +// K_MEM_POOL_DEFINE(tvm_memory_pool, 64, 1024, 120, 4); +K_HEAP_DEFINE(tvm_memory_pool, 120*1024); -// Called by TVM to allocate memory. -tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, void** out_ptr) { - *out_ptr = k_mem_pool_malloc(&tvm_memory_pool, num_bytes); +tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLContext ctx, void** out_ptr) { + // *out_ptr = k_mem_pool_malloc(&tvm_memory_pool, num_bytes); + *out_ptr = k_heap_alloc(&tvm_memory_pool, num_bytes, K_NO_WAIT); return (*out_ptr == NULL) ? kTvmErrorPlatformNoMemory : kTvmErrorNoError; } @@ -295,6 +292,8 @@ void main(void) { while (true) { int bytes_read = uart_rx_buf_read(&uart_rx_rbuf, main_rx_buf, sizeof(main_rx_buf)); if (bytes_read > 0) { + // for (int i_read=0; i_read 0) { diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index 104d955835a1..76c23c72ff73 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -531,7 +531,6 @@ def transport(self, micro_binary): serial_transport, ) - class QemuStartupFailureError(Exception): """Raised when the qemu pipe is not present within startup_timeout_sec.""" @@ -571,7 +570,7 @@ def write(self, data, timeout_sec): class ZephyrQemuTransport(Transport): """The user-facing Zephyr QEMU transport class.""" - def __init__(self, base_dir, startup_timeout_sec=5.0, **kwargs): + def __init__(self, base_dir, startup_timeout_sec=5.0, debugger=None, **kwargs): self.base_dir = base_dir self.startup_timeout_sec = startup_timeout_sec self.kwargs = kwargs @@ -588,15 +587,29 @@ def timeouts(self): def open(self): self.pipe_dir = tempfile.mkdtemp() + # self.pipe_dir = "/tmp/test123" self.pipe = os.path.join(self.pipe_dir, "fifo") self.write_pipe = os.path.join(self.pipe_dir, "fifo.in") self.read_pipe = os.path.join(self.pipe_dir, "fifo.out") + + # if os.path.exists(self.read_pipe): + # os.remove(self.read_pipe) + # if os.path.exists(self.write_pipe): + # os.remove(self.write_pipe) + os.mkfifo(self.write_pipe) os.mkfifo(self.read_pipe) + # log_path = "/Users/mhessar/work/tvm/proc.log" + # if not os.path.exists(log_path): + # os.mknod(log_path) + + # with open(log_path, "w") as f_log: self.proc = subprocess.Popen( ["make", "run", f"QEMU_PIPE={self.pipe}"], cwd=self.base_dir, **self.kwargs, + # stdout=f_log, + # stderr=f_log ) # NOTE: although each pipe is unidirectional, open both as RDWR to work around a select # limitation on linux. Without this, non-blocking I/O can't use timeouts because named @@ -622,7 +635,7 @@ def close(self): self.proc = None if self.pipe_dir is not None: - shutil.rmtree(self.pipe_dir) + # shutil.rmtree(self.pipe_dir) self.pipe_dir = None def read(self, n, timeout_sec): diff --git a/python/tvm/target/target.py b/python/tvm/target/target.py index 6d0a0635221e..77ec7e36ba58 100644 --- a/python/tvm/target/target.py +++ b/python/tvm/target/target.py @@ -289,7 +289,14 @@ def micro(model="unknown", options=None): options : str or list of str Additional options """ - if model not in MICRO_SUPPORTED_MODELS: + trans_table = { + "host": [], + "stm32f746xx": ["-mcpu=cortex-m7", "-march=armv7e-m"], + "nrf5340dk": ["-mcpu=cortex-m33"], + "host_riscv32": [], + "host_riscv64": [], + } + if model not in trans_table: raise ValueError(f"Model {model} not supported by tvm.target.micro.") opts = _merge_opts( MICRO_SUPPORTED_MODELS[model] + ["-runtime=c", "--system-lib", f"-model={model}"], diff --git a/src/runtime/micro/micro_session.cc b/src/runtime/micro/micro_session.cc index cd916d46971d..76d21cb4c539 100644 --- a/src/runtime/micro/micro_session.cc +++ b/src/runtime/micro/micro_session.cc @@ -331,13 +331,16 @@ class MicroTransportChannel : public RPCChannel { size_t message_size_bytes; switch (message_type) { case MessageType::kStartSessionInit: + LOG(ERROR) << "kStartSessionInit"; break; case MessageType::kStartSessionReply: + LOG(ERROR) << "kStartSessionReply"; state_ = State::kSessionEstablished; break; case MessageType::kTerminateSession: + LOG(ERROR) << "kTerminateSession"; if (state_ == State::kReset) { state_ = State::kSessionTerminated; } else if (state_ == State::kSessionTerminated) { @@ -349,6 +352,7 @@ class MicroTransportChannel : public RPCChannel { break; case MessageType::kLog: + LOG(ERROR) << "kLog"; uint8_t message[1024]; message_size_bytes = buf->ReadAvailable(); if (message_size_bytes == 0) { @@ -366,6 +370,7 @@ class MicroTransportChannel : public RPCChannel { return; case MessageType::kNormal: + LOG(ERROR) << "kNormal"; did_receive_message_ = true; message_buffer_ = buf; break; diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index b400a89da084..7750f211e838 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -23,6 +23,7 @@ import os import subprocess import sys +import logging import pytest import numpy as np @@ -48,6 +49,7 @@ # python -m tvm.exec.microtvm_debug_shell DEBUG = False +_LOG = logging.getLogger(__name__) def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): target = tvm.target.target.micro(model) @@ -59,10 +61,10 @@ def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): def _make_session(model, target, zephyr_board, west_cmd, mod): - test_name = f"{os.path.splitext(os.path.abspath(__file__))[0]}-{model}" + test_name = f"{os.path.splitext(os.path.abspath(__file__))[0]}_{model}" prev_build = f"{test_name}-last-build.micro-binary" workspace_root = ( - f'{test_name}-workspace/{datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")}' + f'{test_name}_workspace/{datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")}' ) workspace_parent = os.path.dirname(workspace_root) if not os.path.exists(workspace_parent): @@ -127,9 +129,10 @@ def _make_add_sess(model, zephyr_board, west_cmd): # (model, zephyr_board). PLATFORMS = { "host": ("host", "qemu_x86"), + "host-riscv32": ("host_riscv32", "qemu_riscv32"), + "host-riscv64": ("host_riscv64", "qemu_riscv64"), "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), - "riscv32_host": ("riscv32_host", "qemu_riscv32"), } @@ -194,7 +197,6 @@ def test_relay(platform, west_cmd): xx = relay.multiply(x, x) z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) func = relay.Function([x], z) - import pdb target = tvm.target.target.micro(model) with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): From a17f168e2f9ac16b3aa0012540deac9598aa8d6c Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Sun, 28 Mar 2021 21:19:07 -0700 Subject: [PATCH 11/37] riscv hacks --- .../qemu-hack-riscv32/qemu-system-riscv32 | 33 +++++++++++++++++ .../qemu-hack-riscv64/qemu-system-riscv64 | 37 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 create mode 100755 tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 diff --git a/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 new file mode 100755 index 000000000000..a0bf0f2c4dee --- /dev/null +++ b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 @@ -0,0 +1,33 @@ +#!/bin/bash -e +# 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. + +# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to +# work given the way we've configured docker (the underlying filesystem doesn't suppor the +# file locking it needs to). This script strips any -pidfile option, then invokes qemu. + +ARGS=( "$(basename $0)" ) +while [ "$#" -gt 0 ]; do + if [ "$1" == "-pidfile" ]; then + shift + else + ARGS=( "${ARGS[@]}" "$1" ) + fi + shift +done + +"${ARGS[@]}" diff --git a/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 new file mode 100755 index 000000000000..eb4e12c4f2f8 --- /dev/null +++ b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 @@ -0,0 +1,37 @@ +#!/bin/bash -e +# 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. + +# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to +# work given the way we've configured docker (the underlying filesystem doesn't suppor the +# file locking it needs to). This script strips any -pidfile option, then invokes qemu. + +ARGS=( "$(basename $0)" ) +while [ "$#" -gt 0 ]; do + if [ "$1" == "-pidfile" ]; then + shift + else + ARGS=( "${ARGS[@]}" "$1" ) + fi + shift +done + +if [ "${TVM_QEMU_DEBUG}" != "" ]; then + ARGS=( "${ARGS[@]}" -s -S ) +fi + +"${ARGS[@]}" From 5597bce04bad7d92f942656939e3f76a520d35f7 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 00:11:01 -0700 Subject: [PATCH 12/37] config added --- .../zephyr/demo_runtime/CMakeLists.txt | 3 -- .../demo_runtime/boards/qemu_riscv32.conf | 22 +++++++++++++ .../demo_runtime/boards/qemu_riscv64.conf | 22 +++++++++++++ .../qemu-hack/qemu-system-riscv32 | 33 +++++++++++++++++++ .../qemu-hack}/qemu-system-riscv64 | 0 5 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf create mode 100644 apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf create mode 100755 apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 rename {tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64 => apps/microtvm/zephyr/demo_runtime/qemu-hack}/qemu-system-riscv64 (100%) diff --git a/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt b/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt index 9ee5ff64f639..a99d5edb07e6 100644 --- a/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt +++ b/apps/microtvm/zephyr/demo_runtime/CMakeLists.txt @@ -3,9 +3,6 @@ cmake_minimum_required(VERSION 3.13.1) set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack") -#set(ENV{QEMU_BIN_PATH} "/home/vagrant/zephyr-sdk/sysroots/x86_64-pokysdk-linux/usr/bin/") -#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv32") -#set(ENV{QEMU_BIN_PATH} "${CMAKE_SOURCE_DIR}/qemu-hack-riscv64") set(QEMU_PIPE "\${QEMU_PIPE}") # QEMU_PIPE is set by the calling TVM instance. diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf new file mode 100644 index 000000000000..01f66e65c8bb --- /dev/null +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf @@ -0,0 +1,22 @@ +# 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. + +# This file is specific to the QEMU-emulated microTVM board. + +# For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_TIMER_RANDOM_GENERATOR=y diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf new file mode 100644 index 000000000000..01f66e65c8bb --- /dev/null +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf @@ -0,0 +1,22 @@ +# 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. + +# This file is specific to the QEMU-emulated microTVM board. + +# For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_TIMER_RANDOM_GENERATOR=y diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 new file mode 100755 index 000000000000..a0bf0f2c4dee --- /dev/null +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 @@ -0,0 +1,33 @@ +#!/bin/bash -e +# 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. + +# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to +# work given the way we've configured docker (the underlying filesystem doesn't suppor the +# file locking it needs to). This script strips any -pidfile option, then invokes qemu. + +ARGS=( "$(basename $0)" ) +while [ "$#" -gt 0 ]; do + if [ "$1" == "-pidfile" ]; then + shift + else + ARGS=( "${ARGS[@]}" "$1" ) + fi + shift +done + +"${ARGS[@]}" diff --git a/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 similarity index 100% rename from tests/micro/qemu/zephyr-runtime/qemu-hack-riscv64/qemu-system-riscv64 rename to apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 From d477573b8179d45e3d79d1155102ee6d981d13c1 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 01:10:10 -0700 Subject: [PATCH 13/37] change target platforms --- tests/micro/zephyr/conftest.py | 12 +++++++++++- tests/micro/zephyr/test_zephyr.py | 16 ++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/micro/zephyr/conftest.py b/tests/micro/zephyr/conftest.py index e8ce443adfaf..eb9eb2928217 100644 --- a/tests/micro/zephyr/conftest.py +++ b/tests/micro/zephyr/conftest.py @@ -19,11 +19,21 @@ import tvm.target.target +# The models that should pass this configuration. Maps a short, identifying platform string to +# (model, zephyr_board). +PLATFORMS = { + "host": ("host", "qemu_x86"), + "host_riscv32": ("host", "qemu_riscv32"), + "host_riscv64": ("host", "qemu_riscv64"), + "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), + "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), +} + def pytest_addoption(parser): parser.addoption( "--microtvm-platforms", default="host", - choices=tvm.target.target.MICRO_SUPPORTED_MODELS.keys(), + choices=PLATFORMS.keys(), help=( "Specify a comma-separated list of test models (i.e. as passed to tvm.target.micro()) " "for microTVM tests." diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index 7750f211e838..bbc342ac2fd4 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -40,6 +40,8 @@ from tvm.relay.expr_functor import ExprMutator from tvm.relay.op.annotation import compiler_begin, compiler_end +import conftest + # If set, build the uTVM binary from scratch on each test. # Otherwise, reuses the build from the previous test run. BUILD = True @@ -51,6 +53,8 @@ _LOG = logging.getLogger(__name__) +PLATFORMS = conftest.PLATFORMS + def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): target = tvm.target.target.micro(model) target = tvm.target.Target(target=target, host=target) @@ -124,18 +128,6 @@ def _make_add_sess(model, zephyr_board, west_cmd): sched = tvm.te.create_schedule(C.op) return _make_sess_from_op(model, zephyr_board, west_cmd, "add", sched, [A, B, C]) - -# The models that should pass this configuration. Maps a short, identifying platform string to -# (model, zephyr_board). -PLATFORMS = { - "host": ("host", "qemu_x86"), - "host-riscv32": ("host_riscv32", "qemu_riscv32"), - "host-riscv64": ("host_riscv64", "qemu_riscv64"), - "stm32f746xx": ("stm32f746xx", "nucleo_f746zg"), - "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), -} - - # The same test code can be executed on both the QEMU simulation and on real hardware. def test_compile_runtime(platform, west_cmd): """Test compiling the on-device runtime.""" From ce6fc59772e29e40436925f28b92c7f087e47bd1 Mon Sep 17 00:00:00 2001 From: Andrew Reusch Date: Thu, 25 Mar 2021 17:53:33 -0700 Subject: [PATCH 14/37] fix merge --- apps/microtvm/pyproject.toml | 1 + .../demo_runtime/qemu-hack/qemu-system-i386 | 4 ++ apps/microtvm/zephyr/demo_runtime/src/main.c | 9 ++- python/tvm/micro/contrib/zephyr.py | 55 ++++++++++++++++++- tests/micro/zephyr/test_zephyr.py | 30 +++++++++- 5 files changed, 92 insertions(+), 7 deletions(-) diff --git a/apps/microtvm/pyproject.toml b/apps/microtvm/pyproject.toml index 8bfae0a157cd..20edf6553d7f 100644 --- a/apps/microtvm/pyproject.toml +++ b/apps/microtvm/pyproject.toml @@ -111,6 +111,7 @@ tensorflow-estimator = {version = "^2.1", optional = true} # TFLite frontend tflite = {version = "2.1.0", optional = true} wheel = "*" +colorama = "^0.4.4" [tool.poetry.extras] diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 index a0bf0f2c4dee..eb4e12c4f2f8 100755 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 @@ -30,4 +30,8 @@ while [ "$#" -gt 0 ]; do shift done +if [ "${TVM_QEMU_DEBUG}" != "" ]; then + ARGS=( "${ARGS[@]}" -s -S ) +fi + "${ARGS[@]}" diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index f44ba40aa4ad..944c48048339 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -99,10 +99,13 @@ size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const // Called by TVM when an internal invariant is violated, and execution cannot continue. void TVMPlatformAbort(tvm_crt_error_t error) { + // UtvmErrorReport(g_error); TVMLogf("TVMPlatformAbort: %x", error); - sys_reboot(SYS_REBOOT_WARM); - for (;;) - ; + sys_reboot(SYS_REBOOT_COLD); +#ifdef CONFIG_LED + gpio_pin_set(led0_pin, LED0_PIN, 1); +#endif + for (;;) ; } // Called by TVM to generate random data. tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) { diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index 76c23c72ff73..91fdae8ff58d 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -18,6 +18,7 @@ """Defines a compiler integration that uses an externally-supplied Zephyr project.""" import collections +import copy import logging import multiprocessing import os @@ -428,12 +429,29 @@ def _get_device_args(self, cmake_entries): f"runner {flash_runner}" ) + def _zephyr_transport(self, micro_binary): + qemu_debugger = None + if self._debug_rpc_session: + qemu_debugger = debugger.RpcDebugger( + self._debug_rpc_session, + debugger.DebuggerFactory( + QemuGdbDebugger, + ( + micro_binary.abspath(micro_binary.debug_files[0]), + ), + {}, + ), + ) + + return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0, debugger=qemu_debugger) + + def flash(self, micro_binary): cmake_entries = read_cmake_cache( micro_binary.abspath(micro_binary.labelled_files["cmake_cache"][0]) ) if "qemu" in cmake_entries["BOARD"]: - return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0) + return self._zephyr_transport(micro_binary) build_dir = os.path.dirname( micro_binary.abspath(micro_binary.labelled_files["cmake_cache"][0]) @@ -531,6 +549,19 @@ def transport(self, micro_binary): serial_transport, ) + +class QemuGdbDebugger(debugger.GdbDebugger): + + def __init__(self, elf_file): + super(QemuGdbDebugger, self).__init__() + self._elf_file = elf_file + + def popen_kwargs(self): + return { + "args": ["gdb", "-ex", "target remote :1234", "-ex", f"file {self._elf_file}"], + } + + class QemuStartupFailureError(Exception): """Raised when the qemu pipe is not present within startup_timeout_sec.""" @@ -577,12 +608,13 @@ def __init__(self, base_dir, startup_timeout_sec=5.0, debugger=None, **kwargs): self.proc = None self.fd_transport = None self.pipe_dir = None + self.debugger = debugger def timeouts(self): return TransportTimeouts( session_start_retry_timeout_sec=2.0, session_start_timeout_sec=self.startup_timeout_sec, - session_established_timeout_sec=5.0, + session_established_timeout_sec=5.0 if self.debugger is None else 0, ) def open(self): @@ -604,13 +636,27 @@ def open(self): # os.mknod(log_path) # with open(log_path, "w") as f_log: + if self.debugger is not None: + if 'env' in self.kwargs: + self.kwargs["env"] = copy.copy(self.kwargs["env"]) + else: + self.kwargs["env"] = copy.copy(os.environ) + + self.kwargs["env"]["TVM_QEMU_DEBUG"] = "1" + self.proc = subprocess.Popen( - ["make", "run", f"QEMU_PIPE={self.pipe}"], + ["make", + "run", + f"QEMU_PIPE={self.pipe}"], cwd=self.base_dir, **self.kwargs, # stdout=f_log, # stderr=f_log ) + print('START DEBUG', self.debugger) + if self.debugger is not None: + self.debugger.start() + # NOTE: although each pipe is unidirectional, open both as RDWR to work around a select # limitation on linux. Without this, non-blocking I/O can't use timeouts because named # FIFO are always considered ready to read when no one has opened them for writing. @@ -625,6 +671,9 @@ def open(self): self.fd_transport.open() def close(self): + if self.debugger is not None: + self.debugger.stop() + if self.fd_transport is not None: self.fd_transport.child_transport.write_monitor_quit() self.proc.wait() diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index bbc342ac2fd4..9c4f1d015de1 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -49,7 +49,7 @@ # If set, enable a debug session while the test is running. # Before running the test, in a separate shell, you should run: # python -m tvm.exec.microtvm_debug_shell -DEBUG = False +DEBUG = True _LOG = logging.getLogger(__name__) @@ -391,6 +391,34 @@ def test_byoc_utvm(platform, west_cmd): west_cmd=west_cmd, ) +def test_error_reporting(platform, west_cmd): + # Construct Relay program. + x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype)) + xx = relay.multiply(x, x) + z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) + func = relay.Function([x], z) + + target = tvm.target.target.micro(model) + with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): + graph, mod, params = tvm.relay.build(func, target=target) + + noinit_flag = "" + extra_opts = {"bin_opts": {"cflags": ["-DTEST_ERROR_MODULE"], "ldflags": noinit_flag} + # , + # "generated_lib_opts": {"cflags": noinit_flag, "ldflags": noinit_flag} + } + + with _make_session(model, target, zephyr_board, west_cmd, mod, extra_opts) as session: + graph_mod = tvm.micro.create_local_graph_runtime( + graph, session.get_system_lib(), session.context + ) + return + # graph_mod.set_input(**params) + # x_in = np.random.randint(10, size=shape[0], dtype=dtype) + # graph_mod.run(x=x_in) + # result = graph_mod.get_output(0).asnumpy() + # tvm.testing.assert_allclose(graph_mod.get_input(0).asnumpy(), x_in) + # tvm.testing.assert_allclose(result, x_in * x_in + 1) if __name__ == "__main__": sys.exit(pytest.main([os.path.dirname(__file__)] + sys.argv[1:])) From 52c851291a2aa35666407bd4611064e99b09896c Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 15:14:16 -0700 Subject: [PATCH 15/37] debugging issue with zephyr 2.5 --- .../zephyr/demo_runtime/boards/qemu_x86.conf | 1 - apps/microtvm/zephyr/demo_runtime/prj.conf | 1 - .../demo_runtime/qemu-hack/qemu-system-riscv32 | 4 ++++ python/tvm/micro/contrib/zephyr.py | 14 +------------- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf index e0e4ae2fb2d3..01f66e65c8bb 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf @@ -20,4 +20,3 @@ # For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y - diff --git a/apps/microtvm/zephyr/demo_runtime/prj.conf b/apps/microtvm/zephyr/demo_runtime/prj.conf index bf2b330e35a6..5f4d7a0689dc 100644 --- a/apps/microtvm/zephyr/demo_runtime/prj.conf +++ b/apps/microtvm/zephyr/demo_runtime/prj.conf @@ -33,4 +33,3 @@ CONFIG_FPU=y # For TVMPlatformAbort(). CONFIG_REBOOT=y - diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 index a0bf0f2c4dee..eb4e12c4f2f8 100755 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 @@ -30,4 +30,8 @@ while [ "$#" -gt 0 ]; do shift done +if [ "${TVM_QEMU_DEBUG}" != "" ]; then + ARGS=( "${ARGS[@]}" -s -S ) +fi + "${ARGS[@]}" diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index 91fdae8ff58d..b43bcd003a82 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -619,23 +619,13 @@ def timeouts(self): def open(self): self.pipe_dir = tempfile.mkdtemp() - # self.pipe_dir = "/tmp/test123" self.pipe = os.path.join(self.pipe_dir, "fifo") self.write_pipe = os.path.join(self.pipe_dir, "fifo.in") self.read_pipe = os.path.join(self.pipe_dir, "fifo.out") - # if os.path.exists(self.read_pipe): - # os.remove(self.read_pipe) - # if os.path.exists(self.write_pipe): - # os.remove(self.write_pipe) - os.mkfifo(self.write_pipe) os.mkfifo(self.read_pipe) - # log_path = "/Users/mhessar/work/tvm/proc.log" - # if not os.path.exists(log_path): - # os.mknod(log_path) - # with open(log_path, "w") as f_log: if self.debugger is not None: if 'env' in self.kwargs: self.kwargs["env"] = copy.copy(self.kwargs["env"]) @@ -650,8 +640,6 @@ def open(self): f"QEMU_PIPE={self.pipe}"], cwd=self.base_dir, **self.kwargs, - # stdout=f_log, - # stderr=f_log ) print('START DEBUG', self.debugger) if self.debugger is not None: @@ -684,7 +672,7 @@ def close(self): self.proc = None if self.pipe_dir is not None: - # shutil.rmtree(self.pipe_dir) + shutil.rmtree(self.pipe_dir) self.pipe_dir = None def read(self, n, timeout_sec): From 0d6eaa3861faa60389400c33b8d39e02cd2c4101 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 29 Mar 2021 17:54:55 -0700 Subject: [PATCH 16/37] cleanup --- tests/micro/zephyr/test_zephyr.py | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index 9c4f1d015de1..ee36da1b6f73 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -49,7 +49,7 @@ # If set, enable a debug session while the test is running. # Before running the test, in a separate shell, you should run: # python -m tvm.exec.microtvm_debug_shell -DEBUG = True +DEBUG = False _LOG = logging.getLogger(__name__) @@ -391,34 +391,5 @@ def test_byoc_utvm(platform, west_cmd): west_cmd=west_cmd, ) -def test_error_reporting(platform, west_cmd): - # Construct Relay program. - x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype)) - xx = relay.multiply(x, x) - z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) - func = relay.Function([x], z) - - target = tvm.target.target.micro(model) - with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): - graph, mod, params = tvm.relay.build(func, target=target) - - noinit_flag = "" - extra_opts = {"bin_opts": {"cflags": ["-DTEST_ERROR_MODULE"], "ldflags": noinit_flag} - # , - # "generated_lib_opts": {"cflags": noinit_flag, "ldflags": noinit_flag} - } - - with _make_session(model, target, zephyr_board, west_cmd, mod, extra_opts) as session: - graph_mod = tvm.micro.create_local_graph_runtime( - graph, session.get_system_lib(), session.context - ) - return - # graph_mod.set_input(**params) - # x_in = np.random.randint(10, size=shape[0], dtype=dtype) - # graph_mod.run(x=x_in) - # result = graph_mod.get_output(0).asnumpy() - # tvm.testing.assert_allclose(graph_mod.get_input(0).asnumpy(), x_in) - # tvm.testing.assert_allclose(result, x_in * x_in + 1) - if __name__ == "__main__": sys.exit(pytest.main([os.path.dirname(__file__)] + sys.argv[1:])) From ecbef7a7ef4bd5856735f7837119c0f492333619 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Fri, 2 Apr 2021 12:04:20 -0700 Subject: [PATCH 17/37] testing --- apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf index 01f66e65c8bb..08532c353d1c 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf @@ -20,3 +20,5 @@ # For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y + +CONFIG_MAIN_STACK_SIZE=1530 \ No newline at end of file From 2d6eeba28dbd48119ee924ca2675152be31207c9 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Fri, 2 Apr 2021 13:27:49 -0700 Subject: [PATCH 18/37] pass riscv64 --- apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf | 3 ++- apps/microtvm/zephyr/demo_runtime/src/main.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf index 08532c353d1c..2f85b7e0c72b 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf @@ -21,4 +21,5 @@ CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y -CONFIG_MAIN_STACK_SIZE=1530 \ No newline at end of file +# Default 512, for operations with large floating point data. +CONFIG_MAIN_STACK_SIZE=2048 diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index 944c48048339..4fc0ec4e60a9 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -138,7 +138,7 @@ tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLContext ctx, void* // Called by TVM to deallocate memory. tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) { - k_free(ptr); + k_heap_free(&tvm_memory_pool, ptr); return kTvmErrorNoError; } From 140048bc0542b06109dd7df189ebb887359f936f Mon Sep 17 00:00:00 2001 From: Andrew Reusch Date: Wed, 31 Mar 2021 19:38:17 -0700 Subject: [PATCH 19/37] fix merge --- .../demo_runtime/boards/qemu_riscv32.conf | 6 ++++ .../demo_runtime/qemu-hack/qemu-system-i386 | 1 + .../qemu-hack/qemu-system-riscv32 | 1 + .../qemu-hack/qemu-system-riscv64 | 1 + python/tvm/micro/contrib/zephyr.py | 9 +++-- python/tvm/target/target.py | 9 +---- .../qemu-hack-riscv32/qemu-system-riscv32 | 33 ------------------- 7 files changed, 16 insertions(+), 44 deletions(-) delete mode 100755 tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf index 01f66e65c8bb..13484073d28e 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf @@ -20,3 +20,9 @@ # For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y + +# Default 512, for operations with large floating point data. +CONFIG_MAIN_STACK_SIZE=2048 + +# For floating point operations. +CONFIG_FPU_SHARING=y diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 index eb4e12c4f2f8..a30605204d31 100755 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386 @@ -30,6 +30,7 @@ while [ "$#" -gt 0 ]; do shift done +# For debugging if [ "${TVM_QEMU_DEBUG}" != "" ]; then ARGS=( "${ARGS[@]}" -s -S ) fi diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 index eb4e12c4f2f8..a30605204d31 100755 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 @@ -30,6 +30,7 @@ while [ "$#" -gt 0 ]; do shift done +# For debugging if [ "${TVM_QEMU_DEBUG}" != "" ]; then ARGS=( "${ARGS[@]}" -s -S ) fi diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 index eb4e12c4f2f8..a30605204d31 100755 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 @@ -30,6 +30,7 @@ while [ "$#" -gt 0 ]; do shift done +# For debugging if [ "${TVM_QEMU_DEBUG}" != "" ]; then ARGS=( "${ARGS[@]}" -s -S ) fi diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index 8dbffb82bcbc..b2633b4da1de 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -22,6 +22,7 @@ import logging import multiprocessing import os +import pathlib import re import tempfile import textwrap @@ -445,7 +446,6 @@ def _zephyr_transport(self, micro_binary): return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0, debugger=qemu_debugger) - def flash(self, micro_binary): cmake_entries = read_cmake_cache( micro_binary.abspath(micro_binary.labelled_files["cmake_cache"][0]) @@ -557,8 +557,12 @@ def __init__(self, elf_file): self._elf_file = elf_file def popen_kwargs(self): + # expect self._elf file to follow the form .../zephyr/zephyr.elf + cmake_cache_path = ( + pathlib.Path(self._elf_file).parent.parent / "CMakeCache.txt") + cmake_cache = read_cmake_cache(cmake_cache_path) return { - "args": ["gdb", "-ex", "target remote :1234", "-ex", f"file {self._elf_file}"], + "args": [cmake_cache["CMAKE_GDB"], "-ex", "target remote localhost:1234", "-ex", f"file {self._elf_file}"], } @@ -625,7 +629,6 @@ def open(self): os.mkfifo(self.write_pipe) os.mkfifo(self.read_pipe) - if self.debugger is not None: if 'env' in self.kwargs: self.kwargs["env"] = copy.copy(self.kwargs["env"]) diff --git a/python/tvm/target/target.py b/python/tvm/target/target.py index 649bed1c9763..baf07602bde6 100644 --- a/python/tvm/target/target.py +++ b/python/tvm/target/target.py @@ -292,14 +292,7 @@ def micro(model="unknown", options=None): options : str or list of str Additional options """ - trans_table = { - "host": [], - "stm32f746xx": ["-mcpu=cortex-m7", "-march=armv7e-m"], - "nrf5340dk": ["-mcpu=cortex-m33"], - "host_riscv32": [], - "host_riscv64": [], - } - if model not in trans_table: + if model not in MICRO_SUPPORTED_MODELS: raise ValueError(f"Model {model} not supported by tvm.target.micro.") opts = _merge_opts( MICRO_SUPPORTED_MODELS[model] + ["-runtime=c", "--system-lib", f"-model={model}"], diff --git a/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 b/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 deleted file mode 100755 index a0bf0f2c4dee..000000000000 --- a/tests/micro/qemu/zephyr-runtime/qemu-hack-riscv32/qemu-system-riscv32 +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -e -# 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. - -# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to -# work given the way we've configured docker (the underlying filesystem doesn't suppor the -# file locking it needs to). This script strips any -pidfile option, then invokes qemu. - -ARGS=( "$(basename $0)" ) -while [ "$#" -gt 0 ]; do - if [ "$1" == "-pidfile" ]; then - shift - else - ARGS=( "${ARGS[@]}" "$1" ) - fi - shift -done - -"${ARGS[@]}" From 45234473984ecccc6f9163104a1254aeb1b42016 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Fri, 2 Apr 2021 18:59:39 -0700 Subject: [PATCH 20/37] small fix --- apps/microtvm/reference-vm/zephyr/base-box/setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/microtvm/reference-vm/zephyr/base-box/setup.sh b/apps/microtvm/reference-vm/zephyr/base-box/setup.sh index 719ee7c154e5..6eab652067b3 100644 --- a/apps/microtvm/reference-vm/zephyr/base-box/setup.sh +++ b/apps/microtvm/reference-vm/zephyr/base-box/setup.sh @@ -94,7 +94,7 @@ wget --no-verbose -O $ZEPHYR_SDK_FILE \ https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${ZEPHYR_SDK_VERSION}/zephyr-sdk-${ZEPHYR_SDK_VERSION}-x86_64-linux-setup.run chmod +x $ZEPHYR_SDK_FILE "./$ZEPHYR_SDK_FILE" -- -d ~/zephyr-sdk -y -rm -rf ZEPHYR_SDK_FILE +rm -rf "${ZEPHYR_SDK_FILE}" # GDB for Zephyr SDK depends on python3.8 sudo add-apt-repository ppa:deadsnakes/ppa From c94658561f1beb9715f40d9d6d373634120908c0 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 5 Apr 2021 08:39:58 -0700 Subject: [PATCH 21/37] update vm_name --- apps/microtvm/reference-vm/base-box-tool.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/microtvm/reference-vm/base-box-tool.py b/apps/microtvm/reference-vm/base-box-tool.py index 1eb827d26b76..399ed89babc1 100755 --- a/apps/microtvm/reference-vm/base-box-tool.py +++ b/apps/microtvm/reference-vm/base-box-tool.py @@ -358,6 +358,10 @@ def test_command(args): def release_command(args): + vm_name = f"mehrdadh/microtvm-{args.platform}" + if args.platform == "zephyr": + vm_name = f"{vm_name}-{args.zephyr_version}" + if not args.skip_creating_release_version: subprocess.check_call( [ @@ -365,7 +369,7 @@ def release_command(args): "cloud", "version", "create", - f"tlcpack/microtvm-{args.platform}", + vm_name, args.release_version, ] ) From 39670a50d6aea2e8d35940213024ff06ee6455ab Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Thu, 1 Apr 2021 19:02:31 -0700 Subject: [PATCH 22/37] add zephyr version --- apps/microtvm/reference-vm/base-box-tool.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/microtvm/reference-vm/base-box-tool.py b/apps/microtvm/reference-vm/base-box-tool.py index 399ed89babc1..be398bd9364b 100755 --- a/apps/microtvm/reference-vm/base-box-tool.py +++ b/apps/microtvm/reference-vm/base-box-tool.py @@ -377,13 +377,17 @@ def release_command(args): sys.exit(f"--release-version must be specified") for provider_name in args.provider: + vm_name = f"tlcpack/microtvm-{args.platform}" + if args.platform == "zephyr": + vm_name = f"{vm_name}-{args.zephyr_version}" + subprocess.check_call( [ "vagrant", "cloud", "publish", "-f", - f"tlcpack/microtvm-{args.platform}", + vm_name, args.release_version, provider_name, os.path.join( @@ -465,6 +469,11 @@ def parse_args(): help="For use with 'test' command. MicroTVM platfrom that are used for testing.", ) + parser.add_argument( + "--zephyr-version", + help="Zephyr RTOS version to release, in the form 'x.y'. Must be specified with release.", + ) + return parser.parse_args() From cd9a701502d60008354dfa45cfe130d6462bf2d6 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 16:25:42 -0700 Subject: [PATCH 23/37] add comment for riscv32 issue --- apps/microtvm/zephyr/demo_runtime/src/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index 5bb6f3215002..0d957ea3a9ea 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -214,11 +214,7 @@ tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) { } // Ring buffer used to store data read from the UART on rx interrupt. -#if BOARD == qemu_x86 #define RING_BUF_SIZE_BYTES 4 * 1024 -#else -#define RING_BUF_SIZE_BYTES 1 * 1024 -#endif RING_BUF_DECLARE(uart_rx_rbuf, RING_BUF_SIZE_BYTES); // Small buffer used to read data from the UART into the ring buffer. @@ -269,6 +265,10 @@ static uint8_t main_rx_buf[RING_BUF_SIZE_BYTES]; // The main function of this application. extern void __stdout_hook_install(int (*hook)(int)); void main(void) { + // TODO (mehrdadh): Update this when zephyr version was updated to 2.6. + // Uncomment this for qemu_riscv32, also update zephyr to latest version. + // k_float_enable(_current, 0); + #ifdef CONFIG_LED int ret; led0_pin = device_get_binding(LED0); From 7457b0bd17c8592c204a4c03677f7f485ee25b9c Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 16:26:06 -0700 Subject: [PATCH 24/37] remove debug messages --- src/runtime/micro/micro_session.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/runtime/micro/micro_session.cc b/src/runtime/micro/micro_session.cc index 76d21cb4c539..cd916d46971d 100644 --- a/src/runtime/micro/micro_session.cc +++ b/src/runtime/micro/micro_session.cc @@ -331,16 +331,13 @@ class MicroTransportChannel : public RPCChannel { size_t message_size_bytes; switch (message_type) { case MessageType::kStartSessionInit: - LOG(ERROR) << "kStartSessionInit"; break; case MessageType::kStartSessionReply: - LOG(ERROR) << "kStartSessionReply"; state_ = State::kSessionEstablished; break; case MessageType::kTerminateSession: - LOG(ERROR) << "kTerminateSession"; if (state_ == State::kReset) { state_ = State::kSessionTerminated; } else if (state_ == State::kSessionTerminated) { @@ -352,7 +349,6 @@ class MicroTransportChannel : public RPCChannel { break; case MessageType::kLog: - LOG(ERROR) << "kLog"; uint8_t message[1024]; message_size_bytes = buf->ReadAvailable(); if (message_size_bytes == 0) { @@ -370,7 +366,6 @@ class MicroTransportChannel : public RPCChannel { return; case MessageType::kNormal: - LOG(ERROR) << "kNormal"; did_receive_message_ = true; message_buffer_ = buf; break; From 3aef928c7625ed6d4031838699b7d9d648d47a96 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 16:37:25 -0700 Subject: [PATCH 25/37] cleanup --- apps/microtvm/pyproject.toml | 1 - apps/microtvm/reference-vm/base-box-tool.py | 4 ---- .../microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf | 2 +- .../microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf | 2 +- apps/microtvm/zephyr/demo_runtime/src/main.c | 8 ++------ tests/micro/zephyr/conftest.py | 1 - tests/micro/zephyr/test_zephyr.py | 2 +- 7 files changed, 5 insertions(+), 15 deletions(-) diff --git a/apps/microtvm/pyproject.toml b/apps/microtvm/pyproject.toml index 20edf6553d7f..8bfae0a157cd 100644 --- a/apps/microtvm/pyproject.toml +++ b/apps/microtvm/pyproject.toml @@ -111,7 +111,6 @@ tensorflow-estimator = {version = "^2.1", optional = true} # TFLite frontend tflite = {version = "2.1.0", optional = true} wheel = "*" -colorama = "^0.4.4" [tool.poetry.extras] diff --git a/apps/microtvm/reference-vm/base-box-tool.py b/apps/microtvm/reference-vm/base-box-tool.py index be398bd9364b..b644d9c8df24 100755 --- a/apps/microtvm/reference-vm/base-box-tool.py +++ b/apps/microtvm/reference-vm/base-box-tool.py @@ -377,10 +377,6 @@ def release_command(args): sys.exit(f"--release-version must be specified") for provider_name in args.provider: - vm_name = f"tlcpack/microtvm-{args.platform}" - if args.platform == "zephyr": - vm_name = f"{vm_name}-{args.zephyr_version}" - subprocess.check_call( [ "vagrant", diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf index 13484073d28e..4eea30d8f6f2 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -# This file is specific to the QEMU-emulated microTVM board. +# This file is specific to the QEMU-emulated RISCV32 microTVM board. # For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. CONFIG_TEST_RANDOM_GENERATOR=y diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf index 2f85b7e0c72b..a8a055bcc748 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -# This file is specific to the QEMU-emulated microTVM board. +# This file is specific to the QEMU-emulated RISCV64 microTVM board. # For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random. CONFIG_TEST_RANDOM_GENERATOR=y diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index 0060f362d53c..59fa11ce185d 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -99,13 +99,12 @@ size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const // Called by TVM when an internal invariant is violated, and execution cannot continue. void TVMPlatformAbort(tvm_crt_error_t error) { - // UtvmErrorReport(g_error); - TVMLogf("TVMPlatformAbort: %x", error); sys_reboot(SYS_REBOOT_COLD); #ifdef CONFIG_LED gpio_pin_set(led0_pin, LED0_PIN, 1); #endif - for (;;) ; + for (;;) + ; } // Called by TVM to generate random data. tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) { @@ -290,7 +289,6 @@ void main(void) { // Initialize microTVM RPC server, which will receive commands from the UART and execute them. utvm_rpc_server_t server = UTvmRpcServerInit(write_serial, NULL); TVMLogf("microTVM Zephyr runtime - running"); - TVMLogf("Mehrdad"); #ifdef CONFIG_LED gpio_pin_set(led0_pin, LED0_PIN, 0); #endif @@ -300,8 +298,6 @@ void main(void) { while (true) { int bytes_read = uart_rx_buf_read(&uart_rx_rbuf, main_rx_buf, sizeof(main_rx_buf)); if (bytes_read > 0) { - // for (int i_read=0; i_read 0) { diff --git a/tests/micro/zephyr/conftest.py b/tests/micro/zephyr/conftest.py index eb9eb2928217..f23e64b68777 100644 --- a/tests/micro/zephyr/conftest.py +++ b/tests/micro/zephyr/conftest.py @@ -18,7 +18,6 @@ import tvm.target.target - # The models that should pass this configuration. Maps a short, identifying platform string to # (model, zephyr_board). PLATFORMS = { diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index ee36da1b6f73..f1db9cb51907 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -189,7 +189,7 @@ def test_relay(platform, west_cmd): xx = relay.multiply(x, x) z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype))) func = relay.Function([x], z) - + target = tvm.target.target.micro(model) with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}): graph, mod, params = tvm.relay.build(func, target=target) From 3858ee26d5bc03f729a9f729352d73da1a82f219 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 16:40:17 -0700 Subject: [PATCH 26/37] cleanup --- apps/microtvm/zephyr/demo_runtime/src/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index 59fa11ce185d..60cd25ada799 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -106,6 +106,7 @@ void TVMPlatformAbort(tvm_crt_error_t error) { for (;;) ; } + // Called by TVM to generate random data. tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) { uint32_t random; // one unit of random data. From c5532a58efbb8f94099a5fab181128990e5c408e Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 17:04:28 -0700 Subject: [PATCH 27/37] change workspace --- tests/micro/zephyr/test_zephyr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index f1db9cb51907..4e14bf318822 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -65,7 +65,7 @@ def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): def _make_session(model, target, zephyr_board, west_cmd, mod): - test_name = f"{os.path.splitext(os.path.abspath(__file__))[0]}_{model}" + test_name = f"{os.path.splitext(os.path.abspath(__file__))[0]}_{zephyr_board}" prev_build = f"{test_name}-last-build.micro-binary" workspace_root = ( f'{test_name}_workspace/{datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")}' From 819358b6bac601bcfc9f2a02755545891616259b Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 21:55:20 -0700 Subject: [PATCH 28/37] fix zephyr version --- apps/microtvm/reference-vm/base-box-tool.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/microtvm/reference-vm/base-box-tool.py b/apps/microtvm/reference-vm/base-box-tool.py index b644d9c8df24..c281f5bc14a2 100755 --- a/apps/microtvm/reference-vm/base-box-tool.py +++ b/apps/microtvm/reference-vm/base-box-tool.py @@ -358,7 +358,7 @@ def test_command(args): def release_command(args): - vm_name = f"mehrdadh/microtvm-{args.platform}" + vm_name = f"tlcpack/microtvm-{args.platform}" if args.platform == "zephyr": vm_name = f"{vm_name}-{args.zephyr_version}" @@ -467,6 +467,7 @@ def parse_args(): parser.add_argument( "--zephyr-version", + default="2.5", help="Zephyr RTOS version to release, in the form 'x.y'. Must be specified with release.", ) From d63212921feb56bff1fce369899166832d3587de Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 22:02:22 -0700 Subject: [PATCH 29/37] cleanup --- python/tvm/micro/contrib/zephyr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index b2633b4da1de..76f48d13868e 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -644,7 +644,7 @@ def open(self): cwd=self.base_dir, **self.kwargs, ) - print('START DEBUG', self.debugger) + if self.debugger is not None: self.debugger.start() From 6225e611ecf9626973923acf799641082ada5410 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 22:15:40 -0700 Subject: [PATCH 30/37] change to symlink --- .../qemu-hack/qemu-system-riscv32 | 39 +------------------ .../qemu-hack/qemu-system-riscv64 | 39 +------------------ 2 files changed, 2 insertions(+), 76 deletions(-) mode change 100755 => 120000 apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 mode change 100755 => 120000 apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 deleted file mode 100755 index a30605204d31..000000000000 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash -e -# 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. - -# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to -# work given the way we've configured docker (the underlying filesystem doesn't suppor the -# file locking it needs to). This script strips any -pidfile option, then invokes qemu. - -ARGS=( "$(basename $0)" ) -while [ "$#" -gt 0 ]; do - if [ "$1" == "-pidfile" ]; then - shift - else - ARGS=( "${ARGS[@]}" "$1" ) - fi - shift -done - -# For debugging -if [ "${TVM_QEMU_DEBUG}" != "" ]; then - ARGS=( "${ARGS[@]}" -s -S ) -fi - -"${ARGS[@]}" diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 new file mode 120000 index 000000000000..ebbc8ad5ad9d --- /dev/null +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32 @@ -0,0 +1 @@ +qemu-system-i386 \ No newline at end of file diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 deleted file mode 100755 index a30605204d31..000000000000 --- a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash -e -# 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. - -# Zephyr insists on running qemu with a -pidfile option, but that option doesn't appear to -# work given the way we've configured docker (the underlying filesystem doesn't suppor the -# file locking it needs to). This script strips any -pidfile option, then invokes qemu. - -ARGS=( "$(basename $0)" ) -while [ "$#" -gt 0 ]; do - if [ "$1" == "-pidfile" ]; then - shift - else - ARGS=( "${ARGS[@]}" "$1" ) - fi - shift -done - -# For debugging -if [ "${TVM_QEMU_DEBUG}" != "" ]; then - ARGS=( "${ARGS[@]}" -s -S ) -fi - -"${ARGS[@]}" diff --git a/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 new file mode 120000 index 000000000000..ebbc8ad5ad9d --- /dev/null +++ b/apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64 @@ -0,0 +1 @@ +qemu-system-i386 \ No newline at end of file From 820943a522731e0c82f56ac181be2275b4476cd6 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 22:17:17 -0700 Subject: [PATCH 31/37] fix flag --- apps/microtvm/zephyr/demo_runtime/src/main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index 60cd25ada799..a2f808a42b6d 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -265,9 +265,11 @@ static uint8_t main_rx_buf[RING_BUF_SIZE_BYTES]; // The main function of this application. extern void __stdout_hook_install(int (*hook)(int)); void main(void) { - // TODO (mehrdadh): Update this when zephyr version was updated to 2.6. - // Uncomment this for qemu_riscv32, also update zephyr to latest version. - // k_float_enable(_current, 0); + // TODO (mehrdadh): Update this when zephyr version has updated to 2.6. + // Update zephyr to latest version to use with qemu_riscv32. +#ifdef CONFIG_BOARD_QEMU_RISCV32 + k_float_enable(_current, 0); +#endif #ifdef CONFIG_LED int ret; From a3eb92b86c75450b8124ce7782c8addec7434790 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 6 Apr 2021 22:17:45 -0700 Subject: [PATCH 32/37] add comment --- apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf index 4eea30d8f6f2..dd9c776ca339 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf @@ -24,5 +24,6 @@ CONFIG_TIMER_RANDOM_GENERATOR=y # Default 512, for operations with large floating point data. CONFIG_MAIN_STACK_SIZE=2048 -# For floating point operations. +# For floating point operations. It has exception on floating point operations +# without this flag. CONFIG_FPU_SHARING=y From 53ab2ee0bc303d52e6536301b212866cb5f70647 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Wed, 7 Apr 2021 09:18:48 -0700 Subject: [PATCH 33/37] lint check --- tests/lint/check_file_type.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py index ce20d7838b61..649b18820062 100644 --- a/tests/lint/check_file_type.py +++ b/tests/lint/check_file_type.py @@ -131,11 +131,15 @@ "tests/micro/zephyr/testdata/mnist-8.onnx", # microTVM Zephyr runtime "apps/microtvm/zephyr/demo_runtime/prj.conf", + "apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf", + "apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf", + "apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv64.conf", "apps/microtvm/zephyr/demo_runtime/boards/nrf5340dk_nrf5340_cpuapp.conf", "apps/microtvm/zephyr/demo_runtime/boards/nucleo_f746zg.conf", - "apps/microtvm/zephyr/demo_runtime/boards/qemu_x86.conf", "apps/microtvm/zephyr/demo_runtime/boards/stm32f746g_disco.conf", "apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-i386", + "apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv32", + "apps/microtvm/zephyr/demo_runtime/qemu-hack/qemu-system-riscv64", # microTVM Virtual Machines "apps/microtvm/reference-vm/zephyr/Vagrantfile", "apps/microtvm/reference-vm/zephyr/base-box/Vagrantfile.packer-template", From 319ad3b354ef91f05f016477b95d4b39a99d3dc7 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Wed, 7 Apr 2021 11:23:15 -0700 Subject: [PATCH 34/37] lint fix --- apps/microtvm/zephyr/demo_runtime/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index a2f808a42b6d..4acca0b9ca12 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -265,7 +265,7 @@ static uint8_t main_rx_buf[RING_BUF_SIZE_BYTES]; // The main function of this application. extern void __stdout_hook_install(int (*hook)(int)); void main(void) { - // TODO (mehrdadh): Update this when zephyr version has updated to 2.6. + // TODO (mehrdadh): Update this when zephyr version has updated to 2.6. // Update zephyr to latest version to use with qemu_riscv32. #ifdef CONFIG_BOARD_QEMU_RISCV32 k_float_enable(_current, 0); From 65a1a5957de77c96f74b1720a6b5a4a0772f8a1f Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Wed, 7 Apr 2021 14:29:39 -0700 Subject: [PATCH 35/37] fix format --- python/tvm/micro/contrib/zephyr.py | 28 +++++++++++++++------------- tests/micro/zephyr/conftest.py | 1 + tests/micro/zephyr/test_zephyr.py | 3 +++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index 76f48d13868e..c011219c64fa 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -437,14 +437,14 @@ def _zephyr_transport(self, micro_binary): self._debug_rpc_session, debugger.DebuggerFactory( QemuGdbDebugger, - ( - micro_binary.abspath(micro_binary.debug_files[0]), - ), + (micro_binary.abspath(micro_binary.debug_files[0]),), {}, ), ) - return ZephyrQemuTransport(micro_binary.base_dir, startup_timeout_sec=30.0, debugger=qemu_debugger) + return ZephyrQemuTransport( + micro_binary.base_dir, startup_timeout_sec=30.0, debugger=qemu_debugger + ) def flash(self, micro_binary): cmake_entries = read_cmake_cache( @@ -551,18 +551,22 @@ def transport(self, micro_binary): class QemuGdbDebugger(debugger.GdbDebugger): - def __init__(self, elf_file): super(QemuGdbDebugger, self).__init__() self._elf_file = elf_file def popen_kwargs(self): # expect self._elf file to follow the form .../zephyr/zephyr.elf - cmake_cache_path = ( - pathlib.Path(self._elf_file).parent.parent / "CMakeCache.txt") + cmake_cache_path = pathlib.Path(self._elf_file).parent.parent / "CMakeCache.txt" cmake_cache = read_cmake_cache(cmake_cache_path) return { - "args": [cmake_cache["CMAKE_GDB"], "-ex", "target remote localhost:1234", "-ex", f"file {self._elf_file}"], + "args": [ + cmake_cache["CMAKE_GDB"], + "-ex", + "target remote localhost:1234", + "-ex", + f"file {self._elf_file}", + ], } @@ -626,11 +630,11 @@ def open(self): self.pipe = os.path.join(self.pipe_dir, "fifo") self.write_pipe = os.path.join(self.pipe_dir, "fifo.in") self.read_pipe = os.path.join(self.pipe_dir, "fifo.out") - + os.mkfifo(self.write_pipe) os.mkfifo(self.read_pipe) if self.debugger is not None: - if 'env' in self.kwargs: + if "env" in self.kwargs: self.kwargs["env"] = copy.copy(self.kwargs["env"]) else: self.kwargs["env"] = copy.copy(os.environ) @@ -638,9 +642,7 @@ def open(self): self.kwargs["env"]["TVM_QEMU_DEBUG"] = "1" self.proc = subprocess.Popen( - ["make", - "run", - f"QEMU_PIPE={self.pipe}"], + ["make", "run", f"QEMU_PIPE={self.pipe}"], cwd=self.base_dir, **self.kwargs, ) diff --git a/tests/micro/zephyr/conftest.py b/tests/micro/zephyr/conftest.py index f23e64b68777..edea313f7665 100644 --- a/tests/micro/zephyr/conftest.py +++ b/tests/micro/zephyr/conftest.py @@ -28,6 +28,7 @@ "nrf5340dk": ("nrf5340dk", "nrf5340dk_nrf5340_cpuapp"), } + def pytest_addoption(parser): parser.addoption( "--microtvm-platforms", diff --git a/tests/micro/zephyr/test_zephyr.py b/tests/micro/zephyr/test_zephyr.py index 4e14bf318822..d75e1b607b8d 100644 --- a/tests/micro/zephyr/test_zephyr.py +++ b/tests/micro/zephyr/test_zephyr.py @@ -55,6 +55,7 @@ PLATFORMS = conftest.PLATFORMS + def _make_sess_from_op(model, zephyr_board, west_cmd, op_name, sched, arg_bufs): target = tvm.target.target.micro(model) target = tvm.target.Target(target=target, host=target) @@ -128,6 +129,7 @@ def _make_add_sess(model, zephyr_board, west_cmd): sched = tvm.te.create_schedule(C.op) return _make_sess_from_op(model, zephyr_board, west_cmd, "add", sched, [A, B, C]) + # The same test code can be executed on both the QEMU simulation and on real hardware. def test_compile_runtime(platform, west_cmd): """Test compiling the on-device runtime.""" @@ -391,5 +393,6 @@ def test_byoc_utvm(platform, west_cmd): west_cmd=west_cmd, ) + if __name__ == "__main__": sys.exit(pytest.main([os.path.dirname(__file__)] + sys.argv[1:])) From 9647097794efe472b21eded6069c361c607a99b1 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Wed, 7 Apr 2021 17:16:47 -0700 Subject: [PATCH 36/37] rename debugger --- python/tvm/micro/contrib/zephyr.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/python/tvm/micro/contrib/zephyr.py b/python/tvm/micro/contrib/zephyr.py index c011219c64fa..3fc4d7897095 100644 --- a/python/tvm/micro/contrib/zephyr.py +++ b/python/tvm/micro/contrib/zephyr.py @@ -443,7 +443,7 @@ def _zephyr_transport(self, micro_binary): ) return ZephyrQemuTransport( - micro_binary.base_dir, startup_timeout_sec=30.0, debugger=qemu_debugger + micro_binary.base_dir, startup_timeout_sec=30.0, qemu_debugger=qemu_debugger ) def flash(self, micro_binary): @@ -609,20 +609,20 @@ def write(self, data, timeout_sec): class ZephyrQemuTransport(Transport): """The user-facing Zephyr QEMU transport class.""" - def __init__(self, base_dir, startup_timeout_sec=5.0, debugger=None, **kwargs): + def __init__(self, base_dir, startup_timeout_sec=5.0, qemu_debugger=None, **kwargs): self.base_dir = base_dir self.startup_timeout_sec = startup_timeout_sec self.kwargs = kwargs self.proc = None self.fd_transport = None self.pipe_dir = None - self.debugger = debugger + self.qemu_debugger = qemu_debugger def timeouts(self): return TransportTimeouts( session_start_retry_timeout_sec=2.0, session_start_timeout_sec=self.startup_timeout_sec, - session_established_timeout_sec=5.0 if self.debugger is None else 0, + session_established_timeout_sec=5.0 if self.qemu_debugger is None else 0, ) def open(self): @@ -633,11 +633,11 @@ def open(self): os.mkfifo(self.write_pipe) os.mkfifo(self.read_pipe) - if self.debugger is not None: + if self.qemu_debugger is not None: if "env" in self.kwargs: self.kwargs["env"] = copy.copy(self.kwargs["env"]) else: - self.kwargs["env"] = copy.copy(os.environ) + self.kwargs["env"] = os.environ.copy() self.kwargs["env"]["TVM_QEMU_DEBUG"] = "1" @@ -647,8 +647,8 @@ def open(self): **self.kwargs, ) - if self.debugger is not None: - self.debugger.start() + if self.qemu_debugger is not None: + self.qemu_debugger.start() # NOTE: although each pipe is unidirectional, open both as RDWR to work around a select # limitation on linux. Without this, non-blocking I/O can't use timeouts because named @@ -664,8 +664,8 @@ def open(self): self.fd_transport.open() def close(self): - if self.debugger is not None: - self.debugger.stop() + if self.qemu_debugger is not None: + self.qemu_debugger.stop() if self.fd_transport is not None: self.fd_transport.child_transport.write_monitor_quit() From 25bd2fe4b7292df32434ba5ca9893fdf6e0d4004 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Thu, 8 Apr 2021 09:46:44 -0700 Subject: [PATCH 37/37] rework args --- apps/microtvm/reference-vm/base-box-tool.py | 92 ++++++++----------- .../demo_runtime/boards/qemu_riscv32.conf | 2 +- 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/apps/microtvm/reference-vm/base-box-tool.py b/apps/microtvm/reference-vm/base-box-tool.py index c281f5bc14a2..fb7a9c0b5ce6 100755 --- a/apps/microtvm/reference-vm/base-box-tool.py +++ b/apps/microtvm/reference-vm/base-box-tool.py @@ -358,9 +358,7 @@ def test_command(args): def release_command(args): - vm_name = f"tlcpack/microtvm-{args.platform}" - if args.platform == "zephyr": - vm_name = f"{vm_name}-{args.zephyr_version}" + vm_name = f"tlcpack/microtvm-{args.platform}-{args.platform_version}" if not args.skip_creating_release_version: subprocess.check_call( @@ -396,23 +394,11 @@ def release_command(args): ) -ALL_COMMANDS = { - "build": build_command, - "test": test_command, - "release": release_command, -} - - def parse_args(): parser = argparse.ArgumentParser( description="Automates building, testing, and releasing a base box" ) - parser.add_argument( - "command", - default=",".join(ALL_COMMANDS), - choices=ALL_COMMANDS, - help="Action or actions (comma-separated) to perform.", - ) + subparsers = parser.add_subparsers(help="Action to perform.") parser.add_argument( "platform", help="Name of the platform VM to act on. Must be a sub-directory of this directory.", @@ -421,54 +407,58 @@ def parse_args(): "--provider", choices=ALL_PROVIDERS, action="append", - default=[], - help="Name of the provider or providers to act on; if not specified, act on all", + default=list(ALL_PROVIDERS), + help="Name of the provider or providers to act on; if not specified, act on all.", ) - parser.add_argument( + + parser_build = subparsers.add_parser("build", help="Build a base box.") + parser_build.set_defaults(func=build_command) + parser_test = subparsers.add_parser("test", help="Test a base box before release.") + parser_test.set_defaults(func=test_command) + parser_release = subparsers.add_parser("release", help="Release base box to cloud.") + parser_release.set_defaults(func=release_command) + + parser_build.add_argument( + "--debug-packer", + action="store_true", + help=("Run packer in debug mode, and write log to the base-box directory."), + ) + parser_test.add_argument( "--skip-build", action="store_true", help=( - "For use with the 'test' command. If given, assume a box has already been built in " + "If given, assume a box has already been built in " "the release-test subdirectory. Attach a USB device to this box and execute the " "release test script--do not delete it." ), ) - parser.add_argument( + parser_test.add_argument( "--test-device-serial", help=( "If given, attach the test device with this USB serial number. Corresponds to the " "iSerial field from `lsusb -v` output." ), ) - parser.add_argument( + parser_test.add_argument( + "--microtvm-platform", + choices=ALL_MICROTVM_PLATFORMS, + required=True, + help="MicroTVM platfrom used for testing.", + ) + parser_release.add_argument( "--release-version", + required=True, help="Version to release, in the form 'x.y.z'. Must be specified with release.", ) - parser.add_argument( + parser_release.add_argument( "--skip-creating-release-version", action="store_true", - help="With release, skip creating the version and just upload for this provider.", + help="Skip creating the version and just upload for this provider.", ) - parser.add_argument( - "--debug-packer", - action="store_true", - help=( - "When the build command is given, run packer in debug mode, and write log to the " - "base-box directory" - ), - ) - - parser.add_argument( - "--microtvm-platform", - default="stm32f746xx", - choices=ALL_MICROTVM_PLATFORMS, - help="For use with 'test' command. MicroTVM platfrom that are used for testing.", - ) - - parser.add_argument( - "--zephyr-version", - default="2.5", - help="Zephyr RTOS version to release, in the form 'x.y'. Must be specified with release.", + parser_release.add_argument( + "--platform-version", + required=True, + help="Platform version to release, in the form 'x.y'.", ) return parser.parse_args() @@ -476,21 +466,11 @@ def parse_args(): def main(): args = parse_args() + if os.path.sep in args.platform or not os.path.isdir(os.path.join(THIS_DIR, args.platform)): sys.exit(f" must be a sub-direcotry of {THIS_DIR}; got {args.platform}") - if not args.provider: - args.provider = list(ALL_PROVIDERS) - - todo = [] - for phase in args.command.split(","): - if phase not in ALL_COMMANDS: - sys.exit(f"unknown command: {phase}") - - todo.append(ALL_COMMANDS[phase]) - - for phase in todo: - phase(args) + args.func(args) if __name__ == "__main__": diff --git a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf index dd9c776ca339..3733568ed02f 100644 --- a/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf +++ b/apps/microtvm/zephyr/demo_runtime/boards/qemu_riscv32.conf @@ -21,7 +21,7 @@ CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_TIMER_RANDOM_GENERATOR=y -# Default 512, for operations with large floating point data. +# Default is 512, raised here for operations with large floating point data. CONFIG_MAIN_STACK_SIZE=2048 # For floating point operations. It has exception on floating point operations