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