Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix][microTVM] QEMU RPC issue #8021

Merged
merged 13 commits into from
Jun 8, 2021
6 changes: 5 additions & 1 deletion apps/microtvm/zephyr/demo_runtime/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "crt_config.h"

static const struct device* tvm_uart;
#define UART_BAUDRATE 115200

#ifdef CONFIG_LED
#define LED0_NODE DT_ALIAS(led0)
Expand Down Expand Up @@ -215,7 +216,7 @@ tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) {

// Ring buffer used to store data read from the UART on rx interrupt.
// Should be larger than TVM_CRT_MAX_PACKET_SIZE_BYTES
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you comment how much larger, and why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the minimum value is 25 bytes and I don't know why.
Also I tested with different baudrates (default is 115200) to see if that solve the issue, but it didn't help.

#define RING_BUF_SIZE_BYTES (TVM_CRT_MAX_PACKET_SIZE_BYTES + 100)
#define RING_BUF_SIZE_BYTES (TVM_CRT_MAX_PACKET_SIZE_BYTES + 25)
RING_BUF_DECLARE(uart_rx_rbuf, RING_BUF_SIZE_BYTES);

// Small buffer used to read data from the UART into the ring buffer.
Expand Down Expand Up @@ -248,6 +249,9 @@ void uart_irq_cb(const struct device* dev, void* user_data) {

// Used to initialize the UART receiver.
void uart_rx_init(struct ring_buf* rbuf, const struct device* dev) {
const struct uart_config config = {
.baudrate = UART_BAUDRATE, .parity = 0, .stop_bits = 1, .data_bits = 3, .flow_ctrl = 0};
mehrdadh marked this conversation as resolved.
Show resolved Hide resolved
uart_configure(dev, &config);
uart_irq_callback_user_data_set(dev, uart_irq_cb, (void*)rbuf);
uart_irq_rx_enable(dev);
}
Expand Down
9 changes: 4 additions & 5 deletions tests/micro/zephyr/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ def pytest_addoption(parser):
"--west-cmd", default="west", help="Path to `west` command for flashing device."
)
parser.addoption(
"--tvm-build",
"--skip-build",
action="store_true",
default=True,
help="If set true, build the uTVM binary from scratch on each test. Otherwise, reuses the build from the previous test run.",
help="If set true, reuses build from the previous test run. Otherwise, build from the scratch.",
)
parser.addoption(
"--tvm-debug",
Expand All @@ -69,8 +68,8 @@ def west_cmd(request):


@pytest.fixture
def tvm_build(request):
return request.config.getoption("--tvm-build")
def skip_build(request):
return request.config.getoption("--skip-build")


@pytest.fixture
Expand Down
30 changes: 15 additions & 15 deletions tests/micro/zephyr/test_zephyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _make_session(model, target, zephyr_board, west_cmd, mod, build_config):
"flasher": compiler.flasher(**flasher_kw),
}

if build_config["build"]:
if not build_config["skip_build"]:
session_kw["binary"] = tvm.micro.build_static_runtime(
# the x86 compiler *expects* you to give the exact same dictionary for both
# lib_opts and bin_opts. so the library compiler is mutating lib_opts and
Expand Down Expand Up @@ -118,15 +118,15 @@ def _make_add_sess(model, zephyr_board, west_cmd, build_config):
B = tvm.te.placeholder((1,), dtype="int8")
C = tvm.te.compute(A.shape, lambda i: A[i] + B[0], name="C")
sched = tvm.te.create_schedule(C.op)
return _make_sess_from_op(model, zephyr_board, west_cmd, "add", sched, [A, B, C])
return _make_sess_from_op(model, zephyr_board, west_cmd, "add", sched, [A, B, C], build_config)


# The same test code can be executed on both the QEMU simulation and on real hardware.
def test_compile_runtime(platform, west_cmd, build, tvm_debug):
def test_compile_runtime(platform, west_cmd, skip_build, tvm_debug):
"""Test compiling the on-device runtime."""

model, zephyr_board = PLATFORMS[platform]
build_config = {"build": tvm_build, "debug": tvm_debug}
build_config = {"skip_build": skip_build, "debug": tvm_debug}

# NOTE: run test in a nested function so cPython will delete arrays before closing the session.
def test_basic_add(sess):
Expand All @@ -145,11 +145,11 @@ def test_basic_add(sess):
test_basic_add(sess)


def test_platform_timer(platform, west_cmd, tvm_build, tvm_debug):
def test_platform_timer(platform, west_cmd, skip_build, tvm_debug):
"""Test compiling the on-device runtime."""

model, zephyr_board = PLATFORMS[platform]
build_config = {"build": tvm_build, "debug": tvm_debug}
build_config = {"skip_build": skip_build, "debug": tvm_debug}

# NOTE: run test in a nested function so cPython will delete arrays before closing the session.
def test_basic_add(sess):
Expand All @@ -173,10 +173,10 @@ def test_basic_add(sess):
test_basic_add(sess)


def test_relay(platform, west_cmd, tvm_build, tvm_debug):
def test_relay(platform, west_cmd, skip_build, tvm_debug):
"""Testing a simple relay graph"""
model, zephyr_board = PLATFORMS[platform]
build_config = {"build": tvm_build, "debug": tvm_debug}
build_config = {"skip_build": skip_build, "debug": tvm_debug}
shape = (10,)
dtype = "int8"

Expand All @@ -202,10 +202,10 @@ def test_relay(platform, west_cmd, tvm_build, tvm_debug):
tvm.testing.assert_allclose(result, x_in * x_in + 1)


def test_onnx(platform, west_cmd, tvm_build, tvm_debug):
def test_onnx(platform, west_cmd, skip_build, tvm_debug):
"""Testing a simple ONNX model."""
model, zephyr_board = PLATFORMS[platform]
build_config = {"build": tvm_build, "debug": tvm_debug}
build_config = {"skip_build": skip_build, "debug": tvm_debug}

# Load test images.
this_dir = os.path.dirname(__file__)
Expand Down Expand Up @@ -332,10 +332,10 @@ def check_result(
tvm.testing.assert_allclose(out.asnumpy(), results[idx], rtol=TOL, atol=TOL)


def test_byoc_utvm(platform, west_cmd, tvm_build, tvm_debug):
def test_byoc_utvm(platform, west_cmd, skip_build, tvm_debug):
"""This is a simple test case to check BYOC capabilities of uTVM"""
model, zephyr_board = PLATFORMS[platform]
build_config = {"build": tvm_build, "debug": tvm_debug}
build_config = {"skip_build": skip_build, "debug": tvm_debug}
x = relay.var("x", shape=(10, 10))
w0 = relay.var("w0", shape=(10, 10))
w1 = relay.var("w1", shape=(10, 10))
Expand Down Expand Up @@ -408,10 +408,10 @@ def _make_add_sess_with_shape(model, zephyr_board, west_cmd, shape, build_config
pytest.param((16 * 1024,), id="(16*1024)"),
],
)
def test_rpc_large_array(platform, west_cmd, tvm_build, tvm_debug, shape):
def test_rpc_large_array(platform, west_cmd, skip_build, tvm_debug, shape):
"""Test large RPC array transfer."""
model, zephyr_board = PLATFORMS[platform]
build_config = {"build": tvm_build, "debug": tvm_debug}
build_config = {"skip_build": skip_build, "debug": tvm_debug}

# NOTE: run test in a nested function so cPython will delete arrays before closing the session.
def test_tensors(sess):
Expand All @@ -422,7 +422,7 @@ def test_tensors(sess):
C_data = tvm.nd.array(np.zeros(shape, dtype="int8"), device=sess.device)
assert (C_data.asnumpy() == np.zeros(shape)).all()

with _make_large_tensors_sess(model, zephyr_board, west_cmd, shape, build_config) as sess:
with _make_add_sess_with_shape(model, zephyr_board, west_cmd, shape, build_config) as sess:
test_tensors(sess)


Expand Down