blob: 450fa7b656a35a35bd9ec5774ca2ce6a59c9acff [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# NPU performance estimation functions to estimate performance of a Pass and CascadedPass. Uses a model that takes the
18# maximum of the 'cycles required for bandwidth' and 'cycles required for computing'.
19#
20# Called during scheduling to evaluate different proposals, as well as post-scheduling to provide a final performance
21# estimate.
Tim Halld8339a72021-05-27 18:49:40 +010022import copy
Diqing Zhonge168b962020-11-05 17:18:47 +010023from enum import auto
24from enum import IntEnum
Ayaan Masoodb801dda2022-02-22 11:28:55 +000025from typing import Set
26from uuid import UUID
Diego Russoea6111a2020-04-14 18:41:58 +010027
Tim Hall79d07d22020-04-27 18:20:16 +010028import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010029
30from . import numeric_util
Tim Halld8339a72021-05-27 18:49:40 +010031from .architecture_allocator import ArchitectureBlockConfig
Diqing Zhong09387e22020-09-28 18:46:22 +020032from .architecture_features import Accelerator
Tim Halld8339a72021-05-27 18:49:40 +010033from .architecture_features import NpuBlockType
34from .architecture_features import SHRAMElements
35from .architecture_features import TensorFormat
Ayaan Masoodb801dda2022-02-22 11:28:55 +000036from .nn_graph import Graph
Tim Halld8339a72021-05-27 18:49:40 +010037from .numeric_util import round_up
Johan Alfvénf8e353b2022-02-04 17:24:23 +010038from .numeric_util import round_up_to_int
Tim Halld8339a72021-05-27 18:49:40 +010039from .operation import Kernel
Diqing Zhonge8887a32020-09-24 09:53:48 +020040from .operation import Op
Tim Halld8339a72021-05-27 18:49:40 +010041from .scheduler import Schedule
42from .scheduler import SchedulerOperation
Ayaan Masoodb801dda2022-02-22 11:28:55 +000043from .scheduler import SchedulerOpInfo
Tim Halld8339a72021-05-27 18:49:40 +010044from .shape4d import Shape4D
Diqing Zhongf842b692020-12-11 13:07:37 +010045from .tensor import BandwidthDirection
Diego Russoe8a10452020-04-21 17:39:10 +010046from .tensor import MemArea
Diego Russoe8a10452020-04-21 17:39:10 +010047from .tensor import TensorPurpose
Tim Halld8339a72021-05-27 18:49:40 +010048from .weight_compressor import WeightKey
Tim Hall79d07d22020-04-27 18:20:16 +010049
50
Diqing Zhonge168b962020-11-05 17:18:47 +010051class PassCycles(IntEnum):
Diqing Zhong42e833d2020-10-02 13:18:42 +020052 Npu = 0
Diqing Zhonge168b962020-11-05 17:18:47 +010053 SramAccess = auto()
54 DramAccess = auto()
55 OnChipFlashAccess = auto()
56 OffChipFlashAccess = auto()
57 Total = auto()
58 Size = auto()
Tim Hall79d07d22020-04-27 18:20:16 +010059
60 def display_name(self):
Tim Hall1bd531d2020-11-01 20:59:36 +000061 return ("NPU", "SRAM Access", "DRAM Access", "On-chip Flash Access", "Off-chip Flash Access", "Total", "Size",)[
62 self.value
63 ]
Tim Hall79d07d22020-04-27 18:20:16 +010064
65 def identifier_name(self):
Tim Hall1bd531d2020-11-01 20:59:36 +000066 return ("npu", "sram_access", "dram_access", "on_chip_flash_access", "off_chip_flash_access", "total", "size",)[
67 self.value
68 ]
Tim Hall79d07d22020-04-27 18:20:16 +010069
70 @staticmethod
71 def all():
72 return (
Diqing Zhong42e833d2020-10-02 13:18:42 +020073 PassCycles.Npu,
Tim Hall79d07d22020-04-27 18:20:16 +010074 PassCycles.SramAccess,
75 PassCycles.DramAccess,
76 PassCycles.OnChipFlashAccess,
77 PassCycles.OffChipFlashAccess,
78 PassCycles.Total,
79 )
80
81
Tim Halld8339a72021-05-27 18:49:40 +010082class PerformanceQuery:
83 def __init__(self, npu_block_type=0):
84 self.npu_block_type = npu_block_type
85 self.ifm_shape = Shape4D(0)
86 self.ifm_format = TensorFormat.NHWC
87 self.ifm_memory_area = MemArea.Unknown
88 self.ifm2_memory_area = MemArea.Unknown
89 self.ifm_bits = 0
90 self.ifm2_bits = 0
91 self.ifm2_shape = None
92 self.ifm2_format = TensorFormat.NHWC
93 self.ofm_shape = Shape4D(0)
94 self.ofm_format = TensorFormat.NHWC
95 self.ofm_memory_area = MemArea.Unknown
96 self.ofm_bits = 0
97 self.const_shape = Shape4D(0)
98 self.const_memory_area = MemArea.Unknown
99 self.kernel = Kernel(1, 1)
100 self.config = ArchitectureBlockConfig()
Tim Hall79d07d22020-04-27 18:20:16 +0100101
102
Tim Halld8339a72021-05-27 18:49:40 +0100103class CycleCost:
104 def __init__(self):
105 self.op_macs = 0
106 self.op_cycles = 0
107
108 def __mul__(self, scale):
109 out = CycleCost()
110 out.op_macs = self.op_macs * scale
111 out.op_cycles = self.op_cycles * scale
112 return out
113
114 def __iadd__(self, rhs):
115 self.op_macs += rhs.op_macs
116 self.op_cycles += rhs.op_cycles
117 return self
118
119 def __str__(self):
120 return "macs = {}, cycles = {}".format(self.op_macs, self.op_cycles)
Tim Hall79d07d22020-04-27 18:20:16 +0100121
122
Tim Halld8339a72021-05-27 18:49:40 +0100123class ElementAccess:
124 def __init__(self):
125 # List of ONLY element access counts, consumers
126 # need to scale these values by the correct bitwidths
127 # to calculated memory bandwidth
128 self.ifm_read = [0, 0] # ifm1, ifm2
129 self.ofm_write = 0
130 self.weights_refetch = 0
131 self.const_read = [0, 0] # weights, scales
132
133 def __mul__(self, scale):
134 out = ElementAccess()
135 out.ifm_read[0] = self.ifm_read[0] * scale
136 out.ifm_read[1] = self.ifm_read[1] * scale
137 out.ofm_write = self.ofm_write * scale
138 out.weights_refetch = self.weights_refetch * scale
139 out.const_read[0] = self.const_read[0] * scale
140 out.const_read[1] = self.const_read[1] * scale
141 return out
142
143 def __iadd__(self, rhs):
144 self.ifm_read[0] += rhs.ifm_read[0]
145 self.ifm_read[1] += rhs.ifm_read[1]
146 self.ofm_write += rhs.ofm_write
147 self.weights_refetch += rhs.weights_refetch
148 self.const_read[0] += rhs.const_read[0]
149 self.const_read[1] += rhs.const_read[1]
150 return self
151
152 def __str__(self):
153 return "ifm read = {}, ofm write = {}, const read={}".format(self.ifm_read, self.ofm_write, self.const_read)
Tim Hall79d07d22020-04-27 18:20:16 +0100154
155
Tim Halld8339a72021-05-27 18:49:40 +0100156def _strides_for_shape(shape: Shape4D, format: TensorFormat, element_bits):
157 if format == TensorFormat.NHWC:
158 strides = [0, 0, 0, 0]
159 strides[3] = element_bits / 8 # +Z
160 strides[2] = (element_bits * shape.depth) // 8 # +X
161 strides[1] = (element_bits * shape.depth * shape.width) // 8 # +Y
162 strides[0] = (element_bits * shape.depth * shape.width * shape.height) // 8 # +N
163 elif format == TensorFormat.NHCWB16:
164 strides = [0, 0, 0, 0, 0]
165 strides[4] = element_bits / 8 # +Z
166 strides[3] = (element_bits * 16) / 8 # +X
167 strides[2] = (element_bits * 16 * shape.width) / 8 # +C
168 strides[1] = (element_bits * shape.width * shape.depth) / 8 # +Y
169 strides[0] = (element_bits * shape.width * shape.depth) / 8 # +N
Diqing Zhong42e833d2020-10-02 13:18:42 +0200170
Tim Halld8339a72021-05-27 18:49:40 +0100171 return strides
Diqing Zhong42e833d2020-10-02 13:18:42 +0200172
173
Tim Halld8339a72021-05-27 18:49:40 +0100174def _estimate_memory_transfer_efficiency(
175 arch, is_read, mem_area, format: TensorFormat, element_bits, block_size, shape4D, to_transfer
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100176):
Tim Halld8339a72021-05-27 18:49:40 +0100177 burst_len = 8
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100178
Tim Halld8339a72021-05-27 18:49:40 +0100179 strides = _strides_for_shape(shape4D, format, element_bits)
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100180
Tim Halld8339a72021-05-27 18:49:40 +0100181 if format == TensorFormat.NHCWB16:
182 if strides[2] == block_size.depth: # TODO is this check corrrect for non 8-bit
183 burst_len = element_bits * block_size.depth * block_size.width
184 elif is_read:
185 burst_len = 16 * element_bits * block_size.width
Diqing Zhonge8887a32020-09-24 09:53:48 +0200186 else:
Tim Halld8339a72021-05-27 18:49:40 +0100187 burst_len = 16 * element_bits * block_size.width * arch.ncores
188 elif format == TensorFormat.NHWC:
189 if is_read:
190 if strides[3] == block_size.depth:
191 burst_len = element_bits * block_size.depth * block_size.width
192 else:
193 burst_len = element_bits * block_size.depth
194 else:
195 if block_size.depth <= 16 and strides[3] == block_size.depth:
196 burst_len = element_bits * block_size.depth * block_size.width
197 else:
198 burst_len = min(64 * 8, 16 * element_bits * arch.ncores, block_size.depth * element_bits)
199
200 burst_len = burst_len // 8 # bits->bytes
201 burst_len = min(arch.memory_burst_length[mem_area], burst_len)
202 return to_transfer * (arch.memory_burst_length[mem_area] / burst_len)
203
204
205def _estimate_minimum_memory_cycles(arch, query: PerformanceQuery):
206 # Input block HW transfer (only for elements present)
207 ifm_bytes = Shape4D.min(query.ifm_shape, query.config.ifm_block).elements()
208 cycles_ifm_blk = arch.memory_latency[query.ifm_memory_area][BandwidthDirection.Read]
209 cycles_ifm_blk = cycles_ifm_blk + (
210 _estimate_memory_transfer_efficiency(
211 arch,
212 True,
213 query.ifm_memory_area,
214 query.ifm_format,
215 query.ifm_bits,
216 query.config.ifm_block,
217 query.ifm_shape,
218 ifm_bytes,
219 )
220 / arch.memory_bandwidths_per_cycle[query.ifm_memory_area]
221 )
222 # Output block HW transfer (only for elements present)
223 ofm_bytes = Shape4D.min(query.ofm_shape, query.config.ofm_block).elements()
224 cycles_ofm_blk = arch.memory_latency[query.ofm_memory_area][BandwidthDirection.Write]
225 cycles_ofm_blk = cycles_ofm_blk + (
226 _estimate_memory_transfer_efficiency(
227 arch,
228 False,
229 query.ofm_memory_area,
230 query.ofm_format,
231 query.ofm_bits,
232 query.config.ofm_block,
233 query.ofm_shape,
234 ofm_bytes,
235 )
236 / arch.memory_bandwidths_per_cycle[query.ofm_memory_area]
237 )
238 return cycles_ifm_blk, cycles_ofm_blk
239
240
241def _estimate_output_cycles_per_element(arch, op_type: Op, faf_type: Op, query: PerformanceQuery):
242 if query.npu_block_type == NpuBlockType.ElementWise and query.ifm_bits == 32:
243 # Unary op else Binary op
244 output_perf_index = 0 if query.ifm2_shape is not None else 1
245 elif op_type == Op.Mul and query.ofm_bits == 32:
Diqing Zhonge8887a32020-09-24 09:53:48 +0200246 output_perf_index = 2
Tim Halld8339a72021-05-27 18:49:40 +0100247 elif op_type == Op.Mul or (
248 query.npu_block_type
Diqing Zhonge8887a32020-09-24 09:53:48 +0200249 in (
250 NpuBlockType.ConvolutionMxN,
251 NpuBlockType.ConvolutionDepthWise,
252 NpuBlockType.Pooling,
253 NpuBlockType.ReduceSum,
254 NpuBlockType.VectorProduct,
255 )
Tim Halld8339a72021-05-27 18:49:40 +0100256 and query.config.acc_type == SHRAMElements.Acc40
Diqing Zhonge8887a32020-09-24 09:53:48 +0200257 ):
258 output_perf_index = 3
Tim Halld8339a72021-05-27 18:49:40 +0100259 elif op_type in (Op.Add, Op.Sub):
260 if False:
Diqing Zhonge8887a32020-09-24 09:53:48 +0200261 # Simple Add/Sub
262 output_perf_index = 4
263 else:
Tim Halld8339a72021-05-27 18:49:40 +0100264 # Advanced Add/Sub TODO: Add as perf selection as operator variant
Diqing Zhonge8887a32020-09-24 09:53:48 +0200265 output_perf_index = 5
Tim Halld8339a72021-05-27 18:49:40 +0100266 elif op_type.is_maxpool_op():
Diqing Zhonge8887a32020-09-24 09:53:48 +0200267 output_perf_index = 6
268 else:
269 output_perf_index = 7
270
Tim Halld8339a72021-05-27 18:49:40 +0100271 if faf_type in (Op.Sigmoid, Op.Tanh, Op.LUT):
Diqing Zhonge8887a32020-09-24 09:53:48 +0200272 activation_perf_index = 0
Tim Halld8339a72021-05-27 18:49:40 +0100273 elif faf_type in (Op.Relu, Op.Relu6, Op.ReluN1To1):
Diqing Zhonge8887a32020-09-24 09:53:48 +0200274 activation_perf_index = 1
275 else:
276 activation_perf_index = 2
277
Diqing Zhonge8887a32020-09-24 09:53:48 +0200278 cycle_per_elem = max(
279 arch.output_cycles_per_elem[output_perf_index], arch.activation_cycles_per_elem[activation_perf_index]
280 )
Diqing Zhong986e3192020-11-16 16:15:56 +0100281
Tim Halld8339a72021-05-27 18:49:40 +0100282 if op_type.is_elementwise_op():
283 num_elems_blk = query.config.ofm_block.elements()
284 ifm_blk_cycles, ofm_blk_cycles = _estimate_minimum_memory_cycles(arch, query)
285 cycle_cmd = ifm_blk_cycles + ofm_blk_cycles
286 cycle_cmd = (cycle_cmd + cycle_per_elem * num_elems_blk) / 4 # per DPU
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100287 cycle_per_elem = max(cycle_per_elem, cycle_cmd / num_elems_blk)
288
Tim Halld8339a72021-05-27 18:49:40 +0100289 return cycle_per_elem
Diqing Zhonge8887a32020-09-24 09:53:48 +0200290
291
Tim Halld8339a72021-05-27 18:49:40 +0100292def _estimate_conv_cycles(arch, op_type: Op, faf_type: Op, query: PerformanceQuery):
293 ifm_block = Shape4D.min(query.ifm_shape, query.config.ifm_block)
294 ofm_block = Shape4D.min(query.ofm_shape, query.config.ofm_block)
Diqing Zhonge5204a62020-10-13 11:42:37 +0200295
296 if (
297 arch.config.ofm_ublock.height == 2
Tim Halld8339a72021-05-27 18:49:40 +0100298 and query.npu_block_type
Diqing Zhonge5204a62020-10-13 11:42:37 +0200299 in (NpuBlockType.ConvolutionMxN, NpuBlockType.ConvolutionDepthWise, NpuBlockType.VectorProduct)
Tim Halld8339a72021-05-27 18:49:40 +0100300 and query.ofm_shape.height == 1
Diqing Zhonge5204a62020-10-13 11:42:37 +0200301 # Optimisation only applies for even width tensors
Tim Halld8339a72021-05-27 18:49:40 +0100302 and query.ofm_shape.width % 2 == 0
303 and query.kernel.height == 1
Diqing Zhonge5204a62020-10-13 11:42:37 +0200304 ):
Tim Halld8339a72021-05-27 18:49:40 +0100305 ofm_ublock = Shape4D(1, 1, 4, arch.config.ofm_ublock.depth)
306 ofm_block = ofm_block.with_height(1)
307 else:
308 ofm_ublock = Shape4D(arch.config.ofm_ublock.to_hwc())
Diqing Zhonge5204a62020-10-13 11:42:37 +0200309
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100310 num_ublk_x = numeric_util.round_up_divide(ofm_block.width, ofm_ublock.width)
Tim Halld8339a72021-05-27 18:49:40 +0100311 num_ublk_y = numeric_util.round_up_divide(ofm_block.height, ofm_ublock.height)
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100312 num_ublk_xy = num_ublk_x * num_ublk_y
Tim Halld8339a72021-05-27 18:49:40 +0100313 num_ublk_z = numeric_util.round_up_divide(ofm_block.depth, ofm_ublock.depth)
314 use_acc_40bits = query.config.acc_type == SHRAMElements.Acc40
Diqing Zhong09387e22020-09-28 18:46:22 +0200315
Tim Halld8339a72021-05-27 18:49:40 +0100316 sub_kernel_limits = arch.sub_kernel_limits[query.npu_block_type]
317 n_sub_kernels_y = numeric_util.round_up_divide(query.kernel.height, sub_kernel_limits[0])
318 n_sub_kernels_x = numeric_util.round_up_divide(query.kernel.width, sub_kernel_limits[1])
Diqing Zhong09387e22020-09-28 18:46:22 +0200319 sub_kernel_x = [
Tim Halld8339a72021-05-27 18:49:40 +0100320 min((query.kernel.width - i * sub_kernel_limits[1]), sub_kernel_limits[1]) for i in range(n_sub_kernels_x)
Diqing Zhong09387e22020-09-28 18:46:22 +0200321 ]
322 sub_kernel_y = [
Tim Halld8339a72021-05-27 18:49:40 +0100323 min((query.kernel.height - i * sub_kernel_limits[0]), sub_kernel_limits[0]) for i in range(n_sub_kernels_y)
Diqing Zhong09387e22020-09-28 18:46:22 +0200324 ]
325 sub_kernel_size = (x * y for y in sub_kernel_y for x in sub_kernel_x)
326
Diqing Zhong09387e22020-09-28 18:46:22 +0200327 cycles_dpu_blk = 0
Diqing Zhong986e3192020-11-16 16:15:56 +0100328 cycles_wb = 32 * ofm_ublock.depth // 8
Diqing Zhong09387e22020-09-28 18:46:22 +0200329
330 for num_kernel_elems in sub_kernel_size:
Tim Halld8339a72021-05-27 18:49:40 +0100331 if query.npu_block_type == NpuBlockType.Pooling:
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100332 num_kernel_steps = 1
Diqing Zhong986e3192020-11-16 16:15:56 +0100333 cycles = max(4, num_kernel_elems) * num_ublk_xy * num_ublk_z
Tim Halld8339a72021-05-27 18:49:40 +0100334 if query.ifm_bits == 16 and arch.accelerator_config != Accelerator.Ethos_U55_32:
Diqing Zhong09387e22020-09-28 18:46:22 +0200335 cycles *= 2
Tim Halld8339a72021-05-27 18:49:40 +0100336 elif query.npu_block_type == NpuBlockType.ConvolutionDepthWise:
Diqing Zhong986e3192020-11-16 16:15:56 +0100337 cycles = 4 * num_ublk_xy
Tim Halld8339a72021-05-27 18:49:40 +0100338 if query.ifm_bits == 16:
Diqing Zhong09387e22020-09-28 18:46:22 +0200339 cycles *= 2
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100340 num_kernel_steps = numeric_util.round_up_divide(num_kernel_elems, 4)
341 cycles = max(cycles_wb, cycles) * num_kernel_steps * num_ublk_z
Diqing Zhong09387e22020-09-28 18:46:22 +0200342 elif (
Tim Halld8339a72021-05-27 18:49:40 +0100343 (query.npu_block_type == NpuBlockType.ConvolutionMxN and not query.config.is_partkernel)
344 or query.npu_block_type == NpuBlockType.VectorProduct
345 or query.npu_block_type == NpuBlockType.ReduceSum
Diqing Zhong09387e22020-09-28 18:46:22 +0200346 ):
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100347 num_kernel_steps = num_kernel_elems
348 cycles = max(cycles_wb, 4 * num_ublk_xy) * num_kernel_steps * num_ublk_z
Diqing Zhong09387e22020-09-28 18:46:22 +0200349 else:
Tim Halld8339a72021-05-27 18:49:40 +0100350 assert query.config.is_partkernel
351 divider = 2 if query.ifm_bits == 16 else 4
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100352 num_kernel_steps = numeric_util.round_up_divide(num_kernel_elems, divider)
Diqing Zhong986e3192020-11-16 16:15:56 +0100353 cycles = max(cycles_wb, 4 * num_ublk_xy) * (
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100354 num_kernel_steps * numeric_util.round_up_divide(ifm_block.depth, 8) * num_ublk_z
Diqing Zhong09387e22020-09-28 18:46:22 +0200355 )
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100356
357 delay_cycles = 0
358 if arch.accelerator_config is Accelerator.Ethos_U55_32:
359 delay = 7 if use_acc_40bits else 3
360 if num_ublk_x == 1 and num_ublk_y == 1:
361 if num_ublk_z == 1:
362 delay_cycles = delay * num_kernel_steps
363 elif num_kernel_steps > 1:
364 delay_cycles = delay * (num_kernel_steps - 1) * num_ublk_z
365 if (num_ublk_x == 1 or num_ublk_y == 1) and num_ublk_z > 1 and use_acc_40bits:
366 delay_cycles += delay * num_ublk_z
367 else:
Tim Halld8339a72021-05-27 18:49:40 +0100368 if use_acc_40bits and arch.accelerator_config in (Accelerator.Ethos_U55_64, Accelerator.Ethos_U55_128):
369 delay = 3
370 else:
371 delay = 2
372
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100373 if num_ublk_x == 1 and num_ublk_y == 1:
374 if num_ublk_z == 1:
375 delay_cycles = delay * num_kernel_steps
376 elif num_kernel_steps > 1:
377 delay_cycles = delay * (num_kernel_steps - 1) * num_ublk_z
378
Tim Halld8339a72021-05-27 18:49:40 +0100379 if query.npu_block_type == NpuBlockType.ConvolutionMxN and query.config.is_partkernel:
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100380 delay_cycles *= numeric_util.round_up_divide(ifm_block.depth, 8)
381
Diqing Zhong09387e22020-09-28 18:46:22 +0200382 cycles_dpu_blk += cycles
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100383 cycles_dpu_blk += delay_cycles
384
Tim Halld8339a72021-05-27 18:49:40 +0100385 if query.npu_block_type in (NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct, NpuBlockType.ReduceSum):
386 cycles_dpu_blk *= numeric_util.round_up_divide(query.ifm_shape.depth, ifm_block.depth)
Diqing Zhong09387e22020-09-28 18:46:22 +0200387
388 cycles_dpu_blk /= arch.ncores
389
Tim Halld8339a72021-05-27 18:49:40 +0100390 # Estimate output cycles
391 num_ofm_blks = query.ofm_shape.div_round_up(ofm_block).elements()
Johan Alfvénf8e353b2022-02-04 17:24:23 +0100392 cycles_output_blk = round_up_to_int(
393 _estimate_output_cycles_per_element(arch, op_type, faf_type, query) * ofm_block.elements()
394 )
Diqing Zhong09387e22020-09-28 18:46:22 +0200395
Tim Halld8339a72021-05-27 18:49:40 +0100396 # Scale and bias tensor
397 if query.const_shape.depth > 0:
Diqing Zhongf842b692020-12-11 13:07:37 +0100398 cycles_bias_blk = (
Tim Halld8339a72021-05-27 18:49:40 +0100399 10 * ofm_block.depth * arch.memory_latency[query.const_memory_area][BandwidthDirection.Read] / 256
Diqing Zhongf842b692020-12-11 13:07:37 +0100400 )
Diqing Zhong986e3192020-11-16 16:15:56 +0100401 cycles_output_blk = max(cycles_output_blk, cycles_bias_blk)
402
Tim Halld8339a72021-05-27 18:49:40 +0100403 ifm_blk_cycles, ofm_blk_cycles = _estimate_minimum_memory_cycles(arch, query)
404 cycles_cmd = ifm_blk_cycles + ofm_blk_cycles
405 cycles_cmd = (cycles_cmd + cycles_output_blk + cycles_dpu_blk) / 4 # per DPU
406
Diqing Zhongef0c7fe2020-11-24 14:38:20 +0100407 cycles_dpu_blk = max(cycles_dpu_blk, cycles_cmd)
408 cycles_output_blk = max(cycles_output_blk, cycles_cmd)
409
Diqing Zhong09387e22020-09-28 18:46:22 +0200410 if cycles_dpu_blk > cycles_output_blk:
Tim Halld8339a72021-05-27 18:49:40 +0100411 total_cycles = cycles_dpu_blk * num_ofm_blks + cycles_output_blk
Diqing Zhong09387e22020-09-28 18:46:22 +0200412 else:
Tim Halld8339a72021-05-27 18:49:40 +0100413 total_cycles = cycles_output_blk * num_ofm_blks + cycles_dpu_blk
Diqing Zhong09387e22020-09-28 18:46:22 +0200414
415 return total_cycles
416
417
Tim Halld8339a72021-05-27 18:49:40 +0100418def measure_mem2mem_cycles(arch, from_mem_area, to_mem_area, to_transfer):
419 from_cycles = to_transfer // arch.memory_bandwidths_per_cycle[from_mem_area]
Tim Hall789e6f32021-06-17 17:02:31 +0100420 from_cycles += arch.memory_latency[from_mem_area][BandwidthDirection.Read]
Tim Halld8339a72021-05-27 18:49:40 +0100421 to_cycles = to_transfer // arch.memory_bandwidths_per_cycle[to_mem_area]
422 return max(from_cycles, to_cycles)
Diqing Zhonge168b962020-11-05 17:18:47 +0100423
Patrik Gustavssonee99bb12021-04-08 09:04:00 +0200424
Tim Halld8339a72021-05-27 18:49:40 +0100425def measure_cycle_cost(arch, op_type: Op, faf_type: Op, query: PerformanceQuery):
426 cycles = CycleCost()
Diqing Zhonge168b962020-11-05 17:18:47 +0100427
Tim Halld8339a72021-05-27 18:49:40 +0100428 # Convolution/Vector product cycle calculation
429 if query.npu_block_type in (
430 NpuBlockType.ConvolutionMxN,
431 NpuBlockType.ConvolutionDepthWise,
432 NpuBlockType.VectorProduct,
433 NpuBlockType.Pooling,
434 NpuBlockType.ReduceSum,
435 ):
436 # cycles.op_macs and cycles.op_cycles should both handle >32-bits
437 if query.npu_block_type in (NpuBlockType.ConvolutionDepthWise, NpuBlockType.Pooling):
438 cycles.op_macs = int(query.kernel.elements_wh()) * 1 * int(query.ofm_shape.elements())
Diqing Zhonge168b962020-11-05 17:18:47 +0100439 else:
Tim Halld8339a72021-05-27 18:49:40 +0100440 cycles.op_macs = (
441 int(query.kernel.elements_wh()) * int(query.ifm_shape.depth) * int(query.ofm_shape.elements())
442 )
443
444 cycles.op_cycles = int(_estimate_conv_cycles(arch, op_type, faf_type, query))
445 # Elementwise cycle calculation
446 elif query.npu_block_type == NpuBlockType.ElementWise:
447 cycles.op_macs = 0
Johan Alfvénf8e353b2022-02-04 17:24:23 +0100448 ofm_rounding = Shape4D(list(arch.storage_rounding_quantums[query.ofm_format]))
449 cycles.op_cycles = round_up_to_int(
450 _estimate_output_cycles_per_element(arch, op_type, faf_type, query)
451 * Shape4D.round_up(query.ofm_shape, ofm_rounding).elements()
Tim Halld8339a72021-05-27 18:49:40 +0100452 )
Diqing Zhonge168b962020-11-05 17:18:47 +0100453 else:
Tim Halld8339a72021-05-27 18:49:40 +0100454 assert False
Diqing Zhonge168b962020-11-05 17:18:47 +0100455
Tim Halld8339a72021-05-27 18:49:40 +0100456 return cycles
Diqing Zhonge168b962020-11-05 17:18:47 +0100457
458
Tim Halld8339a72021-05-27 18:49:40 +0100459def measure_element_access(arch, query: PerformanceQuery):
460 access = ElementAccess()
Tim Hall79d07d22020-04-27 18:20:16 +0100461
Tim Halld8339a72021-05-27 18:49:40 +0100462 ifm_block = Shape4D.min(query.ifm_shape, query.config.ifm_block)
463 ofm_block = Shape4D.min(query.ofm_shape, query.config.ofm_block)
464 ifm_rounding = Shape4D(list(arch.storage_rounding_quantums[query.ifm_format]))
Tim Hall79d07d22020-04-27 18:20:16 +0100465
Tim Halld8339a72021-05-27 18:49:40 +0100466 # Number of ofm blocks in the overall output shape
467 ofm_blocks = query.ofm_shape.div_round_up(ofm_block)
468 ofm_block_depth = ofm_block.depth
469 if query.npu_block_type in (NpuBlockType.ConvolutionDepthWise, NpuBlockType.Pooling):
470 ofm_blocks = ofm_blocks.with_depth(1)
471 ofm_block_depth = query.ifm_shape.depth
Diqing Zhonge168b962020-11-05 17:18:47 +0100472
Tim Halld8339a72021-05-27 18:49:40 +0100473 # Convolution & pooling
474 if query.npu_block_type in (
475 NpuBlockType.ConvolutionMxN,
476 NpuBlockType.ConvolutionDepthWise,
477 NpuBlockType.VectorProduct,
478 NpuBlockType.Pooling,
479 NpuBlockType.ReduceSum,
480 ):
481 # Number of sub kernels
482 sub_kernel_limits = arch.sub_kernel_limits[query.npu_block_type]
483 subkernels = numeric_util.round_up_divide(query.kernel.width, sub_kernel_limits[0])
484 subkernels *= numeric_util.round_up_divide(query.kernel.height, sub_kernel_limits[1])
Tim Hall79d07d22020-04-27 18:20:16 +0100485
Tim Halld8339a72021-05-27 18:49:40 +0100486 ofm_block_count = ofm_blocks.elements()
Tim Hall79d07d22020-04-27 18:20:16 +0100487
Tim Halld8339a72021-05-27 18:49:40 +0100488 ifm_fetch = (
489 Shape4D.round_up(ifm_block, ifm_rounding).elements_wh()
490 * Shape4D.round_up(query.ifm_shape, ifm_rounding).depth
Diqing Zhonge168b962020-11-05 17:18:47 +0100491 )
Tim Hall79d07d22020-04-27 18:20:16 +0100492
Tim Halld8339a72021-05-27 18:49:40 +0100493 if query.npu_block_type in (NpuBlockType.ConvolutionDepthWise, NpuBlockType.Pooling):
494 kernel_read = query.kernel.elements_wh() * 1 # force to no reread
495 else:
496 kernel_read = query.kernel.elements_wh() * query.ifm_shape.depth
Tim Hall79d07d22020-04-27 18:20:16 +0100497
Tim Halld8339a72021-05-27 18:49:40 +0100498 weight_fetch = kernel_read * ofm_block_depth * ofm_block_count
499
500 access.ifm_read[0] = ifm_fetch * subkernels * ofm_block_count
501
502 if query.npu_block_type not in (NpuBlockType.Pooling, NpuBlockType.ReduceSum):
503 access.const_read[0] = weight_fetch
504 access.const_read[1] = query.ofm_shape.depth # Scales & biases
505 access.weights_refetch = ofm_blocks.elements_wh()
506 # Elementwise
507 elif query.npu_block_type == NpuBlockType.ElementWise:
508 if query.ifm_shape.elements() == 1:
509 if query.ifm_bits > 8:
510 # ifm is a non 8-bit scalar
511 access.ifm_read[0] = Shape4D.round_up(query.ifm_shape, ifm_rounding).elements()
512 if query.ifm2_shape:
513 access.ifm_read[1] = Shape4D.round_up(query.ofm_shape, ifm_rounding).elements()
514 else:
515 access.ifm_read[0] = Shape4D.round_up(query.ofm_shape, ifm_rounding).elements()
516 if query.ifm2_shape:
517 if query.ifm2_shape.elements() > 1:
518 access.ifm_read[1] = Shape4D.round_up(query.ofm_shape, ifm_rounding).elements()
519 elif query.ifm2_bits > 8:
520 # ifm2 is a non 8-bit scalar
521 access.ifm_read[1] = Shape4D.round_up(query.ifm2_shape, ifm_rounding).elements()
522 # Unknown
523 else:
524 assert False
525
526 ofm_rounding = Shape4D(list(arch.storage_rounding_quantums[query.ofm_format]))
527 access.ofm_write = Shape4D.round_up(query.ofm_shape, ofm_rounding).elements()
528 return access
529
530
531def measure_performance_cost(
532 arch, op_type: Op, faf_type: Op, query: PerformanceQuery, offset: Shape4D, sub_shape: Shape4D
533):
534 assert (query.ofm_bits > 0) and (query.ifm_bits > 0)
535 assert query.ofm_shape.elements() != 0
536
537 # Default to start if no offset provided
538 if offset is None:
539 offset = Shape4D(0, 0, 0, 0)
540
541 # Default to entire area if no sub-shape provided
542 if sub_shape is None:
543 sub_shape = query.ofm_shape
544 else:
545 sub_shape = Shape4D.min(sub_shape, query.ofm_shape)
546
547 sub_query = copy.deepcopy(query)
548 sub_query.ofm_shape = query.ofm_shape.clip(offset, sub_shape)
549
550 access = ElementAccess()
551 cycles = CycleCost()
552
553 cycle_tmp = measure_cycle_cost(arch, op_type, faf_type, sub_query)
554 cycles += cycle_tmp
555 access = measure_element_access(arch, sub_query)
556
557 return access, cycles
558
559
560def make_bandwidth_array():
561 return np.zeros((MemArea.Size, TensorPurpose.Size, BandwidthDirection.Size))
562
563
564def make_cycles_array():
565 return np.zeros(PassCycles.Size)
Tim Hall79d07d22020-04-27 18:20:16 +0100566
567
Diqing Zhonge168b962020-11-05 17:18:47 +0100568def update_summary_cycles(arch, bws, cycles):
569 cycles[PassCycles.SramAccess] = np.sum(bws[MemArea.Sram]) / arch.memory_bandwidths_per_cycle[MemArea.Sram]
Tim Hall79d07d22020-04-27 18:20:16 +0100570 cycles[PassCycles.DramAccess] = np.sum(bws[MemArea.Dram]) / arch.memory_bandwidths_per_cycle[MemArea.Dram]
571 cycles[PassCycles.OnChipFlashAccess] = (
572 np.sum(bws[MemArea.OnChipFlash]) / arch.memory_bandwidths_per_cycle[MemArea.OnChipFlash]
573 )
574 cycles[PassCycles.OffChipFlashAccess] = (
575 np.sum(bws[MemArea.OffChipFlash]) / arch.memory_bandwidths_per_cycle[MemArea.OffChipFlash]
576 )
577
578 cycles[PassCycles.Total] = np.max(cycles[: PassCycles.Total])
579 return cycles
580
581
Tim Halld8339a72021-05-27 18:49:40 +0100582def estimate_full_op_performance(
583 arch, schedule: Schedule, op: SchedulerOperation, prev_op: SchedulerOperation, block_config
584):
585 cycles_a = make_cycles_array()
586 bws = make_bandwidth_array()
587 scaled_bws = make_bandwidth_array() # scaled bw with memory transfer efficiency
588 macs = 0
589
590 query = PerformanceQuery(op.op_type.npu_block_type)
591 query.ifm_shape = op.ifm.shape
592 query.ifm_format = op.ifm.format
593 query.ifm_memory_area = op.ifm.mem_area
594 query.ifm_bits = op.ifm.dtype.size_in_bits()
595 query.ifm2_shape = op.ifm2 and op.ifm2.shape
596 query.ifm2_format = op.ifm2 and op.ifm2.format
597 query.ifm2_memory_area = op.ifm2 and op.ifm2.mem_area
598 query.ifm2_bits = op.ifm2 and op.ifm2.dtype.size_in_bits()
599 query.ofm_shape = op.ofm.shape
600 query.ofm_memory_area = op.ofm.mem_area
601 query.ofm_bits = op.ofm.dtype.size_in_bits()
602 query.ofm_format = op.ofm.format
603 query.kernel = op.kernel
604 query.config = block_config
605
606 cost = schedule.cost_map[op]
607 prev_cost = schedule.cost_map[prev_op] if prev_op else None
608 if op.parent_op.bias:
609 query.const_shape = Shape4D(1, 1, 1, op.ofm.shape.depth)
610 if cost.buffered_weight_tensor:
611 query.const_memory_area = cost.buffered_weight_tensor.mem_area
612 else:
613 query.const_memory_area = cost.npu_weights_tensor.mem_area
614
615 cycles = measure_cycle_cost(arch, op.op_type, op.parent_op.activation and op.parent_op.activation.op_type, query)
616 cycles_a[PassCycles.Npu] = cycles.op_cycles
617 macs = cycles.op_macs
618
619 access = measure_element_access(arch, query)
620
621 # How many NPU cycles are available under the previously executing
622 # operator for performing buffered DMA transfers
623 slack_cycles = prev_cost.slack_buffering_cycles if prev_cost else 0
624
625 # LUT Transfer
626 parent_op = op.parent_op
627 lut_transfer_cycles = 0
628 if parent_op.activation_lut:
629 lut_tensor = [tens for tens in parent_op.inputs if tens.purpose == TensorPurpose.LUT][0]
630 src_tensor = lut_tensor.src_tensor
631 if src_tensor and lut_tensor.mem_area != src_tensor.mem_area:
632 bw = src_tensor.storage_size()
633 lut_transfer_cycles = measure_mem2mem_cycles(arch, src_tensor.mem_area, lut_tensor.mem_area, bw)
634
635 bws[src_tensor.mem_area][lut_tensor.purpose][BandwidthDirection.Read] += bw
636 # LUT read from SHRAM TODO remove?
Ayaan Masoodd5cbef32022-02-22 15:56:35 +0000637 scaled_bws[lut_tensor.mem_area][lut_tensor.purpose][BandwidthDirection.Read] += bw
Tim Halld8339a72021-05-27 18:49:40 +0100638
639 if cost.npu_weights_tensor and cost.buffered_weight_tensor:
640 # DMA Weight Transfer
641 sz = 0
642 # Get the size of the first DMA
643 for core in range(0, arch.ncores):
644 key = WeightKey(core, 0)
645 if key in cost.npu_weights_tensor.encoded_ranges:
646 weight_range = cost.npu_weights_tensor.encoded_ranges[key]
647 sz += round_up(weight_range.total_bytes, 16)
648
649 total_sz = len(cost.npu_weights_tensor.buffer)
650 bws[cost.npu_weights_tensor.mem_area][TensorPurpose.Weights][BandwidthDirection.Read] += total_sz
651 bws[cost.buffered_weight_tensor.mem_area][TensorPurpose.Weights][BandwidthDirection.Write] += total_sz
652
653 ws_first_transfer_cycles = measure_mem2mem_cycles(
654 arch, cost.npu_weights_tensor.mem_area, cost.buffered_weight_tensor.mem_area, sz
655 )
656
657 # Add cycles for Weight + Scale Transfer
658 cycles_a[PassCycles.Npu] = max(
659 cost.full_weight_transfer_cycles - slack_cycles + cost.slack_buffering_cycles,
660 cycles.op_cycles + max(ws_first_transfer_cycles - slack_cycles, 0),
661 )
662
663 # Add cycles for LUT Transfer
664 cycles_a[PassCycles.Npu] += lut_transfer_cycles
665 else:
666 # Add cycles for LUT Transfer
667 cycles_a[PassCycles.Npu] += max(lut_transfer_cycles - slack_cycles, 0)
668
669 # OFM write
670 ofm = op.parent_op.ofm
671 bw = access.ofm_write * ofm.element_size()
672 bws[query.ofm_memory_area][ofm.purpose][BandwidthDirection.Write] += bw
673 scaled_bws[ofm.mem_area][ofm.purpose][BandwidthDirection.Write] += _estimate_memory_transfer_efficiency(
674 arch, False, query.ofm_memory_area, ofm.format, query.ofm_bits, query.config.ofm_block, query.ofm_shape, bw
675 )
676
677 # IFM read
678 ifm = op.parent_op.ifm
679 bw = access.ifm_read[0] * ifm.element_size()
680 bws[ifm.mem_area][ifm.purpose][BandwidthDirection.Read] += bw
681 scaled_bws[ifm.mem_area][ifm.purpose][BandwidthDirection.Read] += _estimate_memory_transfer_efficiency(
682 arch, True, query.ifm_memory_area, ifm.format, query.ifm_bits, query.config.ifm_block, query.ifm_shape, bw
683 )
684 if query.ifm2_shape:
685 ifm2 = op.parent_op.ifm2
686 bw = access.ifm_read[1] * ifm2.element_size()
687 bws[ifm2.mem_area][ifm2.purpose][BandwidthDirection.Read] += bw
688 scaled_bws[ifm2.mem_area][ifm2.purpose][BandwidthDirection.Read] += _estimate_memory_transfer_efficiency(
689 arch,
690 True,
691 query.ifm2_memory_area,
692 ifm2.format,
693 op.ifm2.dtype.size_in_bits(),
694 query.config.ifm_block,
695 query.ifm2_shape,
696 bw,
697 )
698
699 # Weight read
700 if access.const_read[0] > 0:
701 # alignment not accounted for in bandwidth_compression_scale_approx
702 encoded_size_approx = (
703 cost.npu_weights_tensor.elements() - access.const_read[1] * op.parent_op.bias.element_size()
704 )
705 orig_weight_size = parent_op.weights.elements()
706 bandwidth_compression_scale_approx = encoded_size_approx / orig_weight_size
707 bw = access.const_read[0] * bandwidth_compression_scale_approx
708 bws[query.const_memory_area][TensorPurpose.Weights][BandwidthDirection.Read] += bw
709
Patrik Gustavsson225e19d2021-06-01 12:43:43 +0200710 if not cost.buffered_weight_tensor:
711 scaled_bws[query.const_memory_area][TensorPurpose.Weights][BandwidthDirection.Read] += bw
712
Tim Halld8339a72021-05-27 18:49:40 +0100713 if access.const_read[1] > 0:
714 # Scales & biases
715 bw = access.const_read[1] * op.parent_op.bias.element_size()
716 bws[query.const_memory_area][TensorPurpose.FSBias][BandwidthDirection.Read] += bw
717
Patrik Gustavsson225e19d2021-06-01 12:43:43 +0200718 if not cost.buffered_weight_tensor:
719 scaled_bws[query.const_memory_area][TensorPurpose.FSBias][BandwidthDirection.Read] += bw
720
Tim Halld8339a72021-05-27 18:49:40 +0100721 update_summary_cycles(arch, scaled_bws, cycles_a)
722
723 return bws, macs, cycles_a
Tim Hall79d07d22020-04-27 18:20:16 +0100724
725
Ayaan Masoodb801dda2022-02-22 11:28:55 +0000726def calc_new_performance_for_network(nng: Graph, arch):
Tim Hall79d07d22020-04-27 18:20:16 +0100727 total_bws = make_bandwidth_array()
Diqing Zhong69aadd02020-12-08 13:08:48 +0100728 total_macs = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100729 total_cycles = np.zeros(PassCycles.Size)
Ayaan Masoodb801dda2022-02-22 11:28:55 +0000730 total_weight_size = 0
731 total_encoded_weight_size = 0
732
733 # Store unique instances of original/encoded weight tensor uuids to prevent double counting of weights
734 original_weight_uuids: Set[UUID] = set()
735 encoded_npu_weight_uuids: Set[UUID] = set()
Tim Hall79d07d22020-04-27 18:20:16 +0100736
737 for sg in nng.subgraphs:
Tim Halld8339a72021-05-27 18:49:40 +0100738 prev_op = None
739 for sched_op in sg.sched_ops:
Ayaan Masoodb801dda2022-02-22 11:28:55 +0000740 op_info: SchedulerOpInfo = sg.schedule.cost_map[sched_op]
Tim Halld8339a72021-05-27 18:49:40 +0100741 bws, macs, cycles = estimate_full_op_performance(arch, sg.schedule, sched_op, prev_op, op_info.block_config)
Ayaan Masoodb801dda2022-02-22 11:28:55 +0000742
743 # Tensors for calculating weight sizes
744 original_weight = sched_op.parent_op.weights
745 encoded_npu_weight = op_info.npu_weights_tensor
746
747 # Save UUIDs of original_weight so only unique instances of tensors are used to calculate weights
748 if original_weight and (original_weight.equivalence_id not in original_weight_uuids):
749
750 original_weight_uuids.add(original_weight.equivalence_id)
751 total_weight_size += original_weight.values.itemsize * original_weight.values.size
752
753 # Save UUIDs of encoded_npu_weight so only unique instances of tensors are used to calculate weights
754 if encoded_npu_weight and (encoded_npu_weight.equivalence_id not in encoded_npu_weight_uuids):
755
756 encoded_npu_weight_uuids.add(encoded_npu_weight)
757 total_encoded_weight_size += len(encoded_npu_weight.buffer)
758
Tim Hall79d07d22020-04-27 18:20:16 +0100759 total_bws += bws
760 total_macs += macs
761 total_cycles += cycles
Tim Halld8339a72021-05-27 18:49:40 +0100762 prev_op = sched_op
Tim Hall79d07d22020-04-27 18:20:16 +0100763
764 nng.bandwidths = total_bws
765 nng.macs = total_macs
766 nng.cycles = total_cycles
Ayaan Masoodb801dda2022-02-22 11:28:55 +0000767 nng.total_original_weights = total_weight_size
768 nng.total_npu_encoded_weights = total_encoded_weight_size