Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
9169dc6
Initial commit of API server impl.
Mar 11, 2021
64c184d
initial commit of api client
Mar 11, 2021
e1718ac
Add TVM-side glue code to use Project API
Mar 11, 2021
f9286b5
Change tvm.micro.Session to use Project API
Jul 8, 2021
f49f088
Rework how crt_config.h is used on the host.
Jul 8, 2021
a0f5daf
Modify Transport infrastructure to work with Project API
Jul 8, 2021
c7ad52e
Add host microTVM API server
Jul 8, 2021
af6f690
Zephyr implementation of microTVM API server
Jul 8, 2021
ebdace5
consolidate CcompilerAnnotator
Jul 20, 2021
a690762
Allow model library format with c backend, add test.
Jul 20, 2021
90a4464
Update unit tests
May 27, 2021
61cf49f
fix incorrect doc
Jul 7, 2021
1a7e0ef
Delete old Zephyr build infrastructure
Jul 8, 2021
e84aa8e
Delete old build abstractions
Jul 8, 2021
3483c10
Delete old Transport implementations and simplify module
Jul 8, 2021
1a1d44e
lint
Jul 9, 2021
07c0f2b
ASF header
Jul 20, 2021
a6a1c0a
Remove warning from Zephyr API server
guberti Jul 14, 2021
80e2b37
Add Arduino target
guberti Jul 14, 2021
feb1b72
ProjectAPI Arduino unit test
guberti Jul 14, 2021
0c8bbd2
Arduino template_project
guberti Jul 14, 2021
c1961b3
Arduino uTVM API server
guberti Jul 14, 2021
ceadb9a
Move crt_config to dedicated folder
guberti Jul 15, 2021
bb4812b
Kind-of-working conftest
guberti Jul 15, 2021
6f533f1
Fully functional project generation
guberti Jul 15, 2021
1f24b94
Stub unused functions and fix template flagging
guberti Jul 15, 2021
25120dc
Arduino project compilation unit test
guberti Jul 15, 2021
8c538e3
Arduino board flashing functionality
guberti Jul 15, 2021
ad44141
C runtime bugfixes
guberti Jul 16, 2021
c134e3d
Redesign unit tests to mimic workflow
guberti Jul 16, 2021
626018a
Suppress graph_executor.c warning
guberti Jul 16, 2021
9e969c9
Sample test project
guberti Jul 16, 2021
62e3f63
Runtime tests when hardware is present
guberti Jul 16, 2021
e7b829f
Remove unused pytest mark
guberti Jul 16, 2021
7e6741d
Linting
guberti Jul 16, 2021
5d08e53
Slow but usable RNG
guberti Jul 17, 2021
711bfbd
Linting
guberti Jul 17, 2021
db578c1
Add support for other hardware platforms
guberti Jul 20, 2021
b916496
Only copy project if it's a template
guberti Jul 26, 2021
1290cca
Arduino tests README
guberti Jul 26, 2021
e831ed6
Nano 33 BLE fixes
guberti Jul 26, 2021
a604b39
Unit tests for project generation
guberti Jul 26, 2021
5c042fe
Update graph.json path
guberti Jul 26, 2021
4baf6d5
Address PR early feedback
guberti Jul 26, 2021
5453d02
Unit tests for port auto-detect and flashing
guberti Jul 26, 2021
6c434b9
Move unit test to dedicated tests folder
guberti Jul 27, 2021
1516f18
Formatting
guberti Jul 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Arduino project compilation unit test
  • Loading branch information
guberti committed Jul 26, 2021
commit 25120dce9445d3b051d49f6bdb5d089b7f031491
41 changes: 38 additions & 3 deletions apps/microtvm/arduino/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
MODEL_LIBRARY_FORMAT_RELPATH = "src/model/model.tar"
Comment thread
guberti marked this conversation as resolved.
Outdated

API_SERVER_DIR = pathlib.Path(os.path.dirname(__file__) or os.path.getcwd())
BUILD_DIR = API_SERVER_DIR / "build"
IS_TEMPLATE = not (API_SERVER_DIR / MODEL_LIBRARY_FORMAT_RELPATH).exists()
MODEL_LIBRARY_FORMAT_PATH = "" if IS_TEMPLATE else API_SERVER_DIR / MODEL_LIBRARY_FORMAT_RELPATH
Comment thread
guberti marked this conversation as resolved.
Outdated
_LOG = logging.getLogger(__name__)
Expand All @@ -44,6 +45,19 @@ class BoardAutodetectFailed(Exception):
server.ProjectOption("arduino_board", help="Name of the Arduino board to build for"),
Comment thread
guberti marked this conversation as resolved.
Outdated
]

BOARD_PROPERTIES = {
"spresense": {
"package": "SPRESENSE",
"architecture": "spresense",
"board": "spresense",
},
"nano33ble_sense": {
"package": "arduino",
"architecture": "mbed_nano",
"board": "nano33ble",
}
}


class Handler(server.ProjectAPIHandler):
def __init__(self):
Expand Down Expand Up @@ -235,8 +249,6 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
# Unpack the MLF and copy the relevant files
graph = self._disassemble_mlf(model_library_format_path, source_dir)

# Copy the MLF tarball to the model directory for use as a flag
# Very ugly - TODO ask Andrew why this is
shutil.copy2(model_library_format_path, source_dir / "model")

# Populate our parameters file
Expand All @@ -245,8 +257,31 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec
# Recursively change imports
self._convert_imports(project_dir, source_dir)


