Skip to content

Commit

Permalink
[microTVM] Project API infrastructure (apache#8380)
Browse files Browse the repository at this point in the history
* Initial commit of API server impl.

* initial commit of api client

* Add TVM-side glue code to use Project API

* Change tvm.micro.Session to use Project API

* Rework how crt_config.h is used on the host.

 * use template crt_config.h for host test runtime; delete
   src/runtime/crt/host/crt_config.h so that it doesn't diverge from
   the template
 * bring template crt_config.h inline with the one actually in use
  * rename to MAX_STRLEN_DLTYPE
 * Create a dedicated TVM-side host crt_config.h in src/runtime/micro

* Modify Transport infrastructure to work with Project API

* Add host microTVM API server

* Zephyr implementation of microTVM API server

 * move all zephyr projects to apps/microtvm/zephyr/template_project

* consolidate CcompilerAnnotator

* Allow model library format with c backend, add test.

* Update unit tests

* fix incorrect doc

* Delete old Zephyr build infrastructure

* Delete old build abstractions

* Delete old Transport implementations and simplify module

* lint

* ASF header

* address gromero comments

* final fixes?

* fix is_shutdown

* fix user-facing API

* fix TempDirectory / operator

* Update micro_tflite tutorial

* lint

* fix test_crt and test_link_params

* undo global micro import, hopefully fix fixture

* lint

* fix more tests

* Address tmoreau89 comments and mehrdadh comments

 * fix random number generator prj.conf for physical hw
 * uncomment proper aot option
  • Loading branch information
areusch authored and mehrdadh committed Aug 11, 2021
1 parent f9ec02b commit 8da0114
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 65 deletions.
28 changes: 23 additions & 5 deletions apps/microtvm/zephyr/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ def server_info_query(self, tvm_version):
# These files and directories will be recursively copied into generated projects from the CRT.
CRT_COPY_ITEMS = ("include", "Makefile", "src")

# Maps extra line added to prj.conf to a tuple or list of zephyr_board for which it is needed.
EXTRA_PRJ_CONF_DIRECTIVES = {
"CONFIG_TIMER_RANDOM_GENERATOR=y": (
"qemu_x86",
"qemu_riscv32",
"qemu_cortex_r5",
"qemu_riscv64",
),
"CONFIG_ENTROPY_GENERATOR_BOARDS=y": (
"mps2_an521",
"nrf5340dk_nrf5340_cpuapp",
"nucleo_f746zg",
"nucleo_l4r5zi",
"stm32f746g_disco",
),
}

def _create_prj_conf(self, project_dir, options):
with open(project_dir / "prj.conf", "w") as f:
f.write(
Expand All @@ -278,7 +295,7 @@ def _create_prj_conf(self, project_dir, options):
)
f.write("# For TVMPlatformAbort().\n" "CONFIG_REBOOT=y\n" "\n")

if True: # options["project_type"] == "host_driven":
if options["project_type"] == "host_driven":
f.write("# For RPC server C++ bindings.\n" "CONFIG_CPLUSPLUS=y\n" "\n")

f.write("# For math routines\n" "CONFIG_NEWLIB_LIBC=y\n" "\n")
Expand All @@ -296,10 +313,11 @@ def _create_prj_conf(self, project_dir, options):

f.write("# For random number generation.\n" "CONFIG_TEST_RANDOM_GENERATOR=y\n")

if self._is_qemu(options):
f.write("CONFIG_TIMER_RANDOM_GENERATOR=y\n")
else:
f.write("CONFIG_ENTROPY_GENERATOR=y\n")
f.write("\n# Extra prj.conf directives")
for line, board_list in self.EXTRA_PRJ_CONF_DIRECTIVES.items():
if options["zephyr_board"] in board_list:
f.write(f"{line}\n")

f.write("\n")

API_SERVER_CRT_LIBS_TOKEN = "<API_SERVER_CRT_LIBS>"
Expand Down
2 changes: 0 additions & 2 deletions python/tvm/micro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# under the License.
"""MicroTVM module for bare-metal backends"""

from .build import autotvm_build_func
from .build import autotvm_module_loader
from .build import get_standalone_crt_dir
from .model_library_format import export_model_library_format, UnsupportedInModelLibraryFormatError
from .project import generate_project, GeneratedProject, TemplateProject
Expand Down
56 changes: 0 additions & 56 deletions python/tvm/micro/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

"""Defines top-level glue functions for building microTVM artifacts."""

import contextlib
import json
import logging
import os
import pathlib

from .._ffi import libinfo
from .. import rpc as _rpc
Expand Down Expand Up @@ -61,56 +58,3 @@ def get_standalone_crt_dir() -> str:
raise CrtNotFoundError()

return STANDALONE_CRT_DIR


def autotvm_module_loader(
template_project_dir: str,
project_options: dict = None,
):
"""Configure a new adapter.
Parameters
----------
template_project_dir: str
Path to the template project directory on the runner.
project_options : dict
Opt
compiler options specific to this build.
workspace_kw : Optional[dict]
Keyword args passed to the Workspace constructor.
"""
if isinstance(template_project_dir, pathlib.Path):
template_project_dir = str(template_project_dir)
elif not isinstance(template_project_dir, str):
raise TypeError(f"Incorrect type {type(template_project_dir)}.")

@contextlib.contextmanager
def module_loader(remote_kw, build_result):
with open(build_result.filename, "rb") as build_file:
build_result_bin = build_file.read()

tracker = _rpc.connect_tracker(remote_kw["host"], remote_kw["port"])
remote = tracker.request(
remote_kw["device_key"],
priority=remote_kw["priority"],
session_timeout=remote_kw["timeout"],
session_constructor_args=[
"tvm.micro.compile_and_create_micro_session",
build_result_bin,
template_project_dir,
json.dumps(project_options),
],
)
system_lib = remote.get_function("runtime.SystemLib")()
yield remote, system_lib

return module_loader


def autotvm_build_func(*args, **kwargs):
pass


autotvm_build_func.output_format = "tar.gz"
5 changes: 3 additions & 2 deletions tests/micro/zephyr/test_zephyr_aot.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ def _get_message(fd, expr: str):


@tvm.testing.requires_micro
def test_tflite(platform, west_cmd, skip_build, tvm_debug):
def test_tflite(temp_dir, platform, west_cmd, skip_build, tvm_debug):
"""Testing a TFLite model."""

if platform not in ["host", "mps2_an521", "nrf5340dk", "stm32l4r5zi_nucleo", "zynq_mp_r5"]:
pytest.skip(msg="Model does not fit.")

"""Testing a TFLite model."""
model, zephyr_board = PLATFORMS[platform]
input_shape = (1, 32, 32, 3)
output_shape = (1, 10)
Expand Down

0 comments on commit 8da0114

Please sign in to comment.