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