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