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 | # Packs a subgraph with Neural Network Operations into Passes. Each Pass has one or more Operations. |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 18 | import collections |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 19 | import enum |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 20 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 21 | from .debug_database import DebugDatabase |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 22 | from .nn_graph import Pass |
| 23 | from .nn_graph import PassPlacement |
| 24 | from .operation import NpuBlockType |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 25 | from .operation import Op |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 26 | from .operation_util import create_avgpool_nop |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 27 | from .tensor import TensorPurpose |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class PassFlags(enum.Flag): |
| 31 | Empty = 0 |
Patrik Gustavsson | 56b6c71 | 2021-02-16 12:57:03 +0100 | [diff] [blame] | 32 | Main = 1 |
| 33 | Post = 2 |
| 34 | Mac = 4 |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 35 | ElementWise = 8 |
| 36 | Npu = 16 |
| 37 | Cpu = 32 |
| 38 | StartupInit = 64 |
| 39 | MemoryOnly = 128 |
| 40 | PostFusingLimited = 256 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 41 | |
| 42 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 43 | mac_main_ops = set( |
| 44 | ( |
| 45 | # convolutions |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 46 | Op.Conv2DBias, |
| 47 | Op.Conv2D, |
| 48 | Op.QuantizedConv2D, |
| 49 | Op.Conv2DBackpropInputSwitchedBias, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 50 | # depth-wise convolutions |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 51 | Op.DepthwiseConv2DBias, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 52 | # FC layers |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 53 | Op.QuantizedMatMul, |
| 54 | Op.MatMul, |
| 55 | Op.FullyConnected, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 56 | # RNN/LSTM/GRU |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 57 | Op.BlockLSTM, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 58 | # pooling |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 59 | Op.QuantizedMaxPool, |
| 60 | Op.QuantizedAvgPool, |
| 61 | Op.AvgPool, |
| 62 | Op.MaxPool, |
| 63 | Op.ReduceSum, |
Dwight Lidman | 3ec04ac | 2020-04-30 11:54:48 +0200 | [diff] [blame] | 64 | # deconvolution |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 65 | Op.ResizeBilinear, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 66 | ) |
| 67 | ) |
| 68 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 69 | binary_elem_wise_main_ops = Op.op_set(Op.is_binary_elementwise_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 70 | |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 71 | unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 72 | |
| 73 | elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops |
| 74 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 75 | activation_ops = Op.op_set(Op.is_relu_op) |
| 76 | npu_post_ops = activation_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 77 | |
| 78 | npu_post_fuse_limited_ops = set( |
| 79 | # Set of post operators that should not be fused with main/elementwise ops |
Patrik Gustavsson | 138d47f | 2021-02-08 10:13:48 +0100 | [diff] [blame] | 80 | (Op.Sigmoid, Op.Tanh, Op.Quantize) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 81 | ) |
| 82 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 83 | elem_wise_ops = elem_wise_main_ops | activation_ops | set((Op.Sigmoid, Op.Tanh)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 84 | |
| 85 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 86 | quantization_ops = set((Op.Dequantize, Op.Max, Op.Min)) |
| 87 | cpu_ops = set((Op.Softmax, Op.LRN, Op.Shape, Op.Pad, Op.AddN)) | quantization_ops |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 88 | |
patrik.gustavsson | 1068362 | 2020-10-14 10:57:46 +0000 | [diff] [blame] | 89 | startup_init_ops = set((Op.Const, Op.Placeholder, Op.SubgraphInput)) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 90 | memory_only_ops = set((Op.Squeeze, Op.Reshape, Op.QuantizedReshape, Op.ExpandDims,)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 91 | |
| 92 | |
| 93 | test_sequence = [ |
| 94 | ( |
| 95 | # ops_set |
| 96 | npu_post_ops, |
| 97 | # incompatible_pack_flags |
Patrik Gustavsson | 56b6c71 | 2021-02-16 12:57:03 +0100 | [diff] [blame] | 98 | PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.Main, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 99 | # flags_to_set |
| 100 | PassFlags.Npu | PassFlags.Post, |
| 101 | # flags_to_clear |
| 102 | PassFlags.Empty, |
| 103 | ), |
| 104 | ( |
| 105 | # ops_set |
| 106 | npu_post_fuse_limited_ops, |
| 107 | # incompatible_pack_flags |
Tim Hall | b1a9a92 | 2021-10-29 12:51:53 +0100 | [diff] [blame] | 108 | PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.Main | PassFlags.PostFusingLimited, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 109 | # flags_to_set |
| 110 | PassFlags.Npu | PassFlags.PostFusingLimited, |
| 111 | # flags_to_clear |
| 112 | PassFlags.Empty, |
| 113 | ), |
| 114 | ( |
| 115 | # ops_set |
| 116 | mac_main_ops, |
| 117 | # incompatible_pack_flags |
Patrik Gustavsson | 56b6c71 | 2021-02-16 12:57:03 +0100 | [diff] [blame] | 118 | PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.ElementWise | PassFlags.Main | PassFlags.PostFusingLimited, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 119 | # flags_to_set |
| 120 | PassFlags.Npu | PassFlags.Mac | PassFlags.Main, |
| 121 | # flags_to_clear |
| 122 | PassFlags.Empty, |
| 123 | ), |
| 124 | ( |
| 125 | # ops_set |
| 126 | elem_wise_main_ops, |
| 127 | # incompatible_pack_flags |
Patrik Gustavsson | 56b6c71 | 2021-02-16 12:57:03 +0100 | [diff] [blame] | 128 | PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.Mac | PassFlags.Main | PassFlags.PostFusingLimited, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 129 | # flags_to_set |
| 130 | PassFlags.Npu | PassFlags.ElementWise | PassFlags.Main, |
| 131 | # flags_to_clear |
| 132 | PassFlags.Empty, |
| 133 | ), |
| 134 | ( |
| 135 | # ops_set |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 136 | startup_init_ops, |
| 137 | # incompatible_pack_flags |
| 138 | PassFlags.Npu | PassFlags.Cpu | PassFlags.MemoryOnly, |
| 139 | # flags_to_set |
| 140 | PassFlags.StartupInit | PassFlags.Main, |
| 141 | # flags_to_clear |
| 142 | PassFlags.Empty, |
| 143 | ), |
| 144 | ( |
| 145 | # ops_set |
| 146 | memory_only_ops, |
| 147 | # incompatible_pack_flags |
| 148 | PassFlags.Npu | PassFlags.Cpu, |
| 149 | # flags_to_set |
| 150 | PassFlags.MemoryOnly | PassFlags.Main, |
| 151 | # flags_to_clear |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 152 | PassFlags.Empty, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 153 | ), |
| 154 | ( |
| 155 | # ops_set |
| 156 | cpu_ops, |
| 157 | # incompatible_pack_flags |
| 158 | PassFlags.Npu | PassFlags.MemoryOnly | PassFlags.Main, |
| 159 | # flags_to_set |
| 160 | PassFlags.Cpu | PassFlags.Main, |
| 161 | # flags_to_clear |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 162 | PassFlags.Empty, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 163 | ), |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 164 | ( # This last one is a fallback for unrecognised operations |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 165 | # ops_set |
| 166 | None, |
| 167 | # incompatible_pack_flags |
| 168 | PassFlags.Npu | PassFlags.MemoryOnly | PassFlags.Main, |
| 169 | # flags_to_set |
| 170 | PassFlags.Cpu | PassFlags.Main, |
| 171 | # flags_to_clear |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 172 | PassFlags.Empty, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 173 | ), |
| 174 | ] |
| 175 | |
| 176 | # Some sanity checking |
| 177 | for (operation_set, incompatible_pack_flags, flags_to_set, flags_to_clear) in test_sequence: |
| 178 | assert not flags_to_clear & flags_to_set |
| 179 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 180 | |
| 181 | def pack_into_passes(nng, arch, verbose_packing=False): |
Tim Hall | 849ff81 | 2021-12-23 15:40:34 +0000 | [diff] [blame^] | 182 | def visit_op(op, multiple_ops=None): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 183 | visit_op_refcount[op] += 1 |
| 184 | |
| 185 | if visit_op_refcount[op] == 1: # First-time visit, go and fix up unused output tensors |
| 186 | for tens in op.outputs: |
| 187 | if len(tens.consumers()) == 0: |
| 188 | visit_op_refcount[op] += 1 |
| 189 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 190 | if visit_op_refcount[op] == len(op.outputs): |
| 191 | |
| 192 | if op.type in startup_init_ops: |
| 193 | startup_list.append(op) |
| 194 | else: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 195 | ofm_tensor = op.ofm |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 196 | if ofm_tensor is None: |
| 197 | ofm_tensor = op.outputs[0] |
Tim Hall | 73e843f | 2021-02-04 22:47:46 +0000 | [diff] [blame] | 198 | ofm_shape = op.ofm_shapes[0] if op.run_on_npu else None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 199 | |
Tim Hall | 849ff81 | 2021-12-23 15:40:34 +0000 | [diff] [blame^] | 200 | build_pass((op,), ofm_tensor, ofm_shape, multiple_ops) |
Patrik Gustavsson | 6bb8f67 | 2020-12-21 14:49:13 +0100 | [diff] [blame] | 201 | |
Tim Hall | 849ff81 | 2021-12-23 15:40:34 +0000 | [diff] [blame^] | 202 | def build_pass(start_ops_to_process, ofm_tensor=None, ofm_shape=None, multiple_ops=None): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 203 | reverse_ops_list = [] |
| 204 | curr_flags = PassFlags.Empty |
| 205 | npu_block_type = NpuBlockType.Default |
| 206 | |
| 207 | reverse_intermediates = [] |
| 208 | input_set = set() |
| 209 | ifm_tensor = None |
| 210 | primary_op = None |
Patrik Gustavsson | 224e99b | 2021-01-14 10:55:43 +0100 | [diff] [blame] | 211 | ifm_shapes = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 212 | |
| 213 | to_process = collections.deque() |
| 214 | for start_op in start_ops_to_process: |
| 215 | to_process.append((start_op, None)) |
| 216 | |
| 217 | while to_process: |
| 218 | curr_op, tens = to_process.popleft() |
| 219 | |
| 220 | if curr_op in reverse_ops_list: |
| 221 | continue |
| 222 | |
| 223 | for operation_set, incompatible_pack_flags, flags_to_set, flags_to_clear in test_sequence: |
| 224 | if operation_set is None or curr_op.type in operation_set: |
| 225 | if not (curr_flags & incompatible_pack_flags): |
| 226 | if flags_to_set & PassFlags.Npu: |
| 227 | if not curr_op.run_on_npu: |
| 228 | continue |
| 229 | |
| 230 | reverse_ops_list.append(curr_op) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 231 | new_block_type = curr_op.type.npu_block_type |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 232 | if new_block_type != NpuBlockType.Default: |
| 233 | assert npu_block_type == NpuBlockType.Default |
| 234 | npu_block_type = new_block_type # Only one major block type per pass |
| 235 | assert primary_op is None |
| 236 | primary_op = curr_op |
| 237 | |
| 238 | curr_flags &= ~flags_to_clear |
| 239 | curr_flags |= flags_to_set |
| 240 | |
| 241 | if flags_to_set & PassFlags.Npu: |
| 242 | if flags_to_set & ( |
| 243 | PassFlags.Mac | PassFlags.ElementWise | PassFlags.Post | PassFlags.PostFusingLimited |
| 244 | ): |
| 245 | assert len(curr_op.inputs) >= 1 |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 246 | ifm_tensor = curr_op.ifm |
Patrik Gustavsson | 224e99b | 2021-01-14 10:55:43 +0100 | [diff] [blame] | 247 | ifm_shapes = curr_op.ifm_shapes.copy() |
Louis Verhaard | 04f8c00 | 2020-10-09 11:40:21 +0200 | [diff] [blame] | 248 | assert ifm_tensor is not None, "IFM missing in {}".format(curr_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 249 | assert ifm_tensor.purpose == TensorPurpose.FeatureMap |
| 250 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 251 | if operation_set is None: |
| 252 | print("Warning:", curr_op.type, "operation is unknown or unsupported, placing on CPU") |
| 253 | |
Charles Xu | 600351a | 2020-05-18 08:54:47 +0200 | [diff] [blame] | 254 | for inp in reversed(curr_op.inputs): |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 255 | if inp is None: |
| 256 | continue |
Patrik Gustavsson | fcb1a00 | 2021-02-03 09:13:57 +0100 | [diff] [blame] | 257 | if can_pack(inp, curr_op): |
| 258 | to_process.append((inp.ops[0], inp)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 259 | else: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 260 | input_set.add(inp) |
| 261 | |
| 262 | break |
| 263 | |
| 264 | else: |
| 265 | # This operation is not compatible with already packed operations, just register the tensor as an input |
| 266 | assert tens is not None |
| 267 | input_set.add(tens) |
| 268 | |
| 269 | if curr_flags & PassFlags.Npu and not curr_flags & (PassFlags.ElementWise | PassFlags.Mac): |
| 270 | # Make the choice that if we don't have a mac operation, the ambidextrous operations go on the |
| 271 | # element wise unit |
| 272 | curr_flags |= PassFlags.ElementWise |
| 273 | |
| 274 | is_element_wise = True |
| 275 | for op in reverse_ops_list: |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 276 | if op.type not in elem_wise_ops and op.type: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 277 | is_element_wise = False |
| 278 | break |
| 279 | |
| 280 | placement = PassPlacement.Unknown |
| 281 | if curr_flags & PassFlags.Npu: |
| 282 | assert placement == PassPlacement.Unknown |
| 283 | placement = PassPlacement.Npu |
| 284 | if curr_flags & PassFlags.Cpu: |
| 285 | assert placement == PassPlacement.Unknown |
| 286 | placement = PassPlacement.Cpu |
| 287 | if curr_flags & PassFlags.MemoryOnly: |
| 288 | assert placement == PassPlacement.Unknown |
| 289 | placement = PassPlacement.MemoryOnly |
| 290 | if curr_flags & PassFlags.StartupInit: |
| 291 | assert placement == PassPlacement.Unknown |
| 292 | placement = PassPlacement.StartupInit |
| 293 | assert placement != PassPlacement.Unknown |
| 294 | |
| 295 | ops_list = list(reversed(reverse_ops_list)) |
| 296 | intermediates = list(reversed(reverse_intermediates)) |
| 297 | |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 298 | if primary_op is None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 299 | primary_op = create_primary_op(ops_list) |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 300 | if primary_op is not None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 301 | visit_tensor_refcount[primary_op.inputs[0]] += 1 |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 302 | npu_block_type = primary_op.type.npu_block_type |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 303 | for input_tens in primary_op.inputs: |
| 304 | if input_tens not in input_set: |
| 305 | input_set.add(input_tens) |
| 306 | |
| 307 | ordered_input_list = [] |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 308 | # Keep LUT-s in a separate list and add as inputs at the end |
| 309 | # to avoid that they would accidentally be assigned as ifm or ifm2 |
| 310 | lut_list = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 311 | input_refcounts = collections.defaultdict(int) |
Diqing Zhong | 2abd3dd | 2020-08-25 10:40:36 +0200 | [diff] [blame] | 312 | input_ops_list = ops_list.copy() |
| 313 | |
| 314 | # Check primary_op first |
| 315 | if primary_op is not None: |
| 316 | for inp in primary_op.inputs: |
Andreas Nevalainen | d8c032d | 2020-09-11 10:25:09 +0200 | [diff] [blame] | 317 | if inp is None: |
| 318 | continue |
Diqing Zhong | 2abd3dd | 2020-08-25 10:40:36 +0200 | [diff] [blame] | 319 | add_input_list(inp, input_set, input_refcounts, lut_list, ordered_input_list) |
| 320 | input_ops_list.remove(primary_op) |
| 321 | |
| 322 | # Check rest of the list |
| 323 | for op in input_ops_list: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 324 | for inp in op.inputs: |
Diqing Zhong | 2abd3dd | 2020-08-25 10:40:36 +0200 | [diff] [blame] | 325 | add_input_list(inp, input_set, input_refcounts, lut_list, ordered_input_list) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 326 | |
| 327 | name = ops_list[0].name |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 328 | ps = Pass(name, placement, is_element_wise, npu_block_type) |
| 329 | ps.ops = ops_list |
| 330 | ps.primary_op = primary_op |
| 331 | ps.inputs = ordered_input_list |
| 332 | ps.intermediates = intermediates |
| 333 | ps.outputs = list(ops_list[-1].outputs) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 334 | |
| 335 | # ElementWise operation, 2 IFMs |
| 336 | if ps.primary_op and ps.primary_op.type in binary_elem_wise_main_ops: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 337 | ps.ifm_tensor = ps.inputs[0] |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 338 | ps.ifm2_tensor = ps.inputs[-1] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 339 | |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 340 | if len(ps.inputs) > 2: |
| 341 | ps.ifm_tensor = ps.inputs[-2] |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 342 | |
| 343 | # Get the corresponding ifm_shapes |
| 344 | for op in input_ops_list + [primary_op]: |
Patrik Gustavsson | 0a261cd | 2020-12-23 08:50:44 +0100 | [diff] [blame] | 345 | if op.run_on_npu: |
| 346 | if ps.ifm_tensor == op.ifm: |
| 347 | ps.ifm_shapes.append(op.ifm_shapes[0]) |
| 348 | elif ps.ifm_tensor == op.ifm2: |
| 349 | ps.ifm_shapes.append(op.ifm_shapes[1]) |
Tim Hall | ffe8e28 | 2021-06-24 18:29:53 +0100 | [diff] [blame] | 350 | |
Patrik Gustavsson | 0a261cd | 2020-12-23 08:50:44 +0100 | [diff] [blame] | 351 | if ps.ifm2_tensor == op.ifm: |
| 352 | ps.ifm_shapes.append(op.ifm_shapes[0]) |
| 353 | elif ps.ifm2_tensor == op.ifm2: |
| 354 | ps.ifm_shapes.append(op.ifm_shapes[1]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 355 | else: |
| 356 | ps.ifm_tensor = ifm_tensor |
| 357 | ps.ifm2_tensor = None |
Patrik Gustavsson | cc6915c | 2020-12-22 09:16:50 +0100 | [diff] [blame] | 358 | if ps.primary_op is not None and ps.primary_op.run_on_npu: |
Patrik Gustavsson | 224e99b | 2021-01-14 10:55:43 +0100 | [diff] [blame] | 359 | ps.ifm_shapes.append(ifm_shapes[0]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 360 | |
| 361 | ps.ofm_tensor = ofm_tensor |
Patrik Gustavsson | 6bb8f67 | 2020-12-21 14:49:13 +0100 | [diff] [blame] | 362 | ps.ofm_shapes.append(ofm_shape) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 363 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 364 | assert ps.placement != PassPlacement.Npu or ps.ofm_tensor is not None |
| 365 | ps.weight_tensor = ps.get_primary_op_ifm_weights()[1] |
| 366 | ps.scale_tensor = ps.get_primary_op_ifm_weights_biases_ofm()[2] |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 367 | ps.lut_tensor = ps.get_primary_op_lut() |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 368 | ps.inputs.extend(lut_list) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 369 | |
| 370 | for op in ps.ops: |
| 371 | op.scheduled_pass = ps |
| 372 | |
| 373 | reverse_pass_list.append(ps) |
| 374 | |
Tim Hall | 849ff81 | 2021-12-23 15:40:34 +0000 | [diff] [blame^] | 375 | if multiple_ops: |
| 376 | multiple_op_next = multiple_ops.pop(0) |
| 377 | visit_op(multiple_op_next, multiple_ops) |
| 378 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 379 | for inp, refcount in input_refcounts.items(): |
| 380 | for _ in range(refcount): |
| 381 | visit_tensor(inp) |
| 382 | |
| 383 | return ps |
| 384 | |
| 385 | def visit_tensor(tens): |
| 386 | visit_tensor_refcount[tens] += 1 |
| 387 | assert visit_tensor_refcount[tens] <= len(tens.consumers()) |
| 388 | if visit_tensor_refcount[tens] == len(tens.consumers()): |
Tim Hall | 849ff81 | 2021-12-23 15:40:34 +0000 | [diff] [blame^] | 389 | if tens.ops: |
| 390 | op = tens.ops[0] |
| 391 | multiple_ops = [o for o in tens.ops if o != op] |
| 392 | visit_op(op, multiple_ops) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 393 | |
Jacob Bohlin | fb85873 | 2020-08-17 09:42:35 +0200 | [diff] [blame] | 394 | def create_primary_op(op_list): |
Patrik Gustavsson | e3b1b91 | 2021-02-09 15:38:46 +0100 | [diff] [blame] | 395 | if any(op.type in (npu_post_ops | npu_post_fuse_limited_ops) and op.run_on_npu for op in op_list): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 396 | # Configure a 1x1 AvgPool and attach the op onto it |
Jacob Bohlin | fb85873 | 2020-08-17 09:42:35 +0200 | [diff] [blame] | 397 | op = op_list[0] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 398 | inp = op.inputs[0] |
Michael McGeagh | 8dbf8cf | 2020-09-08 11:09:48 +0100 | [diff] [blame] | 399 | avgpool_op = create_avgpool_nop(op.name + "_avgpool") |
| 400 | avgpool_op.add_input_tensor(inp) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 401 | avgpool_out = inp.clone("_avgpooled") |
| 402 | avgpool_out.consumer_list.append(op) |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 403 | avgpool_op.set_output_tensor(avgpool_out) |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 404 | avgpool_op.ifm_shapes = op.ifm_shapes.copy() |
| 405 | avgpool_op.ofm_shapes = op.ofm_shapes.copy() |
Patrik Gustavsson | 56b6c71 | 2021-02-16 12:57:03 +0100 | [diff] [blame] | 406 | avgpool_op.read_offsets = op.read_offsets.copy() |
Tim Hall | 3df5b96 | 2021-12-17 14:09:19 +0000 | [diff] [blame] | 407 | avgpool_op.read_shapes = op.read_shapes.copy() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 408 | |
| 409 | op.inputs[0] = avgpool_out |
Jacob Bohlin | fb85873 | 2020-08-17 09:42:35 +0200 | [diff] [blame] | 410 | op_list.insert(0, avgpool_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 411 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 412 | DebugDatabase.add_optimised(op, avgpool_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 413 | return avgpool_op |
| 414 | |
| 415 | return None |
| 416 | |
Patrik Gustavsson | fcb1a00 | 2021-02-03 09:13:57 +0100 | [diff] [blame] | 417 | def can_pack(inp, curr_op): |
| 418 | if len(inp.ops) == 1: |
| 419 | next_op = inp.ops[0] |
| 420 | for outp in next_op.outputs: |
| 421 | consumers = outp.consumers() |
| 422 | if len(consumers) > 1 or (len(consumers) == 1 and consumers[0] != curr_op): |
| 423 | return False |
| 424 | |
| 425 | # There cannot be any reshaping between next_op ofm and corresponding curr_op ifm |
| 426 | if len(curr_op.ifm_shapes) != 0 and len(next_op.ofm_shapes) != 0: |
| 427 | if inp == curr_op.ifm and next_op.ofm_shapes[0] != curr_op.ifm_shapes[0]: |
| 428 | return False |
| 429 | elif ( |
| 430 | curr_op.ifm2 is not None and inp == curr_op.ifm2 and next_op.ofm_shapes[0] != curr_op.ifm_shapes[1] |
| 431 | ): |
| 432 | return False |
| 433 | else: |
| 434 | return False |
| 435 | |
| 436 | return True |
| 437 | |
Diqing Zhong | 2abd3dd | 2020-08-25 10:40:36 +0200 | [diff] [blame] | 438 | def add_input_list(inp_to_add, inp_set, inp_refcnts, lut_list, ordered_inp_list): |
| 439 | if inp_to_add in inp_set: |
| 440 | if inp_refcnts[inp_to_add] == 0: |
| 441 | if inp_to_add.purpose == TensorPurpose.LUT: |
| 442 | lut_list.append(inp_to_add) |
| 443 | else: |
| 444 | ordered_inp_list.append(inp_to_add) |
| 445 | inp_refcnts[inp_to_add] += 1 |
| 446 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 447 | for sg in nng.subgraphs: |
| 448 | reverse_pass_list = [] |
| 449 | visit_op_refcount = collections.defaultdict(int) |
| 450 | visit_tensor_refcount = collections.defaultdict(int) |
| 451 | |
| 452 | startup_list = [] |
| 453 | |
| 454 | for tens in sg.output_tensors: |
| 455 | visit_tensor(tens) |
| 456 | |
| 457 | if startup_list: |
| 458 | startup_ps = build_pass(startup_list) |
| 459 | startup_ps.outputs = [op.outputs[0] for op in startup_list] # Need to fixup the outputs |
| 460 | startup_ps.name = "startup_weight_initialisation" |
| 461 | |
| 462 | sg.passes = list(reversed(reverse_pass_list)) |
| 463 | sg.build_pass_links() |
| 464 | |
| 465 | if verbose_packing: |
| 466 | nng.print_passes() |
| 467 | |
| 468 | return nng |