blob: e73a26dc6643fe02b71f03d464c25fbe112f2eae [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
Jonas Ohlsson845e2322022-03-01 12:39:55 +010020# For Class name forward references for the type annotations. (see PEP 563).
21from __future__ import annotations
22
Diego Russoea6111a2020-04-14 18:41:58 +010023import copy
Johan Alfvén5e0ae552022-02-09 21:20:10 +010024from collections import namedtuple
Tim Halld8339a72021-05-27 18:49:40 +010025from enum import auto
26from enum import IntEnum
27from typing import Dict
28from typing import List
29from typing import Optional
30from typing import Tuple
Jonas Ohlsson845e2322022-03-01 12:39:55 +010031from typing import TYPE_CHECKING
32
33# Import needed for Type annotations. Only import for Type checking to avoid run-time errors due to cyclic import.
34if TYPE_CHECKING:
35 from .npu_performance import CycleCost
Diego Russoea6111a2020-04-14 18:41:58 +010036
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +010037import numpy as np
38
Diego Russoea6111a2020-04-14 18:41:58 +010039from . import live_range
Tim Hall79d07d22020-04-27 18:20:16 +010040from . import npu_performance
Tim Halld8339a72021-05-27 18:49:40 +010041from . import tensor_allocation
42from . import weight_compressor
43from .architecture_allocator import ArchitectureBlockConfig
44from .architecture_allocator import find_block_config
45from .architecture_allocator import get_ifm_area_required
Tim Halld8339a72021-05-27 18:49:40 +010046from .architecture_features import ArchitectureFeatures
47from .architecture_features import Block
48from .cascade_builder import CascadeBuilder
49from .cascade_builder import CascadeInfo
Fredrik Svedberg880e7352020-08-25 11:31:47 +020050from .data_type import DataType
Diego Russoe8a10452020-04-21 17:39:10 +010051from .nn_graph import CascadedPass
Tim Halld8339a72021-05-27 18:49:40 +010052from .nn_graph import Graph
53from .nn_graph import Pass
Diego Russoe8a10452020-04-21 17:39:10 +010054from .nn_graph import PassPlacement
Diego Russoe8a10452020-04-21 17:39:10 +010055from .nn_graph import SchedulingStrategy
Tim Halld8339a72021-05-27 18:49:40 +010056from .nn_graph import Subgraph
57from .numeric_util import round_down
58from .numeric_util import round_up
Diego Russoe8a10452020-04-21 17:39:10 +010059from .operation import NpuBlockType
Louis Verhaardaee5d752020-09-30 09:01:52 +020060from .operation import Op
Tim Halld8339a72021-05-27 18:49:40 +010061from .shape4d import Shape4D
Diego Russoe8a10452020-04-21 17:39:10 +010062from .tensor import MemArea
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020063from .tensor import MemType
Tim Halld8339a72021-05-27 18:49:40 +010064from .tensor import Tensor
Diego Russoe8a10452020-04-21 17:39:10 +010065from .tensor import TensorFormat
66from .tensor import TensorPurpose
67from .tensor import TensorSubPurpose
Jonas Ohlsson845e2322022-03-01 12:39:55 +010068from .weight_compressor import NpuWeightTensor
Jacob Bohlin1a666972020-09-11 10:04:15 +020069
Tim Hall79d07d22020-04-27 18:20:16 +010070
Tim Halld8339a72021-05-27 18:49:40 +010071def shape_for_format(shape: Shape4D, tensor_format: TensorFormat) -> Shape4D:
72 if tensor_format == TensorFormat.NHCWB16:
73 return shape.with_depth(round_up(shape.depth, 16))
74
75 return shape
76
77
78class OptimizationStrategy(IntEnum):
79 """Enum defining the different optimization strategies for the Scheduler"""
80
81 Size = auto()
82 Performance = auto()
Tim Hall79d07d22020-04-27 18:20:16 +010083
84 def __str__(self):
85 return self.name
86
87
Tim Halld8339a72021-05-27 18:49:40 +010088class SchedulerOpInfo:
89 """Contains metadata about a SchedulerOperation that is unique to one Schedule"""
90
Tim Hall79d07d22020-04-27 18:20:16 +010091 def __init__(
92 self,
Tim Halld8339a72021-05-27 18:49:40 +010093 block_config: ArchitectureBlockConfig,
94 weights_size: int,
95 stripe_input: Shape4D,
96 stripe_input2: Optional[Shape4D],
97 stripe: Shape4D,
Tim Hall79d07d22020-04-27 18:20:16 +010098 ):
Tim Halld8339a72021-05-27 18:49:40 +010099 self.block_config = block_config
100 self.weights_size = weights_size
101 self.stripe_input = stripe_input
102 self.stripe_input2 = stripe_input2
103 self.stripe = stripe
104 self.cascade = 0 # Assigned by CascadeBuilder. 0 means not part of a cascade
105 self.time_index = None # Set by update_op_memory_snapshot
106 self.ofm_depth_slices: List[int] = [0, stripe.depth]
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100107 self.npu_weights_tensor: Optional[NpuWeightTensor] = None
108 self.npu_scales_tensor: Optional[NpuWeightTensor] = None
Tim Hallb5df7732022-05-04 16:20:43 +0100109 self.buffered_weight_tensor: Optional[Tensor] = None
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100110 self.cycles: Optional[CycleCost] = None
Tim Halld8339a72021-05-27 18:49:40 +0100111 self.slack_buffering_cycles = 0
112 self.slack_buffering_memory = 0
113 self.full_weight_transfer_cycles = 0
114
115 def copy(self):
Jonas Ohlssond8575072022-03-30 10:30:25 +0200116 res = SchedulerOpInfo(
117 self.block_config,
118 self.weights_size,
119 self.stripe_input,
120 self.stripe_input2,
121 self.stripe,
122 )
Tim Halld8339a72021-05-27 18:49:40 +0100123 res.cascade = self.cascade
124 return res
125
126 def __str__(self):
127 res = f"\t\tBlock Config = {self.block_config}\n"
128 res += f"\t\tOFM Block = {self.block_config.ofm_block}\n"
129 res += f"\t\tIFM Stripe = {self.stripe_input}\n"
130 res += f"\t\tIFM2 Stripe = {self.stripe_input2}\n"
131 res += f"\t\tOFM Stripe = {self.stripe}\n"
132 res += f"\t\tEncoded Weights = {self.npu_weights_tensor and len(self.npu_weights_tensor.buffer)} bytes\n"
Tim Hallb5df7732022-05-04 16:20:43 +0100133 res += (
134 f"\t\tWeight buffer = {self.buffered_weight_tensor and self.buffered_weight_tensor.storage_size()} bytes\n"
135 )
Tim Halld8339a72021-05-27 18:49:40 +0100136 res += f"\t\tDepth slices = {self.ofm_depth_slices}\n"
137 res += f"\t\tAssigned Cascade = {self.cascade}"
138 return res
139
140
141class SchedulerOptions:
142 """Contains options for the Scheduler"""
143
144 def __init__(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200145 self,
146 optimization_strategy,
147 sram_target,
148 verbose_schedule,
Tim Halld8339a72021-05-27 18:49:40 +0100149 ):
150 self.optimization_strategy = optimization_strategy
151 self.optimization_sram_limit = sram_target
Tim Hall79d07d22020-04-27 18:20:16 +0100152 self.verbose_schedule = verbose_schedule
Tim Hall79d07d22020-04-27 18:20:16 +0100153
Tim Halld8339a72021-05-27 18:49:40 +0100154 def __str__(self) -> str:
155 return f"{type(self).__name__}: {str(self.__dict__)}"
Tim Hall79d07d22020-04-27 18:20:16 +0100156
157 __repr__ = __str__
158
159
Tim Halld8339a72021-05-27 18:49:40 +0100160class SchedulerTensor:
161 def __init__(self, shape, dt, mem_area, _format):
162 self.dtype = dt
163 self.mem_area = mem_area
164 self.shape = shape
165 self.format = _format
166 self.connection = None
Tim Hall79d07d22020-04-27 18:20:16 +0100167
Tim Halld8339a72021-05-27 18:49:40 +0100168
169class SchedulerOperation:
170 """Scheduler internal representation of 'Operation'
171 This class can be seen as a node within the Scheduler Graph representation
172 """
173
174 def __init__(self, ps: Pass, arch: ArchitectureFeatures, nng: Graph):
175 self.arch = arch
176 self.parent_ps = ps
177 self.parent_op = ps.primary_op
178 self.name = ps.primary_op.name
179 self.op_type = ps.primary_op.type
180 self.activation = ps.primary_op.activation
181 self.kernel = ps.primary_op.kernel
Tim Hall3c5cfe92022-03-16 16:31:57 +0000182 self.resampling_mode = ps.primary_op.ifm_resampling_mode
Tim Halld8339a72021-05-27 18:49:40 +0100183 self.uses_scalar = ps.primary_op.ifm2 is not None and (
184 ps.primary_op.ifm.shape == [] or ps.primary_op.ifm2.shape == []
Tim Hall79d07d22020-04-27 18:20:16 +0100185 )
Tim Halld8339a72021-05-27 18:49:40 +0100186 self.ifm_ublock = arch.ifm_ublock
Tim Hall79d07d22020-04-27 18:20:16 +0100187
Jonas Ohlssond8575072022-03-30 10:30:25 +0200188 self.ifm = SchedulerTensor(
189 ps.ifm_shapes[0],
190 ps.ifm_tensor.dtype,
191 ps.ifm_tensor.mem_area,
192 ps.ifm_tensor.format,
193 )
Tim Hall79d07d22020-04-27 18:20:16 +0100194
Tim Halld8339a72021-05-27 18:49:40 +0100195 self.ifm2 = None
196 if ps.ifm2_tensor:
197 self.ifm2 = SchedulerTensor(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200198 ps.ifm_shapes[1],
199 ps.ifm2_tensor.dtype,
200 ps.ifm2_tensor.mem_area,
201 ps.ifm2_tensor.format,
Tim Halld8339a72021-05-27 18:49:40 +0100202 )
Tim Hall79d07d22020-04-27 18:20:16 +0100203
Jonas Ohlssond8575072022-03-30 10:30:25 +0200204 self.ofm = SchedulerTensor(
205 ps.ofm_shapes[0],
206 ps.ofm_tensor.dtype,
207 ps.ofm_tensor.mem_area,
208 ps.ofm_tensor.format,
209 )
Tim Hall79d07d22020-04-27 18:20:16 +0100210
Tim Halld8339a72021-05-27 18:49:40 +0100211 # Input volume width and height required to produce the smallest possible stripe
212 self.min_stripe_input_w, self.min_stripe_input_h = self._calculate_min_stripe_input()
Tim Hall79d07d22020-04-27 18:20:16 +0100213
Tim Halld8339a72021-05-27 18:49:40 +0100214 # Flags that marks whether this SchedulerOperation requires full IFM/OFM
215 self.requires_full_ifm = False
216 self.requires_full_ifm2 = False
217 self.requires_full_ofm = False
Tim Hall79d07d22020-04-27 18:20:16 +0100218
Tim Halld8339a72021-05-27 18:49:40 +0100219 self.index = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100220
Tim Halld8339a72021-05-27 18:49:40 +0100221 def add_ifm_connection(self, conn: "Connection"):
222 """Add input connection to another SchedulerOperation or Subgraph Input"""
223 conn.consumers.append(self)
224 self.ifm.connection = conn
Tim Hall79d07d22020-04-27 18:20:16 +0100225
Tim Halld8339a72021-05-27 18:49:40 +0100226 def add_ifm2_connection(self, conn: "Connection"):
227 """Add input connection to another SchedulerOperation or Subgraph Input"""
228 if self.ifm2:
229 conn.consumers.append(self)
230 self.ifm2.connection = conn
Tim Hall79d07d22020-04-27 18:20:16 +0100231 else:
Tim Halld8339a72021-05-27 18:49:40 +0100232 assert False, f"Trying to set an IFM2 Connection to {self} which has no IFM2"
Tim Hall79d07d22020-04-27 18:20:16 +0100233
Tim Halld8339a72021-05-27 18:49:40 +0100234 def add_ofm_connection(self, conn: "Connection"):
235 """Add output connection to another SchedulerOperation or Subgraph Output"""
236 conn.producers.append(self)
237 self.ofm.connection = conn
238
239 def get_dependants(self):
240 """Returns a list of the Ops that depend on this Operation's OFM"""
241 return self.ofm.connection.consumers
242
243 def ifm_size_in_bytes(self) -> int:
244 """Returns size of the IFM in bytes"""
245 ifm_storage_shape = shape_for_format(self.ifm.shape, self.ifm.format)
246 return round_up(ifm_storage_shape.elements() * self.ifm.dtype.size_in_bytes(), Tensor.AllocationQuantum)
247
248 def ifm2_size_in_bytes(self) -> int:
249 """Returns size of the IFM2 in bytes"""
250 if self.ifm2:
251 ifm2_storage_shape = shape_for_format(self.ifm2.shape, self.ifm2.format)
252 return round_up(ifm2_storage_shape.elements() * self.ifm2.dtype.size_in_bytes(), Tensor.AllocationQuantum)
253
254 return 0
255
256 def ofm_size_in_bytes(self) -> int:
257 """Returns size of the OFM in bytes"""
258 ofm_storage_shape = shape_for_format(self.ofm.shape, self.ofm.format)
259 return round_up(ofm_storage_shape.elements() * self.ofm.dtype.size_in_bytes(), Tensor.AllocationQuantum)
260
261 def create_scheduler_info(self, nng: Graph, stripe: Shape4D) -> SchedulerOpInfo:
262 """Returns schedule info about this SchedulerOperation based on how many ofm elements it should produce"""
263 ifm_shape = self.ifm.shape
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100264 ifm2_shape = self.ifm2.shape if self.ifm2 is not None else None
Tim Halld8339a72021-05-27 18:49:40 +0100265 ofm_shape = stripe
266
267 if ofm_shape != self.ofm.shape:
268 # Striped Op - Need to calculate stripe input volume
269 stripe_input_w, stripe_input_h = self._get_stripe_input_requirement(stripe)
270 # Ensure stripe input volume is within the full IFM volume
271 stripe_input_h = min(stripe_input_h, self.ifm.shape.height)
272 stripe_input_w = min(stripe_input_w, self.ifm.shape.width)
273 ifm_shape = ifm_shape.with_hw(stripe_input_h, stripe_input_w)
274
275 if self.ifm2:
276 stripe_input2_h = min(stripe_input_h, self.ifm2.shape.height)
277 stripe_input2_w = min(stripe_input_w, self.ifm2.shape.width)
278 ifm2_shape = ifm2_shape.with_hw(stripe_input2_h, stripe_input2_w)
279
280 block_config = self._get_block_config(ifm_shape, ifm2_shape, self.uses_scalar, ofm_shape)
281
282 scheduler_op_info = SchedulerOpInfo(block_config, 0, ifm_shape, ifm2_shape, ofm_shape)
283 if self.parent_op.weights:
284 # Default full-depth weight encoding with no buffering
Tim Halld784af72021-06-08 21:25:57 +0100285 (
286 scheduler_op_info.npu_weights_tensor,
287 scheduler_op_info.npu_scales_tensor,
288 ) = weight_compressor.encode_weight_and_scale_tensor(
Tim Halld8339a72021-05-27 18:49:40 +0100289 self.arch,
290 self.parent_op,
291 self.parent_op.weights,
292 self.parent_op.bias,
293 self.kernel,
294 block_config,
295 [0, self.ofm.shape.depth],
296 )
297
298 self.parent_ps.block_config = block_config.old_style_representation()
299 return scheduler_op_info
300
301 def _get_stripe_input_requirement(self, stripe_shape: Shape4D) -> Tuple[int, int]:
302 """Returns the amount of IFM required to produce the stripe with shape:'stripe_shape'"""
303 ofm_shape_to_produce = Block.from_shape(stripe_shape.as_list())
304
Fredrik Svedberg3ff7a4a2021-09-29 10:08:04 +0200305 return get_ifm_area_required(ofm_shape_to_produce, self.kernel, self.resampling_mode)
Tim Halld8339a72021-05-27 18:49:40 +0100306
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100307 def _calculate_min_stripe_input(self) -> Tuple[int, int]:
Tim Halld8339a72021-05-27 18:49:40 +0100308 # Calculate the input volume required height and width for the smallest possible stripe (h,w = 1,1)
309 min_stripe = self.ofm.shape.with_hw(1, 1)
310 return self._get_stripe_input_requirement(min_stripe)
311
312 def _get_block_config(
313 self, ifm_shape: Shape4D, ifm2_shape: Optional[Shape4D], uses_scalar: bool, ofm_shape: Shape4D
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100314 ) -> Optional[ArchitectureBlockConfig]:
Tim Halld8339a72021-05-27 18:49:40 +0100315 # Returns a block config and SHRAM layout
316 lut_banks = 2 if self.parent_op.activation_lut else 0
317 return find_block_config(
318 self.arch,
319 self.op_type.npu_block_type,
320 ofm_shape,
321 ifm_shape,
322 ifm2_shape,
323 uses_scalar,
324 self.ifm.dtype.size_in_bits(),
325 self.kernel,
326 lut_banks,
327 self.parent_op.has_scaling(),
328 self.resampling_mode,
329 )
330
331
332class Connection:
333 """Scheduler internal representation of a Tensor that connects two SchedulerOperations
334 This class can be seen as an edge within the Scheduler Graph representation
335 """
336
337 def __init__(self, tensor: Tensor):
338 self.parent_tens = tensor
339
340 # SchedulerOperation relationships
341 self.producers: List[SchedulerOperation] = []
342 self.consumers: List[SchedulerOperation] = []
Tim Hall79d07d22020-04-27 18:20:16 +0100343
344 def __str__(self):
Tim Halld8339a72021-05-27 18:49:40 +0100345 return f"<Connection {self.parent_tens.name}>"
Tim Hall79d07d22020-04-27 18:20:16 +0100346
347 __repr__ = __str__
348
349
Tim Halld8339a72021-05-27 18:49:40 +0100350class Schedule:
351 """Class that contains a solution of how to schedule an NPU subgraph and its cost"""
Tim Hall79d07d22020-04-27 18:20:16 +0100352
Tim Halld8339a72021-05-27 18:49:40 +0100353 def __init__(self, sg: Subgraph, label: str):
354 self.sg = sg
355 self.label = label
356 self.cost_map: Dict[SchedulerOperation, SchedulerOpInfo] = {}
357 self.cascades: Dict[int, CascadeInfo] = {}
358 self.fast_storage_peak_usage = 0
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100359 self.memory_snapshot: Optional[List[int]] = None
Tim Halld8339a72021-05-27 18:49:40 +0100360
361 @property
362 def name(self):
363 return f"{self.sg.name}_{self.label}"
Tim Hall79d07d22020-04-27 18:20:16 +0100364
365
Tim Halld8339a72021-05-27 18:49:40 +0100366class Scheduler:
367 """Main class of the Vela Scheduling"""
Tim Hall79d07d22020-04-27 18:20:16 +0100368
Tim Halld8339a72021-05-27 18:49:40 +0100369 def __init__(self, nng: Graph, sg: Subgraph, arch: ArchitectureFeatures, options: SchedulerOptions):
Tim Hall79d07d22020-04-27 18:20:16 +0100370 self.nng = nng
371 self.sg = sg
372 self.arch = arch
Ayaan Masoodb801dda2022-02-22 11:28:55 +0000373 self.sched_ops: List[SchedulerOperation] = []
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100374 self.max_schedule: Optional[Schedule] = None
Tim Halld8339a72021-05-27 18:49:40 +0100375 self.scheduler_options = options
Tim Hall79d07d22020-04-27 18:20:16 +0100376
Johan Alfvén5e0ae552022-02-09 21:20:10 +0100377 def avoid_nhcwb16_for_ofm(self, tens, ps, arch):
378 # Only run this check for opt strategy Size
379 if self.scheduler_options.optimization_strategy == OptimizationStrategy.Performance:
380 return False
381
382 op = ps.primary_op
383 if not op.type.is_elementwise_op():
384 return False
385
386 depth = op.ofm_shapes[0][-1]
387 if (depth % 16) == 0:
388 return False
389
390 # Check if overwriting the inputs can be allowed
391 OpShapeTens = namedtuple("OpShapeTens", ["op_shape", "tens"])
392 outp = OpShapeTens(op.ofm_shapes[0], op.ofm)
393 inps = []
394 if op.ifm is not None:
395 inps.append(OpShapeTens(op.ifm_shapes[0], op.ifm))
396 if op.ifm2 is not None:
397 inps.append(OpShapeTens(op.ifm_shapes[1], op.ifm2))
398
399 # Find an input tensor that can be overwritten by the output
400 for inp in inps:
401 if (
402 # check op input and output shapes allow overlapping
403 inp.op_shape == outp.op_shape
404 # check input tensor is valid
405 and inp.tens is not None
406 and inp.tens.shape != []
407 # check input and output tensors are compatible
408 and inp.tens.format == outp.tens.format
409 and inp.tens.dtype == outp.tens.dtype
410 ):
411 if inp.tens.format == TensorFormat.NHWC:
412 return True
413
414 return False
415
Tim Halld8339a72021-05-27 18:49:40 +0100416 def create_scheduler_representation(self, arch: ArchitectureFeatures):
417 """Creates a Scheduler Graph representation"""
418 # Temporary dict for creating connections between the Operations
419 connections: Dict[Tensor, Connection] = {}
420 # Memory required for the largest FeatureMap that has to be full
421 min_memory_req = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100422 for ps in self.sg.passes:
Tim Halld8339a72021-05-27 18:49:40 +0100423 if ps.primary_op:
424 # Set tensor format to NHCWB16 for output FeatureMaps, if possible
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200425 for output in ps.outputs:
Jacob Bohlina5e8c1c2021-06-14 13:33:39 +0200426 if output in self.sg.output_tensors or output.purpose != TensorPurpose.FeatureMap:
Patrik Gustavssonfeeb06d2020-04-22 12:53:47 +0200427 continue
Johan Alfvén5e0ae552022-02-09 21:20:10 +0100428
429 if output.needs_linear_format:
430 continue
431
432 if self.avoid_nhcwb16_for_ofm(output, ps, arch):
433 output.needs_linear_format = True
434 continue
435
436 output.set_format(TensorFormat.NHCWB16, arch)
Tim Halld8339a72021-05-27 18:49:40 +0100437
438 # Create SchedulerOperations
439 op = SchedulerOperation(ps, arch, self.nng)
440 op.index = len(self.sched_ops)
441
442 # Make connections
443 if ps.ifm_tensor not in connections:
444 connections[ps.ifm_tensor] = Connection(ps.ifm_tensor)
445 if ps.ifm2_tensor and ps.ifm2_tensor not in connections:
446 connections[ps.ifm2_tensor] = Connection(ps.ifm2_tensor)
447 if ps.ofm_tensor not in connections:
448 connections[ps.ofm_tensor] = Connection(ps.ofm_tensor)
449
450 op.add_ifm_connection(connections[ps.ifm_tensor])
451 if ps.ifm2_tensor:
452 op.add_ifm2_connection(connections[ps.ifm2_tensor])
453 op.add_ofm_connection(connections[ps.ofm_tensor])
454
455 # Set requirements on the ifm/ofm buffers
456 self.sched_ops.append(op)
457 if ps.ifm_tensor in self.sg.input_tensors:
458 # This Op consumes a subgraph input
459 op.requires_full_ifm = True
460 if ps.ifm2_tensor and ps.ifm2_tensor in self.sg.input_tensors:
461 # This Op consumes a subgraph input
462 op.requires_full_ifm2 = True
463 if ps.ofm_tensor in self.sg.output_tensors:
464 # This Op produces a subgraph output
465 op.requires_full_ofm = True
466 if ps.ifm_tensor.needs_linear_format:
467 op.requires_full_ifm = True
468 if ps.ifm2_tensor and ps.ifm2_tensor.needs_linear_format:
469 op.requires_full_ifm2 = True
470 if ps.ofm_tensor.needs_linear_format or ps.primary_op.memory_function == Op.ConcatSliceWrite:
471 op.requires_full_ofm = True
472 if len(ps.primary_op.outputs) > 1 or len(ps.primary_op.outputs[0].consumer_list) > 1:
473 # Op has multiple outputs or consumers - requires full OFM
474 op.requires_full_ofm = True
475
476 # Check memory requirements if this Op requires any full FeatureMaps
477 op_memory_req = 0
478 if op.requires_full_ifm:
479 op_memory_req += op.ifm_size_in_bytes()
480 if op.requires_full_ifm2:
481 op_memory_req += op.ifm2_size_in_bytes()
482 if op.requires_full_ofm:
483 op_memory_req += op.ofm_size_in_bytes()
484
485 min_memory_req = max(op_memory_req, min_memory_req)
486
487 # Theoretical minimum required memory - used to guide the cascade building
488 self.min_memory_req = min_memory_req
489
490 def create_initial_schedule(self) -> Schedule:
491 """Creates an initial schedule with no cascading or buffering of any kind"""
492 schedule = Schedule(self.sg, "MAX")
Tim Halld8339a72021-05-27 18:49:40 +0100493 for op in self.sched_ops:
494 cost = op.create_scheduler_info(self.nng, op.ofm.shape)
495 cost.cycles = self.estimate_op_performance(op, cost.block_config, op.ofm.shape.depth)
496 schedule.cost_map[op] = cost
497
498 return schedule
499
500 def update_op_memory_snapshot(self, schedule: Schedule):
501 memories_list = [(self.arch.fast_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))]
502
503 # Collect live ranges from tensors
504 lr_graph = live_range.LiveRangeGraph()
505 for mem_area, mem_type_set in memories_list:
506 live_range.extract_live_ranges_from_cascaded_passes(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200507 self.nng.get_root_subgraph(),
508 mem_area,
509 mem_type_set,
510 lr_graph,
511 Tensor.AllocationQuantum,
Tim Halld8339a72021-05-27 18:49:40 +0100512 )
513
514 # Populate time-array with memory used by live ranges
515 temporal_usage = lr_graph.get_temporal_memory_usage(self.arch.fast_storage_mem_area)
516 schedule.memory_snapshot = temporal_usage
517
518 # Set the peak memory usage
519 schedule.fast_storage_peak_usage = max(temporal_usage, default=0)
520
521 def estimate_op_performance(self, op: SchedulerOperation, block_config, ofm_depth):
522 query = npu_performance.PerformanceQuery(op.op_type.npu_block_type)
523 query.ifm_shape = op.ifm.shape
524 query.ifm_memory_area = op.ifm.mem_area
525 query.ifm_bits = op.ifm.dtype.size_in_bits()
526 query.ifm_format = op.ifm.format
527 query.ifm2_shape = op.ifm2 and op.ifm2.shape
528 query.ifm2_memory_area = op.ifm2 and op.ifm2.mem_area
529 query.ifm2_bits = op.ifm2 and op.ifm2.dtype.size_in_bits()
530 query.ifm2_format = op.ifm2 and op.ifm2.format
531 query.ofm_shape = op.ofm.shape.with_depth(ofm_depth)
532 query.ofm_memory_area = op.ofm.mem_area
533 query.ofm_bits = op.ofm.dtype.size_in_bits()
534 query.ofm_format = op.ofm.format
535 if op.parent_op.bias:
536 query.const_shape = Shape4D(1, 1, 1, op.ofm.shape.depth)
537 query.const_memory_area = self.arch.fast_storage_mem_area
538
539 query.kernel = op.kernel
540 query.config = block_config
541
542 return npu_performance.measure_cycle_cost(self.arch, op.op_type, op.activation and op.activation.op_type, query)
543
Tim Hall789e6f32021-06-17 17:02:31 +0100544 def propose_schedule_buffering(self, ref_schedule: Schedule, staging_limit_bytes):
Tim Halld8339a72021-05-27 18:49:40 +0100545 """Create a buffered schedule"""
546 buffered_schedule = Schedule(self.sg, f"{ref_schedule.label}_BUFFERED")
Tim Halld8339a72021-05-27 18:49:40 +0100547
548 prev_op = None
549 for sched_op in self.sched_ops:
550 if sched_op not in ref_schedule.cost_map:
551 # sched_op is not part of this sub-schedule - skip
552 continue
553
554 self.propose_operator_buffering(sched_op, prev_op, buffered_schedule, ref_schedule, staging_limit_bytes)
555 prev_op = sched_op
556
557 return buffered_schedule
558
559 def propose_operator_buffering(
560 self,
561 sched_op: SchedulerOperation,
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100562 prev_op: Optional[SchedulerOperation],
Tim Halld8339a72021-05-27 18:49:40 +0100563 buffered_schedule: Schedule,
564 ref_schedule: Schedule,
565 staging_limit_bytes,
566 ):
567 # Mild recursion might mean this Op has already been seen
568 if sched_op in buffered_schedule.cost_map:
569 return
570
571 # Take the reference schedule as default costings for this schedule
572 ref_cost = ref_schedule.cost_map[sched_op]
573 cost = copy.copy(ref_cost)
574 cost.slack_buffering_cycles = ref_cost.cycles.op_cycles
575 memory_snapshot = ref_schedule.memory_snapshot
576 ref_memory_usage = memory_snapshot[ref_cost.time_index] if ref_cost.time_index < len(memory_snapshot) else 0
577 cost.slack_buffering_memory = staging_limit_bytes - ref_memory_usage
578 buffered_schedule.cost_map[sched_op] = cost
579
580 # Attempt weight buffering on anything with a weights tensor
581 if sched_op.parent_op.weights:
582 self.propose_weight_buffering(
583 sched_op.parent_op.weights,
584 sched_op.parent_op.bias,
585 sched_op,
586 prev_op,
587 buffered_schedule,
588 ref_schedule,
589 cost.slack_buffering_memory,
590 )
591
592 return cost
593
594 def weights_needs_dma(self, weight_tensor):
595 if weight_tensor and weight_tensor.mem_type not in (MemType.Scratch, MemType.Scratch_fast):
596 # Weights are in permanent storage
597 # Only when permanent storage differs from feature map storage, there is a point moving the data
598 if (
599 weight_tensor.mem_area in (MemArea.Dram, MemArea.OffChipFlash)
600 and self.arch.permanent_storage_mem_area != self.arch.fast_storage_mem_area
601 ):
602 return True
603 return False
604
605 def propose_weight_buffering(
606 self,
607 weight_tensor,
608 scale_tensor,
609 sched_op: SchedulerOperation,
610 prev_op: SchedulerOperation,
611 buffered_schedule: Schedule,
612 ref_schedule: Schedule,
613 buffer_limit_bytes,
614 ):
615 cost = buffered_schedule.cost_map[sched_op]
616 prev_cost = buffered_schedule.cost_map.get(prev_op)
617 ref_cost = ref_schedule.cost_map[sched_op]
618 assert cost and ref_cost
619
620 needs_dma = self.weights_needs_dma(weight_tensor)
621
622 ofm_full_depth_slices = [0, ref_cost.stripe.depth]
623
624 # Encode weights for the full depth
Tim Halld784af72021-06-08 21:25:57 +0100625 full_weights, full_scales = weight_compressor.encode_weight_and_scale_tensor(
Tim Halld8339a72021-05-27 18:49:40 +0100626 self.arch,
627 sched_op.parent_op,
628 weight_tensor,
629 scale_tensor,
630 sched_op.kernel,
631 cost.block_config,
632 ofm_full_depth_slices,
633 )
634 full_weights_bytes = len(full_weights.buffer)
635 cost.ofm_depth_slices = ofm_full_depth_slices
636
637 # No buffering required - take all the weights from permanent storage
638 if sched_op.op_type == Op.FullyConnected or not needs_dma:
639 cost.npu_weights_tensor = full_weights
Tim Halld784af72021-06-08 21:25:57 +0100640 cost.npu_scales_tensor = full_scales
Tim Halld8339a72021-05-27 18:49:40 +0100641 return
642
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100643 encoded_weights: Optional[NpuWeightTensor] = full_weights
Tim Halld784af72021-06-08 21:25:57 +0100644 encoded_scales = full_scales
Tim Halld8339a72021-05-27 18:49:40 +0100645
646 # How many NPU cycles are available under the previously executing
647 # operator and SRAM unused for performing buffered DMA transfers
648 slack_cycles = prev_cost.slack_buffering_cycles if prev_cost else 0
649 slack_memory = prev_cost.slack_buffering_memory if prev_cost else 0
650
651 # Force full depth for cascaded Ops
652 if ref_cost.cascade != 0:
653 weight_tensor_purpose = TensorSubPurpose.Standard
654 weight_buffer_size = full_weights_bytes
655 # Update the memory snapshot to reflect the added size of the weights
656 ref_schedule.memory_snapshot[ref_cost.time_index] += weight_buffer_size
657 else:
658 # Estimate the buffering cycle time for the full set of weights
659 full_transfer_cycles = npu_performance.measure_mem2mem_cycles(
660 self.arch, weight_tensor.mem_area, self.arch.fast_storage_mem_area, full_weights_bytes
661 )
662 cost.full_weight_transfer_cycles = full_transfer_cycles
663
664 # Calculate the amount of prebuffering necessary (or what is possible with limited
665 # double buffer buffer size)
666 half_buffer_limit = buffer_limit_bytes // 2
667 if full_transfer_cycles > slack_cycles:
668 prebuffer_ratio = slack_cycles / full_transfer_cycles
669 prebuffer_bytes = min(prebuffer_ratio * full_weights_bytes, half_buffer_limit)
670 else:
671 prebuffer_bytes = min(full_weights_bytes, half_buffer_limit)
Tim Hall789e6f32021-06-17 17:02:31 +0100672
673 prebuffer_ratio = prebuffer_bytes / full_weights_bytes
Tim Halld8339a72021-05-27 18:49:40 +0100674
675 # Have to split the weights if the initial buffering can't store
676 # all of the compressed weights
677 if prebuffer_bytes < full_weights_bytes:
Tim Hall789e6f32021-06-17 17:02:31 +0100678 block_depth = cost.block_config.ofm_block.depth
Tim Halld8339a72021-05-27 18:49:40 +0100679
Tim Hall789e6f32021-06-17 17:02:31 +0100680 # Choose initial prebuffering depth (already buffer clamped)
681 prebuffer_depth = ref_cost.stripe.depth * prebuffer_ratio
Tim Halld8339a72021-05-27 18:49:40 +0100682 prebuffer_depth = int(max(16, round_down(prebuffer_depth, ArchitectureFeatures.OFMSplitDepth)))
683
Tim Hall789e6f32021-06-17 17:02:31 +0100684 # Calculate cycles executed during the prebuffer
685 pre_op_cycles = self.estimate_op_performance(sched_op, cost.block_config, prebuffer_depth)
686 buffering_depth = ref_cost.stripe.depth * (pre_op_cycles.op_cycles / full_transfer_cycles)
Tim Halld8339a72021-05-27 18:49:40 +0100687
Tim Hall789e6f32021-06-17 17:02:31 +0100688 # Choose initial buffering depth and clamp to the double buffering limit
689 buffering_depth = round_up(buffering_depth, block_depth)
690 buffering_bytes = (buffering_depth / ref_cost.stripe.depth) * full_weights_bytes
691 if buffering_bytes > half_buffer_limit:
692 buffering_depth = (half_buffer_limit / full_weights_bytes) * ref_cost.stripe.depth
693
694 while True:
695 # Attempt to buffer whole blocks
Johan Alfvéncce7f2d2022-04-08 10:47:09 +0200696 if buffering_depth > block_depth:
Tim Hall789e6f32021-06-17 17:02:31 +0100697 buffering_depth = round_down(buffering_depth, block_depth)
698 else:
699 buffering_depth = round_down(buffering_depth, ArchitectureFeatures.OFMSplitDepth)
700 buffering_depth = int(max(buffering_depth, ArchitectureFeatures.OFMSplitDepth))
Tim Halld8339a72021-05-27 18:49:40 +0100701
702 # Create list of depth slices
703 depth_slices = [0]
704 if prebuffer_depth < ref_cost.stripe.depth:
705 depth_slices += list(range(prebuffer_depth, ref_cost.stripe.depth, buffering_depth))
706 depth_slices.append(ref_cost.stripe.depth)
707
708 # Encode weights based depth slices
709 cost.ofm_depth_slices = depth_slices
Tim Halld784af72021-06-08 21:25:57 +0100710 encoded_weights, encoded_scales = weight_compressor.encode_weight_and_scale_tensor(
Tim Halld8339a72021-05-27 18:49:40 +0100711 self.arch,
712 sched_op.parent_op,
713 weight_tensor,
714 scale_tensor,
715 sched_op.kernel,
716 cost.block_config,
717 cost.ofm_depth_slices,
718 )
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100719 assert encoded_weights is not None
Tim Halld8339a72021-05-27 18:49:40 +0100720 # Chosen buffering might not fit at all, iterate until it does
721 # or until the minimum usable slice size is reached
722 if (
Tim Hallb5df7732022-05-04 16:20:43 +0100723 encoded_weights.max_range_bytes <= half_buffer_limit
Tim Halld8339a72021-05-27 18:49:40 +0100724 or prebuffer_depth == ArchitectureFeatures.OFMSplitDepth
725 ):
726 break
727
Tim Hall789e6f32021-06-17 17:02:31 +0100728 if buffering_depth > prebuffer_depth:
729 buffering_depth = round_up(buffering_depth // 2, ArchitectureFeatures.OFMSplitDepth)
730 else:
731 prebuffer_depth = round_up(prebuffer_depth // 2, ArchitectureFeatures.OFMSplitDepth)
Tim Halld8339a72021-05-27 18:49:40 +0100732
733 # Calculate cycles required to run the last op for use as future slack
734 tail_cycles = self.estimate_op_performance(
735 sched_op, cost.block_config, depth_slices[-1] - depth_slices[-2]
736 )
737 cost.slack_buffering_cycles = tail_cycles.op_cycles
738
739 # Determine whether the weights need to be double buffered
Tim Hallb5df7732022-05-04 16:20:43 +0100740 weight_buffer_size = min(len(encoded_weights.buffer), encoded_weights.max_range_bytes)
Tim Halld8339a72021-05-27 18:49:40 +0100741
742 # Only buffer weights if there's still space left for the buffer
743 if weight_buffer_size <= buffer_limit_bytes:
744 assert weight_buffer_size % 16 == 0
745 # Determine whether to double buffer or single buffer
Tim Hallb5df7732022-05-04 16:20:43 +0100746 if (weight_buffer_size * 2 <= buffer_limit_bytes) and (weight_buffer_size < len(encoded_weights.buffer)):
747 weight_buffer_size = weight_buffer_size * 2
Tim Halld8339a72021-05-27 18:49:40 +0100748 weight_tensor_purpose = TensorSubPurpose.DoubleBuffer
749 else:
750 weight_tensor_purpose = TensorSubPurpose.Standard
751
Tim Hallb5df7732022-05-04 16:20:43 +0100752 cost.buffered_weight_tensor = self.buffer_tensor(
753 encoded_weights, weight_tensor_purpose, weight_buffer_size, weight_tensor.name
754 )
Tim Halld8339a72021-05-27 18:49:40 +0100755 if ref_cost.cascade == 0:
Tim Hallb5df7732022-05-04 16:20:43 +0100756 # Determine if the lifetime can be extended and pre-buffer weights under the previous operation
757 cost.buffered_weight_tensor.pre_buffer = weight_buffer_size < slack_memory
Tim Halld8339a72021-05-27 18:49:40 +0100758
759 cost.slack_buffering_memory -= weight_buffer_size
760 else:
761 # Don't slice or buffer - use the whole depth from persistent storage
762 cost.ofm_depth_slices = ofm_full_depth_slices
763 encoded_weights = full_weights
Tim Halld784af72021-06-08 21:25:57 +0100764 encoded_scales = full_scales
Tim Halld8339a72021-05-27 18:49:40 +0100765
766 cost.npu_weights_tensor = encoded_weights
Tim Halld784af72021-06-08 21:25:57 +0100767 cost.npu_scales_tensor = encoded_scales
Tim Halld8339a72021-05-27 18:49:40 +0100768
Jacob Bohlineee9e5d2021-08-17 17:44:45 +0200769 def buffer_tensor(self, src_tensor: Tensor, sub_purpose: TensorSubPurpose, buffer_size: int, name: str) -> Tensor:
Tim Hallb5df7732022-05-04 16:20:43 +0100770 buffered_weight_tensor = Tensor([1, 1, 1, buffer_size], DataType.uint8, name + "_buffer")
Jacob Bohlineee9e5d2021-08-17 17:44:45 +0200771 buffered_weight_tensor.src_tensor = src_tensor
772 buffered_weight_tensor.mem_area = self.arch.fast_storage_mem_area
773 buffered_weight_tensor.mem_type = MemType.Scratch_fast
774 buffered_weight_tensor.purpose = TensorPurpose.Weights
775 buffered_weight_tensor.sub_purpose = sub_purpose
776 return buffered_weight_tensor
777
Tim Halld8339a72021-05-27 18:49:40 +0100778 def propose_minimal_schedule(self) -> Schedule:
779 """Proposes scheduling parameters where every operator is subdivided into the smallest stripe that satisfies the
780 next operators stride"""
781 min_schedule = Schedule(self.sg, "MIN")
782 cost_map = min_schedule.cost_map
783
784 # Keep track of the previous Op - which consumes the current Op's OFM
Jonas Ohlsson845e2322022-03-01 12:39:55 +0100785 prev_op: Optional[SchedulerOperation] = None
Tim Halld8339a72021-05-27 18:49:40 +0100786 for sched_op in reversed(self.sched_ops):
787 min_stripe_height = prev_op.kernel.stride.y if prev_op else 1
788 min_stripe = sched_op.ofm.shape.with_height(min_stripe_height)
789
790 cost = sched_op.create_scheduler_info(self.nng, min_stripe)
791 cost.cycles = self.estimate_op_performance(sched_op, cost.block_config, sched_op.ofm.shape.depth)
792 cost_map[sched_op] = cost
793
794 prev_op = sched_op
795
796 return min_schedule
797
798 def propose_schedule_striping(self, final_stripe: Shape4D, label: str, ref_schedule: Schedule) -> Schedule:
799 """Proposes new striping for a schedule. The stripe is derived from the ifm requirements of the next Op down"""
800 ref_cost = ref_schedule.cost_map
801
802 striped_schedule = Schedule(self.sg, label)
803 stripe = final_stripe
804 for sched_op in reversed(self.sched_ops):
805 if sched_op not in ref_cost:
806 # sched_op is not part of the sub-schedule - skip
807 continue
808
809 # Create a cost entry with the new stripe
810 cost = sched_op.create_scheduler_info(self.nng, stripe)
811
Tim Hallb5df7732022-05-04 16:20:43 +0100812 if ref_cost[sched_op].buffered_weight_tensor:
Jacob Bohlineee9e5d2021-08-17 17:44:45 +0200813 # If the weights are buffered in the reference schedule they should be in the new proposal
814 weight_tensor = cost.npu_weights_tensor
Tim Hallb5df7732022-05-04 16:20:43 +0100815 cost.buffered_weight_tensor = self.buffer_tensor(
816 weight_tensor, TensorSubPurpose.Standard, len(weight_tensor.buffer), weight_tensor.name
Jacob Bohlineee9e5d2021-08-17 17:44:45 +0200817 )
Tim Halld8339a72021-05-27 18:49:40 +0100818
819 # Estimate performance
820 cost.cycles = self.estimate_op_performance(sched_op, cost.block_config, sched_op.ofm.shape.depth)
821 striped_schedule.cost_map[sched_op] = cost
822
823 # Calculate the preceeding Op's stripe
824 stripe = sched_op.ifm.shape.with_height(stripe.height * sched_op.kernel.stride.y)
825
826 return striped_schedule
827
828 def estimate_schedule_memory_usage(self, schedule: Schedule, non_local_mem_usage: dict):
829 """Estimates the memory usage of a schedule"""
830 cost = schedule.cost_map
831 cascades = schedule.cascades
832 peak_mem_usage = 0
833 for sched_op in self.sched_ops:
834 if sched_op not in cost:
835 # sched_op is not part of the sub-schedule - skip
836 continue
837
838 if cost[sched_op].cascade:
839 # This Op is part of a cascade - use the cascade's memory usage
840 cascade_info = cascades[cost[sched_op].cascade]
841 # Non-local memory usage is already included in the cascade_info
842 peak_mem_usage = max(cascade_info.mem_usage, peak_mem_usage)
843 else:
844 # This Op is not part of a cascade - calculate the memory usage
Tim Hallb5df7732022-05-04 16:20:43 +0100845 op_weight_buffer = 0
846 if cost[sched_op].buffered_weight_tensor:
847 op_weight_buffer = cost[sched_op].buffered_weight_tensor.storage_size()
Tim Halld8339a72021-05-27 18:49:40 +0100848
849 op_mem_usage = (
850 sched_op.ifm_size_in_bytes()
851 + sched_op.ofm_size_in_bytes()
852 + op_weight_buffer
853 + non_local_mem_usage.get(sched_op, 0)
854 )
855 peak_mem_usage = max(op_mem_usage, peak_mem_usage)
856
857 return peak_mem_usage
858
859 def optimize_sub_schedule(
860 self, cascade_info: CascadeInfo, ref_schedule: Schedule, max_template: Schedule, memory_limit: int
861 ) -> Schedule:
862 """Extracts the Ops covered by the given cascade and creates a sub-schedule. The sub-schedule is optimized by
863 proposing weight buffering and then continously proposing new stripe sizes"""
864 ref_cost = ref_schedule.cost_map
865 # Extract the ops that are part of this sub-schedule
866 start = cascade_info.start
867 end = cascade_info.end
868 sub_schedule_ops = self.sched_ops[start : end + 1]
869 # Create a sub-schedule that contains only the costs for the Ops that are part of the sub-schedule
870 sub_schedule = Schedule(self.sg, f"SUB_{start}_{end}")
871 for sched_op in sub_schedule_ops:
872 sub_schedule.cost_map[sched_op] = ref_cost[sched_op]
873
874 sub_schedule.cascades[end] = cascade_info
875 # Use the memory snapshot from the reference schedule
876 sub_schedule.memory_snapshot = ref_schedule.memory_snapshot
877
878 # Calculate memory usage that is live during the sub-schedule but not part of it
879 time_for_cascade = ref_cost[sub_schedule_ops[0]].time_index
880 mem_usage_parallel_to_sub_schedule = ref_schedule.memory_snapshot[time_for_cascade] - cascade_info.mem_usage
881 # If the first Op's IFM has other consumers it has to live throughout the whole sub-schedule whether it's
882 # included in a cascade or not
883 persistent_initial_ifm = (
884 sub_schedule_ops[0].ifm_size_in_bytes() if len(sub_schedule_ops[0].ifm.connection.consumers) > 1 else 0
885 )
886 # Calculate non-local-mem-usage per Operator
887 non_local_mem_usage = {}
888 for idx, sched_op in enumerate(sub_schedule_ops):
889 non_local_mem_usage[sched_op] = mem_usage_parallel_to_sub_schedule
890 if idx != 0:
891 non_local_mem_usage[sched_op] += persistent_initial_ifm
892
893 cascade_builder = CascadeBuilder(sub_schedule_ops, self.arch.is_spilling_enabled(), non_local_mem_usage)
894
895 # Start by adding buffering
Tim Hall789e6f32021-06-17 17:02:31 +0100896 buffered_sub_schedule = self.propose_schedule_buffering(
897 sub_schedule, self.scheduler_options.optimization_sram_limit
898 )
Tim Halld8339a72021-05-27 18:49:40 +0100899 # Copy the cascades over from the unbuffered-schedule
900 buffered_sub_schedule.cascades = sub_schedule.cascades
901
902 # Generate the possible stripings for the final Op in the sub-schedule
903 final_ofm_shape = sub_schedule_ops[-1].ofm.shape
904 possible_stripes = [
905 final_ofm_shape.with_height(stripe_h) for stripe_h in range(1, final_ofm_shape.height // 2 + 1)
906 ]
907
908 # Propose different striping - the possible stripes are proposed similarly to a binary search
Jacob Bohlinfad72042021-08-24 21:51:41 +0200909 best_schedule = None
Tim Halld8339a72021-05-27 18:49:40 +0100910 iteration = 0
911 while len(possible_stripes) > 1:
912 proposed_stripe = possible_stripes[len(possible_stripes) // 2]
913 proposed_schedule = self.propose_schedule_striping(
914 proposed_stripe, f"OPTIMIZED_{iteration}", buffered_sub_schedule
915 )
916
917 cascade_builder.build_cascades(proposed_schedule, max_template, memory_limit)
918
919 # Check if proposal fits
920 proposed_schedule_mem_usage = self.estimate_schedule_memory_usage(proposed_schedule, non_local_mem_usage)
921 if (proposed_schedule_mem_usage) <= memory_limit:
922 # Remove all possible stripes smaller than this
923 possible_stripes = possible_stripes[len(possible_stripes) // 2 :]
924 best_schedule = proposed_schedule
925 if not proposed_schedule.cascades:
926 # No cascading required - early exit
927 break
928 else:
929 # Proposal doesn't fit within the limit - remove all possible stripes larger than this
930 possible_stripes = possible_stripes[: len(possible_stripes) // 2]
931
932 iteration += 1
933
934 return best_schedule
935
936 def optimize_schedule(
Jonas Ohlssond8575072022-03-30 10:30:25 +0200937 self,
938 schedule: Schedule,
939 max_sched: Schedule,
940 max_template: Schedule,
941 options: SchedulerOptions,
Tim Halld8339a72021-05-27 18:49:40 +0100942 ) -> Schedule:
943 """Extracts sub-schedules based on the cascades and optimizes them and applies them to the final schedule"""
944 sram_limit = options.optimization_sram_limit
945 if max_sched.fast_storage_peak_usage < sram_limit and not self.arch.is_spilling_enabled():
946 # Maximum performance schedule fits within the SRAM target
947 return max_sched
948
Jacob Bohlinfad72042021-08-24 21:51:41 +0200949 # Iterate over a copy of the cascades since they may change during the loop
950 for cascade_info in list(schedule.cascades.values()):
Tim Halld8339a72021-05-27 18:49:40 +0100951 # Optimize the sub-schedule in this cascade
952 opt_sub_schedule = self.optimize_sub_schedule(cascade_info, schedule, max_template, sram_limit)
Jacob Bohlinfad72042021-08-24 21:51:41 +0200953 if opt_sub_schedule:
954 # Remove the existing cascade
955 del schedule.cascades[cascade_info.end]
956 # Update the sub-schedule Op and cascade costs to the full schedule
957 schedule.cost_map.update(opt_sub_schedule.cost_map)
958 schedule.cascades.update(opt_sub_schedule.cascades)
Tim Halld8339a72021-05-27 18:49:40 +0100959
960 # Update memory snapshot
961 self.sg.schedule = schedule
962 self.update_op_memory_snapshot(schedule)
963 # Propose schedule buffering to the optimized schedule
Tim Hall789e6f32021-06-17 17:02:31 +0100964 optimized_sched = self.propose_schedule_buffering(schedule, self.scheduler_options.optimization_sram_limit)
Tim Halld8339a72021-05-27 18:49:40 +0100965 # Copy the cascade's metadata from the unbuffered schedule
966 optimized_sched.cascades = schedule.cascades
967 return optimized_sched
968
969 def apply_schedule(self, sched: Schedule):
970 """Applies the given schedule as a final solution"""
971 for sched_op in self.sched_ops:
972 op_info = sched.cost_map[sched_op]
973 cascade_info = sched.cascades.get(op_info.cascade, None)
974 if cascade_info and sched_op in cascade_info.buffers:
975 buffer_tens = sched_op.ifm.connection.parent_tens
976 # Apply memory area and type
977 buffer_tens.mem_area = self.arch.fast_storage_mem_area
978 buffer_tens.mem_type = MemType.Scratch_fast
979 # Apply Rolling buffer
980 buffer_tens.set_format(TensorFormat.NHCWB16, self.arch)
981 buffer_tens.set_new_sub_purpose(TensorSubPurpose.RollingBufferY, cascade_info.buffers[sched_op].height)
982
983 sched_op.parent_ps.block_config = op_info.block_config.old_style_representation()
984
985 # Ensure that the src_tensor reference is set correctly
Tim Hallb5df7732022-05-04 16:20:43 +0100986 if op_info.buffered_weight_tensor:
987 op_info.buffered_weight_tensor.src_tensor = op_info.npu_weights_tensor
Tim Halld8339a72021-05-27 18:49:40 +0100988
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100989 def use_fast_storage_for_feature_maps(self, schedule, staging_limit):
990 scratched_fms = {}
991 max_mem_usage = []
992 base_mem_usage = []
993 fast_storage_type = MemType.Scratch_fast
994 fast_storage_mem_area = self.arch.fast_storage_mem_area
Tim Halld8339a72021-05-27 18:49:40 +0100995
996 # Force all OFMs to fast-storage
997 for sched_op in self.sched_ops:
998 cost = schedule.cost_map[sched_op]
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +0100999 if cost.cascade == 0 and sched_op.get_dependants():
1000 ofm_tens = sched_op.ofm.connection.parent_tens
1001 if not any(cons is None for cons in ofm_tens.consumer_list):
1002 if ofm_tens not in scratched_fms:
1003 scratched_fms[ofm_tens] = (ofm_tens.mem_area, ofm_tens.mem_type)
1004 ofm_tens.mem_area = fast_storage_mem_area
1005 ofm_tens.mem_type = fast_storage_type
Tim Halld8339a72021-05-27 18:49:40 +01001006
1007 # Collect live ranges from tensors
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +01001008 memories_list = [(fast_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))]
Tim Halld8339a72021-05-27 18:49:40 +01001009 lr_graph = live_range.LiveRangeGraph()
1010 for mem_area, mem_type_set in memories_list:
1011 live_range.extract_live_ranges_from_cascaded_passes(
Jonas Ohlssond8575072022-03-30 10:30:25 +02001012 self.nng.get_root_subgraph(),
1013 mem_area,
1014 mem_type_set,
1015 lr_graph,
1016 Tensor.AllocationQuantum,
Tim Halld8339a72021-05-27 18:49:40 +01001017 )
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +01001018 max_mem_usage = lr_graph.get_temporal_memory_usage(fast_storage_mem_area)
Tim Halld8339a72021-05-27 18:49:40 +01001019
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +01001020 # If true, everything fits and we can proceed
1021 if max(max_mem_usage) <= staging_limit:
1022 return
1023
1024 # Build up the base memory usage by removing the
1025 # mem_usage of the lrs we previously moved to fast-storage
1026 base_mem_usage = np.array(max_mem_usage)
1027 curr_lrs = []
Tim Halld8339a72021-05-27 18:49:40 +01001028 for lr in lr_graph.lrs:
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +01001029 for tens in lr.tensors:
1030 if scratched_fms.get(tens):
1031 curr_lrs.append(lr)
1032 base_mem_usage[lr.start_time : lr.end_time + 1] -= lr.size
1033 break
1034
1035 competing_lrs = []
1036 for lr in curr_lrs:
1037 base_usage = max(base_mem_usage[lr.start_time : lr.end_time + 1])
1038 # If true, the lr will never fit and may thus be evicted
1039 if base_usage + lr.size > staging_limit:
1040 FastStorageComponentAllocator.evict(lr, max_mem_usage, scratched_fms)
1041 continue
1042 # Since max_mem_usage is the memory usage with all FMs still in fast-storage,
1043 # the memory limit cannot be exceeded if max_mem_usage does not.
1044 # Thus, the affected lrs can remain in fast-storage if the following is true
1045 if max(max_mem_usage[lr.start_time : lr.end_time + 1]) <= staging_limit:
1046 FastStorageComponentAllocator.keep(lr, base_mem_usage, staging_limit)
1047 else:
1048 competing_lrs.append(lr)
1049 sz = len(competing_lrs)
1050 # All lrs and their tensors have been handled if sz is zero, we may thus return
1051 if sz == 0:
1052 return
1053
1054 competing_lrs = sorted(competing_lrs, key=lambda lr: (lr.start_time, lr.end_time + 1, lr.size))
1055 start = 0
1056 start_time = competing_lrs[0].start_time
1057 end_time = competing_lrs[0].end_time
1058 component_allocator = FastStorageComponentAllocator(base_mem_usage, max_mem_usage, staging_limit)
1059 # Build up components and then allocate each separately
1060 for i, lr in enumerate(competing_lrs):
1061 if lr.start_time <= end_time and i - start < component_allocator.max_exhaustive_size:
1062 start_time = min(start_time, lr.start_time)
1063 end_time = max(end_time, lr.end_time)
1064 else:
1065 component_allocator.allocate_component(
1066 component_allocator,
1067 competing_lrs[start:i],
1068 max_mem_usage,
1069 base_mem_usage,
1070 staging_limit,
1071 scratched_fms,
1072 )
1073 start = i
1074 start_time = lr.start_time
1075 end_time = lr.end_time
1076 component_allocator.allocate_component(
1077 component_allocator, competing_lrs[start:sz], max_mem_usage, base_mem_usage, staging_limit, scratched_fms
1078 )
Tim Halld8339a72021-05-27 18:49:40 +01001079
1080 def move_constant_data(self):
1081 """Determine if data, can be moved from permanent storage to another memory area. A move
1082 will generate a DMA command in the high-level command stream"""
1083 for sched_op in self.sched_ops:
1084 parent_op = sched_op.parent_op
1085 is_lut_used = any(inp.purpose == TensorPurpose.LUT for inp in parent_op.inputs)
1086 max_ifm_shram_avail = (
1087 (self.arch.available_shram_banks(is_lut_used) - self.arch.shram_reserved_output_banks)
1088 * self.arch.shram_bank_size
1089 // 2
1090 )
1091
1092 for idx, tens in enumerate(parent_op.inputs):
1093 if tens.mem_type not in (MemType.Scratch, MemType.Scratch_fast):
1094 # Tensor is in permanent storage
1095 # Only when permanent storage differs from feature map storage, there is a point moving the data
1096 if (
1097 tens.mem_area in self.arch.permanent_storage_mem_area
1098 and self.arch.permanent_storage_mem_area != self.arch.feature_map_storage_mem_area
1099 ) or tens.purpose == TensorPurpose.LUT:
1100 if tens.purpose == TensorPurpose.LUT or (
Patrik Gustavsson94292fe2021-09-02 08:22:58 +02001101 # For elementwise broadcast
Tim Halld8339a72021-05-27 18:49:40 +01001102 tens.purpose == TensorPurpose.FeatureMap
1103 and sched_op.op_type.is_binary_elementwise_op()
1104 and tens.shape != []
1105 and sched_op.ifm.shape != sched_op.ofm.shape
Patrik Gustavsson94292fe2021-09-02 08:22:58 +02001106 and parent_op.write_shape is None
Tim Halld8339a72021-05-27 18:49:40 +01001107 and tens.storage_size() > max_ifm_shram_avail
1108 ):
1109 only_vector_product_consumers = all(
1110 oper and oper.type.npu_block_type == NpuBlockType.VectorProduct
1111 for oper in tens.consumers()
1112 )
1113
1114 if (not only_vector_product_consumers) or tens.purpose == TensorPurpose.LUT:
1115 new_tens = tens.clone_into_fast_storage(self.arch)
1116 if tens.purpose == TensorPurpose.LUT:
1117 new_tens.mem_area = MemArea.Shram
1118
1119 new_tens.consumer_list.append(parent_op)
1120 parent_op.inputs[idx] = new_tens
Dwight Lidman352607c2021-09-29 17:00:09 +02001121 # If the index is out of range, IFM and IFM2 are the same tensor
1122 # and pass inputs don't have duplicates
1123 if idx < len(sched_op.parent_ps.inputs):
1124 sched_op.parent_ps.inputs[idx] = new_tens
Tim Halld8339a72021-05-27 18:49:40 +01001125
1126 def print_schedule(self, schedule: Schedule):
1127 print(f"Schedule: '{schedule.name}'")
1128 for sched_op in self.sched_ops:
1129 if sched_op not in schedule.cost_map:
1130 # Sub-schedule printing
1131 continue
1132
1133 op_info = schedule.cost_map[sched_op]
1134 print(f"\t{sched_op.index}: Operation {sched_op.name} - OFM {sched_op.ofm.shape}")
1135 print(f"\t\tType: {sched_op.op_type}")
1136 print(f"\t\tKernel: {sched_op.kernel}")
1137 print(f"{op_info}")
1138 mem_usage = (
1139 schedule.memory_snapshot[op_info.time_index]
1140 if op_info.time_index < len(schedule.memory_snapshot)
1141 else 0
1142 )
1143 print(f"\t\tSRAM Used: {mem_usage} bytes")
1144
Jonas Ohlsson25e700c2022-03-04 14:58:56 +01001145 print("\tCascades:")
Tim Halld8339a72021-05-27 18:49:40 +01001146 for i, cascade in enumerate(schedule.cascades.values()):
1147 print(f"\t\t{i}: {cascade.start} -> {cascade.end}, size: {cascade.mem_usage}")
Patrik Gustavssonfeeb06d2020-04-22 12:53:47 +02001148
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001149
Tim Halld8339a72021-05-27 18:49:40 +01001150def _update_tensor_allocation(nng: Graph, arch: ArchitectureFeatures, options):
1151 """
1152 Creates live ranges and runs tensor allocator for the current schedule
1153 (i.e. sg.schedule for all subgraphs), returns the maximum memory usage
1154 and updates SchedulerOpInfo.mem_usage for all operations in the schedule.
1155 """
1156 root_sg = nng.get_root_subgraph()
1157
1158 alloc_list = []
1159 if arch.is_spilling_enabled():
1160 mem_alloc_scratch_fast = (arch.fast_storage_mem_area, set((MemType.Scratch_fast,)))
1161 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch,)))
1162 # Order is important
1163 alloc_list.append(mem_alloc_scratch_fast)
1164 alloc_list.append(mem_alloc_scratch)
1165 else:
1166 mem_alloc_scratch = (arch.feature_map_storage_mem_area, set((MemType.Scratch, MemType.Scratch_fast)))
1167 alloc_list.append(mem_alloc_scratch)
1168
1169 for mem_area, mem_type_set in alloc_list:
1170 tensor_allocation.allocate_tensors(
1171 nng,
1172 root_sg,
1173 arch,
1174 mem_area,
1175 mem_type_set,
1176 tensor_allocator=options.tensor_allocator,
1177 verbose_allocation=options.verbose_allocation,
1178 cpu_tensor_alignment=options.cpu_tensor_alignment,
1179 )
1180
1181
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +01001182class FastStorageComponentAllocator:
1183 def __init__(self, base_mem_usage, max_mem_usage, staging_limit):
1184 self.base_mem_usage = base_mem_usage
1185 self.max_mem_usage = list(max_mem_usage)
1186 self.staging_limit = staging_limit
1187 self.lrs = []
1188 self.evicted = []
1189 self.curr_evicted = []
1190 self.remaining_total_size = []
1191 self.best_allocated_size = 0
1192 self.max_exhaustive_size = 20
1193
1194 def allocate_exhaustive(self, ix, alloc_size):
1195 if ix >= len(self.lrs):
1196 if alloc_size > self.best_allocated_size:
1197 self.best_allocated_size = alloc_size
Louis Verhaard5c8f1e52022-02-23 14:13:07 +01001198 self.evicted = self.curr_evicted.copy()
erik.andersson@arm.comde6cb642022-02-02 14:03:15 +01001199 return
1200
1201 lr = self.lrs[ix]
1202 for t in range(lr.start_time, lr.end_time):
1203 assert self.base_mem_usage[t] <= self.max_mem_usage[t]
1204 base_usage = max(self.base_mem_usage[lr.start_time : lr.end_time + 1])
1205 can_fit = base_usage + lr.size <= self.staging_limit
1206 always_fits = can_fit
1207
1208 if can_fit:
1209 max_usage = max(self.max_mem_usage[lr.start_time : lr.end_time + 1])
1210 always_fits = max_usage <= self.staging_limit
1211
1212 if can_fit or always_fits:
1213 self.curr_evicted[ix] = False
1214 self.base_mem_usage = self.update_mem_usage(self.base_mem_usage, lr, True)
1215 self.allocate_exhaustive(ix + 1, alloc_size + lr.size)
1216 self.base_mem_usage = self.update_mem_usage(self.base_mem_usage, lr, False)
1217
1218 if not always_fits:
1219 self.curr_evicted[ix] = True
1220 self.max_mem_usage = self.update_mem_usage(self.max_mem_usage, lr, False)
1221 self.allocate_exhaustive(ix + 1, alloc_size)
1222 self.max_mem_usage = self.update_mem_usage(self.max_mem_usage, lr, True)
1223
1224 @staticmethod
1225 def update_mem_usage(mem_usage, lr, increase):
1226 for t in range(lr.start_time, lr.end_time + 1):
1227 mem_usage[t] += lr.size if increase else -lr.size
1228 assert mem_usage[t] >= 0
1229 return mem_usage
1230
1231 @staticmethod
1232 def evict(lr, max_mem_usage, scratched_fms):
1233 for t in range(lr.start_time, lr.end_time + 1):
1234 max_mem_usage[t] -= lr.size
1235 for tens in lr.tensors:
1236 if tens in scratched_fms:
1237 tens.mem_area = scratched_fms[tens][0]
1238 tens.mem_type = scratched_fms[tens][1]
1239
1240 @staticmethod
1241 def keep(lr, base_mem_usage, staging_limit):
1242 for t in range(lr.start_time, lr.end_time + 1):
1243 base_mem_usage[t] += lr.size
1244 assert base_mem_usage[t] <= staging_limit
1245
1246 def allocate_component(self, allocator, lrs, max_mem, min_mem, staging_limit, scratched_fms):
1247 sz = len(lrs)
1248 allocator.lrs = lrs
1249 allocator.evicted = [0] * len(lrs)
1250 allocator.curr_evicted = [0] * sz
1251 allocator.best_allocated_size = -1
1252 # Recursively evaluate all permutations of allocations of the lrs found in the component
1253 allocator.allocate_exhaustive(0, 0)
1254
1255 # Optimal allocation has been found, move lrs accordingly
1256 for i, e in enumerate(allocator.evicted):
1257 if e:
1258 self.evict(lrs[i], max_mem, scratched_fms)
1259 else:
1260 self.keep(lrs[i], min_mem, staging_limit)
1261
1262
Tim Halld8339a72021-05-27 18:49:40 +01001263def schedule_passes(nng: Graph, arch: ArchitectureFeatures, options, scheduler_options: SchedulerOptions):
1264 """Entry point for the Scheduler"""
1265 # Initialize CPU subgraphs
1266 schedulers = dict()
1267 # Initialize schedulers with max schedule. Only schedule NPU subgraphs
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001268 for sg in nng.subgraphs:
Tim Halld8339a72021-05-27 18:49:40 +01001269 if sg.placement != PassPlacement.Npu:
1270 # Create cascaded passes for CPU Ops
1271 cascaded_passes = []
1272 for idx, ps in enumerate(sg.passes):
1273 cps = CascadedPass(
Jonas Ohlssond8575072022-03-30 10:30:25 +02001274 ps.name,
1275 SchedulingStrategy.WeightStream,
1276 ps.inputs,
1277 [],
1278 ps.outputs,
1279 [ps],
1280 ps.placement,
1281 False,
Tim Halld8339a72021-05-27 18:49:40 +01001282 )
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001283
Tim Halld8339a72021-05-27 18:49:40 +01001284 cps.time = idx
1285 ps.cascade = cps
1286 cascaded_passes.append(cps)
Andreas Nevalainen27d36f02020-11-19 11:27:50 +01001287
Tim Halld8339a72021-05-27 18:49:40 +01001288 sg.cascaded_passes = cascaded_passes
1289 else:
1290 # Npu subgraph - create schedule
1291 scheduler = Scheduler(nng, sg, arch, scheduler_options)
1292 schedulers[sg] = scheduler
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001293
Tim Halld8339a72021-05-27 18:49:40 +01001294 scheduler.create_scheduler_representation(arch)
1295 sg.sched_ops = scheduler.sched_ops
1296 scheduler.move_constant_data()
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001297
Tim Halld8339a72021-05-27 18:49:40 +01001298 # Create the Max schedule template
1299 max_schedule_template = scheduler.create_initial_schedule()
1300 scheduler.max_schedule = max_schedule_template
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001301
Tim Halld8339a72021-05-27 18:49:40 +01001302 # Create the optimimised Max schedule
1303 sg.schedule = max_schedule_template
1304 scheduler.update_op_memory_snapshot(max_schedule_template)
Tim Hall789e6f32021-06-17 17:02:31 +01001305 opt_max_schedule = scheduler.propose_schedule_buffering(max_schedule_template, 1 << 32)
Tim Halld8339a72021-05-27 18:49:40 +01001306 sg.schedule = opt_max_schedule
1307 scheduler.update_op_memory_snapshot(opt_max_schedule)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001308
Tim Halld8339a72021-05-27 18:49:40 +01001309 # Create Min schedule
1310 min_schedule = scheduler.propose_minimal_schedule()
1311 initial_sram_limit = scheduler_options.optimization_sram_limit
1312 if scheduler_options.optimization_strategy == OptimizationStrategy.Size:
1313 initial_sram_limit = scheduler.min_memory_req
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001314
Tim Halld8339a72021-05-27 18:49:40 +01001315 cascade_builder = CascadeBuilder(scheduler.sched_ops, arch.is_spilling_enabled())
1316 cascade_builder.build_cascades(min_schedule, max_schedule_template, initial_sram_limit)
1317 sg.schedule = min_schedule
1318 scheduler.update_op_memory_snapshot(min_schedule)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001319
Tim Halld8339a72021-05-27 18:49:40 +01001320 if scheduler_options.optimization_strategy == OptimizationStrategy.Performance:
1321 # Create an optimized schedule
1322 sg.schedule = scheduler.optimize_schedule(
1323 min_schedule, opt_max_schedule, max_schedule_template, scheduler_options
1324 )
1325 scheduler.update_op_memory_snapshot(sg.schedule)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001326
Tim Halld8339a72021-05-27 18:49:40 +01001327 scheduler.apply_schedule(sg.schedule)
1328 scheduler.use_fast_storage_for_feature_maps(sg.schedule, scheduler_options.optimization_sram_limit)
Andreas Nevalainen897cc142020-10-28 15:42:08 +01001329
Tim Halld8339a72021-05-27 18:49:40 +01001330 if scheduler_options.verbose_schedule:
1331 scheduler.print_schedule(sg.schedule)
Tim Hall79d07d22020-04-27 18:20:16 +01001332
Tim Halld8339a72021-05-27 18:49:40 +01001333 # Evaluate schedule
1334 _update_tensor_allocation(nng, arch, options)