Skip to content

Commit

Permalink
[TVMC][microTVM] Add new micro context (#9229)
Browse files Browse the repository at this point in the history
* [microTVM] zephyr: Make platform options comply with RFC-0020

Make Zephyr platform options comply with RFC-0020 specification.

Project options now need to specify the required metadata for every
option, i.e. 'required', 'optional', and 'type'.

Signed-off-by: Gustavo Romero <[email protected]>

* [microTVM] arduino: Make platform options comply with RFC-0020

Make Arduino platform options comply with RFC-0020 specification.

Project options now need to specify the required metadata for every
option, i.e. 'required', 'optional', and 'type'.

Signed-off-by: Gustavo Romero <[email protected]>

* [microTVM] crt: Make crt options comply with RFC-0020

Make crt project options comply with RFC-0020 specification.

Project options now need to specify the required metadata for every
option, i.e. 'required', 'optional', and 'type'.

Signed-off-by: Gustavo Romero <[email protected]>

* [microTVM][Unittest] Adapt test to RFC-0020

Adapt test to new metadata fields accordingly to RFC-0020 specification.

Signed-off-by: Gustavo Romero <[email protected]>

* [microTVM] Add info() method to GeneratedProject class

Add info() method to GeneratedProject class so one can use the Project
API to query options for project dirs instead of only for template
projects.

This commit also adds for the sake of convenience a setter and a getter
for 'options' in case it's necessary to set or get 'options' after a
GeneratedProject class is instantiated without initializing 'options'.

Signed-off-by: Gustavo Romero <[email protected]>

* [microTVM] Fix typo in python/tvm/micro/session.py

Fix typo in comment.

Signed-off-by: Gustavo Romero <[email protected]>

* Allow multiple runs on micro targets

Currently there is a limitation on microTVM / TVM which doesn't allow
running a model multiple times in sequence without previously flashing
the model to the device.

Root cause is that RPCModuleNode class destructor is called once a run
finishes. The destructor sends a RPCCode::kFreeHandle packet with
type_code = kTVMModuleHandle to the device which wipes entries in
crt/src/runtime/crt/common/crt_runtime_api.c:147:static const TVMModule*
registered_modules[TVM_CRT_MAX_REGISTERED_MODULES] when TVMFreeMod() is
called when the target receives a kFreeHandle packet.

Hence when one tries to re-run a model registered_modules[0] == NULL
causes a backtrace on the host side. Probably never before a model on
microTVM was run without being flashed just before the run, so tvmc run
implementation for micro targets exposed the issue.

This commit fixes it by not calling TVMFreeMod() for system_lib_handle
on the target side when a session terminates so the pointer to the
system_lib_handle is not flushed from 'registered_modules', allowing
multiple runs on micro targets.

Signed-off-by: Gustavo Romero <[email protected]>

* [TVMC] Pass main parser when calling add_*_parser functions

Currently when a add_*_parser functions are called in main.py to build
and add the various subparsers to the main parser only a subparser is
passed to the functions. However if one of these functions need to build
a dynamic parser it needs also to call the main parser at least once to
parse once the command line and get the arguments necessary to finally
build the complete parser.

This commit fixes that limitation by passing also the main parser when
calling the subparser builders so it can be used to build the dynamic
subparses.

Signed-off-by: Gustavo Romero <[email protected]>

* [TVMC] micro: Add new micro context

This commit introduces support for micro targets (targets supported by
microTVM). It creates a new micro context under the new TVMC command
'tvmc micro'. Moreover, three new subcommands are made available in the
new context under 'tvmc micro': 'create-project', 'build', and 'flash'.

The new support relies on the Project API to query all the options
available for a selected platform (like Zephyr and Arduino) and also
from any adhoc platform template directory which provides a custom
Project API server.

Signed-off-by: Gustavo Romero <[email protected]>

* [TVMC] run: Add support for micro devices

Add support for micro devices using the Project API to query all options
available for a given platform and open a session with an specified
micro device. Use of 'tvmc run' with micro device is enabled via the
'--device micro' option in addition to the project directory.

Once the project directory is specified 'tvmc run' will make all options
specific to the platform found in the project dir available as options
in 'tvmc run'. They can be listed by '--list-options' and passed via
'--options'.

Signed-off-by: Gustavo Romero <[email protected]>
  • Loading branch information
gromero authored Nov 19, 2021
1 parent 6832309 commit 53cb5ea
Show file tree
Hide file tree
Showing 16 changed files with 1,028 additions and 117 deletions.
30 changes: 24 additions & 6 deletions apps/microtvm/arduino/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,40 @@ class BoardAutodetectFailed(Exception):
PROJECT_OPTIONS = [
server.ProjectOption(
"arduino_board",
required=["build", "flash", "open_transport"],
choices=list(BOARD_PROPERTIES),
help="Name of the Arduino board to build for",
type="str",
help="Name of the Arduino board to build for.",
),
server.ProjectOption(
"arduino_cli_cmd",
required=["build", "flash", "open_transport"],
type="str",
help="Path to the arduino-cli tool.",
),
server.ProjectOption(
"port",
optional=["flash", "open_transport"],
type="int",
help="Port to use for connecting to hardware.",
),
server.ProjectOption("arduino_cli_cmd", help="Path to the arduino-cli tool."),
server.ProjectOption("port", help="Port to use for connecting to hardware"),
server.ProjectOption(
"project_type",
help="Type of project to generate.",
required=["generate_project"],
choices=tuple(PROJECT_TYPES),
type="str",
help="Type of project to generate.",
),
server.ProjectOption(
"verbose", help="True to pass --verbose flag to arduino-cli compile and upload"
"verbose",
optional=["build", "flash"],
type="bool",
help="Run arduino-cli compile and upload with verbose output.",
),
server.ProjectOption(
"warning_as_error",
choices=(True, False),
optional=["generate_project"],
type="bool",
help="Treat warnings as errors and raise an Exception.",
),
]
Expand Down
42 changes: 37 additions & 5 deletions apps/microtvm/zephyr/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,49 +234,81 @@ def _get_nrf_device_args(options):
PROJECT_OPTIONS = [
server.ProjectOption(
"extra_files_tar",
optional=["generate_project"],
type="str",
help="If given, during generate_project, uncompress the tarball at this path into the project dir.",
),
server.ProjectOption(
"gdbserver_port", help=("If given, port number to use when running the local gdbserver.")
"gdbserver_port",
help=("If given, port number to use when running the local gdbserver."),
optional=["open_transport"],
type="int",
),
server.ProjectOption(
"nrfjprog_snr",
optional=["open_transport"],
type="int",
help=("When used with nRF targets, serial # of the attached board to use, from nrfjprog."),
),
server.ProjectOption(
"openocd_serial",
optional=["open_transport"],
type="int",
help=("When used with OpenOCD targets, serial # of the attached board to use."),
),
server.ProjectOption(
"project_type",
help="Type of project to generate.",
choices=tuple(PROJECT_TYPES),
required=["generate_project"],
type="str",
help="Type of project to generate.",
),
server.ProjectOption(
"verbose",
optional=["build"],
type="bool",
help="Run build with verbose output.",
),
server.ProjectOption("verbose", help="Run build with verbose output.", choices=(True, False)),
server.ProjectOption(
"west_cmd",
optional=["generate_project"],
default=sys.executable + " -m west" if sys.executable else None,
type="str",
help=(
"Path to the west tool. If given, supersedes both the zephyr_base "
"option and ZEPHYR_BASE environment variable."
),
),
server.ProjectOption("zephyr_base", help="Path to the zephyr base directory."),
server.ProjectOption(
"zephyr_base",
optional=["build", "open_transport"],
default=os.getenv("ZEPHYR_BASE"),
type="str",
help="Path to the zephyr base directory.",
),
server.ProjectOption(
"zephyr_board",
required=["generate_project", "build", "flash", "open_transport"],
choices=list(BOARD_PROPERTIES),
type="str",
help="Name of the Zephyr board to build for.",
),
server.ProjectOption(
"config_main_stack_size",
optional=["generate_project"],
type="int",
help="Sets CONFIG_MAIN_STACK_SIZE for Zephyr board.",
),
server.ProjectOption(
"warning_as_error",
choices=(True, False),
optional=["generate_project"],
type="bool",
help="Treat warnings as errors and raise an Exception.",
),
server.ProjectOption(
"compile_definitions",
optional=["generate_project"],
type="str",
help="Extra definitions added project compile.",
),
]
Expand Down
3 changes: 2 additions & 1 deletion python/tvm/driver/tvmc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
TVMC - TVM driver command-line interface
"""

from . import micro
from . import runner
from . import autotuner
from . import compiler
from . import runner
from . import result_utils
from .frontends import load_model as load
from .compiler import compile_model as compile
Expand Down
2 changes: 1 addition & 1 deletion python/tvm/driver/tvmc/autotuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@


@register_parser
def add_tune_parser(subparsers):
def add_tune_parser(subparsers, _):
"""Include parser for 'tune' subcommand"""

parser = subparsers.add_parser("tune", help="auto-tune a model")
Expand Down
Loading

0 comments on commit 53cb5ea

Please sign in to comment.