erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # Main entry point for the Vela compiler. |
| 18 | # |
| 19 | # Provides command line interface, options parsing, and network loading. Before calling the compiler driver. |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 20 | import argparse |
| 21 | import ast |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 22 | import os |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 23 | import sys |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 24 | import time |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 25 | |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 26 | import flatbuffers |
| 27 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 28 | from . import architecture_features |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 29 | from . import compiler_driver |
| 30 | from . import model_reader |
| 31 | from . import scheduler |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 32 | from . import stats_writer |
| 33 | from . import tflite_writer |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 34 | from ._version import __version__ |
Louis Verhaard | 11831ce | 2020-11-18 18:53:24 +0100 | [diff] [blame] | 35 | from .api import API_VERSION |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 36 | from .debug_database import DebugDatabase |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 37 | from .errors import InputFileError |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 38 | from .nn_graph import PassPlacement |
| 39 | from .nn_graph import TensorAllocator |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 40 | from .scheduler import ParetoMetric |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 41 | from .supported_operators import SupportedOperators |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 42 | from .tensor import MemArea |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 43 | from .tensor import Tensor |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 44 | from .tflite.Model import Model |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 45 | from .tflite_mapping import builtin_operator_map |
| 46 | from .tflite_mapping import builtin_type_name |
Louis Verhaard | 5207830 | 2020-11-18 13:35:06 +0100 | [diff] [blame] | 47 | from ethosu.vela.architecture_features import ArchitectureFeatures |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 48 | |
| 49 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 50 | def process(input_name, enable_debug_db, arch, model_reader_options, compiler_options, scheduler_options): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 51 | if compiler_options.timing: |
| 52 | start = time.time() |
| 53 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 54 | os.makedirs(compiler_options.output_dir, exist_ok=True) |
| 55 | output_basename = os.path.join(compiler_options.output_dir, os.path.splitext(os.path.basename(input_name))[0]) |
| 56 | DebugDatabase.show_warnings = enable_debug_db |
| 57 | |
| 58 | nng = model_reader.read_model(input_name, model_reader_options) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 59 | |
| 60 | if not nng: |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 61 | raise InputFileError(input_name, "Input file could not be read") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 62 | |
| 63 | if compiler_options.verbose_operators: |
| 64 | nng.print_operators() |
| 65 | |
| 66 | if compiler_options.timing: |
| 67 | stop = time.time() |
| 68 | print("Model reading took %f s" % (stop - start)) |
| 69 | start = time.time() |
| 70 | |
| 71 | compiler_driver.compiler_driver(nng, arch, compiler_options, scheduler_options) |
| 72 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 73 | passes_csv_file = "{0}_pass-breakdown_{1}.csv".format(output_basename, arch.system_config) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 74 | stats_writer.write_pass_metrics_csv(nng, passes_csv_file) |
| 75 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 76 | summary_csv_file = "{0}_summary_{1}.csv".format(output_basename, arch.system_config) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 77 | stats_writer.write_summary_metrics_csv(nng, summary_csv_file, arch) |
| 78 | |
| 79 | stats_writer.print_performance_metrics(nng, show_cpu_operations=compiler_options.show_cpu_operations, arch=arch) |
| 80 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 81 | output_filename = output_basename + "_vela.tflite" |
| 82 | if input_name.endswith(".tflite"): |
| 83 | tflite_writer.write_tflite(nng, output_filename) |
| 84 | |
| 85 | if enable_debug_db: |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 86 | file_offsets = calculate_operator_file_offsets(output_filename) |
| 87 | for idx, offset in enumerate(sorted(file_offsets)): |
| 88 | sg = find_subgraph_with_command_stream_order(nng, idx) |
| 89 | if sg is not None: |
| 90 | DebugDatabase.set_stream_offset(sg, offset) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 91 | debug_filename = output_basename + "_debug.xml" |
| 92 | DebugDatabase.write(debug_filename, input_name, output_filename) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 93 | |
| 94 | if compiler_options.timing: |
| 95 | stop = time.time() |
| 96 | print("Compiler driver took %f s" % (stop - start)) |
| 97 | |
| 98 | return nng |
| 99 | |
| 100 | |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 101 | def find_subgraph_with_command_stream_order(nng, idx): |
| 102 | for sg in nng.subgraphs: |
| 103 | if sg.generated_stream_id == idx: |
| 104 | return sg |
| 105 | return None |
| 106 | |
| 107 | |
| 108 | def calculate_operator_file_offsets(name: str): |
| 109 | # Read the vela optimized tflite file |
| 110 | with open(name, "rb") as f: |
| 111 | buf = bytearray(f.read()) |
| 112 | # Calculate the file offsets for each custom operator |
| 113 | file_offsets = [] |
| 114 | model = Model.GetRootAsModel(buf, 0) |
| 115 | for idx in range(model.SubgraphsLength()): # However only one subgraph is supported as of now |
| 116 | sg = model.Subgraphs(idx) |
| 117 | for idx in range(sg.OperatorsLength()): |
| 118 | operator = sg.Operators(idx) |
| 119 | if model.OperatorCodes(operator.OpcodeIndex()).CustomCode() is not None: |
| 120 | tensor_idx = operator.Inputs(0) |
| 121 | tensor = sg.Tensors(tensor_idx) |
| 122 | buffer = model.Buffers(tensor.Buffer()) |
| 123 | offset = flatbuffers.number_types.UOffsetTFlags.py_type(buffer._tab.Offset(4)) |
| 124 | file_offsets.append(buffer._tab.Vector(offset)) |
| 125 | return file_offsets |
| 126 | |
| 127 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 128 | def print_subgraph_io_summary(nng): |
| 129 | """Print a summary of all the input and output tensor sizes for all subgraphs. |
| 130 | Also displays the total tensor size and the memory used area for sram. |
| 131 | """ |
| 132 | |
| 133 | print("Subgraph IO Summary") |
| 134 | print("-------------------") |
| 135 | print("NNG: {0}".format(nng.name)) |
| 136 | max_sg_size = 0 |
| 137 | for sg in reversed(nng.subgraphs): |
| 138 | print(" Subgraph: {0} = {1}".format(sg.name, sg.placement)) |
| 139 | sg_size = 0 |
| 140 | |
| 141 | if sg.placement == PassPlacement.Npu: |
| 142 | for tens in sg.input_tensors + [sg.scratch_tensor] + sg.output_tensors: |
| 143 | if tens in sg.input_tensors: |
| 144 | tens_dir = "In" |
| 145 | elif tens in sg.output_tensors: |
| 146 | tens_dir = "Out" |
| 147 | else: |
| 148 | tens_dir = "In/Out" |
| 149 | |
| 150 | size = tens.elements() * tens.element_size() / 1024.0 |
| 151 | sg_size = sg_size + size |
| 152 | print(" Tensor [{0}]: {1} = {2} KiB".format(tens_dir, tens.name, size)) |
| 153 | |
| 154 | print(" Total Size = {0} KiB".format(sg_size)) |
| 155 | print(" SRAM Memory Used = {0} KiB".format(sg.memory_used.get(MemArea.Sram, 0) / 1024.0)) |
| 156 | max_sg_size = max(sg_size, max_sg_size) |
| 157 | |
| 158 | print(" Maximum Subgraph Size = {0} KiB".format(max_sg_size)) |
| 159 | |
| 160 | |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 161 | def generate_supported_ops(): |
| 162 | lines = [ |
| 163 | "# Supported Ops", |
| 164 | "", |
| 165 | "This file was automatically generated by Vela using the `--supported-ops-report` parameter. ", |
| 166 | f"Vela version: `{__version__}`", |
| 167 | "", |
Michael McGeagh | 54a6111 | 2020-11-24 14:58:51 +0000 | [diff] [blame] | 168 | "This file complies with", |
| 169 | "[**Gitiles Markdown syntax**](https://github.com/google/gitiles/blob/master/Documentation/markdown.md)", |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 170 | "", |
| 171 | "## Summary Table", |
| 172 | "", |
| 173 | "The table below contains TFLite operators that can be placed on the Ethos-U NPU. ", |
| 174 | "If the constraints are not met, then that operator will be scheduled on the CPU instead. ", |
| 175 | "For any other TFLite operator not listed, will be left untouched and scheduled on the CPU. ", |
| 176 | "Please check the supported operator list for your chosen runtime for further information.", |
| 177 | "", |
| 178 | "| Operator | Constraints |", |
Michael McGeagh | 54a6111 | 2020-11-24 14:58:51 +0000 | [diff] [blame] | 179 | "| --- | --- |", |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 180 | ] |
| 181 | supported = SupportedOperators() |
| 182 | op_constraint_links = [] |
| 183 | op_list = sorted(((op, builtin_type_name(op)) for op in builtin_operator_map), key=lambda x: x[1]) |
| 184 | for op, name in op_list: |
| 185 | internal_op = builtin_operator_map[op][0] |
| 186 | if internal_op in SupportedOperators.supported_operators: |
| 187 | links = "[Generic](#generic-constraints)" |
| 188 | if internal_op in supported.specific_constraints: |
| 189 | links += f", [Specific](#{name.lower()}-constraints)" |
| 190 | op_constraint_links.append((internal_op, name)) |
| 191 | lines.append(f"| {name} | {links} |") |
| 192 | lines += [ |
| 193 | "", |
| 194 | "## Generic Constraints", |
| 195 | "", |
| 196 | "This is a list of constraints that all NPU operators must satisfy in order to be scheduled on the NPU.", |
| 197 | "", |
| 198 | ] |
| 199 | for constraint in supported.generic_constraints: |
| 200 | # Markdown needs two spaces at the end of a line to render it as a separate line |
| 201 | reason = constraint.__doc__.replace("\n", " \n") |
| 202 | lines.append(f"- {reason}") |
| 203 | for op, name in op_constraint_links: |
| 204 | lines += [ |
| 205 | "", |
| 206 | f"## {name} Constraints", |
| 207 | "", |
| 208 | f"This is a list of constraints that the {name} operator must satisfy in order to be scheduled on the NPU.", |
| 209 | "", |
| 210 | ] |
| 211 | for constraint in supported.specific_constraints[op]: |
| 212 | # Markdown needs two spaces at the end of a line to render it as a separate line |
| 213 | reason = constraint.__doc__.replace("\n", " \n") |
| 214 | lines.append(f"- {reason}") |
| 215 | |
| 216 | # Note. this will generate the file in the CWD |
| 217 | filepath = os.path.join(os.getcwd(), "SUPPORTED_OPS.md") |
| 218 | with open(filepath, "wt") as md: |
| 219 | md.writelines(line + "\n" for line in lines) |
| 220 | print(f"Report file: {filepath}") |
| 221 | |
| 222 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 223 | def main(args=None): |
| 224 | if args is None: |
| 225 | args = sys.argv[1:] |
| 226 | |
Tim Hall | d13bd06 | 2020-11-12 23:29:25 +0000 | [diff] [blame] | 227 | parser = argparse.ArgumentParser(prog="vela", description="Neural network model compiler for Arm Ethos-U NPUs") |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 228 | parser.add_argument("--version", action="version", version=__version__) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 229 | parser.add_argument( |
Louis Verhaard | 11831ce | 2020-11-18 18:53:24 +0100 | [diff] [blame] | 230 | "--api-version", action="version", version=API_VERSION, help="Displays the version of the external API." |
Patrik Gustavsson | c8a22f1 | 2020-11-18 17:05:50 +0100 | [diff] [blame] | 231 | ) |
| 232 | parser.add_argument( |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 233 | "--supported-ops-report", |
| 234 | action="store_true", |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 235 | help="Generate the SUPPORTED_OPS.md file in the current working directory and exit", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 236 | ) |
| 237 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 238 | # set network nargs to be optional to allow the support-ops-report CLI option to be used standalone |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 239 | parser.add_argument( |
Tim Hall | d13bd06 | 2020-11-12 23:29:25 +0000 | [diff] [blame] | 240 | "network", |
| 241 | metavar="NETWORK", |
| 242 | type=str, |
| 243 | default=None, |
| 244 | nargs="?", |
| 245 | help="Filename of the input TensorFlow Lite for Microcontrollers network", |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 246 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 247 | parser.add_argument( |
| 248 | "--output-dir", type=str, default="output", help="Output directory to write files to (default: %(default)s)" |
| 249 | ) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 250 | parser.add_argument( |
| 251 | "--enable-debug-db", |
| 252 | action="store_true", |
| 253 | default=None, |
| 254 | help="Enables the calculation and writing of a network debug database to output directory", |
| 255 | ) |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 256 | parser.add_argument( |
| 257 | "--config", type=str, action="append", help="Vela configuration file(s) in Python ConfigParser .ini file format" |
| 258 | ) |
Michael McGeagh | 2fa40ae | 2020-12-02 10:55:04 +0000 | [diff] [blame] | 259 | parser.add_argument("--verbose-all", action="store_true", help="Enable all verbose options") |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 260 | parser.add_argument("--verbose-config", action="store_true", help="Verbose system configuration and memory mode") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 261 | parser.add_argument("--verbose-graph", action="store_true", help="Verbose graph rewriter") |
| 262 | parser.add_argument("--verbose-quantization", action="store_true", help="Verbose quantization") |
| 263 | parser.add_argument("--verbose-packing", action="store_true", help="Verbose pass packing") |
| 264 | parser.add_argument("--verbose-tensor-purpose", action="store_true", help="Verbose tensor purpose") |
| 265 | parser.add_argument("--verbose-tensor-format", action="store_true", help="Verbose tensor format") |
| 266 | parser.add_argument("--verbose-schedule", action="store_true", help="Verbose schedule") |
| 267 | parser.add_argument( |
| 268 | "--verbose-pareto-frontier-schedules", |
| 269 | action="store_true", |
| 270 | help="Show all schedules along the pareto frontier of optimisation criteria", |
| 271 | ) |
| 272 | parser.add_argument("--verbose-allocation", action="store_true", help="Verbose tensor allocation") |
| 273 | parser.add_argument( |
| 274 | "--verbose-high-level-command-stream", action="store_true", help="Verbose high level command stream" |
| 275 | ) |
| 276 | parser.add_argument( |
| 277 | "--verbose-register-command-stream", action="store_true", help="Verbose register command stream" |
| 278 | ) |
| 279 | parser.add_argument("--verbose-operators", action="store_true", help="Verbose operator list") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 280 | parser.add_argument( |
| 281 | "--show-cpu-operations", action="store_true", help="Show the operations that fall back to the CPU" |
| 282 | ) |
| 283 | parser.add_argument( |
Tim Hall | 14e8a20 | 2020-11-27 12:23:42 +0000 | [diff] [blame] | 284 | "--cache-bias-scale-tensor", |
| 285 | type=ast.literal_eval, |
| 286 | default=True, |
| 287 | choices=[True, False], |
| 288 | help="Controls the caching of the bias & scale tensors in SRAM (default: %(default)s)", |
Andreas Nevalainen | 897cc14 | 2020-10-28 15:42:08 +0100 | [diff] [blame] | 289 | ) |
| 290 | parser.add_argument( |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 291 | "--cascading", |
| 292 | type=ast.literal_eval, |
| 293 | default=True, |
| 294 | choices=[True, False], |
| 295 | help="Controls the packing of multiple passes into a cascade (default: %(default)s)", |
| 296 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 297 | parser.add_argument("--force-block-config", type=str, default="", help="Force a specific block configuration HxWxC") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 298 | parser.add_argument("--timing", action="store_true", help="Time the compiler doing operations") |
| 299 | parser.add_argument( |
| 300 | "--accelerator-config", |
| 301 | type=str, |
| 302 | default="ethos-u55-256", |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 303 | choices=list(architecture_features.Accelerator.member_list()), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 304 | help="Accelerator configuration to use (default: %(default)s)", |
| 305 | ) |
| 306 | parser.add_argument( |
| 307 | "--system-config", |
| 308 | type=str, |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 309 | default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG, |
| 310 | help="System configuration to select from the Vela configuration file (default: %(default)s)", |
| 311 | ) |
| 312 | parser.add_argument( |
| 313 | "--memory-mode", |
| 314 | type=str, |
| 315 | default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG, |
| 316 | help="Memory mode to select from the Vela configuration file (default: %(default)s)", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 317 | ) |
| 318 | parser.add_argument( |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 319 | "--tensor-allocator", |
Louis Verhaard | d700252 | 2021-01-20 17:23:54 +0100 | [diff] [blame^] | 320 | default=TensorAllocator.HillClimb, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 321 | type=lambda s: TensorAllocator[s], |
| 322 | choices=list(TensorAllocator), |
| 323 | help="Tensor Allocator algorithm (default: %(default)s)", |
| 324 | ) |
| 325 | parser.add_argument( |
| 326 | "--show-subgraph-io-summary", |
| 327 | action="store_true", |
| 328 | help="Shows a summary of all the subgraphs and their inputs and outputs", |
| 329 | ) |
| 330 | parser.add_argument( |
| 331 | "--ifm-streaming", |
| 332 | type=ast.literal_eval, |
| 333 | default=True, |
| 334 | choices=[True, False], |
| 335 | help="Controls scheduler IFM streaming search (default: %(default)s)", |
| 336 | ) |
| 337 | parser.add_argument( |
| 338 | "--block-config-limit", |
| 339 | type=int, |
| 340 | default=16, |
| 341 | help="Limit block config search space, use zero for unlimited (default: %(default)s)", |
| 342 | ) |
| 343 | parser.add_argument( |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 344 | "--pareto-metric", |
| 345 | default=ParetoMetric.BwCycMem, |
| 346 | type=lambda s: ParetoMetric[s], |
| 347 | choices=list(ParetoMetric), |
| 348 | help="Controls the calculation of the pareto metric (default: %(default)s)", |
| 349 | ) |
| 350 | parser.add_argument( |
| 351 | "--recursion-limit", |
| 352 | type=int, |
| 353 | default=10000, |
| 354 | help="Set the recursion depth limit, may result in RecursionError if too low (default: %(default)s)", |
| 355 | ) |
| 356 | parser.add_argument( |
| 357 | "--max-block-dependency", |
| 358 | type=int, |
| 359 | default=architecture_features.ArchitectureFeatures.MAX_BLOCKDEP, |
| 360 | choices=range(0, architecture_features.ArchitectureFeatures.MAX_BLOCKDEP + 1), |
| 361 | help=( |
Tim Hall | b9b515c | 2020-11-01 21:27:19 +0000 | [diff] [blame] | 362 | "Set the maximum value that can be used for the block dependency between npu kernel operations" |
| 363 | " (default: %(default)s)" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 364 | ), |
| 365 | ) |
Charles Xu | 7b8823f | 2020-05-29 13:53:10 +0200 | [diff] [blame] | 366 | parser.add_argument( |
| 367 | "--nhcwb16-between-cascaded-passes", |
| 368 | type=ast.literal_eval, |
| 369 | default=True, |
| 370 | choices=[True, False], |
| 371 | help="Control if NHCWB16 or NHWC should be used in between cascaded passes (default: %(default)s)", |
| 372 | ) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 373 | parser.add_argument( |
Patrik Gustavsson | 90831bc | 2020-08-24 16:26:11 +0200 | [diff] [blame] | 374 | "--weight-estimation-scaling", |
| 375 | type=float, |
| 376 | default=1.0, |
| 377 | help=("Performs an additional scaling of weight compression scale estimate (default: %(default)s)"), |
| 378 | ) |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 379 | parser.add_argument( |
Tim Hall | b9b515c | 2020-11-01 21:27:19 +0000 | [diff] [blame] | 380 | "--cpu-tensor-alignment", |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 381 | type=int, |
| 382 | default=Tensor.AllocationQuantum, |
Tim Hall | b9b515c | 2020-11-01 21:27:19 +0000 | [diff] [blame] | 383 | help=( |
| 384 | "Controls the allocation byte alignment of cpu tensors including Ethos-U Custom operator inputs and outputs" |
| 385 | " (default: %(default)s)" |
| 386 | ), |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 387 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 388 | args = parser.parse_args(args=args) |
| 389 | |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 390 | # Generate the supported ops report and exit |
| 391 | if args.supported_ops_report: |
| 392 | generate_supported_ops() |
| 393 | return 0 |
| 394 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 395 | if args.network is None: |
| 396 | parser.error("the following argument is required: NETWORK") |
| 397 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 398 | # check all config files exist because they will be read as a group |
| 399 | if args.config is not None: |
| 400 | for filename in args.config: |
| 401 | if not os.access(filename, os.R_OK): |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 402 | raise InputFileError(filename, "File not found or is not readable") |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 403 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 404 | sys.setrecursionlimit(args.recursion_limit) |
| 405 | |
| 406 | if args.force_block_config: |
| 407 | force_block_config = architecture_features.Block.from_string(args.force_block_config) |
| 408 | else: |
| 409 | force_block_config = None |
| 410 | |
Tim Hall | b9b515c | 2020-11-01 21:27:19 +0000 | [diff] [blame] | 411 | if args.cpu_tensor_alignment < 16 or args.cpu_tensor_alignment & (args.cpu_tensor_alignment - 1) != 0: |
| 412 | parser.error( |
| 413 | "Invalid argument to --cpu-tensor-alignment = {} (must be greater than or equal to 16 and a power of 2)" |
| 414 | "".format(args.cpu_tensor_alignment) |
| 415 | ) |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 416 | |
Louis Verhaard | 5207830 | 2020-11-18 13:35:06 +0100 | [diff] [blame] | 417 | if args.system_config == ArchitectureFeatures.DEFAULT_CONFIG: |
| 418 | print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for system configuration") |
| 419 | |
| 420 | if args.memory_mode == ArchitectureFeatures.DEFAULT_CONFIG: |
| 421 | print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for memory mode") |
| 422 | |
Michael McGeagh | 2fa40ae | 2020-12-02 10:55:04 +0000 | [diff] [blame] | 423 | if args.verbose_all: |
| 424 | for v in vars(args): |
| 425 | if v.startswith("verbose") and v != "verbose_all": |
| 426 | setattr(args, v, True) |
| 427 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 428 | arch = architecture_features.ArchitectureFeatures( |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 429 | vela_config_files=args.config, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 430 | system_config=args.system_config, |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 431 | memory_mode=args.memory_mode, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 432 | accelerator_config=args.accelerator_config, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 433 | override_block_config=force_block_config, |
| 434 | block_config_limit=args.block_config_limit, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 435 | max_blockdep=args.max_block_dependency, |
Patrik Gustavsson | 90831bc | 2020-08-24 16:26:11 +0200 | [diff] [blame] | 436 | weight_estimation_scaling=args.weight_estimation_scaling, |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 437 | verbose_config=args.verbose_config, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 438 | ) |
| 439 | |
| 440 | compiler_options = compiler_driver.CompilerOptions( |
| 441 | verbose_graph=args.verbose_graph, |
| 442 | verbose_quantization=args.verbose_quantization, |
| 443 | verbose_packing=args.verbose_packing, |
| 444 | verbose_tensor_purpose=args.verbose_tensor_purpose, |
| 445 | verbose_tensor_format=args.verbose_tensor_format, |
| 446 | verbose_allocation=args.verbose_allocation, |
| 447 | verbose_high_level_command_stream=args.verbose_high_level_command_stream, |
| 448 | verbose_register_command_stream=args.verbose_register_command_stream, |
| 449 | verbose_operators=args.verbose_operators, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 450 | show_cpu_operations=args.show_cpu_operations, |
| 451 | tensor_allocator=args.tensor_allocator, |
| 452 | timing=args.timing, |
| 453 | output_dir=args.output_dir, |
Tim Hall | b9b515c | 2020-11-01 21:27:19 +0000 | [diff] [blame] | 454 | cpu_tensor_alignment=args.cpu_tensor_alignment, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 455 | ) |
| 456 | |
| 457 | scheduler_options = scheduler.SchedulerOptions( |
| 458 | use_cascading=args.cascading, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 459 | verbose_schedule=args.verbose_schedule, |
| 460 | verbose_pareto_frontier_schedules=args.verbose_pareto_frontier_schedules, |
| 461 | use_ifm_streaming=args.ifm_streaming, |
| 462 | pareto_metric=args.pareto_metric, |
Charles Xu | 7b8823f | 2020-05-29 13:53:10 +0200 | [diff] [blame] | 463 | use_nhcwb16_between_cascaded_passes=args.nhcwb16_between_cascaded_passes, |
Tim Hall | 14e8a20 | 2020-11-27 12:23:42 +0000 | [diff] [blame] | 464 | cache_bias_scale_tensor=args.cache_bias_scale_tensor, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 465 | ) |
| 466 | |
Tim Hall | 284223e | 2020-06-09 13:17:21 +0100 | [diff] [blame] | 467 | model_reader_options = model_reader.ModelReaderOptions() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 468 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 469 | nng = process(args.network, args.enable_debug_db, arch, model_reader_options, compiler_options, scheduler_options) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 470 | |
| 471 | if args.show_subgraph_io_summary: |
| 472 | print_subgraph_io_summary(nng) |
| 473 | |
| 474 | return 0 |