blob: 3cd769f096b1d327d2390769e2bd2d3d58fc6e57 [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# 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
25from .npu_performance import MacCount
26from .npu_performance import PassCycles
Diego Russoea6111a2020-04-14 18:41:58 +010027from .numeric_util import round_up_to_int
Louis Verhaardaee5d752020-09-30 09:01:52 +020028from .operation import Op
Diego Russoe8a10452020-04-21 17:39:10 +010029from .tensor import MemArea
30from .tensor import TensorPurpose
Diego Russoea6111a2020-04-14 18:41:58 +010031
Tim Hall79d07d22020-04-27 18:20:16 +010032
Louis Verhaard0265f402020-09-29 13:57:21 +020033def mem_areas_to_report():
34 # Exclude SHRAM, as the SHRAM performance numbers only cover LUT usage
35 return [area for area in MemArea.all() if area != MemArea.Shram]
36
37
Tim Hall79d07d22020-04-27 18:20:16 +010038def write_summary_metrics_csv(nng, summary_filename, arch):
39 with open(summary_filename, "w") as f:
40 writer = csv.writer(f)
Louis Verhaard0265f402020-09-29 13:57:21 +020041 mem_areas = mem_areas_to_report()
Tim Hall79d07d22020-04-27 18:20:16 +010042
43 labels = [
44 "experiment",
45 "network",
46 ]
47
48 labels += (
49 ["accelerator_configuration", "system_config", "npu_clock", "sram_size"]
Louis Verhaard0265f402020-09-29 13:57:21 +020050 + [area.identifier_name() + "_bandwidth" for area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +010051 + ["weights_storage_area", "feature_map_storage_area"]
52 )
53
54 labels += [
55 "inferences_per_second",
56 "batch_size",
57 "inference_time",
58 "passes_before_fusing",
59 "passes_after_fusing",
60 ]
Louis Verhaard0265f402020-09-29 13:57:21 +020061 labels += [area.identifier_name() + "_memory_used" for area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +010062 labels += ["on_chip_flash_bits_per_element", "off_chip_flash_bits_per_element"]
63
Louis Verhaard0265f402020-09-29 13:57:21 +020064 for mem_area in mem_areas:
Tim Hall79d07d22020-04-27 18:20:16 +010065 labels += [
66 mem_area.identifier_name() + "_feature_map_read_bytes",
67 mem_area.identifier_name() + "_feature_map_write_bytes",
68 mem_area.identifier_name() + "_weight_read_bytes",
69 mem_area.identifier_name() + "_weight_write_bytes",
70 mem_area.identifier_name() + "_total_bytes",
71 ]
72
73 labels += ["nn_macs", "hardware_macs", "nn_tops", "hardware_tops"]
74
75 labels += ["cycles_" + kind.identifier_name() for kind in PassCycles.all()]
76
77 writer.writerow(labels)
78
79 data_items = [
80 "default",
81 nng.name,
82 ]
83
84 if arch:
85 data_items += (
86 [arch.accelerator_config, arch.system_config, arch.npu_clock, arch.sram_size / 1024]
Louis Verhaard0265f402020-09-29 13:57:21 +020087 + [arch.memory_bandwidths_per_second[mem_area] / 1000.0 / 1000 / 1000 for mem_area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +010088 + [
89 arch.tensor_storage_mem_area[TensorPurpose.Weights].display_name(),
90 arch.tensor_storage_mem_area[TensorPurpose.FeatureMap].display_name(),
91 ]
92 )
93
94 midpoint_inference_time = nng.cycles[PassCycles.Total] / arch.npu_clock
Michael McGeaghb4249742020-07-30 14:36:40 +010095 if midpoint_inference_time > 0:
96 midpoint_fps = 1 / midpoint_inference_time
97 else:
98 midpoint_fps = np.nan
Tim Hall79d07d22020-04-27 18:20:16 +010099
100 n_passes = sum(len(sg.passes) for sg in nng.subgraphs)
101 n_cascaded_passes = sum(len(sg.cascaded_passes) for sg in nng.subgraphs)
102
103 data_items += [midpoint_fps, nng.batch_size, midpoint_inference_time, n_passes, n_cascaded_passes]
Louis Verhaard0265f402020-09-29 13:57:21 +0200104 data_items += [nng.memory_used.get(mem_area, 0) / 1024.0 for mem_area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +0100105
106 data_items += [
107 nng.bits_per_element.get(MemArea.OnChipFlash, 0.0),
108 nng.bits_per_element.get(MemArea.OffChipFlash, 0.0),
109 ]
110
Louis Verhaard0265f402020-09-29 13:57:21 +0200111 for mem_area in mem_areas:
Tim Hall79d07d22020-04-27 18:20:16 +0100112 bws = nng.bandwidths[mem_area]
113 total_bw = np.sum(bws)
114 weight_bws = bws[TensorPurpose.Weights]
115 fm_bws = bws[TensorPurpose.FeatureMap]
116 data_items += [
117 fm_bws[BandwidthDirection.Read],
118 fm_bws[BandwidthDirection.Write],
119 weight_bws[BandwidthDirection.Read],
120 weight_bws[BandwidthDirection.Write],
121 total_bw,
122 ]
123
124 data_items += [
125 nng.macs[MacCount.NeuralNetworkMacs],
126 nng.macs[MacCount.HardwareMacs],
127 nng.macs[MacCount.NeuralNetworkMacs] * 2 * midpoint_fps / 1e12,
128 nng.macs[MacCount.HardwareMacs] * 2 * midpoint_fps / 1e12,
129 ]
130
131 data_items += [nng.cycles[kind] for kind in PassCycles.all()]
132
133 writer.writerow(data_items)
134
135
136def write_pass_metrics_csv(nng, pass_filename):
137
138 with open(pass_filename, "w") as f:
139 writer = csv.writer(f)
140
141 purpose_list = (
142 ("total", (TensorPurpose.Weights, TensorPurpose.FeatureMap)),
143 ("weights", (TensorPurpose.Weights,)),
144 ("feature_map", (TensorPurpose.FeatureMap,)),
145 )
146
147 direction_list = (
148 ("total", (BandwidthDirection.Read, BandwidthDirection.Write)),
149 ("read", (BandwidthDirection.Read,)),
150 ("write", (BandwidthDirection.Write,)),
151 )
152 bandwidth_names = []
153 bandwidth_indices = []
Louis Verhaard0265f402020-09-29 13:57:21 +0200154 for mem_area in mem_areas_to_report():
Tim Hall79d07d22020-04-27 18:20:16 +0100155 for purpose, purpose_candidates in purpose_list:
156 for direction, direction_candidates in direction_list:
Diqing Zhong42e833d2020-10-02 13:18:42 +0200157 label = "bytes_{}_{}_{}".format(mem_area.identifier_name(), purpose, direction)
Tim Hall79d07d22020-04-27 18:20:16 +0100158 bandwidth_names.append(label)
159 bandwidth_indices.append((mem_area, purpose_candidates, direction_candidates))
160
161 all_macs = MacCount.all()
162 all_cycles = (
163 PassCycles.Total,
Diqing Zhong42e833d2020-10-02 13:18:42 +0200164 PassCycles.Npu,
Tim Hall79d07d22020-04-27 18:20:16 +0100165 PassCycles.Cpu,
166 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",
181 "n_blocks_in_pass",
182 ]
183 + ["cycles_" + v.identifier_name() for v in all_cycles]
184 + [v.identifier_name() for v in all_macs]
185 + 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)
203 stats += [ps.n_blocks]
204 stats += [round_up_to_int(ps.cycles[v]) for v in all_cycles]
205 stats += [round_up_to_int(ps.macs[v]) for v in all_macs]
206 for indices in bandwidth_indices:
207 res = 0
208 i = indices[0]
209 for j in indices[1]:
210 for k in indices[2]:
211 res += round_up_to_int(ps.bandwidths[i, j, k])
212 stats.append(res)
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200213 try:
214 stats += [ps.sram_used]
215 except AttributeError:
216 stats += [0]
Tim Hall79d07d22020-04-27 18:20:16 +0100217
218 writer.writerow(stats)
219
220 write_subgraph(nng.get_root_subgraph())
221
222
223def print_performance_metrics_for_strat(
224 arch,
225 name,
226 cycles,
227 macs,
228 bandwidths,
229 batch_size,
230 memory_used,
231 num_passes,
232 num_cascaded_passes,
233 n_operations=0,
234 cpu_operations=[],
235 bits_per_element=None,
236 show_cpu_operations=False,
237 f=sys.stdout,
238):
239
Louis Verhaard0265f402020-09-29 13:57:21 +0200240 orig_mem_areas_labels = [(v, v.display_name()) for v in mem_areas_to_report()]
Tim Hall79d07d22020-04-27 18:20:16 +0100241
242 midpoint_inference_time = cycles[PassCycles.Total] / arch.npu_clock
Michael McGeaghb4249742020-07-30 14:36:40 +0100243 if midpoint_inference_time > 0:
244 midpoint_fps = 1 / midpoint_inference_time
245 else:
246 midpoint_fps = np.nan
Tim Hall79d07d22020-04-27 18:20:16 +0100247
248 mem_area_labels = [
249 (mem_area, label) for mem_area, label in orig_mem_areas_labels if np.sum(bandwidths[mem_area]) > 0
250 ]
251
252 if name:
253 print("", file=f)
254 print("Network summary for", name, file=f)
Diqing Zhong42e833d2020-10-02 13:18:42 +0200255 print("Accelerator configuration {:20}".format(arch.accelerator_config), file=f)
256 print("System configuration {:20}".format(arch.system_config), file=f)
257 print("Accelerator clock {:12d} MHz".format(int(arch.npu_clock / 1e6)), file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100258 for mem_area, label in mem_area_labels:
259 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200260 "Design peak {:25} {:12.2f} GB/s".format(
261 label + " bandwidth", arch.memory_bandwidths_per_second[mem_area] / 1000.0 / 1000 / 1000
262 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100263 file=f,
264 )
Tim Hall79d07d22020-04-27 18:20:16 +0100265 print(file=f)
266 for mem_area, label in mem_area_labels:
Diego Russoea6111a2020-04-14 18:41:58 +0100267 if mem_area not in memory_used:
Tim Hall79d07d22020-04-27 18:20:16 +0100268 continue
269
270 aug_label = label + " used"
271
272 extra = ""
273 if (mem_area == MemArea.OnChipFlash or mem_area == MemArea.OffChipFlash) and bits_per_element is not None:
Diqing Zhong42e833d2020-10-02 13:18:42 +0200274 extra = " ({:.2f} bits per element)".format(bits_per_element[mem_area])
Tim Hall79d07d22020-04-27 18:20:16 +0100275
Diqing Zhong42e833d2020-10-02 13:18:42 +0200276 print("Total {:25} {:12.2f} KiB{}".format(aug_label, memory_used[mem_area] / 1024.0, extra), file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100277
278 print(file=f)
Diqing Zhong42e833d2020-10-02 13:18:42 +0200279 print("{:d} passes fused into {:d}".format(num_passes, num_cascaded_passes), file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100280
281 n_cpu_operations = len(cpu_operations)
282 if n_operations > 0:
283 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200284 "{:d}/{:d} ({:4.1%}) operations falling back to the CPU".format(
285 n_cpu_operations, n_operations, n_cpu_operations / n_operations * 100
286 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100287 file=f,
288 )
289
290 if show_cpu_operations:
291 for op in cpu_operations:
292
293 def format_tens_list(lst):
294 return " ".join(str(list(tens.shape)) for tens in lst)
295
296 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200297 "CPU operation: {} inputs {}, outputs {}".format(
298 op.type, format_tens_list(op.inputs), format_tens_list(op.outputs)
299 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100300 file=f,
301 )
302
303 print("", file=f)
304
305 for mem_area, label in mem_area_labels:
306 bws = bandwidths[mem_area]
307 total_bw = np.sum(bws)
308 weight_bws = bws[TensorPurpose.Weights]
309 fm_bws = bws[TensorPurpose.FeatureMap]
310 aug_label = label + " bandwidth"
311 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200312 "Average {:25} {:12.2f} GB/s".format(aug_label, total_bw * midpoint_fps / 1000.0 / 1000.0 / 1000.0),
Tim Hall79d07d22020-04-27 18:20:16 +0100313 file=f,
314 )
315 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200316 "Input {:25} {:12.2f} MB/batch".format(
317 aug_label, np.sum(fm_bws[BandwidthDirection.Read]) / 1000.0 / 1000.0
318 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100319 file=f,
320 )
Diqing Zhong42e833d2020-10-02 13:18:42 +0200321 print("Weight {:25} {:12.2f} MB/batch".format(aug_label, np.sum(weight_bws) / 1000.0 / 1000.0), file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100322 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200323 "Output {:25} {:12.2f} MB/batch".format(
324 aug_label, np.sum(fm_bws[BandwidthDirection.Write]) / 1000.0 / 1000.0
325 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100326 file=f,
327 )
Diqing Zhong42e833d2020-10-02 13:18:42 +0200328 print("Total {:25} {:12.2f} MB/batch".format(aug_label, total_bw / 1000.0 / 1000.0), file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100329 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200330 "Total {:25} per input {:9.2f} MB/inference (batch size {:d})".format(
331 aug_label, total_bw / 1000.0 / 1000.0 / batch_size, batch_size
332 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100333 file=f,
334 )
335 print(file=f)
336
Tim Hall79d07d22020-04-27 18:20:16 +0100337 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200338 "Neural network macs {:12d} MACs/batch".format(int(macs[MacCount.NeuralNetworkMacs])),
339 file=f,
340 )
341 print("Hardware macs {:12d} MACs/batch".format(int(macs[MacCount.HardwareMacs])), file=f)
342 print(
343 "Network Tops/s {:12.2f} Tops/s".format(
344 macs[MacCount.NeuralNetworkMacs] * 2 * midpoint_fps / 1e12
345 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100346 file=f,
347 )
348 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200349 "Hardware Tops/s {:12.2f} Tops/s".format(
350 macs[MacCount.HardwareMacs] * 2 * midpoint_fps / 1e12
351 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100352 file=f,
353 )
354 print(file=f)
355
356 for kind in PassCycles.all():
357 aug_label = kind.display_name() + " cycles"
358 cyc = cycles[kind]
Diqing Zhong42e833d2020-10-02 13:18:42 +0200359 print("{:30} {:12d} cycles/batch".format(aug_label, int(cyc)), file=f)
Tim Hall79d07d22020-04-27 18:20:16 +0100360 print(file=f)
361
362 print(
Diqing Zhong42e833d2020-10-02 13:18:42 +0200363 "Batch Inference time {:7.2f} ms, {:7.2f} inferences/s (batch size {:d})".format(
364 midpoint_inference_time * 1000, midpoint_fps, batch_size
365 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100366 file=f,
367 )
368 print(file=f)
369
370
371def print_performance_metrics(nng, arch, show_cpu_operations=False, f=sys.stdout):
372 n_passes = sum(len(sg.passes) for sg in nng.subgraphs)
373 n_cascaded_passes = sum(len(sg.cascaded_passes) for sg in nng.subgraphs)
374 n_operations = sum(len(ps.ops) for sg in nng.subgraphs for ps in sg.passes)
375 cpu_operations = sum((ps.ops for sg in nng.subgraphs for ps in sg.passes if ps.placement == PassPlacement.Cpu), [])
376 return print_performance_metrics_for_strat(
377 arch,
378 nng.name,
379 nng.cycles,
380 nng.macs,
381 nng.bandwidths,
382 nng.batch_size,
383 nng.memory_used,
384 n_passes,
385 n_cascaded_passes,
386 n_operations,
387 cpu_operations,
388 nng.bits_per_element,
389 show_cpu_operations,
390 f,
391 )
392
393
394def write_human_friendly_metrics(nng, arch, filename):
395 f = open(filename, "w")
396 print_performance_metrics(nng, arch, f=f)