Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
| 18 | # Description: |
| 19 | # The scheduler costs various strategies for scheduling the network in order to select the block configuration. |
| 20 | |
| 21 | import enum |
| 22 | from .nn_graph import ( |
| 23 | TensorPurpose, |
| 24 | TensorSubPurpose, |
| 25 | TensorFormat, |
| 26 | MemArea, |
| 27 | SchedulingStrategy, |
| 28 | CascadedPass, |
| 29 | PassPlacement, |
| 30 | SchedulerRewrite, |
| 31 | Operation, |
| 32 | NpuBlockType, |
| 33 | ) |
| 34 | from . import live_range |
| 35 | import numpy as np |
| 36 | from . import npu_performance |
| 37 | from . import stats_writer |
| 38 | from .npu_performance import make_bandwidth_array, make_macs_array, make_cycles_array, make_metrics_arrays, PassCycles |
| 39 | import time, copy |
| 40 | from .high_level_command_stream_generator import calc_allowed_ofm_ifm_overlap_for_pass_list |
| 41 | from .shared_buffer_allocation import ( |
| 42 | find_block_configs_suitable_for_pass_and_shared_buffer, |
| 43 | shared_buffer_allocation_for_pass_and_block_config, |
| 44 | ) |
| 45 | from functools import lru_cache |
| 46 | |
| 47 | |
| 48 | class ParetoMetric(enum.Enum): |
| 49 | BwCycMem = 1 |
| 50 | BwCycMemBlkH = 2 |
| 51 | |
| 52 | def __str__(self): |
| 53 | return self.name |
| 54 | |
| 55 | |
| 56 | class SchedulerOptions: |
| 57 | def __init__( |
| 58 | self, |
| 59 | use_cascading=True, |
| 60 | use_ifm_ofm_overlap=True, |
| 61 | verbose_schedule=False, |
| 62 | verbose_pareto_frontier_schedules=False, |
| 63 | use_ifm_streaming=True, |
| 64 | pareto_metric=ParetoMetric.BwCycMem, |
| 65 | ): |
| 66 | self.use_cascading = use_cascading |
| 67 | self.use_ifm_ofm_overlap = use_ifm_ofm_overlap |
| 68 | self.verbose_schedule = verbose_schedule |
| 69 | self.verbose_pareto_frontier_schedules = verbose_pareto_frontier_schedules |
| 70 | self.use_ifm_streaming = use_ifm_streaming |
| 71 | self.pareto_metric = pareto_metric |
| 72 | |
| 73 | def __str__(self): |
| 74 | return type(self).__name__ + ": " + str(self.__dict__) |
| 75 | |
| 76 | __repr__ = __str__ |
| 77 | |
| 78 | |
| 79 | class Strategy: |
| 80 | __slots__ = "strat", "param", "passes", "block_configs", "rewrite_list", "bws", "macs", "cycles", "sram_used" |
| 81 | |
| 82 | def __init__(self, strat, param, passes, block_configs, rewrite_list, bws, macs, cycles, sram_used): |
| 83 | self.strat = strat |
| 84 | self.param = param |
| 85 | self.passes = passes |
| 86 | self.block_configs = block_configs |
| 87 | self.rewrite_list = ( |
| 88 | rewrite_list # list of (SchedulerRewrite, Tensor, new sub purpose, purpose param a, purpose param b, pass) |
| 89 | ) |
| 90 | self.bws = bws |
| 91 | self.macs = macs |
| 92 | self.cycles = cycles |
| 93 | self.sram_used = sram_used |
| 94 | |
| 95 | def __eq__(self, other): |
| 96 | if self.strat != other.strat: |
| 97 | return False |
| 98 | if self.param != other.param: |
| 99 | return False |
| 100 | if self.block_configs != other.block_configs: |
| 101 | return False |
| 102 | if self.passes != other.passes: |
| 103 | return False |
| 104 | if (self.bws != other.bws).any(): |
| 105 | return False |
| 106 | if (self.macs != other.macs).any(): |
| 107 | return False |
| 108 | if (self.cycles != other.cycles).any(): |
| 109 | return False |
| 110 | if self.sram_used != other.sram_used: |
| 111 | return False |
| 112 | return True |
| 113 | |
| 114 | def empty(self): |
| 115 | return not self.passes |
| 116 | |
| 117 | def key(self): |
| 118 | return self.passes[-1] |
| 119 | |
| 120 | def clone(self): |
| 121 | return Strategy( |
| 122 | self.strat, |
| 123 | self.param, |
| 124 | self.passes, |
| 125 | self.block_configs, |
| 126 | self.rewrite_list, |
| 127 | self.bws, |
| 128 | self.macs, |
| 129 | self.cycles, |
| 130 | self.sram_used, |
| 131 | ) |
| 132 | |
| 133 | def __str__(self): |
| 134 | return "<scheduler.Strategy: %s %s %s %s %s %s %s>" % ( |
| 135 | self.strat, |
| 136 | self.passes, |
| 137 | self.rewrite_list, |
| 138 | self.bws, |
| 139 | self.macs, |
| 140 | self.cycles, |
| 141 | self.sram_used, |
| 142 | ) |
| 143 | |
| 144 | __repr__ = __str__ |
| 145 | |
| 146 | |
| 147 | class StrategySet: |
| 148 | __slots__ = "strats", "bws", "macs", "cycles", "max_sram_used", "total_sram_used" |
| 149 | |
| 150 | def __init__(self, strats=None): |
| 151 | if strats is None: |
| 152 | strats = dict() |
| 153 | self.strats = strats # final pass in packed pass -> Strategy |
| 154 | self.bws, self.macs, self.cycles = make_metrics_arrays() |
| 155 | self.max_sram_used = 0 |
| 156 | self.total_sram_used = 0 |
| 157 | |
| 158 | def update_statistics(self): |
| 159 | self.bws = make_bandwidth_array() |
| 160 | self.max_sram_used = 0 |
| 161 | for ps, strat in self.strats.items(): |
| 162 | self.bws += strat.bws |
| 163 | self.macs += strat.macs |
| 164 | self.cycles += strat.cycles |
| 165 | self.max_sram_used = max(self.max_sram_used, strat.sram_used) |
| 166 | self.total_sram_used += strat.sram_used |
| 167 | |
| 168 | def clone_add_strategy(self, new_strat): |
| 169 | key = new_strat.key() |
| 170 | if key in self.strats: |
| 171 | assert new_strat == self.strats[key] |
| 172 | return self |
| 173 | else: |
| 174 | new_strats = dict(self.strats) |
| 175 | new_strats[key] = new_strat |
| 176 | new_set = StrategySet(new_strats) |
| 177 | new_set.bws = self.bws + new_strat.bws |
| 178 | new_set.macs = self.macs + new_strat.macs |
| 179 | new_set.cycles = self.cycles + new_strat.cycles |
| 180 | new_set.max_sram_used = max(self.max_sram_used, new_strat.sram_used) |
| 181 | new_set.total_sram_used = self.total_sram_used + new_strat.sram_used |
| 182 | return new_set |
| 183 | |
| 184 | def __eq__(self, other): |
| 185 | if (self.bws != other.bws).any(): |
| 186 | return False |
| 187 | if (self.macs != other.macs).any(): |
| 188 | return False |
| 189 | if (self.cycles != other.cycles).any(): |
| 190 | return False |
| 191 | if self.max_sram_used != other.max_sram_used: |
| 192 | return False |
| 193 | if self.total_sram_used != other.total_sram_used: |
| 194 | return False |
| 195 | if self.strats != other.strats: |
| 196 | return False |
| 197 | return True |
| 198 | |
| 199 | def __str__(self): |
| 200 | return "<scheduler.StrategySet: max_sram_used=%s passes_covered=%s>" % ( |
| 201 | self.max_sram_used, |
| 202 | list(ps.name for ps in self.strats), |
| 203 | ) |
| 204 | |
| 205 | __repr__ = __str__ |
| 206 | |
| 207 | |
| 208 | empty_strategy = Strategy( |
| 209 | SchedulingStrategy.Unknown, None, [], [], [], make_bandwidth_array(), make_macs_array(), make_cycles_array(), 0 |
| 210 | ) |
| 211 | INFINITY = 1e30 |
| 212 | |
| 213 | ABORT_SEARCH = [] |
| 214 | |
| 215 | |
| 216 | def flatten_list_of_lists(lstlst): |
| 217 | lst = [] |
| 218 | for v in lstlst: |
| 219 | lst.extend(v) |
| 220 | return lst |
| 221 | |
| 222 | |
| 223 | class DynamicProgrammingScheduler: |
| 224 | def __init__(self, nng, sg, arch, sram_limit, options: SchedulerOptions): |
| 225 | self.nng = nng |
| 226 | self.sg = sg |
| 227 | self.arch = arch |
| 228 | self.sram_limit = sram_limit |
| 229 | self.options = copy.copy(options) |
| 230 | self.use_cascading = options.use_cascading |
| 231 | |
| 232 | if self.arch.feature_map_storage_mem_area != MemArea.Sram: |
| 233 | self.use_ifm_ofm_overlap = False # force off IFM/OFM overlap if IFMs and OFMs are not in the SRAM |
| 234 | self.use_ifm_ofm_overlap = options.use_ifm_ofm_overlap |
| 235 | |
| 236 | self.verbose_schedule = options.verbose_schedule |
| 237 | self.verbose_pareto_frontier_schedules = options.verbose_pareto_frontier_schedules |
| 238 | self.mem_area = MemArea.Sram |
| 239 | |
| 240 | self.bandwidth_weights = arch.bandwidth_weights |
| 241 | self.cycles_weight = arch.cycles_weight |
| 242 | self.max_sram_used_weight = arch.max_sram_used_weight |
| 243 | |
| 244 | self.n_combinations_searched = 0 |
| 245 | |
| 246 | self.feature_maps_not_in_fast_storage = ( |
| 247 | arch.tensor_storage_mem_area[TensorPurpose.FeatureMap] != arch.fast_storage_mem_area |
| 248 | ) |
| 249 | |
| 250 | self.pareto_max_candidates = 16 |
| 251 | |
| 252 | self.ifm_stream_npu_blocks = set( |
| 253 | (NpuBlockType.ConvolutionMxN, NpuBlockType.ConvolutionDepthWise, NpuBlockType.Pooling,) |
| 254 | ) |
| 255 | |
| 256 | num_pareto_metrics = 4 |
| 257 | view_values = ",".join(["d"] * num_pareto_metrics) |
| 258 | order_values = ["f%d" % (idx,) for idx in range(num_pareto_metrics)] |
| 259 | |
| 260 | def pareto_metric(self, candidate): |
| 261 | strat, strat_set = candidate |
| 262 | total_cycles = strat.cycles[PassCycles.Total] + strat_set.cycles[PassCycles.Total] |
| 263 | bws = strat.bws + strat_set.bws |
| 264 | last_block_height = 0 |
| 265 | if self.options.pareto_metric == ParetoMetric.BwCycMemBlkH and len(strat.block_configs) > 0: |
| 266 | last_block_height = strat.block_configs[-1][0] |
| 267 | |
| 268 | return ( |
| 269 | np.tensordot(bws, self.bandwidth_weights, axes=3) + total_cycles * self.cycles_weight, |
| 270 | strat_set.max_sram_used, |
| 271 | strat.sram_used, |
| 272 | last_block_height, |
| 273 | ) |
| 274 | |
| 275 | def filter_pareto_frontier(self, candidates, remove_equally_good_candidates): |
| 276 | |
| 277 | candidates = [cand for cand in candidates if max(cand[0].sram_used, cand[1].max_sram_used) <= self.sram_limit] |
| 278 | |
| 279 | if len(candidates) <= 1: |
| 280 | return candidates |
| 281 | assert remove_equally_good_candidates |
| 282 | start = time.time() |
| 283 | pareto_vals = np.zeros((len(candidates), DynamicProgrammingScheduler.num_pareto_metrics)) |
| 284 | ids = np.arange(len(candidates), dtype=np.int32) |
| 285 | for idx, cand in enumerate(candidates): |
| 286 | pareto_vals[idx] = self.pareto_metric(cand) |
| 287 | |
| 288 | sort_order = np.argsort( |
| 289 | pareto_vals.view(DynamicProgrammingScheduler.view_values), |
| 290 | order=DynamicProgrammingScheduler.order_values, |
| 291 | axis=0, |
| 292 | kind="stable", |
| 293 | ).flatten() |
| 294 | pareto_vals = pareto_vals[sort_order] |
| 295 | ids = ids[sort_order] |
| 296 | |
| 297 | pareto_frontier = [] |
| 298 | while len(ids) > 0: |
| 299 | pareto_frontier.append(candidates[ids[0]]) |
| 300 | not_dominated_by_first = (pareto_vals < pareto_vals[0]).any(axis=1) |
| 301 | ids = ids[not_dominated_by_first] |
| 302 | pareto_vals = pareto_vals[not_dominated_by_first] |
| 303 | |
| 304 | if len(pareto_frontier) > self.pareto_max_candidates: |
| 305 | pareto_frontier = self.sort_by_candidate_metric(pareto_frontier) |
| 306 | pareto_frontier = pareto_frontier[: self.pareto_max_candidates] |
| 307 | |
| 308 | return pareto_frontier |
| 309 | |
| 310 | def candidate_metric(self, candidate): |
| 311 | strat, strat_set = candidate |
| 312 | max_sram_used = max(strat_set.max_sram_used, strat.sram_used) |
| 313 | bws = strat.bws + strat_set.bws |
| 314 | total_cycles = strat.cycles[PassCycles.Total] + strat_set.cycles[PassCycles.Total] |
| 315 | |
| 316 | return ( |
| 317 | max_sram_used * self.max_sram_used_weight |
| 318 | + np.tensordot(bws, self.bandwidth_weights, axes=3) |
| 319 | + total_cycles * self.cycles_weight |
| 320 | ) |
| 321 | |
| 322 | def sort_by_candidate_metric(self, candidate_list): |
| 323 | sorted_list = list(sorted(candidate_list, key=self.candidate_metric)) |
| 324 | return sorted_list |
| 325 | |
| 326 | def best_candidate(self, candidate_list): |
| 327 | if len(candidate_list) == 0: |
| 328 | return ABORT_SEARCH |
| 329 | if len(candidate_list) == 1: |
| 330 | return candidate_list[0] |
| 331 | sorted_list = self.sort_by_candidate_metric(candidate_list) |
| 332 | return sorted_list[0] |
| 333 | |
| 334 | def graduate_strat(self, strat_type, sram_used, old_strat_data): |
| 335 | res = [] |
| 336 | for old_strat, old_strat_set in old_strat_data: |
| 337 | if old_strat.sram_used + sram_used > self.sram_limit: |
| 338 | continue # This strategy is bad, drop it |
| 339 | if old_strat_set.max_sram_used > self.sram_limit: |
| 340 | continue # This strategy is bad, drop it |
| 341 | assert old_strat.strat == SchedulingStrategy.Unknown |
| 342 | |
| 343 | new_strat = old_strat.clone() |
| 344 | new_strat.strat = strat_type |
| 345 | new_strat.sram_used = old_strat.sram_used + sram_used |
| 346 | |
| 347 | if self.use_ifm_ofm_overlap: |
| 348 | overlap = calc_allowed_ofm_ifm_overlap_for_pass_list( |
| 349 | new_strat.strat, new_strat.passes, new_strat.block_configs |
| 350 | ) |
| 351 | new_strat.sram_used -= overlap |
| 352 | |
| 353 | new_strat_set = old_strat_set.clone_add_strategy(new_strat) |
| 354 | res.append((empty_strategy, new_strat_set)) |
| 355 | return self.filter_pareto_frontier(res, remove_equally_good_candidates=True) |
| 356 | |
| 357 | def append_sram(self, sram_used, old_strat_data): |
| 358 | res = [] |
| 359 | for old_strat, strat_set in old_strat_data: |
| 360 | assert old_strat.strat == SchedulingStrategy.Unknown |
| 361 | assert old_strat.sram_used == 0 |
| 362 | new_strat = old_strat.clone() |
| 363 | new_strat.sram_used = old_strat.sram_used + sram_used |
| 364 | |
| 365 | res.append((new_strat, strat_set)) |
| 366 | return res |
| 367 | |
| 368 | def append_sram_block_config_performance_metrics(self, sram_used, block_config, metrics, old_strat_data): |
| 369 | res = [] |
| 370 | for old_strat, strat_set in old_strat_data: |
| 371 | assert old_strat.strat == SchedulingStrategy.Unknown |
| 372 | new_strat = old_strat.clone() |
| 373 | bws, macs, cycles = metrics[:3] |
| 374 | |
| 375 | new_strat.sram_used = old_strat.sram_used + sram_used |
| 376 | new_strat.block_configs = old_strat.block_configs + [block_config] |
| 377 | new_strat.bws = old_strat.bws + bws |
| 378 | new_strat.macs = old_strat.macs + macs |
| 379 | new_strat.cycles = old_strat.cycles + cycles |
| 380 | new_strat.bws, new_strat.macs, new_strat.cycles = npu_performance.collate_stats_for_cascaded_pass( |
| 381 | self.arch, new_strat.bws, new_strat.macs, new_strat.cycles |
| 382 | ) |
| 383 | |
| 384 | res.append((new_strat, strat_set)) |
| 385 | return res |
| 386 | |
| 387 | def append_sram_pass_block_config_performance_metrics_rewrite_list( |
| 388 | self, sram_used, new_pass, block_config, metrics, rewrite_list, old_strat_data |
| 389 | ): |
| 390 | res = [] |
| 391 | for old_strat, strat_set in old_strat_data: |
| 392 | assert old_strat.strat == SchedulingStrategy.Unknown |
| 393 | new_strat = old_strat.clone() |
| 394 | bws, macs, cycles = metrics[:3] |
| 395 | new_strat.sram_used = old_strat.sram_used + sram_used |
| 396 | new_strat.block_configs = old_strat.block_configs + [block_config] |
| 397 | new_strat.bws = old_strat.bws + bws |
| 398 | new_strat.macs = old_strat.macs + macs |
| 399 | new_strat.cycles = old_strat.cycles + cycles |
| 400 | new_strat.passes = old_strat.passes + [new_pass] |
| 401 | new_strat.bws, new_strat.macs, new_strat.cycles = npu_performance.collate_stats_for_cascaded_pass( |
| 402 | self.arch, new_strat.bws, new_strat.macs, new_strat.cycles |
| 403 | ) |
| 404 | new_strat.rewrite_list = old_strat.rewrite_list + rewrite_list |
| 405 | res.append((new_strat, strat_set)) |
| 406 | return res |
| 407 | |
| 408 | def append_sram_rewrite_list(self, sram_used, rewrite_list, old_strat_data): |
| 409 | res = [] |
| 410 | for old_strat, strat_set in old_strat_data: |
| 411 | assert old_strat.strat == SchedulingStrategy.Unknown |
| 412 | new_strat = old_strat.clone() |
| 413 | new_strat.sram_used = old_strat.sram_used + sram_used |
| 414 | new_strat.rewrite_list = old_strat.rewrite_list + rewrite_list |
| 415 | res.append((new_strat, strat_set)) |
| 416 | return res |
| 417 | |
| 418 | def pass_to_strat(self, strat_data): |
| 419 | res = {} |
| 420 | for strat in strat_data[1].strats.values(): |
| 421 | for ps in strat.passes: |
| 422 | res[ps] = strat |
| 423 | return res |
| 424 | |
| 425 | def compatible_strats(self, a, b): |
| 426 | intersection = a.keys() & b.keys() |
| 427 | for k in intersection: |
| 428 | if a[k] != b[k]: |
| 429 | return False |
| 430 | return True |
| 431 | |
| 432 | def collate_strats_for_passes(self, all_passes): |
| 433 | if len(all_passes) == 0: |
| 434 | return [(empty_strategy, StrategySet(dict()))] |
| 435 | if len(all_passes) == 1: |
| 436 | return all_passes[0] # save some space in the common case |
| 437 | all_strands = [[self.pass_to_strat(strat_data) for strat_data in strand] for strand in all_passes] |
| 438 | prev_combos = [dict()] |
| 439 | for j, strand in enumerate(all_strands): |
| 440 | new_combos = [] |
| 441 | for i, alt in enumerate(strand): |
| 442 | for prev in prev_combos: |
| 443 | if self.compatible_strats(prev, alt): |
| 444 | cmb = dict(prev) |
| 445 | cmb.update(all_passes[j][i][1].strats) |
| 446 | new_combos.append(cmb) |
| 447 | prev_combos = new_combos |
| 448 | |
| 449 | res = [] |
| 450 | for d in prev_combos: |
| 451 | s = StrategySet(d) |
| 452 | s.update_statistics() |
| 453 | res.append((empty_strategy, s)) |
| 454 | return res |
| 455 | |
| 456 | def search_all_but_one_predecessor(self, ps, pred_pass, pred_pass_data): |
| 457 | # get the rest of the predecessors |
| 458 | other_predecessors = [pred for pred in ps.dag_predecessors if pred != pred_pass] |
| 459 | other_predecessor_data = self.search_pass_list(other_predecessors) |
| 460 | |
| 461 | # pred strat data has an incomplete strategy, which we need |
| 462 | # to continue on, whereas the other ones have completed strategies. |
| 463 | # we need to merge these, but keep the incomplete strategy too. |
| 464 | |
| 465 | res = [] |
| 466 | for pred_pass_strat, pred_pass_strat_set in pred_pass_data: |
| 467 | all_strats = [ |
| 468 | [(empty_strategy, pred_pass_strat_set)], # pred strat data but with a dummy empty strategy |
| 469 | other_predecessor_data, # this one is fine to use as-is |
| 470 | ] |
| 471 | collated_strat_data = self.collate_strats_for_passes(all_strats) |
| 472 | strat_data = [(pred_pass_strat, strat_set) for _, strat_set in collated_strat_data] |
| 473 | res.extend(strat_data) |
| 474 | return res |
| 475 | |
| 476 | def calc_non_local_mem_usage(self): |
| 477 | ignore_subgraph_input_output_tensors = self.sg.placement == PassPlacement.Cpu |
| 478 | range_set = live_range.extract_live_ranges_from_passes( |
| 479 | self.sg, |
| 480 | self.mem_area, |
| 481 | mark_output_tensors_overlapping_with_input_tensors=True, |
| 482 | ignore_subgraph_input_output_tensors=ignore_subgraph_input_output_tensors, |
| 483 | ) |
| 484 | range_dict = range_set.ranges |
| 485 | |
| 486 | # find which ranges overlap passes but aren't input/outputs of the passes. |
| 487 | # these won't be counted by the dynamic programming search and must be counted in manually. |
| 488 | end_pos = max(ps.time for ps in self.sg.passes) + 2 |
| 489 | mem_usage = np.zeros(end_pos) + self.sg.base_sram_used |
| 490 | non_local_mem_usage = np.zeros(end_pos, dtype=np.int64) |
| 491 | |
| 492 | for tens, rng in range_dict.items(): |
| 493 | storage_size = tens.storage_size() |
| 494 | assert tens.mem_area == self.mem_area |
| 495 | mem_usage[rng.start_time : rng.end_time] += storage_size |
| 496 | |
| 497 | for ps in self.sg.passes: |
| 498 | local_mem_usage = 0 |
| 499 | for tens in ps.inputs + ps.outputs + ps.intermediates: |
| 500 | if tens.mem_area != self.mem_area: |
| 501 | continue |
| 502 | |
| 503 | local_mem_usage += tens.storage_size() |
| 504 | |
| 505 | non_local_mem_usage[ps.time] = mem_usage[ps.time] - local_mem_usage |
| 506 | |
| 507 | self.non_local_mem_usage = non_local_mem_usage |
| 508 | |
| 509 | def search(self): |
| 510 | self.calc_non_local_mem_usage() |
| 511 | starting_passes = [ps for ps in self.sg.passes if not ps.successors] |
| 512 | strat_data = self.search_pass_list(starting_passes) |
| 513 | |
| 514 | _, best_set = self.best_candidate(strat_data) |
| 515 | |
| 516 | if self.verbose_pareto_frontier_schedules: |
| 517 | print( |
| 518 | "Scheduler searched %d combinations and found %d candidate schedules along the pareto frontier" |
| 519 | % (self.n_combinations_searched, len(strat_data,)) |
| 520 | ) |
| 521 | for idx, (_, strat_set) in enumerate(strat_data): |
| 522 | extra = "" |
| 523 | if strat_set == best_set: |
| 524 | extra = "(Best candidate)" |
| 525 | print("Candidate", idx, extra) |
| 526 | memory_used = {MemArea.Sram: strat_set.max_sram_used} |
| 527 | stats_writer.print_performance_metrics_for_strat( |
| 528 | self.arch, |
| 529 | "", |
| 530 | strat_set.cycles, |
| 531 | strat_set.macs, |
| 532 | strat_set.bws, |
| 533 | self.nng.batch_size, |
| 534 | memory_used, |
| 535 | len(self.sg.passes), |
| 536 | len(strat_set.strats), |
| 537 | ) |
| 538 | |
| 539 | return best_set |
| 540 | |
| 541 | def search_pass_list(self, pass_list): |
| 542 | all_strats = [] |
| 543 | for ps in pass_list: |
| 544 | strat = self.search_output(ps) |
| 545 | all_strats.append(strat) |
| 546 | strat_data = self.collate_strats_for_passes(all_strats) |
| 547 | for strd in strat_data: |
| 548 | for ps in pass_list: |
| 549 | assert ps in strd[1].strats # should have strategies for everything we asked to search |
| 550 | return strat_data |
| 551 | |
| 552 | def search_predecessors(self, ps): |
| 553 | |
| 554 | # protect against graphs with loops. collate_strats_for_passes will sort this out later so that |
| 555 | # we have strats for all passes |
| 556 | |
| 557 | pass_list = ps.dag_predecessors |
| 558 | strat_data = self.search_pass_list(pass_list) |
| 559 | |
| 560 | return strat_data |
| 561 | |
| 562 | @lru_cache(maxsize=None) |
| 563 | def search_output(self, ps): |
| 564 | |
| 565 | assert ps in self.sg.passes |
| 566 | candidate_list = [] |
| 567 | |
| 568 | candidate_list.extend(self.search_weight_streaming_output(ps)) |
| 569 | |
| 570 | if self.options.use_ifm_streaming: |
| 571 | candidate_list.extend(self.search_ifm_streaming_output(ps)) |
| 572 | |
| 573 | best = self.filter_pareto_frontier(candidate_list, remove_equally_good_candidates=True) |
| 574 | |
| 575 | if not best: |
| 576 | print( |
| 577 | "Warning: Dynamic search programming algorithm failed for pass %s, invoking fallback strategy" |
| 578 | % (ps.name,) |
| 579 | ) |
| 580 | return self.search_predecessors(ps) |
| 581 | |
| 582 | return best |
| 583 | |
| 584 | def search_ifm_streaming_output(self, ps): |
| 585 | if ps.placement != PassPlacement.Npu: |
| 586 | return ABORT_SEARCH |
| 587 | if ps.npu_block_type not in self.ifm_stream_npu_blocks: |
| 588 | return ABORT_SEARCH |
| 589 | strat_data = self.search_ifm_streaming_body(ps, False) |
| 590 | |
| 591 | sram_used = self.non_local_mem_usage[ps.time] |
| 592 | for tens in ps.outputs: |
| 593 | if tens.mem_area == self.mem_area: |
| 594 | sram_used += tens.storage_size() |
| 595 | |
| 596 | return self.graduate_strat(SchedulingStrategy.IfmStream, sram_used, strat_data) |
| 597 | |
| 598 | @lru_cache(maxsize=None) |
| 599 | def search_ifm_streaming_body(self, ps, force_outputs_to_fast_storage): |
| 600 | if ps.placement != PassPlacement.Npu: |
| 601 | return ABORT_SEARCH |
| 602 | if ps.npu_block_type not in self.ifm_stream_npu_blocks: |
| 603 | return ABORT_SEARCH |
| 604 | ifm_input_search_resuls = self.search_ifm_streaming_input(ps) |
| 605 | res = [] |
| 606 | |
| 607 | base_sram_used = 0 |
| 608 | for tens in ps.intermediates: |
| 609 | if tens.mem_area == self.mem_area: |
| 610 | base_sram_used += tens.storage_size() |
| 611 | |
| 612 | all_block_configs = self.get_block_configs(ps) |
| 613 | for block_config in all_block_configs: |
| 614 | all_strats = [] |
| 615 | |
| 616 | if self.use_cascading: |
| 617 | all_strats.extend(self.search_ifm_streaming_partial(ps, block_config)) |
| 618 | |
| 619 | all_strats.extend(ifm_input_search_resuls) |
| 620 | |
| 621 | rewrite_list = [] |
| 622 | sram_used = base_sram_used |
| 623 | |
| 624 | metrics = npu_performance.performance_metrics_for_pass( |
| 625 | self.arch, |
| 626 | ps, |
| 627 | block_config, |
| 628 | rewrite_list=rewrite_list, |
| 629 | force_outputs_to_fast_storage=force_outputs_to_fast_storage, |
| 630 | ) |
| 631 | |
| 632 | res.extend( |
| 633 | self.append_sram_pass_block_config_performance_metrics_rewrite_list( |
| 634 | sram_used, ps, block_config, metrics, rewrite_list, all_strats |
| 635 | ) |
| 636 | ) |
| 637 | |
| 638 | self.n_combinations_searched += len(res) |
| 639 | res = self.filter_pareto_frontier(res, remove_equally_good_candidates=True) |
| 640 | return res |
| 641 | |
| 642 | def search_ifm_streaming_partial(self, ps, block_config): |
| 643 | if ps.placement != PassPlacement.Npu: |
| 644 | return ABORT_SEARCH |
| 645 | |
| 646 | if len(ps.inputs) < 1: |
| 647 | return ABORT_SEARCH |
| 648 | |
| 649 | ifm_tensor = ps.ifm_tensor |
| 650 | |
| 651 | if ifm_tensor is None: |
| 652 | return ABORT_SEARCH |
| 653 | if ifm_tensor.purpose != TensorPurpose.FeatureMap: |
| 654 | return ABORT_SEARCH |
| 655 | if not ifm_tensor.storage_shape or len(ifm_tensor.storage_shape) != 4: |
| 656 | return ABORT_SEARCH |
| 657 | |
| 658 | pred_pass_list = [] |
| 659 | for pred_candidate in ps.dag_predecessors: |
| 660 | if len(pred_candidate.outputs) == 1 and pred_candidate.outputs[0] == ifm_tensor: |
| 661 | # we found a predecessor that produces this IFM tensor |
| 662 | if len(pred_candidate.successors) == 1 and pred_candidate.successors[0] == ps: |
| 663 | # and it only has one successor, namely us |
| 664 | if pred_candidate.placement == PassPlacement.Npu: |
| 665 | if pred_candidate.npu_block_type in self.ifm_stream_npu_blocks: |
| 666 | # and it is on the Npu and fusable - it's a candidate |
| 667 | pred_pass_list.append(pred_candidate) |
| 668 | |
| 669 | if not pred_pass_list: |
| 670 | return ABORT_SEARCH |
| 671 | |
| 672 | all_candidates = [] |
| 673 | for pred_pass in pred_pass_list: |
| 674 | # recurse into the next pass |
| 675 | ifm_strat_data = self.search_ifm_streaming_body(pred_pass, self.feature_maps_not_in_fast_storage) |
| 676 | |
| 677 | strat_data = self.search_all_but_one_predecessor(ps, pred_pass, ifm_strat_data) |
| 678 | for strat_opt in strat_data: |
| 679 | |
| 680 | pred_pass_block_config = strat_opt[0].block_configs[-1] |
| 681 | rolling_buffer_dims = npu_performance.rolling_buffer_dims_from_passes( |
| 682 | self.arch, pred_pass, pred_pass_block_config, ps, block_config |
| 683 | ) |
| 684 | if rolling_buffer_dims is None: |
| 685 | continue # this does not pack properly, skip it. |
| 686 | |
| 687 | sram_used = 0 |
| 688 | for tens in ps.inputs: |
| 689 | if tens != ifm_tensor: |
| 690 | if tens.mem_area == self.mem_area: |
| 691 | sram_used += tens.storage_size() |
| 692 | |
| 693 | rolling_buffer_y, rolling_buffer_x = rolling_buffer_dims |
| 694 | |
| 695 | rewrite_list = [ |
| 696 | ( |
| 697 | SchedulerRewrite.ChangeTensorSubPurpose, |
| 698 | ifm_tensor, |
| 699 | TensorSubPurpose.RollingBufferY, |
| 700 | rolling_buffer_y, |
| 701 | None, |
| 702 | ps, |
| 703 | ) |
| 704 | ] |
| 705 | sram_used += ifm_tensor.storage_size_for_sub_purpose( |
| 706 | TensorSubPurpose.RollingBufferY, rolling_buffer_y, None |
| 707 | ) |
| 708 | |
| 709 | all_candidates.extend(self.append_sram_rewrite_list(sram_used, rewrite_list, [strat_opt])) |
| 710 | |
| 711 | self.n_combinations_searched += len(all_candidates) |
| 712 | return all_candidates |
| 713 | |
| 714 | def get_block_configs(self, ps): |
| 715 | if ps.placement != PassPlacement.Npu: |
| 716 | return [(1, 1, 1, 1)] # default |
| 717 | |
| 718 | block_configs = find_block_configs_suitable_for_pass_and_shared_buffer(self.arch, ps) |
| 719 | |
| 720 | # Take a limited number of the largest blocks |
| 721 | if self.arch.block_config_limit > 0: |
| 722 | # Sort by block area, followed by depth |
| 723 | block_configs.sort(key=lambda cfg: (cfg[0] * cfg[1]) << 8 | cfg[3], reverse=True) |
| 724 | bound = min(len(block_configs), self.arch.block_config_limit) |
| 725 | # We take 'n' from the fat end of the list, and 'n' from the thin end of the list. |
| 726 | tmp = block_configs[:bound] |
| 727 | tmp.extend(block_configs[max(bound, len(block_configs) - bound) :]) |
| 728 | block_configs = tmp |
| 729 | |
| 730 | return block_configs |
| 731 | |
| 732 | def search_ifm_streaming_input(self, ps): |
| 733 | sram_used = 0 |
| 734 | for tens in ps.inputs: |
| 735 | if tens.mem_area == self.mem_area: |
| 736 | sram_used += tens.storage_size() |
| 737 | |
| 738 | return self.append_sram(sram_used, self.search_predecessors(ps)) |
| 739 | |
| 740 | def search_weight_streaming_output(self, ps): |
| 741 | strat_data = self.search_weight_streaming_body(ps) |
| 742 | |
| 743 | sram_used = self.non_local_mem_usage[ps.time] |
| 744 | for tens in ps.outputs: |
| 745 | if tens.mem_area == self.mem_area: |
| 746 | sram_used += tens.storage_size() |
| 747 | |
| 748 | return self.graduate_strat(SchedulingStrategy.WeightStream, sram_used, strat_data) |
| 749 | |
| 750 | @lru_cache(maxsize=None) |
| 751 | def search_weight_streaming_body(self, ps): |
| 752 | |
| 753 | strat_data = self.search_weight_streaming_input(ps) |
| 754 | |
| 755 | res = [] |
| 756 | |
| 757 | all_block_configs = self.get_block_configs(ps) |
| 758 | |
| 759 | for block_config in all_block_configs: |
| 760 | |
| 761 | sram_used = 0 |
| 762 | rewrite_list = [] |
| 763 | |
| 764 | for tens in ps.intermediates: |
| 765 | if tens.mem_area == self.mem_area: |
| 766 | if tens.purpose == TensorPurpose.Weights: |
| 767 | sram_used += tens.storage_size_for_sub_purpose( |
| 768 | TensorSubPurpose.DoubleBuffer, block_config[3] |
| 769 | ) |
| 770 | rewrite_list.append( |
| 771 | ( |
| 772 | SchedulerRewrite.ChangeTensorSubPurpose, |
| 773 | tens, |
| 774 | TensorSubPurpose.DoubleBuffer, |
| 775 | block_config[3], |
| 776 | None, |
| 777 | ps, |
| 778 | ) |
| 779 | ) |
| 780 | else: |
| 781 | sram_used += tens.storage_size() |
| 782 | |
| 783 | metrics = npu_performance.performance_metrics_for_pass( |
| 784 | self.arch, ps, block_config, rewrite_list=rewrite_list |
| 785 | ) |
| 786 | |
| 787 | res.extend( |
| 788 | self.append_sram_pass_block_config_performance_metrics_rewrite_list( |
| 789 | sram_used, ps, block_config, metrics, rewrite_list, strat_data |
| 790 | ) |
| 791 | ) |
| 792 | |
| 793 | self.n_combinations_searched += len(res) |
| 794 | res = self.filter_pareto_frontier(res, remove_equally_good_candidates=True) |
| 795 | return res |
| 796 | |
| 797 | def search_weight_streaming_input(self, ps): |
| 798 | sram_used = 0 |
| 799 | for tens in ps.inputs: |
| 800 | if tens.mem_area == self.mem_area: |
| 801 | sram_used += tens.storage_size() |
| 802 | |
| 803 | return self.append_sram(sram_used, self.search_predecessors(ps)) |
| 804 | |
| 805 | def apply_result(self, strat_set, arch): |
| 806 | pass_to_cascaded_pass = dict() |
| 807 | for _, strat in strat_set.strats.items(): |
| 808 | # rewrite the tensors that need this first. e.g. make rolling buffers |
| 809 | inputs = [] |
| 810 | intermediates = [] |
| 811 | outputs = [] |
| 812 | |
| 813 | for ps in strat.passes: |
| 814 | inputs += ps.inputs |
| 815 | intermediates += ps.intermediates |
| 816 | outputs += ps.outputs |
| 817 | |
| 818 | for tens in set(inputs) & set(outputs): |
| 819 | # tensors that are in both sets are intermediates |
| 820 | |
| 821 | # find pass with input/output tensor, and check if they are both placed on NPU |
| 822 | input_placement = None |
| 823 | output_placement = None |
| 824 | for ps in strat.passes: |
| 825 | if tens in ps.inputs: |
| 826 | input_placement = ps.placement |
| 827 | if tens in ps.outputs: |
| 828 | output_placement = ps.placement |
| 829 | if input_placement == output_placement == PassPlacement.Npu: |
| 830 | tens.set_format(TensorFormat.NHCWB16, arch) |
| 831 | |
| 832 | intermediates.append(tens) |
| 833 | inputs.remove(tens) |
| 834 | outputs.remove(tens) |
| 835 | |
| 836 | for rewrite_op, tens, sub_purpose, param_a, param_b, ps in strat.rewrite_list: |
| 837 | if rewrite_op == SchedulerRewrite.ChangeTensorSubPurpose: |
| 838 | tens.mem_area = self.arch.fast_storage_mem_area |
| 839 | tens.set_new_sub_purpose(sub_purpose, param_a, param_b) |
| 840 | else: |
| 841 | assert 0, "unknown rewrite_op " + str(rewrite_op) |
| 842 | |
| 843 | is_element_wise = True |
| 844 | for ps in strat.passes: |
| 845 | assert ps.placement == strat.passes[0].placement |
| 846 | if not ps.is_element_wise: |
| 847 | is_element_wise = False |
| 848 | break |
| 849 | |
| 850 | cascaded_pass = CascadedPass( |
| 851 | strat.passes[0].name, |
| 852 | strat.strat, |
| 853 | inputs, |
| 854 | intermediates, |
| 855 | outputs, |
| 856 | strat.passes, |
| 857 | strat.passes[0].placement, |
| 858 | is_element_wise, |
| 859 | ) |
| 860 | assert strat.sram_used >= 0 |
| 861 | cascaded_pass.sram_used = strat.sram_used |
| 862 | |
| 863 | for idx, ps in enumerate(strat.passes): |
| 864 | assert ps not in pass_to_cascaded_pass |
| 865 | pass_to_cascaded_pass[ps] = cascaded_pass |
| 866 | ps.cascade = cascaded_pass |
| 867 | ps.block_config = strat.block_configs[idx] |
| 868 | |
| 869 | if ps.placement == PassPlacement.Npu: |
| 870 | ps.shared_buffer = shared_buffer_allocation_for_pass_and_block_config( |
| 871 | self.arch, ps, ps.block_config |
| 872 | ) |
| 873 | assert ps.shared_buffer is not None |
| 874 | |
| 875 | for op in ps.ops: |
| 876 | subgraph = op.attrs.get("subgraph") |
| 877 | if subgraph: |
| 878 | subgraph.base_sram_used = cascaded_pass.sram_used |
| 879 | |
| 880 | # all passes should have a cascaded pass now |
| 881 | if len(pass_to_cascaded_pass) != len(self.sg.passes): |
| 882 | print( |
| 883 | "mismatch: we have %d passes, but only %d have cascaded passes associated" |
| 884 | % (len(self.sg.passes), len(pass_to_cascaded_pass)) |
| 885 | ) |
| 886 | for ps in self.sg.passes: |
| 887 | if not ps in pass_to_cascaded_pass: |
| 888 | print("%3d pass missing cascaded pass %s" % (ps.time, ps)) |
| 889 | |
| 890 | assert len(pass_to_cascaded_pass) == len(self.sg.passes) |
| 891 | # we have all the passes, but we need to put them in order and build predecessor/successor links. |
| 892 | |
| 893 | visit_pass_set = set() |
| 894 | cascaded_passes = [] |
| 895 | |
| 896 | def visit_pass(ps): |
| 897 | if ps in visit_pass_set: |
| 898 | return |
| 899 | visit_pass_set.add(ps) |
| 900 | |
| 901 | cps = ps.cascade |
| 902 | dont_traverse = set(cps.passes) |
| 903 | |
| 904 | for ps in cps.passes: |
| 905 | for pred in ps.predecessors: |
| 906 | if pred in dont_traverse: |
| 907 | continue |
| 908 | visit_pass(pred) |
| 909 | |
| 910 | cascaded_passes.append(cps) |
| 911 | |
| 912 | starting_passes = [ps for ps in self.sg.passes if not ps.successors] |
| 913 | for ps in starting_passes: |
| 914 | visit_pass(ps) |
| 915 | |
| 916 | # reorder so startup init cascaded passes come first |
| 917 | def is_startup_cascaded_pass(cps): |
| 918 | if not cps.passes: |
| 919 | return False |
| 920 | return cps.placement == PassPlacement.StartupInit |
| 921 | |
| 922 | cascaded_passes = [cps for cps in cascaded_passes if is_startup_cascaded_pass(cps)] + [ |
| 923 | cps for cps in cascaded_passes if not is_startup_cascaded_pass(cps) |
| 924 | ] |
| 925 | |
| 926 | self.sg.cascaded_passes = cascaded_passes |
| 927 | self.sg.build_cascaded_pass_links() |
| 928 | |
Patrik Gustavsson | feeb06d | 2020-04-22 12:53:47 +0200 | [diff] [blame^] | 929 | # Check if NHCWB16 can be used in between cascaded passes |
| 930 | # (NHCWB16 within cascaded passes has been handled earlier in this function) |
| 931 | if self.sg.placement == PassPlacement.Npu: |
| 932 | for ps in self.sg.cascaded_passes: |
| 933 | if ps.placement != PassPlacement.Npu: |
| 934 | continue |
| 935 | for output in ps.outputs: |
| 936 | if output.purpose != TensorPurpose.FeatureMap: |
| 937 | continue |
| 938 | |
| 939 | use_NHCWB16 = True |
| 940 | for op in output.consumer_list: |
| 941 | if op == None or op.type == 'Reshape': |
| 942 | use_NHCWB16 = False |
| 943 | else: |
| 944 | use_NHCWB16 &= op.run_on_npu |
| 945 | |
| 946 | if use_NHCWB16: |
| 947 | output.set_format(TensorFormat.NHCWB16, arch) |
| 948 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 949 | |
| 950 | def schedule_passes(nng, arch, options: SchedulerOptions): |
| 951 | |
| 952 | for sg in nng.subgraphs: |
| 953 | sg.base_sram_used = 0 |
| 954 | |
| 955 | for sg in nng.subgraphs: |
| 956 | # re-entering the same nodes from different contexts requires us to |
| 957 | # build a simplified directed acyclic (DAG) version of the graph to |
| 958 | # use for traversal, rather than using a visit dictionary. this avoids |
| 959 | # recursing infinitely due to loops. |
| 960 | sg.build_pass_dag_predecessors() |
| 961 | |
| 962 | dps = DynamicProgrammingScheduler(nng, sg, arch, arch.sram_size, options) |
| 963 | |
| 964 | strat_set = dps.search() |
| 965 | |
| 966 | dps.apply_result(strat_set, arch) |
| 967 | |
| 968 | if options.verbose_schedule: |
| 969 | sg.print_cascaded_passes() |