blob: 8f2426c1ccaeca66bde78cbe881a2c663926a163 [file] [log] [blame]
Tim Halld8339a72021-05-27 18:49:40 +01001# Copyright (C) 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 Halld8339a72021-05-27 18:49:40 +010016#
Tim Hall79d07d22020-04-27 18:20:16 +010017# Description:
Tim Halld8339a72021-05-27 18:49:40 +010018# The scheduler creates and searches for an optimal plan for the network, selecting block configurations and
19# subdivisions for the Operators
Diego Russoea6111a2020-04-14 18:41:58 +010020import copy
Johan Alfvén5e0ae552022-02-09 21:20:10 +010021from collections import namedtuple
Tim Halld8339a72021-05-27 18:49:40 +010022from enum import auto
23from enum import IntEnum
24from typing import Dict
25from typing import List
26from typing import Optional
27from typing import Tuple
Diego Russoea6111a2020-04-14 18:41:58 +010028
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +010029import numpy as np
30
Diego Russoea6111a2020-04-14 18:41:58 +010031from . import live_range
Tim Hall79d07d22020-04-27 18:20:16 +010032from . import npu_performance
Tim Halld8339a72021-05-27 18:49:40 +010033from . import tensor_allocation
34from . import weight_compressor
35from .architecture_allocator import ArchitectureBlockConfig
36from .architecture_allocator import find_block_config
37from .architecture_allocator import get_ifm_area_required
Tim Halld8339a72021-05-27 18:49:40 +010038from .architecture_features import ArchitectureFeatures
39from .architecture_features import Block
40from .cascade_builder import CascadeBuilder
41from .cascade_builder import CascadeInfo
Fredrik Svedberg880e7352020-08-25 11:31:47 +020042from .data_type import DataType
Diego Russoe8a10452020-04-21 17:39:10 +010043from .nn_graph import CascadedPass
Tim Halld8339a72021-05-27 18:49:40 +010044from .nn_graph import Graph
45from .nn_graph import Pass
Diego Russoe8a10452020-04-21 17:39:10 +010046from .nn_graph import PassPlacement
Diego Russoe8a10452020-04-21 17:39:10 +010047from .nn_graph import SchedulingStrategy
Tim Halld8339a72021-05-27 18:49:40 +010048from .nn_graph import Subgraph
49from .numeric_util import round_down
50from .numeric_util import round_up
Diego Russoe8a10452020-04-21 17:39:10 +010051from .operation import NpuBlockType
Louis Verhaardaee5d752020-09-30 09:01:52 +020052from .operation import Op
Tim Halld8339a72021-05-27 18:49:40 +010053from .shape4d import Shape4D
Diego Russoe8a10452020-04-21 17:39:10 +010054from .tensor import MemArea
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020055from .tensor import MemType
Tim Halld8339a72021-05-27 18:49:40 +010056from .tensor import Tensor
Diego Russoe8a10452020-04-21 17:39:10 +010057from .tensor import TensorFormat
58from .tensor import TensorPurpose
59from .tensor import TensorSubPurpose
Jacob Bohlin1a666972020-09-11 10:04:15 +020060
Tim Hall79d07d22020-04-27 18:20:16 +010061
Tim Halld8339a72021-05-27 18:49:40 +010062def shape_for_format(shape: Shape4D, tensor_format: TensorFormat) -> Shape4D:
63 if tensor_format == TensorFormat.NHCWB16:
64 return shape.with_depth(round_up(shape.depth, 16))
65
66 return shape
67
68
69class OptimizationStrategy(IntEnum):
70 """Enum defining the different optimization strategies for the Scheduler"""
71
72 Size = auto()
73 Performance = auto()
Tim Hall79d07d22020-04-27 18:20:16 +010074
75 def __str__(self):
76 return self.name
77
78
Tim Halld8339a72021-05-27 18:49:40 +010079class SchedulerOpInfo:
80 """Contains metadata about a SchedulerOperation that is unique to one Schedule"""
81
Tim Hall79d07d22020-04-27 18:20:16 +010082 def __init__(
83 self,
Tim Halld8339a72021-05-27 18:49:40 +010084 block_config: ArchitectureBlockConfig,
85 weights_size: int,
86 stripe_input: Shape4D,
87 stripe_input2: Optional[Shape4D],
88 stripe: Shape4D,
Tim Hall79d07d22020-04-27 18:20:16 +010089 ):
Tim Halld8339a72021-05-27 18:49:40 +010090 self.block_config = block_config
91 self.weights_size = weights_size
92 self.stripe_input = stripe_input
93 self.stripe_input2 = stripe_input2
94 self.stripe = stripe
95 self.cascade = 0 # Assigned by CascadeBuilder. 0 means not part of a cascade
96 self.time_index = None # Set by update_op_memory_snapshot
97 self.ofm_depth_slices: List[int] = [0, stripe.depth]
98 self.npu_weights_tensor = None
Tim Halld784af72021-06-08 21:25:57 +010099 self.npu_scales_tensor = None
Tim Halld8339a72021-05-27 18:49:40 +0100100 self.buffered_weight_tensor = None
101 self.cycles = None
102 self.slack_buffering_cycles = 0
103 self.slack_buffering_memory = 0
104 self.full_weight_transfer_cycles = 0
105
106 def copy(self):
107 res = SchedulerOpInfo(self.block_config, self.weights_size, self.stripe_input, self.stripe_input2, self.stripe,)
108 res.cascade = self.cascade
109 return res
110
111 def __str__(self):
112 res = f"\t\tBlock Config = {self.block_config}\n"
113 res += f"\t\tOFM Block = {self.block_config.ofm_block}\n"
114 res += f"\t\tIFM Stripe = {self.stripe_input}\n"
115 res += f"\t\tIFM2 Stripe = {self.stripe_input2}\n"
116 res += f"\t\tOFM Stripe = {self.stripe}\n"
117 res += f"\t\tEncoded Weights = {self.npu_weights_tensor and len(self.npu_weights_tensor.buffer)} bytes\n"
118 res += (
119 f"\t\tWeight buffer = {self.buffered_weight_tensor and self.buffered_weight_tensor.storage_size()} bytes\n"
120 )
121 res += f"\t\tDepth slices = {self.ofm_depth_slices}\n"
122 res += f"\t\tAssigned Cascade = {self.cascade}"
123 return res
124
125
126class SchedulerOptions:
127 """Contains options for the Scheduler"""
128
129 def __init__(
130 self, optimization_strategy, sram_target, verbose_schedule,
131 ):
132 self.optimization_strategy = optimization_strategy
133 self.optimization_sram_limit = sram_target
Tim Hall79d07d22020-04-27 18:20:16 +0100134 self.verbose_schedule = verbose_schedule
Tim Hall79d07d22020-04-27 18:20:16 +0100135
Tim Halld8339a72021-05-27 18:49:40 +0100136 def __str__(self) -> str:
137 return f"{type(self).__name__}: {str(self.__dict__)}"
Tim Hall79d07d22020-04-27 18:20:16 +0100138
139 __repr__ = __str__
140
141
Tim Halld8339a72021-05-27 18:49:40 +0100142class SchedulerTensor:
143 def __init__(self, shape, dt, mem_area, _format):
144 self.dtype = dt
145 self.mem_area = mem_area
146 self.shape = shape
147 self.format = _format
148 self.connection = None
Tim Hall79d07d22020-04-27 18:20:16 +0100149
Tim Halld8339a72021-05-27 18:49:40 +0100150
151class SchedulerOperation:
152 """Scheduler internal representation of 'Operation'
153 This class can be seen as a node within the Scheduler Graph representation
154 """
155
156 def __init__(self, ps: Pass, arch: ArchitectureFeatures, nng: Graph):
157 self.arch = arch
158 self.parent_ps = ps
159 self.parent_op = ps.primary_op
160 self.name = ps.primary_op.name
161 self.op_type = ps.primary_op.type
162 self.activation = ps.primary_op.activation
163 self.kernel = ps.primary_op.kernel
164 self.resampling_mode = ps.primary_op.ifm.resampling_mode
165 self.uses_scalar = ps.primary_op.ifm2 is not None and (
166 ps.primary_op.ifm.shape == [] or ps.primary_op.ifm2.shape == []
Tim Hall79d07d22020-04-27 18:20:16 +0100167 )
Tim Halld8339a72021-05-27 18:49:40 +0100168 self.ifm_ublock = arch.ifm_ublock
Tim Hall79d07d22020-04-27 18:20:16 +0100169
Tim Halld8339a72021-05-27 18:49:40 +0100170 self.ifm = SchedulerTensor(ps.ifm_shapes[0], ps.ifm_tensor.dtype, ps.ifm_tensor.mem_area, ps.ifm_tensor.format,)
Tim Hall79d07d22020-04-27 18:20:16 +0100171
Tim Halld8339a72021-05-27 18:49:40 +0100172 self.ifm2 = None
173 if ps.ifm2_tensor:
174 self.ifm2 = SchedulerTensor(
175 ps.ifm_shapes[1], ps.ifm2_tensor.dtype, ps.ifm2_tensor.mem_area, ps.ifm2_tensor.format,
176 )
Tim Hall79d07d22020-04-27 18:20:16 +0100177
Tim Halld8339a72021-05-27 18:49:40 +0100178 self.ofm = SchedulerTensor(ps.ofm_shapes[0], ps.ofm_tensor.dtype, ps.ofm_tensor.mem_area, ps.ofm_tensor.format,)
Tim Hall79d07d22020-04-27 18:20:16 +0100179
Tim Halld8339a72021-05-27 18:49:40 +0100180 # Input volume width and height required to produce the smallest possible stripe
181 self.min_stripe_input_w, self.min_stripe_input_h = self._calculate_min_stripe_input()
Tim Hall79d07d22020-04-27 18:20:16 +0100182
Tim Halld8339a72021-05-27 18:49:40 +0100183 # Flags that marks whether this SchedulerOperation requires full IFM/OFM
184 self.requires_full_ifm = False
185 self.requires_full_ifm2 = False
186 self.requires_full_ofm = False
Tim Hall79d07d22020-04-27 18:20:16 +0100187
Tim Halld8339a72021-05-27 18:49:40 +0100188 self.index = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100189
Tim Halld8339a72021-05-27 18:49:40 +0100190 def add_ifm_connection(self, conn: "Connection"):
191 """Add input connection to another SchedulerOperation or Subgraph Input"""
192 conn.consumers.append(self)
193 self.ifm.connection = conn
Tim Hall79d07d22020-04-27 18:20:16 +0100194
Tim Halld8339a72021-05-27 18:49:40 +0100195 def add_ifm2_connection(self, conn: "Connection"):
196 """Add input connection to another SchedulerOperation or Subgraph Input"""
197 if self.ifm2:
198 conn.consumers.append(self)
199 self.ifm2.connection = conn
Tim Hall79d07d22020-04-27 18:20:16 +0100200 else:
Tim Halld8339a72021-05-27 18:49:40 +0100201 assert False, f"Trying to set an IFM2 Connection to {self} which has no IFM2"
Tim Hall79d07d22020-04-27 18:20:16 +0100202
Tim Halld8339a72021-05-27 18:49:40 +0100203 def add_ofm_connection(self, conn: "Connection"):
204 """Add output connection to another SchedulerOperation or Subgraph Output"""
205 conn.producers.append(self)
206 self.ofm.connection = conn
207
208 def get_dependants(self):
209 """Returns a list of the Ops that depend on this Operation's OFM"""
210 return self.ofm.connection.consumers
211
212 def ifm_size_in_bytes(self) -> int:
213 """Returns size of the IFM in bytes"""
214 ifm_storage_shape = shape_for_format(self.ifm.shape, self.ifm.format)
215 return round_up(ifm_storage_shape.elements() * self.ifm.dtype.size_in_bytes(), Tensor.AllocationQuantum)
216
217 def ifm2_size_in_bytes(self) -> int:
218 """Returns size of the IFM2 in bytes"""
219 if self.ifm2:
220 ifm2_storage_shape = shape_for_format(self.ifm2.shape, self.ifm2.format)
221 return round_up(ifm2_storage_shape.elements() * self.ifm2.dtype.size_in_bytes(), Tensor.AllocationQuantum)
222
223 return 0
224
225 def ofm_size_in_bytes(self) -> int:
226 """Returns size of the OFM in bytes"""
227 ofm_storage_shape = shape_for_format(self.ofm.shape, self.ofm.format)
228 return round_up(ofm_storage_shape.elements() * self.ofm.dtype.size_in_bytes(), Tensor.AllocationQuantum)
229
230 def create_scheduler_info(self, nng: Graph, stripe: Shape4D) -> SchedulerOpInfo:
231 """Returns schedule info about this SchedulerOperation based on how many ofm elements it should produce"""
232 ifm_shape = self.ifm.shape
233 ifm2_shape = self.ifm2 and self.ifm2.shape
234 ofm_shape = stripe
235
236 if ofm_shape != self.ofm.shape:
237 # Striped Op - Need to calculate stripe input volume
238 stripe_input_w, stripe_input_h = self._get_stripe_input_requirement(stripe)
239 # Ensure stripe input volume is within the full IFM volume
240 stripe_input_h = min(stripe_input_h, self.ifm.shape.height)
241 stripe_input_w = min(stripe_input_w, self.ifm.shape.width)
242 ifm_shape = ifm_shape.with_hw(stripe_input_h, stripe_input_w)
243
244 if self.ifm2:
245 stripe_input2_h = min(stripe_input_h, self.ifm2.shape.height)
246 stripe_input2_w = min(stripe_input_w, self.ifm2.shape.width)
247 ifm2_shape = ifm2_shape.with_hw(stripe_input2_h, stripe_input2_w)
248
249 block_config = self._get_block_config(ifm_shape, ifm2_shape, self.uses_scalar, ofm_shape)
250
251 scheduler_op_info = SchedulerOpInfo(block_config, 0, ifm_shape, ifm2_shape, ofm_shape)
252 if self.parent_op.weights:
253 # Default full-depth weight encoding with no buffering
Tim Halld784af72021-06-08 21:25:57 +0100254 (
255 scheduler_op_info.npu_weights_tensor,
256 scheduler_op_info.npu_scales_tensor,
257 ) = weight_compressor.encode_weight_and_scale_tensor(
Tim Halld8339a72021-05-27 18:49:40 +0100258 self.arch,
259 self.parent_op,
260 self.parent_op.weights,
261 self.parent_op.bias,
262 self.kernel,
263 block_config,
264 [0, self.ofm.shape.depth],
265 )
266
267 self.parent_ps.block_config = block_config.old_style_representation()
268 return scheduler_op_info
269
270 def _get_stripe_input_requirement(self, stripe_shape: Shape4D) -> Tuple[int, int]:
271 """Returns the amount of IFM required to produce the stripe with shape:'stripe_shape'"""
272 ofm_shape_to_produce = Block.from_shape(stripe_shape.as_list())
273
Fredrik Svedberg3ff7a4a2021-09-29 10:08:04 +0200274 return get_ifm_area_required(ofm_shape_to_produce, self.kernel, self.resampling_mode)
Tim Halld8339a72021-05-27 18:49:40 +0100275
276 def _calculate_min_stripe_input(self) -> Shape4D:
277 # Calculate the input volume required height and width for the smallest possible stripe (h,w = 1,1)
278 min_stripe = self.ofm.shape.with_hw(1, 1)
279 return self._get_stripe_input_requirement(min_stripe)
280
281 def _get_block_config(
282 self, ifm_shape: Shape4D, ifm2_shape: Optional[Shape4D], uses_scalar: bool, ofm_shape: Shape4D
283 ) -> ArchitectureBlockConfig:
284 # Returns a block config and SHRAM layout
285 lut_banks = 2 if self.parent_op.activation_lut else 0
286 return find_block_config(
287 self.arch,
288 self.op_type.npu_block_type,
289 ofm_shape,
290 ifm_shape,
291 ifm2_shape,
292 uses_scalar,
293 self.ifm.dtype.size_in_bits(),
294 self.kernel,
295 lut_banks,
296 self.parent_op.has_scaling(),
297 self.resampling_mode,
298 )
299
300
301class Connection:
302 """Scheduler internal representation of a Tensor that connects two SchedulerOperations
303 This class can be seen as an edge within the Scheduler Graph representation
304 """
305
306 def __init__(self, tensor: Tensor):
307 self.parent_tens = tensor
308
309 # SchedulerOperation relationships
310 self.producers: List[SchedulerOperation] = []
311 self.consumers: List[SchedulerOperation] = []
Tim Hall79d07d22020-04-27 18:20:16 +0100312
313 def __str__(self):
Tim Halld8339a72021-05-27 18:49:40 +0100314 return f"<Connection {self.parent_tens.name}>"
Tim Hall79d07d22020-04-27 18:20:16 +0100315
316 __repr__ = __str__
317
318
Tim Halld8339a72021-05-27 18:49:40 +0100319class Schedule:
320 """Class that contains a solution of how to schedule an NPU subgraph and its cost"""
Tim Hall79d07d22020-04-27 18:20:16 +0100321
Tim Halld8339a72021-05-27 18:49:40 +0100322 def __init__(self, sg: Subgraph, label: str):
323 self.sg = sg
324 self.label = label
325 self.cost_map: Dict[SchedulerOperation, SchedulerOpInfo] = {}
326 self.cascades: Dict[int, CascadeInfo] = {}
327 self.fast_storage_peak_usage = 0
328 self.memory_snapshot = None
329
330 @property
331 def name(self):
332 return f"{self.sg.name}_{self.label}"
Tim Hall79d07d22020-04-27 18:20:16 +0100333
334
Tim Halld8339a72021-05-27 18:49:40 +0100335class Scheduler:
336 """Main class of the Vela Scheduling"""
Tim Hall79d07d22020-04-27 18:20:16 +0100337
Tim Halld8339a72021-05-27 18:49:40 +0100338 def __init__(self, nng: Graph, sg: Subgraph, arch: ArchitectureFeatures, options: SchedulerOptions):
Tim Hall79d07d22020-04-27 18:20:16 +0100339 self.nng = nng
340 self.sg = sg
341 self.arch = arch
Tim Halld8339a72021-05-27 18:49:40 +0100342 self.sched_ops: List(SchedulerOperation) = []
343 self.max_schedule = None
344 self.scheduler_options = options
Tim Hall79d07d22020-04-27 18:20:16 +0100345
Johan Alfvén5e0ae552022-02-09 21:20:10 +0100346 def avoid_nhcwb16_for_ofm(self, tens, ps, arch):
347 # Only run this check for opt strategy Size
348 if self.scheduler_options.optimization_strategy == OptimizationStrategy.Performance:
349 return False
350
351 op = ps.primary_op
352 if not op.type.is_elementwise_op():
353 return False
354
355 depth = op.ofm_shapes[0][-1]
356 if (depth % 16) == 0:
357 return False
358
359 # Check if overwriting the inputs can be allowed
360 OpShapeTens = namedtuple("OpShapeTens", ["op_shape", "tens"])
361 outp = OpShapeTens(op.ofm_shapes[0], op.ofm)
362 inps = []
363 if op.ifm is not None:
364 inps.append(OpShapeTens(op.ifm_shapes[0], op.ifm))
365 if op.ifm2 is not None:
366 inps.append(OpShapeTens(op.ifm_shapes[1], op.ifm2))
367
368 # Find an input tensor that can be overwritten by the output
369 for inp in inps:
370 if (
371 # check op input and output shapes allow overlapping
372 inp.op_shape == outp.op_shape
373 # check input tensor is valid
374 and inp.tens is not None
375 and inp.tens.shape != []
376 # check input and output tensors are compatible
377 and inp.tens.format == outp.tens.format
378 and inp.tens.dtype == outp.tens.dtype
379 ):
380 if inp.tens.format == TensorFormat.NHWC:
381 return True
382
383 return False
384
Tim Halld8339a72021-05-27 18:49:40 +0100385 def create_scheduler_representation(self, arch: ArchitectureFeatures):
386 """Creates a Scheduler Graph representation"""
387 # Temporary dict for creating connections between the Operations
388 connections: Dict[Tensor, Connection] = {}
389 # Memory required for the largest FeatureMap that has to be full
390 min_memory_req = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100391 for ps in self.sg.passes:
Tim Halld8339a72021-05-27 18:49:40 +0100392 if ps.primary_op:
393 # Set tensor format to NHCWB16 for output FeatureMaps, if possible
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200394 for output in ps.outputs:
Jacob Bohlina5e8c1c2021-06-14 13:33:39 +0200395 if output in self.sg.output_tensors or output.purpose != TensorPurpose.FeatureMap:
Patrik Gustavssonfeeb06d2020-04-22 12:53:47 +0200396 continue
Johan Alfvén5e0ae552022-02-09 21:20:10 +0100397
398 if output.needs_linear_format:
399 continue
400
401 if self.avoid_nhcwb16_for_ofm(output, ps, arch):
402 output.needs_linear_format = True
403 continue
404
405 output.set_format(TensorFormat.NHCWB16, arch)
Tim Halld8339a72021-05-27 18:49:40 +0100406
407 # Create SchedulerOperations
408 op = SchedulerOperation(ps, arch, self.nng)
409 op.index = len(self.sched_ops)
410
411 # Make connections
412 if ps.ifm_tensor not in connections:
413 connections[ps.ifm_tensor] = Connection(ps.ifm_tensor)
414 if ps.ifm2_tensor and ps.ifm2_tensor not in connections:
415 connections[ps.ifm2_tensor] = Connection(ps.ifm2_tensor)
416 if ps.ofm_tensor not in connections:
417 connections[ps.ofm_tensor] = Connection(ps.ofm_tensor)
418
419 op.add_ifm_connection(connections[ps.ifm_tensor])
420 if ps.ifm2_tensor:
421 op.add_ifm2_connection(connections[ps.ifm2_tensor])
422 op.add_ofm_connection(connections[ps.ofm_tensor])
423
424 # Set requirements on the ifm/ofm buffers
425 self.sched_ops.append(op)
426 if ps.ifm_tensor in self.sg.input_tensors:
427 # This Op consumes a subgraph input
428 op.requires_full_ifm = True
429 if ps.ifm2_tensor and ps.ifm2_tensor in self.sg.input_tensors:
430 # This Op consumes a subgraph input
431 op.requires_full_ifm2 = True
432 if ps.ofm_tensor in self.sg.output_tensors:
433 # This Op produces a subgraph output
434 op.requires_full_ofm = True
435 if ps.ifm_tensor.needs_linear_format:
436 op.requires_full_ifm = True
437 if ps.ifm2_tensor and ps.ifm2_tensor.needs_linear_format:
438 op.requires_full_ifm2 = True
439 if ps.ofm_tensor.needs_linear_format or ps.primary_op.memory_function == Op.ConcatSliceWrite:
440 op.requires_full_ofm = True
441 if len(ps.primary_op.outputs) > 1 or len(ps.primary_op.outputs[0].consumer_list) > 1:
442 # Op has multiple outputs or consumers - requires full OFM
443 op.requires_full_ofm = True
444
445 # Check memory requirements if this Op requires any full FeatureMaps
446 op_memory_req = 0
447 if op.requires_full_ifm:
448 op_memory_req += op.ifm_size_in_bytes()
449 if op.requires_full_ifm2:
450 op_memory_req += op.ifm2_size_in_bytes()
451 if op.requires_full_ofm:
452 op_memory_req += op.ofm_size_in_bytes()
453
454 min_memory_req = max(op_memory_req, min_memory_req)
455
456 # Theoretical minimum required memory - used to guide the cascade building
457 self.min_memory_req = min_memory_req
458
459 def create_initial_schedule(self) -> Schedule:
460 """Creates an initial schedule with no cascading or buffering of any kind"""
461 schedule = Schedule(self.sg, "MAX")
462
463 for op in self.sched_ops:
464 cost = op.create_scheduler_info(self.nng, op.ofm.shape)
465 cost.cycles = self.estimate_op_performance(op, cost.block_config, op.ofm.shape.depth)
466 schedule.cost_map[op] = cost
467
468 return schedule
469
470 def update_op_memory_snapshot(self, schedule: Schedule):
471 memories_list = [(self.arch.fast_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))]
472
473 # Collect live ranges from tensors
474 lr_graph = live_range.LiveRangeGraph()
475 for mem_area, mem_type_set in memories_list:
476 live_range.extract_live_ranges_from_cascaded_passes(
Fredrik Svedberg0ae28482021-10-27 13:58:03 +0200477 self.nng.get_root_subgraph(), mem_area, mem_type_set, lr_graph, Tensor.AllocationQuantum,
Tim Halld8339a72021-05-27 18:49:40 +0100478 )
479
480 # Populate time-array with memory used by live ranges
481 temporal_usage = lr_graph.get_temporal_memory_usage(self.arch.fast_storage_mem_area)
482 schedule.memory_snapshot = temporal_usage
483
484 # Set the peak memory usage
485 schedule.fast_storage_peak_usage = max(temporal_usage, default=0)
486
487 def estimate_op_performance(self, op: SchedulerOperation, block_config, ofm_depth):
488 query = npu_performance.PerformanceQuery(op.op_type.npu_block_type)
489 query.ifm_shape = op.ifm.shape
490 query.ifm_memory_area = op.ifm.mem_area
491 query.ifm_bits = op.ifm.dtype.size_in_bits()
492 query.ifm_format = op.ifm.format
493 query.ifm2_shape = op.ifm2 and op.ifm2.shape
494 query.ifm2_memory_area = op.ifm2 and op.ifm2.mem_area
495 query.ifm2_bits = op.ifm2 and op.ifm2.dtype.size_in_bits()
496 query.ifm2_format = op.ifm2 and op.ifm2.format
497 query.ofm_shape = op.ofm.shape.with_depth(ofm_depth)
498 query.ofm_memory_area = op.ofm.mem_area
499 query.ofm_bits = op.ofm.dtype.size_in_bits()
500 query.ofm_format = op.ofm.format
501 if op.parent_op.bias:
502 query.const_shape = Shape4D(1, 1, 1, op.ofm.shape.depth)
503 query.const_memory_area = self.arch.fast_storage_mem_area
504
505 query.kernel = op.kernel
506 query.config = block_config
507
508 return npu_performance.measure_cycle_cost(self.arch, op.op_type, op.activation and op.activation.op_type, query)
509
Tim Hall789e6f32021-06-17 17:02:31 +0100510 def propose_schedule_buffering(self, ref_schedule: Schedule, staging_limit_bytes):
Tim Halld8339a72021-05-27 18:49:40 +0100511 """Create a buffered schedule"""
512 buffered_schedule = Schedule(self.sg, f"{ref_schedule.label}_BUFFERED")
Tim Halld8339a72021-05-27 18:49:40 +0100513
514 prev_op = None
515 for sched_op in self.sched_ops:
516 if sched_op not in ref_schedule.cost_map:
517 # sched_op is not part of this sub-schedule - skip
518 continue
519
520 self.propose_operator_buffering(sched_op, prev_op, buffered_schedule, ref_schedule, staging_limit_bytes)
521 prev_op = sched_op
522
523 return buffered_schedule
524
525 def propose_operator_buffering(
526 self,
527 sched_op: SchedulerOperation,
528 prev_op: SchedulerOperation,
529 buffered_schedule: Schedule,
530 ref_schedule: Schedule,
531 staging_limit_bytes,
532 ):
533 # Mild recursion might mean this Op has already been seen
534 if sched_op in buffered_schedule.cost_map:
535 return
536
537 # Take the reference schedule as default costings for this schedule
538 ref_cost = ref_schedule.cost_map[sched_op]
539 cost = copy.copy(ref_cost)
540 cost.slack_buffering_cycles = ref_cost.cycles.op_cycles
541 memory_snapshot = ref_schedule.memory_snapshot
542 ref_memory_usage = memory_snapshot[ref_cost.time_index] if ref_cost.time_index < len(memory_snapshot) else 0
543 cost.slack_buffering_memory = staging_limit_bytes - ref_memory_usage
544 buffered_schedule.cost_map[sched_op] = cost
545
546 # Attempt weight buffering on anything with a weights tensor
547 if sched_op.parent_op.weights:
548 self.propose_weight_buffering(
549 sched_op.parent_op.weights,
550 sched_op.parent_op.bias,
551 sched_op,
552 prev_op,
553 buffered_schedule,
554 ref_schedule,
555 cost.slack_buffering_memory,
556 )
557
558 return cost
559
560 def weights_needs_dma(self, weight_tensor):
561 if weight_tensor and weight_tensor.mem_type not in (MemType.Scratch, MemType.Scratch_fast):
562 # Weights are in permanent storage
563 # Only when permanent storage differs from feature map storage, there is a point moving the data
564 if (
565 weight_tensor.mem_area in (MemArea.Dram, MemArea.OffChipFlash)
566 and self.arch.permanent_storage_mem_area != self.arch.fast_storage_mem_area
567 ):
568 return True
569 return False
570
571 def propose_weight_buffering(
572 self,
573 weight_tensor,
574 scale_tensor,
575 sched_op: SchedulerOperation,
576 prev_op: SchedulerOperation,
577 buffered_schedule: Schedule,
578 ref_schedule: Schedule,
579 buffer_limit_bytes,
580 ):
581 cost = buffered_schedule.cost_map[sched_op]
582 prev_cost = buffered_schedule.cost_map.get(prev_op)
583 ref_cost = ref_schedule.cost_map[sched_op]
584 assert cost and ref_cost
585
586 needs_dma = self.weights_needs_dma(weight_tensor)
587
588 ofm_full_depth_slices = [0, ref_cost.stripe.depth]
589
590 # Encode weights for the full depth
Tim Halld784af72021-06-08 21:25:57 +0100591 full_weights, full_scales = weight_compressor.encode_weight_and_scale_tensor(
Tim Halld8339a72021-05-27 18:49:40 +0100592 self.arch,
593 sched_op.parent_op,
594 weight_tensor,
595 scale_tensor,
596 sched_op.kernel,
597 cost.block_config,
598 ofm_full_depth_slices,
599 )
600 full_weights_bytes = len(full_weights.buffer)
601 cost.ofm_depth_slices = ofm_full_depth_slices
602
603 # No buffering required - take all the weights from permanent storage
604 if sched_op.op_type == Op.FullyConnected or not needs_dma:
605 cost.npu_weights_tensor = full_weights
Tim Halld784af72021-06-08 21:25:57 +0100606 cost.npu_scales_tensor = full_scales
Tim Halld8339a72021-05-27 18:49:40 +0100607 return
608
609 encoded_weights = full_weights
Tim Halld784af72021-06-08 21:25:57 +0100610 encoded_scales = full_scales
Tim Halld8339a72021-05-27 18:49:40 +0100611
612 # How many NPU cycles are available under the previously executing
613 # operator and SRAM unused for performing buffered DMA transfers
614 slack_cycles = prev_cost.slack_buffering_cycles if prev_cost else 0
615 slack_memory = prev_cost.slack_buffering_memory if prev_cost else 0
616
617 # Force full depth for cascaded Ops
618 if ref_cost.cascade != 0:
619 weight_tensor_purpose = TensorSubPurpose.Standard
620 weight_buffer_size = full_weights_bytes
621 # Update the memory snapshot to reflect the added size of the weights
622 ref_schedule.memory_snapshot[ref_cost.time_index] += weight_buffer_size
623 else:
624 # Estimate the buffering cycle time for the full set of weights
625 full_transfer_cycles = npu_performance.measure_mem2mem_cycles(
626 self.arch, weight_tensor.mem_area, self.arch.fast_storage_mem_area, full_weights_bytes
627 )
628 cost.full_weight_transfer_cycles = full_transfer_cycles
629
630 # Calculate the amount of prebuffering necessary (or what is possible with limited
631 # double buffer buffer size)
632 half_buffer_limit = buffer_limit_bytes // 2
633 if full_transfer_cycles > slack_cycles:
634 prebuffer_ratio = slack_cycles / full_transfer_cycles
635 prebuffer_bytes = min(prebuffer_ratio * full_weights_bytes, half_buffer_limit)
636 else:
637 prebuffer_bytes = min(full_weights_bytes, half_buffer_limit)
Tim Hall789e6f32021-06-17 17:02:31 +0100638
639 prebuffer_ratio = prebuffer_bytes / full_weights_bytes
Tim Halld8339a72021-05-27 18:49:40 +0100640
641 # Have to split the weights if the initial buffering can't store
642 # all of the compressed weights
643 if prebuffer_bytes < full_weights_bytes:
Tim Hall789e6f32021-06-17 17:02:31 +0100644 block_depth = cost.block_config.ofm_block.depth
Tim Halld8339a72021-05-27 18:49:40 +0100645
Tim Hall789e6f32021-06-17 17:02:31 +0100646 # Choose initial prebuffering depth (already buffer clamped)
647 prebuffer_depth = ref_cost.stripe.depth * prebuffer_ratio
Tim Halld8339a72021-05-27 18:49:40 +0100648 prebuffer_depth = int(max(16, round_down(prebuffer_depth, ArchitectureFeatures.OFMSplitDepth)))
649
Tim Hall789e6f32021-06-17 17:02:31 +0100650 # Calculate cycles executed during the prebuffer
651 pre_op_cycles = self.estimate_op_performance(sched_op, cost.block_config, prebuffer_depth)
652 buffering_depth = ref_cost.stripe.depth * (pre_op_cycles.op_cycles / full_transfer_cycles)
Tim Halld8339a72021-05-27 18:49:40 +0100653
Tim Hall789e6f32021-06-17 17:02:31 +0100654 # Choose initial buffering depth and clamp to the double buffering limit
655 buffering_depth = round_up(buffering_depth, block_depth)
656 buffering_bytes = (buffering_depth / ref_cost.stripe.depth) * full_weights_bytes
657 if buffering_bytes > half_buffer_limit:
658 buffering_depth = (half_buffer_limit / full_weights_bytes) * ref_cost.stripe.depth
659
660 while True:
661 # Attempt to buffer whole blocks
662 if buffering_bytes > block_depth:
663 buffering_depth = round_down(buffering_depth, block_depth)
664 else:
665 buffering_depth = round_down(buffering_depth, ArchitectureFeatures.OFMSplitDepth)
666 buffering_depth = int(max(buffering_depth, ArchitectureFeatures.OFMSplitDepth))
Tim Halld8339a72021-05-27 18:49:40 +0100667
668 # Create list of depth slices
669 depth_slices = [0]
670 if prebuffer_depth < ref_cost.stripe.depth:
671 depth_slices += list(range(prebuffer_depth, ref_cost.stripe.depth, buffering_depth))
672 depth_slices.append(ref_cost.stripe.depth)
673
674 # Encode weights based depth slices
675 cost.ofm_depth_slices = depth_slices
Tim Halld784af72021-06-08 21:25:57 +0100676 encoded_weights, encoded_scales = weight_compressor.encode_weight_and_scale_tensor(
Tim Halld8339a72021-05-27 18:49:40 +0100677 self.arch,
678 sched_op.parent_op,
679 weight_tensor,
680 scale_tensor,
681 sched_op.kernel,
682 cost.block_config,
683 cost.ofm_depth_slices,
684 )
685
686 # Chosen buffering might not fit at all, iterate until it does
687 # or until the minimum usable slice size is reached
688 if (
689 encoded_weights.max_range_bytes <= half_buffer_limit
690 or prebuffer_depth == ArchitectureFeatures.OFMSplitDepth
691 ):
692 break
693
Tim Hall789e6f32021-06-17 17:02:31 +0100694 if buffering_depth > prebuffer_depth:
695 buffering_depth = round_up(buffering_depth // 2, ArchitectureFeatures.OFMSplitDepth)
696 else:
697 prebuffer_depth = round_up(prebuffer_depth // 2, ArchitectureFeatures.OFMSplitDepth)
Tim Halld8339a72021-05-27 18:49:40 +0100698
699 # Calculate cycles required to run the last op for use as future slack
700 tail_cycles = self.estimate_op_performance(
701 sched_op, cost.block_config, depth_slices[-1] - depth_slices[-2]
702 )
703 cost.slack_buffering_cycles = tail_cycles.op_cycles
704
705 # Determine whether the weights need to be double buffered
706 weight_buffer_size = min(len(encoded_weights.buffer), encoded_weights.max_range_bytes)
707
708 # Only buffer weights if there's still space left for the buffer
709 if weight_buffer_size <= buffer_limit_bytes:
710 assert weight_buffer_size % 16 == 0
711 # Determine whether to double buffer or single buffer
712 if (weight_buffer_size * 2 <= buffer_limit_bytes) and (weight_buffer_size < len(encoded_weights.buffer)):
713 weight_buffer_size = weight_buffer_size * 2
714 weight_tensor_purpose = TensorSubPurpose.DoubleBuffer
715 else:
716 weight_tensor_purpose = TensorSubPurpose.Standard
717
Jacob Bohlineee9e5d2021-08-17 17:44:45 +0200718 cost.buffered_weight_tensor = self.buffer_tensor(
719 encoded_weights, weight_tensor_purpose, weight_buffer_size, weight_tensor.name
Tim Halld8339a72021-05-27 18:49:40 +0100720 )
Tim Halld8339a72021-05-27 18:49:40 +0100721 if ref_cost.cascade == 0:
722 # Determine if the lifetime can be extended and pre-buffer weights under the previous operation
723 cost.buffered_weight_tensor.pre_buffer = weight_buffer_size < slack_memory
724
725 cost.slack_buffering_memory -= weight_buffer_size
726 else:
727 # Don't slice or buffer - use the whole depth from persistent storage
728 cost.ofm_depth_slices = ofm_full_depth_slices
729 encoded_weights = full_weights
Tim Halld784af72021-06-08 21:25:57 +0100730 encoded_scales = full_scales
Tim Halld8339a72021-05-27 18:49:40 +0100731
732 cost.npu_weights_tensor = encoded_weights
Tim Halld784af72021-06-08 21:25:57 +0100733 cost.npu_scales_tensor = encoded_scales
Tim Halld8339a72021-05-27 18:49:40 +0100734
Jacob Bohlineee9e5d2021-08-17 17:44:45 +0200735 def buffer_tensor(self, src_tensor: Tensor, sub_purpose: TensorSubPurpose, buffer_size: int, name: str) -> Tensor:
736 buffered_weight_tensor = Tensor([1, 1, 1, buffer_size], DataType.uint8, name + "_buffer")
737 buffered_weight_tensor.src_tensor = src_tensor
738 buffered_weight_tensor.mem_area = self.arch.fast_storage_mem_area
739 buffered_weight_tensor.mem_type = MemType.Scratch_fast
740 buffered_weight_tensor.purpose = TensorPurpose.Weights
741 buffered_weight_tensor.sub_purpose = sub_purpose
742 return buffered_weight_tensor
743
Tim Halld8339a72021-05-27 18:49:40 +0100744 def propose_minimal_schedule(self) -> Schedule:
745 """Proposes scheduling parameters where every operator is subdivided into the smallest stripe that satisfies the
746 next operators stride"""
747 min_schedule = Schedule(self.sg, "MIN")
748 cost_map = min_schedule.cost_map
749
750 # Keep track of the previous Op - which consumes the current Op's OFM
751 prev_op = None
752 for sched_op in reversed(self.sched_ops):
753 min_stripe_height = prev_op.kernel.stride.y if prev_op else 1
754 min_stripe = sched_op.ofm.shape.with_height(min_stripe_height)
755
756 cost = sched_op.create_scheduler_info(self.nng, min_stripe)
757 cost.cycles = self.estimate_op_performance(sched_op, cost.block_config, sched_op.ofm.shape.depth)
758 cost_map[sched_op] = cost
759
760 prev_op = sched_op
761
762 return min_schedule
763
764 def propose_schedule_striping(self, final_stripe: Shape4D, label: str, ref_schedule: Schedule) -> Schedule:
765 """Proposes new striping for a schedule. The stripe is derived from the ifm requirements of the next Op down"""
766 ref_cost = ref_schedule.cost_map
767
768 striped_schedule = Schedule(self.sg, label)
769 stripe = final_stripe
770 for sched_op in reversed(self.sched_ops):
771 if sched_op not in ref_cost:
772 # sched_op is not part of the sub-schedule - skip
773 continue
774
775 # Create a cost entry with the new stripe
776 cost = sched_op.create_scheduler_info(self.nng, stripe)
777
Jacob Bohlineee9e5d2021-08-17 17:44:45 +0200778 if ref_cost[sched_op].buffered_weight_tensor:
779 # If the weights are buffered in the reference schedule they should be in the new proposal
780 weight_tensor = cost.npu_weights_tensor
781 cost.buffered_weight_tensor = self.buffer_tensor(
782 weight_tensor, TensorSubPurpose.Standard, len(weight_tensor.buffer), weight_tensor.name
783 )
Tim Halld8339a72021-05-27 18:49:40 +0100784
785 # Estimate performance
786 cost.cycles = self.estimate_op_performance(sched_op, cost.block_config, sched_op.ofm.shape.depth)
787 striped_schedule.cost_map[sched_op] = cost
788
789 # Calculate the preceeding Op's stripe
790 stripe = sched_op.ifm.shape.with_height(stripe.height * sched_op.kernel.stride.y)
791
792 return striped_schedule
793
794 def estimate_schedule_memory_usage(self, schedule: Schedule, non_local_mem_usage: dict):
795 """Estimates the memory usage of a schedule"""
796 cost = schedule.cost_map
797 cascades = schedule.cascades
798 peak_mem_usage = 0
799 for sched_op in self.sched_ops:
800 if sched_op not in cost:
801 # sched_op is not part of the sub-schedule - skip
802 continue
803
804 if cost[sched_op].cascade:
805 # This Op is part of a cascade - use the cascade's memory usage
806 cascade_info = cascades[cost[sched_op].cascade]
807 # Non-local memory usage is already included in the cascade_info
808 peak_mem_usage = max(cascade_info.mem_usage, peak_mem_usage)
809 else:
810 # This Op is not part of a cascade - calculate the memory usage
811 op_weight_buffer = 0
812 if cost[sched_op].buffered_weight_tensor:
813 op_weight_buffer = cost[sched_op].buffered_weight_tensor.storage_size()
814
815 op_mem_usage = (
816 sched_op.ifm_size_in_bytes()
817 + sched_op.ofm_size_in_bytes()
818 + op_weight_buffer
819 + non_local_mem_usage.get(sched_op, 0)
820 )
821 peak_mem_usage = max(op_mem_usage, peak_mem_usage)
822
823 return peak_mem_usage
824
825 def optimize_sub_schedule(
826 self, cascade_info: CascadeInfo, ref_schedule: Schedule, max_template: Schedule, memory_limit: int
827 ) -> Schedule:
828 """Extracts the Ops covered by the given cascade and creates a sub-schedule. The sub-schedule is optimized by
829 proposing weight buffering and then continously proposing new stripe sizes"""
830 ref_cost = ref_schedule.cost_map
831 # Extract the ops that are part of this sub-schedule
832 start = cascade_info.start
833 end = cascade_info.end
834 sub_schedule_ops = self.sched_ops[start : end + 1]
835 # Create a sub-schedule that contains only the costs for the Ops that are part of the sub-schedule
836 sub_schedule = Schedule(self.sg, f"SUB_{start}_{end}")
837 for sched_op in sub_schedule_ops:
838 sub_schedule.cost_map[sched_op] = ref_cost[sched_op]
839
840 sub_schedule.cascades[end] = cascade_info
841 # Use the memory snapshot from the reference schedule
842 sub_schedule.memory_snapshot = ref_schedule.memory_snapshot
843
844 # Calculate memory usage that is live during the sub-schedule but not part of it
845 time_for_cascade = ref_cost[sub_schedule_ops[0]].time_index
846 mem_usage_parallel_to_sub_schedule = ref_schedule.memory_snapshot[time_for_cascade] - cascade_info.mem_usage
847 # If the first Op's IFM has other consumers it has to live throughout the whole sub-schedule whether it's
848 # included in a cascade or not
849 persistent_initial_ifm = (
850 sub_schedule_ops[0].ifm_size_in_bytes() if len(sub_schedule_ops[0].ifm.connection.consumers) > 1 else 0
851 )
852 # Calculate non-local-mem-usage per Operator
853 non_local_mem_usage = {}
854 for idx, sched_op in enumerate(sub_schedule_ops):
855 non_local_mem_usage[sched_op] = mem_usage_parallel_to_sub_schedule
856 if idx != 0:
857 non_local_mem_usage[sched_op] += persistent_initial_ifm
858
859 cascade_builder = CascadeBuilder(sub_schedule_ops, self.arch.is_spilling_enabled(), non_local_mem_usage)
860
861 # Start by adding buffering
Tim Hall789e6f32021-06-17 17:02:31 +0100862 buffered_sub_schedule = self.propose_schedule_buffering(
863 sub_schedule, self.scheduler_options.optimization_sram_limit
864 )
Tim Halld8339a72021-05-27 18:49:40 +0100865 # Copy the cascades over from the unbuffered-schedule
866 buffered_sub_schedule.cascades = sub_schedule.cascades
867
868 # Generate the possible stripings for the final Op in the sub-schedule
869 final_ofm_shape = sub_schedule_ops[-1].ofm.shape
870 possible_stripes = [
871 final_ofm_shape.with_height(stripe_h) for stripe_h in range(1, final_ofm_shape.height // 2 + 1)
872 ]
873
874 # Propose different striping - the possible stripes are proposed similarly to a binary search
Jacob Bohlinfad72042021-08-24 21:51:41 +0200875 best_schedule = None
Tim Halld8339a72021-05-27 18:49:40 +0100876 iteration = 0
877 while len(possible_stripes) > 1:
878 proposed_stripe = possible_stripes[len(possible_stripes) // 2]
879 proposed_schedule = self.propose_schedule_striping(
880 proposed_stripe, f"OPTIMIZED_{iteration}", buffered_sub_schedule
881 )
882
883 cascade_builder.build_cascades(proposed_schedule, max_template, memory_limit)
884
885 # Check if proposal fits
886 proposed_schedule_mem_usage = self.estimate_schedule_memory_usage(proposed_schedule, non_local_mem_usage)
887 if (proposed_schedule_mem_usage) <= memory_limit:
888 # Remove all possible stripes smaller than this
889 possible_stripes = possible_stripes[len(possible_stripes) // 2 :]
890 best_schedule = proposed_schedule
891 if not proposed_schedule.cascades:
892 # No cascading required - early exit
893 break
894 else:
895 # Proposal doesn't fit within the limit - remove all possible stripes larger than this
896 possible_stripes = possible_stripes[: len(possible_stripes) // 2]
897
898 iteration += 1
899
900 return best_schedule
901
902 def optimize_schedule(
903 self, schedule: Schedule, max_sched: Schedule, max_template: Schedule, options: SchedulerOptions,
904 ) -> Schedule:
905 """Extracts sub-schedules based on the cascades and optimizes them and applies them to the final schedule"""
906 sram_limit = options.optimization_sram_limit
907 if max_sched.fast_storage_peak_usage < sram_limit and not self.arch.is_spilling_enabled():
908 # Maximum performance schedule fits within the SRAM target
909 return max_sched
910
Jacob Bohlinfad72042021-08-24 21:51:41 +0200911 # Iterate over a copy of the cascades since they may change during the loop
912 for cascade_info in list(schedule.cascades.values()):
Tim Halld8339a72021-05-27 18:49:40 +0100913 # Optimize the sub-schedule in this cascade
914 opt_sub_schedule = self.optimize_sub_schedule(cascade_info, schedule, max_template, sram_limit)
Jacob Bohlinfad72042021-08-24 21:51:41 +0200915 if opt_sub_schedule:
916 # Remove the existing cascade
917 del schedule.cascades[cascade_info.end]
918 # Update the sub-schedule Op and cascade costs to the full schedule
919 schedule.cost_map.update(opt_sub_schedule.cost_map)
920 schedule.cascades.update(opt_sub_schedule.cascades)
Tim Halld8339a72021-05-27 18:49:40 +0100921
922 # Update memory snapshot
923 self.sg.schedule = schedule
924 self.update_op_memory_snapshot(schedule)
925 # Propose schedule buffering to the optimized schedule
Tim Hall789e6f32021-06-17 17:02:31 +0100926 optimized_sched = self.propose_schedule_buffering(schedule, self.scheduler_options.optimization_sram_limit)
Tim Halld8339a72021-05-27 18:49:40 +0100927 # Copy the cascade's metadata from the unbuffered schedule
928 optimized_sched.cascades = schedule.cascades
929 return optimized_sched
930
931 def apply_schedule(self, sched: Schedule):
932 """Applies the given schedule as a final solution"""
933 for sched_op in self.sched_ops:
934 op_info = sched.cost_map[sched_op]
935 cascade_info = sched.cascades.get(op_info.cascade, None)
936 if cascade_info and sched_op in cascade_info.buffers:
937 buffer_tens = sched_op.ifm.connection.parent_tens
938 # Apply memory area and type
939 buffer_tens.mem_area = self.arch.fast_storage_mem_area
940 buffer_tens.mem_type = MemType.Scratch_fast
941 # Apply Rolling buffer
942 buffer_tens.set_format(TensorFormat.NHCWB16, self.arch)
943 buffer_tens.set_new_sub_purpose(TensorSubPurpose.RollingBufferY, cascade_info.buffers[sched_op].height)
944
945 sched_op.parent_ps.block_config = op_info.block_config.old_style_representation()
946
947 # Ensure that the src_tensor reference is set correctly
948 if op_info.buffered_weight_tensor:
949 op_info.buffered_weight_tensor.src_tensor = op_info.npu_weights_tensor
950
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100951 def use_fast_storage_for_feature_maps(self, schedule, staging_limit):
952 scratched_fms = {}
953 max_mem_usage = []
954 base_mem_usage = []
955 fast_storage_type = MemType.Scratch_fast
956 fast_storage_mem_area = self.arch.fast_storage_mem_area
Tim Halld8339a72021-05-27 18:49:40 +0100957
958 # Force all OFMs to fast-storage
959 for sched_op in self.sched_ops:
960 cost = schedule.cost_map[sched_op]
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100961 if cost.cascade == 0 and sched_op.get_dependants():
962 ofm_tens = sched_op.ofm.connection.parent_tens
963 if not any(cons is None for cons in ofm_tens.consumer_list):
964 if ofm_tens not in scratched_fms:
965 scratched_fms[ofm_tens] = (ofm_tens.mem_area, ofm_tens.mem_type)
966 ofm_tens.mem_area = fast_storage_mem_area
967 ofm_tens.mem_type = fast_storage_type
Tim Halld8339a72021-05-27 18:49:40 +0100968
969 # Collect live ranges from tensors
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100970 memories_list = [(fast_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))]
Tim Halld8339a72021-05-27 18:49:40 +0100971 lr_graph = live_range.LiveRangeGraph()
972 for mem_area, mem_type_set in memories_list:
973 live_range.extract_live_ranges_from_cascaded_passes(
Fredrik Svedberg0ae28482021-10-27 13:58:03 +0200974 self.nng.get_root_subgraph(), mem_area, mem_type_set, lr_graph, Tensor.AllocationQuantum,
Tim Halld8339a72021-05-27 18:49:40 +0100975 )
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100976 max_mem_usage = lr_graph.get_temporal_memory_usage(fast_storage_mem_area)
Tim Halld8339a72021-05-27 18:49:40 +0100977
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100978 # If true, everything fits and we can proceed
979 if max(max_mem_usage) <= staging_limit:
980 return
981
982 # Build up the base memory usage by removing the
983 # mem_usage of the lrs we previously moved to fast-storage
984 base_mem_usage = np.array(max_mem_usage)
985 curr_lrs = []
Tim Halld8339a72021-05-27 18:49:40 +0100986 for lr in lr_graph.lrs:
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100987 for tens in lr.tensors:
988 if scratched_fms.get(tens):
989 curr_lrs.append(lr)
990 base_mem_usage[lr.start_time : lr.end_time + 1] -= lr.size
991 break
992
993 competing_lrs = []
994 for lr in curr_lrs:
995 base_usage = max(base_mem_usage[lr.start_time : lr.end_time + 1])
996 # If true, the lr will never fit and may thus be evicted
997 if base_usage + lr.size > staging_limit:
998 FastStorageComponentAllocator.evict(lr, max_mem_usage, scratched_fms)
999 continue
1000 # Since max_mem_usage is the memory usage with all FMs still in fast-storage,
1001 # the memory limit cannot be exceeded if max_mem_usage does not.
1002 # Thus, the affected lrs can remain in fast-storage if the following is true
1003 if max(max_mem_usage[lr.start_time : lr.end_time + 1]) <= staging_limit:
1004 FastStorageComponentAllocator.keep(lr, base_mem_usage, staging_limit)
1005 else:
1006 competing_lrs.append(lr)
1007 sz = len(competing_lrs)
1008 # All lrs and their tensors have been handled if sz is zero, we may thus return
1009 if sz == 0:
1010 return
1011
1012 competing_lrs = sorted(competing_lrs, key=lambda lr: (lr.start_time, lr.end_time + 1, lr.size))
1013 start = 0
1014 start_time = competing_lrs[0].start_time
1015 end_time = competing_lrs[0].end_time
1016 component_allocator = FastStorageComponentAllocator(base_mem_usage, max_mem_usage, staging_limit)
1017 # Build up components and then allocate each separately
1018 for i, lr in enumerate(competing_lrs):
1019 if lr.start_time <= end_time and i - start < component_allocator.max_exhaustive_size:
1020 start_time = min(start_time, lr.start_time)
1021 end_time = max(end_time, lr.end_time)
1022 else:
1023 component_allocator.allocate_component(
1024 component_allocator,
1025 competing_lrs[start:i],
1026 max_mem_usage,
1027 base_mem_usage,
1028 staging_limit,
1029 scratched_fms,
1030 )
1031 start = i
1032 start_time = lr.start_time
1033 end_time = lr.end_time
1034 component_allocator.allocate_component(
1035 component_allocator, competing_lrs[start:sz], max_mem_usage, base_mem_usage, staging_limit, scratched_fms
1036 )
Tim Halld8339a72021-05-27 18:49:40 +01001037
1038 def move_constant_data(self):
1039 """Determine if data, can be moved from permanent storage to another memory area. A move
1040 will generate a DMA command in the high-level command stream"""
1041 for sched_op in self.sched_ops:
1042 parent_op = sched_op.parent_op
1043 is_lut_used = any(inp.purpose == TensorPurpose.LUT for inp in parent_op.inputs)
1044 max_ifm_shram_avail = (
1045 (self.arch.available_shram_banks(is_lut_used) - self.arch.shram_reserved_output_banks)
1046 * self.arch.shram_bank_size
1047 // 2
1048 )
1049
1050 for idx, tens in enumerate(parent_op.inputs):
1051 if tens.mem_type not in (MemType.Scratch, MemType.Scratch_fast):
1052 # Tensor is in permanent storage
1053 # Only when permanent storage differs from feature map storage, there is a point moving the data
1054 if (
1055 tens.mem_area in self.arch.permanent_storage_mem_area
1056 and self.arch.permanent_storage_mem_area != self.arch.feature_map_storage_mem_area
1057 ) or tens.purpose == TensorPurpose.LUT:
1058 if tens.purpose == TensorPurpose.LUT or (
Patrik Gustavsson94292fe2021-09-02 08:22:58 +02001059 # For elementwise broadcast
Tim Halld8339a72021-05-27 18:49:40 +01001060 tens.purpose == TensorPurpose.FeatureMap
1061 and sched_op.op_type.is_binary_elementwise_op()
1062 and tens.shape != []
1063 and sched_op.ifm.shape != sched_op.ofm.shape
Patrik Gustavsson94292fe2021-09-02 08:22:58 +02001064 and parent_op.write_shape is None
Tim Halld8339a72021-05-27 18:49:40 +01001065 and tens.storage_size() > max_ifm_shram_avail
1066 ):
1067 only_vector_product_consumers = all(
1068 oper and oper.type.npu_block_type == NpuBlockType.VectorProduct
1069 for oper in tens.consumers()
1070 )
1071
1072 if (not only_vector_product_consumers) or tens.purpose == TensorPurpose.LUT:
1073 new_tens = tens.clone_into_fast_storage(self.arch)
1074 if tens.purpose == TensorPurpose.LUT:
1075 new_tens.mem_area = MemArea.Shram
1076
1077 new_tens.consumer_list.append(parent_op)
1078 parent_op.inputs[idx] = new_tens
Dwight Lidman352607c2021-09-29 17:00:09 +02001079 # If the index is out of range, IFM and IFM2 are the same tensor
1080 # and pass inputs don't have duplicates
1081 if idx < len(sched_op.parent_ps.inputs):
1082 sched_op.parent_ps.inputs[idx] = new_tens
Tim Halld8339a72021-05-27 18:49:40 +01001083
1084 def print_schedule(self, schedule: Schedule):
1085 print(f"Schedule: '{schedule.name}'")
1086 for sched_op in self.sched_ops:
1087 if sched_op not in schedule.cost_map:
1088 # Sub-schedule printing
1089 continue
1090
1091 op_info = schedule.cost_map[sched_op]
1092 print(f"\t{sched_op.index}: Operation {sched_op.name} - OFM {sched_op.ofm.shape}")
1093 print(f"\t\tType: {sched_op.op_type}")
1094 print(f"\t\tKernel: {sched_op.kernel}")
1095 print(f"{op_info}")
1096 mem_usage = (
1097 schedule.memory_snapshot[op_info.time_index]
1098 if op_info.time_index < len(schedule.memory_snapshot)
1099 else 0
1100 )
1101 print(f"\t\tSRAM Used: {mem_usage} bytes")
1102
1103 print(f"\tCascades:")
1104 for i, cascade in enumerate(schedule.cascades.values()):
1105 print(f"\t\t{i}: {cascade.start} -> {cascade.end}, size: {cascade.mem_usage}")
Patrik Gustavssonfeeb06d2020-04-22 12:53:47 +02001106
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001107
Tim Halld8339a72021-05-27 18:49:40 +01001108def _update_tensor_allocation(nng: Graph, arch: ArchitectureFeatures, options):
1109 """
1110 Creates live ranges and runs tensor allocator for the current schedule
1111 (i.e. sg.schedule for all subgraphs), returns the maximum memory usage
1112 and updates SchedulerOpInfo.mem_usage for all operations in the schedule.
1113 """
1114 root_sg = nng.get_root_subgraph()
1115
1116 alloc_list = []
1117 if arch.is_spilling_enabled():
1118 mem_alloc_scratch_fast = (arch.fast_storage_mem_area, set((MemType.Scratch_fast,)))
1119 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch,)))
1120 # Order is important
1121 alloc_list.append(mem_alloc_scratch_fast)
1122 alloc_list.append(mem_alloc_scratch)
1123 else:
1124 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))
1125 alloc_list.append(mem_alloc_scratch)
1126
1127 for mem_area, mem_type_set in alloc_list:
1128 tensor_allocation.allocate_tensors(
1129 nng,
1130 root_sg,
1131 arch,
1132 mem_area,
1133 mem_type_set,
1134 tensor_allocator=options.tensor_allocator,
1135 verbose_allocation=options.verbose_allocation,
1136 cpu_tensor_alignment=options.cpu_tensor_alignment,
1137 )
1138
1139
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +01001140class FastStorageComponentAllocator:
1141 def __init__(self, base_mem_usage, max_mem_usage, staging_limit):
1142 self.base_mem_usage = base_mem_usage
1143 self.max_mem_usage = list(max_mem_usage)
1144 self.staging_limit = staging_limit
1145 self.lrs = []
1146 self.evicted = []
1147 self.curr_evicted = []
1148 self.remaining_total_size = []
1149 self.best_allocated_size = 0
1150 self.max_exhaustive_size = 20
1151
1152 def allocate_exhaustive(self, ix, alloc_size):
1153 if ix >= len(self.lrs):
1154 if alloc_size > self.best_allocated_size:
1155 self.best_allocated_size = alloc_size
1156 self.evicted = self.curr_evicted
1157 return
1158
1159 lr = self.lrs[ix]
1160 for t in range(lr.start_time, lr.end_time):
1161 assert self.base_mem_usage[t] <= self.max_mem_usage[t]
1162 base_usage = max(self.base_mem_usage[lr.start_time : lr.end_time + 1])
1163 can_fit = base_usage + lr.size <= self.staging_limit
1164 always_fits = can_fit
1165
1166 if can_fit:
1167 max_usage = max(self.max_mem_usage[lr.start_time : lr.end_time + 1])
1168 always_fits = max_usage <= self.staging_limit
1169
1170 if can_fit or always_fits:
1171 self.curr_evicted[ix] = False
1172 self.base_mem_usage = self.update_mem_usage(self.base_mem_usage, lr, True)
1173 self.allocate_exhaustive(ix + 1, alloc_size + lr.size)
1174 self.base_mem_usage = self.update_mem_usage(self.base_mem_usage, lr, False)
1175
1176 if not always_fits:
1177 self.curr_evicted[ix] = True
1178 self.max_mem_usage = self.update_mem_usage(self.max_mem_usage, lr, False)
1179 self.allocate_exhaustive(ix + 1, alloc_size)
1180 self.max_mem_usage = self.update_mem_usage(self.max_mem_usage, lr, True)
1181
1182 @staticmethod
1183 def update_mem_usage(mem_usage, lr, increase):
1184 for t in range(lr.start_time, lr.end_time + 1):
1185 mem_usage[t] += lr.size if increase else -lr.size
1186 assert mem_usage[t] >= 0
1187 return mem_usage
1188
1189 @staticmethod
1190 def evict(lr, max_mem_usage, scratched_fms):
1191 for t in range(lr.start_time, lr.end_time + 1):
1192 max_mem_usage[t] -= lr.size
1193 for tens in lr.tensors:
1194 if tens in scratched_fms:
1195 tens.mem_area = scratched_fms[tens][0]
1196 tens.mem_type = scratched_fms[tens][1]
1197
1198 @staticmethod
1199 def keep(lr, base_mem_usage, staging_limit):
1200 for t in range(lr.start_time, lr.end_time + 1):
1201 base_mem_usage[t] += lr.size
1202 assert base_mem_usage[t] <= staging_limit
1203
1204 def allocate_component(self, allocator, lrs, max_mem, min_mem, staging_limit, scratched_fms):
1205 sz = len(lrs)
1206 allocator.lrs = lrs
1207 allocator.evicted = [0] * len(lrs)
1208 allocator.curr_evicted = [0] * sz
1209 allocator.best_allocated_size = -1
1210 # Recursively evaluate all permutations of allocations of the lrs found in the component
1211 allocator.allocate_exhaustive(0, 0)
1212
1213 # Optimal allocation has been found, move lrs accordingly
1214 for i, e in enumerate(allocator.evicted):
1215 if e:
1216 self.evict(lrs[i], max_mem, scratched_fms)
1217 else:
1218 self.keep(lrs[i], min_mem, staging_limit)
1219
1220
Tim Halld8339a72021-05-27 18:49:40 +01001221def schedule_passes(nng: Graph, arch: ArchitectureFeatures, options, scheduler_options: SchedulerOptions):
1222 """Entry point for the Scheduler"""
1223 # Initialize CPU subgraphs
1224 schedulers = dict()
1225 # Initialize schedulers with max schedule. Only schedule NPU subgraphs
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001226 for sg in nng.subgraphs:
Tim Halld8339a72021-05-27 18:49:40 +01001227 if sg.placement != PassPlacement.Npu:
1228 # Create cascaded passes for CPU Ops
1229 cascaded_passes = []
1230 for idx, ps in enumerate(sg.passes):
1231 cps = CascadedPass(
1232 ps.name, SchedulingStrategy.WeightStream, ps.inputs, [], ps.outputs, [ps], ps.placement, False,
1233 )
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001234
Tim Halld8339a72021-05-27 18:49:40 +01001235 cps.time = idx
1236 ps.cascade = cps
1237 cascaded_passes.append(cps)
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001238
Tim Halld8339a72021-05-27 18:49:40 +01001239 sg.cascaded_passes = cascaded_passes
1240 else:
1241 # Npu subgraph - create schedule
1242 scheduler = Scheduler(nng, sg, arch, scheduler_options)
1243 schedulers[sg] = scheduler
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001244
Tim Halld8339a72021-05-27 18:49:40 +01001245 scheduler.create_scheduler_representation(arch)
1246 sg.sched_ops = scheduler.sched_ops
1247 scheduler.move_constant_data()
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001248
Tim Halld8339a72021-05-27 18:49:40 +01001249 # Create the Max schedule template
1250 max_schedule_template = scheduler.create_initial_schedule()
1251 scheduler.max_schedule = max_schedule_template
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001252
Tim Halld8339a72021-05-27 18:49:40 +01001253 # Create the optimimised Max schedule
1254 sg.schedule = max_schedule_template
1255 scheduler.update_op_memory_snapshot(max_schedule_template)
Tim Hall789e6f32021-06-17 17:02:31 +01001256 opt_max_schedule = scheduler.propose_schedule_buffering(max_schedule_template, 1 << 32)
Tim Halld8339a72021-05-27 18:49:40 +01001257 sg.schedule = opt_max_schedule
1258 scheduler.update_op_memory_snapshot(opt_max_schedule)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001259
Tim Halld8339a72021-05-27 18:49:40 +01001260 # Create Min schedule
1261 min_schedule = scheduler.propose_minimal_schedule()
1262 initial_sram_limit = scheduler_options.optimization_sram_limit
1263 if scheduler_options.optimization_strategy == OptimizationStrategy.Size:
1264 initial_sram_limit = scheduler.min_memory_req
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001265
Tim Halld8339a72021-05-27 18:49:40 +01001266 cascade_builder = CascadeBuilder(scheduler.sched_ops, arch.is_spilling_enabled())
1267 cascade_builder.build_cascades(min_schedule, max_schedule_template, initial_sram_limit)
1268 sg.schedule = min_schedule
1269 scheduler.update_op_memory_snapshot(min_schedule)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001270
Tim Halld8339a72021-05-27 18:49:40 +01001271 if scheduler_options.optimization_strategy == OptimizationStrategy.Performance:
1272 # Create an optimized schedule
1273 sg.schedule = scheduler.optimize_schedule(
1274 min_schedule, opt_max_schedule, max_schedule_template, scheduler_options
1275 )
1276 scheduler.update_op_memory_snapshot(sg.schedule)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001277
Tim Halld8339a72021-05-27 18:49:40 +01001278 scheduler.apply_schedule(sg.schedule)
1279 scheduler.use_fast_storage_for_feature_maps(sg.schedule, scheduler_options.optimization_sram_limit)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001280
Tim Halld8339a72021-05-27 18:49:40 +01001281 if scheduler_options.verbose_schedule:
1282 scheduler.print_schedule(sg.schedule)
Tim Hall79d07d22020-04-27 18:20:16 +01001283
Tim Halld8339a72021-05-27 18:49:40 +01001284 # Evaluate schedule
1285 _update_tensor_allocation(nng, arch, options)