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 |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 21 | import os |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 22 | import sys |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 23 | import time |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 24 | |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 25 | import flatbuffers |
| 26 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 27 | from . import architecture_features |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 28 | from . import compiler_driver |
| 29 | from . import model_reader |
Diqing Zhong | 5e5a784 | 2021-08-16 17:24:09 +0200 | [diff] [blame] | 30 | from . import rawdata_writer |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 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 |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 38 | from .errors import VelaError |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 39 | from .nn_graph import NetworkType |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 40 | from .nn_graph import PassPlacement |
| 41 | from .nn_graph import TensorAllocator |
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 |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 47 | from .tflite_model_semantic import TFLiteSemantic |
| 48 | from .tflite_supported_operators import TFLiteSupportedOperators |
| 49 | from .tosa_model_semantic import TosaSemantic |
| 50 | from .tosa_supported_operators import TosaSupportedOperators |
Louis Verhaard | 5207830 | 2020-11-18 13:35:06 +0100 | [diff] [blame] | 51 | from ethosu.vela.architecture_features import ArchitectureFeatures |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 52 | |
| 53 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 54 | 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] | 55 | if compiler_options.timing: |
| 56 | start = time.time() |
| 57 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 58 | os.makedirs(compiler_options.output_dir, exist_ok=True) |
| 59 | output_basename = os.path.join(compiler_options.output_dir, os.path.splitext(os.path.basename(input_name))[0]) |
| 60 | DebugDatabase.show_warnings = enable_debug_db |
| 61 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 62 | nng, network_type = model_reader.read_model(input_name, model_reader_options) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 63 | |
| 64 | if not nng: |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 65 | raise InputFileError(input_name, "Input file could not be read") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 66 | |
| 67 | if compiler_options.verbose_operators: |
| 68 | nng.print_operators() |
| 69 | |
| 70 | if compiler_options.timing: |
| 71 | stop = time.time() |
| 72 | print("Model reading took %f s" % (stop - start)) |
| 73 | start = time.time() |
| 74 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 75 | compiler_driver.compiler_driver(nng, arch, compiler_options, scheduler_options, network_type) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 76 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 77 | 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] | 78 | stats_writer.write_summary_metrics_csv(nng, summary_csv_file, arch) |
| 79 | |
Fredrik Svedberg | f5c07c4 | 2021-04-23 14:36:42 +0200 | [diff] [blame] | 80 | stats_writer.print_performance_metrics( |
| 81 | nng, |
| 82 | show_cpu_operations=compiler_options.show_cpu_operations, |
| 83 | verbose_weights=compiler_options.verbose_weights, |
| 84 | arch=arch, |
| 85 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 86 | |
Diqing Zhong | 5e5a784 | 2021-08-16 17:24:09 +0200 | [diff] [blame] | 87 | output_tfl_filename = output_basename + "_vela.tflite" |
Patrik Gustavsson | b081d67 | 2021-08-25 13:49:25 +0200 | [diff] [blame^] | 88 | if input_name.endswith(".tflite"): |
Diqing Zhong | 5e5a784 | 2021-08-16 17:24:09 +0200 | [diff] [blame] | 89 | tflite_writer.write_tflite(nng, output_tfl_filename) |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 90 | if input_name.endswith(".tosa"): |
Diqing Zhong | 5e5a784 | 2021-08-16 17:24:09 +0200 | [diff] [blame] | 91 | rawdata_writer.write_rawdata_output(nng, arch, output_basename) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 92 | |
| 93 | if enable_debug_db: |
Diqing Zhong | 5e5a784 | 2021-08-16 17:24:09 +0200 | [diff] [blame] | 94 | file_offsets = calculate_operator_file_offsets(output_tfl_filename) |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 95 | for idx, offset in enumerate(sorted(file_offsets)): |
| 96 | sg = find_subgraph_with_command_stream_order(nng, idx) |
| 97 | if sg is not None: |
| 98 | DebugDatabase.set_stream_offset(sg, offset) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 99 | debug_filename = output_basename + "_debug.xml" |
Diqing Zhong | 5e5a784 | 2021-08-16 17:24:09 +0200 | [diff] [blame] | 100 | DebugDatabase.write(debug_filename, input_name, output_tfl_filename) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 101 | |
| 102 | if compiler_options.timing: |
| 103 | stop = time.time() |
| 104 | print("Compiler driver took %f s" % (stop - start)) |
| 105 | |
| 106 | return nng |
| 107 | |
| 108 | |
erik.andersson@arm.com | ad45f79 | 2021-02-03 10:20:16 +0100 | [diff] [blame] | 109 | def find_subgraph_with_command_stream_order(nng, idx): |
| 110 | for sg in nng.subgraphs: |
| 111 | if sg.generated_stream_id == idx: |
| 112 | return sg |
| 113 | return None |
| 114 | |
| 115 | |
| 116 | def calculate_operator_file_offsets(name: str): |
| 117 | # Read the vela optimized tflite file |
| 118 | with open(name, "rb") as f: |
| 119 | buf = bytearray(f.read()) |
| 120 | # Calculate the file offsets for each custom operator |
| 121 | file_offsets = [] |
| 122 | model = Model.GetRootAsModel(buf, 0) |
| 123 | for idx in range(model.SubgraphsLength()): # However only one subgraph is supported as of now |
| 124 | sg = model.Subgraphs(idx) |
| 125 | for idx in range(sg.OperatorsLength()): |
| 126 | operator = sg.Operators(idx) |
| 127 | if model.OperatorCodes(operator.OpcodeIndex()).CustomCode() is not None: |
| 128 | tensor_idx = operator.Inputs(0) |
| 129 | tensor = sg.Tensors(tensor_idx) |
| 130 | buffer = model.Buffers(tensor.Buffer()) |
| 131 | offset = flatbuffers.number_types.UOffsetTFlags.py_type(buffer._tab.Offset(4)) |
| 132 | file_offsets.append(buffer._tab.Vector(offset)) |
| 133 | return file_offsets |
| 134 | |
| 135 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 136 | def print_subgraph_io_summary(nng): |
| 137 | """Print a summary of all the input and output tensor sizes for all subgraphs. |
| 138 | Also displays the total tensor size and the memory used area for sram. |
| 139 | """ |
| 140 | |
| 141 | print("Subgraph IO Summary") |
| 142 | print("-------------------") |
| 143 | print("NNG: {0}".format(nng.name)) |
| 144 | max_sg_size = 0 |
| 145 | for sg in reversed(nng.subgraphs): |
| 146 | print(" Subgraph: {0} = {1}".format(sg.name, sg.placement)) |
| 147 | sg_size = 0 |
| 148 | |
| 149 | if sg.placement == PassPlacement.Npu: |
| 150 | for tens in sg.input_tensors + [sg.scratch_tensor] + sg.output_tensors: |
| 151 | if tens in sg.input_tensors: |
| 152 | tens_dir = "In" |
| 153 | elif tens in sg.output_tensors: |
| 154 | tens_dir = "Out" |
| 155 | else: |
| 156 | tens_dir = "In/Out" |
| 157 | |
| 158 | size = tens.elements() * tens.element_size() / 1024.0 |
| 159 | sg_size = sg_size + size |
| 160 | print(" Tensor [{0}]: {1} = {2} KiB".format(tens_dir, tens.name, size)) |
| 161 | |
| 162 | print(" Total Size = {0} KiB".format(sg_size)) |
| 163 | print(" SRAM Memory Used = {0} KiB".format(sg.memory_used.get(MemArea.Sram, 0) / 1024.0)) |
| 164 | max_sg_size = max(sg_size, max_sg_size) |
| 165 | |
| 166 | print(" Maximum Subgraph Size = {0} KiB".format(max_sg_size)) |
| 167 | |
| 168 | |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 169 | def generate_supported_ops(): |
| 170 | lines = [ |
| 171 | "# Supported Ops", |
| 172 | "", |
| 173 | "This file was automatically generated by Vela using the `--supported-ops-report` parameter. ", |
| 174 | f"Vela version: `{__version__}`", |
| 175 | "", |
Michael McGeagh | 54a6111 | 2020-11-24 14:58:51 +0000 | [diff] [blame] | 176 | "This file complies with", |
| 177 | "[**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] | 178 | "", |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 179 | "Summary table of constraints for:", |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 180 | ] |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 181 | |
| 182 | for network_type in NetworkType: |
| 183 | lines += [ |
| 184 | f"- [{network_type.name}](#{network_type.name.lower()}-summary-table)", |
| 185 | ] |
| 186 | |
| 187 | for network_type in NetworkType: |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 188 | lines += [ |
| 189 | "", |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 190 | f"## {network_type.name} Summary Table", |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 191 | "", |
| 192 | ] |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 193 | if network_type == NetworkType.TFLite: |
| 194 | lines += [ |
| 195 | "The table below contains TFLite operators that can be placed on the Ethos-U NPU. ", |
| 196 | "If the constraints are not met, then that operator will be scheduled on the CPU instead. ", |
| 197 | "For any other TFLite operator not listed, will be left untouched and scheduled on the CPU. ", |
| 198 | "Please check the supported operator list for your chosen runtime for further information.", |
| 199 | "", |
| 200 | "| Operator | TFLite Constraints |", |
| 201 | "| --- | --- |", |
| 202 | ] |
| 203 | semantic_checker = TFLiteSemantic() |
| 204 | supported = TFLiteSupportedOperators() |
| 205 | elif network_type == NetworkType.TOSA: |
| 206 | lines += [ |
| 207 | "The table below contains TOSA operators that can be placed on the Ethos-U NPU. ", |
| 208 | "Note: There is limited support for compiling a TOSA neural network (EXPERIMENTAL). ", |
| 209 | "The related constraints have not yet been populated in the list.", |
| 210 | "", |
| 211 | "| Operator | TOSA Constraints |", |
| 212 | "| --- | --- |", |
| 213 | ] |
| 214 | semantic_checker = TosaSemantic() |
| 215 | supported = TosaSupportedOperators() |
| 216 | else: |
| 217 | raise ValueError |
| 218 | |
| 219 | op_constraint_links = [] |
| 220 | op_list = sorted(((op, builtin_type_name(op)) for op in builtin_operator_map), key=lambda x: x[1]) |
| 221 | for op, name in op_list: |
| 222 | internal_op = builtin_operator_map[op][0] |
| 223 | if internal_op in TFLiteSupportedOperators.supported_operators: |
| 224 | links = f"[Generic](#{network_type.name.lower()}-generic-constraints)" |
| 225 | if ( |
| 226 | internal_op in supported.specific_constraints |
| 227 | or internal_op in semantic_checker.specific_constraints |
| 228 | ): |
| 229 | links += f", [Specific](#{network_type.name.lower()}-{name.lower()}-constraints)" |
| 230 | op_constraint_links.append((internal_op, name)) |
| 231 | lines.append(f"| {name} | {links} |") |
| 232 | lines += [ |
| 233 | "", |
| 234 | f"### {network_type.name} Generic Constraints", |
| 235 | "", |
| 236 | "This is a list of constraints that all NPU operators must satisfy in order to be scheduled on the NPU.", |
| 237 | "", |
| 238 | ] |
| 239 | for constraint in semantic_checker.generic_constraints: |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 240 | # Markdown needs two spaces at the end of a line to render it as a separate line |
| 241 | reason = constraint.__doc__.replace("\n", " \n") |
| 242 | lines.append(f"- {reason}") |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 243 | for constraint in supported.generic_constraints: |
| 244 | # Markdown needs two spaces at the end of a line to render it as a separate line |
| 245 | reason = constraint.__doc__.replace("\n", " \n") |
| 246 | lines.append(f"- {reason}") |
| 247 | for op, name in op_constraint_links: |
| 248 | lines += [ |
| 249 | "", |
| 250 | f"### {network_type.name} {name} Constraints", |
| 251 | "", |
| 252 | f"This is a list of constraints that the {name} operator must satisfy in order to be scheduled on the" |
| 253 | " NPU.", |
| 254 | "", |
| 255 | ] |
| 256 | for constraint in semantic_checker.specific_constraints[op]: |
| 257 | # Markdown needs two spaces at the end of a line to render it as a separate line |
| 258 | reason = constraint.__doc__.replace("\n", " \n") |
| 259 | lines.append(f"- {reason}") |
| 260 | for constraint in supported.specific_constraints[op]: |
| 261 | # Markdown needs two spaces at the end of a line to render it as a separate line |
| 262 | reason = constraint.__doc__.replace("\n", " \n") |
| 263 | lines.append(f"- {reason}") |
Michael McGeagh | 837dc1b | 2020-11-10 12:38:25 +0000 | [diff] [blame] | 264 | |
| 265 | # Note. this will generate the file in the CWD |
| 266 | filepath = os.path.join(os.getcwd(), "SUPPORTED_OPS.md") |
| 267 | with open(filepath, "wt") as md: |
| 268 | md.writelines(line + "\n" for line in lines) |
| 269 | print(f"Report file: {filepath}") |
| 270 | |
| 271 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 272 | def main(args=None): |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 273 | try: |
| 274 | if args is None: |
| 275 | args = sys.argv[1:] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 276 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 277 | parser = argparse.ArgumentParser(prog="vela", description="Neural network model compiler for Arm Ethos-U NPUs") |
| 278 | parser.add_argument("--version", action="version", version=__version__) |
| 279 | parser.add_argument( |
| 280 | "--api-version", action="version", version=API_VERSION, help="Displays the version of the external API." |
| 281 | ) |
| 282 | parser.add_argument( |
| 283 | "--supported-ops-report", |
| 284 | action="store_true", |
| 285 | help="Generate the SUPPORTED_OPS.md file in the current working directory and exit", |
Tim Hall | b9b515c | 2020-11-01 21:27:19 +0000 | [diff] [blame] | 286 | ) |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 287 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 288 | # set network nargs to be optional to allow the support-ops-report CLI option to be used standalone |
| 289 | parser.add_argument( |
| 290 | "network", |
| 291 | metavar="NETWORK", |
| 292 | type=str, |
| 293 | default=None, |
| 294 | nargs="?", |
| 295 | help="Filename of the input TensorFlow Lite for Microcontrollers network", |
| 296 | ) |
| 297 | parser.add_argument( |
| 298 | "--output-dir", type=str, default="output", help="Output directory to write files to (default: %(default)s)" |
| 299 | ) |
| 300 | parser.add_argument( |
| 301 | "--enable-debug-db", |
| 302 | action="store_true", |
| 303 | default=None, |
| 304 | help="Enables the calculation and writing of a network debug database to output directory", |
| 305 | ) |
| 306 | parser.add_argument( |
| 307 | "--config", |
| 308 | type=str, |
| 309 | action="append", |
| 310 | help="Vela configuration file(s) in Python ConfigParser .ini file format", |
| 311 | ) |
| 312 | parser.add_argument("--verbose-all", action="store_true", help="Enable all verbose options") |
| 313 | parser.add_argument( |
| 314 | "--verbose-config", action="store_true", help="Verbose system configuration and memory mode" |
| 315 | ) |
| 316 | parser.add_argument("--verbose-graph", action="store_true", help="Verbose graph rewriter") |
| 317 | parser.add_argument("--verbose-quantization", action="store_true", help="Verbose quantization") |
| 318 | parser.add_argument("--verbose-packing", action="store_true", help="Verbose pass packing") |
| 319 | parser.add_argument("--verbose-tensor-purpose", action="store_true", help="Verbose tensor purpose") |
| 320 | parser.add_argument("--verbose-tensor-format", action="store_true", help="Verbose tensor format") |
| 321 | parser.add_argument("--verbose-schedule", action="store_true", help="Verbose schedule") |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 322 | parser.add_argument("--verbose-allocation", action="store_true", help="Verbose tensor allocation") |
| 323 | parser.add_argument( |
| 324 | "--verbose-high-level-command-stream", action="store_true", help="Verbose high level command stream" |
| 325 | ) |
| 326 | parser.add_argument( |
| 327 | "--verbose-register-command-stream", action="store_true", help="Verbose register command stream" |
| 328 | ) |
| 329 | parser.add_argument("--verbose-operators", action="store_true", help="Verbose operator list") |
Fredrik Svedberg | f5c07c4 | 2021-04-23 14:36:42 +0200 | [diff] [blame] | 330 | parser.add_argument("--verbose-weights", action="store_true", help="Verbose weights information") |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 331 | parser.add_argument( |
| 332 | "--show-cpu-operations", action="store_true", help="Show the operations that fall back to the CPU" |
| 333 | ) |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 334 | parser.add_argument("--timing", action="store_true", help="Time the compiler doing operations") |
| 335 | parser.add_argument( |
| 336 | "--accelerator-config", |
| 337 | type=str, |
| 338 | default="ethos-u55-256", |
| 339 | choices=list(architecture_features.Accelerator.member_list()), |
| 340 | help="Accelerator configuration to use (default: %(default)s)", |
| 341 | ) |
| 342 | parser.add_argument( |
| 343 | "--system-config", |
| 344 | type=str, |
| 345 | default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG, |
| 346 | help="System configuration to select from the Vela configuration file (default: %(default)s)", |
| 347 | ) |
| 348 | parser.add_argument( |
| 349 | "--memory-mode", |
| 350 | type=str, |
| 351 | default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG, |
| 352 | help="Memory mode to select from the Vela configuration file (default: %(default)s)", |
| 353 | ) |
| 354 | parser.add_argument( |
| 355 | "--tensor-allocator", |
| 356 | default=TensorAllocator.HillClimb, |
| 357 | type=lambda s: TensorAllocator[s], |
| 358 | choices=list(TensorAllocator), |
| 359 | help="Tensor Allocator algorithm (default: %(default)s)", |
| 360 | ) |
| 361 | parser.add_argument( |
| 362 | "--show-subgraph-io-summary", |
| 363 | action="store_true", |
| 364 | help="Shows a summary of all the subgraphs and their inputs and outputs", |
| 365 | ) |
| 366 | parser.add_argument( |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 367 | "--max-block-dependency", |
| 368 | type=int, |
| 369 | default=architecture_features.ArchitectureFeatures.MAX_BLOCKDEP, |
| 370 | choices=range(0, architecture_features.ArchitectureFeatures.MAX_BLOCKDEP + 1), |
| 371 | help=( |
| 372 | "Set the maximum value that can be used for the block dependency between npu kernel operations" |
| 373 | " (default: %(default)s)" |
| 374 | ), |
| 375 | ) |
| 376 | parser.add_argument( |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 377 | "--optimise", |
| 378 | type=lambda s: scheduler.OptimizationStrategy[s], |
| 379 | default=scheduler.OptimizationStrategy.Performance, |
| 380 | choices=list(scheduler.OptimizationStrategy), |
| 381 | help=( |
| 382 | "Set the optimisation strategy. The Size strategy results in minimal SRAM usage (does not use" |
| 383 | " arena-cache-size). The Performance strategy results in maximal performance (uses the arena-cache-size" |
| 384 | " if specified) (default: %(default)s)" |
| 385 | ), |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 386 | ) |
| 387 | parser.add_argument( |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 388 | "--arena-cache-size", |
| 389 | type=int, |
| 390 | help=( |
| 391 | "Set the size of the arena cache memory area, in bytes. If specified, this option overrides the memory" |
| 392 | " mode attribute with the same name in a Vela configuration file" |
| 393 | ), |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 394 | ) |
| 395 | parser.add_argument( |
| 396 | "--cpu-tensor-alignment", |
| 397 | type=int, |
| 398 | default=Tensor.AllocationQuantum, |
| 399 | help=( |
| 400 | "Controls the allocation byte alignment of cpu tensors including Ethos-U Custom" |
| 401 | " operator inputs and outputs (default: %(default)s)" |
| 402 | ), |
| 403 | ) |
Dwight Lidman | b9c9542 | 2021-08-18 19:24:14 +0200 | [diff] [blame] | 404 | parser.add_argument( |
| 405 | "--recursion-limit", |
| 406 | type=int, |
| 407 | default=1000, |
| 408 | help="Set the recursion depth limit, may result in RecursionError if too low (default: %(default)s)", |
| 409 | ) |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 410 | args = parser.parse_args(args=args) |
Louis Verhaard | 5207830 | 2020-11-18 13:35:06 +0100 | [diff] [blame] | 411 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 412 | # Generate the supported ops report and exit |
| 413 | if args.supported_ops_report: |
| 414 | generate_supported_ops() |
| 415 | return 0 |
Louis Verhaard | 5207830 | 2020-11-18 13:35:06 +0100 | [diff] [blame] | 416 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 417 | if args.network is None: |
| 418 | parser.error("the following argument is required: NETWORK") |
Michael McGeagh | 2fa40ae | 2020-12-02 10:55:04 +0000 | [diff] [blame] | 419 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 420 | # check all config files exist because they will be read as a group |
| 421 | if args.config is not None: |
| 422 | for filename in args.config: |
| 423 | if not os.access(filename, os.R_OK): |
| 424 | raise InputFileError(filename, "File not found or is not readable") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 425 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 426 | if args.cpu_tensor_alignment < 16 or args.cpu_tensor_alignment & (args.cpu_tensor_alignment - 1) != 0: |
| 427 | parser.error( |
| 428 | "Invalid argument to --cpu-tensor-alignment = {} (must be greater than or equal to 16 and a power of 2)" |
| 429 | "".format(args.cpu_tensor_alignment) |
| 430 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 431 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 432 | if args.system_config == ArchitectureFeatures.DEFAULT_CONFIG: |
| 433 | print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for system configuration") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 434 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 435 | if args.memory_mode == ArchitectureFeatures.DEFAULT_CONFIG: |
| 436 | print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for memory mode") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 437 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 438 | if args.verbose_all: |
| 439 | for v in vars(args): |
| 440 | if v.startswith("verbose") and v != "verbose_all": |
| 441 | setattr(args, v, True) |
| 442 | |
Dwight Lidman | b9c9542 | 2021-08-18 19:24:14 +0200 | [diff] [blame] | 443 | sys.setrecursionlimit(args.recursion_limit) |
| 444 | |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 445 | arch = architecture_features.ArchitectureFeatures( |
| 446 | vela_config_files=args.config, |
| 447 | system_config=args.system_config, |
| 448 | memory_mode=args.memory_mode, |
| 449 | accelerator_config=args.accelerator_config, |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 450 | max_blockdep=args.max_block_dependency, |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 451 | verbose_config=args.verbose_config, |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 452 | arena_cache_size=args.arena_cache_size, |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 453 | ) |
| 454 | |
| 455 | compiler_options = compiler_driver.CompilerOptions( |
| 456 | verbose_graph=args.verbose_graph, |
| 457 | verbose_quantization=args.verbose_quantization, |
| 458 | verbose_packing=args.verbose_packing, |
| 459 | verbose_tensor_purpose=args.verbose_tensor_purpose, |
| 460 | verbose_tensor_format=args.verbose_tensor_format, |
| 461 | verbose_allocation=args.verbose_allocation, |
| 462 | verbose_high_level_command_stream=args.verbose_high_level_command_stream, |
| 463 | verbose_register_command_stream=args.verbose_register_command_stream, |
| 464 | verbose_operators=args.verbose_operators, |
Fredrik Svedberg | f5c07c4 | 2021-04-23 14:36:42 +0200 | [diff] [blame] | 465 | verbose_weights=args.verbose_weights, |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 466 | show_cpu_operations=args.show_cpu_operations, |
| 467 | tensor_allocator=args.tensor_allocator, |
| 468 | timing=args.timing, |
| 469 | output_dir=args.output_dir, |
| 470 | cpu_tensor_alignment=args.cpu_tensor_alignment, |
| 471 | ) |
| 472 | |
| 473 | scheduler_options = scheduler.SchedulerOptions( |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 474 | optimization_strategy=args.optimise, |
| 475 | sram_target=arch.arena_cache_size, |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 476 | verbose_schedule=args.verbose_schedule, |
Henrik G Olsson | ea9b23c | 2021-03-23 17:34:49 +0100 | [diff] [blame] | 477 | ) |
| 478 | |
| 479 | model_reader_options = model_reader.ModelReaderOptions() |
| 480 | |
| 481 | nng = process( |
| 482 | args.network, args.enable_debug_db, arch, model_reader_options, compiler_options, scheduler_options |
| 483 | ) |
| 484 | |
| 485 | if args.show_subgraph_io_summary: |
| 486 | print_subgraph_io_summary(nng) |
| 487 | |
| 488 | return 0 |
| 489 | except VelaError as e: |
| 490 | print(e.data) |
| 491 | return 1 |