You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/10/13 22:27:35 UTC

[GitHub] [tvm] mehrdadh commented on a change in pull request #9229: [TVMC] Add new micro context

mehrdadh commented on a change in pull request #9229:
URL: https://github.com/apache/tvm/pull/9229#discussion_r728480988



##########
File path: python/tvm/driver/tvmc/common.py
##########
@@ -520,3 +522,84 @@ def parse_configs(input_configs):
         pass_context_configs[name] = parsed_value
 
     return pass_context_configs
+
+
+def get_project_options(project_info):
+    options = project_info["project_options"]
+
+    options_by_method = defaultdict(list)
+    for opt in options:
+        # Get list of methods associated with an option based on the
+        # existance of a 'required' or 'optional' lists. API specification
+        # guarantees at least one of these lists will exist. If a list does
+        # not exist it's returned as None by the API.
+        metadata = ["required", "optional"]
+        om = [(opt[md], True if md == "required" else False) for md in metadata if opt[md]]
+        for methods, is_opt_required in om:
+            for method in methods:
+                name = opt["name"]
+
+                if opt["type"] == "bool":
+                    # TODO(gromero): Use only choices= and merge with non-bool options below
+                    option_choices_text = f"{name}={{true, false}}"
+
+                else:
+                    if opt["choices"]:
+                        choices = "{" + ", ".join(opt["choices"]) + "}"
+                    else:
+                        choices = opt["name"].upper()
+
+                    option_choices_text = f"{name}={choices}"
+
+                help_text = opt["help"][0].lower() + opt["help"][1:]
+                formatted_help_text = format_option(option_choices_text, help_text, is_opt_required)
+
+                option = {
+                    "name": opt["name"],
+                    "choices": opt["choices"],
+                    "help_text": formatted_help_text,
+                    "required": is_opt_required,
+                }
+                options_by_method[method].append(option)
+
+    return options_by_method
+
+
+def get_options(options):

Review comment:
       Could you please add docstring to all methods? it makes it much easier to understand
   also maybe add type to arguments?

##########
File path: python/tvm/driver/tvmc/micro.py
##########
@@ -0,0 +1,278 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import os
+import shutil
+from pathlib import Path
+from collections import defaultdict
+import argparse
+
+import tvm.micro.project as project
+from tvm.micro.project_api.server import ServerError
+from .main import register_parser
+from .common import (
+    TVMCException,
+    get_project_options,
+    get_options,
+    check_options,
+    check_options_choices,
+)
+from .fmtopt import format_option
+
+
+TVM_HOME = os.getenv("TVM_HOME")
+
+
+ZEPHYR_TEMPLATE_DIR = TVM_HOME + "/apps/microtvm/zephyr/template_project"
+ARDUINO_TEMPLATE_DIR = TVM_HOME + "/apps/microtvm/arduino/template_project"
+
+
+TEMPLATES = {
+    "zephyr": ZEPHYR_TEMPLATE_DIR,
+    "arduino": ARDUINO_TEMPLATE_DIR,
+}
+
+
+@register_parser
+def add_micro_parser(subparsers, main_parser):
+    micro = subparsers.add_parser("micro", help="select micro context.")
+    micro.set_defaults(func=drive_micro)
+
+    micro_parser = micro.add_subparsers(title="subcommands")
+    # Selecting a subcommand under 'micro' is mandatory
+    micro_parser.required = True
+    micro_parser.dest = "subcommand"  # options available to select
+
+    # 'create_project' subcommand
+    create_project_parser = micro_parser.add_parser(
+        "create-project", help="create a project template of a given type or given a template dir."
+    )
+    create_project_parser.set_defaults(subcommand_handler=create_project_handler)
+    create_project_parser.add_argument(
+        "PROJECT_DIR",
+        help="Project dir where the new project based on the template dir will be created.",
+    )
+    create_project_parser.add_argument("MLF", help="MLF .tar archive.")
+    create_project_parser.add_argument(
+        "-f",
+        "--force",
+        action="store_true",
+        help="force project creating even if the specified PROJECT_DIR already exists.",
+    )
+
+    # 'build' subcommand
+    build_parser = micro_parser.add_parser(
+        "build",
+        help="build a project dir, generally creating an image to be flashed, e.g. zephyr.elf.",
+    )
+    build_parser.set_defaults(subcommand_handler=build_handler)
+    build_parser.add_argument("PROJECT_DIR", help="Project dir to build.")
+    build_parser.add_argument("-f", "--force", action="store_true", help="Force rebuild.")
+
+    # 'flash' subcommand
+    flash_parser = micro_parser.add_parser(
+        "flash", help="flash the built image on a given micro target."
+    )
+    flash_parser.set_defaults(subcommand_handler=flash_handler)
+    flash_parser.add_argument("PROJECT_DIR", help="Project dir with a image built.")
+
+    # TODO(gromero): list and select serial when multiple devices exist
+    # It's not clear yet if the device list should be determined by TVMC or returned by the Project API

Review comment:
       I think serial number should come from TVMC. It could be optional and if it's not passed used the one that project API finds.

##########
File path: apps/microtvm/zephyr/template_project/microtvm_api_server.py
##########
@@ -229,40 +229,69 @@ 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"],
+        help="Run build with verbose output.",
+        type="bool",
     ),
-    server.ProjectOption("verbose", help="Run build with verbose output.", choices=(True, False)),
     server.ProjectOption(
         "west_cmd",
+        optional=["generate_project"],
+        default="python3 -m west",
+        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="ZEPHYR_BASE",

Review comment:
       I think by default we could get it from environment variable ZEPHYR_BASE?

##########
File path: python/tvm/driver/tvmc/micro.py
##########
@@ -0,0 +1,278 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import os
+import shutil
+from pathlib import Path
+from collections import defaultdict
+import argparse
+
+import tvm.micro.project as project
+from tvm.micro.project_api.server import ServerError
+from .main import register_parser
+from .common import (
+    TVMCException,
+    get_project_options,
+    get_options,
+    check_options,
+    check_options_choices,
+)
+from .fmtopt import format_option
+
+
+TVM_HOME = os.getenv("TVM_HOME")
+
+
+ZEPHYR_TEMPLATE_DIR = TVM_HOME + "/apps/microtvm/zephyr/template_project"
+ARDUINO_TEMPLATE_DIR = TVM_HOME + "/apps/microtvm/arduino/template_project"
+
+
+TEMPLATES = {
+    "zephyr": ZEPHYR_TEMPLATE_DIR,
+    "arduino": ARDUINO_TEMPLATE_DIR,
+}
+
+
+@register_parser
+def add_micro_parser(subparsers, main_parser):
+    micro = subparsers.add_parser("micro", help="select micro context.")
+    micro.set_defaults(func=drive_micro)
+
+    micro_parser = micro.add_subparsers(title="subcommands")
+    # Selecting a subcommand under 'micro' is mandatory
+    micro_parser.required = True
+    micro_parser.dest = "subcommand"  # options available to select
+
+    # 'create_project' subcommand
+    create_project_parser = micro_parser.add_parser(
+        "create-project", help="create a project template of a given type or given a template dir."
+    )
+    create_project_parser.set_defaults(subcommand_handler=create_project_handler)
+    create_project_parser.add_argument(
+        "PROJECT_DIR",

Review comment:
       +1 to lowercase.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org