blob: c44c78949d3fae2cc913f826ec52f4866645c4cb [file] [log] [blame]
wilisa0146c94772023-02-08 09:56:14 +00001# SPDX-FileCopyrightText: Copyright 2020-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Tim Hall79d07d22020-04-27 18:20:16 +01002#
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.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Tim Hall79d07d22020-04-27 18:20:16 +010017# Description:
18# Main entry point for the Vela compiler.
19#
20# Provides command line interface, options parsing, and network loading. Before calling the compiler driver.
Diego Russoe8a10452020-04-21 17:39:10 +010021import argparse
Rickard Bolin1538dce2022-04-25 11:07:56 +000022import glob
Tim Hall1bd531d2020-11-01 20:59:36 +000023import os
Diego Russoea6111a2020-04-14 18:41:58 +010024import sys
Tim Hall79d07d22020-04-27 18:20:16 +010025import time
Tim Hall79d07d22020-04-27 18:20:16 +010026
erik.andersson@arm.comad45f792021-02-03 10:20:16 +010027import flatbuffers
28
Tim Hall79d07d22020-04-27 18:20:16 +010029from . import architecture_features
Diego Russoe8a10452020-04-21 17:39:10 +010030from . import compiler_driver
31from . import model_reader
Diqing Zhong5e5a7842021-08-16 17:24:09 +020032from . import rawdata_writer
Diego Russoe8a10452020-04-21 17:39:10 +010033from . import scheduler
Tim Hall79d07d22020-04-27 18:20:16 +010034from . import stats_writer
35from . import tflite_writer
Tim Hall79d07d22020-04-27 18:20:16 +010036from ._version import __version__
Louis Verhaard11831ce2020-11-18 18:53:24 +010037from .api import API_VERSION
Tim Halle6ccd872020-11-09 16:46:37 +000038from .debug_database import DebugDatabase
Louis Verhaard7db78962020-05-25 15:05:26 +020039from .errors import InputFileError
Henrik G Olssonea9b23c2021-03-23 17:34:49 +010040from .errors import VelaError
Tim Hallcda4fcb2022-05-19 12:36:58 +010041from .hillclimb_allocation import HillClimbAllocator
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020042from .nn_graph import NetworkType
Diego Russoe8a10452020-04-21 17:39:10 +010043from .nn_graph import TensorAllocator
Diego Russoea6111a2020-04-14 18:41:58 +010044from .tensor import MemArea
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020045from .tensor import Tensor
erik.andersson@arm.comad45f792021-02-03 10:20:16 +010046from .tflite.Model import Model
Michael McGeagh837dc1b2020-11-10 12:38:25 +000047from .tflite_mapping import builtin_operator_map
Tim Halla3fe6652022-03-03 17:43:16 +000048from .tflite_mapping import builtin_operator_name_map
Fredrik Svedberg88d5b122022-09-16 16:24:55 +020049from .tflite_mapping import optype_to_builtintype
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020050from .tflite_model_semantic import TFLiteSemantic
51from .tflite_supported_operators import TFLiteSupportedOperators
52from .tosa_model_semantic import TosaSemantic
53from .tosa_supported_operators import TosaSupportedOperators
Louis Verhaard52078302020-11-18 13:35:06 +010054from ethosu.vela.architecture_features import ArchitectureFeatures
Tim Hall79d07d22020-04-27 18:20:16 +010055
Rickard Bolin7ce6b322022-06-02 09:30:33 +000056CONFIG_FILES_PATH = os.path.normpath(os.path.join(__file__, "..", "..", "config_files"))
Rickard Bolin1538dce2022-04-25 11:07:56 +000057
Tim Hall79d07d22020-04-27 18:20:16 +010058
Tim Halle6ccd872020-11-09 16:46:37 +000059def process(input_name, enable_debug_db, arch, model_reader_options, compiler_options, scheduler_options):
Tim Hall79d07d22020-04-27 18:20:16 +010060 if compiler_options.timing:
61 start = time.time()
62
Tim Halle6ccd872020-11-09 16:46:37 +000063 os.makedirs(compiler_options.output_dir, exist_ok=True)
64 output_basename = os.path.join(compiler_options.output_dir, os.path.splitext(os.path.basename(input_name))[0])
65 DebugDatabase.show_warnings = enable_debug_db
66
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020067 nng, network_type = model_reader.read_model(input_name, model_reader_options)
Tim Hall79d07d22020-04-27 18:20:16 +010068
69 if not nng:
Michael McGeagh7a6f8432020-12-02 15:29:22 +000070 raise InputFileError(input_name, "Input file could not be read")
Tim Hall79d07d22020-04-27 18:20:16 +010071
72 if compiler_options.verbose_operators:
73 nng.print_operators()
74
75 if compiler_options.timing:
76 stop = time.time()
77 print("Model reading took %f s" % (stop - start))
78 start = time.time()
79
wilisa0189a8cdd2022-08-22 16:13:06 +000080 compiler_driver.compiler_driver(nng, arch, compiler_options, scheduler_options, network_type, output_basename)
Tim Hall79d07d22020-04-27 18:20:16 +010081
Tim Halle6ccd872020-11-09 16:46:37 +000082 summary_csv_file = "{0}_summary_{1}.csv".format(output_basename, arch.system_config)
Tim Hall79d07d22020-04-27 18:20:16 +010083 stats_writer.write_summary_metrics_csv(nng, summary_csv_file, arch)
84
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +020085 stats_writer.print_performance_metrics(
86 nng,
87 show_cpu_operations=compiler_options.show_cpu_operations,
88 verbose_weights=compiler_options.verbose_weights,
89 arch=arch,
90 )
Tim Hall79d07d22020-04-27 18:20:16 +010091
Diqing Zhong5e5a7842021-08-16 17:24:09 +020092 output_tfl_filename = output_basename + "_vela.tflite"
Patrik Gustavssonb081d672021-08-25 13:49:25 +020093 if input_name.endswith(".tflite"):
Diqing Zhong5e5a7842021-08-16 17:24:09 +020094 tflite_writer.write_tflite(nng, output_tfl_filename)
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020095 if input_name.endswith(".tosa"):
Diqing Zhong5e5a7842021-08-16 17:24:09 +020096 rawdata_writer.write_rawdata_output(nng, arch, output_basename)
Tim Halle6ccd872020-11-09 16:46:37 +000097
98 if enable_debug_db:
Diqing Zhong5e5a7842021-08-16 17:24:09 +020099 file_offsets = calculate_operator_file_offsets(output_tfl_filename)
erik.andersson@arm.comad45f792021-02-03 10:20:16 +0100100 for idx, offset in enumerate(sorted(file_offsets)):
101 sg = find_subgraph_with_command_stream_order(nng, idx)
102 if sg is not None:
103 DebugDatabase.set_stream_offset(sg, offset)
Tim Halle6ccd872020-11-09 16:46:37 +0000104 debug_filename = output_basename + "_debug.xml"
Diqing Zhong5e5a7842021-08-16 17:24:09 +0200105 DebugDatabase.write(debug_filename, input_name, output_tfl_filename)
Tim Hall79d07d22020-04-27 18:20:16 +0100106
107 if compiler_options.timing:
108 stop = time.time()
109 print("Compiler driver took %f s" % (stop - start))
110
111 return nng
112
113
erik.andersson@arm.comad45f792021-02-03 10:20:16 +0100114def find_subgraph_with_command_stream_order(nng, idx):
115 for sg in nng.subgraphs:
116 if sg.generated_stream_id == idx:
117 return sg
118 return None
119
120
121def calculate_operator_file_offsets(name: str):
122 # Read the vela optimized tflite file
123 with open(name, "rb") as f:
124 buf = bytearray(f.read())
125 # Calculate the file offsets for each custom operator
126 file_offsets = []
127 model = Model.GetRootAsModel(buf, 0)
128 for idx in range(model.SubgraphsLength()): # However only one subgraph is supported as of now
129 sg = model.Subgraphs(idx)
130 for idx in range(sg.OperatorsLength()):
131 operator = sg.Operators(idx)
132 if model.OperatorCodes(operator.OpcodeIndex()).CustomCode() is not None:
133 tensor_idx = operator.Inputs(0)
134 tensor = sg.Tensors(tensor_idx)
135 buffer = model.Buffers(tensor.Buffer())
136 offset = flatbuffers.number_types.UOffsetTFlags.py_type(buffer._tab.Offset(4))
137 file_offsets.append(buffer._tab.Vector(offset))
138 return file_offsets
139
140
Tim Hall79d07d22020-04-27 18:20:16 +0100141def print_subgraph_io_summary(nng):
142 """Print a summary of all the input and output tensor sizes for all subgraphs.
143 Also displays the total tensor size and the memory used area for sram.
144 """
145
146 print("Subgraph IO Summary")
147 print("-------------------")
James Ward93389782021-10-14 12:58:02 +0100148 print(f"NNG: {nng.name}")
Tim Hall79d07d22020-04-27 18:20:16 +0100149 max_sg_size = 0
150 for sg in reversed(nng.subgraphs):
James Ward93389782021-10-14 12:58:02 +0100151 print(f" NNG Subgraph: {sg.name} = {sg.placement}")
Tim Hall79d07d22020-04-27 18:20:16 +0100152 sg_size = 0
153
James Ward93389782021-10-14 12:58:02 +0100154 if hasattr(sg, "scratch_tensor") and sg.scratch_tensor is not None:
155 sg_tensors = sg.input_tensors + [sg.scratch_tensor] + sg.output_tensors
156 else:
157 sg_tensors = sg.input_tensors + sg.output_tensors
Tim Hall79d07d22020-04-27 18:20:16 +0100158
James Ward93389782021-10-14 12:58:02 +0100159 for tens in sg_tensors:
160 if tens in sg.input_tensors:
161 tens_dir = "In"
162 elif tens in sg.output_tensors:
163 tens_dir = "Out"
164 else:
165 tens_dir = "In/Out"
Tim Hall79d07d22020-04-27 18:20:16 +0100166
James Ward93389782021-10-14 12:58:02 +0100167 size = tens.elements() * tens.element_size() / 1024.0
168 sg_size = sg_size + size
169 print(f" Tensor [{tens_dir}]: {tens.name} = {size} KiB")
170
171 print(f" Total Size = {sg_size} KiB")
172 print(f" SRAM Memory Used = {sg.memory_used.get(MemArea.Sram, 0) / 1024.0} KiB")
Tim Hall79d07d22020-04-27 18:20:16 +0100173 max_sg_size = max(sg_size, max_sg_size)
174
James Ward93389782021-10-14 12:58:02 +0100175 print(f" Maximum NNG Subgraph Size = {max_sg_size} KiB")
Tim Hall79d07d22020-04-27 18:20:16 +0100176
177
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000178def generate_supported_ops():
Jonas Ohlsson0957e3e2021-09-01 15:57:21 +0200179 # Exclude network type from generation by adding value to exclude list.
180 # To easily exclude NetworkType from generated documentation.
181 exclude_generation_network_type_value = [NetworkType.TOSA.value]
182
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200183 def _exclude_list_names(constraint, exclude_list):
184 constraints_excluded_names = [
185 optype_to_builtintype(op) for op, exclude_constraint in exclude_list if constraint in exclude_constraint
186 ]
187 return f" - [{', '.join(sorted(constraints_excluded_names))}]" if constraints_excluded_names else ""
188
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000189 lines = [
190 "# Supported Ops",
191 "",
192 "This file was automatically generated by Vela using the `--supported-ops-report` parameter. ",
193 f"Vela version: `{__version__}`",
194 "",
Michael McGeagh54a61112020-11-24 14:58:51 +0000195 "This file complies with",
196 "[**Gitiles Markdown syntax**](https://github.com/google/gitiles/blob/master/Documentation/markdown.md)",
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000197 "",
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200198 "Summary table of constraints for:",
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000199 ]
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200200
201 for network_type in NetworkType:
Jonas Ohlsson0957e3e2021-09-01 15:57:21 +0200202 if network_type.value in exclude_generation_network_type_value:
203 continue
204
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200205 lines += [
206 f"- [{network_type.name}](#{network_type.name.lower()}-summary-table)",
207 ]
208
209 for network_type in NetworkType:
Jonas Ohlsson0957e3e2021-09-01 15:57:21 +0200210 if network_type.value in exclude_generation_network_type_value:
211 continue
212
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000213 lines += [
214 "",
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200215 f"## {network_type.name} Summary Table",
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000216 "",
217 ]
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200218 if network_type == NetworkType.TFLite:
219 lines += [
220 "The table below contains TFLite operators that can be placed on the Ethos-U NPU. ",
221 "If the constraints are not met, then that operator will be scheduled on the CPU instead. ",
222 "For any other TFLite operator not listed, will be left untouched and scheduled on the CPU. ",
223 "Please check the supported operator list for your chosen runtime for further information.",
224 "",
225 "| Operator | TFLite Constraints |",
226 "| --- | --- |",
227 ]
228 semantic_checker = TFLiteSemantic()
229 supported = TFLiteSupportedOperators()
230 elif network_type == NetworkType.TOSA:
231 lines += [
232 "The table below contains TOSA operators that can be placed on the Ethos-U NPU. ",
233 "Note: There is limited support for compiling a TOSA neural network (EXPERIMENTAL). ",
234 "The related constraints have not yet been populated in the list.",
235 "",
236 "| Operator | TOSA Constraints |",
237 "| --- | --- |",
238 ]
239 semantic_checker = TosaSemantic()
240 supported = TosaSupportedOperators()
241 else:
242 raise ValueError
243
244 op_constraint_links = []
Tim Halla3fe6652022-03-03 17:43:16 +0000245 op_list = sorted(((op, builtin_operator_name_map[op]) for op in builtin_operator_map), key=lambda x: x[1])
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200246 for op, name in op_list:
247 internal_op = builtin_operator_map[op][0]
248 if internal_op in TFLiteSupportedOperators.supported_operators:
249 links = f"[Generic](#{network_type.name.lower()}-generic-constraints)"
250 if (
251 internal_op in supported.specific_constraints
252 or internal_op in semantic_checker.specific_constraints
253 ):
254 links += f", [Specific](#{network_type.name.lower()}-{name.lower()}-constraints)"
255 op_constraint_links.append((internal_op, name))
256 lines.append(f"| {name} | {links} |")
257 lines += [
258 "",
259 f"### {network_type.name} Generic Constraints",
260 "",
Ayaan Masood4965fae2022-06-29 11:30:57 +0100261 "This is a list of constraints most NPU operators must satisfy in order to be scheduled on the NPU.",
262 "(Operators excluded from certain constraints are shown in brackets [ ] )\n" "",
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200263 ]
264 for constraint in semantic_checker.generic_constraints:
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000265 # Markdown needs two spaces at the end of a line to render it as a separate line
266 reason = constraint.__doc__.replace("\n", " \n")
Ayaan Masood4965fae2022-06-29 11:30:57 +0100267 exclude_list = TFLiteSemantic.get_generic_constraint_exclude_list().items()
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200268 lines.append(f"- {reason}{_exclude_list_names(constraint, exclude_list)}")
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200269 for constraint in supported.generic_constraints:
270 # Markdown needs two spaces at the end of a line to render it as a separate line
271 reason = constraint.__doc__.replace("\n", " \n")
Fredrik Svedberg88d5b122022-09-16 16:24:55 +0200272 exclude_list = supported.generic_constraints_exceptions.items()
273 lines.append(f"- {reason}{_exclude_list_names(constraint, exclude_list)}")
Jonas Ohlsson45e653d2021-07-26 16:13:12 +0200274 for op, name in op_constraint_links:
275 lines += [
276 "",
277 f"### {network_type.name} {name} Constraints",
278 "",
279 f"This is a list of constraints that the {name} operator must satisfy in order to be scheduled on the"
280 " NPU.",
281 "",
282 ]
283 for constraint in semantic_checker.specific_constraints[op]:
284 # Markdown needs two spaces at the end of a line to render it as a separate line
285 reason = constraint.__doc__.replace("\n", " \n")
286 lines.append(f"- {reason}")
287 for constraint in supported.specific_constraints[op]:
288 # Markdown needs two spaces at the end of a line to render it as a separate line
289 reason = constraint.__doc__.replace("\n", " \n")
290 lines.append(f"- {reason}")
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000291
292 # Note. this will generate the file in the CWD
293 filepath = os.path.join(os.getcwd(), "SUPPORTED_OPS.md")
294 with open(filepath, "wt") as md:
295 md.writelines(line + "\n" for line in lines)
296 print(f"Report file: {filepath}")
297
298
Rickard Bolin1538dce2022-04-25 11:07:56 +0000299def list_config_files():
Rickard Bolin9b8b4482022-05-24 07:43:03 +0000300 print("Available config files:")
301 path_length = len(CONFIG_FILES_PATH + os.path.sep)
Rickard Bolin1538dce2022-04-25 11:07:56 +0000302 for config in glob.glob(os.path.join(CONFIG_FILES_PATH, "*", "*.ini")):
Rickard Bolin9b8b4482022-05-24 07:43:03 +0000303 print(config[path_length:])
Rickard Bolin1538dce2022-04-25 11:07:56 +0000304
305
Tim Hall79d07d22020-04-27 18:20:16 +0100306def main(args=None):
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100307 try:
308 if args is None:
309 args = sys.argv[1:]
Tim Hall79d07d22020-04-27 18:20:16 +0100310
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100311 parser = argparse.ArgumentParser(prog="vela", description="Neural network model compiler for Arm Ethos-U NPUs")
312 parser.add_argument("--version", action="version", version=__version__)
313 parser.add_argument(
314 "--api-version", action="version", version=API_VERSION, help="Displays the version of the external API."
315 )
316 parser.add_argument(
317 "--supported-ops-report",
318 action="store_true",
319 help="Generate the SUPPORTED_OPS.md file in the current working directory and exit",
Tim Hallb9b515c2020-11-01 21:27:19 +0000320 )
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200321
Rickard Bolin1538dce2022-04-25 11:07:56 +0000322 parser.add_argument(
323 "--list-config-files",
324 action="store_true",
325 help=(
326 "Display all available configurations in the `config_files` folder and exit. To select config file, "
327 "use the --config argument with one of the listed config files (For example: --config Arm/vela.ini )"
328 ),
329 )
330
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100331 # set network nargs to be optional to allow the support-ops-report CLI option to be used standalone
332 parser.add_argument(
333 "network",
334 metavar="NETWORK",
335 type=str,
336 default=None,
337 nargs="?",
338 help="Filename of the input TensorFlow Lite for Microcontrollers network",
339 )
340 parser.add_argument(
341 "--output-dir", type=str, default="output", help="Output directory to write files to (default: %(default)s)"
342 )
343 parser.add_argument(
344 "--enable-debug-db",
345 action="store_true",
346 default=None,
347 help="Enables the calculation and writing of a network debug database to output directory",
348 )
349 parser.add_argument(
350 "--config",
351 type=str,
352 action="append",
353 help="Vela configuration file(s) in Python ConfigParser .ini file format",
354 )
355 parser.add_argument("--verbose-all", action="store_true", help="Enable all verbose options")
356 parser.add_argument(
357 "--verbose-config", action="store_true", help="Verbose system configuration and memory mode"
358 )
359 parser.add_argument("--verbose-graph", action="store_true", help="Verbose graph rewriter")
360 parser.add_argument("--verbose-quantization", action="store_true", help="Verbose quantization")
361 parser.add_argument("--verbose-packing", action="store_true", help="Verbose pass packing")
362 parser.add_argument("--verbose-tensor-purpose", action="store_true", help="Verbose tensor purpose")
363 parser.add_argument("--verbose-tensor-format", action="store_true", help="Verbose tensor format")
364 parser.add_argument("--verbose-schedule", action="store_true", help="Verbose schedule")
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100365 parser.add_argument("--verbose-allocation", action="store_true", help="Verbose tensor allocation")
366 parser.add_argument(
367 "--verbose-high-level-command-stream", action="store_true", help="Verbose high level command stream"
368 )
369 parser.add_argument(
370 "--verbose-register-command-stream", action="store_true", help="Verbose register command stream"
371 )
372 parser.add_argument("--verbose-operators", action="store_true", help="Verbose operator list")
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200373 parser.add_argument("--verbose-weights", action="store_true", help="Verbose weights information")
Tim Hallc1be0872022-03-03 17:50:52 +0000374 parser.add_argument("--verbose-performance", action="store_true", help="Verbose performance information")
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100375 parser.add_argument(
376 "--show-cpu-operations", action="store_true", help="Show the operations that fall back to the CPU"
377 )
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100378 parser.add_argument("--timing", action="store_true", help="Time the compiler doing operations")
379 parser.add_argument(
wilisa0146c94772023-02-08 09:56:14 +0000380 "--force-symmetric-int-weights",
381 action="store_true",
382 help="Forces all zero points to 0 for signed integer weights",
383 )
384 parser.add_argument(
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100385 "--accelerator-config",
386 type=str,
387 default="ethos-u55-256",
388 choices=list(architecture_features.Accelerator.member_list()),
389 help="Accelerator configuration to use (default: %(default)s)",
390 )
391 parser.add_argument(
392 "--system-config",
393 type=str,
394 default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG,
395 help="System configuration to select from the Vela configuration file (default: %(default)s)",
396 )
397 parser.add_argument(
398 "--memory-mode",
399 type=str,
400 default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG,
401 help="Memory mode to select from the Vela configuration file (default: %(default)s)",
402 )
403 parser.add_argument(
404 "--tensor-allocator",
405 default=TensorAllocator.HillClimb,
406 type=lambda s: TensorAllocator[s],
407 choices=list(TensorAllocator),
408 help="Tensor Allocator algorithm (default: %(default)s)",
409 )
410 parser.add_argument(
411 "--show-subgraph-io-summary",
412 action="store_true",
413 help="Shows a summary of all the subgraphs and their inputs and outputs",
414 )
415 parser.add_argument(
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100416 "--max-block-dependency",
417 type=int,
418 default=architecture_features.ArchitectureFeatures.MAX_BLOCKDEP,
419 choices=range(0, architecture_features.ArchitectureFeatures.MAX_BLOCKDEP + 1),
420 help=(
421 "Set the maximum value that can be used for the block dependency between npu kernel operations"
422 " (default: %(default)s)"
423 ),
424 )
425 parser.add_argument(
Tim Halld8339a72021-05-27 18:49:40 +0100426 "--optimise",
427 type=lambda s: scheduler.OptimizationStrategy[s],
428 default=scheduler.OptimizationStrategy.Performance,
429 choices=list(scheduler.OptimizationStrategy),
430 help=(
431 "Set the optimisation strategy. The Size strategy results in minimal SRAM usage (does not use"
432 " arena-cache-size). The Performance strategy results in maximal performance (uses the arena-cache-size"
433 " if specified) (default: %(default)s)"
434 ),
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100435 )
436 parser.add_argument(
Tim Halld8339a72021-05-27 18:49:40 +0100437 "--arena-cache-size",
438 type=int,
439 help=(
440 "Set the size of the arena cache memory area, in bytes. If specified, this option overrides the memory"
441 " mode attribute with the same name in a Vela configuration file"
442 ),
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100443 )
444 parser.add_argument(
445 "--cpu-tensor-alignment",
446 type=int,
447 default=Tensor.AllocationQuantum,
448 help=(
449 "Controls the allocation byte alignment of cpu tensors including Ethos-U Custom"
450 " operator inputs and outputs (default: %(default)s)"
451 ),
452 )
Dwight Lidmanb9c95422021-08-18 19:24:14 +0200453 parser.add_argument(
454 "--recursion-limit",
455 type=int,
456 default=1000,
457 help="Set the recursion depth limit, may result in RecursionError if too low (default: %(default)s)",
458 )
Tim Hallcda4fcb2022-05-19 12:36:58 +0100459 parser.add_argument(
460 "--hillclimb-max-iterations",
461 type=int,
462 default=HillClimbAllocator.MAX_ITERATIONS,
463 help=(
464 "Set the maximum number of iterations the Hill Climb tensor allocator will run (default: %(default)s)"
465 ),
466 )
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100467 args = parser.parse_args(args=args)
Louis Verhaard52078302020-11-18 13:35:06 +0100468
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100469 # Generate the supported ops report and exit
470 if args.supported_ops_report:
471 generate_supported_ops()
472 return 0
Louis Verhaard52078302020-11-18 13:35:06 +0100473
Rickard Bolin1538dce2022-04-25 11:07:56 +0000474 if args.list_config_files:
475 list_config_files()
476 return 0
477
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100478 if args.network is None:
479 parser.error("the following argument is required: NETWORK")
Michael McGeagh2fa40ae2020-12-02 10:55:04 +0000480
Rickard Bolin1538dce2022-04-25 11:07:56 +0000481 def _parse_config(config):
Rickard Bolin7ce6b322022-06-02 09:30:33 +0000482 # Make sure the correct separator is used depending on OS
483 config = os.path.normpath(config)
484
Rickard Bolin1538dce2022-04-25 11:07:56 +0000485 if not config.endswith(".ini"):
486 raise InputFileError(config, "Configuration files must use the .ini extension")
487
Rickard Bolin6d7a4f02022-05-24 14:17:58 +0000488 if (
489 len(config.split(os.path.sep)) == 2
490 and not config.startswith(os.path.sep)
491 and not config.startswith(".")
492 and not config.startswith("~")
493 ):
Rickard Bolin1538dce2022-04-25 11:07:56 +0000494 config_path = os.path.join(CONFIG_FILES_PATH, config)
495 else:
Rickard Bolin6d7a4f02022-05-24 14:17:58 +0000496 # Check if the configuration file is correctly placed inside the config_files directory
497 if os.access(os.path.join(CONFIG_FILES_PATH, *config.split(os.path.sep)[-2:]), os.R_OK):
498 rel_path = os.path.join(*config.split(os.path.sep)[-2:])
499 print(
500 f"Warning: Consider accessing the configuration by --config {rel_path} since it is located "
501 "inside the config_files directory."
502 )
Rickard Bolin1538dce2022-04-25 11:07:56 +0000503 config_path = config
504
505 if not os.access(config_path, os.R_OK):
Rickard Bolin6d7a4f02022-05-24 14:17:58 +0000506 raise InputFileError(
507 config_path,
508 "File not found or is not readable. The configuration file is either not located in a folder "
509 "directly under the `config_files` directory or its path has not been provided correctly.",
510 )
Rickard Bolin1538dce2022-04-25 11:07:56 +0000511
512 return config_path
513
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100514 # check all config files exist because they will be read as a group
Rickard Bolin1538dce2022-04-25 11:07:56 +0000515 config_files = [_parse_config(cfg) for cfg in args.config] if args.config else None
Tim Hall79d07d22020-04-27 18:20:16 +0100516
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100517 if args.cpu_tensor_alignment < 16 or args.cpu_tensor_alignment & (args.cpu_tensor_alignment - 1) != 0:
518 parser.error(
519 "Invalid argument to --cpu-tensor-alignment = {} (must be greater than or equal to 16 and a power of 2)"
520 "".format(args.cpu_tensor_alignment)
521 )
Tim Hall79d07d22020-04-27 18:20:16 +0100522
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100523 if args.system_config == ArchitectureFeatures.DEFAULT_CONFIG:
524 print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for system configuration")
Tim Hall79d07d22020-04-27 18:20:16 +0100525
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100526 if args.memory_mode == ArchitectureFeatures.DEFAULT_CONFIG:
527 print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for memory mode")
Tim Hall79d07d22020-04-27 18:20:16 +0100528
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100529 if args.verbose_all:
530 for v in vars(args):
531 if v.startswith("verbose") and v != "verbose_all":
532 setattr(args, v, True)
533
Dwight Lidmanb9c95422021-08-18 19:24:14 +0200534 sys.setrecursionlimit(args.recursion_limit)
535
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100536 arch = architecture_features.ArchitectureFeatures(
Rickard Bolin1538dce2022-04-25 11:07:56 +0000537 vela_config_files=config_files,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100538 system_config=args.system_config,
539 memory_mode=args.memory_mode,
540 accelerator_config=args.accelerator_config,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100541 max_blockdep=args.max_block_dependency,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100542 verbose_config=args.verbose_config,
Tim Halld8339a72021-05-27 18:49:40 +0100543 arena_cache_size=args.arena_cache_size,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100544 )
545
546 compiler_options = compiler_driver.CompilerOptions(
547 verbose_graph=args.verbose_graph,
548 verbose_quantization=args.verbose_quantization,
549 verbose_packing=args.verbose_packing,
550 verbose_tensor_purpose=args.verbose_tensor_purpose,
551 verbose_tensor_format=args.verbose_tensor_format,
552 verbose_allocation=args.verbose_allocation,
553 verbose_high_level_command_stream=args.verbose_high_level_command_stream,
554 verbose_register_command_stream=args.verbose_register_command_stream,
555 verbose_operators=args.verbose_operators,
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200556 verbose_weights=args.verbose_weights,
Tim Hallc1be0872022-03-03 17:50:52 +0000557 verbose_performance=args.verbose_performance,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100558 show_cpu_operations=args.show_cpu_operations,
559 tensor_allocator=args.tensor_allocator,
560 timing=args.timing,
wilisa0146c94772023-02-08 09:56:14 +0000561 force_symmetric_int_weights=args.force_symmetric_int_weights,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100562 output_dir=args.output_dir,
563 cpu_tensor_alignment=args.cpu_tensor_alignment,
Tim Hallcda4fcb2022-05-19 12:36:58 +0100564 hillclimb_max_iterations=args.hillclimb_max_iterations,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100565 )
566
567 scheduler_options = scheduler.SchedulerOptions(
Tim Halld8339a72021-05-27 18:49:40 +0100568 optimization_strategy=args.optimise,
569 sram_target=arch.arena_cache_size,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100570 verbose_schedule=args.verbose_schedule,
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100571 )
572
573 model_reader_options = model_reader.ModelReaderOptions()
574
575 nng = process(
576 args.network, args.enable_debug_db, arch, model_reader_options, compiler_options, scheduler_options
577 )
578
579 if args.show_subgraph_io_summary:
580 print_subgraph_io_summary(nng)
581
582 return 0
583 except VelaError as e:
584 print(e.data)
585 return 1