Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1 | # 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 22 | import enum |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 23 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 24 | import numpy as np |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 25 | |
| 26 | from . import numeric_util |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 27 | from .architecture_features import Block |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 28 | from .nn_graph import PassPlacement |
| 29 | from .nn_graph import SchedulerRewrite |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 30 | from .operation import NpuBlockType |
Louis Verhaard | 93dc553 | 2020-06-07 12:40:18 +0200 | [diff] [blame] | 31 | from .register_command_stream_generator import get_op_kernel |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 32 | from .tensor import MemArea |
| 33 | from .tensor import shape_num_elements |
| 34 | from .tensor import TensorBlockTraversal |
| 35 | from .tensor import TensorPurpose |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def rolling_buffer_dims_from_passes(arch, ps1, block_config_ps1, ps2, block_config_ps2): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 39 | ofm_block = Block(block_config_ps2[-3], block_config_ps2[-4], block_config_ps2[-1]) |
Louis Verhaard | 93dc553 | 2020-06-07 12:40:18 +0200 | [diff] [blame] | 40 | kernel = get_op_kernel(ps2) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 41 | |
| 42 | if ps2.npu_block_type in set((NpuBlockType.ConvolutionMxN, NpuBlockType.VectorProduct)): |
Louis Verhaard | 93dc553 | 2020-06-07 12:40:18 +0200 | [diff] [blame] | 43 | op = ps2.primary_op |
| 44 | ifm_idx, _, _, _, _ = op.get_ifm_ifm2_weight_bias_ofm_indices() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 45 | ifm_block_depth = arch.calc_ifm_block_depth( |
| 46 | op.inputs[ifm_idx].shape[-1], op.inputs[ifm_idx].dtype.size_in_bits() |
| 47 | ) |
| 48 | else: |
| 49 | ifm_block_depth = block_config_ps2[-1] |
| 50 | |
Louis Verhaard | 93dc553 | 2020-06-07 12:40:18 +0200 | [diff] [blame] | 51 | ifm_block = arch.get_ifm_block_size(ifm_block_depth, ofm_block, kernel, arch.ofm_block_max) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 52 | |
| 53 | # The performed height calculation is for worst case |
| 54 | height = numeric_util.round_up(ifm_block.height + block_config_ps1[0], block_config_ps1[0]) |
| 55 | width = ifm_block.width |
Louis Verhaard | 93dc553 | 2020-06-07 12:40:18 +0200 | [diff] [blame] | 56 | return [height, width] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 57 | |
| 58 | |
| 59 | class PassCycles(enum.IntEnum): |
| 60 | Dpu = 0 |
| 61 | ElementWise = 1 |
| 62 | Cpu = 2 |
| 63 | SramAccess = 3 |
| 64 | TotalPerPass = 4 |
| 65 | DramAccess = 5 |
| 66 | OnChipFlashAccess = 6 |
| 67 | OffChipFlashAccess = 7 |
| 68 | Total = 8 |
| 69 | Size = 9 |
| 70 | |
| 71 | def display_name(self): |
| 72 | return ( |
| 73 | "DPU", |
| 74 | "Element wise", |
| 75 | "CPU", |
| 76 | "SRAM Access", |
| 77 | "Total per Pass", |
| 78 | "DRAM Access", |
| 79 | "On-chip Flash Access", |
| 80 | "Off-chip Flash Access", |
| 81 | "Total", |
| 82 | "Size", |
| 83 | )[self.value] |
| 84 | |
| 85 | def identifier_name(self): |
| 86 | return ( |
| 87 | "dpu", |
| 88 | "element_wise", |
| 89 | "cpu", |
| 90 | "sram_access", |
| 91 | "total_per_pass", |
| 92 | "dram_access", |
| 93 | "on_chip_flash_access", |
| 94 | "off_chip_flash_access", |
| 95 | "total", |
| 96 | "size", |
| 97 | )[self.value] |
| 98 | |
| 99 | @staticmethod |
| 100 | def all(): |
| 101 | return ( |
| 102 | PassCycles.Dpu, |
| 103 | PassCycles.ElementWise, |
| 104 | PassCycles.Cpu, |
| 105 | PassCycles.SramAccess, |
| 106 | PassCycles.DramAccess, |
| 107 | PassCycles.OnChipFlashAccess, |
| 108 | PassCycles.OffChipFlashAccess, |
| 109 | PassCycles.Total, |
| 110 | ) |
| 111 | |
| 112 | |
| 113 | class MacCount(enum.IntEnum): |
| 114 | NeuralNetworkMacs = 0 |
| 115 | HardwareMacs = 1 |
| 116 | Size = 2 |
| 117 | |
| 118 | def display_name(self): |
| 119 | return ("Neural Network Macs", "Hardware Macs", "Size")[self.value] |
| 120 | |
| 121 | def identifier_name(self): |
| 122 | return ("nn_macs", "hardware_macs", "size")[self.value] |
| 123 | |
| 124 | @staticmethod |
| 125 | def all(): |
| 126 | return (MacCount.NeuralNetworkMacs, MacCount.HardwareMacs) |
| 127 | |
| 128 | |
| 129 | class BandwidthDirection(enum.IntEnum): |
| 130 | Read = 0 |
| 131 | Write = 1 |
| 132 | Size = 2 |
| 133 | |
| 134 | def display_name(self): |
| 135 | return self.name |
| 136 | |
| 137 | def identifier_name(self): |
| 138 | return self.name.lower() |
| 139 | |
| 140 | @staticmethod |
| 141 | def all(): |
| 142 | return (BandwidthDirection.Read, BandwidthDirection.Write) |
| 143 | |
| 144 | |
| 145 | def make_bandwidth_array(): |
| 146 | return np.zeros((MemArea.Size, TensorPurpose.Size, BandwidthDirection.Size)) |
| 147 | |
| 148 | |
| 149 | def make_macs_array(): |
| 150 | return np.zeros(MacCount.Size, np.int) |
| 151 | |
| 152 | |
| 153 | def make_cycles_array(): |
| 154 | return np.zeros(PassCycles.Size) |
| 155 | |
| 156 | |
| 157 | def make_metrics_arrays(): |
| 158 | return (make_bandwidth_array(), make_macs_array(), make_cycles_array()) |
| 159 | |
| 160 | |
| 161 | def get_n_blocks_and_area( |
| 162 | ifm_brick_size, ifm_height_width, orig_skirt, clamped_skirt, block_config, min_block_size, strides |
| 163 | ): |
| 164 | |
| 165 | ifm_block_config = (block_config[0] * strides[1], block_config[1] * strides[2]) |
| 166 | |
| 167 | n_normal_blocks = [] |
| 168 | remainder_size = [] |
| 169 | for i in range(2): |
| 170 | non_skirt_dim = ifm_height_width[i] - orig_skirt[i] - orig_skirt[2 + i] |
| 171 | n_blocks = non_skirt_dim // ifm_block_config[i] |
| 172 | n_normal_blocks.append(n_blocks) |
| 173 | remainder_dim = numeric_util.round_up( |
| 174 | ((non_skirt_dim - n_blocks * ifm_block_config[i] - 1) // strides[i + 1]) + 1, min_block_size[i] |
| 175 | ) |
| 176 | remainder_size.append(remainder_dim) |
| 177 | |
| 178 | # this will actually calculate reads into the edge padding. |
| 179 | |
| 180 | # there are four cases in total, handling the edges that will not fill a complete block. |
| 181 | |
| 182 | # 0000000001 |
| 183 | # 0000000001 |
| 184 | # 0000000001 |
| 185 | # 0000000001 |
| 186 | # 0000000001 |
| 187 | # 0000000001 |
| 188 | # 2222222223 |
| 189 | total_blocks = 0 |
| 190 | total_area = 0 |
| 191 | |
| 192 | block_setup = ( |
| 193 | (n_normal_blocks[0] * n_normal_blocks[1], block_config), |
| 194 | (1 * n_normal_blocks[1], (remainder_size[0], block_config[1])), |
| 195 | (n_normal_blocks[0] * 1, (block_config[0], remainder_size[1])), |
| 196 | (1 * 1, remainder_size), |
| 197 | ) |
| 198 | |
| 199 | for n_blocks, block_size in block_setup: |
| 200 | if block_size[0] == 0 or block_size[1] == 0: |
| 201 | continue |
| 202 | read_dims = [0, 0] |
| 203 | for i in range(2): |
| 204 | read_dims[i] = ( |
| 205 | numeric_util.round_up(clamped_skirt[i], ifm_brick_size[i + 1]) |
| 206 | + block_size[i] * strides[i + 1] |
| 207 | + numeric_util.round_up(clamped_skirt[2 + i], ifm_brick_size[i + 1]) |
| 208 | ) |
| 209 | assert n_blocks >= 0 |
| 210 | total_blocks += n_blocks |
| 211 | total_area += n_blocks * read_dims[0] * read_dims[1] |
| 212 | assert total_blocks >= 1 |
| 213 | return total_blocks, total_area, block_setup |
| 214 | |
| 215 | |
| 216 | def performance_metrics_for_pass(arch, ps, block_config=None, rewrite_list=[], force_outputs_to_fast_storage=False): |
| 217 | if block_config is None: |
| 218 | block_config = ps.block_config |
| 219 | bws = make_bandwidth_array() |
| 220 | macs = make_macs_array() |
| 221 | cycles = make_cycles_array() |
| 222 | blocks = 0 |
| 223 | ifm_read_multiple = 1 |
| 224 | weight_read_multiple = 0 |
| 225 | |
| 226 | if ps.placement in set((PassPlacement.MemoryOnly, PassPlacement.StartupInit)): |
| 227 | return bws, macs, cycles, blocks, ifm_read_multiple, weight_read_multiple # nothing real happening in this pass |
| 228 | |
| 229 | min_block_size = arch.min_block_sizes[ps.npu_block_type] |
| 230 | |
| 231 | skirt = (0, 0, 0, 0) |
| 232 | explicit_padding = (0, 0, 0, 0) |
| 233 | primary_op = ps.primary_op |
| 234 | replacement_read_bws = {} |
Charles Xu | b02c8d9 | 2020-06-25 16:05:25 +0200 | [diff] [blame] | 235 | if ps.placement == PassPlacement.Cpu: |
| 236 | cycles[PassCycles.Cpu] = arch.cpu_cycle_estimate(ps.ops[0]) |
| 237 | elif primary_op: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 238 | skirt = primary_op.attrs.get("skirt", skirt) |
| 239 | explicit_padding = primary_op.attrs.get("explicit_padding", explicit_padding) |
| 240 | assert primary_op.attrs["npu_block_type"] == ps.npu_block_type |
| 241 | npu_block_type = primary_op.attrs["npu_block_type"] |
| 242 | |
| 243 | ifm_tensor, _, weight_tensor, ofm_tensor = ps.get_primary_op_ifm_ifm2_weights_ofm() |
| 244 | |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 245 | if npu_block_type in set( |
| 246 | (NpuBlockType.ConvolutionMxN, NpuBlockType.ConvolutionDepthWise, NpuBlockType.Pooling) |
| 247 | ): |
Charles Xu | 3e9c434 | 2020-04-22 08:31:43 +0200 | [diff] [blame] | 248 | # extent the ifm to full dimension |
| 249 | ifm_tensor_brick_size = tuple(numeric_util.full_shape(4, list(ifm_tensor.brick_size), 1)) |
| 250 | ifm_tensor_shape = numeric_util.full_shape(4, ifm_tensor.shape, 1) |
| 251 | ifm_tensor_bandwidth_shape = numeric_util.full_shape(4, ifm_tensor.bandwidth_shape, 1) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 252 | |
| 253 | batch_size = ifm_tensor.shape[0] |
Charles Xu | 3e9c434 | 2020-04-22 08:31:43 +0200 | [diff] [blame] | 254 | ifm_depth = ifm_tensor_bandwidth_shape[3] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 255 | |
| 256 | # add in padding |
| 257 | ifm_tensor_shape[1] += explicit_padding[0] + explicit_padding[2] # height += top and bottom |
| 258 | ifm_tensor_shape[2] += explicit_padding[1] + explicit_padding[3] # width += left and right |
| 259 | |
| 260 | strides = primary_op.attrs["strides"] |
| 261 | if npu_block_type != NpuBlockType.Pooling: |
| 262 | weight_tensor_shape = weight_tensor.shape |
| 263 | weight_tensor_bandwidth_shape = weight_tensor.bandwidth_shape |
| 264 | weight_tensor_element_size = weight_tensor.element_size() |
| 265 | weight_tensor_bandwidth_compression_scale = weight_tensor.bandwidth_compression_scale |
| 266 | nn_ops = ( |
| 267 | int(ofm_tensor.shape[0]) |
| 268 | * int(ofm_tensor.shape[1]) |
| 269 | * int(ofm_tensor.shape[2]) |
| 270 | * int(weight_tensor_shape[0]) |
| 271 | * int(weight_tensor_shape[1]) |
| 272 | * int(weight_tensor_shape[2]) |
| 273 | * int(weight_tensor_shape[3]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 274 | ) |
| 275 | else: |
| 276 | weight_tensor_shape = [ |
| 277 | primary_op.attrs["ksize"][1], |
| 278 | primary_op.attrs["ksize"][2], |
| 279 | 1, |
| 280 | ifm_tensor_shape[3], |
| 281 | ] |
| 282 | weight_tensor_bandwidth_shape = weight_tensor_shape |
| 283 | weight_tensor_element_size = 0 |
| 284 | weight_tensor_bandwidth_compression_scale = 0.0 |
| 285 | nn_ops = 0 # pooling doesn't count as NN ops |
| 286 | |
| 287 | kernel_dims = weight_tensor_shape[:2] |
| 288 | |
| 289 | sub_kernel_limits = arch.sub_kernel_limits[npu_block_type] |
| 290 | # count the sub kernels; the IFM block needs to be refetched for each of them |
| 291 | n_sub_kernels_y = numeric_util.round_up_divide(kernel_dims[0], sub_kernel_limits[0]) |
| 292 | n_sub_kernels_x = numeric_util.round_up_divide(kernel_dims[1], sub_kernel_limits[1]) |
| 293 | n_sub_kernels = n_sub_kernels_y * n_sub_kernels_x |
| 294 | |
| 295 | clamped_skirt = list(skirt) |
| 296 | clamped_skirt[2] = min(clamped_skirt[2], sub_kernel_limits[0] - 1 - clamped_skirt[0]) |
| 297 | clamped_skirt[3] = min(clamped_skirt[3], sub_kernel_limits[1] - 1 - clamped_skirt[1]) |
| 298 | n_blocks, area, block_setup = get_n_blocks_and_area( |
Charles Xu | 3e9c434 | 2020-04-22 08:31:43 +0200 | [diff] [blame] | 299 | ifm_tensor_brick_size, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 300 | ifm_tensor_shape[1:3], |
| 301 | skirt, |
| 302 | clamped_skirt, |
| 303 | block_config, |
| 304 | min_block_size, |
| 305 | strides, |
| 306 | ) |
| 307 | |
| 308 | blocks = n_blocks * numeric_util.round_up_divide(weight_tensor_shape[3], block_config[3]) |
| 309 | |
| 310 | n_weight_stages = numeric_util.round_up_divide(weight_tensor_bandwidth_shape[3], block_config[3]) |
| 311 | if npu_block_type == NpuBlockType.ConvolutionDepthWise or npu_block_type == NpuBlockType.Pooling: |
| 312 | n_weight_stages = 1 # force to no reread |
| 313 | |
| 314 | ifm_tensor_bw = ( |
| 315 | n_sub_kernels |
| 316 | * batch_size |
| 317 | * area |
| 318 | * ifm_depth |
| 319 | * n_weight_stages |
| 320 | * ifm_tensor.element_size() |
| 321 | * ifm_tensor.bandwidth_compression_scale |
| 322 | ) |
| 323 | replacement_read_bws[ifm_tensor] = ifm_tensor_bw |
| 324 | ifm_read_multiple = n_weight_stages |
| 325 | |
| 326 | replacement_read_bws[weight_tensor] = ( |
| 327 | batch_size |
| 328 | * shape_num_elements(weight_tensor_bandwidth_shape) |
| 329 | * weight_tensor_element_size |
| 330 | * weight_tensor_bandwidth_compression_scale |
| 331 | * n_blocks |
| 332 | ) # read once per block and batch |
| 333 | weight_read_multiple = n_blocks |
| 334 | |
| 335 | n_kernel_xy = kernel_dims[0] * kernel_dims[1] |
| 336 | n_input_channels_at_a_time = block_config[2] |
| 337 | |
| 338 | if npu_block_type == NpuBlockType.Pooling or weight_tensor.block_traversal in set( |
| 339 | (TensorBlockTraversal.PartKernelFirst, TensorBlockTraversal.DepthWise) |
| 340 | ): |
| 341 | n_input_channels_at_a_time = numeric_util.round_up_divide(n_input_channels_at_a_time, 4) |
| 342 | n_kernel_xy = max( |
| 343 | n_kernel_xy, 4 |
| 344 | ) # need at least 4, as this is the minimum duty cycle for secondary accumulator writes |
| 345 | if weight_tensor is not None: |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 346 | n_kernel_xy = numeric_util.round_up(n_kernel_xy, 4) # weights need to be read in blocks of 4 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 347 | |
| 348 | num_mac_ops = 0 |
| 349 | for n_blocks_for_size, block_size in block_setup: |
| 350 | num_mac_ops += ( |
| 351 | batch_size |
| 352 | * n_blocks_for_size |
| 353 | * block_size[0] |
| 354 | * block_size[1] |
| 355 | * numeric_util.round_up(weight_tensor_shape[2], n_input_channels_at_a_time) |
| 356 | * numeric_util.round_up(weight_tensor_shape[3], block_config[3]) |
| 357 | * n_kernel_xy |
| 358 | ) |
| 359 | |
| 360 | if npu_block_type == NpuBlockType.Pooling: |
| 361 | # TODO: improve pooling estimation |
| 362 | cycles[PassCycles.Dpu] = num_mac_ops / arch.num_macs_per_cycle / 2 |
| 363 | else: |
| 364 | cycles[PassCycles.Dpu] = num_mac_ops / arch.num_macs_per_cycle |
| 365 | macs[MacCount.NeuralNetworkMacs] += nn_ops |
| 366 | macs[MacCount.HardwareMacs] += num_mac_ops |
| 367 | |
| 368 | elif npu_block_type == NpuBlockType.VectorProduct: |
| 369 | nn_macs = ( |
| 370 | ifm_tensor.shape[0] |
| 371 | * numeric_util.round_up(weight_tensor.shape[-2], block_config[2]) |
| 372 | * numeric_util.round_up(weight_tensor.shape[-1], block_config[3]) |
| 373 | ) |
| 374 | num_mac_ops = nn_macs |
| 375 | |
| 376 | cycles[PassCycles.Dpu] = num_mac_ops / arch.num_macs_per_cycle |
| 377 | macs[MacCount.NeuralNetworkMacs] += nn_macs |
| 378 | macs[MacCount.HardwareMacs] += num_mac_ops |
| 379 | |
| 380 | blocks = 1 * numeric_util.round_up_divide(weight_tensor.shape[-1], block_config[3]) |
| 381 | |
| 382 | non_zero_fraction = 1.0 |
| 383 | if ifm_tensor.values is not None: |
| 384 | nz_vector = np.amax(ifm_tensor.values != 0, axis=0) # max across batch axis |
| 385 | non_zero_fraction = np.average(nz_vector) |
| 386 | |
| 387 | replacement_read_bws[ifm_tensor] = ifm_tensor.bandwidth() |
| 388 | replacement_read_bws[weight_tensor] = weight_tensor.bandwidth() * non_zero_fraction |
| 389 | ifm_read_multiple = 1 |
| 390 | weight_read_multiple = non_zero_fraction |
| 391 | else: |
| 392 | if ps.placement == PassPlacement.Npu and len(ps.outputs): |
| 393 | # Assume element-wise operation going through the element pipelines. |
| 394 | # Work out how many elements we have and calculate performance. |
| 395 | out = ps.outputs[0] |
| 396 | elms = out.elements() |
| 397 | |
| 398 | cycles[PassCycles.ElementWise] = numeric_util.round_up_divide(elms, arch.num_elem_wise_units) |
| 399 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 400 | # apply the desired rewrites |
| 401 | for rewrite_op, tens, _, _, _, ps_to_rewrite in rewrite_list: |
| 402 | if ps != ps_to_rewrite: |
| 403 | continue |
| 404 | if rewrite_op == SchedulerRewrite.Nop: |
| 405 | pass # these are fine, no bandwidth changes |
| 406 | elif rewrite_op in (SchedulerRewrite.ChangeTensorSubPurpose,): |
| 407 | bws[arch.fast_storage_mem_area][tens.purpose][BandwidthDirection.Read] += replacement_read_bws[tens] |
| 408 | replacement_read_bws[tens] = 0 |
| 409 | |
| 410 | for tens in ps.outputs: |
| 411 | if force_outputs_to_fast_storage: |
| 412 | bws[arch.fast_storage_mem_area][tens.purpose][BandwidthDirection.Write] += tens.bandwidth() |
| 413 | else: |
| 414 | bws[tens.mem_area][tens.purpose][BandwidthDirection.Write] += tens.bandwidth() |
| 415 | |
| 416 | for tens in ps.intermediates: |
| 417 | bws[tens.mem_area][tens.purpose][BandwidthDirection.Write] += tens.bandwidth() |
| 418 | |
| 419 | if tens in replacement_read_bws: |
| 420 | bw = replacement_read_bws[tens] |
| 421 | else: |
| 422 | bw = tens.bandwidth() |
| 423 | |
| 424 | bws[tens.mem_area][tens.purpose][BandwidthDirection.Read] += bw |
| 425 | |
| 426 | for tens in ps.inputs: |
| 427 | if tens in replacement_read_bws: |
| 428 | bw = replacement_read_bws[tens] |
| 429 | else: |
| 430 | bw = tens.bandwidth() |
| 431 | |
| 432 | bws[tens.mem_area][tens.purpose][BandwidthDirection.Read] += bw |
| 433 | |
| 434 | cycles[PassCycles.SramAccess] = np.sum(bws[MemArea.Sram]) / arch.memory_bandwidths_per_cycle[MemArea.Sram] |
| 435 | cycles[PassCycles.TotalPerPass] = np.max(cycles[: PassCycles.TotalPerPass]) |
| 436 | |
| 437 | # quick build access counts for only current pass, even though these aren't the final numbers |
| 438 | update_summary_cycles(arch, bws, macs, cycles) |
| 439 | |
| 440 | return bws, macs, cycles, blocks, ifm_read_multiple, weight_read_multiple |
| 441 | |
| 442 | |
| 443 | def update_summary_cycles(arch, bws, macs, cycles): |
| 444 | cycles[PassCycles.DramAccess] = np.sum(bws[MemArea.Dram]) / arch.memory_bandwidths_per_cycle[MemArea.Dram] |
| 445 | cycles[PassCycles.OnChipFlashAccess] = ( |
| 446 | np.sum(bws[MemArea.OnChipFlash]) / arch.memory_bandwidths_per_cycle[MemArea.OnChipFlash] |
| 447 | ) |
| 448 | cycles[PassCycles.OffChipFlashAccess] = ( |
| 449 | np.sum(bws[MemArea.OffChipFlash]) / arch.memory_bandwidths_per_cycle[MemArea.OffChipFlash] |
| 450 | ) |
| 451 | |
| 452 | cycles[PassCycles.Total] = np.max(cycles[: PassCycles.Total]) |
| 453 | return cycles |
| 454 | |
| 455 | |
| 456 | def collate_stats_for_cascaded_pass(arch, bws, macs, cycles): |
| 457 | return bws, macs, cycles |
| 458 | |
| 459 | |
| 460 | def performance_for_cascaded_pass(arch, cps): |
| 461 | total_bws = make_bandwidth_array() |
| 462 | total_macs = make_macs_array() |
| 463 | total_cycles = make_cycles_array() |
| 464 | |
| 465 | for ps in cps.passes: |
| 466 | bws, macs, cycles, blocks, _, _ = performance_metrics_for_pass(arch, ps) |
| 467 | ps.bandwidths = bws |
| 468 | ps.macs = macs |
| 469 | ps.cycles = cycles |
| 470 | ps.n_blocks = blocks |
| 471 | total_bws += bws |
| 472 | total_macs += macs |
| 473 | total_cycles += cycles |
| 474 | |
| 475 | bws, macs, cycles = collate_stats_for_cascaded_pass(arch, total_bws, total_macs, total_cycles) |
| 476 | cps.bandwidths = bws |
| 477 | cps.macs = macs |
| 478 | cps.cycles = cycles |
| 479 | return bws, macs, cycles |
| 480 | |
| 481 | |
| 482 | def calc_performance_for_network(nng, arch): |
| 483 | total_bws = make_bandwidth_array() |
| 484 | total_macs = np.zeros(MacCount.Size) |
| 485 | total_cycles = np.zeros(PassCycles.Size) |
| 486 | |
| 487 | for sg in nng.subgraphs: |
| 488 | for cps in sg.cascaded_passes: |
| 489 | bws, macs, cycles = performance_for_cascaded_pass(arch, cps) |
| 490 | total_bws += bws |
| 491 | total_macs += macs |
| 492 | total_cycles += cycles |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 493 | |
| 494 | nng.bandwidths = total_bws |
| 495 | nng.macs = total_macs |
| 496 | nng.cycles = total_cycles |