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 | # Holds a container for Ethos-U55/System architecture parameters. |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 18 | import enum |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 19 | from collections import namedtuple |
| 20 | from configparser import ConfigParser |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 21 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 22 | import numpy as np |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 23 | |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 24 | from .errors import OptionError |
Dwight Lidman | a9390f7 | 2020-05-13 12:00:08 +0200 | [diff] [blame] | 25 | from .ethos_u55_regs.ethos_u55_regs import resampling_mode |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 26 | from .numeric_util import round_up |
| 27 | from .numeric_util import round_up_divide |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 28 | from .operation import NpuBlockType |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 29 | from .supported_operators import SupportedOperators |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 30 | from .tensor import MemArea |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 31 | from .tensor import MemType |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 32 | from .tensor import TensorFormat |
| 33 | from .tensor import TensorPurpose |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 34 | |
| 35 | PointXY = namedtuple("PointXY", "x y") |
| 36 | PointXYZ = namedtuple("PointXYZ", "x y z") |
| 37 | |
| 38 | |
| 39 | class Block: |
| 40 | def __init__(self, w, h, d): |
| 41 | self.width = w |
| 42 | self.height = h |
| 43 | self.depth = d |
| 44 | |
| 45 | def __eq__(self, other): |
| 46 | if self.width == other.width and self.height == other.height and self.depth == other.depth: |
| 47 | return True |
| 48 | else: |
| 49 | return False |
| 50 | |
| 51 | def __repr__(self): |
| 52 | return "<Block: {0},{1},{2}>".format(self.width, self.height, self.depth) |
| 53 | |
| 54 | @classmethod |
| 55 | def from_string(cls, s): |
| 56 | w, h, c = (int(v) for v in s.split("x")) |
| 57 | return cls(w, h, c) |
| 58 | |
| 59 | |
| 60 | class Rect: |
| 61 | def __init__(self, x, y, z, x2, y2, z2): |
| 62 | self.x = x |
| 63 | self.y = y |
| 64 | self.z = z |
| 65 | self.x2 = x2 |
| 66 | self.y2 = y2 |
| 67 | self.z2 = z2 |
| 68 | |
| 69 | def start(self): |
| 70 | return PointXYZ(self.x, self.y, self.z) |
| 71 | |
| 72 | def end(self): |
| 73 | return PointXYZ(self.x2, self.y2, self.z2) |
| 74 | |
| 75 | def size(self): |
| 76 | return Block(self.x2 - self.x + 1, self.y2 - self.y + 1, self.z2 - self.z + 1) |
| 77 | |
| 78 | def __repr__(self): |
| 79 | return "<Rect: ({0},{1},{2}) ({3},{4},{5})>".format(self.x, self.y, self.z, self.x2, self.y2, self.z2) |
| 80 | |
| 81 | |
| 82 | class Kernel: |
| 83 | def __init__(self, w, h, sx=1, sy=1, dx=1, dy=1): |
| 84 | assert sx > 0 and sy > 0 |
| 85 | assert dx > 0 and dy > 0 |
| 86 | self.width = w |
| 87 | self.height = h |
| 88 | self.stride = PointXY(sx, sy) |
| 89 | self.dilation = PointXY(dx, dy) |
| 90 | |
| 91 | |
| 92 | class SHRAMElements: |
| 93 | IFM8 = 0 |
| 94 | IFM16 = 1 |
| 95 | IFM8_Elementwise = 2 |
| 96 | IFM16_Elementwise = 3 |
| 97 | Acc16 = 4 |
| 98 | Acc32 = 5 |
| 99 | Acc40 = 6 |
| 100 | Last = Acc40 |
| 101 | BitSizes = np.array([8, 16, 8, 16, 16, 32, 40], np.int32) |
Louis Verhaard | f98c674 | 2020-05-12 14:22:38 +0200 | [diff] [blame] | 102 | ByteSizes = BitSizes // 8 |
| 103 | PostAlign = np.array([8, 8, 8, 8, 1, 1, 1], np.int32) |
| 104 | PreAlign = np.array([1, 1, 1, 1, 8, 8, 8], np.int32) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 105 | |
| 106 | |
| 107 | class SHRAMBlockConfig: |
| 108 | def __init__(self, sizes, banks): |
| 109 | assert len(banks) == SHRAMElements.Last + 1 |
| 110 | self.sizes = sizes |
| 111 | self.banks = banks |
| 112 | |
| 113 | |
| 114 | # Area indices must match Ethos-U55 SHRAM layout spec |
| 115 | class SharedBufferArea(enum.IntEnum): |
| 116 | OFM = 0 |
| 117 | Weights = 1 |
| 118 | IFM = 2 |
| 119 | Accumulators = 3 |
| 120 | Size = Accumulators + 1 |
| 121 | |
| 122 | |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame^] | 123 | class Accelerator(enum.Enum): |
| 124 | Ethos_U55_32 = "ethos-u55-32" |
| 125 | Ethos_U55_64 = "ethos-u55-64" |
| 126 | Ethos_U55_128 = "ethos-u55-128" |
| 127 | Ethos_U55_256 = "ethos-u55-256" |
| 128 | Yoda_256 = "yoda-256" |
| 129 | Yoda_512 = "yoda-512" |
| 130 | |
| 131 | @classmethod |
| 132 | def member_list(cls): |
| 133 | return [e.value for e in cls] |
| 134 | |
| 135 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 136 | class ArchitectureFeatures: |
| 137 | """This class is a container for various parameters of the Ethos-U55 core |
| 138 | and system configuration that can be tuned, either by command line |
| 139 | parameters or by the Ethos-U55 architects. The class is often passed |
| 140 | around to passes that need to do architecture-dependent actions. |
| 141 | |
| 142 | Note the difference between ArchitectureFeatures and CompilerOptions |
| 143 | - ArchitectureFeatures is for changing the Ethos-U55 and system architecture |
| 144 | - CompilerOptions is for changing the behaviour of the compiler |
| 145 | |
| 146 | """ |
| 147 | |
| 148 | ArchitectureConfig = namedtuple( |
| 149 | "ArchitectureConfig", "macs cores ofm_ublock ifm_ublock shram_banks shram_granules elem_units" |
| 150 | ) |
| 151 | accelerator_configs = { |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame^] | 152 | Accelerator.Yoda_512: ArchitectureConfig( |
| 153 | 256, 2, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 8, 16, 20], 8 |
| 154 | ), |
| 155 | Accelerator.Yoda_256: ArchitectureConfig( |
| 156 | 256, 1, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 8, 16, 20], 8 |
| 157 | ), |
| 158 | Accelerator.Ethos_U55_256: ArchitectureConfig( |
| 159 | 256, 1, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 8, 16, 20], 8 |
| 160 | ), |
| 161 | Accelerator.Ethos_U55_128: ArchitectureConfig( |
| 162 | 128, 1, Block(2, 1, 8), Block(2, 2, 8), 24, [4, 4, 4, 4, 4, 8, 12], 4 |
| 163 | ), |
| 164 | Accelerator.Ethos_U55_64: ArchitectureConfig( |
| 165 | 64, 1, Block(1, 1, 8), Block(1, 1, 8), 16, [2, 2, 2, 2, 4, 4, 8], 2 |
| 166 | ), |
| 167 | Accelerator.Ethos_U55_32: ArchitectureConfig( |
| 168 | 32, 1, Block(1, 1, 4), Block(1, 1, 8), 16, [2, 2, 2, 2, 4, 4, 4], 1 |
| 169 | ), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | OFMSplitDepth = 16 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame^] | 173 | SubKernelMax = Block(8, 8, 65536) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 174 | |
| 175 | def __init__( |
| 176 | self, |
| 177 | vela_config: ConfigParser, |
| 178 | accelerator_config, |
| 179 | system_config, |
| 180 | permanent_storage, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 181 | override_block_config, |
| 182 | block_config_limit, |
| 183 | global_memory_clock_scale, |
| 184 | max_blockdep, |
| 185 | ): |
| 186 | accelerator_config = accelerator_config.lower() |
| 187 | self.vela_config = vela_config |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame^] | 188 | if accelerator_config not in Accelerator.member_list(): |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 189 | raise OptionError("--accelerator-config", self.accelerator_config, "Unknown accelerator configuration") |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame^] | 190 | self.accelerator_config = Accelerator(accelerator_config) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 191 | accel_config = ArchitectureFeatures.accelerator_configs[self.accelerator_config] |
| 192 | self.config = accel_config |
| 193 | |
| 194 | self.system_config = system_config |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame^] | 195 | self.is_yoda_system = self.accelerator_config in (Accelerator.Yoda_256, Accelerator.Yoda_512) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 196 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 197 | self.ncores = accel_config.cores |
| 198 | self.ofm_ublock = accel_config.ofm_ublock |
| 199 | self.ifm_ublock = accel_config.ifm_ublock |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 200 | self.ofm_block_max = Block(64, 32, 128) |
| 201 | self.override_block_config = override_block_config |
| 202 | self.block_config_limit = block_config_limit |
| 203 | |
| 204 | self.global_memory_clock_scale = global_memory_clock_scale |
| 205 | if self.global_memory_clock_scale <= 0.0 or self.global_memory_clock_scale > 1.0: |
| 206 | raise Exception( |
| 207 | "Invalid global_memory_clock_scale = " |
| 208 | + str(self.global_memory_clock_scale) |
| 209 | + " (must be > 0.0 and <= 1.0)" |
| 210 | ) |
| 211 | |
| 212 | self.max_blockdep = max_blockdep |
| 213 | |
| 214 | dpu_min_height = accel_config.ofm_ublock.height |
| 215 | dpu_min_width = accel_config.ofm_ublock.width |
| 216 | dpu_dot_product_width = 8 |
| 217 | dpu_min_ofm_channels = accel_config.ofm_ublock.depth |
| 218 | |
| 219 | self.num_elem_wise_units = accel_config.elem_units |
| 220 | self.num_macs_per_cycle = dpu_min_height * dpu_min_width * dpu_dot_product_width * dpu_min_ofm_channels |
| 221 | |
| 222 | self.memory_clock_scales = np.zeros(MemArea.Size) |
| 223 | self.memory_port_widths = np.zeros(MemArea.Size) |
| 224 | |
| 225 | # Get system configuration |
Tim Hall | 42e4189 | 2020-07-06 10:51:31 +0100 | [diff] [blame] | 226 | self.__read_sys_config(self.is_yoda_system) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 227 | |
| 228 | # apply the global memory clock scales to the individual ones from the system config |
| 229 | for mem in MemArea.all(): |
| 230 | self.memory_clock_scales[mem] *= self.global_memory_clock_scale |
| 231 | |
| 232 | self.memory_clocks = self.memory_clock_scales * self.npu_clock |
| 233 | self.memory_bandwidths_per_cycle = self.memory_port_widths * self.memory_clock_scales / 8 |
| 234 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 235 | self.memory_bandwidths_per_second = self.memory_bandwidths_per_cycle * self.npu_clock |
| 236 | |
| 237 | # sizes as N x H x W x C. we need to round up to these when allocating storage |
| 238 | self.storage_rounding_quantums = { |
| 239 | TensorFormat.Unknown: (1, 1, 1, 1), |
| 240 | TensorFormat.WeightsCompressed: (1, 1, 1, 1), |
| 241 | TensorFormat.NHWC: (1, 1, 1, 1), |
| 242 | TensorFormat.NHCWB16: (1, 1, 1, 16), |
| 243 | } |
| 244 | |
| 245 | # brick sizes as N x H x W x C. We have to fetch whole bricks at a time |
| 246 | self.brick_sizes = { |
| 247 | TensorFormat.Unknown: (1, 1, 1, 1), |
| 248 | TensorFormat.WeightsCompressed: (1, 1, 1, 1), |
| 249 | TensorFormat.NHWC: (1, 1, 1, 1), |
| 250 | TensorFormat.NHCWB16: (1, 1, 1, 16), |
| 251 | } |
| 252 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 253 | self.default_weight_format = TensorFormat.WeightsCompressed |
| 254 | self.default_feature_map_format = TensorFormat.NHWC |
| 255 | |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 256 | # This is to ignore permanent_storage = On/OffChipflash for Yoda |
Tim Hall | 42e4189 | 2020-07-06 10:51:31 +0100 | [diff] [blame] | 257 | if not self.is_yoda_system and permanent_storage != MemArea.OffChipFlash: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 258 | self.permanent_storage_mem_area = permanent_storage |
| 259 | |
| 260 | self.tensor_storage_mem_area = { |
| 261 | # permanent mem_area |
Tim Hall | 465582c | 2020-05-26 09:33:14 +0100 | [diff] [blame] | 262 | TensorPurpose.Unknown: MemArea.Unknown, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 263 | TensorPurpose.Weights: self.permanent_storage_mem_area, |
| 264 | TensorPurpose.FeatureMap: self.feature_map_storage_mem_area, |
| 265 | } |
| 266 | |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 267 | self.tensor_storage_mem_type = { |
| 268 | TensorPurpose.Weights: MemType.Permanent_NPU, |
| 269 | TensorPurpose.FeatureMap: MemType.Scratch, |
| 270 | } |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 271 | |
| 272 | self.min_block_sizes = { |
| 273 | NpuBlockType.Default: (dpu_min_height, dpu_min_width), |
| 274 | NpuBlockType.VectorProduct: (1, 1), |
| 275 | NpuBlockType.ConvolutionMxN: (dpu_min_height, dpu_min_width), |
| 276 | NpuBlockType.Pooling: (dpu_min_height, dpu_min_width), |
| 277 | NpuBlockType.ConvolutionDepthWise: (dpu_min_height, dpu_min_width), |
| 278 | NpuBlockType.ElementWise: (1, 1), |
| 279 | } |
| 280 | |
| 281 | self.sub_kernel_limits = { |
| 282 | NpuBlockType.Default: (8, 8), |
| 283 | NpuBlockType.VectorProduct: (1, 1), |
| 284 | NpuBlockType.ConvolutionMxN: (8, 8), |
| 285 | NpuBlockType.Pooling: (8, 8), |
| 286 | NpuBlockType.ConvolutionDepthWise: (8, 8), |
| 287 | NpuBlockType.ElementWise: (1, 1), |
| 288 | } |
| 289 | |
| 290 | # weights for scheduler search |
| 291 | from .npu_performance import make_bandwidth_array |
| 292 | |
| 293 | self.bandwidth_weights = make_bandwidth_array() |
| 294 | self.bandwidth_weights[MemArea.Sram] = 1.0 |
| 295 | self.bandwidth_weights[MemArea.Dram] = 10.0 |
| 296 | self.bandwidth_weights[MemArea.OnChipFlash] = 2.0 |
| 297 | self.bandwidth_weights[MemArea.OffChipFlash] = 20.0 |
| 298 | self.cycles_weight = 40 |
| 299 | self.max_sram_used_weight = 1000 |
| 300 | |
Patrik Gustavsson | 3ab9452 | 2020-06-29 17:36:55 +0200 | [diff] [blame] | 301 | if self.is_yoda_system and (self.fast_storage_mem_area != self.feature_map_storage_mem_area): |
| 302 | self.max_sram_used_weight = 0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 303 | |
| 304 | # Shared Buffer Block allocations |
| 305 | self.shram_bank_size = 1024 # bytes |
| 306 | self.shram_size_bytes = accel_config.shram_banks * self.shram_bank_size |
| 307 | self.shram_reserved_output_banks = 2 |
| 308 | self.shram_reserved_weight_banks = 0 |
| 309 | self.shram_reserved_unused_banks = 2 if accel_config.shram_banks > 16 else 0 |
| 310 | self.shram_total_banks = accel_config.shram_banks - self.shram_reserved_unused_banks |
| 311 | self.shram_bank_granules = np.array(accel_config.shram_granules, np.int32) |
| 312 | |
| 313 | # Build a map of acceptable IFM/OFM block configurations up to the maximum |
| 314 | # IFM/OFM block size. |
| 315 | ifm_block_max = self.get_ifm_block_size(32, self.ofm_block_max, Kernel(8, 8)) |
| 316 | self.block_config_map = dict() |
| 317 | self.generate_block_config_map(Block(ifm_block_max.width, ifm_block_max.height, 128)) |
| 318 | |
| 319 | # Setup supported operators and restriction checkers class |
| 320 | self.supported_operators = SupportedOperators() |
| 321 | |
| 322 | # Calculate block configuration for ALL known IFM operations and |
| 323 | # accumulator sizes. Consumers will need to select their preferred |
| 324 | # operation and bit-width at read-time. |
| 325 | def generate_block_config(self, width, height, depth): |
Louis Verhaard | f98c674 | 2020-05-12 14:22:38 +0200 | [diff] [blame] | 326 | # Number of bytes required for any SHRAM element for a FM of given dimensions. |
| 327 | # For IFM: size = H*W*Align(D*BYTE_WIDTH, 8) |
| 328 | # For ACC: size = H*W*Align(D,8)*BYTE_WIDTH |
| 329 | d1 = round_up(depth, SHRAMElements.PreAlign) |
| 330 | d2 = round_up(d1 * SHRAMElements.ByteSizes, SHRAMElements.PostAlign) |
| 331 | size_bytes = (height * width) * d2 |
| 332 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 333 | # Convert byte size (rounded) to size in banks |
| 334 | size_banks = round_up_divide(size_bytes, self.shram_bank_size) |
| 335 | size_banks *= 2 # Double buffer the IFM/Acc (need twice as many banks) |
| 336 | # Round bank requirement to bank granularity |
| 337 | required_banks = round_up(size_banks, self.shram_bank_granules) |
| 338 | return SHRAMBlockConfig(size_bytes, required_banks) |
| 339 | |
| 340 | @staticmethod |
| 341 | def make_block_config_key(width, height, depth): |
| 342 | return (int(height), int(width), int(depth)) |
| 343 | |
| 344 | def get_block_config(self, width, height, depth): |
| 345 | assert depth <= self.ofm_block_max.depth |
| 346 | key = ArchitectureFeatures.make_block_config_key(width, height, depth) |
| 347 | config = self.block_config_map.get(key, None) |
| 348 | return config |
| 349 | |
| 350 | # Generate a key:value map of possible block configurations, where the |
| 351 | # key is compounded from the block dimensions: 0x00HHWWCC |
| 352 | def generate_block_config_map(self, block: Block): |
| 353 | for h in range(1, block.height + 1): |
| 354 | for w in range(1, block.width + 1): |
| 355 | # All possible IFM/OFM depth values |
| 356 | for c in [4, 8, 12, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128]: |
| 357 | key = ArchitectureFeatures.make_block_config_key(w, h, c) |
| 358 | self.block_config_map[key] = self.generate_block_config(w, h, c) |
| 359 | |
| 360 | def calc_ifm_block_depth(self, ifm_depth, ifm_bits): |
| 361 | assert ifm_bits == 8 or ifm_bits == 16 |
| 362 | assert ifm_depth > 0 |
| 363 | ifm_depth = round_up(ifm_depth, self.ifm_ublock.depth) |
| 364 | max_block_depth = 32 if ifm_bits == 8 else 16 |
| 365 | return min(max_block_depth, ifm_depth) |
| 366 | |
| 367 | # Calculate the size of the IFM block given a depth, target OFM block and a kernel |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 368 | def get_ifm_block_size( |
| 369 | self, |
| 370 | ifm_block_depth, |
| 371 | ofm_block: Block, |
| 372 | kernel: Kernel, |
| 373 | subkernel: Block = Block(8, 8, 65536), |
| 374 | ifm_resampling_mode=resampling_mode.NONE, |
| 375 | ): |
Dwight Lidman | a9390f7 | 2020-05-13 12:00:08 +0200 | [diff] [blame] | 376 | upscaling = 1 if ifm_resampling_mode == resampling_mode.NONE else 2 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 377 | # Height |
| 378 | ifm_odd_2x_height_enable = 0 |
| 379 | dilated_kernel_height = ((kernel.height - 1) * kernel.dilation.y) + 1 |
| 380 | ifm_block_height = ( |
| 381 | (ofm_block.height - 1) * kernel.stride.y |
| 382 | + min(subkernel.height, dilated_kernel_height) |
| 383 | + ifm_odd_2x_height_enable |
| 384 | ) // upscaling |
| 385 | |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 386 | ifm_block_height = round_up(ifm_block_height, self.ofm_ublock.height) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 387 | |
| 388 | # Width |
| 389 | ifm_odd_2x_width_enable = 0 |
| 390 | dilated_kernel_width = ((kernel.width - 1) * kernel.dilation.x) + 1 |
| 391 | ifm_block_width = ( |
| 392 | (ofm_block.width - 1) * kernel.stride.x |
| 393 | + min(subkernel.width, dilated_kernel_width) |
| 394 | + ifm_odd_2x_width_enable |
| 395 | ) // upscaling |
| 396 | |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 397 | ifm_block_width = round_up(ifm_block_width, self.ofm_ublock.width) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 398 | |
| 399 | return Block(ifm_block_width, ifm_block_height, ifm_block_depth) |
| 400 | |
| 401 | @staticmethod |
| 402 | def intersects(start_a, end_a, start_b, end_b): |
| 403 | start_x = max(start_a[0], start_b[0]) |
| 404 | end_x = min(end_a[0], end_b[0]) |
| 405 | start_y = max(start_a[1], start_b[1]) |
| 406 | end_y = min(end_a[1], end_b[1]) |
| 407 | start_z = max(start_a[2], start_b[2]) |
| 408 | end_z = min(end_a[2], end_b[2]) |
| 409 | return ((end_x - start_x) > 0) and ((end_y - start_y) > 0) and ((end_z - start_z) > 0) |
| 410 | |
| 411 | # Block job dependency: |
| 412 | # Does the VOLUME of IFMs for block job B(0) overlap with VOLUME of OFMs block jobs A(8,9,10) |
| 413 | # |
| 414 | # A | B |
| 415 | # ----------------------+------------------ |
| 416 | # .... 3,4,5,6,7,8,9,10 | 0,1,2,3,4,5,6,8 10 < JOB NUMBER |
| 417 | # |<------->| dependency offset |
| 418 | # |
| 419 | MAX_BLOCKDEP = 3 |
| 420 | |
| 421 | # Get the coordinates of a block offset from either the end (negative) |
| 422 | # or the start (zero or positive) of the given 3d area |
| 423 | def get_offset_block_coords(self, area: Rect, block: Block, offset): |
| 424 | size = area.size() |
| 425 | # Dimensions of the region, in blocks |
| 426 | width_blocks = round_up_divide(size.width, block.width) |
| 427 | height_blocks = round_up_divide(size.height, block.height) |
| 428 | depth_blocks = round_up_divide(size.depth, block.depth) |
| 429 | total_blocks = width_blocks * height_blocks * depth_blocks |
| 430 | if offset < 0: |
| 431 | index = total_blocks + offset |
| 432 | else: |
| 433 | index = offset |
| 434 | |
| 435 | if index >= total_blocks: |
| 436 | return None |
| 437 | |
| 438 | # Coordinates of the indexed block |
| 439 | coord_z = block.depth * (index % depth_blocks) |
| 440 | coord_y = block.height * (index // (depth_blocks * width_blocks)) |
| 441 | coord_x = block.width * ((index // depth_blocks) % width_blocks) |
| 442 | |
| 443 | return (coord_x + area.x, coord_y + area.y, coord_z + area.z) |
| 444 | |
| 445 | def get_first_job_input_volume( |
| 446 | self, ifm: Rect, ofm: Rect, ifm_block_depth, ofm_block: Block, kernel: Kernel, padLT, block_offset |
| 447 | ): |
| 448 | # Get ifm block size (jobs are invisibly decomposed into subkernels) |
| 449 | ifm_block = self.get_ifm_block_size(ifm_block_depth, ofm_block, kernel, self.ofm_block_max) |
| 450 | ifm_depth_blocks = round_up_divide(ifm.size().depth, ifm_block_depth) |
| 451 | |
| 452 | # Which OFM block are we calculating |
| 453 | ofm_coord = self.get_offset_block_coords(ofm, ofm_block, block_offset // ifm_depth_blocks) |
| 454 | if ofm_coord is None: |
| 455 | return None |
| 456 | |
| 457 | # Coordinate of the source IFM block |
| 458 | ifm_coord_x = max(0, ofm_coord[0] * kernel.stride.x - padLT[0]) |
| 459 | ifm_coord_y = max(0, ofm_coord[1] * kernel.stride.y - padLT[1]) |
| 460 | ifm_coord_z = ifm.z + (block_offset % ifm_depth_blocks) * ifm_block.depth |
| 461 | |
| 462 | # IFM block that will be sampled for the FIRST+block_offset job in the next operator's OFM |
| 463 | start_coord = (ifm_coord_x, ifm_coord_y, ifm_coord_z) |
| 464 | end_coord = ( |
| 465 | start_coord[0] + ifm_block.width, |
| 466 | start_coord[1] + ifm_block.height, |
| 467 | start_coord[2] + ifm_block.depth, |
| 468 | ) |
| 469 | |
| 470 | return (start_coord, end_coord, 1) # start, end, total jobs |
| 471 | |
| 472 | def get_prev_job_output_volume( |
| 473 | self, ifm: Block, ofm: Rect, ifm_block_depth, ofm_block: Block, kernel: Kernel, block_offset |
| 474 | ): |
| 475 | assert block_offset >= 0 |
| 476 | |
| 477 | # Get OFM block's volume coordinates |
| 478 | start_coord = self.get_offset_block_coords(ofm, ofm_block, -1 - block_offset) |
| 479 | if start_coord is None: |
| 480 | return None |
| 481 | end_coord = ( |
| 482 | start_coord[0] + ofm_block.width, |
| 483 | start_coord[1] + ofm_block.height, |
| 484 | start_coord[2] + ofm_block.depth, |
| 485 | ) |
| 486 | |
| 487 | # Calculate how many IFM blocks this OFM block requires (i.e how many jobs) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 488 | ifm_depth_blocks = round_up_divide(ifm.size().depth, ifm_block_depth) |
| 489 | ifm_depth_blocks = 1 # Overwrite with 1 to force OFM block dependency, not IFM |
| 490 | |
| 491 | return (start_coord, end_coord, ifm_depth_blocks) # start, end, total jobs for this OFM block |
| 492 | |
| 493 | def calc_block_dep( |
| 494 | self, |
| 495 | prev_ifm: Block, |
| 496 | prev_ofm: Block, |
| 497 | prev_ifm_block_depth, |
| 498 | prev_ofm_block: Block, |
| 499 | prev_kernel: Kernel, |
| 500 | ifm: Block, |
| 501 | ofm: Block, |
| 502 | ifm_block_depth, |
| 503 | ofm_block: Block, |
| 504 | kernel: Kernel, |
| 505 | padLT, |
| 506 | ): |
| 507 | |
| 508 | blockdep = ArchitectureFeatures.MAX_BLOCKDEP |
| 509 | |
| 510 | # Iterate over the next BLOCKDEP inputs, checking to see if a sliding window |
| 511 | # of IFM area overlaps with any previous OFM block generation. |
| 512 | elapsed_jobs = 0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 513 | for forward_offset in range(ArchitectureFeatures.MAX_BLOCKDEP): |
| 514 | # This is the IFM block we want to sample from |
| 515 | in_area = self.get_first_job_input_volume( |
| 516 | ifm, ofm, ifm_block_depth, ofm_block, kernel, padLT, forward_offset |
| 517 | ) |
| 518 | if in_area is None: |
| 519 | break |
| 520 | |
| 521 | # Try several previous-OFM blocks in the past (they still might comprise multiple IFM jobs) |
| 522 | outstanding_jobs = 0 |
| 523 | for block_offset in range(ArchitectureFeatures.MAX_BLOCKDEP): |
| 524 | # This is the OFM block being generated by the previous op |
| 525 | out_area = self.get_prev_job_output_volume( |
| 526 | prev_ifm, prev_ofm, prev_ifm_block_depth, prev_ofm_block, prev_kernel, block_offset |
| 527 | ) |
| 528 | if out_area is None: |
| 529 | break |
| 530 | |
| 531 | # Block dependency is the max number of allowed outstanding jobs |
| 532 | # in the pipeline. Selected by determining how many jobs occur |
| 533 | # in between two operators' overlapping OFM->IFM block volumes |
| 534 | if ArchitectureFeatures.intersects(in_area[0], in_area[1], out_area[0], out_area[1]): |
| 535 | break |
| 536 | # Early exit if no intersections and we've seen enough jobs in the pipeline |
| 537 | elif outstanding_jobs > ArchitectureFeatures.MAX_BLOCKDEP: |
| 538 | break |
| 539 | |
| 540 | # This OFM had this many jobs (accumulate over multiple OFM blocks) |
| 541 | outstanding_jobs += out_area[2] |
| 542 | |
| 543 | blockdep = min(blockdep, elapsed_jobs + outstanding_jobs) |
| 544 | elapsed_jobs += in_area[2] |
| 545 | # Early exit if no intersections and we've seen enough jobs in the pipeline |
| 546 | if elapsed_jobs > ArchitectureFeatures.MAX_BLOCKDEP: |
| 547 | break |
| 548 | |
| 549 | return blockdep |
| 550 | |
| 551 | def cpu_cycle_estimate(self, op): |
| 552 | """ |
| 553 | Gets estimated performance of a CPU operation, based on a linear model of intercept, slope, |
| 554 | specified in the vela config file, in ConfigParser file format (.ini file). |
| 555 | Example configuration snippet: |
| 556 | [CpuPerformance.MyOperationType] |
| 557 | Cortex-Mx.intercept=<some float value> |
| 558 | Cortex-Mx.slope=<some float value> |
| 559 | """ |
| 560 | section = "CpuPerformance." + op.type |
| 561 | if self.vela_config is not None and section in self.vela_config: |
| 562 | op_config = self.vela_config[section] |
| 563 | try: |
| 564 | intercept = float(op_config.get(self.cpu_config + ".intercept", op_config["default.intercept"])) |
| 565 | slope = float(op_config.get(self.cpu_config + ".slope", op_config["default.slope"])) |
| 566 | n_elements = op.inputs[0].elements() |
| 567 | cycles = intercept + n_elements * slope |
| 568 | return cycles |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 569 | except Exception: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 570 | print("Error: Reading CPU cycle estimate in vela configuration file, section {}".format(section)) |
| 571 | raise |
| 572 | |
| 573 | print("Warning: No configured CPU performance estimate for", op.type) |
| 574 | return 0 |
| 575 | |
Patrik Gustavsson | 5f47c05 | 2020-06-25 12:56:04 +0200 | [diff] [blame] | 576 | def __read_sys_config(self, is_yoda_system): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 577 | """ |
| 578 | Gets the system configuration with the given name from the vela configuration file |
| 579 | Example configuration snippet: |
| 580 | [SysConfig.MyConfigName] |
| 581 | npu_freq=<some float value> |
| 582 | cpu=Cortex-Mx |
| 583 | ... |
| 584 | """ |
| 585 | # Get system configuration from the vela configuration file |
| 586 | if self.vela_config is None: |
| 587 | print("Warning: Using default values for system configuration") |
| 588 | else: |
| 589 | section_key = "SysConfig." + self.system_config |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 590 | if section_key not in self.vela_config: |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 591 | raise OptionError("--system-config", self.system_config, "Unknown system configuration") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 592 | |
| 593 | try: |
| 594 | self.npu_clock = float(self.__sys_config("npu_freq", "500e6")) |
| 595 | self.cpu_config = self.__sys_config("cpu", "Cortex-M7") |
| 596 | |
| 597 | self.memory_clock_scales[MemArea.Sram] = float(self.__sys_config("Sram_clock_scale", "1")) |
| 598 | self.memory_port_widths[MemArea.Sram] = int(self.__sys_config("Sram_port_width", "64")) |
| 599 | |
| 600 | self.memory_clock_scales[MemArea.OnChipFlash] = float(self.__sys_config("OnChipFlash_clock_scale", "1")) |
| 601 | self.memory_port_widths[MemArea.OnChipFlash] = int(self.__sys_config("OnChipFlash_port_width", "64")) |
| 602 | |
| 603 | self.memory_clock_scales[MemArea.OffChipFlash] = float( |
| 604 | self.__sys_config("OffChipFlash_clock_scale", "0.25") |
| 605 | ) |
| 606 | self.memory_port_widths[MemArea.OffChipFlash] = int(self.__sys_config("OffChipFlash_port_width", "32")) |
| 607 | |
| 608 | self.memory_clock_scales[MemArea.Dram] = float(self.__sys_config("Dram_clock_scale", "1")) |
| 609 | self.memory_port_widths[MemArea.Dram] = int(self.__sys_config("Dram_port_width", "32")) |
| 610 | |
| 611 | self.fast_storage_mem_area = MemArea[self.__sys_config("fast_storage_mem_area", "Sram")] |
| 612 | self.feature_map_storage_mem_area = MemArea[self.__sys_config("feature_map_storage_mem_area", "Sram")] |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 613 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 614 | self.permanent_storage_mem_area = MemArea[self.__sys_config("permanent_storage_mem_area", "OffChipFlash")] |
Patrik Gustavsson | 5f47c05 | 2020-06-25 12:56:04 +0200 | [diff] [blame] | 615 | if is_yoda_system: |
| 616 | if self.permanent_storage_mem_area is not MemArea.Dram: |
| 617 | raise Exception( |
| 618 | "Invalid permanent_storage_mem_area = " |
| 619 | + str(self.permanent_storage_mem_area) |
| 620 | + " (must be 'DRAM' for Yoda)." |
| 621 | ) |
| 622 | else: |
| 623 | if self.permanent_storage_mem_area not in set((MemArea.OnChipFlash, MemArea.OffChipFlash)): |
| 624 | raise Exception( |
| 625 | "Invalid permanent_storage_mem_area = " |
| 626 | + str(self.permanent_storage_mem_area) |
| 627 | + " (must be 'OnChipFlash' or 'OffChipFlash' for ethosu-55)." |
| 628 | " To store the weights and other constant data in SRAM on ethosu-55 select 'OnChipFlash'" |
| 629 | ) |
| 630 | |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 631 | self.sram_size = 1024 * int(self.__sys_config("sram_size_kb", "204800")) |
| 632 | |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 633 | except Exception: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 634 | print("Error: Reading System Configuration in vela configuration file, section {}".format(section_key)) |
| 635 | raise |
| 636 | |
| 637 | def __sys_config(self, key, default_value): |
| 638 | """ |
| 639 | Gets the system configuration value with the given key from the vela config file. |
| 640 | """ |
| 641 | if self.vela_config is None: |
| 642 | return default_value |
| 643 | section = "SysConfig." + self.system_config |
| 644 | result = self.vela_config[section].get(key, None) |
| 645 | if result is None: |
| 646 | raise Exception("Error: System Configuration Missing key {} in section [{}] ".format(key, section)) |
| 647 | return result |