blob: 168d0e6716f26da5cf7dd56c8f7ad5831b7efaa7 [file] [log] [blame]
erik.andersson@arm.com460c6892021-02-24 14:38:09 +01001# Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved.
Tim Hall79d07d22020-04-27 18:20:16 +01002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
Tim Hallc8a73862020-10-27 12:43:14 +000017# Holds a container for Ethos-U and System architecture parameters.
Diego Russoea6111a2020-04-14 18:41:58 +010018import enum
Tim Hall79d07d22020-04-27 18:20:16 +010019from collections import namedtuple
20from configparser import ConfigParser
Diego Russoea6111a2020-04-14 18:41:58 +010021
Tim Hall79d07d22020-04-27 18:20:16 +010022import numpy as np
Diego Russoea6111a2020-04-14 18:41:58 +010023
Louis Verhaardaeae5672020-11-02 18:04:27 +010024from .api import NpuAccelerator
Tim Hall1bd531d2020-11-01 20:59:36 +000025from .errors import CliOptionError
26from .errors import ConfigOptionError
Dwight Lidmana9390f72020-05-13 12:00:08 +020027from .ethos_u55_regs.ethos_u55_regs import resampling_mode
Louis Verhaard69b31762020-11-17 09:45:20 +010028from .numeric_util import full_shape
Diego Russoe8a10452020-04-21 17:39:10 +010029from .numeric_util import round_up
30from .numeric_util import round_up_divide
Tim Hall4ed38bc2020-10-20 18:54:20 +010031from .operation import Kernel
Diego Russoea6111a2020-04-14 18:41:58 +010032from .operation import NpuBlockType
Tim Hall4ed38bc2020-10-20 18:54:20 +010033from .operation import PointXYZ
Diego Russoea6111a2020-04-14 18:41:58 +010034from .supported_operators import SupportedOperators
Diqing Zhongf842b692020-12-11 13:07:37 +010035from .tensor import BandwidthDirection
Diego Russoe8a10452020-04-21 17:39:10 +010036from .tensor import MemArea
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020037from .tensor import MemType
Diego Russoe8a10452020-04-21 17:39:10 +010038from .tensor import TensorFormat
39from .tensor import TensorPurpose
Tim Hall79d07d22020-04-27 18:20:16 +010040
Tim Hall79d07d22020-04-27 18:20:16 +010041
42class Block:
43 def __init__(self, w, h, d):
44 self.width = w
45 self.height = h
46 self.depth = d
47
48 def __eq__(self, other):
49 if self.width == other.width and self.height == other.height and self.depth == other.depth:
50 return True
51 else:
52 return False
53
54 def __repr__(self):
55 return "<Block: {0},{1},{2}>".format(self.width, self.height, self.depth)
56
57 @classmethod
58 def from_string(cls, s):
59 w, h, c = (int(v) for v in s.split("x"))
60 return cls(w, h, c)
61
Louis Verhaard69b31762020-11-17 09:45:20 +010062 @classmethod
63 def from_shape(cls, shape) -> "Block":
64 """Converts the shape to a Block"""
65 shp = full_shape(3, shape, 1)
66 # Note: index from end, as len(shp) may be > 3
67 return Block(shp[-2], shp[-3], shp[-1])
68
Tim Hall79d07d22020-04-27 18:20:16 +010069
70class Rect:
71 def __init__(self, x, y, z, x2, y2, z2):
72 self.x = x
73 self.y = y
74 self.z = z
75 self.x2 = x2
76 self.y2 = y2
77 self.z2 = z2
78
79 def start(self):
80 return PointXYZ(self.x, self.y, self.z)
81
82 def end(self):
83 return PointXYZ(self.x2, self.y2, self.z2)
84
85 def size(self):
86 return Block(self.x2 - self.x + 1, self.y2 - self.y + 1, self.z2 - self.z + 1)
87
88 def __repr__(self):
89 return "<Rect: ({0},{1},{2}) ({3},{4},{5})>".format(self.x, self.y, self.z, self.x2, self.y2, self.z2)
90
91
Tim Hall79d07d22020-04-27 18:20:16 +010092class SHRAMElements:
93 IFM8 = 0
94 IFM16 = 1
95 IFM8_Elementwise = 2
96 IFM16_Elementwise = 3
Fredrik Svedberg597fd3f2020-08-13 10:02:53 +020097 IFM32 = 4
Fredrik Svedberga0c36242020-06-03 15:43:31 +020098 Acc16 = 5
99 Acc32 = 6
100 Acc40 = 7
Tim Hall79d07d22020-04-27 18:20:16 +0100101 Last = Acc40
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200102 BitSizes = np.array([8, 16, 8, 16, 32, 16, 32, 40], np.int32)
Louis Verhaardf98c6742020-05-12 14:22:38 +0200103 ByteSizes = BitSizes // 8
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200104 PostAlign = np.array([8, 8, 8, 8, 8, 1, 1, 1], np.int32)
105 PreAlign = np.array([1, 1, 1, 1, 1, 8, 8, 8], np.int32)
Tim Hall79d07d22020-04-27 18:20:16 +0100106
107
108class SHRAMBlockConfig:
109 def __init__(self, sizes, banks):
110 assert len(banks) == SHRAMElements.Last + 1
111 self.sizes = sizes
112 self.banks = banks
113
114
Tim Hallc8a73862020-10-27 12:43:14 +0000115# Area indices must match Ethos-U SHRAM layout spec
Tim Hall79d07d22020-04-27 18:20:16 +0100116class SharedBufferArea(enum.IntEnum):
117 OFM = 0
118 Weights = 1
119 IFM = 2
120 Accumulators = 3
121 Size = Accumulators + 1
122
123
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100124class Accelerator(enum.Enum):
125 Ethos_U55_32 = "ethos-u55-32"
126 Ethos_U55_64 = "ethos-u55-64"
127 Ethos_U55_128 = "ethos-u55-128"
128 Ethos_U55_256 = "ethos-u55-256"
Tim Hallc8a73862020-10-27 12:43:14 +0000129 Ethos_U65_256 = "ethos-u65-256"
130 Ethos_U65_512 = "ethos-u65-512"
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100131
132 @classmethod
133 def member_list(cls):
134 return [e.value for e in cls]
135
Louis Verhaardaeae5672020-11-02 18:04:27 +0100136 @classmethod
137 def from_npu_accelerator(cls, npu_accelerator: NpuAccelerator) -> "Accelerator":
138 """Converts the given public API object to Accelerator (used internally)"""
139 accelerator_map = {
140 NpuAccelerator.Ethos_U55_32: cls.Ethos_U55_32,
141 NpuAccelerator.Ethos_U55_64: cls.Ethos_U55_64,
142 NpuAccelerator.Ethos_U55_128: cls.Ethos_U55_128,
143 NpuAccelerator.Ethos_U55_256: cls.Ethos_U55_256,
144 NpuAccelerator.Ethos_U65_256: cls.Ethos_U65_256,
145 NpuAccelerator.Ethos_U65_512: cls.Ethos_U65_512,
146 }
147 assert npu_accelerator in accelerator_map, f"Unsupported accelerator {npu_accelerator}"
148 return accelerator_map[npu_accelerator]
149
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100150
Tim Hall1bd531d2020-11-01 20:59:36 +0000151@enum.unique
152class MemPort(enum.Enum):
153 Axi0 = enum.auto()
154 Axi1 = enum.auto()
155
156
Tim Hall79d07d22020-04-27 18:20:16 +0100157class ArchitectureFeatures:
Tim Hallc8a73862020-10-27 12:43:14 +0000158 """This class is a container for various parameters of the Ethos-U core
Diqing Zhonge8887a32020-09-24 09:53:48 +0200159 and system configuration that can be tuned, either by command line
Tim Hallc8a73862020-10-27 12:43:14 +0000160 parameters or by the Ethos-U architects. The class is often passed
Diqing Zhonge8887a32020-09-24 09:53:48 +0200161 around to passes that need to do architecture-dependent actions.
Tim Hall79d07d22020-04-27 18:20:16 +0100162
Diqing Zhonge8887a32020-09-24 09:53:48 +0200163 Note the difference between ArchitectureFeatures and CompilerOptions
Tim Hallc8a73862020-10-27 12:43:14 +0000164 - ArchitectureFeatures is for changing the Ethos-U and system architecture
Diqing Zhonge8887a32020-09-24 09:53:48 +0200165 - CompilerOptions is for changing the behaviour of the compiler
166 """
Tim Hall79d07d22020-04-27 18:20:16 +0100167
168 ArchitectureConfig = namedtuple(
169 "ArchitectureConfig", "macs cores ofm_ublock ifm_ublock shram_banks shram_granules elem_units"
170 )
171 accelerator_configs = {
Tim Hallc8a73862020-10-27 12:43:14 +0000172 Accelerator.Ethos_U65_512: ArchitectureConfig(
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200173 256, 2, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 16, 8, 16, 20], 8
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100174 ),
Tim Hallc8a73862020-10-27 12:43:14 +0000175 Accelerator.Ethos_U65_256: ArchitectureConfig(
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200176 256, 1, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 16, 8, 16, 20], 8
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100177 ),
178 Accelerator.Ethos_U55_256: ArchitectureConfig(
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200179 256, 1, Block(2, 2, 8), Block(2, 2, 8), 48, [8, 8, 8, 8, 16, 8, 16, 20], 8
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100180 ),
181 Accelerator.Ethos_U55_128: ArchitectureConfig(
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200182 128, 1, Block(2, 1, 8), Block(2, 2, 8), 24, [4, 4, 4, 4, 8, 4, 8, 12], 4
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100183 ),
184 Accelerator.Ethos_U55_64: ArchitectureConfig(
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200185 64, 1, Block(1, 1, 8), Block(1, 1, 8), 16, [2, 2, 2, 2, 4, 4, 4, 8], 2
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100186 ),
187 Accelerator.Ethos_U55_32: ArchitectureConfig(
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200188 32, 1, Block(1, 1, 4), Block(1, 1, 8), 16, [2, 2, 2, 2, 4, 4, 4, 4], 1
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100189 ),
Tim Hall79d07d22020-04-27 18:20:16 +0100190 }
191
192 OFMSplitDepth = 16
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100193 SubKernelMax = Block(8, 8, 65536)
Tim Hall79d07d22020-04-27 18:20:16 +0100194
Tim Hall1bd531d2020-11-01 20:59:36 +0000195 DEFAULT_CONFIG = "internal-default"
Louis Verhaard1e170182020-11-26 11:42:04 +0100196 MAX_BLOCKDEP = 3
Tim Hall1bd531d2020-11-01 20:59:36 +0000197
Tim Hall79d07d22020-04-27 18:20:16 +0100198 def __init__(
199 self,
Tim Hall1bd531d2020-11-01 20:59:36 +0000200 vela_config_files,
Tim Hall79d07d22020-04-27 18:20:16 +0100201 accelerator_config,
202 system_config,
Tim Hall1bd531d2020-11-01 20:59:36 +0000203 memory_mode,
Tim Hall79d07d22020-04-27 18:20:16 +0100204 override_block_config,
205 block_config_limit,
Tim Hall79d07d22020-04-27 18:20:16 +0100206 max_blockdep,
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200207 weight_estimation_scaling,
Tim Hall1bd531d2020-11-01 20:59:36 +0000208 verbose_config,
Tim Hall79d07d22020-04-27 18:20:16 +0100209 ):
210 accelerator_config = accelerator_config.lower()
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100211 if accelerator_config not in Accelerator.member_list():
Tim Hall1bd531d2020-11-01 20:59:36 +0000212 raise CliOptionError("--accelerator-config", self.accelerator_config, "Unknown accelerator configuration")
Manupa Karunaratned83d2e12020-07-20 12:05:32 +0100213 self.accelerator_config = Accelerator(accelerator_config)
Tim Hall79d07d22020-04-27 18:20:16 +0100214 accel_config = ArchitectureFeatures.accelerator_configs[self.accelerator_config]
215 self.config = accel_config
216
217 self.system_config = system_config
Tim Hall1bd531d2020-11-01 20:59:36 +0000218 self.memory_mode = memory_mode
Tim Hallc8a73862020-10-27 12:43:14 +0000219 self.is_ethos_u65_system = self.accelerator_config in (Accelerator.Ethos_U65_256, Accelerator.Ethos_U65_512)
Tim Hall79d07d22020-04-27 18:20:16 +0100220
Tim Hallc8a73862020-10-27 12:43:14 +0000221 self.max_outstanding_dma = 2 if self.is_ethos_u65_system else 1
Tim Hall289a41d2020-08-04 21:40:14 +0100222 self.max_outstanding_kernels = 3
223
Tim Hall79d07d22020-04-27 18:20:16 +0100224 self.ncores = accel_config.cores
225 self.ofm_ublock = accel_config.ofm_ublock
226 self.ifm_ublock = accel_config.ifm_ublock
Tim Hall79d07d22020-04-27 18:20:16 +0100227 self.ofm_block_max = Block(64, 32, 128)
228 self.override_block_config = override_block_config
229 self.block_config_limit = block_config_limit
230
Tim Hall79d07d22020-04-27 18:20:16 +0100231 self.max_blockdep = max_blockdep
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200232 self.weight_estimation_scaling = weight_estimation_scaling
Tim Hall79d07d22020-04-27 18:20:16 +0100233
234 dpu_min_height = accel_config.ofm_ublock.height
235 dpu_min_width = accel_config.ofm_ublock.width
236 dpu_dot_product_width = 8
237 dpu_min_ofm_channels = accel_config.ofm_ublock.depth
238
239 self.num_elem_wise_units = accel_config.elem_units
240 self.num_macs_per_cycle = dpu_min_height * dpu_min_width * dpu_dot_product_width * dpu_min_ofm_channels
241
Tim Hall1bd531d2020-11-01 20:59:36 +0000242 # Get system configuration and memory mode
243 self._get_vela_config(vela_config_files, verbose_config)
Tim Hall79d07d22020-04-27 18:20:16 +0100244
Tim Hall1bd531d2020-11-01 20:59:36 +0000245 self.axi_port_width = 128 if self.is_ethos_u65_system else 64
246 self.memory_bandwidths_per_cycle = self.axi_port_width * self.memory_clock_scales / 8
Tim Hall79d07d22020-04-27 18:20:16 +0100247
Tim Hall1bd531d2020-11-01 20:59:36 +0000248 self.memory_bandwidths_per_second = self.memory_bandwidths_per_cycle * self.core_clock
Louis Verhaard024c3552021-03-17 14:26:34 +0100249 # Max value in address offsets
250 self.max_address_offset = 1 << 48 if self.is_ethos_u65_system else 1 << 32
Tim Hall79d07d22020-04-27 18:20:16 +0100251
Diqing Zhonge8887a32020-09-24 09:53:48 +0200252 # Get output/activation performance numbers
253 self._generate_output_perf_tables(self.accelerator_config)
254
Tim Hall79d07d22020-04-27 18:20:16 +0100255 # sizes as N x H x W x C. we need to round up to these when allocating storage
256 self.storage_rounding_quantums = {
257 TensorFormat.Unknown: (1, 1, 1, 1),
258 TensorFormat.WeightsCompressed: (1, 1, 1, 1),
259 TensorFormat.NHWC: (1, 1, 1, 1),
260 TensorFormat.NHCWB16: (1, 1, 1, 16),
261 }
262
263 # brick sizes as N x H x W x C. We have to fetch whole bricks at a time
264 self.brick_sizes = {
265 TensorFormat.Unknown: (1, 1, 1, 1),
266 TensorFormat.WeightsCompressed: (1, 1, 1, 1),
267 TensorFormat.NHWC: (1, 1, 1, 1),
268 TensorFormat.NHCWB16: (1, 1, 1, 16),
269 }
270
Tim Hall79d07d22020-04-27 18:20:16 +0100271 self.default_weight_format = TensorFormat.WeightsCompressed
272 self.default_feature_map_format = TensorFormat.NHWC
273
Tim Hall79d07d22020-04-27 18:20:16 +0100274 self.tensor_storage_mem_area = {
275 # permanent mem_area
Tim Hall465582c2020-05-26 09:33:14 +0100276 TensorPurpose.Unknown: MemArea.Unknown,
Tim Hall79d07d22020-04-27 18:20:16 +0100277 TensorPurpose.Weights: self.permanent_storage_mem_area,
278 TensorPurpose.FeatureMap: self.feature_map_storage_mem_area,
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200279 TensorPurpose.LUT: self.permanent_storage_mem_area,
Fredrik Svedberge22ba8c2021-01-27 16:53:41 +0100280 TensorPurpose.Scratch: self.feature_map_storage_mem_area,
281 TensorPurpose.ScratchFast: self.fast_storage_mem_area,
Tim Hall79d07d22020-04-27 18:20:16 +0100282 }
283
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200284 self.tensor_storage_mem_type = {
Dwight Lidman1a9d20e2020-08-11 12:10:36 +0200285 TensorPurpose.Unknown: MemType.Unknown,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200286 TensorPurpose.Weights: MemType.Permanent_NPU,
287 TensorPurpose.FeatureMap: MemType.Scratch,
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200288 TensorPurpose.LUT: MemType.Scratch,
Fredrik Svedberge22ba8c2021-01-27 16:53:41 +0100289 TensorPurpose.Scratch: MemType.Scratch,
290 TensorPurpose.ScratchFast: MemType.Scratch_fast,
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200291 }
Tim Hall79d07d22020-04-27 18:20:16 +0100292
293 self.min_block_sizes = {
294 NpuBlockType.Default: (dpu_min_height, dpu_min_width),
295 NpuBlockType.VectorProduct: (1, 1),
296 NpuBlockType.ConvolutionMxN: (dpu_min_height, dpu_min_width),
297 NpuBlockType.Pooling: (dpu_min_height, dpu_min_width),
298 NpuBlockType.ConvolutionDepthWise: (dpu_min_height, dpu_min_width),
299 NpuBlockType.ElementWise: (1, 1),
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200300 NpuBlockType.ReduceSum: (dpu_min_height, dpu_min_width),
Tim Hall79d07d22020-04-27 18:20:16 +0100301 }
302
303 self.sub_kernel_limits = {
304 NpuBlockType.Default: (8, 8),
305 NpuBlockType.VectorProduct: (1, 1),
306 NpuBlockType.ConvolutionMxN: (8, 8),
307 NpuBlockType.Pooling: (8, 8),
308 NpuBlockType.ConvolutionDepthWise: (8, 8),
309 NpuBlockType.ElementWise: (1, 1),
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200310 NpuBlockType.ReduceSum: (8, 8),
Tim Hall79d07d22020-04-27 18:20:16 +0100311 }
312
313 # weights for scheduler search
314 from .npu_performance import make_bandwidth_array
315
316 self.bandwidth_weights = make_bandwidth_array()
317 self.bandwidth_weights[MemArea.Sram] = 1.0
318 self.bandwidth_weights[MemArea.Dram] = 10.0
319 self.bandwidth_weights[MemArea.OnChipFlash] = 2.0
320 self.bandwidth_weights[MemArea.OffChipFlash] = 20.0
321 self.cycles_weight = 40
322 self.max_sram_used_weight = 1000
323
Tim Hall1bd531d2020-11-01 20:59:36 +0000324 if self.is_spilling_enabled():
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200325 self.max_sram_used_weight = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100326
327 # Shared Buffer Block allocations
328 self.shram_bank_size = 1024 # bytes
329 self.shram_size_bytes = accel_config.shram_banks * self.shram_bank_size
330 self.shram_reserved_output_banks = 2
331 self.shram_reserved_weight_banks = 0
332 self.shram_reserved_unused_banks = 2 if accel_config.shram_banks > 16 else 0
333 self.shram_total_banks = accel_config.shram_banks - self.shram_reserved_unused_banks
334 self.shram_bank_granules = np.array(accel_config.shram_granules, np.int32)
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200335 self.shram_lut_size = 2048
336 # SHRAM base address of the activation lookup table
337 self.shram_lut_address = self.shram_bank_size * self.available_shram_banks(True)
Tim Hall79d07d22020-04-27 18:20:16 +0100338
339 # Build a map of acceptable IFM/OFM block configurations up to the maximum
340 # IFM/OFM block size.
341 ifm_block_max = self.get_ifm_block_size(32, self.ofm_block_max, Kernel(8, 8))
342 self.block_config_map = dict()
343 self.generate_block_config_map(Block(ifm_block_max.width, ifm_block_max.height, 128))
344
345 # Setup supported operators and restriction checkers class
Fredrik Svedberg880e7352020-08-25 11:31:47 +0200346 self.supported_operators = SupportedOperators()
Tim Hall79d07d22020-04-27 18:20:16 +0100347
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200348 # Returns available number of SHRAM banks depending on activation lookup table
349 # being used or not
350 def available_shram_banks(self, uses_activation_lut):
351 banks = self.shram_total_banks
352 if uses_activation_lut and self.shram_reserved_unused_banks == 0:
353 banks -= 2
354 return banks
355
Tim Hall79d07d22020-04-27 18:20:16 +0100356 # Calculate block configuration for ALL known IFM operations and
357 # accumulator sizes. Consumers will need to select their preferred
358 # operation and bit-width at read-time.
359 def generate_block_config(self, width, height, depth):
Louis Verhaardf98c6742020-05-12 14:22:38 +0200360 # Number of bytes required for any SHRAM element for a FM of given dimensions.
361 # For IFM: size = H*W*Align(D*BYTE_WIDTH, 8)
362 # For ACC: size = H*W*Align(D,8)*BYTE_WIDTH
363 d1 = round_up(depth, SHRAMElements.PreAlign)
364 d2 = round_up(d1 * SHRAMElements.ByteSizes, SHRAMElements.PostAlign)
365 size_bytes = (height * width) * d2
366
Tim Hall79d07d22020-04-27 18:20:16 +0100367 # Convert byte size (rounded) to size in banks
368 size_banks = round_up_divide(size_bytes, self.shram_bank_size)
369 size_banks *= 2 # Double buffer the IFM/Acc (need twice as many banks)
370 # Round bank requirement to bank granularity
371 required_banks = round_up(size_banks, self.shram_bank_granules)
372 return SHRAMBlockConfig(size_bytes, required_banks)
373
374 @staticmethod
375 def make_block_config_key(width, height, depth):
376 return (int(height), int(width), int(depth))
377
378 def get_block_config(self, width, height, depth):
379 assert depth <= self.ofm_block_max.depth
380 key = ArchitectureFeatures.make_block_config_key(width, height, depth)
381 config = self.block_config_map.get(key, None)
382 return config
383
384 # Generate a key:value map of possible block configurations, where the
385 # key is compounded from the block dimensions: 0x00HHWWCC
386 def generate_block_config_map(self, block: Block):
387 for h in range(1, block.height + 1):
388 for w in range(1, block.width + 1):
389 # All possible IFM/OFM depth values
390 for c in [4, 8, 12, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128]:
391 key = ArchitectureFeatures.make_block_config_key(w, h, c)
392 self.block_config_map[key] = self.generate_block_config(w, h, c)
393
Diqing Zhonge8887a32020-09-24 09:53:48 +0200394 def _generate_output_perf_tables(self, accel_config):
395 if accel_config == Accelerator.Ethos_U55_32:
396 self.output_cycles_per_elem = (2.0, 3.0, 3.0, 3.0, 4.0, 6.0, 1.0, 2.0)
397 self.activation_cycles_per_elem = (1.0, 1.0, 0.0)
398 elif accel_config == Accelerator.Ethos_U55_64:
399 self.output_cycles_per_elem = (1.0, 1.5, 1.5, 1.5, 2.0, 3.0, 0.5, 1.0)
400 self.activation_cycles_per_elem = (1.0, 1.0, 0.0)
401 elif accel_config == Accelerator.Ethos_U55_128:
402 self.output_cycles_per_elem = (0.75, 1.25, 0.75, 0.75, 1.0, 1.5, 0.25, 0.5)
403 self.activation_cycles_per_elem = (1.0, 0.5, 0.0)
Tim Hallc8a73862020-10-27 12:43:14 +0000404 elif accel_config in (Accelerator.Ethos_U55_256, Accelerator.Ethos_U65_256):
Diqing Zhonge8887a32020-09-24 09:53:48 +0200405 self.output_cycles_per_elem = (0.625, 1.125, 0.5, 0.375, 0.5, 0.75, 0.125, 0.25)
406 self.activation_cycles_per_elem = (1.0, 0.25, 0.0)
407 else:
Tim Hallc8a73862020-10-27 12:43:14 +0000408 assert accel_config == Accelerator.Ethos_U65_512
Diqing Zhonge8887a32020-09-24 09:53:48 +0200409 self.output_cycles_per_elem = (0.3125, 0.5625, 0.25, 0.1875, 0.25, 0.375, 0.0625, 0.125)
410 self.activation_cycles_per_elem = (0.5, 0.125, 0.0)
411
Tim Hall79d07d22020-04-27 18:20:16 +0100412 def calc_ifm_block_depth(self, ifm_depth, ifm_bits):
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200413 assert ifm_bits in (8, 16, 32)
Tim Hall79d07d22020-04-27 18:20:16 +0100414 assert ifm_depth > 0
415 ifm_depth = round_up(ifm_depth, self.ifm_ublock.depth)
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200416 max_block_depth = 8 * 32 // ifm_bits
Tim Hall79d07d22020-04-27 18:20:16 +0100417 return min(max_block_depth, ifm_depth)
418
419 # Calculate the size of the IFM block given a depth, target OFM block and a kernel
Tim Hallc30f4952020-06-15 20:47:35 +0100420 def get_ifm_block_size(
421 self,
422 ifm_block_depth,
423 ofm_block: Block,
424 kernel: Kernel,
425 subkernel: Block = Block(8, 8, 65536),
426 ifm_resampling_mode=resampling_mode.NONE,
427 ):
Dwight Lidmana9390f72020-05-13 12:00:08 +0200428 upscaling = 1 if ifm_resampling_mode == resampling_mode.NONE else 2
Tim Hall79d07d22020-04-27 18:20:16 +0100429 # Height
430 ifm_odd_2x_height_enable = 0
431 dilated_kernel_height = ((kernel.height - 1) * kernel.dilation.y) + 1
432 ifm_block_height = (
433 (ofm_block.height - 1) * kernel.stride.y
434 + min(subkernel.height, dilated_kernel_height)
435 + ifm_odd_2x_height_enable
436 ) // upscaling
437
Dwight Lidman0538a772020-05-06 14:09:17 +0200438 ifm_block_height = round_up(ifm_block_height, self.ofm_ublock.height)
Tim Hall79d07d22020-04-27 18:20:16 +0100439
440 # Width
441 ifm_odd_2x_width_enable = 0
442 dilated_kernel_width = ((kernel.width - 1) * kernel.dilation.x) + 1
443 ifm_block_width = (
444 (ofm_block.width - 1) * kernel.stride.x
445 + min(subkernel.width, dilated_kernel_width)
446 + ifm_odd_2x_width_enable
447 ) // upscaling
448
Dwight Lidman0538a772020-05-06 14:09:17 +0200449 ifm_block_width = round_up(ifm_block_width, self.ofm_ublock.width)
Tim Hall79d07d22020-04-27 18:20:16 +0100450
451 return Block(ifm_block_width, ifm_block_height, ifm_block_depth)
452
Tim Hall1bd531d2020-11-01 20:59:36 +0000453 def is_spilling_enabled(self):
Tim Hall79d07d22020-04-27 18:20:16 +0100454 """
Tim Hall1bd531d2020-11-01 20:59:36 +0000455 Spilling is a feature that allows the Ethos-U to use a dedicated SRAM as a cache for various types of data
Tim Hall79d07d22020-04-27 18:20:16 +0100456 """
Tim Hall1bd531d2020-11-01 20:59:36 +0000457 return (
458 self._mem_port_mapping(self.cache_mem_area) == MemArea.Sram and self.cache_mem_area != self.arena_mem_area
459 )
Tim Hall79d07d22020-04-27 18:20:16 +0100460
Louis Verhaard024c3552021-03-17 14:26:34 +0100461 def mem_type_size(self, mem_type: MemType) -> int:
462 """Returns size in bytes available for the given memory type"""
463 if mem_type == MemType.Scratch_fast and self.is_spilling_enabled():
464 return self.sram_size
465 # Size is unknown, return max possible address offset
466 return self.max_address_offset
467
Tim Hall1bd531d2020-11-01 20:59:36 +0000468 def _mem_port_mapping(self, mem_port):
469 mem_port_mapping = {MemPort.Axi0: self.axi0_port, MemPort.Axi1: self.axi1_port}
470 return mem_port_mapping[mem_port]
Tim Hall79d07d22020-04-27 18:20:16 +0100471
Tim Hall1bd531d2020-11-01 20:59:36 +0000472 def _set_default_sys_config(self):
Tim Hall1bd531d2020-11-01 20:59:36 +0000473 # ArchitectureFeatures.DEFAULT_CONFIG values
474 if self.is_ethos_u65_system:
475 # Default Ethos-U65 system configuration
476 # Ethos-U65 Client-Server: SRAM (16 GB/s) and DRAM (12 GB/s)
477 self.core_clock = 1e9
478 self.axi0_port = MemArea.Sram
479 self.axi1_port = MemArea.Dram
480 self.memory_clock_scales[MemArea.Sram] = 1.0
481 self.memory_clock_scales[MemArea.Dram] = 0.75 # 3 / 4
Diqing Zhongf842b692020-12-11 13:07:37 +0100482 self.memory_burst_length[MemArea.Sram] = 32
483 self.memory_burst_length[MemArea.Dram] = 128
484 self.memory_latency[MemArea.Sram][BandwidthDirection.Read] = 32
485 self.memory_latency[MemArea.Sram][BandwidthDirection.Write] = 32
486 self.memory_latency[MemArea.Dram][BandwidthDirection.Read] = 500
487 self.memory_latency[MemArea.Dram][BandwidthDirection.Write] = 250
Tim Hall79d07d22020-04-27 18:20:16 +0100488 else:
Tim Hall1bd531d2020-11-01 20:59:36 +0000489 # Default Ethos-U55 system configuration
490 # Ethos-U55 High-End Embedded: SRAM (4 GB/s) and Flash (0.5 GB/s)
491 self.core_clock = 500e6
492 self.axi0_port = MemArea.Sram
493 self.axi1_port = MemArea.OffChipFlash
494 self.memory_clock_scales[MemArea.Sram] = 1.0
495 self.memory_clock_scales[MemArea.OffChipFlash] = 0.125 # 1 / 8
Diqing Zhongf842b692020-12-11 13:07:37 +0100496 self.memory_burst_length[MemArea.Sram] = 32
497 self.memory_burst_length[MemArea.OffChipFlash] = 128
498 self.memory_latency[MemArea.Sram][BandwidthDirection.Read] = 32
499 self.memory_latency[MemArea.Sram][BandwidthDirection.Write] = 32
500 self.memory_latency[MemArea.OffChipFlash][BandwidthDirection.Read] = 64
501 self.memory_latency[MemArea.OffChipFlash][BandwidthDirection.Write] = 64
Tim Hall79d07d22020-04-27 18:20:16 +0100502
Tim Hall1bd531d2020-11-01 20:59:36 +0000503 def _set_default_mem_mode(self):
Tim Hall1bd531d2020-11-01 20:59:36 +0000504 # ArchitectureFeatures.DEFAULT_CONFIG values
505 if self.is_ethos_u65_system:
506 # Default Ethos-U65 memory mode
Tim Hall70b71a52020-12-22 11:47:54 +0000507 # Dedicated SRAM: the SRAM is only for use by the Ethos-U
508 # The non-SRAM memory is assumed to be read-writeable
Tim Hall1bd531d2020-11-01 20:59:36 +0000509 self.const_mem_area = MemPort.Axi1
510 self.arena_mem_area = MemPort.Axi1
511 self.cache_mem_area = MemPort.Axi0
512 self.cache_sram_size = 384 * 1024
513 else:
Tim Hall70b71a52020-12-22 11:47:54 +0000514 # Default Ethos-U55 memory mode
515 # Shared SRAM: the SRAM is shared between the Ethos-U and the Cortex-M software
516 # The non-SRAM memory is assumed to be read-only
Tim Hall1bd531d2020-11-01 20:59:36 +0000517 self.const_mem_area = MemPort.Axi1
518 self.arena_mem_area = MemPort.Axi0
519 self.cache_mem_area = MemPort.Axi0
Tim Hall79d07d22020-04-27 18:20:16 +0100520
Tim Hall1bd531d2020-11-01 20:59:36 +0000521 def _get_vela_config(self, vela_config_files, verbose_config):
522 """
523 Gets the system configuration and memory modes from one or more Vela configuration file(s) or uses some
524 defaults.
525 """
Tim Hall79d07d22020-04-27 18:20:16 +0100526
Tim Hall1bd531d2020-11-01 20:59:36 +0000527 # all properties are optional and are initialised to a value of 1 (or the equivalent)
528 self.core_clock = 1
529 self.axi0_port = MemArea(1)
530 self.axi1_port = MemArea(1)
531 self.memory_clock_scales = np.ones(MemArea.Size)
Tim Hall70b71a52020-12-22 11:47:54 +0000532 self.memory_burst_length = np.ones(MemArea.Size, np.int)
533 self.memory_latency = np.zeros((MemArea.Size, BandwidthDirection.Size), np.int)
Tim Hall1bd531d2020-11-01 20:59:36 +0000534 self.const_mem_area = MemPort(1)
535 self.arena_mem_area = MemPort(1)
536 self.cache_mem_area = MemPort(1)
537 self.cache_sram_size = 1
Tim Hall79d07d22020-04-27 18:20:16 +0100538
Tim Hall1bd531d2020-11-01 20:59:36 +0000539 # read configuration file(s)
540 self.vela_config = None
541
542 if vela_config_files is not None:
543 self.vela_config = ConfigParser()
544 self.vela_config.read(vela_config_files)
545
546 # read system configuration
547 sys_cfg_section = "System_Config." + self.system_config
548
549 if self.vela_config is not None and self.vela_config.has_section(sys_cfg_section):
550 self.core_clock = float(self._read_config(sys_cfg_section, "core_clock", self.core_clock))
551 self.axi0_port = MemArea[self._read_config(sys_cfg_section, "axi0_port", self.axi0_port)]
552 self.axi1_port = MemArea[self._read_config(sys_cfg_section, "axi1_port", self.axi1_port)]
553
554 for mem_area in (self.axi0_port, self.axi1_port):
555 self.memory_clock_scales[mem_area] = float(
556 self._read_config(
557 sys_cfg_section, mem_area.name + "_clock_scale", self.memory_clock_scales[mem_area]
558 )
559 )
Diqing Zhongf842b692020-12-11 13:07:37 +0100560 self.memory_burst_length[mem_area] = int(
561 self._read_config(
562 sys_cfg_section, mem_area.name + "_burst_length", self.memory_burst_length[mem_area]
563 )
564 )
565 self.memory_latency[mem_area][BandwidthDirection.Read] = int(
566 self._read_config(
567 sys_cfg_section,
568 mem_area.name + "_read_latency",
569 self.memory_latency[mem_area][BandwidthDirection.Read],
570 )
571 )
572 self.memory_latency[mem_area][BandwidthDirection.Write] = int(
573 self._read_config(
574 sys_cfg_section,
575 mem_area.name + "_write_latency",
576 self.memory_latency[mem_area][BandwidthDirection.Write],
577 )
578 )
Tim Hall1bd531d2020-11-01 20:59:36 +0000579 elif self.system_config == ArchitectureFeatures.DEFAULT_CONFIG:
580 self._set_default_sys_config()
581
582 elif vela_config_files is None:
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000583 raise CliOptionError("--config", vela_config_files, "Vela config file not specified")
Tim Hall1bd531d2020-11-01 20:59:36 +0000584
585 else:
586 raise CliOptionError(
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000587 "--system-config", self.system_config, f"Section {sys_cfg_section} not found in Vela config file",
Tim Hall79d07d22020-04-27 18:20:16 +0100588 )
Tim Hall79d07d22020-04-27 18:20:16 +0100589
Tim Hall1bd531d2020-11-01 20:59:36 +0000590 # read the memory mode
591 mem_mode_section = "Memory_Mode." + self.memory_mode
Tim Hall79d07d22020-04-27 18:20:16 +0100592
Tim Hall1bd531d2020-11-01 20:59:36 +0000593 if self.vela_config is not None and self.vela_config.has_section(mem_mode_section):
594 self.const_mem_area = MemPort[
595 self._read_config(mem_mode_section, "const_mem_area", self.const_mem_area.name)
596 ]
597 self.arena_mem_area = MemPort[
598 self._read_config(mem_mode_section, "arena_mem_area", self.arena_mem_area.name)
599 ]
600 self.cache_mem_area = MemPort[
601 self._read_config(mem_mode_section, "cache_mem_area", self.cache_mem_area.name)
602 ]
603 self.cache_sram_size = int(self._read_config(mem_mode_section, "cache_sram_size", self.cache_sram_size))
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200604
Tim Hall1bd531d2020-11-01 20:59:36 +0000605 elif self.memory_mode == ArchitectureFeatures.DEFAULT_CONFIG:
606 self._set_default_mem_mode()
Patrik Gustavsson5f47c052020-06-25 12:56:04 +0200607
Tim Hall1bd531d2020-11-01 20:59:36 +0000608 elif vela_config_files is None:
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000609 raise CliOptionError("--config", vela_config_files, "Vela config file not specified")
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200610
Tim Hall1bd531d2020-11-01 20:59:36 +0000611 else:
612 raise CliOptionError(
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000613 "--memory-mode", self.memory_mode, f"Section {mem_mode_section} not found in Vela config file",
Tim Hall1bd531d2020-11-01 20:59:36 +0000614 )
Tim Hall79d07d22020-04-27 18:20:16 +0100615
Tim Hall1bd531d2020-11-01 20:59:36 +0000616 # override sram to onchipflash
617 if self._mem_port_mapping(self.const_mem_area) == MemArea.Sram:
618 if self.const_mem_area == self.arena_mem_area == self.cache_mem_area:
619 print(
620 "Info: Changing const_mem_area from Sram to OnChipFlash. This will use the same characteristics as"
621 " Sram."
622 )
623 if self.const_mem_area == MemPort.Axi0:
624 self.const_mem_area = MemPort.Axi1
625 self.axi1_port = MemArea.OnChipFlash
626 else:
627 self.const_mem_area = MemPort.Axi0
628 self.axi0_port = MemArea.OnChipFlash
629 self.memory_clock_scales[MemArea.OnChipFlash] = self.memory_clock_scales[MemArea.Sram]
Diqing Zhongf842b692020-12-11 13:07:37 +0100630 self.memory_burst_length[MemArea.OnChipFlash] = self.memory_burst_length[MemArea.Sram]
631 self.memory_latency[MemArea.OnChipFlash] = self.memory_latency[MemArea.Sram]
Tim Hall1bd531d2020-11-01 20:59:36 +0000632
633 # check configuration
Tim Hall70b71a52020-12-22 11:47:54 +0000634 if self._mem_port_mapping(self.const_mem_area) not in (
635 MemArea.Dram,
636 MemArea.OnChipFlash,
637 MemArea.OffChipFlash,
638 ):
639 raise ConfigOptionError(
640 "const_mem_area",
641 self._mem_port_mapping(self.const_mem_area).name,
642 "Dram or OnChipFlash or OffChipFlash",
643 )
644
645 if self._mem_port_mapping(self.arena_mem_area) not in (MemArea.Sram, MemArea.Dram):
646 raise ConfigOptionError("arena_mem_area", self._mem_port_mapping(self.arena_mem_area).name, "Sram or Dram")
647
Tim Hall1bd531d2020-11-01 20:59:36 +0000648 if self._mem_port_mapping(self.cache_mem_area) != MemArea.Sram:
649 raise ConfigOptionError("cache_mem_area", self._mem_port_mapping(self.cache_mem_area).name, "Sram")
650
Tim Hall1bd531d2020-11-01 20:59:36 +0000651 # assign existing memory areas
652 self.permanent_storage_mem_area = self._mem_port_mapping(self.const_mem_area)
653 self.feature_map_storage_mem_area = self._mem_port_mapping(self.arena_mem_area)
654 self.fast_storage_mem_area = self._mem_port_mapping(self.cache_mem_area)
655
656 self.sram_size = self.cache_sram_size if self.is_spilling_enabled() else 9999 * 1024 * 1024
657
658 # display the system configuration and memory mode
659 if verbose_config:
660 print(f"System Configuration ({self.system_config}):")
661 print(f" core_clock = {self.core_clock}")
662 print(f" axi0_port = {self.axi0_port.name}")
663 print(f" axi1_port = {self.axi1_port.name}")
664 for mem in (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash):
665 print(f" {mem.name}_clock_scales = {self.memory_clock_scales[mem]}")
Diqing Zhongf842b692020-12-11 13:07:37 +0100666 print(f" {mem.name}_burst_length = {self.memory_burst_length[mem]}")
667 print(f" {mem.name}_read_latency = {self.memory_latency[mem][BandwidthDirection.Read]}")
668 print(f" {mem.name}_write_latency = {self.memory_latency[mem][BandwidthDirection.Write]}")
Tim Hall1bd531d2020-11-01 20:59:36 +0000669
670 print(f"Memory Mode ({self.memory_mode}):")
671 print(f" const_mem_area = {self.const_mem_area.name}")
672 print(f" arena_mem_area = {self.arena_mem_area.name}")
673 print(f" cache_mem_area = {self.cache_mem_area.name}")
674 print(f" cache_sram_size = {self.cache_sram_size}")
675
676 print("Architecture Settings:")
677 print(f" permanent_storage_mem_area = {self.permanent_storage_mem_area.name}")
678 print(f" feature_map_storage_mem_area = {self.feature_map_storage_mem_area.name}")
679 print(f" fast_storage_mem_area = {self.fast_storage_mem_area.name}")
680 print(f" sram_size = {self.sram_size}")
681
682 def _read_config(self, section, key, current_value):
Tim Hall79d07d22020-04-27 18:20:16 +0100683 """
Tim Hall1bd531d2020-11-01 20:59:36 +0000684 Reads a given key from a particular section in the Vela config file. If the section contains the 'inherit'
685 option then we recurse into the section specified. If inherited sections result in multiple keys for a
686 particular option then the key from the parent section is used, regardless of the parsing order
Tim Hall79d07d22020-04-27 18:20:16 +0100687 """
Tim Hall1bd531d2020-11-01 20:59:36 +0000688 if not self.vela_config.has_section(section):
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000689 raise ConfigOptionError("section", f"{section}. The section was not found in the Vela config file(s)")
Tim Hall1bd531d2020-11-01 20:59:36 +0000690
691 result = str(current_value)
692 if self.vela_config.has_option(section, "inherit"):
693 inheritance_section = self.vela_config.get(section, "inherit")
694 # check for recursion loop
695 if inheritance_section == section:
696 raise ConfigOptionError(
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000697 "inherit", f"{inheritance_section}. This references its own section and recursion is not allowed",
Tim Hall1bd531d2020-11-01 20:59:36 +0000698 )
699 result = self._read_config(inheritance_section, key, result)
700
701 if self.vela_config.has_option(section, key):
702 result = self.vela_config.get(section, key)
703
Tim Hall79d07d22020-04-27 18:20:16 +0100704 return result
Louis Verhaard52078302020-11-18 13:35:06 +0100705
706
Louis Verhaard061eeb42020-11-27 08:24:03 +0100707# Cache for default arch instances, as these are expensive to create
708default_arch_cache = dict()
709
710
Louis Verhaard52078302020-11-18 13:35:06 +0100711def create_default_arch(accelerator: Accelerator) -> ArchitectureFeatures:
712 """Creates architecture features object using default settings"""
Louis Verhaard061eeb42020-11-27 08:24:03 +0100713 if accelerator not in default_arch_cache:
714 default_arch_cache[accelerator] = ArchitectureFeatures(
715 vela_config_files=None,
716 accelerator_config=accelerator.value,
717 system_config=ArchitectureFeatures.DEFAULT_CONFIG,
718 memory_mode=ArchitectureFeatures.DEFAULT_CONFIG,
719 override_block_config=None,
720 block_config_limit=None,
721 max_blockdep=ArchitectureFeatures.MAX_BLOCKDEP,
722 weight_estimation_scaling=1.0,
723 verbose_config=False,
724 )
725 return default_arch_cache[accelerator]