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