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