From 724f35ee839ee3752f741b299e9b187611324c82 Mon Sep 17 00:00:00 2001 From: Gustavo Romero Date: Thu, 2 Dec 2021 19:42:01 -0300 Subject: [PATCH] [TVMC] micro, run: Disable micro support when USE_MICRO=OFF (#9632) 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 --- python/tvm/driver/tvmc/micro.py | 38 ++++++++++++++++++++------------ python/tvm/driver/tvmc/runner.py | 18 ++++++++++++--- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/python/tvm/driver/tvmc/micro.py b/python/tvm/driver/tvmc/micro.py index 9b31d3278b91..ef72446b931c 100644 --- a/python/tvm/driver/tvmc/micro.py +++ b/python/tvm/driver/tvmc/micro.py @@ -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, @@ -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) @@ -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 @@ -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) diff --git a/python/tvm/driver/tvmc/runner.py b/python/tvm/driver/tvmc/runner.py index eb571143e551..29a82db959a5 100644 --- a/python/tvm/driver/tvmc/runner.py +++ b/python/tvm/driver/tvmc/runner.py @@ -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, @@ -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") @@ -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: