Skip to content

Commit

Permalink
Fix several lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gromero committed Oct 22, 2021
1 parent a63a486 commit 2ff3d80
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 39 deletions.
2 changes: 1 addition & 1 deletion python/tvm/driver/tvmc/autotuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@


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

parser = subparsers.add_parser("tune", help="auto-tune a model")
Expand Down
18 changes: 11 additions & 7 deletions python/tvm/driver/tvmc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def get_project_options(project_info):
# 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]]
om = [(opt[md], bool(md == "required")) for md in metadata if opt[md]]
for methods, is_opt_required in om:
for method in methods:
name = opt["name"]
Expand All @@ -584,7 +584,9 @@ def get_project_options(project_info):
else:
default_text = None

formatted_help_text = format_option(option_choices_text, help_text, default_text, is_opt_required)
formatted_help_text = format_option(
option_choices_text, help_text, default_text, is_opt_required
)

option = {
"name": opt["name"],
Expand Down Expand Up @@ -638,15 +640,16 @@ def check_options(options, valid_options):
None. Raise TVMCException if check fails, i.e. if an option is not in the list of valid options.
"""
required_options = [opt["name"] for opt in valid_options if opt["required"] == True]
required_options = [opt["name"] for opt in valid_options if opt["required"]]
for required_option in required_options:
if required_option not in options:
raise TVMCException(
f"Option '{required_option}' is required but was not specified. Use --list-options to see all required options."
f"Option '{required_option}' is required but was not specified. Use --list-options "
"to see all required options."
)

remaining_options = set(options) - set(required_options)
optional_options = [opt["name"] for opt in valid_options if opt["required"] == False]
optional_options = [opt["name"] for opt in valid_options if not opt["required"]]
for option in remaining_options:
if option not in optional_options:
raise TVMCException(
Expand All @@ -673,12 +676,13 @@ def check_options_choices(options, valid_options):
# Dict of all valid options and associated valid choices.
# Options with no choices are excluded from the dict.
valid_options_choices = {
opt["name"]: opt["choices"] for opt in valid_options if opt["choices"] is not None
opt["name"]: opt["choices"] for opt in valid_options if opt["choices"] is not None
}

for option in options:
if option in valid_options_choices:
if options[option] not in valid_options_choices[option]:
raise TVMCException(
f"Choice '{options[option]} for option '{option}' is invalid. Use --list-options to see all available choices for that option."
f"Choice '{options[option]} for option '{option}' is invalid. "
"Use --list-options to see all available choices for that option."
)
2 changes: 1 addition & 1 deletion python/tvm/driver/tvmc/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@


@register_parser
def add_compile_parser(subparsers, main_parser):
def add_compile_parser(subparsers, _):
"""Include parser for 'compile' subcommand"""

parser = subparsers.add_parser("compile", help="compile a model.")
Expand Down
1 change: 0 additions & 1 deletion python/tvm/driver/tvmc/fmtopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,4 @@ def format_option(option_text, help_text, default_text, required=True):
if required:
help_text_just_chunk += " (required)"


return choices_text_just_chunk + "\n" + help_text_just_chunk
53 changes: 32 additions & 21 deletions python/tvm/driver/tvmc/micro.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
# specific language governing permissions and limitations
# under the License.

import argparse
import os
import shutil
from pathlib import Path
from collections import defaultdict
import argparse
import shutil
import sys

import tvm.micro.project as project
from tvm.micro.project_api.server import ServerError
Expand Down Expand Up @@ -91,8 +91,11 @@ def add_micro_parser(subparsers, main_parser):

# For each platform add arguments detected automatically using Project API info query.

# Create subparsers for the platforms under 'create-project', 'build', and 'flash' subcommands first.
help_msg = "you must selected a platform from the list. You can pass '-h' for a selected platform to list its options."
# Create subparsers for the platforms under 'create-project', 'build', and 'flash' subcommands.
help_msg = (
"you must selected a platform from the list. You can pass '-h' for a selected "
"platform to list its options."
)
create_project_platforms_parser = create_project_parser.add_subparsers(
title="platforms", help=help_msg, dest="platform"
)
Expand All @@ -103,30 +106,36 @@ def add_micro_parser(subparsers, main_parser):
title="platforms", help=help_msg, dest="platform"
)

# Map used to map a API method to proper CLI parsers and handlers.
# API method parser handler smbcmd domain
subcmds = {
# API method name Parser associated to method Handler func to call after parsing
"generate_project": [create_project_platforms_parser, create_project_handler],
"build": [build_platforms_parser, build_handler],
"flash": [flash_platforms_parser, flash_handler],
}

# Helper to add a platform to a subcmd parser.
# Helper to add a platform parser to a subcmd parser.
def _add_parser(parser, platform):
platform_name = platform[0].upper() + platform[1:] + " platform"
o = parser.add_parser(platform, add_help=False, help=f"select {platform_name}.")
o.set_defaults(platform=platform)
return o
platform_parser = parser.add_parser(
platform, add_help=False, help=f"select {platform_name}."
)
platform_parser.set_defaults(platform=platform)
return platform_parser

parser_by_subcmd = {}
for subcmd, o in subcmds.items():
for subcmd, subcmd_parser_handler in subcmds.items():
subcmd_parser = subcmd_parser_handler[0]
subcmd_parser.required = True # Selecting a platform or template is mandatory
parser_by_platform = {}
o[0].required = True # Selecting a platform/template is mandatory
for platform in TEMPLATES:
new_parser = _add_parser(o[0], platform)
new_parser = _add_parser(subcmd_parser, platform)
parser_by_platform[platform] = new_parser

new_parser = o[0].add_parser("template", add_help=False, help="select an adhoc template.")
# Besides adding the parsers for each default platform (like Zephyr and Arduino), add a
# parser for 'template' to deal with adhoc projects/platforms.
new_parser = subcmd_parser.add_parser(
"template", add_help=False, help="select an adhoc template."
)
new_parser.add_argument(
"--template-dir", required=True, help="Project API template directory."
)
Expand All @@ -135,7 +144,7 @@ def _add_parser(parser, platform):

parser_by_subcmd[subcmd] = parser_by_platform

known_args, unkown_args = main_parser.parse_known_args()
known_args, _ = main_parser.parse_known_args()

try:
subcmd = known_args.subcommand
Expand Down Expand Up @@ -207,7 +216,8 @@ def create_project_handler(args):
shutil.rmtree(args.project_dir)
else:
raise TVMCException(
"The specified project dir already exists. To force overwriting it use '-f' or '--force'."
"The specified project dir already exists. "
"To force overwriting it use '-f' or '--force'."
)
project_dir = args.project_dir

Expand All @@ -227,7 +237,7 @@ def create_project_handler(args):
project.generate_project_from_mlf(template_dir, project_dir, mlf_path, options)
except ServerError as error:
print("The following error occured on the Project API server side: \n", error)
exit(1)
sys.exit(1)


def build_handler(args):
Expand All @@ -239,7 +249,8 @@ def build_handler(args):
shutil.rmtree(args.project_dir + "/build")
else:
raise TVMCException(
f"There is already a build in {args.project_dir}. To force rebuild it use '-f' or '--force'."
f"There is already a build in {args.project_dir}. "
"To force rebuild it use '-f' or '--force'."
)

project_dir = args.project_dir
Expand All @@ -253,7 +264,7 @@ def build_handler(args):
prj.build()
except ServerError as error:
print("The following error occured on the Project API server side: ", error)
exit(1)
sys.exit(1)


def flash_handler(args):
Expand All @@ -271,4 +282,4 @@ def flash_handler(args):
prj.flash()
except ServerError as error:
print("The following error occured on the Project API server side: ", error)
exit(1)
sys.exit(1)
17 changes: 9 additions & 8 deletions python/tvm/driver/tvmc/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@
from typing import Dict, List, Optional, Union
from tarfile import ReadError
import argparse

import numpy as np
import os
import sys
import numpy as np

import tvm
from tvm import rpc
from tvm.autotvm.measure import request_remote
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 Down Expand Up @@ -122,7 +120,7 @@ def add_run_parser(subparsers, main_parser):
#
# print(sys.argv)
if "run" in sys.argv:
known_args, unknown_args = main_parser.parse_known_args()
known_args, _ = main_parser.parse_known_args()
else:
return

Expand All @@ -135,9 +133,11 @@ def add_run_parser(subparsers, main_parser):
try:
project_ = project.GeneratedProject.from_directory(project_dir, None)
except ProjectAPIServerNotFoundError:
exit(f"Error: Project API server not found in {project_dir}!")
sys.exit(f"Error: Project API server not found in {project_dir}!")
except TemplateProjectError:
exit(f"Error: Project directory error. That usually happens when model.tar is not found.")
sys.exit(
f"Error: Project directory error. That usually happens when model.tar is not found."
)

project_info = project_.info()
options_by_method = get_project_options(project_info)
Expand Down Expand Up @@ -189,7 +189,8 @@ def drive_run(args):

if args.device != "micro":
raise TVMCException(
f"Device '{args.device}' not supported. Only device 'micro' is supported to run a model in MLF (--device micro)."
f"Device '{args.device}' not supported. "
"Only device 'micro' is supported to run a model in MLF (--device micro)."
)

if args.profile:
Expand Down

0 comments on commit 2ff3d80

Please sign in to comment.