blob: 2ea14f2be82a33b30ce05d1f635a4aa5a9acb8a7 [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
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 += (
48 ["accelerator_configuration", "system_config", "npu_clock", "sram_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]
Tim Hall79d07d22020-04-27 18:20:16 +010061 labels += ["on_chip_flash_bits_per_element", "off_chip_flash_bits_per_element"]
62
Louis Verhaard0265f402020-09-29 13:57:21 +020063 for mem_area in mem_areas:
Tim Hall79d07d22020-04-27 18:20:16 +010064 labels += [
65 mem_area.identifier_name() + "_feature_map_read_bytes",
66 mem_area.identifier_name() + "_feature_map_write_bytes",
67 mem_area.identifier_name() + "_weight_read_bytes",
68 mem_area.identifier_name() + "_weight_write_bytes",
69 mem_area.identifier_name() + "_total_bytes",
70 ]
71
72 labels += ["nn_macs", "hardware_macs", "nn_tops", "hardware_tops"]
73
74 labels += ["cycles_" + kind.identifier_name() for kind in PassCycles.all()]
75
76 writer.writerow(labels)
77
78 data_items = [
79 "default",
80 nng.name,
81 ]
82
83 if arch:
84 data_items += (
85 [arch.accelerator_config, arch.system_config, arch.npu_clock, arch.sram_size / 1024]
Louis Verhaard0265f402020-09-29 13:57:21 +020086 + [arch.memory_bandwidths_per_second[mem_area] / 1000.0 / 1000 / 1000 for mem_area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +010087 + [
88 arch.tensor_storage_mem_area[TensorPurpose.Weights].display_name(),
89 arch.tensor_storage_mem_area[TensorPurpose.FeatureMap].display_name(),
90 ]
91 )
92
93 midpoint_inference_time = nng.cycles[PassCycles.Total] / arch.npu_clock
Michael McGeaghb4249742020-07-30 14:36:40 +010094 if midpoint_inference_time > 0:
95 midpoint_fps = 1 / midpoint_inference_time
96 else:
97 midpoint_fps = np.nan
Tim Hall79d07d22020-04-27 18:20:16 +010098
99 n_passes = sum(len(sg.passes) for sg in nng.subgraphs)
100 n_cascaded_passes = sum(len(sg.cascaded_passes) for sg in nng.subgraphs)
101
102 data_items += [midpoint_fps, nng.batch_size, midpoint_inference_time, n_passes, n_cascaded_passes]
Louis Verhaard0265f402020-09-29 13:57:21 +0200103 data_items += [nng.memory_used.get(mem_area, 0) / 1024.0 for mem_area in mem_areas]
Tim Hall79d07d22020-04-27 18:20:16 +0100104
105 data_items += [
106 nng.bits_per_element.get(MemArea.OnChipFlash, 0.0),
107 nng.bits_per_element.get(MemArea.OffChipFlash, 0.0),
108 ]
109
Louis Verhaard0265f402020-09-29 13:57:21 +0200110 for mem_area in mem_areas:
Tim Hall79d07d22020-04-27 18:20:16 +0100111 bws = nng.bandwidths[mem_area]
112 total_bw = np.sum(bws)
113 weight_bws = bws[TensorPurpose.Weights]
114 fm_bws = bws[TensorPurpose.FeatureMap]
115 data_items += [
116 fm_bws[BandwidthDirection.Read],
117 fm_bws[BandwidthDirection.Write],
118 weight_bws[BandwidthDirection.Read],
119 weight_bws[BandwidthDirection.Write],
120 total_bw,
121 ]
122
123 data_items += [
124 nng.macs[MacCount.NeuralNetworkMacs],
125 nng.macs[MacCount.HardwareMacs],
126 nng.macs[MacCount.NeuralNetworkMacs] * 2 * midpoint_fps / 1e12,
127 nng.macs[MacCount.HardwareMacs] * 2 * midpoint_fps / 1e12,
128 ]
129
130 data_items += [nng.cycles[kind] for kind in PassCycles.all()]
131
132 writer.writerow(data_items)
133
134
135def write_pass_metrics_csv(nng, pass_filename):
136
137 with open(pass_filename, "w") as f:
138 writer = csv.writer(f)
139
140 purpose_list = (
141 ("total", (TensorPurpose.Weights, TensorPurpose.FeatureMap)),
142 ("weights", (TensorPurpose.Weights,)),
143 ("feature_map", (TensorPurpose.FeatureMap,)),
144 )
145
146 direction_list = (
147 ("total", (BandwidthDirection.Read, BandwidthDirection.Write)),
148 ("read", (BandwidthDirection.Read,)),
149 ("write", (BandwidthDirection.Write,)),
150 )
151 bandwidth_names = []
152 bandwidth_indices = []
Louis Verhaard0265f402020-09-29 13:57:21 +0200153 for mem_area in mem_areas_to_report():
Tim Hall79d07d22020-04-27 18:20:16 +0100154 for purpose, purpose_candidates in purpose_list:
155 for direction, direction_candidates in direction_list:
156 label = "bytes_%s_%s_%s" % (mem_area.identifier_name(), purpose, direction)
157 bandwidth_names.append(label)
158 bandwidth_indices.append((mem_area, purpose_candidates, direction_candidates))
159
160 all_macs = MacCount.all()
161 all_cycles = (
162 PassCycles.Total,
163 PassCycles.Dpu,
164 PassCycles.ElementWise,
165 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:
195 if len(ps.ops) == 1 and ps.ops[0].type == "NpuOp":
196 # just treat this as a call, unroll it
197 write_subgraph(ps.ops[0].attrs["subgraph"])
198 continue
199 stats = [ps.name, " ".join(op.type for op in ps.ops)]
200 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)
255 print("Accelerator configuration %20s" % (arch.accelerator_config,), file=f)
256 print("System configuration %20s" % (arch.system_config,), file=f)
257 print("Accelerator clock %12d MHz" % (arch.npu_clock / 1e6,), file=f)
258 for mem_area, label in mem_area_labels:
259 print(
260 "Design peak %-25s %12.2f GB/s"
261 % (label + " bandwidth", arch.memory_bandwidths_per_second[mem_area] / 1000.0 / 1000 / 1000,),
262 file=f,
263 )
264
265 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:
274 extra = " (%.2f bits per element)" % (bits_per_element[mem_area],)
275
276 print("Total %-25s %12.2f KiB%s" % (aug_label, memory_used[mem_area] / 1024.0, extra), file=f)
277
278 print(file=f)
279 print("%d passes fused into %d" % (num_passes, num_cascaded_passes), file=f)
280
281 n_cpu_operations = len(cpu_operations)
282 if n_operations > 0:
283 print(
284 "%d/%d (%4.1f %%) operations falling back to the CPU"
285 % (n_cpu_operations, n_operations, n_cpu_operations / n_operations * 100),
286 file=f,
287 )
288
289 if show_cpu_operations:
290 for op in cpu_operations:
291
292 def format_tens_list(lst):
293 return " ".join(str(list(tens.shape)) for tens in lst)
294
295 print(
296 "CPU operation: %s, inputs %s, outputs %s"
297 % (op.type, format_tens_list(op.inputs), format_tens_list(op.outputs)),
298 file=f,
299 )
300
301 print("", file=f)
302
303 for mem_area, label in mem_area_labels:
304 bws = bandwidths[mem_area]
305 total_bw = np.sum(bws)
306 weight_bws = bws[TensorPurpose.Weights]
307 fm_bws = bws[TensorPurpose.FeatureMap]
308 aug_label = label + " bandwidth"
309 print(
310 "Average %-25s %12.2f GB/s" % (aug_label, total_bw * midpoint_fps / 1000.0 / 1000.0 / 1000.0,),
311 file=f,
312 )
313 print(
314 "Input %-25s %12.2f MB/batch"
315 % (aug_label, np.sum(fm_bws[BandwidthDirection.Read]) / 1000.0 / 1000.0,),
316 file=f,
317 )
318 print("Weight %-25s %12.2f MB/batch" % (aug_label, np.sum(weight_bws) / 1000.0 / 1000.0,), file=f)
319 print(
320 "Output %-25s %12.2f MB/batch"
321 % (aug_label, np.sum(fm_bws[BandwidthDirection.Write]) / 1000.0 / 1000.0,),
322 file=f,
323 )
324 print("Total %-25s %12.2f MB/batch" % (aug_label, total_bw / 1000.0 / 1000.0,), file=f)
325 print(
326 "Total %-25s per input %9.2f MB/inference (batch size %d)"
327 % (aug_label, total_bw / 1000.0 / 1000.0 / batch_size, batch_size),
328 file=f,
329 )
330 print(file=f)
331
332 print("Neural network macs %12d MACs/batch" % (macs[MacCount.NeuralNetworkMacs],), file=f)
333 print("Hardware macs %12d MACs/batch" % (macs[MacCount.HardwareMacs],), file=f)
334 print(
335 "Network Tops/s %12.2f Tops/s"
336 % (macs[MacCount.NeuralNetworkMacs] * 2 * midpoint_fps / 1e12),
337 file=f,
338 )
339 print(
340 "Hardware Tops/s %12.2f Tops/s"
341 % (macs[MacCount.HardwareMacs] * 2 * midpoint_fps / 1e12),
342 file=f,
343 )
344 print(file=f)
345
346 for kind in PassCycles.all():
347 aug_label = kind.display_name() + " cycles"
348 cyc = cycles[kind]
349 print("%-30s %12d cycles/batch" % (aug_label, cyc,), file=f)
350 print(file=f)
351
352 print(
353 "Batch Inference time %7.2f ms, %7.2f inferences/s (batch size %d)"
354 % (midpoint_inference_time * 1000, midpoint_fps, batch_size),
355 file=f,
356 )
357 print(file=f)
358
359
360def print_performance_metrics(nng, arch, show_cpu_operations=False, f=sys.stdout):
361 n_passes = sum(len(sg.passes) for sg in nng.subgraphs)
362 n_cascaded_passes = sum(len(sg.cascaded_passes) for sg in nng.subgraphs)
363 n_operations = sum(len(ps.ops) for sg in nng.subgraphs for ps in sg.passes)
364 cpu_operations = sum((ps.ops for sg in nng.subgraphs for ps in sg.passes if ps.placement == PassPlacement.Cpu), [])
365 return print_performance_metrics_for_strat(
366 arch,
367 nng.name,
368 nng.cycles,
369 nng.macs,
370 nng.bandwidths,
371 nng.batch_size,
372 nng.memory_used,
373 n_passes,
374 n_cascaded_passes,
375 n_operations,
376 cpu_operations,
377 nng.bits_per_element,
378 show_cpu_operations,
379 f,
380 )
381
382
383def write_human_friendly_metrics(nng, arch, filename):
384 f = open(filename, "w")
385 print_performance_metrics(nng, arch, f=f)