Skip to content

Commit e99c17d

Browse files
committed
Fix microTVM demos
1 parent 6084fb9 commit e99c17d

6 files changed

Lines changed: 36 additions & 28 deletions

File tree

docs/arch/microtvm_design.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,24 @@ logs use it to rank measured performance (but see Future Work).
127127
Targets are currently represented as strings structured similarly to command-line arguments. An
128128
example target is shown below:
129129

130-
``c -keys=arm_cpu -mcpu=cortex-m7 -link-params -model=stm32f746xx -runtime=c -system-lib=1``
130+
``c -keys=arm_cpu -mcpu=cortex-m7 -model=stm32f746xx``
131131

132132
The relevant parts to microTVM are:
133133

134134
* Code generator (``llvm`` or ``c``)
135135
* ``-mcpu=cortex-m7``: used by TOPI to enable Cortex-M schedules, and, when the C source code
136136
generator is selected, included in the output as a comment to help identify the code and
137137
configure the downstream C compiler.
138-
* ``-link-params``: include parameters as global constants to load from flash.
139-
* ``-runtime=c``: build glue code to allow operators to work with the C runtime
140-
* ``-system-lib=1``: emit a system library (i.e. which can be loaded by calling the PackedFunc
141-
``runtime.SystemLib``.
138+
139+
Runtime and Executor configuration for microTVM
140+
-----------------------------------------------
141+
142+
When using microTVM, it's important to use the C Runtime (``Runtime('crt')``), which is the runtime that works best on micro devices rather than the more dynamic C++ Runtime. Alongside this, there are two executors which you could use in combination with the C runtime:
143+
144+
* ``Executor("aot")`` - The Ahead of Time (AOT) executor precompiles the network into a runnable function which you can add directly into your micro application
145+
* ``Executor("graph", {"link-params": True})`` - The Graph executor provides a JSON representation of your network and requires the C Runtime's system library to be generated to find functions in the function registry (``Runtime("crt", {"system-lib": True})``). ``{"link-params":True}`` enables parameters to be linked into the generated files rather than provided externally.
146+
147+
These are specified when building a runtime module: ``relay.build(..., runtime=..., executor=...)``.
142148

143149
Writing Schedules for microTVM
144150
------------------------------

gallery/how_to/work_with_microtvm/micro_autotune.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import pathlib
3333

3434
import tvm
35+
from tvm.relay.backend import Executor, Runtime
3536

3637
####################
3738
# Defining the model
@@ -69,13 +70,15 @@
6970
# Defining the target #
7071
#######################
7172
# Now we define the TVM target that describes the execution environment. This looks very similar
72-
# to target definitions from other microTVM tutorials.
73+
# to target definitions from other microTVM tutorials. Alongside this we pick the C Runtime to code
74+
# generate our model against.
7375
#
7476
# When running on physical hardware, choose a target and a board that
7577
# describe the hardware. There are multiple hardware targets that could be selected from
7678
# PLATFORM list in this tutorial. You can chose the platform by passing --platform argument when running
7779
# this tutorial.
7880
#
81+
RUNTIME = Runtime("crt", {"system-lib": True})
7982
TARGET = tvm.target.target.micro("host")
8083

8184
# Compiling for physical hardware
@@ -126,6 +129,7 @@
126129
build_kwargs={"build_option": {"tir.disable_vectorize": True}},
127130
do_fork=True,
128131
build_func=tvm.micro.autotvm_build_func,
132+
runtime=RUNTIME,
129133
)
130134
runner = tvm.autotvm.LocalRunner(number=1, repeat=1, timeout=100, module_loader=module_loader)
131135

@@ -178,7 +182,7 @@
178182
# the tuned operator.
179183

180184
with pass_context:
181-
lowered = tvm.relay.build(relay_mod, target=TARGET, params=params)
185+
lowered = tvm.relay.build(relay_mod, target=TARGET, runtime=RUNTIME, params=params)
182186

183187
temp_dir = tvm.contrib.utils.tempdir()
184188

@@ -221,7 +225,7 @@
221225

222226
with tvm.autotvm.apply_history_best("microtvm_autotune.log.txt"):
223227
with pass_context:
224-
lowered_tuned = tvm.relay.build(relay_mod, target=TARGET, params=params)
228+
lowered_tuned = tvm.relay.build(relay_mod, target=TARGET, runtime=RUNTIME, params=params)
225229

226230
temp_dir = tvm.contrib.utils.tempdir()
227231

gallery/how_to/work_with_microtvm/micro_tflite.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,9 @@
124124

125125
import os
126126
import numpy as np
127-
import logging
128127

129128
import tvm
130-
import tvm.micro as micro
131129
from tvm.contrib.download import download_testdata
132-
from tvm.contrib import graph_executor, utils
133130
from tvm import relay
134131

