|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +""" |
| 20 | +manipulate json config file to work with TVMC |
| 21 | +""" |
| 22 | +import os |
| 23 | +import json |
| 24 | +from tvm.driver.tvmc import TVMCException |
| 25 | + |
| 26 | + |
| 27 | +def find_json_file(name, path): |
| 28 | + """search for json file given file name a path |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + name: string |
| 33 | + the file name need to be searched |
| 34 | + path: string |
| 35 | + path to search at |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + string |
| 40 | + the full path to that file |
| 41 | +
|
| 42 | + """ |
| 43 | + match = "" |
| 44 | + for root, _dirs, files in os.walk(path): |
| 45 | + if name in files: |
| 46 | + match = os.path.join(root, name) |
| 47 | + break |
| 48 | + |
| 49 | + return match |
| 50 | + |
| 51 | + |
| 52 | +def read_and_convert_json_into_dict(config_args): |
| 53 | + """Read json configuration file and return a dictionary with all parameters |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + args: argparse.Namespace |
| 58 | + Arguments from command line parser holding the json file path. |
| 59 | +
|
| 60 | + Returns |
| 61 | + ------- |
| 62 | + dictionary |
| 63 | + dictionary with all the json arguments keys and values |
| 64 | +
|
| 65 | + """ |
| 66 | + try: |
| 67 | + if ".json" not in config_args.config: |
| 68 | + config_args.config = config_args.config.strip() + ".json" |
| 69 | + if os.path.isfile(config_args.config): |
| 70 | + json_config_file = config_args.config |
| 71 | + else: |
| 72 | + config_dir = os.path.abspath( |
| 73 | + os.path.join(os.path.realpath(__file__), "..", "..", "..", "..", "..", "configs") |
| 74 | + ) |
| 75 | + json_config_file = find_json_file(config_args.config, config_dir) |
| 76 | + return json.load(open(json_config_file, "rb")) |
| 77 | + |
| 78 | + except FileNotFoundError: |
| 79 | + raise TVMCException( |
| 80 | + f"File {config_args.config} does not exist at {config_dir} or is wrong format." |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def parse_target_from_json(one_target, command_line_list): |
| 85 | + """parse the targets out of the json file struct |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + one_target: dict |
| 90 | + dictionary with all target's details |
| 91 | + command_line_list: list |
| 92 | + list to update with target parameters |
| 93 | + """ |
| 94 | + target_kind, *sub_type = [ |
| 95 | + one_target[key] if key == "kind" else (key, one_target[key]) for key in one_target |
| 96 | + ] |
| 97 | + |
| 98 | + internal_dict = {} |
| 99 | + if sub_type: |
| 100 | + sub_target_type = sub_type[0][0] |
| 101 | + target_value = sub_type[0][1] |
| 102 | + internal_dict[f"target_{target_kind}_{sub_target_type}"] = target_value |
| 103 | + command_line_list.append(internal_dict) |
| 104 | + |
| 105 | + return target_kind |
| 106 | + |
| 107 | + |
| 108 | +def convert_config_json_to_cli(json_params): |
| 109 | + """convert all configuration keys & values from dictionary to cli format |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + args: dictionary |
| 114 | + dictionary with all configuration keys & values. |
| 115 | +
|
| 116 | + Returns |
| 117 | + ------- |
| 118 | + int |
| 119 | + list of configuration values in cli format |
| 120 | +
|
| 121 | + """ |
| 122 | + command_line_list = [] |
| 123 | + for param_key in json_params: |
| 124 | + if param_key == "targets": |
| 125 | + target_list = [ |
| 126 | + parse_target_from_json(one_target, command_line_list) |
| 127 | + for one_target in json_params[param_key] |
| 128 | + ] |
| 129 | + |
| 130 | + internal_dict = {} |
| 131 | + internal_dict["target"] = ", ".join(map(str, target_list)) |
| 132 | + command_line_list.append(internal_dict) |
| 133 | + |
| 134 | + elif param_key in ("executor", "runtime"): |
| 135 | + for key, value in json_params[param_key].items(): |
| 136 | + if key == "kind": |
| 137 | + kind = f"{value}_" |
| 138 | + new_dict_key = param_key |
| 139 | + else: |
| 140 | + new_dict_key = f"{param_key}_{kind}{key}" |
| 141 | + |
| 142 | + internal_dict = {} |
| 143 | + internal_dict[new_dict_key.replace("-", "_")] = value |
| 144 | + command_line_list.append(internal_dict) |
| 145 | + |
| 146 | + elif isinstance(json_params[param_key], dict): |
| 147 | + internal_dict = {} |
| 148 | + modify_param_key = param_key.replace("-", "_") |
| 149 | + internal_dict[modify_param_key] = [] |
| 150 | + for key, value in json_params[param_key].items(): |
| 151 | + internal_dict[modify_param_key].append(f"{key}={value}") |
| 152 | + command_line_list.append(internal_dict) |
| 153 | + |
| 154 | + else: |
| 155 | + internal_dict = {} |
| 156 | + internal_dict[param_key.replace("-", "_")] = json_params[param_key] |
| 157 | + command_line_list.append(internal_dict) |
| 158 | + |
| 159 | + return command_line_list |
0 commit comments