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