135132
model_url = "https://people.linaro.org/~tom.gall/sine_model.tflite"
@@ -179,9 +176,10 @@
179176
# Now we create a build config for relay, turning off two options and then calling relay.build which
180177
# will result in a C source file for the selected TARGET. When running on a simulated target of the
181178
# same architecture as the host (where this Python script is executed) choose "host" below for the
182-
# TARGET and a proper board/VM to run it (Zephyr will create the right QEMU VM based on BOARD. In
183-
# the example below the x86 arch is selected and a x86 VM is picked up accordingly:
179+
# TARGET, the C Runtime as the RUNTIME and a proper board/VM to run it (Zephyr will create the right
180+
# QEMU VM based on BOARD. In the example below the x86 arch is selected and a x86 VM is picked up accordingly:
184181
#
182+
RUNTIME = tvm.relay.backend.Runtime("crt", {"system-lib": True})
185183
TARGET = tvm.target.target.micro("host")
186184
BOARD = "qemu_x86"
187185
#
@@ -210,7 +208,7 @@
210208
with tvm.transform.PassContext(
211209
opt_level=3, config={"tir.disable_vectorize": True}, disabled_pass=["AlterOpLayout"]
212210
):
213-
module = relay.build(mod, target=TARGET, params=params)
211+
module = relay.build(mod, target=TARGET, runtime=RUNTIME, params=params)
214212

215213

216214
# Inspecting the compilation output

tests/micro/arduino/test_arduino_rpc_server.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
2323
"""
2424

25-
import datetime
2625
import pathlib
2726
import sys
2827

@@ -31,8 +30,9 @@
3130
import pytest
3231
import tvm
3332
from PIL import Image
34-
from tvm import micro, relay
33+
from tvm import relay
3534
from tvm.relay.testing import byoc
35+
from tvm.relay.backend import Executor, Runtime
3636

3737
import conftest
3838

@@ -191,9 +191,11 @@ def test_onnx(board, arduino_cli_cmd, tvm_debug, workspace_dir):
191191
relay_mod, params = relay.frontend.from_onnx(onnx_model, shape=shape, freeze_params=True)
192192
relay_mod = relay.transform.DynamicToStatic()(relay_mod)
193193

194-
target = tvm.target.target.micro(model, options=["-link-params=1"])
194+
target = tvm.target.target.micro(model)
195195
with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
196-
lowered = relay.build(relay_mod, target, params=params)
196+
executor = Executor("graph", {"link-params": True})
197+
runtime = Runtime("crt", {"system-lib": True})
198+
lowered = relay.build(relay_mod, target, params=params, executor=executor, runtime=runtime)
197199
graph = lowered.get_graph_json()
198200

199201
with _make_session(

tests/micro/zephyr/test_zephyr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import logging
1818
import os
1919
import pathlib
20-
import subprocess
2120
import sys
2221
import logging
2322

@@ -220,7 +219,7 @@ def test_onnx(temp_dir, board, west_cmd, tvm_debug):
220219
relay_mod, params = relay.frontend.from_onnx(onnx_model, shape=shape, freeze_params=True)
221220
relay_mod = relay.transform.DynamicToStatic()(relay_mod)
222221

223-
# We add the -link-params=1 option to ensure the model parameters are compiled in.
222+
# We add the link-params=True option to ensure the model parameters are compiled in.
224223
# There is currently a bug preventing the host_driven environment from receiving
225224
# the model weights when set using graph_mod.set_input().
226225
# See: https://github.com/apache/tvm/issues/7567

tests/micro/zephyr/test_zephyr_armv7m.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
from tvm.contrib.download import download_testdata
3737
from tvm.micro.model_library_format import generate_c_interface_header
38+
from tvm.relay.backend import Executor, Runtime
3839

3940
import conftest
4041

@@ -187,14 +188,10 @@ def test_armv7m_intrinsic(temp_dir, board, west_cmd, tvm_debug):
187188

188189
target = tvm.target.target.micro(
189190
model,
190-
options=[
191-
"-keys=arm_cpu,cpu",
192-
"-link-params=1",
193-
"--executor=aot",
194-
"--unpacked-api=1",
195-
"--interface-api=c",
196-
],
191+
options=["-keys=arm_cpu,cpu"],
197192
)
193+
executor = Executor("aot", {"unpacked-api": True, "interface-api": "c"})
194+
runtime = Runtime("crt")
198195

199196
temp_dir_simd = temp_dir / "simd"
200197
temp_dir_no_simd = temp_dir / "nosimd"
@@ -204,7 +201,9 @@ def test_armv7m_intrinsic(temp_dir, board, west_cmd, tvm_debug):
204201

205202
with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
206203
lowered_simd = relay.build(relay_mod_simd, target, params=params)
207-
lowered_no_simd = relay.build(relay_mod_no_simd, target, params=params)
204+
lowered_no_simd = relay.build(
205+
relay_mod_no_simd, target, params=params, runtime=runtime, executor=executor
206+
)
208207
result_simd, time_simd = _run_model(
209208
temp_dir_simd, board, west_cmd, lowered_simd, build_config, sample, output_shape
210209
)

0 commit comments

Comments
 (0)