forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TVMC] Add
--config
argument for config files (apache#11012)
* [TVMC] Add `--config` argument for config files Collecting common configurations for users of TVM and exposing them gracefully in tvmc using a `--config` option as defined in https://github.com/apache/tvm-rfcs/blob/main/rfcs/0030-tvmc-comand-line-configuration-files.md Co-authored-by: Shai Maor <[email protected]> * Add correct test guards Co-authored-by: Shai Maor <[email protected]>
- Loading branch information
Showing
12 changed files
with
401 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"kind": "llvm" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"targets": [ | ||
{"kind": "cmsis-nn", "from_device": "1"}, | ||
{"kind": "c", "mcpu": "cortex-m55"} | ||
], | ||
"executor": { "kind": "aot"}, | ||
"runtime": { "kind": "crt"}, | ||
"pass-config": { "tir.disable_vectorize": "1"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"targets": [ | ||
{ "kind": "llvm" } | ||
], | ||
"trials": "2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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. | ||
""" | ||
manipulate json config file to work with TVMC | ||
""" | ||
import os | ||
import json | ||
from tvm.driver.tvmc import TVMCException | ||
|
||
|
||
def find_json_file(name, path): | ||
"""search for json file given file name a path | ||
Parameters | ||
---------- | ||
name: string | ||
the file name need to be searched | ||
path: string | ||
path to search at | ||
Returns | ||
------- | ||
string | ||
the full path to that file | ||
""" | ||
match = "" | ||
for root, _dirs, files in os.walk(path): | ||
if name in files: | ||
match = os.path.join(root, name) | ||
break | ||
|
||
return match | ||
|
||
|
||
def read_and_convert_json_into_dict(config_args): | ||
"""Read json configuration file and return a dictionary with all parameters | ||
Parameters | ||
---------- | ||
args: argparse.Namespace | ||
Arguments from command line parser holding the json file path. | ||
Returns | ||
------- | ||
dictionary | ||
dictionary with all the json arguments keys and values | ||
""" | ||
try: | ||
if ".json" not in config_args.config: | ||
config_args.config = config_args.config.strip() + ".json" | ||
if os.path.isfile(config_args.config): | ||
json_config_file = config_args.config | ||
else: | ||
config_dir = os.path.abspath( | ||
os.path.join(os.path.realpath(__file__), "..", "..", "..", "..", "..", "configs") | ||
) | ||
json_config_file = find_json_file(config_args.config, config_dir) | ||
return json.load(open(json_config_file, "rb")) | ||
|
||
except FileNotFoundError: | ||
raise TVMCException( | ||
f"File {config_args.config} does not exist at {config_dir} or is wrong format." | ||
) | ||
|
||
|
||
def parse_target_from_json(one_target, command_line_list): | ||
"""parse the targets out of the json file struct | ||
Parameters | ||
---------- | ||
one_target: dict | ||
dictionary with all target's details | ||
command_line_list: list | ||
list to update with target parameters | ||
""" | ||
target_kind, *sub_type = [ | ||
one_target[key] if key == "kind" else (key, one_target[key]) for key in one_target | ||
] | ||
|
||
internal_dict = {} | ||
if sub_type: | ||
sub_target_type = sub_type[0][0] | ||
target_value = sub_type[0][1] | ||
internal_dict[f"target_{target_kind}_{sub_target_type}"] = target_value | ||
command_line_list.append(internal_dict) | ||
|
||
return target_kind | ||
|
||
|
||
def convert_config_json_to_cli(json_params): | ||
"""convert all configuration keys & values from dictionary to cli format | ||
Parameters | ||
---------- | ||
args: dictionary | ||
dictionary with all configuration keys & values. | ||
Returns | ||
------- | ||
int | ||
list of configuration values in cli format | ||
""" | ||
command_line_list = [] | ||
for param_key in json_params: | ||
if param_key == "targets": | ||
target_list = [ | ||
parse_target_from_json(one_target, command_line_list) | ||
for one_target in json_params[param_key] | ||
] | ||
|
||
internal_dict = {} | ||
internal_dict["target"] = ", ".join(map(str, target_list)) | ||
command_line_list.append(internal_dict) | ||
|
||
elif param_key in ("executor", "runtime"): | ||
for key, value in json_params[param_key].items(): | ||
if key == "kind": | ||
kind = f"{value}_" | ||
new_dict_key = param_key | ||
else: | ||
new_dict_key = f"{param_key}_{kind}{key}" | ||
|
||
internal_dict = {} | ||
internal_dict[new_dict_key.replace("-", "_")] = value | ||
command_line_list.append(internal_dict) | ||
|
||
elif isinstance(json_params[param_key], dict): | ||
internal_dict = {} | ||
modify_param_key = param_key.replace("-", "_") | ||
internal_dict[modify_param_key] = [] | ||
for key, value in json_params[param_key].items(): | ||
internal_dict[modify_param_key].append(f"{key}={value}") | ||
command_line_list.append(internal_dict) | ||
|
||
else: | ||
internal_dict = {} | ||
internal_dict[param_key.replace("-", "_")] = json_params[param_key] | ||
command_line_list.append(internal_dict) | ||
|
||
return command_line_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.