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 | # Generate a high-level command stream from a scheduled subgraph with CascadedPasses. |
| 18 | # |
| 19 | # Also used during scheduling to work out allowable IFM/OFM overlap, this functionality can be accessed using |
| 20 | # calc_allowed_ofm_ifm_overlap_for_cascaded_pass(). |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 21 | from .high_level_command_stream import Box |
| 22 | from .high_level_command_stream import DMA |
| 23 | from .high_level_command_stream import NpuStripe |
| 24 | from .nn_graph import PassPlacement |
| 25 | from .nn_graph import SchedulingStrategy |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 26 | from .operation import NpuBlockType |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 27 | from .tensor import TensorPurpose |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 28 | |
| 29 | |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 30 | def dma_if_necessary(ps, box, tensor): |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 31 | if tensor.needs_dma(): |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 32 | dma_op = tensor.ops[0] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 33 | in_tensor = dma_op.inputs[0] |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 34 | yield DMA(in_tensor, tensor, box) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 35 | |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 36 | |
Charles Xu | 600351a | 2020-05-18 08:54:47 +0200 | [diff] [blame] | 37 | def match_tensor(source, derived): |
| 38 | if source == derived: |
| 39 | return True |
| 40 | ops = derived.ops |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 41 | return ops != [] and len(ops) == 1 and ops[0].type == "SplitSliceRead" and source == ops[0].inputs[0] |
| 42 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 43 | |
| 44 | def generate_high_level_command_stream_for_pass(strat, passes, block_configs, idx): |
| 45 | is_first = idx == 0 |
| 46 | is_last = idx == len(passes) - 1 |
| 47 | ps = passes[idx] |
| 48 | block_config = block_configs[idx] |
Charles Xu | 600351a | 2020-05-18 08:54:47 +0200 | [diff] [blame] | 49 | npu_block_type = ps.npu_block_type |
| 50 | split_offsets = [None, None] # offset for [ifm, ifm2] |
| 51 | |
| 52 | ifm_idx = 0 |
| 53 | for op in ps.ops: |
| 54 | if op.type == "SplitSliceRead": |
| 55 | split_offsets[ifm_idx] = op.attrs["split_start"] |
| 56 | ps.primary_op.attrs["fused_memory_function"] = op.type |
| 57 | ifm_idx += 1 |
| 58 | |
| 59 | if len(ps.inputs) == 2 and npu_block_type == NpuBlockType.ElementWise: |
| 60 | # Ensure correct imf and ifm2 order |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 61 | if match_tensor(ps.inputs[0], ps.primary_op.inputs[1]) and match_tensor(ps.inputs[1], ps.primary_op.inputs[0]): |
Charles Xu | 600351a | 2020-05-18 08:54:47 +0200 | [diff] [blame] | 62 | ps.ifm_tensor, ps.ifm2_tensor = ps.ifm2_tensor, ps.ifm_tensor |
| 63 | split_offsets[0], split_offsets[1] = split_offsets[1], split_offsets[0] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 64 | |
| 65 | ifm_tensor = ps.ifm_tensor |
| 66 | ifm2_tensor = ps.ifm2_tensor |
| 67 | ofm_tensor = ps.ofm_tensor |
| 68 | weight_tensor = ps.weight_tensor |
| 69 | scale_tensor = ps.scale_tensor |
| 70 | |
| 71 | ofm_start = [0] * len(ofm_tensor.shape) |
| 72 | ofm_end = list(ofm_tensor.shape) |
| 73 | |
| 74 | strides = None |
| 75 | skirt = None |
Jacob Bohlin | 611fcdf | 2020-06-11 15:09:57 +0200 | [diff] [blame] | 76 | upscaling = 1 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 77 | if ps.primary_op is not None: |
| 78 | strides = ps.primary_op.attrs.get("strides", None) |
| 79 | skirt = ps.primary_op.attrs.get("skirt", None) |
Jacob Bohlin | 611fcdf | 2020-06-11 15:09:57 +0200 | [diff] [blame] | 80 | if ps.primary_op.type in set(("Conv2DBackpropInputSwitchedBias", "ResizeBilinear")): |
| 81 | upscaling = ofm_tensor.shape[-3] // ifm_tensor.shape[-3] |
| 82 | assert ofm_tensor.shape[-2] == (ifm_tensor.shape[-2] * upscaling) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 83 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 84 | concat_axis = 0 |
| 85 | concat_offset = 0 |
| 86 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 87 | # Fusable activation functions |
| 88 | activation_ops = set(("Sigmoid", "Tanh", "Relu", "Relu6", "ReluN1To1")) |
| 89 | |
| 90 | for op in ps.ops: |
| 91 | if op.type == "ConcatSliceWrite": |
| 92 | concat_axis = op.attrs["concat_axis"] |
| 93 | concat_start = op.attrs["concat_start"] |
| 94 | concat_end = op.attrs["concat_end"] |
| 95 | |
| 96 | ofm_start[concat_axis] = concat_start |
| 97 | ofm_end[concat_axis] = concat_end |
| 98 | concat_offset = concat_start |
| 99 | ps.primary_op.attrs["fused_memory_function"] = op.type |
| 100 | elif op.type in activation_ops: |
| 101 | ps.primary_op.attrs["fused_activation_function"] = op.type |
| 102 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 103 | if strat == SchedulingStrategy.WeightStream: |
| 104 | ofm_step = block_config[-1] |
| 105 | ofm_stop = ofm_end[-1] |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 106 | if weight_tensor is None or not weight_tensor.needs_dma(): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 107 | ofm_step = ofm_stop |
| 108 | for start in range(ofm_start[-1], ofm_stop, ofm_step): |
| 109 | end = min(start + ofm_step, ofm_stop) |
| 110 | ofm_start[-1] = start |
| 111 | ofm_end[-1] = end |
| 112 | ofm_box = Box(ofm_start, ofm_end) |
| 113 | ifm_box = None |
| 114 | ifm2_box = None |
| 115 | |
| 116 | if ifm_tensor.shape != []: |
| 117 | ifm_box, _, _ = ofm_box.transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 118 | strides, |
| 119 | skirt, |
| 120 | ifm_tensor.shape, |
| 121 | npu_block_type, |
| 122 | concat_axis, |
| 123 | concat_offset, |
| 124 | split_offsets[0], |
| 125 | upscaling, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 126 | ) |
| 127 | else: |
| 128 | ifm_box = Box([], []) |
| 129 | if ifm2_tensor is not None and ifm2_tensor.shape != []: |
| 130 | ifm2_box, _, _ = ofm_box.transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 131 | strides, |
| 132 | skirt, |
| 133 | ifm2_tensor.shape, |
| 134 | npu_block_type, |
| 135 | concat_axis, |
| 136 | concat_offset, |
| 137 | split_offsets[1], |
| 138 | upscaling, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 139 | ) |
| 140 | else: |
| 141 | ifm2_box = Box([], []) |
| 142 | |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 143 | for intermediate in ps.intermediates: |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 144 | if ( |
| 145 | intermediate is not None |
| 146 | and intermediate.shape != [] |
| 147 | and intermediate.purpose == TensorPurpose.FeatureMap |
| 148 | ): |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 149 | intermediate_box, _, _ = ofm_box.transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 150 | strides, |
| 151 | skirt, |
| 152 | intermediate.shape, |
| 153 | npu_block_type, |
| 154 | concat_axis, |
| 155 | concat_offset, |
| 156 | split_offsets[0], |
| 157 | upscaling, |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 158 | ) |
| 159 | yield from dma_if_necessary(ps, intermediate_box, intermediate) |
| 160 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 161 | weight_box = None |
| 162 | if weight_tensor is not None: |
| 163 | weight_oc_start = start |
| 164 | weight_oc_end = end |
| 165 | if concat_axis - len(weight_tensor.shape) == -1: |
| 166 | weight_oc_start -= concat_offset |
| 167 | weight_oc_end -= concat_offset |
| 168 | |
| 169 | weight_box = Box.make_weight_box( |
| 170 | weight_tensor.shape, |
| 171 | npu_block_type, |
| 172 | weight_oc_start, |
| 173 | weight_oc_end, |
| 174 | weight_tensor.weight_transpose_depthwise, |
| 175 | ) |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 176 | yield from dma_if_necessary(ps, weight_box, weight_tensor) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 177 | |
| 178 | yield NpuStripe( |
| 179 | ps, |
| 180 | block_config, |
| 181 | is_first, |
| 182 | is_last, |
| 183 | True, |
| 184 | True, |
| 185 | ifm_tensor, |
| 186 | ifm_box, |
| 187 | ofm_tensor, |
| 188 | ofm_box, |
| 189 | weight_tensor, |
| 190 | weight_box, |
| 191 | scale_tensor, |
| 192 | concat_axis, |
| 193 | concat_offset, |
| 194 | ifm2_tensor=ifm2_tensor, |
| 195 | ifm2_box=ifm2_box, |
| 196 | ) |
| 197 | |
| 198 | elif strat == SchedulingStrategy.IfmStream: |
| 199 | y_step = block_config[0] |
| 200 | y_start = 0 |
| 201 | y_dim = 1 |
| 202 | if len(ofm_tensor.shape) >= 3: |
| 203 | y_start = ofm_start[-3] |
| 204 | y_dim = ofm_end[-3] |
| 205 | if idx > 0: |
| 206 | ifm_y_present = 0 |
| 207 | prev_pass = passes[idx - 1] |
| 208 | prev_pass_gen = generate_high_level_command_stream_for_pass(strat, passes, block_configs, idx - 1) |
| 209 | else: |
| 210 | ifm_y_present = 1 |
| 211 | if len(ifm_tensor.shape) >= 3: |
| 212 | ifm_y_present = ifm_tensor.shape[-3] |
| 213 | prev_pass_gen = [] |
| 214 | prev_pass = None |
| 215 | |
| 216 | if len(passes) == 1: |
| 217 | # no cascading, can just issue one big stripe |
| 218 | # but only if we've done allocation and OFM does not overlap IFM |
| 219 | if ifm_tensor.address != -1 and ofm_tensor.address != -1: |
| 220 | if ( |
| 221 | ifm_tensor.address + ifm_tensor.storage_size() <= ofm_tensor.address |
| 222 | or ofm_tensor.address + ofm_tensor.storage_size() <= ifm_tensor.address |
| 223 | ): |
| 224 | y_step = y_dim |
| 225 | |
| 226 | weight_box = None |
| 227 | |
| 228 | for start in range(y_start, y_dim, y_step): |
| 229 | end = min(start + y_step, y_dim) |
| 230 | if len(ofm_tensor.shape) >= 3: |
| 231 | ofm_start[-3] = start |
| 232 | ofm_end[-3] = end |
| 233 | ofm_box = Box(ofm_start, ofm_end) |
| 234 | |
| 235 | k_height = 1 |
| 236 | if npu_block_type == NpuBlockType.Pooling: |
| 237 | if ps.primary_op is not None: |
| 238 | k_height = ps.primary_op.attrs["ksize"][1] |
| 239 | else: |
| 240 | if weight_tensor is not None: |
| 241 | k_height = weight_tensor.shape[0] |
| 242 | |
| 243 | ifm_box, pad_top, pad_bottom = ofm_box.transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 244 | strides, |
| 245 | skirt, |
| 246 | ifm_tensor.shape, |
| 247 | npu_block_type, |
| 248 | concat_axis, |
| 249 | concat_offset, |
| 250 | split_offsets[0], |
| 251 | k_height, |
| 252 | upscaling, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 253 | ) |
| 254 | |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 255 | for intermediate in ps.intermediates: |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 256 | if ( |
| 257 | intermediate is not None |
| 258 | and intermediate.shape != [] |
| 259 | and intermediate.purpose == TensorPurpose.FeatureMap |
| 260 | ): |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 261 | intermediate_box, _, _ = ofm_box.transform_with_strides_and_skirt( |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame^] | 262 | strides, |
| 263 | skirt, |
| 264 | intermediate.shape, |
| 265 | npu_block_type, |
| 266 | concat_axis, |
| 267 | concat_offset, |
| 268 | split_offsets[0], |
| 269 | upscaling, |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 270 | ) |
| 271 | yield from dma_if_necessary(ps, intermediate_box, intermediate) |
| 272 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 273 | ifm_y_needed = 1 |
| 274 | if len(ifm_box.end_coord) >= 3: |
| 275 | ifm_y_needed = ifm_box.end_coord[-3] |
| 276 | if ifm_y_present < ifm_y_needed: |
| 277 | for prev_cmd in prev_pass_gen: |
| 278 | yield prev_cmd |
| 279 | rng = prev_cmd.get_ofm_y_range_for_pass(prev_pass) |
| 280 | if rng is not None: |
| 281 | ifm_y_present = max(ifm_y_present, rng[1]) |
| 282 | if ifm_y_present >= ifm_y_needed: |
| 283 | break |
| 284 | |
| 285 | if weight_tensor is not None and weight_box is None: |
| 286 | weight_box = Box.make_weight_box( |
| 287 | weight_tensor.shape, npu_block_type, weights_transposed=weight_tensor.weight_transpose_depthwise |
| 288 | ) |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 289 | yield from dma_if_necessary(ps, weight_box, weight_tensor) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 290 | |
| 291 | # Check if first/last stripe in pass |
| 292 | is_first_h_stripe = start == y_start |
| 293 | is_last_h_stripe = (start + y_step) >= y_dim |
| 294 | |
| 295 | stripe = NpuStripe( |
| 296 | ps, |
| 297 | block_config, |
| 298 | is_first, |
| 299 | is_last, |
| 300 | is_first_h_stripe, |
| 301 | is_last_h_stripe, |
| 302 | ifm_tensor, |
| 303 | ifm_box, |
| 304 | ofm_tensor, |
| 305 | ofm_box, |
| 306 | weight_tensor, |
| 307 | weight_box, |
| 308 | scale_tensor, |
| 309 | concat_axis, |
| 310 | concat_offset, |
| 311 | None, |
| 312 | None, |
| 313 | pad_top, |
| 314 | pad_bottom, |
| 315 | ) |
| 316 | yield stripe |
| 317 | else: |
| 318 | assert 0, "unknown scheduling strategy" |
| 319 | |
| 320 | |
| 321 | def generate_high_level_command_stream_for_pass_list(strat, passes, block_configs): |
| 322 | if strat == SchedulingStrategy.WeightStream: |
| 323 | for idx in range(len(passes)): |
| 324 | yield from generate_high_level_command_stream_for_pass(strat, passes, block_configs, idx) |
| 325 | elif strat == SchedulingStrategy.IfmStream: |
| 326 | yield from generate_high_level_command_stream_for_pass(strat, passes, block_configs, len(passes) - 1) |
| 327 | else: |
| 328 | assert 0, "Unknown streaming strategy" |
| 329 | |
| 330 | |
| 331 | def generate_high_level_command_stream_for_cascaded_pass(cps): |
| 332 | yield from generate_high_level_command_stream_for_pass_list( |
| 333 | cps.strategy, cps.passes, [ps.block_config for ps in cps.passes] |
| 334 | ) |
| 335 | |
| 336 | |
| 337 | def generate_high_level_command_stream(nng, sg, arch, verbose_high_level_command_stream): |
| 338 | res = [] |
| 339 | for cps in sg.cascaded_passes: |
| 340 | if cps.placement == PassPlacement.Npu: |
| 341 | res += list(generate_high_level_command_stream_for_cascaded_pass(cps)) |
| 342 | |
| 343 | sg.high_level_command_stream = res |
| 344 | if verbose_high_level_command_stream: |
| 345 | sg.print_high_level_command_stream() |
| 346 | |
| 347 | |
| 348 | def calc_allowed_ofm_ifm_overlap_for_pass_list(strat, passes, block_configs): |
| 349 | highest_ofm_write = 0 |
| 350 | if not passes[0].ifm_tensor or not passes[-1].ofm_tensor: |
| 351 | return 0 |
| 352 | |
| 353 | ifm_read = passes[0].ifm_tensor.storage_size |
| 354 | min_overlap = 999999999999999999999 |
| 355 | ofm_size = passes[-1].ofm_tensor.storage_size() |
| 356 | if strat == SchedulingStrategy.WeightStream: |
| 357 | return 0 |
| 358 | for cmd in generate_high_level_command_stream_for_pass_list(strat, passes, block_configs): |
| 359 | if cmd.is_npu_pass_command(): |
| 360 | if cmd.is_first: |
| 361 | ifm_read = cmd.ifm_tensor.address_offset_for_coordinate(cmd.ifm_box.start_coord, is_top_box=False) |
| 362 | if ifm_read is None: |
| 363 | return 0 |
| 364 | if cmd.is_last: |
| 365 | write_offset = cmd.ofm_tensor.address_offset_for_coordinate(cmd.ofm_box.end_coord, is_top_box=True) |
| 366 | if write_offset is None: |
| 367 | return 0 |
| 368 | highest_ofm_write = max(write_offset, highest_ofm_write) |
| 369 | |
| 370 | if cmd.is_first or cmd.is_last: |
| 371 | overlap_required = max(highest_ofm_write - min(ifm_read, ofm_size), 0) |
| 372 | can_overwrite = ofm_size - overlap_required |
| 373 | min_overlap = min(min_overlap, can_overwrite) |
| 374 | |
| 375 | if cmd.is_first: |
| 376 | ifm_read = cmd.ifm_tensor.address_offset_for_coordinate(cmd.ifm_box.end_coord, is_top_box=True) |
| 377 | |
| 378 | min_overlap = max(min_overlap, 0) |
| 379 | return min_overlap |
| 380 | |
| 381 | |
| 382 | def calc_allowed_ofm_ifm_overlap_for_cascaded_pass(cps): |
| 383 | return calc_allowed_ofm_ifm_overlap_for_pass_list(cps.strategy, cps.passes, [ps.block_config for ps in cps.passes]) |