Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [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. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 16 | # |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 17 | # Description: |
| 18 | # Common functions and definitions used during the graph optimization. |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 19 | from typing import Tuple |
| 20 | |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 21 | import numpy as np |
| 22 | |
Patrik Gustavsson | f436ada | 2021-09-14 14:56:48 +0200 | [diff] [blame] | 23 | from . import lut |
Tim Hall | d6efcd3 | 2022-09-02 15:01:01 +0100 | [diff] [blame] | 24 | from .architecture_features import Accelerator |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 25 | from .data_type import DataType |
| 26 | from .debug_database import DebugDatabase |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 27 | from .errors import UnsupportedFeatureError |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 28 | from .errors import VelaError |
| 29 | from .operation import Op |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 30 | from .operation_util import create_avgpool_nop |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 31 | from .shape4d import Shape4D |
Patrik Gustavsson | f436ada | 2021-09-14 14:56:48 +0200 | [diff] [blame] | 32 | from .tensor import create_const_tensor |
| 33 | from .tensor import QuantizationParameters |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 34 | |
Jonas Ohlsson | 81942e9 | 2021-08-20 09:33:28 +0200 | [diff] [blame] | 35 | memory_only_ops = ( |
| 36 | Op.Reshape, |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 37 | Op.QuantizedReshape, |
Jonas Ohlsson | 81942e9 | 2021-08-20 09:33:28 +0200 | [diff] [blame] | 38 | Op.Squeeze, |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 39 | Op.ExpandDims, |
Patrik Gustavsson | ef3ebdd | 2021-10-01 11:10:25 +0200 | [diff] [blame] | 40 | Op.Identity, |
Jonas Ohlsson | 81942e9 | 2021-08-20 09:33:28 +0200 | [diff] [blame] | 41 | ) |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 42 | |
Johan Alfvén | 48e5159 | 2022-09-28 20:06:25 +0200 | [diff] [blame] | 43 | # Ops that are dependent that the original ifm tensor shape is not changed |
| 44 | # by the bypass memory op function |
| 45 | original_ifm_shape_ops = (Op.Mean,) |
| 46 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 47 | |
| 48 | def _avoid_nhcwb16_for_concat(tens): |
| 49 | # If axis corresponds to C-dimension, NHCWB16 can only be used in the output if all the concat_start's are a |
| 50 | # multiple of 16. This as, it is only then the address offset for the ofm, for all operations, will be 16 byte |
| 51 | # aligned. For other values of axis the address offsets will be 16 byte aligned, as they are all based on c = 0 |
| 52 | # and those addresses are always 16 byte aligned due to the NHCWB16 format. |
| 53 | return any(op.write_offset.depth % 16 != 0 for op in tens.ops if op.write_offset is not None) |
| 54 | |
| 55 | |
| 56 | def _avoid_nhcwb16_for_split(tens): |
| 57 | # If read offset is not a multiple of 16 in the C-dimension, NHCWB16 need to be avoided in the input |
James Ward | 6bf1613 | 2021-09-08 11:14:20 +0100 | [diff] [blame] | 58 | |
| 59 | # Return True if NHCWB16 needs to be avoided |
| 60 | def offset_not_aligned(read_offset): |
| 61 | return read_offset is not None and (read_offset.depth % 16) != 0 |
| 62 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 63 | for cons_op in tens.consumer_list: |
| 64 | if cons_op.ifm == tens: |
James Ward | 6bf1613 | 2021-09-08 11:14:20 +0100 | [diff] [blame] | 65 | if offset_not_aligned(cons_op.read_offsets[0]): |
| 66 | return True |
| 67 | if cons_op.ifm2 is not None and cons_op.ifm2 == tens: |
| 68 | if offset_not_aligned(cons_op.read_offsets[1]): |
| 69 | return True |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 70 | return False |
| 71 | |
| 72 | |
| 73 | def _avoid_nhcwb16_for_shapes(tens): |
| 74 | # check all producers/consumers to see if any op shape is preventing NHCWB16 |
| 75 | for cons_op in tens.consumer_list: |
| 76 | if cons_op.ifm == tens: |
| 77 | cons_op_shape = cons_op.ifm_shapes[0] |
| 78 | elif cons_op.type.is_binary_elementwise_op() and cons_op.ifm2 == tens: |
| 79 | cons_op_shape = cons_op.ifm_shapes[1] |
| 80 | else: |
| 81 | assert False |
| 82 | if Shape4D(tens.shape) != cons_op_shape: |
| 83 | return True |
| 84 | |
| 85 | for prod_op in tens.ops: |
| 86 | if Shape4D(tens.shape) != prod_op.ofm_shapes[0]: |
| 87 | return True |
| 88 | |
| 89 | return False |
| 90 | |
| 91 | |
| 92 | # Check if non linear format can be used |
| 93 | def check_format_restrictions(tens, arch): |
| 94 | if len(tens.ops) < 1: |
| 95 | return |
| 96 | if tens.ops[0].type in (Op.Placeholder, Op.SubgraphInput, Op.Const) or any( |
| 97 | cons is None for cons in tens.consumer_list |
| 98 | ): |
| 99 | return |
| 100 | |
| 101 | # Check if any of the producers/consumers is run on CPU |
| 102 | if not all(cons.run_on_npu for cons in tens.consumer_list): |
| 103 | return |
| 104 | if not all(prod.run_on_npu for prod in tens.ops): |
| 105 | return |
| 106 | |
| 107 | # "Concat" ofm exception: |
| 108 | if _avoid_nhcwb16_for_concat(tens): |
| 109 | return |
| 110 | |
| 111 | # "Split" ifm exception: |
| 112 | if _avoid_nhcwb16_for_split(tens): |
| 113 | return |
| 114 | |
| 115 | # Shapes checking: check all producers/consumers are NHCWB16 compatible with tens.shape |
| 116 | if _avoid_nhcwb16_for_shapes(tens): |
| 117 | return |
| 118 | |
Rickard Bolin | fea1516 | 2022-07-04 16:19:16 +0000 | [diff] [blame] | 119 | # Resize bilinear half pixel center implementation requires OFM with linear format to |
| 120 | # allow stride modification in H/W dimensions. |
| 121 | for op in tens.ops: |
| 122 | if op.original_type == Op.ResizeBilinear and op.type == Op.DepthwiseConv2DBias: |
| 123 | return |
| 124 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 125 | for op in tens.consumer_list: |
Tim Hall | d6efcd3 | 2022-09-02 15:01:01 +0100 | [diff] [blame] | 126 | if op.type == Op.ReduceSum and ( |
| 127 | tens.dtype == DataType.int32 or arch.accelerator_config == Accelerator.Ethos_U65_512 |
| 128 | ): |
| 129 | # ReduceSum requires NHWC input |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 130 | return |
| 131 | if op.type == Op.Reshape: |
| 132 | # Using NHCWB16 format for a no-op reshape is only an option if subsequent |
| 133 | # consumers do not also need to perform a reshape or if the OFM is going to |
| 134 | # be processed by CPU operations. No-op reshape consumers with empty lists |
| 135 | # (those that have no consumers, or null-consumers used as list terminators) |
| 136 | # must use normal NHWC output. |
| 137 | |
| 138 | def incompatible_consumers(oper): |
| 139 | if oper and oper.type == Op.Reshape: |
| 140 | for consumer in oper.outputs[0].consumer_list: |
| 141 | yield from incompatible_consumers(consumer) |
| 142 | yield not oper or not oper.run_on_npu |
| 143 | |
| 144 | if not any(incompatible_consumers(op)): |
| 145 | |
| 146 | def get_rewrites(oper): |
| 147 | if oper and oper.type == Op.Reshape: |
| 148 | for consumer in oper.outputs[0].consumer_list: |
| 149 | yield from get_rewrites(consumer) |
| 150 | yield oper |
| 151 | |
| 152 | # Detect no-op reshapes by comparing their full input and output tensor shapes. |
| 153 | inshape = op.ifm_shapes[0] |
| 154 | compatible_shape = [(inshape == oper.ofm_shapes[0]) for oper in get_rewrites(op)] |
| 155 | if not (compatible_shape and all(compatible_shape)): |
| 156 | return |
| 157 | else: |
| 158 | return |
| 159 | |
| 160 | tens.needs_linear_format = False |
| 161 | |
| 162 | |
Patrik Gustavsson | c74682c | 2021-08-17 14:26:38 +0200 | [diff] [blame] | 163 | def calc_explicit_padding(input_size, stride, filter_size, pad_before, pad_after) -> Tuple[int, int]: |
| 164 | """ |
| 165 | Based on explicit padding provided in a PAD operation, returns the corresponding hardware padding |
| 166 | that provides equivalent results. |
| 167 | """ |
| 168 | total_padding = needed_total_padding(input_size, stride, filter_size) |
| 169 | |
| 170 | # The bottom/right padding might need downward adjustment depending on stride/input size |
| 171 | total_minus_before = total_padding - pad_before |
| 172 | output_pad_after = pad_after |
| 173 | while output_pad_after > 0 and output_pad_after % stride != total_minus_before % stride: |
| 174 | output_pad_after -= 1 |
| 175 | return pad_before, output_pad_after |
| 176 | |
| 177 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 178 | def needed_total_padding(input_size, stride, filter_size): |
| 179 | out_size = (input_size + stride - 1) // stride |
| 180 | needed_input = (out_size - 1) * stride + filter_size |
| 181 | total_padding = max(0, needed_input - input_size) |
| 182 | return total_padding |
| 183 | |
| 184 | |
| 185 | # Set input/output tensor equivalence to the same id for memory operations |
| 186 | def set_tensor_equivalence(op, arch, nng): |
| 187 | if op.type in memory_only_ops: |
| 188 | eid = op.outputs[0].equivalence_id |
| 189 | for inp in op.inputs: |
| 190 | inp.equivalence_id = eid |
| 191 | return op |
| 192 | |
| 193 | |
| 194 | def set_ifm_ofm_op_shapes(op, arch, nng): |
| 195 | if op.run_on_npu and op.type.needs_shapes(): |
| 196 | if op.ifm_shapes or op.ofm_shapes: |
| 197 | # Shapes already set |
| 198 | return op |
| 199 | op.set_ifm_ofm_shapes() |
| 200 | return op |
| 201 | |
| 202 | |
Johan Alfvén | 48e5159 | 2022-09-28 20:06:25 +0200 | [diff] [blame] | 203 | def bypass_need_to_keep_ofm_shape(op): |
| 204 | # Check if ifm must be replaced by ofm (rank is changed or the op that follow must have original ifm shape) |
| 205 | ifm_replaced_by_ofm = any( |
| 206 | ofm_cons is not None and ofm_cons.type in original_ifm_shape_ops for ofm_cons in op.ofm.consumer_list |
| 207 | ) or len(op.ifm.shape) != len(op.ofm.shape) |
| 208 | return ifm_replaced_by_ofm |
| 209 | |
| 210 | |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 211 | def bypass_memory_only_ops(op): |
| 212 | assert op.type in memory_only_ops |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 213 | ofm = op.ofm |
| 214 | ifm = op.ifm |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 215 | |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 216 | # Check if ifm/ofm are network ifm/ofm |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 217 | ifm_is_sg_ifm = ifm.ops[0].type in (Op.Placeholder, Op.SubgraphInput, Op.Const) |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 218 | ifm_is_sg_ofm = any(ifm_cons is None for ifm_cons in ifm.consumer_list) |
| 219 | ofm_is_sg_ofm = any(ofm_cons is None for ofm_cons in ofm.consumer_list) |
| 220 | # Check if ifm/ofm is produced respectively consumed by CPU |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 221 | ifm_is_cpu_produced = any(ifm_prod is not None and not ifm_prod.run_on_npu for ifm_prod in op.ifm.ops) |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 222 | ofm_is_cpu_consumed = any(ofm_cons is not None and not ofm_cons.run_on_npu for ofm_cons in op.ofm.consumer_list) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 223 | |
| 224 | # This case should be handled prior to this function |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 225 | assert not ((ifm_is_sg_ifm or ifm_is_sg_ofm or ifm_is_cpu_produced) and (ofm_is_sg_ofm or ofm_is_cpu_consumed)) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 226 | |
Johan Alfvén | 48e5159 | 2022-09-28 20:06:25 +0200 | [diff] [blame] | 227 | if (ifm.shape != ofm.shape) and (ofm_is_sg_ofm or ofm_is_cpu_consumed or bypass_need_to_keep_ofm_shape(op)): |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 228 | # Bypassed by replacing ifm with ofm |
| 229 | ofm.ops = [] |
| 230 | for prev_op in ifm.ops: |
| 231 | prev_op.outputs = [ofm] |
| 232 | ofm.ops.append(prev_op) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 233 | |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 234 | # All ifm consumers need to use ofm as input |
| 235 | for ifm_cons in ifm.consumer_list: |
| 236 | for ifm_idx, cons_ifm in enumerate(ifm_cons.inputs): |
| 237 | if cons_ifm == ifm: |
| 238 | ifm_cons.set_input_tensor(ofm, ifm_idx) |
| 239 | else: |
| 240 | # Bypassed by replacing ofm with ifm |
| 241 | for cons in ofm.consumer_list: |
| 242 | for ifm_idx, cons_ifm in enumerate(cons.inputs): |
| 243 | if cons_ifm == ofm: |
| 244 | cons.set_input_tensor(ifm, ifm_idx) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 245 | |
| 246 | |
Patrik Gustavsson | f1580f0 | 2021-09-01 12:43:02 +0200 | [diff] [blame] | 247 | def move_splitsliceread_to_consumer(op, cons_op): |
| 248 | assert op.type == Op.SplitSliceRead |
| 249 | |
| 250 | if cons_op.ifm == op.ofm: |
| 251 | cons_op.read_offsets[0] = op.read_offsets[0] |
| 252 | cons_op.read_shapes[0] = op.read_shapes[0] |
| 253 | cons_op.set_input_tensor(op.ifm, cons_op.type.info.indices.ifms[0]) |
| 254 | cons_op.ifm_shapes[0] = op.ifm_shapes[0] |
| 255 | elif cons_op.type.is_binary_elementwise_op() and cons_op.ifm2 == op.ofm: |
| 256 | cons_op.read_offsets[1] = op.read_offsets[0] |
| 257 | cons_op.read_shapes[1] = op.read_shapes[0] |
| 258 | cons_op.set_input_tensor(op.ifm, cons_op.type.info.indices.ifms[1]) |
| 259 | cons_op.ifm_shapes[1] = op.ifm_shapes[0] |
Patrik Gustavsson | f1580f0 | 2021-09-01 12:43:02 +0200 | [diff] [blame] | 260 | op.ofm.consumer_list.remove(cons_op) |
| 261 | op.ofm.ops = [] |
| 262 | op.ifm.consumer_list.remove(op) |
| 263 | |
| 264 | |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 265 | def check_memory_only_removed(op, arch): |
| 266 | if op.run_on_npu and op.type in memory_only_ops: |
| 267 | # Memory only operators should have been removed |
| 268 | raise VelaError(f"Memory only {op.type} op {op} expected to have been removed, still remains") |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 269 | |
| 270 | |
| 271 | def record_optimised(op, arch): |
wilisa01 | 79a8904 | 2022-11-02 17:18:43 +0000 | [diff] [blame^] | 272 | if op.type not in (Op.Const, Op.Placeholder): |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 273 | DebugDatabase.add_optimised(op, op) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 274 | |
| 275 | |
Johan Alfvén | 48e5159 | 2022-09-28 20:06:25 +0200 | [diff] [blame] | 276 | def insert_copy_op_before_op(op): |
| 277 | # Create a avg_pool nop op with ifm as input |
| 278 | tens = op.ifm |
| 279 | copy_tens = tens.clone() |
| 280 | copy_op = create_avgpool_nop(f"{tens.name}_avgpool") |
| 281 | copy_op.add_input_tensor(tens) |
| 282 | copy_op.set_output_tensor(copy_tens) |
| 283 | copy_op.set_ifm_ofm_shapes() |
| 284 | |
| 285 | op.set_input_tensor(copy_tens, 0) |
| 286 | |
| 287 | DebugDatabase.add_optimised(op, copy_op) |
| 288 | |
| 289 | |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 290 | def insert_copy_op_after_tens(tens): |
| 291 | tens_cons_list_copy = tens.consumer_list.copy() |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 292 | |
| 293 | # Create a avg_pool nop op with ifm as input |
| 294 | copy_tens = tens.clone() |
| 295 | copy_op = create_avgpool_nop(tens.name + "_avgpool") |
| 296 | copy_op.add_input_tensor(tens) |
| 297 | copy_op.set_output_tensor(copy_tens) |
| 298 | copy_op.set_ifm_ofm_shapes() |
| 299 | copy_op.run_on_npu = True |
| 300 | |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 301 | # Set copy_ifm consumers |
| 302 | for tens_cons in tens_cons_list_copy: |
| 303 | if tens_cons is not None: |
| 304 | for ifm_idx, cons_inp in enumerate(tens_cons.inputs): |
| 305 | if cons_inp == tens: |
| 306 | tens_cons.set_input_tensor(copy_tens, ifm_idx) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 307 | |
| 308 | DebugDatabase.add_optimised(tens.ops[0], copy_op) |
| 309 | |
| 310 | |
| 311 | def fix_sg_input_output(op, arch, nng): |
Jonas Ohlsson | 0957e3e | 2021-09-01 15:57:21 +0200 | [diff] [blame] | 312 | if not op.run_on_npu or op.type not in memory_only_ops: |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 313 | return op |
| 314 | |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 315 | # For the memory only operators we want to remove, tensors are removed. |
| 316 | # But in order to to do this, they cannot be outputs of the sg, |
| 317 | # this need to be fixed prior to the removal. |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 318 | # Solution is to add a avgpool NOP, to maintain the original tensor. |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 319 | # This is also valid when reshape ifm/ofm is produced respectively |
| 320 | # consumed by CPU |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 321 | |
Johan Alfvén | 48e5159 | 2022-09-28 20:06:25 +0200 | [diff] [blame] | 322 | # Rare case: original_ifm_shape_ops contain ops that are dependent |
| 323 | # that the original ifm tensor shape is not changed by the bypass memory |
| 324 | # function. If the memory only op ifm is subgraph ifm/ifm is cpu produced |
| 325 | # or the ifm is consumed by many, then there is a need to insert an avgpool |
| 326 | # NOP before the original_ifm_shape_ops. Also note that the NOP is only inserted |
| 327 | # before original_ifm_shape_ops. The above is also true when the memory only |
| 328 | # op change the rank between the IFM and OFM. |
| 329 | # |
| 330 | # Below is an example showing the case when there is a need for an AVG NOP |
| 331 | # when RESHAPE is bypassed by replacing IFM with OFM. |
| 332 | # |
| 333 | # Converts to And in bypass_memory |
| 334 | # ---> ---> |
| 335 | # -----ADD----- -----ADD----- -----ADD----- |
| 336 | # | | | | | | |
| 337 | # 1x6x6x10 1x6x6x10 1x6x6x10 1x6x6x10 1x6x6x10 1x6x6x10 |
| 338 | # RESHAPE MEAN AVG POOL MEAN AVG POOL MEAN |
| 339 | # | | | | |
| 340 | # 1x20x3x6 1x6x6x10 1x20x3x6 |
| 341 | # MEAN RESHAPE MEAN |
| 342 | # | |
| 343 | # 1x20x3x6 |
| 344 | # MEAN |
| 345 | ifm_has_multiple_cons = len(op.ifm.consumer_list) > 1 |
| 346 | |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 347 | # Check if operator ifm/ofm are sg ifm/ofm |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 348 | ifm_is_sg_ifm = op.ifm.ops[0].type in (Op.Placeholder, Op.SubgraphInput, Op.Const) |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 349 | ifm_is_sg_ofm = any(ifm_cons is None for ifm_cons in op.ifm.consumer_list) |
| 350 | ofm_is_sg_ofm = any(ofm_cons is None for ofm_cons in op.ofm.consumer_list) |
| 351 | # Check if ifm/ofm is produced respectively consumed by CPU |
Johan Alfvén | 5060ff5 | 2022-09-15 15:50:30 +0200 | [diff] [blame] | 352 | ifm_is_cpu_produced = any(ifm_prod is not None and not ifm_prod.run_on_npu for ifm_prod in op.ifm.ops) |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 353 | ofm_is_cpu_consumed = any(ofm_cons is not None and not ofm_cons.run_on_npu for ofm_cons in op.ofm.consumer_list) |
Johan Alfvén | 5060ff5 | 2022-09-15 15:50:30 +0200 | [diff] [blame] | 354 | |
Johan Alfvén | 48e5159 | 2022-09-28 20:06:25 +0200 | [diff] [blame] | 355 | if bypass_need_to_keep_ofm_shape(op): |
| 356 | # Bypass need to keep OFM shape |
| 357 | if ifm_has_multiple_cons: |
| 358 | # Rare case: |
| 359 | # IFM need to persist due to multiple consumers and copy op is needed |
| 360 | # OFM will replace IFM for the memory only op |
| 361 | insert_copy_op_before_op(op) |
| 362 | elif not (ofm_is_sg_ofm or ofm_is_cpu_consumed): |
| 363 | # Only one consumer and OFM is not subgraph output or cpu consumed, |
| 364 | # safe to replace ifm.shape by ofm.shape |
| 365 | # IFM can then replace OFM for the memory only op and no copy op is needed |
| 366 | op.ifm.shape = op.ofm.shape |
| 367 | |
| 368 | # Special case when when OFM is sg_ofm or cpu_consumed |
Johan Alfvén | 8484d6e | 2022-09-28 14:22:54 +0200 | [diff] [blame] | 369 | if (ifm_is_sg_ofm or ifm_is_sg_ifm or ifm_is_cpu_produced) and (ofm_is_sg_ofm or ofm_is_cpu_consumed): |
| 370 | # Both ifm and ofm need to persist, but only ifm need a copy, in order to remove the memory only operator. |
| 371 | insert_copy_op_after_tens(op.ifm) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 372 | |
| 373 | return op |
| 374 | |
| 375 | |
| 376 | def convert_depthwise_to_conv(op, arch, nng): |
| 377 | # Depthwise is equivalent to a single conv2d if the ifm depth is 1 and |
| 378 | # the ofm depth equals the depth multipler. |
| 379 | # If those conditions are true, then we can perform a simple |
| 380 | # switch of the operator type (and weight order) |
| 381 | |
| 382 | if op.type == Op.DepthwiseConv2DBias and (op.attrs["depth_multiplier"] != 1): |
| 383 | ifm_shape = op.ifm_shapes[0] |
| 384 | weight_tensor = op.inputs[1] |
| 385 | ofm_shape = op.ofm_shapes[0] |
| 386 | if (ifm_shape.depth == 1) and (ofm_shape.depth == op.attrs["depth_multiplier"]): |
| 387 | # Change op type to Conv2d |
| 388 | op.type = Op.Conv2DBias |
| 389 | del op.attrs["channel_multiplier"] |
| 390 | del op.attrs["depth_multiplier"] |
| 391 | |
| 392 | weight_tensor.values = np.transpose(weight_tensor.values, (0, 1, 3, 2)) |
| 393 | weight_tensor.set_all_shapes(list(weight_tensor.values.shape)) |
wilisa01 | 79a8904 | 2022-11-02 17:18:43 +0000 | [diff] [blame^] | 394 | DebugDatabase.add_optimised(op, op) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 395 | else: |
| 396 | raise UnsupportedFeatureError( |
| 397 | f"Unsupported 'DEPTHWISE_CONV_2D' with depth_multiplier = {op.attrs['depth_multiplier']},", |
| 398 | f" ifm channels = {ifm_shape.depth}, ofm channels = {ofm_shape.depth}", |
| 399 | ) |
Patrik Gustavsson | df99510 | 2021-08-23 15:33:59 +0200 | [diff] [blame] | 400 | return op |
Patrik Gustavsson | f436ada | 2021-09-14 14:56:48 +0200 | [diff] [blame] | 401 | |
| 402 | |
| 403 | def convert_to_lut(op, lut_values, lut_name): |
| 404 | # Rewrite the operation by Add with scalar 0 + LUT activation |
| 405 | ifm = op.inputs[0] |
| 406 | if ifm is None: |
| 407 | return op |
| 408 | assert ifm.dtype.size_in_bytes() == 1 |
| 409 | op.type = Op.Add |
| 410 | op.name = op.name + "_lut_" + lut_name |
| 411 | # Mark as no-op to enable potential fusing optimizations |
| 412 | op.attrs["is_nop"] = True |
| 413 | # Create an input tensor containing scalar zero |
| 414 | quantization = QuantizationParameters(0.0, 255.0) |
| 415 | quantization.scale_f32 = ifm.quantization.scale_f32 |
| 416 | quantization.zero_point = 0 |
| 417 | tens = create_const_tensor(op.inputs[0].name + "_scalar0", [], ifm.dtype, [0], np.uint8, quantization=quantization) |
| 418 | op.add_input_tensor(tens) |
| 419 | op.ifm_shapes.append(Shape4D(tens.shape)) # TODO no shape? |
| 420 | |
| 421 | # The LUT must be applied without any preceding rescaling (the LUT itself performs the rescale), |
| 422 | # so even if the OFM has a different scale than the IFM, the generated OFM scale instructions |
| 423 | # should be the same as the IFM |
| 424 | op.forced_output_quantization = ifm.quantization |
| 425 | lut_tensor = lut.create_lut_tensor(op.name + "_values", lut_values, DataType.int8) |
| 426 | op.set_activation_lut(lut_tensor) |
| 427 | op.set_ifm_ofm_shapes() |
wilisa01 | 79a8904 | 2022-11-02 17:18:43 +0000 | [diff] [blame^] | 428 | DebugDatabase.add_optimised(op, op) |
Patrik Gustavsson | f436ada | 2021-09-14 14:56:48 +0200 | [diff] [blame] | 429 | return op |