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: |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 17 | # Holds a container for Ethos-U and 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 | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 24 | from .api import NpuAccelerator |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 25 | from .errors import CliOptionError |
| 26 | from .errors import ConfigOptionError |
Dwight Lidman | a9390f7 | 2020-05-13 12:00:08 +0200 | [diff] [blame] | 27 | from .ethos_u55_regs.ethos_u55_regs import resampling_mode |
Louis Verhaard | 69b3176 | 2020-11-17 09:45:20 +0100 | [diff] [blame] | 28 | from .numeric_util import full_shape |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 29 | from .numeric_util import round_up |
| 30 | from .numeric_util import round_up_divide |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 31 | from .operation import Kernel |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 32 | from .operation import NpuBlockType |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 33 | from .operation import PointXYZ |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 34 | from .supported_operators import SupportedOperators |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 35 | from .tensor import MemArea |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 36 | from .tensor import MemType |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 37 | from .tensor import TensorFormat |
| 38 | from .tensor import TensorPurpose |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 39 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 40 | |
| 41 | class Block: |
| 42 | def __init__(self, w, h, d): |
| 43 | self.width = w |
| 44 | self.height = h |
| 45 | self.depth = d |
| 46 | |
| 47 | def __eq__(self, other): |
| 48 | if self.width == other.width and self.height == other.height and self.depth == other.depth: |
| 49 | return True |
| 50 | else: |
| 51 | return False |
| 52 | |
| 53 | def __repr__(self): |
| 54 | return "<Block: {0},{1},{2}>".format(self.width, self.height, self.depth) |
| 55 | |
| 56 | @classmethod |
| 57 | def from_string(cls, s): |
| 58 | w, h, c = (int(v) for v in s.split("x")) |
| 59 | return cls(w, h, c) |
| 60 | |
Louis Verhaard | 69b3176 | 2020-11-17 09:45:20 +0100 | [diff] [blame] | 61 | @classmethod |
| 62 | def from_shape(cls, shape) -> "Block": |
| 63 | """Converts the shape to a Block""" |
| 64 | shp = full_shape(3, shape, 1) |
| 65 | # Note: index from end, as len(shp) may be > 3 |
| 66 | return Block(shp[-2], shp[-3], shp[-1]) |
| 67 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 68 | |
| 69 | class Rect: |
| 70 | def __init__(self, x, y, z, x2, y2, z2): |
| 71 | self.x = x |
| 72 | self.y = y |
| 73 | self.z = z |
| 74 | self.x2 = x2 |
| 75 | self.y2 = y2 |
| 76 | self.z2 = z2 |
| 77 | |
| 78 | def start(self): |
| 79 | return PointXYZ(self.x, self.y, self.z) |
| 80 | |
| 81 | def end(self): |
| 82 | return PointXYZ(self.x2, self.y2, self.z2) |
| 83 | |
| 84 | def size(self): |
| 85 | return Block(self.x2 - self.x + 1, self.y2 - self.y + 1, self.z2 - self.z + 1) |
| 86 | |
| 87 | def __repr__(self): |
| 88 | return "<Rect: ({0},{1},{2}) ({3},{4},{5})>".format(self.x, self.y, self.z, self.x2, self.y2, self.z2) |
| 89 | |
| 90 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 91 | class SHRAMElements: |
| 92 | IFM8 = 0 |
| 93 | IFM16 = 1 |
| 94 | IFM8_Elementwise = 2 |
| 95 | IFM16_Elementwise = 3 |
Fredrik Svedberg | 597fd3f | 2020-08-13 10:02:53 +0200 | [diff] [blame] | 96 | IFM32 = 4 |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 97 | Acc16 = 5 |
| 98 | Acc32 = 6 |
| 99 | Acc40 = 7 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 100 | Last = Acc40 |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 101 | BitSizes = np.array([8, 16, 8, 16, 32, 16, 32, 40], np.int32) |
Louis Verhaard | f98c674 | 2020-05-12 14:22:38 +0200 | [diff] [blame] | 102 | ByteSizes = BitSizes // 8 |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 103 | PostAlign = np.array([8, 8, 8, 8, 8, 1, 1, 1], np.int32) |
| 104 | PreAlign = np.array([1, 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 | |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 114 | # Area indices must match Ethos-U SHRAM layout spec |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 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" |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 128 | Ethos_U65_256 = "ethos-u65-256" |
| 129 | Ethos_U65_512 = "ethos-u65-512" |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 130 | |
| 131 | @classmethod |
| 132 | def member_list(cls): |
| 133 | return [e.value for e in cls] |
| 134 | |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 135 | @classmethod |
| 136 | def from_npu_accelerator(cls, npu_accelerator: NpuAccelerator) -> "Accelerator": |
| 137 | """Converts the given public API object to Accelerator (used internally)""" |
| 138 | accelerator_map = { |
| 139 | NpuAccelerator.Ethos_U55_32: cls.Ethos_U55_32, |
| 140 | NpuAccelerator.Ethos_U55_64: cls.Ethos_U55_64, |
| 141 | NpuAccelerator.Ethos_U55_128: cls.Ethos_U55_128, |
| 142 | NpuAccelerator.Ethos_U55_256: cls.Ethos_U55_256, |
| 143 | NpuAccelerator.Ethos_U65_256: cls.Ethos_U65_256, |
| 144 | NpuAccelerator.Ethos_U65_512: cls.Ethos_U65_512, |
| 145 | } |
| 146 | assert npu_accelerator in accelerator_map, f"Unsupported accelerator {npu_accelerator}" |
| 147 | return accelerator_map[npu_accelerator] |
| 148 | |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 149 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 150 | @enum.unique |
| 151 | class MemPort(enum.Enum): |
| 152 | Axi0 = enum.auto() |
| 153 | Axi1 = enum.auto() |
| 154 | |
| 155 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 156 | class ArchitectureFeatures: |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 157 | """This class is a container for various parameters of the Ethos-U core |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 158 | and system configuration that can be tuned, either by command line |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 159 | parameters or by the Ethos-U architects. The class is often passed |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 160 | around to passes that need to do architecture-dependent actions. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 161 | |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 162 | Note the difference between ArchitectureFeatures and CompilerOptions |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 163 | - ArchitectureFeatures is for changing the Ethos-U and system architecture |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 164 | - CompilerOptions is for changing the behaviour of the compiler |
| 165 | """ |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 166 | |
| 167 | ArchitectureConfig = namedtuple( |
| 168 | "ArchitectureConfig", "macs cores ofm_ublock ifm_ublock shram_banks shram_granules elem_units" |
| 169 | ) |
| 170 | accelerator_configs = { |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 171 | Accelerator.Ethos_U65_512: ArchitectureConfig( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 172 | 256, 2, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 16, 8, 16, 20], 8 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 173 | ), |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 174 | Accelerator.Ethos_U65_256: ArchitectureConfig( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 175 | 256, 1, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 16, 8, 16, 20], 8 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 176 | ), |
| 177 | Accelerator.Ethos_U55_256: ArchitectureConfig( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 178 | 256, 1, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 16, 8, 16, 20], 8 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 179 | ), |
| 180 | Accelerator.Ethos_U55_128: ArchitectureConfig( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 181 | 128, 1, Block(2, 1, 8), Block(2, 2, 8), 24, [4, 4, 4, 4, 8, 4, 8, 12], 4 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 182 | ), |
| 183 | Accelerator.Ethos_U55_64: ArchitectureConfig( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 184 | 64, 1, Block(1, 1, 8), Block(1, 1, 8), 16, [2, 2, 2, 2, 4, 4, 4, 8], 2 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 185 | ), |
| 186 | Accelerator.Ethos_U55_32: ArchitectureConfig( |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 187 | 32, 1, Block(1, 1, 4), Block(1, 1, 8), 16, [2, 2, 2, 2, 4, 4, 4, 4], 1 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 188 | ), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | OFMSplitDepth = 16 |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 192 | SubKernelMax = Block(8, 8, 65536) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 193 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 194 | DEFAULT_CONFIG = "internal-default" |
| 195 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 196 | def __init__( |
| 197 | self, |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 198 | vela_config_files, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 199 | accelerator_config, |
| 200 | system_config, |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 201 | memory_mode, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 202 | override_block_config, |
| 203 | block_config_limit, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 204 | max_blockdep, |
Patrik Gustavsson | 90831bc | 2020-08-24 16:26:11 +0200 | [diff] [blame] | 205 | weight_estimation_scaling, |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 206 | verbose_config, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 207 | ): |
| 208 | accelerator_config = accelerator_config.lower() |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 209 | if accelerator_config not in Accelerator.member_list(): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 210 | raise CliOptionError("--accelerator-config", self.accelerator_config, "Unknown accelerator configuration") |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 211 | self.accelerator_config = Accelerator(accelerator_config) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 212 | accel_config = ArchitectureFeatures.accelerator_configs[self.accelerator_config] |
| 213 | self.config = accel_config |
| 214 | |
| 215 | self.system_config = system_config |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 216 | self.memory_mode = memory_mode |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 217 | self.is_ethos_u65_system = self.accelerator_config in (Accelerator.Ethos_U65_256, Accelerator.Ethos_U65_512) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 218 | |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 219 | self.max_outstanding_dma = 2 if self.is_ethos_u65_system else 1 |
Tim Hall | 289a41d | 2020-08-04 21:40:14 +0100 | [diff] [blame] | 220 | self.max_outstanding_kernels = 3 |
| 221 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 222 | self.ncores = accel_config.cores |
| 223 | self.ofm_ublock = accel_config.ofm_ublock |
| 224 | self.ifm_ublock = accel_config.ifm_ublock |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 225 | self.ofm_block_max = Block(64, 32, 128) |
| 226 | self.override_block_config = override_block_config |
| 227 | self.block_config_limit = block_config_limit |
| 228 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 229 | self.max_blockdep = max_blockdep |
Patrik Gustavsson | 90831bc | 2020-08-24 16:26:11 +0200 | [diff] [blame] | 230 | self.weight_estimation_scaling = weight_estimation_scaling |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 231 | |
| 232 | dpu_min_height = accel_config.ofm_ublock.height |
| 233 | dpu_min_width = accel_config.ofm_ublock.width |
| 234 | dpu_dot_product_width = 8 |
| 235 | dpu_min_ofm_channels = accel_config.ofm_ublock.depth |
| 236 | |
| 237 | self.num_elem_wise_units = accel_config.elem_units |
| 238 | self.num_macs_per_cycle = dpu_min_height * dpu_min_width * dpu_dot_product_width * dpu_min_ofm_channels |
| 239 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 240 | # Get system configuration and memory mode |
| 241 | self._get_vela_config(vela_config_files, verbose_config) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 242 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 243 | self.axi_port_width = 128 if self.is_ethos_u65_system else 64 |
| 244 | self.memory_bandwidths_per_cycle = self.axi_port_width * self.memory_clock_scales / 8 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 245 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 246 | self.memory_bandwidths_per_second = self.memory_bandwidths_per_cycle * self.core_clock |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 247 | |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 248 | # Get output/activation performance numbers |
| 249 | self._generate_output_perf_tables(self.accelerator_config) |
| 250 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 251 | # sizes as N x H x W x C. we need to round up to these when allocating storage |
| 252 | self.storage_rounding_quantums = { |
| 253 | TensorFormat.Unknown: (1, 1, 1, 1), |
| 254 | TensorFormat.WeightsCompressed: (1, 1, 1, 1), |
| 255 | TensorFormat.NHWC: (1, 1, 1, 1), |
| 256 | TensorFormat.NHCWB16: (1, 1, 1, 16), |
| 257 | } |
| 258 | |
| 259 | # brick sizes as N x H x W x C. We have to fetch whole bricks at a time |
| 260 | self.brick_sizes = { |
| 261 | TensorFormat.Unknown: (1, 1, 1, 1), |
| 262 | TensorFormat.WeightsCompressed: (1, 1, 1, 1), |
| 263 | TensorFormat.NHWC: (1, 1, 1, 1), |
| 264 | TensorFormat.NHCWB16: (1, 1, 1, 16), |
| 265 | } |
| 266 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 267 | self.default_weight_format = TensorFormat.WeightsCompressed |
| 268 | self.default_feature_map_format = TensorFormat.NHWC |
| 269 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 270 | self.tensor_storage_mem_area = { |
| 271 | # permanent mem_area |
Tim Hall | 465582c | 2020-05-26 09:33:14 +0100 | [diff] [blame] | 272 | TensorPurpose.Unknown: MemArea.Unknown, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 273 | TensorPurpose.Weights: self.permanent_storage_mem_area, |
| 274 | TensorPurpose.FeatureMap: self.feature_map_storage_mem_area, |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 275 | TensorPurpose.LUT: self.permanent_storage_mem_area, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 278 | self.tensor_storage_mem_type = { |
Dwight Lidman | 1a9d20e | 2020-08-11 12:10:36 +0200 | [diff] [blame] | 279 | TensorPurpose.Unknown: MemType.Unknown, |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 280 | TensorPurpose.Weights: MemType.Permanent_NPU, |
| 281 | TensorPurpose.FeatureMap: MemType.Scratch, |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 282 | TensorPurpose.LUT: MemType.Scratch, |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 283 | } |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 284 | |
| 285 | self.min_block_sizes = { |
| 286 | NpuBlockType.Default: (dpu_min_height, dpu_min_width), |
| 287 | NpuBlockType.VectorProduct: (1, 1), |
| 288 | NpuBlockType.ConvolutionMxN: (dpu_min_height, dpu_min_width), |
| 289 | NpuBlockType.Pooling: (dpu_min_height, dpu_min_width), |
| 290 | NpuBlockType.ConvolutionDepthWise: (dpu_min_height, dpu_min_width), |
| 291 | NpuBlockType.ElementWise: (1, 1), |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 292 | NpuBlockType.ReduceSum: (dpu_min_height, dpu_min_width), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | self.sub_kernel_limits = { |
| 296 | NpuBlockType.Default: (8, 8), |
| 297 | NpuBlockType.VectorProduct: (1, 1), |
| 298 | NpuBlockType.ConvolutionMxN: (8, 8), |
| 299 | NpuBlockType.Pooling: (8, 8), |
| 300 | NpuBlockType.ConvolutionDepthWise: (8, 8), |
| 301 | NpuBlockType.ElementWise: (1, 1), |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 302 | NpuBlockType.ReduceSum: (8, 8), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | # weights for scheduler search |
| 306 | from .npu_performance import make_bandwidth_array |
| 307 | |
| 308 | self.bandwidth_weights = make_bandwidth_array() |
| 309 | self.bandwidth_weights[MemArea.Sram] = 1.0 |
| 310 | self.bandwidth_weights[MemArea.Dram] = 10.0 |
| 311 | self.bandwidth_weights[MemArea.OnChipFlash] = 2.0 |
| 312 | self.bandwidth_weights[MemArea.OffChipFlash] = 20.0 |
| 313 | self.cycles_weight = 40 |
| 314 | self.max_sram_used_weight = 1000 |
| 315 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 316 | if self.is_spilling_enabled(): |
Patrik Gustavsson | 3ab9452 | 2020-06-29 17:36:55 +0200 | [diff] [blame] | 317 | self.max_sram_used_weight = 0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 318 | |
| 319 | # Shared Buffer Block allocations |
| 320 | self.shram_bank_size = 1024 # bytes |
| 321 | self.shram_size_bytes = accel_config.shram_banks * self.shram_bank_size |
| 322 | self.shram_reserved_output_banks = 2 |
| 323 | self.shram_reserved_weight_banks = 0 |
| 324 | self.shram_reserved_unused_banks = 2 if accel_config.shram_banks > 16 else 0 |
| 325 | self.shram_total_banks = accel_config.shram_banks - self.shram_reserved_unused_banks |
| 326 | self.shram_bank_granules = np.array(accel_config.shram_granules, np.int32) |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 327 | self.shram_lut_size = 2048 |
| 328 | # SHRAM base address of the activation lookup table |
| 329 | self.shram_lut_address = self.shram_bank_size * self.available_shram_banks(True) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 330 | |
| 331 | # Build a map of acceptable IFM/OFM block configurations up to the maximum |
| 332 | # IFM/OFM block size. |
| 333 | ifm_block_max = self.get_ifm_block_size(32, self.ofm_block_max, Kernel(8, 8)) |
| 334 | self.block_config_map = dict() |
| 335 | self.generate_block_config_map(Block(ifm_block_max.width, ifm_block_max.height, 128)) |
| 336 | |
| 337 | # Setup supported operators and restriction checkers class |
Fredrik Svedberg | 880e735 | 2020-08-25 11:31:47 +0200 | [diff] [blame] | 338 | self.supported_operators = SupportedOperators() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 339 | |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 340 | # Returns available number of SHRAM banks depending on activation lookup table |
| 341 | # being used or not |
| 342 | def available_shram_banks(self, uses_activation_lut): |
| 343 | banks = self.shram_total_banks |
| 344 | if uses_activation_lut and self.shram_reserved_unused_banks == 0: |
| 345 | banks -= 2 |
| 346 | return banks |
| 347 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 348 | # Calculate block configuration for ALL known IFM operations and |
| 349 | # accumulator sizes. Consumers will need to select their preferred |
| 350 | # operation and bit-width at read-time. |
| 351 | def generate_block_config(self, width, height, depth): |
Louis Verhaard | f98c674 | 2020-05-12 14:22:38 +0200 | [diff] [blame] | 352 | # Number of bytes required for any SHRAM element for a FM of given dimensions. |
| 353 | # For IFM: size = H*W*Align(D*BYTE_WIDTH, 8) |
| 354 | # For ACC: size = H*W*Align(D,8)*BYTE_WIDTH |
| 355 | d1 = round_up(depth, SHRAMElements.PreAlign) |
| 356 | d2 = round_up(d1 * SHRAMElements.ByteSizes, SHRAMElements.PostAlign) |
| 357 | size_bytes = (height * width) * d2 |
| 358 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 359 | # Convert byte size (rounded) to size in banks |
| 360 | size_banks = round_up_divide(size_bytes, self.shram_bank_size) |
| 361 | size_banks *= 2 # Double buffer the IFM/Acc (need twice as many banks) |
| 362 | # Round bank requirement to bank granularity |
| 363 | required_banks = round_up(size_banks, self.shram_bank_granules) |
| 364 | return SHRAMBlockConfig(size_bytes, required_banks) |
| 365 | |
| 366 | @staticmethod |
| 367 | def make_block_config_key(width, height, depth): |
| 368 | return (int(height), int(width), int(depth)) |
| 369 | |
| 370 | def get_block_config(self, width, height, depth): |
| 371 | assert depth <= self.ofm_block_max.depth |
| 372 | key = ArchitectureFeatures.make_block_config_key(width, height, depth) |
| 373 | config = self.block_config_map.get(key, None) |
| 374 | return config |
| 375 | |
| 376 | # Generate a key:value map of possible block configurations, where the |
| 377 | # key is compounded from the block dimensions: 0x00HHWWCC |
| 378 | def generate_block_config_map(self, block: Block): |
| 379 | for h in range(1, block.height + 1): |
| 380 | for w in range(1, block.width + 1): |
| 381 | # All possible IFM/OFM depth values |
| 382 | for c in [4, 8, 12, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128]: |
| 383 | key = ArchitectureFeatures.make_block_config_key(w, h, c) |
| 384 | self.block_config_map[key] = self.generate_block_config(w, h, c) |
| 385 | |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 386 | def _generate_output_perf_tables(self, accel_config): |
| 387 | if accel_config == Accelerator.Ethos_U55_32: |
| 388 | self.output_cycles_per_elem = (2.0, 3.0, 3.0, 3.0, 4.0, 6.0, 1.0, 2.0) |
| 389 | self.activation_cycles_per_elem = (1.0, 1.0, 0.0) |
| 390 | elif accel_config == Accelerator.Ethos_U55_64: |
| 391 | self.output_cycles_per_elem = (1.0, 1.5, 1.5, 1.5, 2.0, 3.0, 0.5, 1.0) |
| 392 | self.activation_cycles_per_elem = (1.0, 1.0, 0.0) |
| 393 | elif accel_config == Accelerator.Ethos_U55_128: |
| 394 | self.output_cycles_per_elem = (0.75, 1.25, 0.75, 0.75, 1.0, 1.5, 0.25, 0.5) |
| 395 | self.activation_cycles_per_elem = (1.0, 0.5, 0.0) |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 396 | elif accel_config in (Accelerator.Ethos_U55_256, Accelerator.Ethos_U65_256): |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 397 | self.output_cycles_per_elem = (0.625, 1.125, 0.5, 0.375, 0.5, 0.75, 0.125, 0.25) |
| 398 | self.activation_cycles_per_elem = (1.0, 0.25, 0.0) |
| 399 | else: |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 400 | assert accel_config == Accelerator.Ethos_U65_512 |
Diqing Zhong | e8887a3 | 2020-09-24 09:53:48 +0200 | [diff] [blame] | 401 | self.output_cycles_per_elem = (0.3125, 0.5625, 0.25, 0.1875, 0.25, 0.375, 0.0625, 0.125) |
| 402 | self.activation_cycles_per_elem = (0.5, 0.125, 0.0) |
| 403 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 404 | def calc_ifm_block_depth(self, ifm_depth, ifm_bits): |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 405 | assert ifm_bits in (8, 16, 32) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 406 | assert ifm_depth > 0 |
| 407 | ifm_depth = round_up(ifm_depth, self.ifm_ublock.depth) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 408 | max_block_depth = 8 * 32 // ifm_bits |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 409 | return min(max_block_depth, ifm_depth) |
| 410 | |
| 411 | # 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] | 412 | def get_ifm_block_size( |
| 413 | self, |
| 414 | ifm_block_depth, |
| 415 | ofm_block: Block, |
| 416 | kernel: Kernel, |
| 417 | subkernel: Block = Block(8, 8, 65536), |
| 418 | ifm_resampling_mode=resampling_mode.NONE, |
| 419 | ): |
Dwight Lidman | a9390f7 | 2020-05-13 12:00:08 +0200 | [diff] [blame] | 420 | upscaling = 1 if ifm_resampling_mode == resampling_mode.NONE else 2 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 421 | # Height |
| 422 | ifm_odd_2x_height_enable = 0 |
| 423 | dilated_kernel_height = ((kernel.height - 1) * kernel.dilation.y) + 1 |
| 424 | ifm_block_height = ( |
| 425 | (ofm_block.height - 1) * kernel.stride.y |
| 426 | + min(subkernel.height, dilated_kernel_height) |
| 427 | + ifm_odd_2x_height_enable |
| 428 | ) // upscaling |
| 429 | |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 430 | ifm_block_height = round_up(ifm_block_height, self.ofm_ublock.height) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 431 | |
| 432 | # Width |
| 433 | ifm_odd_2x_width_enable = 0 |
| 434 | dilated_kernel_width = ((kernel.width - 1) * kernel.dilation.x) + 1 |
| 435 | ifm_block_width = ( |
| 436 | (ofm_block.width - 1) * kernel.stride.x |
| 437 | + min(subkernel.width, dilated_kernel_width) |
| 438 | + ifm_odd_2x_width_enable |
| 439 | ) // upscaling |
| 440 | |
Dwight Lidman | 0538a77 | 2020-05-06 14:09:17 +0200 | [diff] [blame] | 441 | ifm_block_width = round_up(ifm_block_width, self.ofm_ublock.width) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 442 | |
| 443 | return Block(ifm_block_width, ifm_block_height, ifm_block_depth) |
| 444 | |
| 445 | @staticmethod |
| 446 | def intersects(start_a, end_a, start_b, end_b): |
| 447 | start_x = max(start_a[0], start_b[0]) |
| 448 | end_x = min(end_a[0], end_b[0]) |
| 449 | start_y = max(start_a[1], start_b[1]) |
| 450 | end_y = min(end_a[1], end_b[1]) |
| 451 | start_z = max(start_a[2], start_b[2]) |
| 452 | end_z = min(end_a[2], end_b[2]) |
| 453 | return ((end_x - start_x) > 0) and ((end_y - start_y) > 0) and ((end_z - start_z) > 0) |
| 454 | |
| 455 | # Block job dependency: |
| 456 | # Does the VOLUME of IFMs for block job B(0) overlap with VOLUME of OFMs block jobs A(8,9,10) |
| 457 | # |
| 458 | # A | B |
| 459 | # ----------------------+------------------ |
| 460 | # .... 3,4,5,6,7,8,9,10 | 0,1,2,3,4,5,6,8 10 < JOB NUMBER |
| 461 | # |<------->| dependency offset |
| 462 | # |
| 463 | MAX_BLOCKDEP = 3 |
| 464 | |
| 465 | # Get the coordinates of a block offset from either the end (negative) |
| 466 | # or the start (zero or positive) of the given 3d area |
| 467 | def get_offset_block_coords(self, area: Rect, block: Block, offset): |
| 468 | size = area.size() |
| 469 | # Dimensions of the region, in blocks |
| 470 | width_blocks = round_up_divide(size.width, block.width) |
| 471 | height_blocks = round_up_divide(size.height, block.height) |
| 472 | depth_blocks = round_up_divide(size.depth, block.depth) |
| 473 | total_blocks = width_blocks * height_blocks * depth_blocks |
| 474 | if offset < 0: |
| 475 | index = total_blocks + offset |
| 476 | else: |
| 477 | index = offset |
| 478 | |
| 479 | if index >= total_blocks: |
| 480 | return None |
| 481 | |
| 482 | # Coordinates of the indexed block |
| 483 | coord_z = block.depth * (index % depth_blocks) |
| 484 | coord_y = block.height * (index // (depth_blocks * width_blocks)) |
| 485 | coord_x = block.width * ((index // depth_blocks) % width_blocks) |
| 486 | |
| 487 | return (coord_x + area.x, coord_y + area.y, coord_z + area.z) |
| 488 | |
| 489 | def get_first_job_input_volume( |
| 490 | self, ifm: Rect, ofm: Rect, ifm_block_depth, ofm_block: Block, kernel: Kernel, padLT, block_offset |
| 491 | ): |
| 492 | # Get ifm block size (jobs are invisibly decomposed into subkernels) |
| 493 | ifm_block = self.get_ifm_block_size(ifm_block_depth, ofm_block, kernel, self.ofm_block_max) |
| 494 | ifm_depth_blocks = round_up_divide(ifm.size().depth, ifm_block_depth) |
| 495 | |
| 496 | # Which OFM block are we calculating |
| 497 | ofm_coord = self.get_offset_block_coords(ofm, ofm_block, block_offset // ifm_depth_blocks) |
| 498 | if ofm_coord is None: |
| 499 | return None |
| 500 | |
| 501 | # Coordinate of the source IFM block |
| 502 | ifm_coord_x = max(0, ofm_coord[0] * kernel.stride.x - padLT[0]) |
| 503 | ifm_coord_y = max(0, ofm_coord[1] * kernel.stride.y - padLT[1]) |
| 504 | ifm_coord_z = ifm.z + (block_offset % ifm_depth_blocks) * ifm_block.depth |
| 505 | |
| 506 | # IFM block that will be sampled for the FIRST+block_offset job in the next operator's OFM |
| 507 | start_coord = (ifm_coord_x, ifm_coord_y, ifm_coord_z) |
| 508 | end_coord = ( |
| 509 | start_coord[0] + ifm_block.width, |
| 510 | start_coord[1] + ifm_block.height, |
| 511 | start_coord[2] + ifm_block.depth, |
| 512 | ) |
| 513 | |
| 514 | return (start_coord, end_coord, 1) # start, end, total jobs |
| 515 | |
| 516 | def get_prev_job_output_volume( |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 517 | self, ifm: Rect, ofm: Rect, ifm_block_depth, ofm_block: Block, kernel: Kernel, block_offset |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 518 | ): |
| 519 | assert block_offset >= 0 |
| 520 | |
| 521 | # Get OFM block's volume coordinates |
| 522 | start_coord = self.get_offset_block_coords(ofm, ofm_block, -1 - block_offset) |
| 523 | if start_coord is None: |
| 524 | return None |
| 525 | end_coord = ( |
| 526 | start_coord[0] + ofm_block.width, |
| 527 | start_coord[1] + ofm_block.height, |
| 528 | start_coord[2] + ofm_block.depth, |
| 529 | ) |
| 530 | |
| 531 | # 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] | 532 | ifm_depth_blocks = round_up_divide(ifm.size().depth, ifm_block_depth) |
| 533 | ifm_depth_blocks = 1 # Overwrite with 1 to force OFM block dependency, not IFM |
| 534 | |
| 535 | return (start_coord, end_coord, ifm_depth_blocks) # start, end, total jobs for this OFM block |
| 536 | |
| 537 | def calc_block_dep( |
| 538 | self, |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 539 | prev_ifm: Rect, |
| 540 | prev_ofm: Rect, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 541 | prev_ifm_block_depth, |
| 542 | prev_ofm_block: Block, |
| 543 | prev_kernel: Kernel, |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 544 | ifm: Rect, |
| 545 | ofm: Rect, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 546 | ifm_block_depth, |
| 547 | ofm_block: Block, |
| 548 | kernel: Kernel, |
| 549 | padLT, |
| 550 | ): |
| 551 | |
| 552 | blockdep = ArchitectureFeatures.MAX_BLOCKDEP |
| 553 | |
| 554 | # Iterate over the next BLOCKDEP inputs, checking to see if a sliding window |
| 555 | # of IFM area overlaps with any previous OFM block generation. |
| 556 | elapsed_jobs = 0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 557 | for forward_offset in range(ArchitectureFeatures.MAX_BLOCKDEP): |
| 558 | # This is the IFM block we want to sample from |
| 559 | in_area = self.get_first_job_input_volume( |
| 560 | ifm, ofm, ifm_block_depth, ofm_block, kernel, padLT, forward_offset |
| 561 | ) |
| 562 | if in_area is None: |
| 563 | break |
| 564 | |
| 565 | # Try several previous-OFM blocks in the past (they still might comprise multiple IFM jobs) |
| 566 | outstanding_jobs = 0 |
| 567 | for block_offset in range(ArchitectureFeatures.MAX_BLOCKDEP): |
| 568 | # This is the OFM block being generated by the previous op |
| 569 | out_area = self.get_prev_job_output_volume( |
| 570 | prev_ifm, prev_ofm, prev_ifm_block_depth, prev_ofm_block, prev_kernel, block_offset |
| 571 | ) |
| 572 | if out_area is None: |
| 573 | break |
| 574 | |
| 575 | # Block dependency is the max number of allowed outstanding jobs |
| 576 | # in the pipeline. Selected by determining how many jobs occur |
| 577 | # in between two operators' overlapping OFM->IFM block volumes |
| 578 | if ArchitectureFeatures.intersects(in_area[0], in_area[1], out_area[0], out_area[1]): |
| 579 | break |
| 580 | # Early exit if no intersections and we've seen enough jobs in the pipeline |
| 581 | elif outstanding_jobs > ArchitectureFeatures.MAX_BLOCKDEP: |
| 582 | break |
| 583 | |
| 584 | # This OFM had this many jobs (accumulate over multiple OFM blocks) |
| 585 | outstanding_jobs += out_area[2] |
| 586 | |
| 587 | blockdep = min(blockdep, elapsed_jobs + outstanding_jobs) |
| 588 | elapsed_jobs += in_area[2] |
| 589 | # Early exit if no intersections and we've seen enough jobs in the pipeline |
| 590 | if elapsed_jobs > ArchitectureFeatures.MAX_BLOCKDEP: |
| 591 | break |
| 592 | |
| 593 | return blockdep |
| 594 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 595 | def is_spilling_enabled(self): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 596 | """ |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 597 | Spilling is a feature that allows the Ethos-U to use a dedicated SRAM as a cache for various types of data |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 598 | """ |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 599 | return ( |
| 600 | self._mem_port_mapping(self.cache_mem_area) == MemArea.Sram and self.cache_mem_area != self.arena_mem_area |
| 601 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 602 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 603 | def _mem_port_mapping(self, mem_port): |
| 604 | mem_port_mapping = {MemPort.Axi0: self.axi0_port, MemPort.Axi1: self.axi1_port} |
| 605 | return mem_port_mapping[mem_port] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 606 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 607 | def _set_default_sys_config(self): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 608 | # ArchitectureFeatures.DEFAULT_CONFIG values |
| 609 | if self.is_ethos_u65_system: |
| 610 | # Default Ethos-U65 system configuration |
| 611 | # Ethos-U65 Client-Server: SRAM (16 GB/s) and DRAM (12 GB/s) |
| 612 | self.core_clock = 1e9 |
| 613 | self.axi0_port = MemArea.Sram |
| 614 | self.axi1_port = MemArea.Dram |
| 615 | self.memory_clock_scales[MemArea.Sram] = 1.0 |
| 616 | self.memory_clock_scales[MemArea.Dram] = 0.75 # 3 / 4 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 617 | else: |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 618 | # Default Ethos-U55 system configuration |
| 619 | # Ethos-U55 High-End Embedded: SRAM (4 GB/s) and Flash (0.5 GB/s) |
| 620 | self.core_clock = 500e6 |
| 621 | self.axi0_port = MemArea.Sram |
| 622 | self.axi1_port = MemArea.OffChipFlash |
| 623 | self.memory_clock_scales[MemArea.Sram] = 1.0 |
| 624 | self.memory_clock_scales[MemArea.OffChipFlash] = 0.125 # 1 / 8 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 625 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 626 | def _set_default_mem_mode(self): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 627 | # ArchitectureFeatures.DEFAULT_CONFIG values |
| 628 | if self.is_ethos_u65_system: |
| 629 | # Default Ethos-U65 memory mode |
| 630 | # Dedicated SRAM: SRAM is only used by the Ethos-U |
| 631 | self.const_mem_area = MemPort.Axi1 |
| 632 | self.arena_mem_area = MemPort.Axi1 |
| 633 | self.cache_mem_area = MemPort.Axi0 |
| 634 | self.cache_sram_size = 384 * 1024 |
| 635 | else: |
| 636 | # Default Ethos-U65 memory mode |
| 637 | self.const_mem_area = MemPort.Axi1 |
| 638 | self.arena_mem_area = MemPort.Axi0 |
| 639 | self.cache_mem_area = MemPort.Axi0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 640 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 641 | def _get_vela_config(self, vela_config_files, verbose_config): |
| 642 | """ |
| 643 | Gets the system configuration and memory modes from one or more Vela configuration file(s) or uses some |
| 644 | defaults. |
| 645 | """ |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 646 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 647 | # all properties are optional and are initialised to a value of 1 (or the equivalent) |
| 648 | self.core_clock = 1 |
| 649 | self.axi0_port = MemArea(1) |
| 650 | self.axi1_port = MemArea(1) |
| 651 | self.memory_clock_scales = np.ones(MemArea.Size) |
| 652 | self.const_mem_area = MemPort(1) |
| 653 | self.arena_mem_area = MemPort(1) |
| 654 | self.cache_mem_area = MemPort(1) |
| 655 | self.cache_sram_size = 1 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 656 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 657 | # read configuration file(s) |
| 658 | self.vela_config = None |
| 659 | |
| 660 | if vela_config_files is not None: |
| 661 | self.vela_config = ConfigParser() |
| 662 | self.vela_config.read(vela_config_files) |
| 663 | |
| 664 | # read system configuration |
| 665 | sys_cfg_section = "System_Config." + self.system_config |
| 666 | |
| 667 | if self.vela_config is not None and self.vela_config.has_section(sys_cfg_section): |
| 668 | self.core_clock = float(self._read_config(sys_cfg_section, "core_clock", self.core_clock)) |
| 669 | self.axi0_port = MemArea[self._read_config(sys_cfg_section, "axi0_port", self.axi0_port)] |
| 670 | self.axi1_port = MemArea[self._read_config(sys_cfg_section, "axi1_port", self.axi1_port)] |
| 671 | |
| 672 | for mem_area in (self.axi0_port, self.axi1_port): |
| 673 | self.memory_clock_scales[mem_area] = float( |
| 674 | self._read_config( |
| 675 | sys_cfg_section, mem_area.name + "_clock_scale", self.memory_clock_scales[mem_area] |
| 676 | ) |
| 677 | ) |
| 678 | |
| 679 | elif self.system_config == ArchitectureFeatures.DEFAULT_CONFIG: |
| 680 | self._set_default_sys_config() |
| 681 | |
| 682 | elif vela_config_files is None: |
| 683 | raise CliOptionError("--config", vela_config_files, "CLI Option not specified") |
| 684 | |
| 685 | else: |
| 686 | raise CliOptionError( |
| 687 | "--system-config", |
| 688 | self.system_config, |
| 689 | "Section {} not found in Vela config file".format(sys_cfg_section), |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 690 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 691 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 692 | # read the memory mode |
| 693 | mem_mode_section = "Memory_Mode." + self.memory_mode |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 694 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 695 | if self.vela_config is not None and self.vela_config.has_section(mem_mode_section): |
| 696 | self.const_mem_area = MemPort[ |
| 697 | self._read_config(mem_mode_section, "const_mem_area", self.const_mem_area.name) |
| 698 | ] |
| 699 | self.arena_mem_area = MemPort[ |
| 700 | self._read_config(mem_mode_section, "arena_mem_area", self.arena_mem_area.name) |
| 701 | ] |
| 702 | self.cache_mem_area = MemPort[ |
| 703 | self._read_config(mem_mode_section, "cache_mem_area", self.cache_mem_area.name) |
| 704 | ] |
| 705 | self.cache_sram_size = int(self._read_config(mem_mode_section, "cache_sram_size", self.cache_sram_size)) |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 706 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 707 | elif self.memory_mode == ArchitectureFeatures.DEFAULT_CONFIG: |
| 708 | self._set_default_mem_mode() |
Patrik Gustavsson | 5f47c05 | 2020-06-25 12:56:04 +0200 | [diff] [blame] | 709 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 710 | elif vela_config_files is None: |
| 711 | raise CliOptionError("--config", vela_config_files, "CLI Option not specified") |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 712 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 713 | else: |
| 714 | raise CliOptionError( |
| 715 | "--memory-mode", self.memory_mode, "Section {} not found in Vela config file".format(mem_mode_section), |
| 716 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 717 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 718 | # override sram to onchipflash |
| 719 | if self._mem_port_mapping(self.const_mem_area) == MemArea.Sram: |
| 720 | if self.const_mem_area == self.arena_mem_area == self.cache_mem_area: |
| 721 | print( |
| 722 | "Info: Changing const_mem_area from Sram to OnChipFlash. This will use the same characteristics as" |
| 723 | " Sram." |
| 724 | ) |
| 725 | if self.const_mem_area == MemPort.Axi0: |
| 726 | self.const_mem_area = MemPort.Axi1 |
| 727 | self.axi1_port = MemArea.OnChipFlash |
| 728 | else: |
| 729 | self.const_mem_area = MemPort.Axi0 |
| 730 | self.axi0_port = MemArea.OnChipFlash |
| 731 | self.memory_clock_scales[MemArea.OnChipFlash] = self.memory_clock_scales[MemArea.Sram] |
| 732 | |
| 733 | # check configuration |
| 734 | if self._mem_port_mapping(self.cache_mem_area) != MemArea.Sram: |
| 735 | raise ConfigOptionError("cache_mem_area", self._mem_port_mapping(self.cache_mem_area).name, "Sram") |
| 736 | |
| 737 | if self.is_ethos_u65_system: |
| 738 | if self._mem_port_mapping(self.const_mem_area) not in ( |
| 739 | MemArea.Dram, |
| 740 | MemArea.OnChipFlash, |
| 741 | MemArea.OffChipFlash, |
| 742 | ): |
| 743 | raise ConfigOptionError( |
| 744 | "const_mem_area", |
| 745 | self._mem_port_mapping(self.const_mem_area).name, |
| 746 | "Dram or OnChipFlash or OffChipFlash", |
| 747 | ) |
| 748 | |
| 749 | if self._mem_port_mapping(self.arena_mem_area) not in (MemArea.Sram, MemArea.Dram): |
| 750 | raise ConfigOptionError( |
| 751 | "arena_mem_area", self._mem_port_mapping(self.arena_mem_area).name, "Sram or Dram" |
| 752 | ) |
| 753 | else: |
| 754 | if self._mem_port_mapping(self.const_mem_area) not in (MemArea.OnChipFlash, MemArea.OffChipFlash): |
| 755 | raise ConfigOptionError( |
| 756 | "const_mem_area", self._mem_port_mapping(self.const_mem_area).name, "OnChipFlash or OffChipFlash" |
| 757 | ) |
| 758 | |
| 759 | if self._mem_port_mapping(self.arena_mem_area) != MemArea.Sram: |
| 760 | raise ConfigOptionError("arena_mem_area", self._mem_port_mapping(self.arena_mem_area).name, "Sram") |
| 761 | |
| 762 | # assign existing memory areas |
| 763 | self.permanent_storage_mem_area = self._mem_port_mapping(self.const_mem_area) |
| 764 | self.feature_map_storage_mem_area = self._mem_port_mapping(self.arena_mem_area) |
| 765 | self.fast_storage_mem_area = self._mem_port_mapping(self.cache_mem_area) |
| 766 | |
| 767 | self.sram_size = self.cache_sram_size if self.is_spilling_enabled() else 9999 * 1024 * 1024 |
| 768 | |
| 769 | # display the system configuration and memory mode |
| 770 | if verbose_config: |
| 771 | print(f"System Configuration ({self.system_config}):") |
| 772 | print(f" core_clock = {self.core_clock}") |
| 773 | print(f" axi0_port = {self.axi0_port.name}") |
| 774 | print(f" axi1_port = {self.axi1_port.name}") |
| 775 | for mem in (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash): |
| 776 | print(f" {mem.name}_clock_scales = {self.memory_clock_scales[mem]}") |
| 777 | |
| 778 | print(f"Memory Mode ({self.memory_mode}):") |
| 779 | print(f" const_mem_area = {self.const_mem_area.name}") |
| 780 | print(f" arena_mem_area = {self.arena_mem_area.name}") |
| 781 | print(f" cache_mem_area = {self.cache_mem_area.name}") |
| 782 | print(f" cache_sram_size = {self.cache_sram_size}") |
| 783 | |
| 784 | print("Architecture Settings:") |
| 785 | print(f" permanent_storage_mem_area = {self.permanent_storage_mem_area.name}") |
| 786 | print(f" feature_map_storage_mem_area = {self.feature_map_storage_mem_area.name}") |
| 787 | print(f" fast_storage_mem_area = {self.fast_storage_mem_area.name}") |
| 788 | print(f" sram_size = {self.sram_size}") |
| 789 | |
| 790 | def _read_config(self, section, key, current_value): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 791 | """ |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 792 | Reads a given key from a particular section in the Vela config file. If the section contains the 'inherit' |
| 793 | option then we recurse into the section specified. If inherited sections result in multiple keys for a |
| 794 | particular option then the key from the parent section is used, regardless of the parsing order |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 795 | """ |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 796 | if not self.vela_config.has_section(section): |
| 797 | raise ConfigOptionError( |
| 798 | "section", "{}. The section was not found in the Vela config file(s)".format(section) |
| 799 | ) |
| 800 | |
| 801 | result = str(current_value) |
| 802 | if self.vela_config.has_option(section, "inherit"): |
| 803 | inheritance_section = self.vela_config.get(section, "inherit") |
| 804 | # check for recursion loop |
| 805 | if inheritance_section == section: |
| 806 | raise ConfigOptionError( |
| 807 | "inherit", |
| 808 | "{}. This references its own section and recursion is not allowed".format(inheritance_section), |
| 809 | ) |
| 810 | result = self._read_config(inheritance_section, key, result) |
| 811 | |
| 812 | if self.vela_config.has_option(section, key): |
| 813 | result = self.vela_config.get(section, key) |
| 814 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 815 | return result |
Louis Verhaard | 5207830 | 2020-11-18 13:35:06 +0100 | [diff] [blame] | 816 | |
| 817 | |
| 818 | def create_default_arch(accelerator: Accelerator) -> ArchitectureFeatures: |
| 819 | """Creates architecture features object using default settings""" |
| 820 | return ArchitectureFeatures( |
| 821 | vela_config_files=None, |
| 822 | accelerator_config=accelerator.value, |
| 823 | system_config=ArchitectureFeatures.DEFAULT_CONFIG, |
| 824 | memory_mode=ArchitectureFeatures.DEFAULT_CONFIG, |
| 825 | override_block_config=None, |
| 826 | block_config_limit=None, |
| 827 | max_blockdep=ArchitectureFeatures.MAX_BLOCKDEP, |
| 828 | weight_estimation_scaling=1.0, |
| 829 | verbose_config=False, |
| 830 | ) |