blob: 08ab4831338c80513f1de798cd274d2f93c5ea48 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
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 Hall79d07d22020-04-27 18:20:16 +010016# 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 Russoe8a10452020-04-21 17:39:10 +010020import argparse
21import ast
Tim Hall1bd531d2020-11-01 20:59:36 +000022import os
Diego Russoea6111a2020-04-14 18:41:58 +010023import sys
Tim Hall79d07d22020-04-27 18:20:16 +010024import time
Tim Hall79d07d22020-04-27 18:20:16 +010025
26from . import architecture_features
Diego Russoe8a10452020-04-21 17:39:10 +010027from . import compiler_driver
28from . import model_reader
29from . import scheduler
Tim Hall79d07d22020-04-27 18:20:16 +010030from . import stats_writer
31from . import tflite_writer
Tim Hall79d07d22020-04-27 18:20:16 +010032from ._version import __version__
Louis Verhaard11831ce2020-11-18 18:53:24 +010033from .api import API_VERSION
Tim Halle6ccd872020-11-09 16:46:37 +000034from .debug_database import DebugDatabase
Louis Verhaard7db78962020-05-25 15:05:26 +020035from .errors import InputFileError
Diego Russoe8a10452020-04-21 17:39:10 +010036from .nn_graph import PassPlacement
37from .nn_graph import TensorAllocator
Tim Hall79d07d22020-04-27 18:20:16 +010038from .scheduler import ParetoMetric
Michael McGeagh837dc1b2020-11-10 12:38:25 +000039from .supported_operators import SupportedOperators
Diego Russoea6111a2020-04-14 18:41:58 +010040from .tensor import MemArea
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020041from .tensor import Tensor
Michael McGeagh837dc1b2020-11-10 12:38:25 +000042from .tflite_mapping import builtin_operator_map
43from .tflite_mapping import builtin_type_name
Louis Verhaard52078302020-11-18 13:35:06 +010044from ethosu.vela.architecture_features import ArchitectureFeatures
Tim Hall79d07d22020-04-27 18:20:16 +010045
46
Tim Halle6ccd872020-11-09 16:46:37 +000047def process(input_name, enable_debug_db, arch, model_reader_options, compiler_options, scheduler_options):
Tim Hall79d07d22020-04-27 18:20:16 +010048 if compiler_options.timing:
49 start = time.time()
50
Tim Halle6ccd872020-11-09 16:46:37 +000051 os.makedirs(compiler_options.output_dir, exist_ok=True)
52 output_basename = os.path.join(compiler_options.output_dir, os.path.splitext(os.path.basename(input_name))[0])
53 DebugDatabase.show_warnings = enable_debug_db
54
55 nng = model_reader.read_model(input_name, model_reader_options)
Tim Hall79d07d22020-04-27 18:20:16 +010056
57 if not nng:
Tim Halle6ccd872020-11-09 16:46:37 +000058 raise InputFileError(input_name, "input file could not be read")
Tim Hall79d07d22020-04-27 18:20:16 +010059
60 if compiler_options.verbose_operators:
61 nng.print_operators()
62
63 if compiler_options.timing:
64 stop = time.time()
65 print("Model reading took %f s" % (stop - start))
66 start = time.time()
67
68 compiler_driver.compiler_driver(nng, arch, compiler_options, scheduler_options)
69
Tim Halle6ccd872020-11-09 16:46:37 +000070 passes_csv_file = "{0}_pass-breakdown_{1}.csv".format(output_basename, arch.system_config)
Tim Hall79d07d22020-04-27 18:20:16 +010071 stats_writer.write_pass_metrics_csv(nng, passes_csv_file)
72
Tim Halle6ccd872020-11-09 16:46:37 +000073 summary_csv_file = "{0}_summary_{1}.csv".format(output_basename, arch.system_config)
Tim Hall79d07d22020-04-27 18:20:16 +010074 stats_writer.write_summary_metrics_csv(nng, summary_csv_file, arch)
75
76 stats_writer.print_performance_metrics(nng, show_cpu_operations=compiler_options.show_cpu_operations, arch=arch)
77
Tim Halle6ccd872020-11-09 16:46:37 +000078 output_filename = output_basename + "_vela.tflite"
79 if input_name.endswith(".tflite"):
80 tflite_writer.write_tflite(nng, output_filename)
81
82 if enable_debug_db:
83 debug_filename = output_basename + "_debug.xml"
84 DebugDatabase.write(debug_filename, input_name, output_filename)
Tim Hall79d07d22020-04-27 18:20:16 +010085
86 if compiler_options.timing:
87 stop = time.time()
88 print("Compiler driver took %f s" % (stop - start))
89
90 return nng
91
92
93def print_subgraph_io_summary(nng):
94 """Print a summary of all the input and output tensor sizes for all subgraphs.
95 Also displays the total tensor size and the memory used area for sram.
96 """
97
98 print("Subgraph IO Summary")
99 print("-------------------")
100 print("NNG: {0}".format(nng.name))
101 max_sg_size = 0
102 for sg in reversed(nng.subgraphs):
103 print(" Subgraph: {0} = {1}".format(sg.name, sg.placement))
104 sg_size = 0
105
106 if sg.placement == PassPlacement.Npu:
107 for tens in sg.input_tensors + [sg.scratch_tensor] + sg.output_tensors:
108 if tens in sg.input_tensors:
109 tens_dir = "In"
110 elif tens in sg.output_tensors:
111 tens_dir = "Out"
112 else:
113 tens_dir = "In/Out"
114
115 size = tens.elements() * tens.element_size() / 1024.0
116 sg_size = sg_size + size
117 print(" Tensor [{0}]: {1} = {2} KiB".format(tens_dir, tens.name, size))
118
119 print(" Total Size = {0} KiB".format(sg_size))
120 print(" SRAM Memory Used = {0} KiB".format(sg.memory_used.get(MemArea.Sram, 0) / 1024.0))
121 max_sg_size = max(sg_size, max_sg_size)
122
123 print(" Maximum Subgraph Size = {0} KiB".format(max_sg_size))
124
125
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000126def generate_supported_ops():
127 lines = [
128 "# Supported Ops",
129 "",
130 "This file was automatically generated by Vela using the `--supported-ops-report` parameter. ",
131 f"Vela version: `{__version__}`",
132 "",
Michael McGeagh54a61112020-11-24 14:58:51 +0000133 "This file complies with",
134 "[**Gitiles Markdown syntax**](https://github.com/google/gitiles/blob/master/Documentation/markdown.md)",
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000135 "",
136 "## Summary Table",
137 "",
138 "The table below contains TFLite operators that can be placed on the Ethos-U NPU. ",
139 "If the constraints are not met, then that operator will be scheduled on the CPU instead. ",
140 "For any other TFLite operator not listed, will be left untouched and scheduled on the CPU. ",
141 "Please check the supported operator list for your chosen runtime for further information.",
142 "",
143 "| Operator | Constraints |",
Michael McGeagh54a61112020-11-24 14:58:51 +0000144 "| --- | --- |",
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000145 ]
146 supported = SupportedOperators()
147 op_constraint_links = []
148 op_list = sorted(((op, builtin_type_name(op)) for op in builtin_operator_map), key=lambda x: x[1])
149 for op, name in op_list:
150 internal_op = builtin_operator_map[op][0]
151 if internal_op in SupportedOperators.supported_operators:
152 links = "[Generic](#generic-constraints)"
153 if internal_op in supported.specific_constraints:
154 links += f", [Specific](#{name.lower()}-constraints)"
155 op_constraint_links.append((internal_op, name))
156 lines.append(f"| {name} | {links} |")
157 lines += [
158 "",
159 "## Generic Constraints",
160 "",
161 "This is a list of constraints that all NPU operators must satisfy in order to be scheduled on the NPU.",
162 "",
163 ]
164 for constraint in supported.generic_constraints:
165 # Markdown needs two spaces at the end of a line to render it as a separate line
166 reason = constraint.__doc__.replace("\n", " \n")
167 lines.append(f"- {reason}")
168 for op, name in op_constraint_links:
169 lines += [
170 "",
171 f"## {name} Constraints",
172 "",
173 f"This is a list of constraints that the {name} operator must satisfy in order to be scheduled on the NPU.",
174 "",
175 ]
176 for constraint in supported.specific_constraints[op]:
177 # Markdown needs two spaces at the end of a line to render it as a separate line
178 reason = constraint.__doc__.replace("\n", " \n")
179 lines.append(f"- {reason}")
180
181 # Note. this will generate the file in the CWD
182 filepath = os.path.join(os.getcwd(), "SUPPORTED_OPS.md")
183 with open(filepath, "wt") as md:
184 md.writelines(line + "\n" for line in lines)
185 print(f"Report file: {filepath}")
186
187
Tim Hall79d07d22020-04-27 18:20:16 +0100188def main(args=None):
189 if args is None:
190 args = sys.argv[1:]
191
Tim Halld13bd062020-11-12 23:29:25 +0000192 parser = argparse.ArgumentParser(prog="vela", description="Neural network model compiler for Arm Ethos-U NPUs")
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000193 parser.add_argument("--version", action="version", version=__version__)
Tim Hall79d07d22020-04-27 18:20:16 +0100194 parser.add_argument(
Louis Verhaard11831ce2020-11-18 18:53:24 +0100195 "--api-version", action="version", version=API_VERSION, help="Displays the version of the external API."
Patrik Gustavssonc8a22f12020-11-18 17:05:50 +0100196 )
197 parser.add_argument(
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000198 "--supported-ops-report",
199 action="store_true",
Tim Hall1bd531d2020-11-01 20:59:36 +0000200 help="Generate the SUPPORTED_OPS.md file in the current working directory and exit",
Tim Hall79d07d22020-04-27 18:20:16 +0100201 )
202
Tim Hall1bd531d2020-11-01 20:59:36 +0000203 # set network nargs to be optional to allow the support-ops-report CLI option to be used standalone
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000204 parser.add_argument(
Tim Halld13bd062020-11-12 23:29:25 +0000205 "network",
206 metavar="NETWORK",
207 type=str,
208 default=None,
209 nargs="?",
210 help="Filename of the input TensorFlow Lite for Microcontrollers network",
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000211 )
Tim Hall79d07d22020-04-27 18:20:16 +0100212 parser.add_argument(
213 "--output-dir", type=str, default="output", help="Output directory to write files to (default: %(default)s)"
214 )
Tim Halle6ccd872020-11-09 16:46:37 +0000215 parser.add_argument(
216 "--enable-debug-db",
217 action="store_true",
218 default=None,
219 help="Enables the calculation and writing of a network debug database to output directory",
220 )
Tim Hall1bd531d2020-11-01 20:59:36 +0000221 parser.add_argument(
222 "--config", type=str, action="append", help="Vela configuration file(s) in Python ConfigParser .ini file format"
223 )
224 parser.add_argument("--verbose-config", action="store_true", help="Verbose system configuration and memory mode")
Tim Hall79d07d22020-04-27 18:20:16 +0100225 parser.add_argument("--verbose-graph", action="store_true", help="Verbose graph rewriter")
226 parser.add_argument("--verbose-quantization", action="store_true", help="Verbose quantization")
227 parser.add_argument("--verbose-packing", action="store_true", help="Verbose pass packing")
228 parser.add_argument("--verbose-tensor-purpose", action="store_true", help="Verbose tensor purpose")
229 parser.add_argument("--verbose-tensor-format", action="store_true", help="Verbose tensor format")
230 parser.add_argument("--verbose-schedule", action="store_true", help="Verbose schedule")
231 parser.add_argument(
232 "--verbose-pareto-frontier-schedules",
233 action="store_true",
234 help="Show all schedules along the pareto frontier of optimisation criteria",
235 )
236 parser.add_argument("--verbose-allocation", action="store_true", help="Verbose tensor allocation")
237 parser.add_argument(
238 "--verbose-high-level-command-stream", action="store_true", help="Verbose high level command stream"
239 )
240 parser.add_argument(
241 "--verbose-register-command-stream", action="store_true", help="Verbose register command stream"
242 )
243 parser.add_argument("--verbose-operators", action="store_true", help="Verbose operator list")
Tim Hall79d07d22020-04-27 18:20:16 +0100244 parser.add_argument(
245 "--show-cpu-operations", action="store_true", help="Show the operations that fall back to the CPU"
246 )
247 parser.add_argument(
Tim Hall14e8a202020-11-27 12:23:42 +0000248 "--cache-bias-scale-tensor",
249 type=ast.literal_eval,
250 default=True,
251 choices=[True, False],
252 help="Controls the caching of the bias & scale tensors in SRAM (default: %(default)s)",
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100253 )
254 parser.add_argument(
Tim Hall79d07d22020-04-27 18:20:16 +0100255 "--cascading",
256 type=ast.literal_eval,
257 default=True,
258 choices=[True, False],
259 help="Controls the packing of multiple passes into a cascade (default: %(default)s)",
260 )
Tim Hall79d07d22020-04-27 18:20:16 +0100261 parser.add_argument("--force-block-config", type=str, default="", help="Force a specific block configuration HxWxC")
Tim Hall79d07d22020-04-27 18:20:16 +0100262 parser.add_argument("--timing", action="store_true", help="Time the compiler doing operations")
263 parser.add_argument(
264 "--accelerator-config",
265 type=str,
266 default="ethos-u55-256",
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100267 choices=list(architecture_features.Accelerator.member_list()),
Tim Hall79d07d22020-04-27 18:20:16 +0100268 help="Accelerator configuration to use (default: %(default)s)",
269 )
270 parser.add_argument(
271 "--system-config",
272 type=str,
Tim Hall1bd531d2020-11-01 20:59:36 +0000273 default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG,
274 help="System configuration to select from the Vela configuration file (default: %(default)s)",
275 )
276 parser.add_argument(
277 "--memory-mode",
278 type=str,
279 default=architecture_features.ArchitectureFeatures.DEFAULT_CONFIG,
280 help="Memory mode to select from the Vela configuration file (default: %(default)s)",
Tim Hall79d07d22020-04-27 18:20:16 +0100281 )
282 parser.add_argument(
Tim Hall79d07d22020-04-27 18:20:16 +0100283 "--tensor-allocator",
284 default=TensorAllocator.Greedy,
285 type=lambda s: TensorAllocator[s],
286 choices=list(TensorAllocator),
287 help="Tensor Allocator algorithm (default: %(default)s)",
288 )
289 parser.add_argument(
290 "--show-subgraph-io-summary",
291 action="store_true",
292 help="Shows a summary of all the subgraphs and their inputs and outputs",
293 )
294 parser.add_argument(
295 "--ifm-streaming",
296 type=ast.literal_eval,
297 default=True,
298 choices=[True, False],
299 help="Controls scheduler IFM streaming search (default: %(default)s)",
300 )
301 parser.add_argument(
302 "--block-config-limit",
303 type=int,
304 default=16,
305 help="Limit block config search space, use zero for unlimited (default: %(default)s)",
306 )
307 parser.add_argument(
Tim Hall79d07d22020-04-27 18:20:16 +0100308 "--pareto-metric",
309 default=ParetoMetric.BwCycMem,
310 type=lambda s: ParetoMetric[s],
311 choices=list(ParetoMetric),
312 help="Controls the calculation of the pareto metric (default: %(default)s)",
313 )
314 parser.add_argument(
315 "--recursion-limit",
316 type=int,
317 default=10000,
318 help="Set the recursion depth limit, may result in RecursionError if too low (default: %(default)s)",
319 )
320 parser.add_argument(
321 "--max-block-dependency",
322 type=int,
323 default=architecture_features.ArchitectureFeatures.MAX_BLOCKDEP,
324 choices=range(0, architecture_features.ArchitectureFeatures.MAX_BLOCKDEP + 1),
325 help=(
Tim Hallb9b515c2020-11-01 21:27:19 +0000326 "Set the maximum value that can be used for the block dependency between npu kernel operations"
327 " (default: %(default)s)"
Tim Hall79d07d22020-04-27 18:20:16 +0100328 ),
329 )
Charles Xu7b8823f2020-05-29 13:53:10 +0200330 parser.add_argument(
331 "--nhcwb16-between-cascaded-passes",
332 type=ast.literal_eval,
333 default=True,
334 choices=[True, False],
335 help="Control if NHCWB16 or NHWC should be used in between cascaded passes (default: %(default)s)",
336 )
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200337 parser.add_argument(
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200338 "--weight-estimation-scaling",
339 type=float,
340 default=1.0,
341 help=("Performs an additional scaling of weight compression scale estimate (default: %(default)s)"),
342 )
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200343 parser.add_argument(
Tim Hallb9b515c2020-11-01 21:27:19 +0000344 "--cpu-tensor-alignment",
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200345 type=int,
346 default=Tensor.AllocationQuantum,
Tim Hallb9b515c2020-11-01 21:27:19 +0000347 help=(
348 "Controls the allocation byte alignment of cpu tensors including Ethos-U Custom operator inputs and outputs"
349 " (default: %(default)s)"
350 ),
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200351 )
Tim Hall79d07d22020-04-27 18:20:16 +0100352 args = parser.parse_args(args=args)
353
Michael McGeagh837dc1b2020-11-10 12:38:25 +0000354 # Generate the supported ops report and exit
355 if args.supported_ops_report:
356 generate_supported_ops()
357 return 0
358
Tim Hall79d07d22020-04-27 18:20:16 +0100359 if args.network is None:
360 parser.error("the following argument is required: NETWORK")
361
Tim Hall1bd531d2020-11-01 20:59:36 +0000362 # check all config files exist because they will be read as a group
363 if args.config is not None:
364 for filename in args.config:
365 if not os.access(filename, os.R_OK):
366 raise InputFileError(filename, "File not found or is not readable.")
367
Tim Hall79d07d22020-04-27 18:20:16 +0100368 sys.setrecursionlimit(args.recursion_limit)
369
370 if args.force_block_config:
371 force_block_config = architecture_features.Block.from_string(args.force_block_config)
372 else:
373 force_block_config = None
374
Tim Hallb9b515c2020-11-01 21:27:19 +0000375 if args.cpu_tensor_alignment < 16 or args.cpu_tensor_alignment & (args.cpu_tensor_alignment - 1) != 0:
376 parser.error(
377 "Invalid argument to --cpu-tensor-alignment = {} (must be greater than or equal to 16 and a power of 2)"
378 "".format(args.cpu_tensor_alignment)
379 )
Jacob Bohlin0628a8c2020-08-28 13:25:14 +0200380
Louis Verhaard52078302020-11-18 13:35:06 +0100381 if args.system_config == ArchitectureFeatures.DEFAULT_CONFIG:
382 print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for system configuration")
383
384 if args.memory_mode == ArchitectureFeatures.DEFAULT_CONFIG:
385 print(f"Warning: Using {ArchitectureFeatures.DEFAULT_CONFIG} values for memory mode")
386
Tim Hall79d07d22020-04-27 18:20:16 +0100387 arch = architecture_features.ArchitectureFeatures(
Tim Hall1bd531d2020-11-01 20:59:36 +0000388 vela_config_files=args.config,
Tim Hall79d07d22020-04-27 18:20:16 +0100389 system_config=args.system_config,
Tim Hall1bd531d2020-11-01 20:59:36 +0000390 memory_mode=args.memory_mode,
Tim Hall79d07d22020-04-27 18:20:16 +0100391 accelerator_config=args.accelerator_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100392 override_block_config=force_block_config,
393 block_config_limit=args.block_config_limit,
Tim Hall79d07d22020-04-27 18:20:16 +0100394 max_blockdep=args.max_block_dependency,
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200395 weight_estimation_scaling=args.weight_estimation_scaling,
Tim Hall1bd531d2020-11-01 20:59:36 +0000396 verbose_config=args.verbose_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100397 )
398
399 compiler_options = compiler_driver.CompilerOptions(
400 verbose_graph=args.verbose_graph,
401 verbose_quantization=args.verbose_quantization,
402 verbose_packing=args.verbose_packing,
403 verbose_tensor_purpose=args.verbose_tensor_purpose,
404 verbose_tensor_format=args.verbose_tensor_format,
405 verbose_allocation=args.verbose_allocation,
406 verbose_high_level_command_stream=args.verbose_high_level_command_stream,
407 verbose_register_command_stream=args.verbose_register_command_stream,
408 verbose_operators=args.verbose_operators,
Tim Hall79d07d22020-04-27 18:20:16 +0100409 show_cpu_operations=args.show_cpu_operations,
410 tensor_allocator=args.tensor_allocator,
411 timing=args.timing,
412 output_dir=args.output_dir,
Tim Hallb9b515c2020-11-01 21:27:19 +0000413 cpu_tensor_alignment=args.cpu_tensor_alignment,
Tim Hall79d07d22020-04-27 18:20:16 +0100414 )
415
416 scheduler_options = scheduler.SchedulerOptions(
417 use_cascading=args.cascading,
Tim Hall79d07d22020-04-27 18:20:16 +0100418 verbose_schedule=args.verbose_schedule,
419 verbose_pareto_frontier_schedules=args.verbose_pareto_frontier_schedules,
420 use_ifm_streaming=args.ifm_streaming,
421 pareto_metric=args.pareto_metric,
Charles Xu7b8823f2020-05-29 13:53:10 +0200422 use_nhcwb16_between_cascaded_passes=args.nhcwb16_between_cascaded_passes,
Tim Hall14e8a202020-11-27 12:23:42 +0000423 cache_bias_scale_tensor=args.cache_bias_scale_tensor,
Tim Hall79d07d22020-04-27 18:20:16 +0100424 )
425
Tim Hall284223e2020-06-09 13:17:21 +0100426 model_reader_options = model_reader.ModelReaderOptions()
Tim Hall79d07d22020-04-27 18:20:16 +0100427
Tim Halle6ccd872020-11-09 16:46:37 +0000428 nng = process(args.network, args.enable_debug_db, arch, model_reader_options, compiler_options, scheduler_options)
Tim Hall79d07d22020-04-27 18:20:16 +0100429
430 if args.show_subgraph_io_summary:
431 print_subgraph_io_summary(nng)
432
433 return 0