def _get_fqbn(self, options):
o = BOARD_PROPERTIES[options['arduino_board']]
print(o['package'])
return f"{o['package']}:{o['architecture']}:{o['board']}"


def build(self, options):
raise NotImplementedError
BUILD_DIR.mkdir()
print(BUILD_DIR)

compile_cmd = [
options['arduino_cmd'], "compile",
"--fqbn", self._get_fqbn(options),
"--build-path", BUILD_DIR.resolve()
]

if options.get("verbose"):
compile_cmd.append("--verbose")

# Specify project to compile
compile_cmd.append("./project/")
print(compile_cmd)
print(API_SERVER_DIR)
subprocess.check_call(compile_cmd)


def flash(self, options):
Expand Down
4 changes: 2 additions & 2 deletions tests/micro/arduino/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# The models that should pass this configuration. Maps a short, identifying platform string to
# (model, zephyr_board).
PLATFORMS = {
"spresense_main": ("cxd5602gg", "spresense"),
"spresense": ("cxd5602gg", "spresense"),
}


def pytest_addoption(parser):
parser.addoption(
"--microtvm-platforms",
default="spresense_main",
default="spresense",
choices=PLATFORMS.keys(),
help=(
"Specify a comma-separated list of test models (i.e. as passed to tvm.target.micro()) "
Expand Down
74 changes: 65 additions & 9 deletions tests/micro/arduino/test_arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

PLATFORMS = conftest.PLATFORMS

def _make_session(model, target, arduino_board, arduino_cmd, mod, build_config):
def _generate_project(model, target, arduino_board, arduino_cmd, mod, build_config):
parent_dir = os.path.dirname(__file__)
filename = os.path.splitext(os.path.basename(__file__))[0]
prev_build = f"{os.path.join(parent_dir, 'archive')}_{filename}_{arduino_board}_last_build.micro"
Expand All @@ -24,8 +24,7 @@ def _make_session(model, target, arduino_board, arduino_cmd, mod, build_config):
if not os.path.exists(workspace_parent):
os.makedirs(workspace_parent)
workspace = tvm.micro.Workspace(debug=True, root=workspace_root)
print("Outputing workspace root:")
print(workspace_root)

template_project_dir = (
pathlib.Path(__file__).parent
/ ".."
Expand All @@ -42,15 +41,12 @@ def _make_session(model, target, arduino_board, arduino_cmd, mod, build_config):
workspace.relpath("project"),
{"arduino_board": arduino_board, "arduino_cmd": arduino_cmd, "verbose": 0},
)
#project.build()
#project.flash()
#return tvm.micro.Session(project.transport())
return (workspace, project)


# This is bad, don't do this
TARGET = "c -keys=cpu -link-params=1 -mcpu=cortex-m33 -model=nrf5340dk -runtime=c -system-lib=1"

def test_generate_yes_no_project(platform, arduino_cmd):
def _generate_yes_no_project(platform, arduino_cmd):
current_dir = os.path.dirname(__file__)
model, arduino_board = PLATFORMS[platform]
#target = tvm.target.target.micro(model, options=["-link-params=1"])
Expand All @@ -63,7 +59,67 @@ def test_generate_yes_no_project(platform, arduino_cmd):
with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
mod = relay.build(mod, TARGET, params=params)

session = _make_session(model, TARGET, arduino_board, arduino_cmd, mod, build_config)
return _generate_project(model, TARGET, arduino_board, arduino_cmd, mod, build_config)
#project.build()
#project.flash()
#return tvm.micro.Session(project.transport())


def test_generate_yes_no_project(platform, arduino_cmd):
workspace, project = _generate_yes_no_project(platform, arduino_cmd)

# Ensure top-level directory structure looks good
assert(os.listdir(workspace.path) == ['project'])

project_dir = pathlib.Path(workspace.path) / "project"
assert(set([
'microtvm_api_server.py', 'project.ino', 'src']
).issubset(os.listdir(project_dir)))

source_dir = project_dir / "src"
assert(set(os.listdir(source_dir)) == set([
'model', 'standalone_crt', 'implementation.c',
'model.cpp', 'model.h', 'parameters.h'
]))


# Ensure model was connected and graph_json compiled
model_dir = source_dir / "model"
assert(set(os.listdir(model_dir)) == set([
'default_lib0.c', 'default_lib1.c', 'graph_json.c', 'model.tar'
]))
with (model_dir / "graph_json.c").open() as f:
graph_json_c = f.read()
assert("static const char* graph_json" in graph_json_c)


# Ensure parameters.h was templated with correct information
# for our yes/no model
with (source_dir / "parameters.h").open() as f:
parameters_h = f.read()
assert("INPUT_DATA_SHAPE[] = {1, 1960};" in parameters_h)


# Check one file to ensure imports were rerouted
runtime_c_path = source_dir / "standalone_crt" / "src" / "runtime"
load_json_path = runtime_c_path / "crt" / "graph_executor" / "load_json.c"
assert(load_json_path.exists())

with (load_json_path).open() as f:
load_json_c = f.read()
assert('#include "stdlib.h"' in load_json_c)
assert('include/tvm/runtime/crt/platform.h' in load_json_c)


def test_compile_yes_no_project(platform, arduino_cmd):
workspace, project = _generate_yes_no_project(platform, arduino_cmd)
project.build()

# Make sure build_dir is not empty
build_dir = pathlib.Path(workspace.path) / "project" / "build"
assert(build_dir.exists())
first_build_file = next(build_dir.iterdir(), None)
assert(first_build_file is not None)


if __name__ == "__main__":
Expand Down