blob: 86f531a8487fe940727d61ab186f8357484fb94b [file] [log] [blame]
erik.andersson@arm.com460c6892021-02-24 14:38:09 +01001# Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved.
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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Writes out per-pass and summary performance statistics to CSV files.
Tim Hall79d07d22020-04-27 18:20:16 +010018import csv
Tim Hall79d07d22020-04-27 18:20:16 +010019import sys
20
Diego Russoea6111a2020-04-14 18:41:58 +010021import numpy as np
22
Diego Russoea6111a2020-04-14 18:41:58 +010023from .nn_graph import PassPlacement
Diego Russoe8a10452020-04-21 17:39:10 +010024from .npu_performance import BandwidthDirection
Diego Russoe8a10452020-04-21 17:39:10 +010025from .npu_performance import PassCycles
Diego Russoea6111a2020-04-14 18:41:58 +010026from .numeric_util import round_up_to_int
Louis Verhaardaee5d752020-09-30 09:01:52 +020027from .operation import Op
Diego Russoe8a10452020-04-21 17:39:10 +010028from .tensor import MemArea
29from .tensor import TensorPurpose
Diego Russoea6111a2020-04-14 18:41:58 +010030
Tim Hall79d07d22020-04-27 18:20:16 +010031
Louis Verhaard0265f402020-09-29 13:57:21 +020032def mem_areas_to_report():
33 # Exclude SHRAM, as the SHRAM performance numbers only cover LUT usage
34 return [area for area in MemArea.all() if area != MemArea.Shram]
35
36
Tim Hall79d07d22020-04-27 18:20:16 +010037def write_summary_metrics_csv(nng, summary_filename, arch):
38 with open(summary_filename, "w") as f:
39 writer = csv.writer(f)
Louis Verhaard0265f402020-09-29 13:57:21 +020040 mem_areas = mem_areas_to_report()
Tim Hall79d07d22020-04-27 18:20:16 +010041
42 labels = [
43 "experiment",
44 "network",
45 ]
46
47 labels += (
Tim Halld8339a72021-05-27 18:49:40 +010048 ["accelerator_configuration", "system_config", "memory_mode", "core_clock", "arena_cache_size"]
Louis Verhaard0265f402020-09-29 13:57:21 +020049 + [area.identifier_name() + "_bandwidth" for area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +010050 + ["weights_storage_area", "feature_map_storage_area"]
51 )
52
53 labels += [
54 "inferences_per_second",
55 "batch_size",
56 "inference_time",
57 "passes_before_fusing",
58 "passes_after_fusing",
59 ]
Louis Verhaard0265f402020-09-29 13:57:21 +020060 labels += [area.identifier_name() + "_memory_used" for area in mem_areas]
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +020061 labels += ["total_original_weights"]
62 labels += ["total_npu_weights"]
63 labels += ["total_npu_encoded_weights"]
Tim Hall79d07d22020-04-27 18:20:16 +010064
Louis Verhaard0265f402020-09-29 13:57:21 +020065 for mem_area in mem_areas:
Tim Hall79d07d22020-04-27 18:20:16 +010066 labels += [
67 mem_area.identifier_name() + "_feature_map_read_bytes",
68 mem_area.identifier_name() + "_feature_map_write_bytes",
69 mem_area.identifier_name() + "_weight_read_bytes",
70 mem_area.identifier_name() + "_weight_write_bytes",
71 mem_area.identifier_name() + "_total_bytes",
72 ]
73
Diqing Zhong69aadd02020-12-08 13:08:48 +010074 labels += ["nn_macs", "nn_tops"]
Tim Hall79d07d22020-04-27 18:20:16 +010075
76 labels += ["cycles_" + kind.identifier_name() for kind in PassCycles.all()]
77
78 writer.writerow(labels)
79
80 data_items = [
81 "default",
82 nng.name,
83 ]
84
85 if arch:
86 data_items += (
Tim Hall1bd531d2020-11-01 20:59:36 +000087 [
88 arch.accelerator_config.name,
89 arch.system_config,
90 arch.memory_mode,
91 arch.core_clock,
Tim Halld8339a72021-05-27 18:49:40 +010092 arch.arena_cache_size / 1024,
Tim Hall1bd531d2020-11-01 20:59:36 +000093 ]
Louis Verhaard0265f402020-09-29 13:57:21 +020094 + [arch.memory_bandwidths_per_second[mem_area] / 1000.0 / 1000 / 1000 for mem_area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +010095 + [
96 arch.tensor_storage_mem_area[TensorPurpose.Weights].display_name(),
97 arch.tensor_storage_mem_area[TensorPurpose.FeatureMap].display_name(),
98 ]
99 )
100
Tim Hall1bd531d2020-11-01 20:59:36 +0000101 midpoint_inference_time = nng.cycles[PassCycles.Total] / arch.core_clock
Michael McGeaghb4249742020-07-30 14:36:40 +0100102 if midpoint_inference_time > 0:
103 midpoint_fps = 1 / midpoint_inference_time
104 else:
105 midpoint_fps = np.nan
Tim Hall79d07d22020-04-27 18:20:16 +0100106
107 n_passes = sum(len(sg.passes) for sg in nng.subgraphs)
108 n_cascaded_passes = sum(len(sg.cascaded_passes) for sg in nng.subgraphs)
109
110 data_items += [midpoint_fps, nng.batch_size, midpoint_inference_time, n_passes, n_cascaded_passes]
Louis Verhaard0265f402020-09-29 13:57:21 +0200111 data_items += [nng.memory_used.get(mem_area, 0) / 1024.0 for mem_area in mem_areas]
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200112 data_items += [nng.total_original_weights]
113 data_items += [nng.total_npu_weights]
114 data_items += [nng.total_npu_encoded_weights]
Tim Hall79d07d22020-04-27 18:20:16 +0100115
Louis Verhaard0265f402020-09-29 13:57:21 +0200116 for mem_area in mem_areas:
Tim Hall79d07d22020-04-27 18:20:16 +0100117 bws = nng.bandwidths[mem_area]
118 total_bw = np.sum(bws)
119 weight_bws = bws[TensorPurpose.Weights]
120 fm_bws = bws[TensorPurpose.FeatureMap]
121 data_items += [
122 fm_bws[BandwidthDirection.Read],
123 fm_bws[BandwidthDirection.Write],
124 weight_bws[BandwidthDirection.Read],
125 weight_bws[BandwidthDirection.Write],
126 total_bw,
127 ]
128
129 data_items += [
Diqing Zhong69aadd02020-12-08 13:08:48 +0100130 nng.macs,
131 nng.macs * 2 * midpoint_fps / 1e12,
Tim Hall79d07d22020-04-27 18:20:16 +0100132 ]
133
134 data_items += [nng.cycles[kind] for kind in PassCycles.all()]
135
136 writer.writerow(data_items)
137
138
139def write_pass_metrics_csv(nng, pass_filename):
140
141 with open(pass_filename, "w") as f:
142 writer = csv.writer(f)
143
144 purpose_list = (
145 ("total", (TensorPurpose.Weights, TensorPurpose.FeatureMap)),
146 ("weights", (TensorPurpose.Weights,)),
147 ("feature_map", (TensorPurpose.FeatureMap,)),
148 )
149
150 direction_list = (
151 ("total", (BandwidthDirection.Read, BandwidthDirection.Write)),
152 ("read", (BandwidthDirection.Read,)),
153 ("write", (BandwidthDirection.Write,)),
154 )
155 bandwidth_names = []
156 bandwidth_indices = []
Louis Verhaard0265f402020-09-29 13:57:21 +0200157 for mem_area in mem_areas_to_report():
Tim Hall79d07d22020-04-27 18:20:16 +0100158 for purpose, purpose_candidates in purpose_list:
159 for direction, direction_candidates in direction_list:
Diqing Zhong42e833d2020-10-02 13:18:42 +0200160 label = "bytes_{}_{}_{}".format(mem_area.identifier_name(), purpose, direction)
Tim Hall79d07d22020-04-27 18:20:16 +0100161 bandwidth_names.append(label)
162 bandwidth_indices.append((mem_area, purpose_candidates, direction_candidates))
163
Tim Hall79d07d22020-04-27 18:20:16 +0100164 all_cycles = (
165 PassCycles.Total,
Diqing Zhong42e833d2020-10-02 13:18:42 +0200166 PassCycles.Npu,
Tim Hall79d07d22020-04-27 18:20:16 +0100167 PassCycles.SramAccess,
168 PassCycles.DramAccess,
169 PassCycles.OnChipFlashAccess,
170 PassCycles.OffChipFlashAccess,
171 )
172 writer.writerow(
173 [
174 "name",
175 "operators",
176 "placement",
177 "streaming_strategy",
178 "block_config_height",
179 "block_config_width",
180 "block_config_input_channels",
181 "block_config_output_channels",
Tim Hall79d07d22020-04-27 18:20:16 +0100182 ]
183 + ["cycles_" + v.identifier_name() for v in all_cycles]
Diqing Zhong69aadd02020-12-08 13:08:48 +0100184 + ["nn_macs"]
Tim Hall79d07d22020-04-27 18:20:16 +0100185 + bandwidth_names
186 + ["sram_used"]
187 )
188
189 def write_subgraph(sg):
190 for cps in sg.cascaded_passes:
191 if cps.placement == PassPlacement.StartupInit:
192 continue # skip the dummy init pass
193
194 for ps in cps.passes:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200195 if len(ps.ops) == 1 and ps.ops[0].type == Op.CustomNpuOp:
Tim Hall79d07d22020-04-27 18:20:16 +0100196 # just treat this as a call, unroll it
197 write_subgraph(ps.ops[0].attrs["subgraph"])
198 continue
Louis Verhaardaee5d752020-09-30 09:01:52 +0200199 stats = [ps.name, " ".join(op.type.name for op in ps.ops)]
Tim Hall79d07d22020-04-27 18:20:16 +0100200 stats += [ps.placement.name]
201 stats += [cps.strategy.name]
202 stats += list(ps.block_config)
Tim Hall79d07d22020-04-27 18:20:16 +0100203 stats += [round_up_to_int(ps.cycles[v]) for v in all_cycles]
Diqing Zhong69aadd02020-12-08 13:08:48 +0100204 stats += [round_up_to_int(ps.macs)]
Tim Hall79d07d22020-04-27 18:20:16 +0100205 for indices in bandwidth_indices:
206 res = 0
207 i = indices[0]
208 for j in indices[1]:
209 for k in indices[2]:
210 res += round_up_to_int(ps.bandwidths[i, j, k])
211 stats.append(res)
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200212 try:
213 stats += [ps.sram_used]
214 except AttributeError:
215 stats += [0]
Tim Hall79d07d22020-04-27 18:20:16 +0100216
217 writer.writerow(stats)
218
219 write_subgraph(nng.get_root_subgraph())
220
221
222def print_performance_metrics_for_strat(
223 arch,
224 name,
225 cycles,
226 macs,
227 bandwidths,
228 batch_size,
229 memory_used,
Michael McGeagh6f725262020-12-03 15:21:36 +0000230 cpu_operations=None,
Tim Hall837c31c2021-11-24 15:39:46 +0000231 npu_operations=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100232 show_cpu_operations=False,
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200233 weights_data=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100234 f=sys.stdout,
235):
236
Louis Verhaard0265f402020-09-29 13:57:21 +0200237 orig_mem_areas_labels = [(v, v.display_name()) for v in mem_areas_to_report()]
Tim Hall79d07d22020-04-27 18:20:16 +0100238
Tim Hall1bd531d2020-11-01 20:59:36 +0000239 midpoint_inference_time = cycles[PassCycles.Total] / arch.core_clock
Michael McGeaghb4249742020-07-30 14:36:40 +0100240 if midpoint_inference_time > 0:
241 midpoint_fps = 1 / midpoint_inference_time
242 else:
243 midpoint_fps = np.nan
Tim Hall79d07d22020-04-27 18:20:16 +0100244
245 mem_area_labels = [
246 (mem_area, label) for mem_area, label in orig_mem_areas_labels if np.sum(bandwidths[mem_area]) > 0
247 ]
248
249 if name:
250 print("", file=f)
Diqing Zhong69aadd02020-12-08 13:08:48 +0100251 print(f"Network summary for {name}", file=f)
252 print(f"Accelerator configuration {arch.accelerator_config.name:>20}", file=f)
253 print(f"System configuration {arch.system_config:>20}", file=f)
254 print(f"Memory mode {arch.memory_mode:>20}", file=f)
255 print(f"Accelerator clock {int(arch.core_clock / 1e6):12d} MHz", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100256 for mem_area, label in mem_area_labels:
Diqing Zhong69aadd02020-12-08 13:08:48 +0100257 label += " bandwidth"
258 bandwidth = arch.memory_bandwidths_per_second[mem_area] / 1000.0 / 1000 / 1000
Tim Hall79d07d22020-04-27 18:20:16 +0100259 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100260 f"Design peak {label:25} {bandwidth:12.2f} GB/s", file=f,
Tim Hall79d07d22020-04-27 18:20:16 +0100261 )
Tim Hall79d07d22020-04-27 18:20:16 +0100262 print(file=f)
263 for mem_area, label in mem_area_labels:
Diego Russoea6111a2020-04-14 18:41:58 +0100264 if mem_area not in memory_used:
Tim Hall79d07d22020-04-27 18:20:16 +0100265 continue
266
267 aug_label = label + " used"
268
Diqing Zhongdb5124c2021-01-11 12:52:48 +0100269 print(f"Total {aug_label:25} {memory_used[mem_area] / 1024.0:12.2f} KiB", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100270
271 print(file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100272
Michael McGeagh6f725262020-12-03 15:21:36 +0000273 if cpu_operations is None:
274 cpu_operations = []
Tim Hall837c31c2021-11-24 15:39:46 +0000275 if npu_operations is None:
276 npu_operations = []
Michael McGeagh6f725262020-12-03 15:21:36 +0000277
Tim Hall79d07d22020-04-27 18:20:16 +0100278 n_cpu_operations = len(cpu_operations)
Tim Hall837c31c2021-11-24 15:39:46 +0000279 n_npu_operations = len(npu_operations)
280 n_total_operations = n_cpu_operations + n_npu_operations
Tim Hall79d07d22020-04-27 18:20:16 +0100281
Tim Hall837c31c2021-11-24 15:39:46 +0000282 def format_tens_list(lst):
283 return " ".join(str(list(tens.shape)) for tens in lst)
Tim Hall79d07d22020-04-27 18:20:16 +0100284
Tim Hall837c31c2021-11-24 15:39:46 +0000285 for str_ops_type, n_ops, ops in (
286 ("CPU", n_cpu_operations, cpu_operations),
287 ("NPU", n_npu_operations, npu_operations),
288 ):
289 print(f"{str_ops_type} operators = {n_ops:d} ({n_ops / n_total_operations:4.1%})", file=f)
290 if show_cpu_operations:
291 for op in ops:
292 print(
293 f" {str_ops_type}: {op.type} = {op.name}"
294 f" (inputs {format_tens_list(op.inputs)}, outputs {format_tens_list(op.outputs)})"
295 )
Tim Hall79d07d22020-04-27 18:20:16 +0100296
Tim Hall837c31c2021-11-24 15:39:46 +0000297 print("", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100298
299 for mem_area, label in mem_area_labels:
300 bws = bandwidths[mem_area]
301 total_bw = np.sum(bws)
302 weight_bws = bws[TensorPurpose.Weights]
303 fm_bws = bws[TensorPurpose.FeatureMap]
304 aug_label = label + " bandwidth"
305 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100306 f"Average {aug_label:25} {total_bw * midpoint_fps / 1000.0 / 1000.0 / 1000.0:12.2f} GB/s", file=f,
Tim Hall79d07d22020-04-27 18:20:16 +0100307 )
308 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100309 f"Input {aug_label:25} {np.sum(fm_bws[BandwidthDirection.Read]) / 1000.0 / 1000.0:12.2f} MB/batch",
Tim Hall79d07d22020-04-27 18:20:16 +0100310 file=f,
311 )
Diqing Zhong69aadd02020-12-08 13:08:48 +0100312 print(f"Weight {aug_label:25} {np.sum(weight_bws) / 1000.0 / 1000.0:12.2f} MB/batch", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100313 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100314 f"Output {aug_label:25} "
315 f"{np.sum(fm_bws[BandwidthDirection.Write]) / 1000.0 / 1000.0:12.2f} MB/batch",
Tim Hall79d07d22020-04-27 18:20:16 +0100316 file=f,
317 )
Diqing Zhong69aadd02020-12-08 13:08:48 +0100318 print(f"Total {aug_label:25} {total_bw / 1000.0 / 1000.0:12.2f} MB/batch", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100319 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100320 f"Total {aug_label:25} per input "
321 f"{total_bw / 1000.0 / 1000.0 / batch_size:9.2f} MB/inference (batch size {batch_size:d})",
Tim Hall79d07d22020-04-27 18:20:16 +0100322 file=f,
323 )
324 print(file=f)
325
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200326 if weights_data:
327 print(f"Original Weights Size {weights_data['original'] / 1024.0:12.2f} KiB", file=f)
328 print(f"NPU Weights Size {weights_data['npu'] / 1024.0:12.2f} KiB", file=f)
329 print(f"NPU Encoded Weights Size {weights_data['npu_encoded'] / 1024.0:12.2f} KiB", file=f)
330 print(file=f)
Diqing Zhongdb5124c2021-01-11 12:52:48 +0100331
Tim Hall79d07d22020-04-27 18:20:16 +0100332 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100333 f"Neural network macs {int(macs):12d} MACs/batch", file=f,
Tim Hall79d07d22020-04-27 18:20:16 +0100334 )
335 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100336 f"Network Tops/s {macs * 2 * midpoint_fps / 1e12:12.2f} Tops/s", file=f,
Tim Hall79d07d22020-04-27 18:20:16 +0100337 )
338 print(file=f)
339
340 for kind in PassCycles.all():
341 aug_label = kind.display_name() + " cycles"
342 cyc = cycles[kind]
Diqing Zhong69aadd02020-12-08 13:08:48 +0100343 print(f"{aug_label:30} {int(cyc):12d} cycles/batch", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100344 print(file=f)
345
346 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100347 f"Batch Inference time {midpoint_inference_time * 1000:7.2f} ms,"
348 f" {midpoint_fps:7.2f} inferences/s (batch size {batch_size:d})",
Tim Hall79d07d22020-04-27 18:20:16 +0100349 file=f,
350 )
351 print(file=f)
352
353
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200354def print_performance_metrics(nng, arch, show_cpu_operations=False, verbose_weights=False, f=sys.stdout):
Tim Hall837c31c2021-11-24 15:39:46 +0000355 cpu_operations = []
356 npu_operations = []
357 ir_only_ops = (
358 Op.Const,
359 Op.Placeholder,
360 Op.CustomNpuOp,
361 Op.SubgraphInput,
362 )
363
364 for sg in nng.subgraphs:
365 if sg.placement == PassPlacement.Cpu:
366 for op in sg.get_all_ops():
367 if op.type not in ir_only_ops:
368 cpu_operations.append(op)
369 elif sg.placement == PassPlacement.Npu:
370 for op in sg.get_all_ops():
371 if op.type not in ir_only_ops:
372 npu_operations.append(op)
373
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200374 weights_data = (
375 {
376 "original": nng.total_original_weights,
377 "npu": nng.total_npu_weights,
378 "npu_encoded": nng.total_npu_encoded_weights,
379 }
380 if verbose_weights
381 else None
382 )
Tim Hall79d07d22020-04-27 18:20:16 +0100383 return print_performance_metrics_for_strat(
384 arch,
385 nng.name,
386 nng.cycles,
387 nng.macs,
388 nng.bandwidths,
389 nng.batch_size,
390 nng.memory_used,
Tim Hall79d07d22020-04-27 18:20:16 +0100391 cpu_operations,
Tim Hall837c31c2021-11-24 15:39:46 +0000392 npu_operations,
Tim Hall79d07d22020-04-27 18:20:16 +0100393 show_cpu_operations,
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200394 weights_data,
Tim Hall79d07d22020-04-27 18:20:16 +0100395 f,
396 )