Skip to content

Commit

Permalink
[TVMC] micro, run: Disable micro support when USE_MICRO=OFF (#9632)
Browse files Browse the repository at this point in the history
Do not generate 'tvmc micro' subcommand and disable micro device option
in 'tvmc run' when TVM is built without support for microTVM, i.e. with
set(USE_MICRO OFF).

Signed-off-by: Gustavo Romero <[email protected]>
  • Loading branch information
gromero authored Dec 2, 2021
1 parent f435a13 commit 724f35e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
38 changes: 24 additions & 14 deletions python/tvm/driver/tvmc/micro.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
import shutil
import sys

import tvm.micro.project as project
from tvm.micro import get_microtvm_template_projects
from tvm.micro.build import MicroTVMTemplateProjectNotFoundError
from tvm.micro.project_api.server import ServerError
from tvm.micro.project_api.client import ProjectAPIServerNotFoundError
from .main import register_parser
from .common import (
TVMCException,
Expand All @@ -36,21 +31,36 @@
get_and_check_options,
)

try:
import tvm.micro.project as project
from tvm.micro import get_microtvm_template_projects
from tvm.micro.build import MicroTVMTemplateProjectNotFoundError
from tvm.micro.project_api.server import ServerError
from tvm.micro.project_api.client import ProjectAPIServerNotFoundError

TEMPLATES = {}
for p in ("zephyr", "arduino"):
try:
TEMPLATES[p] = get_microtvm_template_projects(p)
except MicroTVMTemplateProjectNotFoundError:
pass
SUPPORT_MICRO = True
except (ImportError, NameError):
SUPPORT_MICRO = False


@register_parser
def add_micro_parser(subparsers, main_parser):
"""Includes parser for 'micro' context and associated subcommands:
create-project, build, and flash.
create-project (create), build, and flash.
"""

if SUPPORT_MICRO is False:
# Don't create 'tvmc micro' parser.
return

# Probe available default platform templates.
templates = {}
for p in ("zephyr", "arduino"):
try:
templates[p] = get_microtvm_template_projects(p)
except MicroTVMTemplateProjectNotFoundError:
pass

micro = subparsers.add_parser("micro", help="select micro context.")
micro.set_defaults(func=drive_micro)

Expand Down Expand Up @@ -132,7 +142,7 @@ def _add_parser(parser, platform):
subcmd_parser = subcmd_parser_handler[0]
subcmd_parser.required = True # Selecting a platform or template is mandatory
parser_by_platform = {}
for platform in TEMPLATES:
for platform in templates:
new_parser = _add_parser(subcmd_parser, platform)
parser_by_platform[platform] = new_parser

Expand Down Expand Up @@ -169,7 +179,7 @@ def _add_parser(parser, platform):
template_dir = str(Path(known_args.template_dir).resolve())
else:
# default template
template_dir = TEMPLATES[platform]
template_dir = templates[platform]

try:
template = project.TemplateProject.from_directory(template_dir)
Expand Down
18 changes: 15 additions & 3 deletions python/tvm/driver/tvmc/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
from tvm.contrib import graph_executor as runtime
from tvm.contrib.debugger import debug_executor
from tvm.relay.param_dict import load_param_dict
import tvm.micro.project as project
from tvm.micro.project import TemplateProjectError
from tvm.micro.project_api.client import ProjectAPIServerNotFoundError
from . import common
from .common import (
TVMCException,
Expand All @@ -48,6 +45,15 @@
from .model import TVMCPackage, TVMCResult
from .result_utils import get_top_results

try:
import tvm.micro.project as project
from tvm.micro.project import TemplateProjectError
from tvm.micro.project_api.client import ProjectAPIServerNotFoundError

SUPPORT_MICRO = True
except ImportError:
SUPPORT_MICRO = False

# pylint: disable=invalid-name
logger = logging.getLogger("TVMC")

Expand Down Expand Up @@ -135,6 +141,12 @@ def add_run_parser(subparsers, main_parser):
# No need to augment the parser for micro targets.
return

if SUPPORT_MICRO is False:
sys.exit(
"'--device micro' is not supported. "
"Please build TVM with micro support (USE_MICRO ON)!"
)

project_dir = known_args.PATH

try:
Expand Down

0 comments on commit 724f35e

Please sign in to comment.