blob: f3e2dbac644e80bd35f919d9a0cc0758cde9b106 [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]
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200113 data_items += [nng.total_npu_encoded_weights]
Tim Hall79d07d22020-04-27 18:20:16 +0100114
Louis Verhaard0265f402020-09-29 13:57:21 +0200115 for mem_area in mem_areas:
Tim Hall79d07d22020-04-27 18:20:16 +0100116 bws = nng.bandwidths[mem_area]
117 total_bw = np.sum(bws)
118 weight_bws = bws[TensorPurpose.Weights]
119 fm_bws = bws[TensorPurpose.FeatureMap]
120 data_items += [
121 fm_bws[BandwidthDirection.Read],
122 fm_bws[BandwidthDirection.Write],
123 weight_bws[BandwidthDirection.Read],
124 weight_bws[BandwidthDirection.Write],
125 total_bw,
126 ]
127
128 data_items += [
Diqing Zhong69aadd02020-12-08 13:08:48 +0100129 nng.macs,
130 nng.macs * 2 * midpoint_fps / 1e12,
Tim Hall79d07d22020-04-27 18:20:16 +0100131 ]
132
133 data_items += [nng.cycles[kind] for kind in PassCycles.all()]
134
135 writer.writerow(data_items)
136
137
138def write_pass_metrics_csv(nng, pass_filename):
139
140 with open(pass_filename, "w") as f:
141 writer = csv.writer(f)
142
143 purpose_list = (
144 ("total", (TensorPurpose.Weights, TensorPurpose.FeatureMap)),
145 ("weights", (TensorPurpose.Weights,)),
146 ("feature_map", (TensorPurpose.FeatureMap,)),
147 )
148
149 direction_list = (
150 ("total", (BandwidthDirection.Read, BandwidthDirection.Write)),
151 ("read", (BandwidthDirection.Read,)),
152 ("write", (BandwidthDirection.Write,)),
153 )
154 bandwidth_names = []
155 bandwidth_indices = []
Louis Verhaard0265f402020-09-29 13:57:21 +0200156 for mem_area in mem_areas_to_report():
Tim Hall79d07d22020-04-27 18:20:16 +0100157 for purpose, purpose_candidates in purpose_list:
158 for direction, direction_candidates in direction_list:
Diqing Zhong42e833d2020-10-02 13:18:42 +0200159 label = "bytes_{}_{}_{}".format(mem_area.identifier_name(), purpose, direction)
Tim Hall79d07d22020-04-27 18:20:16 +0100160 bandwidth_names.append(label)
161 bandwidth_indices.append((mem_area, purpose_candidates, direction_candidates))
162
Tim Hall79d07d22020-04-27 18:20:16 +0100163 all_cycles = (
164 PassCycles.Total,
Diqing Zhong42e833d2020-10-02 13:18:42 +0200165 PassCycles.Npu,
Tim Hall79d07d22020-04-27 18:20:16 +0100166 PassCycles.SramAccess,
167 PassCycles.DramAccess,
168 PassCycles.OnChipFlashAccess,
169 PassCycles.OffChipFlashAccess,
170 )
171 writer.writerow(
172 [
173 "name",
174 "operators",
175 "placement",
176 "streaming_strategy",
177 "block_config_height",
178 "block_config_width",
179 "block_config_input_channels",
180 "block_config_output_channels",
Tim Hall79d07d22020-04-27 18:20:16 +0100181 ]
182 + ["cycles_" + v.identifier_name() for v in all_cycles]
Diqing Zhong69aadd02020-12-08 13:08:48 +0100183 + ["nn_macs"]
Tim Hall79d07d22020-04-27 18:20:16 +0100184 + bandwidth_names
185 + ["sram_used"]
186 )
187
188 def write_subgraph(sg):
189 for cps in sg.cascaded_passes:
190 if cps.placement == PassPlacement.StartupInit:
191 continue # skip the dummy init pass
192
193 for ps in cps.passes:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200194 if len(ps.ops) == 1 and ps.ops[0].type == Op.CustomNpuOp:
Tim Hall79d07d22020-04-27 18:20:16 +0100195 # just treat this as a call, unroll it
196 write_subgraph(ps.ops[0].attrs["subgraph"])
197 continue
Louis Verhaardaee5d752020-09-30 09:01:52 +0200198 stats = [ps.name, " ".join(op.type.name for op in ps.ops)]
Tim Hall79d07d22020-04-27 18:20:16 +0100199 stats += [ps.placement.name]
200 stats += [cps.strategy.name]
201 stats += list(ps.block_config)
Tim Hall79d07d22020-04-27 18:20:16 +0100202 stats += [round_up_to_int(ps.cycles[v]) for v in all_cycles]
Diqing Zhong69aadd02020-12-08 13:08:48 +0100203 stats += [round_up_to_int(ps.macs)]
Tim Hall79d07d22020-04-27 18:20:16 +0100204 for indices in bandwidth_indices:
205 res = 0
206 i = indices[0]
207 for j in indices[1]:
208 for k in indices[2]:
209 res += round_up_to_int(ps.bandwidths[i, j, k])
210 stats.append(res)
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200211 try:
212 stats += [ps.sram_used]
213 except AttributeError:
214 stats += [0]
Tim Hall79d07d22020-04-27 18:20:16 +0100215
216 writer.writerow(stats)
217
218 write_subgraph(nng.get_root_subgraph())
219
220
221def print_performance_metrics_for_strat(
222 arch,
223 name,
224 cycles,
225 macs,
226 bandwidths,
227 batch_size,
228 memory_used,
Michael McGeagh6f725262020-12-03 15:21:36 +0000229 cpu_operations=None,
Tim Hall837c31c2021-11-24 15:39:46 +0000230 npu_operations=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100231 show_cpu_operations=False,
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200232 weights_data=None,
Tim Hall79d07d22020-04-27 18:20:16 +0100233 f=sys.stdout,
234):
235
Louis Verhaard0265f402020-09-29 13:57:21 +0200236 orig_mem_areas_labels = [(v, v.display_name()) for v in mem_areas_to_report()]
Tim Hall79d07d22020-04-27 18:20:16 +0100237
Tim Hall1bd531d2020-11-01 20:59:36 +0000238 midpoint_inference_time = cycles[PassCycles.Total] / arch.core_clock
Michael McGeaghb4249742020-07-30 14:36:40 +0100239 if midpoint_inference_time > 0:
240 midpoint_fps = 1 / midpoint_inference_time
241 else:
242 midpoint_fps = np.nan
Tim Hall79d07d22020-04-27 18:20:16 +0100243
244 mem_area_labels = [
245 (mem_area, label) for mem_area, label in orig_mem_areas_labels if np.sum(bandwidths[mem_area]) > 0
246 ]
247
248 if name:
249 print("", file=f)
Diqing Zhong69aadd02020-12-08 13:08:48 +0100250 print(f"Network summary for {name}", file=f)
251 print(f"Accelerator configuration {arch.accelerator_config.name:>20}", file=f)
252 print(f"System configuration {arch.system_config:>20}", file=f)
253 print(f"Memory mode {arch.memory_mode:>20}", file=f)
254 print(f"Accelerator clock {int(arch.core_clock / 1e6):12d} MHz", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100255 for mem_area, label in mem_area_labels:
Diqing Zhong69aadd02020-12-08 13:08:48 +0100256 label += " bandwidth"
257 bandwidth = arch.memory_bandwidths_per_second[mem_area] / 1000.0 / 1000 / 1000
Tim Hall79d07d22020-04-27 18:20:16 +0100258 print(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200259 f"Design peak {label:25} {bandwidth:12.2f} GB/s",
260 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)
Tim Hall1bbd06b2022-08-25 13:38:50 +0100280 n_total_operations = max(n_cpu_operations + n_npu_operations, 1) # avoid potential divide by zero
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(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200306 f"Average {aug_label:25} {total_bw * midpoint_fps / 1000.0 / 1000.0 / 1000.0:12.2f} GB/s",
307 file=f,
Tim Hall79d07d22020-04-27 18:20:16 +0100308 )
309 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100310 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 +0100311 file=f,
312 )
Diqing Zhong69aadd02020-12-08 13:08:48 +0100313 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 +0100314 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100315 f"Output {aug_label:25} "
316 f"{np.sum(fm_bws[BandwidthDirection.Write]) / 1000.0 / 1000.0:12.2f} MB/batch",
Tim Hall79d07d22020-04-27 18:20:16 +0100317 file=f,
318 )
Diqing Zhong69aadd02020-12-08 13:08:48 +0100319 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 +0100320 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100321 f"Total {aug_label:25} per input "
322 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 +0100323 file=f,
324 )
325 print(file=f)
326
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200327 if weights_data:
328 print(f"Original Weights Size {weights_data['original'] / 1024.0:12.2f} KiB", file=f)
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200329 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(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200333 f"Neural network macs {int(macs):12d} MACs/batch",
334 file=f,
Tim Hall79d07d22020-04-27 18:20:16 +0100335 )
336 print(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200337 f"Network Tops/s {macs * 2 * midpoint_fps / 1e12:12.2f} Tops/s",
338 file=f,
Tim Hall79d07d22020-04-27 18:20:16 +0100339 )
340 print(file=f)
341
342 for kind in PassCycles.all():
343 aug_label = kind.display_name() + " cycles"
344 cyc = cycles[kind]
Diqing Zhong69aadd02020-12-08 13:08:48 +0100345 print(f"{aug_label:30} {int(cyc):12d} cycles/batch", file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100346 print(file=f)
347
348 print(
Diqing Zhong69aadd02020-12-08 13:08:48 +0100349 f"Batch Inference time {midpoint_inference_time * 1000:7.2f} ms,"
350 f" {midpoint_fps:7.2f} inferences/s (batch size {batch_size:d})",
Tim Hall79d07d22020-04-27 18:20:16 +0100351 file=f,
352 )
353 print(file=f)
354
355
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200356def print_performance_metrics(nng, arch, show_cpu_operations=False, verbose_weights=False, f=sys.stdout):
Tim Hall837c31c2021-11-24 15:39:46 +0000357 cpu_operations = []
358 npu_operations = []
359 ir_only_ops = (
360 Op.Const,
361 Op.Placeholder,
362 Op.CustomNpuOp,
363 Op.SubgraphInput,
364 )
365
366 for sg in nng.subgraphs:
367 if sg.placement == PassPlacement.Cpu:
368 for op in sg.get_all_ops():
369 if op.type not in ir_only_ops:
370 cpu_operations.append(op)
371 elif sg.placement == PassPlacement.Npu:
372 for op in sg.get_all_ops():
373 if op.type not in ir_only_ops:
374 npu_operations.append(op)
375
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200376 weights_data = (
Ayaan Masoodb801dda2022-02-22 11:28:55 +0000377 {"original": nng.total_original_weights, "npu_encoded": nng.total_npu_encoded_weights}
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200378 if verbose_weights
379 else None
380 )
Tim Hall79d07d22020-04-27 18:20:16 +0100381 return print_performance_metrics_for_strat(
382 arch,
383 nng.name,
384 nng.cycles,
385 nng.macs,
386 nng.bandwidths,
387 nng.batch_size,
388 nng.memory_used,
Tim Hall79d07d22020-04-27 18:20:16 +0100389 cpu_operations,
Tim Hall837c31c2021-11-24 15:39:46 +0000390 npu_operations,
Tim Hall79d07d22020-04-27 18:20:16 +0100391 show_cpu_operations,
Fredrik Svedbergf5c07c42021-04-23 14:36:42 +0200392 weights_data,
Tim Hall79d07d22020-04-27 18:20:16 +0100393 f,
394 )