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 | # Early optimisation of the network graph, using the rewrite_graph module to do the traversal of the graph. These are |
| 18 | # split into two parts optimise_graph_a and optimise_graph_b. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 19 | import math |
Diqing Zhong | 016b827 | 2020-12-16 16:46:06 +0100 | [diff] [blame] | 20 | import uuid |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 21 | |
| 22 | import numpy as np |
| 23 | |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 24 | from . import fp_math |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 25 | from . import lut |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 26 | from . import rewrite_graph |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 27 | from . import scaling |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 28 | from .data_type import DataType |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 29 | from .debug_database import DebugDatabase |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 30 | from .errors import UnsupportedFeatureError |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 31 | from .ethos_u55_regs.ethos_u55_regs import resampling_mode |
Louis Verhaard | 8912c53 | 2020-09-30 12:11:49 +0200 | [diff] [blame] | 32 | from .numeric_util import clamp_sigmoid |
Louis Verhaard | e0ef273 | 2020-06-03 08:56:44 +0200 | [diff] [blame] | 33 | from .numeric_util import full_shape |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 34 | from .numeric_util import round_away_zero |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 35 | from .operation import create_activation_function |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 36 | from .operation import NpuBlockType |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 37 | from .operation import Op |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 38 | from .operation import Operation |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 39 | from .operation import Padding |
Fredrik Svedberg | d9c2c42 | 2020-12-01 16:33:45 +0100 | [diff] [blame] | 40 | from .operation_util import create_avgpool_nop |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 41 | from .shape4d import Shape4D |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 42 | from .softmax import SoftMax |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 43 | from .tensor import check_quantized_tens_scaling_equal |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 44 | from .tensor import create_const_tensor |
| 45 | from .tensor import create_reshape_tensor |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 46 | from .tensor import QuantizationParameters |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 47 | from .tensor import Tensor |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 48 | from .tflite_mapping import optype_to_builtintype |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 49 | |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 50 | passthrough_nodes = (Op.Identity,) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 51 | |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 52 | memory_only_ops = (Op.Reshape,) |
Michael McGeagh | 11b0bdb | 2020-09-08 11:07:35 +0100 | [diff] [blame] | 53 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 54 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 55 | def remove_passthrough_tensor(tens, arch, nng): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 56 | if len(tens.ops) == 1 and tens.ops[0].type in passthrough_nodes: |
| 57 | assert len(tens.ops[0].inputs) == 1 |
| 58 | tens = tens.ops[0].inputs[0] |
| 59 | return tens |
| 60 | |
| 61 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 62 | def rewrite_concat(tens, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 63 | if len(tens.ops) == 1 and tens.ops[0].type.is_concat_op(): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 64 | concat_op = tens.ops[0] |
| 65 | if tens != concat_op.outputs[0]: |
| 66 | return tens # don't attempt to rewrite the min/max outputs of QuantizedConcat |
| 67 | |
| 68 | # Not supported so leave it and run on CPU |
| 69 | if not concat_op.run_on_npu: |
| 70 | return tens |
| 71 | |
| 72 | inputs, axis = concat_op.get_concat_inputs_axis() |
| 73 | |
| 74 | tens.ops = [] |
| 75 | offset = 0 |
| 76 | for idx, inp in enumerate(inputs): |
Patrik Gustavsson | 3d73717 | 2020-12-22 10:40:51 +0100 | [diff] [blame] | 77 | if axis >= 0: |
| 78 | axis_4D = axis + (4 - len(inp.shape)) |
| 79 | else: |
| 80 | axis_4D = axis |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 81 | new_op = Operation(Op.ConcatSliceWrite, concat_op.name + str(idx)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 82 | new_op.inputs = [inp] |
| 83 | new_op.outputs = [tens] |
Patrik Gustavsson | 3d73717 | 2020-12-22 10:40:51 +0100 | [diff] [blame] | 84 | new_op.attrs["concat_axis"] = axis_4D |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 85 | new_op.attrs["concat_start"] = offset |
| 86 | offset += inp.shape[axis] |
| 87 | new_op.attrs["concat_end"] = offset |
| 88 | new_op.run_on_npu = True |
| 89 | tens.ops.append(new_op) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 90 | DebugDatabase.add_optimised(concat_op, new_op) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 91 | new_op.set_ifm_ofm_shapes() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 92 | assert tens.shape[axis] == offset |
| 93 | |
Patrik Gustavsson | 29d568e | 2020-08-18 10:11:21 +0200 | [diff] [blame] | 94 | # If axis corresponds to C-dimension, NHCWB16 can only be used in the output if all the concat_start's are a |
| 95 | # multiple of 16. This as, it is only then the address offset for the ofm, for all operations, will be 16 byte |
| 96 | # aligned. For other values of axis the address offsets will be 16 byte aligned, as they are all based on c = 0 |
Patrik Gustavsson | 458a208 | 2020-08-13 13:41:05 +0200 | [diff] [blame] | 97 | # and those addresses are always 16 byte aligned due to the NHCWB16 format. |
Patrik Gustavsson | 6c97e9a | 2020-09-23 11:02:18 +0200 | [diff] [blame] | 98 | if axis == -1 or axis == (len(tens.shape) - 1): |
Patrik Gustavsson | 458a208 | 2020-08-13 13:41:05 +0200 | [diff] [blame] | 99 | for op in tens.ops: |
| 100 | if op.attrs["concat_start"] % 16 != 0: |
| 101 | tens.avoid_NHCWB16 = True |
| 102 | break |
| 103 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 104 | return tens |
| 105 | |
| 106 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 107 | def rewrite_split(tens, arch, nng): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 108 | |
Patrik Gustavsson | 224e99b | 2021-01-14 10:55:43 +0100 | [diff] [blame^] | 109 | if len(tens.ops) == 1 and tens.ops[0].type.is_split_op() and tens.ops[0].type != Op.Unpack: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 110 | split_op = tens.ops[0] |
| 111 | |
| 112 | # Not supported so leave it and run on CPU |
| 113 | if not split_op.run_on_npu: |
| 114 | return tens |
| 115 | |
| 116 | inp, outputs, axis, offset_start, offset_end = split_op.get_split_inputs_axis() |
| 117 | |
| 118 | tens.ops = [] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 119 | new_op = Operation(Op.SplitSliceRead, split_op.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 120 | new_op.inputs = [inp] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 121 | |
| 122 | # For Split the offset cannot be extracted from the tensor so it has to |
| 123 | # be calculated from the index of the output tensor |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 124 | if axis is not None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 125 | # Get the start and end of the split |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 126 | offset_start = [0] * 4 |
| 127 | for idx, out in enumerate(outputs): |
Patrik Gustavsson | 224e99b | 2021-01-14 10:55:43 +0100 | [diff] [blame^] | 128 | split_op.ofm_shapes[idx] = Shape4D(out.shape) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 129 | if out == tens: |
| 130 | break |
Patrik Gustavsson | 3d73717 | 2020-12-22 10:40:51 +0100 | [diff] [blame] | 131 | if axis >= 0: |
| 132 | axis_4D = axis + (4 - len(out.shape)) |
| 133 | else: |
| 134 | axis_4D = axis |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 135 | |
| 136 | offset_start[axis_4D] += split_op.ofm_shapes[idx].get_dim(axis_4D) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 137 | |
Patrik Gustavsson | eebb1c2 | 2020-08-18 15:03:04 +0200 | [diff] [blame] | 138 | # If start offset is not a multiple of 16 in the C-dimension, NHCWB16 need to be avoided in the input |
| 139 | if (offset_start[-1] % 16) != 0: |
| 140 | inp.avoid_NHCWB16 = True |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 141 | else: |
| 142 | offset_start = full_shape(4, offset_start, 0) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 143 | |
| 144 | new_op.attrs["split_start"] = offset_start |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 145 | new_op.run_on_npu = True |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 146 | new_op.set_output_tensor(tens) |
Patrik Gustavsson | 224e99b | 2021-01-14 10:55:43 +0100 | [diff] [blame^] | 147 | new_op.ifm_shapes.append(Shape4D(inp.shape)) |
| 148 | new_op.ofm_shapes.append(Shape4D(full_shape(4, tens.shape, 1))) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 149 | DebugDatabase.add_optimised(split_op, new_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 150 | |
| 151 | return tens |
| 152 | |
| 153 | |
| 154 | def needed_total_padding(input_size, stride, filter_size): |
| 155 | out_size = (input_size + stride - 1) // stride |
| 156 | needed_input = (out_size - 1) * stride + filter_size |
| 157 | total_padding = max(0, needed_input - input_size) |
| 158 | return total_padding |
| 159 | |
| 160 | |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame] | 161 | def calc_padding_and_skirt(padding_type, kernel_size, stride, input_dims, explicit_padding): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 162 | ypad = needed_total_padding(int(input_dims[1]), int(stride[1]), int(kernel_size[0])) |
| 163 | xpad = needed_total_padding(int(input_dims[2]), int(stride[2]), int(kernel_size[1])) |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 164 | if padding_type == Padding.SAME: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 165 | left_pad = (xpad + 0) // 2 |
| 166 | right_pad = (xpad + 1) // 2 |
| 167 | top_pad = (ypad + 0) // 2 |
| 168 | bottom_pad = (ypad + 1) // 2 |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 169 | elif padding_type == Padding.VALID: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 170 | left_pad = 0 |
| 171 | right_pad = 0 |
| 172 | top_pad = 0 |
| 173 | bottom_pad = 0 |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame] | 174 | elif padding_type == Padding.EXPLICIT: |
| 175 | # Padding is specified in a PAD operator which has been bypassed. |
| 176 | # The top and left padding are taken from the PAD; bottom and right are calculated. |
| 177 | top_pad, left_pad, _, _ = explicit_padding |
| 178 | bottom_pad = ypad - top_pad |
| 179 | right_pad = xpad - left_pad |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 180 | else: |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 181 | raise UnsupportedFeatureError(f"Unknown padding") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 182 | padding = (top_pad, left_pad, bottom_pad, right_pad) |
| 183 | skirt = (top_pad, left_pad, ypad - top_pad, xpad - left_pad) |
| 184 | return padding, skirt |
| 185 | |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 186 | |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 187 | def calc_upscaled_padding_and_skirt(padding_type, kernel_size, stride, input_dims, upscaling_factor): |
| 188 | kernel_height, kernel_width = kernel_size[0], kernel_size[1] |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 189 | if padding_type == Padding.SAME: |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 190 | ypad = needed_total_padding(int(input_dims[1]) * upscaling_factor, int(stride[1]), int(kernel_height)) |
| 191 | xpad = needed_total_padding(int(input_dims[2]) * upscaling_factor, int(stride[2]), int(kernel_width)) |
Jacob Bohlin | d47cc27 | 2020-08-24 11:42:14 +0200 | [diff] [blame] | 192 | right_pad = max(((xpad + 1) // upscaling_factor) - 1, 0) |
| 193 | bottom_pad = max(((ypad + 1) // upscaling_factor) - 1, 0) |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 194 | left_pad = max(kernel_width - 1 - right_pad, 0) |
| 195 | top_pad = max(kernel_height - 1 - bottom_pad, 0) |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 196 | elif padding_type == Padding.VALID: |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 197 | right_pad = max(kernel_width - 2, 0) |
| 198 | bottom_pad = max(kernel_height - 2, 0) |
| 199 | left_pad = kernel_width - 1 |
| 200 | top_pad = kernel_height - 1 |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 201 | else: |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 202 | raise UnsupportedFeatureError(f"Unknown padding") |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 203 | padding = (top_pad, left_pad, bottom_pad, right_pad) |
Jacob Bohlin | 9b64ba0 | 2020-07-07 17:15:22 +0200 | [diff] [blame] | 204 | skirt = padding |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 205 | return padding, skirt |
| 206 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 207 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 208 | def fixup_conv2d_backprop(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 209 | if op.type == Op.Conv2DBackpropInput: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 210 | # flip the inputs |
| 211 | op.inputs[0], op.inputs[2] = op.inputs[2], op.inputs[0] |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 212 | op.set_ifm_ofm_shapes() |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 213 | op.type = Op.Conv2DBackpropInputSwitchedBias |
Louis Verhaard | 69b8480 | 2020-12-16 12:02:28 +0100 | [diff] [blame] | 214 | op.ifm.resampling_mode = resampling_mode.TRANSPOSE |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 215 | |
| 216 | # Update strides |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 217 | op.attrs.update({"stride_w": 1, "stride_h": 1, "strides": (1, 1, 1, 1)}) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 218 | |
| 219 | return op |
| 220 | |
| 221 | |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 222 | # Convert the op to an elementwise add |
| 223 | def convert_resizebilinear_1x1_to_add(op): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 224 | op.type = Op.Add |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 225 | op.name = op.name + "_add" |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 226 | op.attrs["resizebilinear"] = True |
| 227 | # Create an input tensor filled with zeros |
| 228 | shape = op.outputs[0].shape |
| 229 | tens = Tensor(shape, op.inputs[0].dtype, op.inputs[1].name + "_add") |
| 230 | tens.values = np.zeros(shape) |
| 231 | tens.quant_values = np.zeros(shape, np.uint8) |
| 232 | tens.quantization = QuantizationParameters(0.0, 255.0) |
| 233 | tens.quantization.scale_f32 = 1.0 |
| 234 | tens.quantization.zero_point = 0 |
| 235 | tens.consumer_list = [op] |
| 236 | tens_op = op.inputs[1].ops[0] |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 237 | tens_op.set_output_tensor(tens) |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 238 | # Set the add inputs |
| 239 | op.inputs[1] = op.inputs[0] |
| 240 | op.inputs[0] = tens |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 241 | op.set_ifm_ofm_shapes() |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 242 | |
| 243 | return op |
| 244 | |
| 245 | |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 246 | # Convert ResizeBilinear to a number of 2x2 pool ops |
| 247 | def convert_resizebilinear_to_2x2_pool(op): |
| 248 | count = 0 |
| 249 | pre_op = op |
| 250 | outputs = op.outputs |
| 251 | |
| 252 | op.attrs.update({"strides": (1, 1, 1, 1), "ksize": (1, 2, 2, 1)}) |
| 253 | if op.attrs["align_corners"]: |
| 254 | shape_modifier = 1 |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 255 | op.attrs["padding"] = Padding.VALID |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 256 | else: |
| 257 | shape_modifier = 0 |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 258 | op.attrs["padding"] = Padding.SAME |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 259 | op.inputs[0].resampling_mode = resampling_mode.NEAREST |
| 260 | |
| 261 | upscaled_shape = np.array(op.inputs[0].shape[1:3]) |
| 262 | out_shape = np.array(op.outputs[0].shape[1:3]) |
| 263 | if (upscaled_shape == upscaled_shape * 2 - shape_modifier).all(): |
| 264 | return op |
| 265 | |
| 266 | while (upscaled_shape < out_shape).all(): |
| 267 | if count == 0: |
| 268 | scaled_op = pre_op |
| 269 | else: |
| 270 | scaled_op = op.clone("_{}".format(count)) |
| 271 | scaled_op.inputs[0] = pre_op.outputs[0] |
| 272 | |
| 273 | upscaled_shape = upscaled_shape * 2 - shape_modifier |
| 274 | |
| 275 | if (upscaled_shape == out_shape).all(): |
| 276 | scaled_op.outputs = outputs |
| 277 | scaled_op.outputs[0].ops = [scaled_op] |
| 278 | else: |
| 279 | shape = outputs[0].shape.copy() |
| 280 | shape[1:3] = upscaled_shape[0:2] |
| 281 | out_tens = Tensor(shape, DataType.int16, "{}_{}".format(op.outputs[0].name, count)) |
| 282 | out_tens.quantization = op.outputs[0].quantization.clone() |
| 283 | out_tens.quantization.quant_min = np.iinfo(np.int16).min |
| 284 | out_tens.quantization.quant_max = np.iinfo(np.int16).max |
| 285 | scaled_op.set_output_tensor(out_tens) |
| 286 | pre_op = scaled_op |
| 287 | count += 1 |
| 288 | |
| 289 | # Setup the scale value |
| 290 | if scaled_op.inputs[0].dtype.bits == 8 and scaled_op.outputs[0].dtype.bits == 16: |
| 291 | scaled_op.attrs["rescale"] = 128 |
| 292 | elif scaled_op.inputs[0].dtype.bits == 16 and scaled_op.outputs[0].dtype.bits == 8: |
| 293 | scaled_op.attrs["rescale"] = 1 / 128 |
| 294 | elif "rescale" in scaled_op.attrs: |
| 295 | del scaled_op.attrs["rescale"] |
Patrik Gustavsson | cc6915c | 2020-12-22 09:16:50 +0100 | [diff] [blame] | 296 | scaled_op.set_ifm_ofm_shapes() |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 297 | |
| 298 | return op |
| 299 | |
| 300 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 301 | def fixup_resizebilinear(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 302 | if op.type == Op.ResizeBilinear and op.run_on_npu: |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 303 | if op.inputs[0].shape == op.outputs[0].shape: |
Charles Xu | 36ffaf3 | 2020-08-05 15:40:44 +0200 | [diff] [blame] | 304 | # Bypass nop resizebilinear |
| 305 | op.inputs = op.inputs[:1] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 306 | op.type = Op.Identity |
Charles Xu | 87c1350 | 2020-08-06 12:17:26 +0200 | [diff] [blame] | 307 | elif op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1: |
| 308 | convert_resizebilinear_1x1_to_add(op) |
| 309 | else: |
| 310 | convert_resizebilinear_to_2x2_pool(op) |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 311 | |
| 312 | return op |
| 313 | |
| 314 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 315 | def convert_nop_split_to_identity(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 316 | if op.type == Op.Split and op.attrs.get("num_splits") == 1: |
Dwight Lidman | c3862c2 | 2020-09-14 15:22:33 +0200 | [diff] [blame] | 317 | # the list comprehension should return a list with a single tensor |
| 318 | # if it shouldn't, remove_passthrough_tensor will fail appropriately |
| 319 | op.inputs = [i for i in op.inputs if i.shape == op.outputs[0].shape] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 320 | op.type = Op.Identity |
Dwight Lidman | c3862c2 | 2020-09-14 15:22:33 +0200 | [diff] [blame] | 321 | return op |
| 322 | |
| 323 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 324 | def fixup_fully_connected_input(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 325 | if op.type == Op.FullyConnected: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 326 | inp = op.inputs[0] |
| 327 | weights = op.inputs[1] |
| 328 | |
| 329 | n_in_elems = weights.shape[-2] |
| 330 | elms = inp.elements() |
| 331 | batch_size = elms // n_in_elems |
| 332 | assert batch_size * n_in_elems == elms |
| 333 | |
| 334 | desired_shape = [batch_size, n_in_elems] |
| 335 | if inp.shape != desired_shape: |
| 336 | # mismatch, insert a reshape to fix this. |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 337 | op.set_input_tensor(create_reshape_tensor(inp, desired_shape), 0) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 338 | |
| 339 | return op |
| 340 | |
| 341 | |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 342 | def convert_batched_fc_shape(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 343 | if op.type == Op.FullyConnected: |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 344 | ifm = op.inputs[0] |
| 345 | ofm = op.outputs[0] |
| 346 | # Check if the FC is 2D and first dimension indicates batching |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 347 | # TOD0 op.ifm_shape[0] > 1 is enough when refactory is complete |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 348 | if len(ifm.shape) == len(ofm.shape) == 2 and ifm.shape[0] > 1 and op.ifm_shapes[0].batch > 1: |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 349 | n = ifm.shape[0] |
| 350 | batching_split = {4: (2, 2), 8: (2, 4), 16: (4, 4)} |
| 351 | h, w = batching_split.get(n, (1, n)) |
| 352 | |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 353 | prev_op = ifm.ops[0] |
| 354 | desired_shape = [1, h, w, ifm.shape[-1]] |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 355 | op.ifm_shapes[0] = Shape4D(desired_shape) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 356 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 357 | if len(ifm.consumer_list) == 1 and prev_op is not None and prev_op.type == Op.Reshape: |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 358 | # There is a preceding Reshape |
| 359 | # Compare input of prev_op and input of op, to see if prev_op can be removed |
| 360 | ifm_prev_op = prev_op.inputs[0] |
Tim Hall | 8956761 | 2020-10-27 11:57:57 +0000 | [diff] [blame] | 361 | if ifm_prev_op.shape == ifm.shape and check_quantized_tens_scaling_equal(ifm_prev_op, ifm): |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 362 | # prev_op can be removed |
| 363 | op.set_input_tensor(ifm_prev_op, 0) |
| 364 | else: |
| 365 | op.inputs[0].set_all_shapes(desired_shape) |
| 366 | prev_op.set_input_tensor( |
| 367 | create_const_tensor(prev_op.inputs[1].name, [1], DataType.int32, desired_shape), 1 |
| 368 | ) |
| 369 | prev_op.attrs["new_shape"] = desired_shape |
| 370 | else: |
| 371 | # Add reshape op to the input if there is no preceding reshape |
| 372 | ifm.consumer_list.remove(op) |
| 373 | op.set_input_tensor(create_reshape_tensor(ifm, desired_shape), 0) |
| 374 | |
| 375 | # Reshape Weights to be 4D. IO becomes HWIO |
| 376 | weight_tensor = op.inputs[1] |
| 377 | weight_tensor.quant_values = np.expand_dims(np.expand_dims(weight_tensor.quant_values, axis=0), axis=0) |
| 378 | weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape)) |
| 379 | |
| 380 | desired_shape = [1, h, w, ofm.shape[-1]] |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 381 | op.ofm_shapes[0] = Shape4D(desired_shape) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 382 | |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 383 | if ( |
| 384 | len(ofm.consumer_list) == 1 |
| 385 | and ofm.consumer_list[0] is not None |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 386 | and ofm.consumer_list[0].type == Op.Reshape |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 387 | ): |
| 388 | # There is a subsequent Reshape |
| 389 | # Compare desired shape and output of consumer op, to see if consumer op can be removed |
| 390 | ofm_cons_op = ofm.consumer_list[0].outputs[0] |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 391 | if desired_shape == ofm_cons_op.shape and check_quantized_tens_scaling_equal(ofm, ofm_cons_op): |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 392 | op.outputs[0] = ofm_cons_op |
| 393 | op.outputs[0].ops = [op] |
| 394 | else: |
| 395 | op.outputs[0].set_all_shapes(desired_shape) |
| 396 | else: |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 397 | # Add reshape op to the output |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 398 | op.set_output_tensor(create_reshape_tensor(ofm, desired_shape, False)) |
| 399 | return op |
| 400 | |
| 401 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 402 | def fixup_pack_input(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 403 | if op.type == Op.Pack: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 404 | # Pack is also referred to as Stack |
| 405 | # Requires the rewrite_concat function to be called on the op afterwards |
| 406 | axis = int(op.attrs["axis"]) |
| 407 | desired_shape = op.inputs[0].shape[:axis] + [1] + op.inputs[0].shape[axis:] |
| 408 | |
| 409 | # Construct 1 shape tensor to be used by all inserted reshape ops |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 410 | new_shape_tens = create_const_tensor(op.name + "_reshape_shape", [1], DataType.int32, desired_shape) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 411 | |
| 412 | for idx, inp in enumerate(op.inputs): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 413 | reshape_out = inp.clone("_reshaped") |
Michael McGeagh | 6a8d424 | 2020-07-28 12:17:59 +0100 | [diff] [blame] | 414 | reshape_out.set_all_shapes(desired_shape) |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 415 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 416 | reshape_op = Operation(Op.Reshape, "{}{}_reshape".format(op.name, idx)) |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 417 | reshape_op.attrs["new_shape"] = desired_shape |
| 418 | reshape_op.inputs = [inp, new_shape_tens] |
| 419 | reshape_op.set_output_tensor(reshape_out) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 420 | reshape_op.set_ifm_ofm_shapes() |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 421 | DebugDatabase.add_optimised(op, reshape_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 422 | |
| 423 | op.inputs[idx] = reshape_out |
| 424 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 425 | op.type = Op.PackReshaped |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 426 | |
| 427 | return op |
| 428 | |
| 429 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 430 | def unfuse_activation_function(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 431 | if op.type == Op.ConcatTFLite and op.run_on_npu and op.activation is not None: |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 432 | act_op = Operation(op.activation.op_type, op.name + op.activation.op_type.name) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 433 | op.activation = None |
Fredrik Svedberg | 0f98b36 | 2020-09-29 10:00:39 +0200 | [diff] [blame] | 434 | out_tens = op.outputs[0] |
| 435 | intermediate_tens = out_tens.clone("_act_intermediate") |
| 436 | act_op.set_output_tensor(out_tens) |
| 437 | act_op.add_input_tensor(intermediate_tens) |
| 438 | op.set_output_tensor(intermediate_tens) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 439 | act_op.set_ifm_ofm_shapes() |
Fredrik Svedberg | 0f98b36 | 2020-09-29 10:00:39 +0200 | [diff] [blame] | 440 | |
| 441 | return op |
| 442 | |
Louis Verhaard | 8912c53 | 2020-09-30 12:11:49 +0200 | [diff] [blame] | 443 | |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 444 | def fixup_stridedslice_output(tens, arch, nng): |
| 445 | op = tens.ops[0] |
Dwight Lidman | 73320a4 | 2020-11-05 10:34:41 +0100 | [diff] [blame] | 446 | if op.run_on_npu and op.type == Op.StridedSlice: |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 447 | reshape_input_shape = tens.shape |
| 448 | new_axis_mask = op.attrs["new_axis_mask"] |
| 449 | shrink_axis_mask = op.attrs["shrink_axis_mask"] |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 450 | |
Dwight Lidman | 73320a4 | 2020-11-05 10:34:41 +0100 | [diff] [blame] | 451 | if shrink_axis_mask != 0: |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 452 | n = 0 |
| 453 | axis = 0 |
| 454 | while shrink_axis_mask: |
| 455 | prev_mask = shrink_axis_mask |
| 456 | n += 1 |
| 457 | shrink_axis_mask &= shrink_axis_mask - 1 |
| 458 | axis = int(math.log2(prev_mask - shrink_axis_mask)) |
| 459 | reshape_input_shape = reshape_input_shape[:axis] + [1] + reshape_input_shape[axis:] |
| 460 | |
| 461 | assert len(tens.shape) == (len(op.inputs[0].shape) - n) |
| 462 | op.attrs["shrink_axis_mask"] = 0 |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 463 | elif new_axis_mask != 0: |
| 464 | n = 0 |
| 465 | axis = 0 |
| 466 | while new_axis_mask: |
| 467 | prev_mask = new_axis_mask |
| 468 | n += 1 |
| 469 | new_axis_mask &= new_axis_mask - 1 |
| 470 | axis = int(math.log2(prev_mask - new_axis_mask)) |
| 471 | reshape_input_shape = reshape_input_shape[:axis] + reshape_input_shape[(axis + 1) :] |
| 472 | new_axis_mask >>= 1 |
| 473 | |
| 474 | assert len(tens.shape) == (len(op.inputs[0].shape) + n) |
| 475 | op.attrs["new_axis_mask"] = 0 |
Dwight Lidman | dda21af | 2020-11-11 15:44:57 +0100 | [diff] [blame] | 476 | else: |
| 477 | # Equal Rank StridedSlice, no need to insert reshape |
| 478 | return tens |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 479 | |
| 480 | # Construct 1 shape tensor to be used by all inserted reshape ops |
| 481 | new_shape_tens = create_const_tensor(op.name + "_reshape_shape", [1], DataType.int32, tens.shape) |
| 482 | |
| 483 | for idx, out_tens in enumerate(op.outputs): |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 484 | op.ofm_shapes[idx] = Shape4D(new_shape_tens.shape) |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 485 | reshape_in = out_tens.clone("_reshaped") |
| 486 | reshape_in.set_all_shapes(reshape_input_shape) |
| 487 | reshape_in.ops = [op] |
| 488 | |
| 489 | reshape_op = Operation(Op.Reshape, "{}{}_reshape".format(op.name, idx)) |
| 490 | reshape_op.attrs["new_shape"] = reshape_input_shape |
| 491 | reshape_op.inputs = [reshape_in, new_shape_tens] |
| 492 | reshape_op.set_output_tensor(out_tens) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 493 | reshape_op.set_ifm_ofm_shapes() |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 494 | |
| 495 | op.outputs[idx] = reshape_in |
| 496 | |
| 497 | return tens |
| 498 | |
| 499 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 500 | def fixup_unpack_output(tens, arch, nng): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 501 | op = tens.ops[0] |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 502 | if op.run_on_npu and op.type == Op.Unpack: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 503 | # Unpack is also referred to as Unstack |
| 504 | # Requires the rewrite_split function to be called on the op afterwards |
Diqing Zhong | c7c0b1b | 2020-10-26 11:45:25 +0100 | [diff] [blame] | 505 | axis = int(op.attrs["axis"]) |
| 506 | op.type = Op.UnpackReshaped |
| 507 | reshape_input_shape = tens.shape[:axis] + [1] + tens.shape[axis:] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 508 | |
| 509 | # Construct 1 shape tensor to be used by all inserted reshape ops |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 510 | new_shape_tens = create_const_tensor(op.name + "_reshape_shape", [1], DataType.int32, tens.shape) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 511 | |
| 512 | for idx, out_tens in enumerate(op.outputs): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 513 | reshape_in = out_tens.clone("_reshaped") |
Michael McGeagh | 6a8d424 | 2020-07-28 12:17:59 +0100 | [diff] [blame] | 514 | reshape_in.set_all_shapes(reshape_input_shape) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 515 | reshape_in.ops = [op] |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 516 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 517 | reshape_op = Operation(Op.Reshape, "{}{}_reshape".format(op.name, idx)) |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 518 | reshape_op.attrs["new_shape"] = reshape_input_shape |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 519 | reshape_op.inputs = [reshape_in, new_shape_tens] |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 520 | reshape_op.set_output_tensor(out_tens) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 521 | reshape_op.set_ifm_ofm_shapes() |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 522 | DebugDatabase.add_optimised(op, reshape_op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 523 | |
| 524 | op.outputs[idx] = reshape_in |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 525 | return tens |
| 526 | |
| 527 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 528 | def add_padding_fields(op, arch, nng): |
Jacob Bohlin | 90033f3 | 2020-08-28 15:45:44 +0200 | [diff] [blame] | 529 | if op.run_on_npu: |
| 530 | if "padding" in op.attrs: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 531 | if op.type.is_conv2d_op() or op.type.is_depthwise_conv2d_op(): |
Jacob Bohlin | 90033f3 | 2020-08-28 15:45:44 +0200 | [diff] [blame] | 532 | kernel_size = op.inputs[1].shape[:2] |
| 533 | input_shape = op.inputs[0].shape |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 534 | elif op.type.is_pool_op() or op.type.npu_block_type == NpuBlockType.ReduceSum: |
Jacob Bohlin | 90033f3 | 2020-08-28 15:45:44 +0200 | [diff] [blame] | 535 | kernel_size = op.attrs["ksize"][1:3] |
| 536 | input_shape = op.inputs[0].shape |
Jacob Bohlin | 90033f3 | 2020-08-28 15:45:44 +0200 | [diff] [blame] | 537 | else: |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 538 | raise UnsupportedFeatureError(f"Unknown operation that uses padding: {optype_to_builtintype(op.type)}") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 539 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 540 | if op.type == Op.Conv2DBackpropInputSwitchedBias: |
Jacob Bohlin | 90033f3 | 2020-08-28 15:45:44 +0200 | [diff] [blame] | 541 | upscaling_factor = op.outputs[0].shape[1] // input_shape[1] |
| 542 | padding, skirt = calc_upscaled_padding_and_skirt( |
| 543 | op.attrs["padding"], kernel_size, op.attrs["strides"], input_shape, upscaling_factor |
| 544 | ) |
| 545 | else: |
| 546 | dilation_h, dilation_w = op.get_dilation_h_w() |
| 547 | dilated_kernel_size = [dilation_h * (kernel_size[0] - 1) + 1, dilation_w * (kernel_size[1] - 1) + 1] |
| 548 | padding, skirt = calc_padding_and_skirt( |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame] | 549 | op.attrs["padding"], |
| 550 | dilated_kernel_size, |
| 551 | op.attrs["strides"], |
| 552 | input_shape, |
| 553 | op.attrs.get("explicit_padding"), |
Jacob Bohlin | 90033f3 | 2020-08-28 15:45:44 +0200 | [diff] [blame] | 554 | ) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 555 | |
Jacob Bohlin | 90033f3 | 2020-08-28 15:45:44 +0200 | [diff] [blame] | 556 | op.attrs["explicit_padding"] = padding |
| 557 | op.attrs["skirt"] = skirt |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 558 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 559 | return op |
| 560 | |
| 561 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 562 | # Check if the op can be reordered |
| 563 | def get_prepend_op(op): |
| 564 | inp = op.inputs[0] |
| 565 | # The op should be reordered between prev_op and prep_op |
| 566 | prev_op = inp.ops[-1] |
| 567 | prep_op = None |
| 568 | while prev_op.type in memory_only_ops and len(prev_op.outputs) == 1 and len(prev_op.outputs[0].consumers()) == 1: |
| 569 | prep_op = prev_op |
| 570 | inp = prev_op.inputs[0] |
| 571 | prev_op = inp.ops[-1] |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 572 | if prev_op is not None and len(prev_op.outputs) == 1 and len(prev_op.outputs[0].consumers()) == 1: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 573 | return prep_op |
| 574 | |
| 575 | return None |
| 576 | |
| 577 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 578 | def convert_depthwise_to_conv(op, arch, nng): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 579 | # Depthwise is equivalent to a single conv2d if the ifm depth is 1 and |
| 580 | # the ofm depth equals the depth multipler. |
| 581 | # If those conditions are true, then we can perform a simple |
| 582 | # switch of the operator type (and weight order) |
| 583 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 584 | if op.type == Op.DepthwiseConv2DBias and (op.attrs["depth_multiplier"] != 1): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 585 | ifm_tensor = op.inputs[0] |
| 586 | weight_tensor = op.inputs[1] |
| 587 | ofm_tensor = op.outputs[0] |
| 588 | if (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"]): |
| 589 | # Change op type to Conv2d |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 590 | op.type = Op.Conv2DBias |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 591 | del op.attrs["channel_multiplier"] |
| 592 | del op.attrs["depth_multiplier"] |
| 593 | |
| 594 | weight_tensor.quant_values = np.transpose(weight_tensor.quant_values, (0, 1, 3, 2)) |
Michael McGeagh | 6a8d424 | 2020-07-28 12:17:59 +0100 | [diff] [blame] | 595 | weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 596 | else: |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 597 | raise UnsupportedFeatureError( |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 598 | f"Unsupported 'DEPTHWISE_CONV_2D' with depth_multiplier = {op.attrs['depth_multiplier']},", |
| 599 | f" ifm channels = {ifm_tensor.shape[3]}, ofm channels = {ofm_tensor.shape[3]}", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 600 | ) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 601 | DebugDatabase.add_optimised(op, op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 602 | return op |
| 603 | |
| 604 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 605 | def reorder_depthwise_weights(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 606 | if op.type.is_depthwise_conv2d_op(): |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 607 | weight_tensor = op.inputs[1] |
| 608 | weight_tensor.quant_values = np.transpose(weight_tensor.quant_values, (0, 1, 3, 2)) |
Michael McGeagh | 6a8d424 | 2020-07-28 12:17:59 +0100 | [diff] [blame] | 609 | weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape)) |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 610 | weight_tensor.weight_transpose_depthwise = True |
| 611 | |
| 612 | return op |
| 613 | |
| 614 | |
Diqing Zhong | 016b827 | 2020-12-16 16:46:06 +0100 | [diff] [blame] | 615 | def optimise_strided_conv(op, arch, nng): |
| 616 | stride_x, stride_y = op.get_kernel_stride() |
| 617 | ifm_tensor, _, weight_tensor, _ = op.get_ifm_ifm2_weights_ofm() |
| 618 | |
| 619 | if ( |
| 620 | op.type == Op.Conv2DBias |
| 621 | and op.op_index == 0 |
| 622 | and stride_x == 2 |
| 623 | and len(ifm_tensor.shape) == 4 |
| 624 | and ifm_tensor.shape[3] <= 4 |
| 625 | and ifm_tensor.shape[2] % 2 == 0 |
| 626 | and weight_tensor is not None |
| 627 | and weight_tensor.shape[1] >= 2 |
| 628 | ): |
| 629 | # IFM |
| 630 | ifm_reshaped = create_reshape_tensor( |
| 631 | ifm_tensor, [ifm_tensor.shape[0], ifm_tensor.shape[1], ifm_tensor.shape[2] // 2, ifm_tensor.shape[3] * 2] |
| 632 | ) |
| 633 | op.set_input_tensor(ifm_reshaped, 0) |
| 634 | |
| 635 | # Weights |
| 636 | weight_shape = weight_tensor.shape |
| 637 | if weight_shape[1] % 2 != 0: |
| 638 | weight_shape[1] = weight_shape[1] + 1 |
| 639 | padded_array = np.zeros(weight_shape) |
| 640 | for i in range(weight_shape[0]): |
| 641 | padded_array[i] = np.vstack( |
| 642 | [ |
| 643 | weight_tensor.quant_values[i], |
| 644 | np.full((1, weight_shape[2], weight_shape[3]), weight_tensor.quantization.zero_point), |
| 645 | ] |
| 646 | ) |
| 647 | weight_tensor.quant_values = padded_array |
| 648 | weight_shape[1] //= 2 |
| 649 | weight_shape[2] *= 2 |
| 650 | weight_tensor.quant_values = np.reshape(weight_tensor.quant_values, weight_shape) |
| 651 | weight_tensor.set_all_shapes(weight_shape) |
| 652 | # If multiple copies of the weights are used, we could avoid |
| 653 | # them having the same address by changing the value_id |
| 654 | weight_tensor.value_id = uuid.uuid4() |
| 655 | |
| 656 | # Strides |
| 657 | stride_x = 1 |
| 658 | op.attrs.update({"stride_w": stride_x, "stride_h": stride_y, "strides": (1, stride_y, stride_x, 1)}) |
| 659 | |
| 660 | op.set_ifm_ofm_shapes() |
| 661 | |
| 662 | return op |
| 663 | |
| 664 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 665 | def convert_conv_to_fc(op, arch, nng): |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 666 | # Conv 1x1 can be equivalent to Fully Connected. |
| 667 | # By representing certain convs as fully connected layers, Vela can better determine wether or not to use |
| 668 | # caching/double buffering for the weights. |
| 669 | # (Weights dont need to be reloaded for convs when IFM H and W are 1) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 670 | if op.type == Op.Conv2DBias: |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 671 | h = op.ifm_shapes[0].height |
| 672 | w = op.ifm_shapes[0].width |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 673 | kh, kw, _, _ = op.inputs[1].shape |
| 674 | if h == 1 and w == 1 and kh == 1 and kw == 1: |
| 675 | # Overwrite this op as a Fully Connected Op |
| 676 | op.name += "_fc" |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 677 | op.type = Op.FullyConnected |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 678 | op.attrs = { |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 679 | "weights_format": 0, |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 680 | } |
| 681 | # Reshape Weights to be 2D. HWIO becomes just IO (as H and W are 1, they can just be dropped) |
| 682 | weight_tensor = op.inputs[1] |
| 683 | weight_tensor.quant_values = weight_tensor.quant_values.squeeze(axis=(0, 1)) |
| 684 | weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape)) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 685 | |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 686 | # The output from a fully connected is expected to be 2D so we need to add a reshape layer to convert it |
| 687 | # back to 4D afterwards as the next layer is expecting that shape |
| 688 | orig_ofm_tensor = op.outputs[0] |
| 689 | # Reshape this ops output to be 2D: {(N*H*W), C} (We know N H and W are all 1 so this becomes {1, C}) |
| 690 | fc_ofm_tensor = orig_ofm_tensor.clone("_fc") |
| 691 | fc_ofm_tensor.set_all_shapes([1, fc_ofm_tensor.shape[-1]]) |
| 692 | fc_ofm_tensor.ops = [op] |
| 693 | # Add a reshape after the new OFM to convert it back to the original 4D shape |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 694 | reshape_name = op.name + "_reshape" |
| 695 | new_shape_tens = create_const_tensor(reshape_name + "_shape", [1], DataType.int32, orig_ofm_tensor.shape) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 696 | reshape_op = Operation(Op.Reshape, reshape_name) |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 697 | reshape_op.attrs["new_shape"] = orig_ofm_tensor.shape |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 698 | reshape_op.inputs = [fc_ofm_tensor, new_shape_tens] |
| 699 | reshape_op.set_output_tensor(orig_ofm_tensor) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 700 | reshape_op.set_ifm_ofm_shapes() |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 701 | |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 702 | # Replace this ops OFM to point to the 2D tensor |
| 703 | op.outputs[0] = fc_ofm_tensor |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 704 | op.set_ifm_ofm_shapes() |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 705 | # Record optimisation in debug database |
| 706 | DebugDatabase.add_optimised(op, reshape_op) |
| 707 | DebugDatabase.add_optimised(op, op) |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 708 | return op |
| 709 | |
| 710 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 711 | def fixup_relus_with_differing_ifm_ofm_scaling(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 712 | if op.run_on_npu and op.type.is_relu_op(): |
Michael McGeagh | 8dbf8cf | 2020-09-08 11:09:48 +0100 | [diff] [blame] | 713 | ifm = op.inputs[0] |
| 714 | ofm = op.outputs[0] |
| 715 | # Relu with differing IFM and OFM scaling cannot be fused with another primary op |
| 716 | # and requires its own to be inserted |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 717 | if not check_quantized_tens_scaling_equal(ifm, ofm): |
Michael McGeagh | 8dbf8cf | 2020-09-08 11:09:48 +0100 | [diff] [blame] | 718 | # Override this op with its own primary op (avgpool) |
| 719 | relu_fused_op = create_avgpool_nop(op.name + "_avgpool") |
| 720 | # And fuse the original activation function to it |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 721 | relu_fused_op.activation = create_activation_function(op.type) |
Michael McGeagh | 8dbf8cf | 2020-09-08 11:09:48 +0100 | [diff] [blame] | 722 | # Tidy up and assign the ifm and ofm to the new op |
| 723 | ifm.consumer_list.remove(op) |
Andreas Nevalainen | f3d737e | 2020-09-25 14:12:43 +0200 | [diff] [blame] | 724 | |
| 725 | # if not 4d, reshape ifm/ofm |
| 726 | if len(ifm.shape) < 4: |
| 727 | ifm_shaped = create_reshape_tensor(ifm, full_shape(4, ifm.shape, 1)) |
| 728 | ifm = ifm_shaped |
| 729 | if len(ofm.shape) < 4: |
| 730 | ofm_shaped = create_reshape_tensor(ofm, full_shape(4, ofm.shape, 1), False) |
| 731 | ofm = ofm_shaped |
| 732 | |
Michael McGeagh | 8dbf8cf | 2020-09-08 11:09:48 +0100 | [diff] [blame] | 733 | relu_fused_op.add_input_tensor(ifm) |
| 734 | relu_fused_op.set_output_tensor(ofm) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 735 | relu_fused_op.set_ifm_ofm_shapes() |
Michael McGeagh | 8dbf8cf | 2020-09-08 11:09:48 +0100 | [diff] [blame] | 736 | op = relu_fused_op |
| 737 | return op |
| 738 | |
| 739 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 740 | # Reorder activation op if it's after the memory only operations |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 741 | def fixup_act_reorder(op, arch, nng): |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 742 | if op.type.is_relu_op() or op.type in (Op.Sigmoid, Op.Tanh): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 743 | prep_op = get_prepend_op(op) |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 744 | if prep_op is not None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 745 | act_op = op.clone("_reordered") |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 746 | act_op.ifm_shapes = list(op.ifm_shapes) |
| 747 | act_op.ofm_shapes = list(op.ofm_shapes) |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 748 | |
| 749 | # There is only one input tensor, overwrite it |
| 750 | act_op.set_input_tensor(prep_op.inputs[0], 0) |
| 751 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 752 | act_op_out = act_op.inputs[0].clone("_acted") |
| 753 | act_op_out.quantization = op.outputs[0].quantization.clone() |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 754 | act_op.set_output_tensor(act_op_out) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 755 | act_op.ifm_shapes[0] = Shape4D(prep_op.inputs[0].shape) |
| 756 | act_op.ofm_shapes[0] = Shape4D(act_op_out.shape) |
Patrik Gustavsson | cb33704 | 2020-09-16 14:55:40 +0200 | [diff] [blame] | 757 | |
| 758 | # Update the consumer list |
| 759 | act_op_out.consumer_list = op.outputs[0].consumer_list.copy() |
| 760 | act_op_out.consumer_list.append(prep_op) |
| 761 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 762 | prep_op.inputs[0] = act_op_out |
| 763 | prep_op.outputs[0].quantization = act_op_out.quantization.clone() |
| 764 | |
| 765 | # Mark the op so that it will be removed as passthrough later on |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 766 | op.type = Op.Identity |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 767 | |
| 768 | # Record optimisation in debug database |
| 769 | DebugDatabase.add_optimised(op, act_op) |
| 770 | DebugDatabase.add_optimised(op, op) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 771 | return op |
| 772 | |
Louis Verhaard | e0ef273 | 2020-06-03 08:56:44 +0200 | [diff] [blame] | 773 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 774 | def fixup_elementwise_with_scalars(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 775 | if op.type.is_binary_elementwise_op(): |
Louis Verhaard | e0ef273 | 2020-06-03 08:56:44 +0200 | [diff] [blame] | 776 | ifm_tensor, ifm2_tensor, _, _ = op.get_ifm_ifm2_weights_ofm() |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 777 | if ifm2_tensor.shape != [] and ifm_tensor.shape != []: |
| 778 | diff = len(ifm_tensor.shape) - len(ifm2_tensor.shape) |
| 779 | if diff > 0: |
| 780 | ifm2_tensor.shape = full_shape(len(ifm_tensor.shape), ifm2_tensor.shape, 1) |
| 781 | elif diff < 0: |
| 782 | ifm_tensor.shape = full_shape(len(ifm2_tensor.shape), ifm_tensor.shape, 1) |
Louis Verhaard | e0ef273 | 2020-06-03 08:56:44 +0200 | [diff] [blame] | 783 | elif ifm_tensor.shape == [] and ifm_tensor.quant_values is None: |
| 784 | # IFM is marked as a scalar, but is a result of an operation; change it to a shape of size 1 |
| 785 | ifm_tensor.shape = len(ifm2_tensor.shape) * [1] |
| 786 | ifm_tensor.storage_shape = ifm_tensor.shape |
| 787 | elif ifm2_tensor.shape == [] and ifm2_tensor.quant_values is None: |
| 788 | # IFM2 is marked as a scalar, but is a result of an operation; change it to a shape of size 1 |
| 789 | ifm2_tensor.shape = len(ifm_tensor.shape) * [1] |
| 790 | ifm2_tensor.storage_shape = ifm2_tensor.shape |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 791 | return op |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 792 | |
Louis Verhaard | e0ef273 | 2020-06-03 08:56:44 +0200 | [diff] [blame] | 793 | |
Tim Hall | 4e12776 | 2020-05-15 16:05:49 +0100 | [diff] [blame] | 794 | # Set input/output tensor equivalence to the same id for memory operations |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 795 | def set_tensor_equivalence(op, arch, nng): |
Michael McGeagh | 11b0bdb | 2020-09-08 11:07:35 +0100 | [diff] [blame] | 796 | if op.type in memory_only_ops: |
Tim Hall | 4e12776 | 2020-05-15 16:05:49 +0100 | [diff] [blame] | 797 | eid = op.outputs[0].equivalence_id |
| 798 | for inp in op.inputs: |
| 799 | inp.equivalence_id = eid |
| 800 | return op |
| 801 | |
| 802 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 803 | def set_ifm_ofm_op_shapes(op, arch, nng): |
| 804 | if op.run_on_npu and op.type.needs_shapes(): |
| 805 | if op.ifm_shapes or op.ofm_shapes: |
| 806 | # Shapes already set |
| 807 | return op |
| 808 | op.set_ifm_ofm_shapes() |
| 809 | return op |
| 810 | |
| 811 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 812 | def convert_softmax(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 813 | if op.type == Op.Softmax and op.run_on_npu: |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 814 | softmax = SoftMax(op) |
| 815 | op = softmax.get_graph() |
| 816 | return op |
| 817 | |
| 818 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 819 | def convert_mul_max_to_abs_or_lrelu(op, arch, nng): |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 820 | r"""Whenever there is a subgraph with this topology: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 821 | |
| 822 | Input X For X = -1 or X > 0 |
| 823 | | \ / This subgraph can be replaced with either |
| 824 | | Mul an Abs (if X = -1) or a LeakyReLU (if X > 0) |
| 825 | | / |
| 826 | Max |
| 827 | """ |
| 828 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 829 | if op.type == Op.Maximum: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 830 | # finds the Mul input(s) to the Max |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 831 | muls = [i for i in op.inputs if i.ops[0].type == Op.Mul] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 832 | if len(muls) == 1: |
| 833 | mul = muls[0].ops[0] |
| 834 | elif len(muls) == 2: |
| 835 | # In the case both inputs are Muls, find the one with the same input as the Max |
| 836 | mul = [m for m in muls if len(set(op.inputs + m.ops[0].inputs)) == 1][0].ops[0] |
| 837 | else: |
| 838 | # No Mul inputs |
| 839 | return op |
| 840 | |
| 841 | # make sure the Mul doesn't have any other consumers |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 842 | mul_ofm = mul.outputs[0] |
| 843 | if len(mul_ofm.consumers()) != 1: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 844 | return op |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 845 | # make sure the Mul doesn't have a fused activation function |
| 846 | if mul.activation: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 847 | return op |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 848 | ifm, ofm = op.get_ifm_ofm() |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 849 | if ifm is None or ofm is None: |
| 850 | return op |
| 851 | |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 852 | if ifm.dtype not in (DataType.uint8, DataType.int8) or ifm.dtype != ofm.dtype: |
| 853 | return op |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 854 | if not check_quantized_tens_scaling_equal(ifm, ofm) or not check_quantized_tens_scaling_equal(ifm, mul_ofm): |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 855 | # rewrite to LeakyRelu currently only makes sense if the quantization is identical |
| 856 | return op |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 857 | |
| 858 | # finds the branched input that goes to both the Max and the Mul |
| 859 | shared = set(op.inputs) & set(mul.inputs) |
| 860 | if len(shared) == 1: |
| 861 | shared_in = shared.pop() |
| 862 | # find the constant scalar input to the Mul |
| 863 | const_tens = (set(mul.inputs) - {shared_in}).pop() |
| 864 | # check that it is a scalar |
| 865 | if const_tens.shape != []: |
| 866 | return op |
| 867 | const = const_tens.ops[0] |
| 868 | # check that it is a constant |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 869 | if const.type != Op.Const: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 870 | return op |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 871 | # Remove the Mul from the shared input's consumers |
| 872 | shared_in.consumer_list.remove(mul) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 873 | else: |
| 874 | return op |
| 875 | |
| 876 | val = const.outputs[0].values |
| 877 | if val >= 0: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 878 | new_op = Op.LeakyRelu |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 879 | op.attrs["alpha"] = val |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 880 | # to produce bit exact results, the alpha is not enough; |
| 881 | # save additional scaling info in attr "alpha_scale", to be used as input |
| 882 | # to the LUT construction |
| 883 | alpha_scalar = const_tens.quant_values - const_tens.quantization.zero_point |
| 884 | mul_ifm_scale = np.double(ifm.quantization.scale_f32) |
| 885 | mul_ifm2_scale = np.double(const_tens.quantization.scale_f32) |
| 886 | mul_ofm_scale = np.double(mul_ofm.quantization.scale_f32) |
| 887 | alpha_scale, alpha_shift = scaling.elementwise_mul_scale(mul_ifm_scale, mul_ifm2_scale, mul_ofm_scale) |
| 888 | op.attrs["alpha_scaling"] = (alpha_scalar, alpha_scale, alpha_shift) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 889 | elif val == -1: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 890 | new_op = Op.Abs |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 891 | else: |
| 892 | return op |
| 893 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 894 | op.type = new_op |
| 895 | op.name = op.name.replace("Maximum", new_op.name) |
| 896 | op.outputs[0].name = op.outputs[0].name.replace("Maximum", new_op.name) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 897 | op.inputs = [shared_in] |
Patrik Gustavsson | c509d33 | 2020-12-22 13:53:52 +0100 | [diff] [blame] | 898 | op.set_ifm_ofm_shapes() |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 899 | |
| 900 | # Record optimisation in debug database |
| 901 | DebugDatabase.add_optimised(op, op) |
| 902 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 903 | return op |
| 904 | |
| 905 | |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 906 | def convert_lrelu_to_mul_max(op, arch): |
| 907 | # Converts LeakyRelu to Max(alpha * IFM, identity * IFM) |
| 908 | # (the opposite of convert_mul_max_to_abs_or_lrelu) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 909 | ifm, ofm = op.get_ifm_ofm() |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 910 | if ifm is None or ofm is None: |
| 911 | return op |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 912 | |
| 913 | # Add multiplication with alpha |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 914 | mul_alpha = Operation(Op.Mul, op.name + "_mul_alpha") |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 915 | mul_alpha.add_input_tensor(ifm) |
| 916 | # Create const tensor containing alpha as scalar |
| 917 | alpha = op.attrs["alpha"] |
| 918 | quantization = ifm.quantization.clone() |
| 919 | quantization.min = 0 |
| 920 | quantization.max = alpha * (quantization.quant_max - quantization.quant_min) |
| 921 | quantization.scale_f32 = alpha |
| 922 | quantization.zero_point = 0 |
| 923 | alpha_tens = create_const_tensor(op.name + "_alpha_scalar", [], ifm.dtype, [1], np.int8, quantization=quantization) |
| 924 | mul_alpha.add_input_tensor(alpha_tens) |
| 925 | fm_alpha = ofm.clone(op.name + "_alpha") |
| 926 | mul_alpha.set_output_tensor(fm_alpha) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 927 | mul_alpha.set_ifm_ofm_shapes() |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 928 | DebugDatabase.add_optimised(op, mul_alpha) |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 929 | |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 930 | if check_quantized_tens_scaling_equal(ifm, ofm): |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 931 | # No identity multiplication is needed |
| 932 | fm_id = ifm |
| 933 | else: |
| 934 | # Add multiplication with identity |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 935 | mul_identity = Operation(Op.Mul, op.name + "_mul_identity") |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 936 | mul_identity.add_input_tensor(ifm) |
| 937 | # Create const tensor containing identity as scalar |
| 938 | quantization = ifm.quantization.clone() |
| 939 | quantization.min = 0 |
| 940 | quantization.max = quantization.quant_max - quantization.quant_min |
| 941 | quantization.scale_f32 = 1 |
| 942 | quantization.zero_point = 0 |
| 943 | identity_tens = create_const_tensor( |
| 944 | op.name + "_id_scalar", [], ifm.dtype, [1], np.uint8, quantization=quantization |
| 945 | ) |
| 946 | mul_identity.add_input_tensor(identity_tens) |
| 947 | fm_id = ofm.clone(op.name + "_id") |
| 948 | mul_identity.set_output_tensor(fm_id) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 949 | mul_identity.set_ifm_ofm_shapes() |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 950 | DebugDatabase.add_optimised(op, mul_identity) |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 951 | |
| 952 | # Convert LeakyRelu to Max, add the results of the multiplication(s) as inputs |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 953 | op.type = Op.Maximum |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 954 | op.name = op.name.replace("LeakyRelu", "Maximum") |
| 955 | op.inputs = [] |
| 956 | ifm.consumer_list.remove(op) |
| 957 | op.add_input_tensor(fm_alpha) |
| 958 | op.add_input_tensor(fm_id) |
Patrik Gustavsson | c509d33 | 2020-12-22 13:53:52 +0100 | [diff] [blame] | 959 | op.set_ifm_ofm_shapes() |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 960 | |
| 961 | DebugDatabase.add_optimised(op, op) |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 962 | return op |
| 963 | |
| 964 | |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 965 | def convert_to_lut(op, lut_values, lut_name): |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 966 | # Rewrite the operation by Add with scalar 0 + LUT activation |
| 967 | ifm = op.inputs[0] |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 968 | if ifm is None: |
| 969 | return op |
Louis Verhaard | 58520b9 | 2020-08-24 16:45:38 +0200 | [diff] [blame] | 970 | assert ifm.dtype.size_in_bytes() == 1 |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 971 | op.type = Op.Add |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 972 | op.name = op.name + "_lut_" + lut_name |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 973 | # Mark as no-op to enable potential fusing optimizations |
| 974 | op.attrs["is_nop"] = True |
| 975 | # Create an input tensor containing scalar zero |
| 976 | quantization = QuantizationParameters(0.0, 255.0) |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 977 | quantization.scale_f32 = ifm.quantization.scale_f32 |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 978 | quantization.zero_point = 0 |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 979 | tens = create_const_tensor(op.inputs[0].name + "_scalar0", [], ifm.dtype, [0], np.uint8, quantization=quantization) |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 980 | op.add_input_tensor(tens) |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 981 | op.ifm_shapes.append(Shape4D(tens.shape)) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 982 | |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 983 | # The LUT must be applied without any preceding rescaling (the LUT itself performs the rescale), |
| 984 | # so even if the OFM has a different scale than the IFM, the generated OFM scale instructions |
| 985 | # should be the same as the IFM |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 986 | op.forced_output_quantization = ifm.quantization |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 987 | lut_tensor = lut.create_lut_tensor(op.name + "_values", lut_values, DataType.int8) |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 988 | op.set_activation_lut(lut_tensor) |
Patrik Gustavsson | c509d33 | 2020-12-22 13:53:52 +0100 | [diff] [blame] | 989 | op.set_ifm_ofm_shapes() |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 990 | return op |
| 991 | |
| 992 | |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 993 | def convert_to_lut8(op, fn, fn_name): |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 994 | # Converts op to a no-op + int8/uint8 LUT which is generated with the given function. |
| 995 | # fn is a function(real) -> real |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 996 | ifm, ofm = op.get_ifm_ofm() |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 997 | if ifm.dtype not in (DataType.uint8, DataType.int8) or ifm.dtype != ofm.dtype: |
| 998 | return op |
| 999 | # Generate the LUT |
| 1000 | ifm_scale = np.double(ifm.quantization.scale_f32) |
| 1001 | ofm_scale = np.double(ofm.quantization.scale_f32) |
| 1002 | zp_in = ifm.quantization.zero_point |
| 1003 | zp_out = ofm.quantization.zero_point |
| 1004 | values = [] |
| 1005 | ix = range(256) if ifm.dtype == DataType.uint8 else range(-128, 128) |
| 1006 | quantized_min = min(ix) |
| 1007 | quantized_max = max(ix) |
| 1008 | for x in ix: |
| 1009 | x_real = ifm_scale * (x - zp_in) |
| 1010 | y_real = fn(x_real) |
| 1011 | lut_result = round_away_zero(zp_out + y_real / ofm_scale) |
| 1012 | lut_result = min(quantized_max, max(quantized_min, lut_result)) |
| 1013 | values.append(lut_result) |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 1014 | return convert_to_lut(op, values, fn_name) |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 1015 | |
| 1016 | |
| 1017 | def convert_lrelu_to_lut(op, arch): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1018 | ifm, ofm = op.get_ifm_ofm() |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1019 | # Generate the LUT |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 1020 | alpha = op.attrs["alpha"] |
| 1021 | ifm_scale = np.double(ifm.quantization.scale_f32) |
| 1022 | ofm_scale = np.double(ofm.quantization.scale_f32) |
| 1023 | zp_in = ifm.quantization.zero_point |
| 1024 | zp_out = ofm.quantization.zero_point |
| 1025 | identity_scale, identity_shift = scaling.elementwise_mul_scale(ifm_scale, 1, ofm_scale) |
| 1026 | alpha_scalar = 1 |
| 1027 | alpha_scale, alpha_shift = scaling.elementwise_mul_scale(ifm_scale, alpha, ofm_scale) |
| 1028 | if "alpha_scaling" in op.attrs: |
| 1029 | # The LeakyRelu was the result from convert_mul_max_to_abs_or_lrelu |
| 1030 | alpha_scalar, alpha_scale, alpha_shift = op.attrs["alpha_scaling"] |
| 1031 | values = [] |
Louis Verhaard | 58520b9 | 2020-08-24 16:45:38 +0200 | [diff] [blame] | 1032 | ix = range(256) if ifm.dtype == DataType.uint8 else range(-128, 128) |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 1033 | quantized_min = min(ix) |
| 1034 | quantized_max = max(ix) |
| 1035 | for x in ix: |
| 1036 | if x < zp_in: |
| 1037 | lut_result = zp_out + fp_math.multiply_by_quantized_multiplier( |
| 1038 | alpha_scalar * (x - zp_in), alpha_scale, alpha_shift |
| 1039 | ) |
| 1040 | else: |
| 1041 | lut_result = zp_out + fp_math.multiply_by_quantized_multiplier(x - zp_in, identity_scale, identity_shift) |
| 1042 | lut_result = min(quantized_max, max(quantized_min, lut_result)) |
| 1043 | values.append(lut_result) |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 1044 | return convert_to_lut(op, values, "lrelu") |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1045 | |
| 1046 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 1047 | def convert_lrelu(op, arch, nng): |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1048 | # Converts LeakyRelu to a LUT based solution if possible, otherwise a mul + max |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1049 | if op.type != Op.LeakyRelu: |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1050 | return op |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1051 | ifm, ofm = op.get_ifm_ofm() |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 1052 | if ifm is None or ofm is None: |
| 1053 | return op |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 1054 | if ifm.dtype in (DataType.uint8, DataType.int8) and ifm.dtype == ofm.dtype: |
| 1055 | # use LUT for int8/uint8 |
| 1056 | return convert_lrelu_to_lut(op, arch) |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 1057 | if check_quantized_tens_scaling_equal(ifm, ofm) and ifm.dtype == ofm.dtype == DataType.int16: |
Louis Verhaard | d7911c4 | 2020-08-25 13:36:41 +0200 | [diff] [blame] | 1058 | # use LeakyRelu unmodified for int16 with equal input/output scaling |
| 1059 | return op |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1060 | return convert_lrelu_to_mul_max(op, arch) |
| 1061 | |
| 1062 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 1063 | def convert_tanh_sigmoid_to_lut(op, arch, nng): |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 1064 | # Converts int8/uint8 Sigmoid and Tanh to a LUT based solution |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1065 | if op.type == Op.Sigmoid: |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 1066 | return convert_to_lut8(op, clamp_sigmoid, "sigmoid") |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1067 | elif op.type == Op.Tanh: |
Louis Verhaard | 2e186c7 | 2020-10-09 10:47:04 +0200 | [diff] [blame] | 1068 | return convert_to_lut8(op, math.tanh, "tanh") |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 1069 | return op |
| 1070 | |
| 1071 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 1072 | def remove_unwanted_reshapes(op, arch, nng): |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1073 | # Try to remove reshapes enclosing ElementWise operator with only one non-constant input |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1074 | if not op.run_on_npu or not op.type.is_elementwise_op(): |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1075 | return op |
| 1076 | |
| 1077 | # Check if the ElementWise operator only have one non-constant input |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1078 | non_const_tens = [x for x in op.inputs if x.ops[0].type != Op.Const] |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1079 | if len(non_const_tens) != 1: |
| 1080 | return op |
| 1081 | ifm = non_const_tens[0] |
| 1082 | |
| 1083 | # Check if operation is enclosed by Reshapes that can be removed |
| 1084 | ofm = op.outputs[0] |
| 1085 | prev_op = ifm.ops[0] |
| 1086 | if ( |
| 1087 | len(ifm.consumer_list) == 1 |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1088 | and prev_op.type == Op.Reshape |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1089 | and len(ofm.consumer_list) == 1 |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1090 | and ofm.consumer_list[0].type == Op.Reshape |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1091 | ): |
| 1092 | # Operation is enclosed by reshapes, check if they can be removed |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1093 | prev_op_ifm, prev_op_ofm = prev_op.get_ifm_ofm() |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1094 | cons_op = ofm.consumer_list[0] |
| 1095 | cons_op_ifm = ofm |
| 1096 | cons_op_ofm = cons_op.outputs[0] |
| 1097 | if len(prev_op_ifm.shape) == len(cons_op_ofm.shape): |
| 1098 | # Check if quantization is the same in the input and output for the reshape ops |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 1099 | if check_quantized_tens_scaling_equal(prev_op_ifm, prev_op_ofm) and check_quantized_tens_scaling_equal( |
| 1100 | cons_op_ifm, cons_op_ofm |
| 1101 | ): |
Patrik Gustavsson | 7ad862a | 2020-09-29 14:09:43 +0200 | [diff] [blame] | 1102 | op.set_input_tensor(prev_op_ifm, 0) |
| 1103 | op.set_output_tensor(cons_op_ofm) |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1104 | return op |
| 1105 | |
| 1106 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 1107 | def fuse_activation_function_with_prev(op, arch, nng): |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1108 | # if op is a no-op: attempts to move the activation function to the preceding op |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1109 | if not op.attrs.get("is_nop", False) or op.activation is None: |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1110 | return op |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1111 | ifm, ofm = op.get_ifm_ofm() |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 1112 | if ifm is None or ofm is None: |
| 1113 | return op |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1114 | # finds the input(s) to the operation |
| 1115 | prev_op = ifm.ops[0] |
| 1116 | # Note: the below checks on prev_op require that a first optimize pass on the full graph has been performed |
| 1117 | fuse = ( |
| 1118 | prev_op.run_on_npu |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1119 | and prev_op.type.npu_block_type != NpuBlockType.Default |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1120 | and len(ifm.ops) == 1 |
| 1121 | and len(prev_op.outputs[0].consumers()) == 1 |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1122 | and prev_op.activation is None |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1123 | ) |
| 1124 | if op.activation_lut is not None and arch.shram_reserved_unused_banks == 0: |
| 1125 | # TODO: if SHRAM LUT space is shared with SHRAM ACC (32, 64 MAC), |
| 1126 | # LUT currently only works correctly for elementwise ops |
| 1127 | fuse = False |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1128 | if not fuse: |
| 1129 | return op |
| 1130 | # Move the fused activation function + corresponding info to prev_op |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1131 | prev_op.activation = op.activation |
| 1132 | prev_op.forced_output_quantization = op.forced_output_quantization |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1133 | if op.activation_lut is not None: |
| 1134 | prev_op.set_activation_lut(op.activation_lut) |
| 1135 | # Bypass op |
Louis Verhaard | 98a3499 | 2020-09-01 10:39:04 +0200 | [diff] [blame] | 1136 | prev_op.set_output_tensor(ofm) |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 1137 | DebugDatabase.add_optimised(op, prev_op) |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1138 | return op |
| 1139 | |
| 1140 | |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame] | 1141 | def optimise_pad(op, arch, nng): |
| 1142 | """ |
| 1143 | Converts tens1 -> PAD -> tens2 -> CONV to tens1 -> CONV |
| 1144 | if both operations can be run on the NPU. |
| 1145 | """ |
| 1146 | if ( |
| 1147 | (op.type.is_conv2d_op() or op.type.is_depthwise_conv2d_op()) |
| 1148 | and op.run_on_npu |
| 1149 | and op.attrs["padding"] == Padding.VALID |
| 1150 | ): |
| 1151 | pad_op = op.ifm.ops[0] |
| 1152 | if pad_op.type != Op.Pad or not pad_op.run_on_npu: |
| 1153 | return op |
| 1154 | # Bypass the PAD operator |
| 1155 | op.set_input_tensor(pad_op.ifm, 0) |
| 1156 | # Adjust the padding attributes of the convolution operator |
| 1157 | op.attrs["padding"] = Padding.EXPLICIT |
| 1158 | padding = pad_op.inputs[1].values # 4x2 tensor, first dimension is N, H, W, C |
| 1159 | top, left, bottom, right = (padding[1][0], padding[2][0], padding[1][1], padding[2][1]) |
| 1160 | op.attrs["explicit_padding"] = (top, left, bottom, right) |
| 1161 | op.set_ifm_ofm_shapes() |
| 1162 | return op |
| 1163 | |
| 1164 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 1165 | def add_attrs_to_resizebilinear(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1166 | if op.type == Op.ResizeBilinear and op.run_on_npu: |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 1167 | input_tensor = op.inputs[0] |
| 1168 | upscaled_shape = [input_tensor.shape[1] * 2, input_tensor.shape[2] * 2] |
| 1169 | out_shape = op.outputs[0].shape[1:3] |
| 1170 | if not op.attrs["align_corners"] and out_shape == upscaled_shape: |
| 1171 | # this means the output is supposed to be a x2 upscale, |
| 1172 | # so we need to do SAME padding |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 1173 | op.attrs["padding"] = Padding.SAME |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 1174 | elif op.attrs["align_corners"] and out_shape == [upscaled_shape[0] - 1, upscaled_shape[1] - 1]: |
| 1175 | # here we can just run the avg pool without padding and |
| 1176 | # produce a (M * 2 - 1, N * 2 - 1) sized output |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 1177 | op.attrs["padding"] = Padding.VALID |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 1178 | else: |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 1179 | return op |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 1180 | input_tensor.resampling_mode = resampling_mode.NEAREST |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 1181 | op.attrs.update({"strides": (1, 1, 1, 1), "ksize": (1, 2, 2, 1)}) |
Dwight Lidman | 42fed94 | 2020-05-29 09:37:03 +0200 | [diff] [blame] | 1182 | return op |
| 1183 | |
| 1184 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 1185 | def fixup_bias_tensors(op, arch, nng): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 1186 | if op.type.needs_bias() and op.bias is None: |
Jacob Bohlin | a41cd4d | 2020-08-26 18:21:28 +0200 | [diff] [blame] | 1187 | # Op has no bias, add bias tensor filled with zeros |
| 1188 | nr_biases = op.inputs[1].shape[-1] |
| 1189 | bias_values = [0] * nr_biases |
| 1190 | bias_tensor = create_const_tensor(op.name + "_bias", [nr_biases], DataType.int32, bias_values) |
| 1191 | bias_tensor.quant_values = bias_tensor.values |
| 1192 | op.set_input_tensor(bias_tensor, -1) |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 1193 | |
| 1194 | return op |
| 1195 | |
| 1196 | |
Patrik Gustavsson | 3010d9b | 2020-10-01 08:22:10 +0200 | [diff] [blame] | 1197 | def supported_operator_check(op, arch, nng): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1198 | op.run_on_npu = arch.supported_operators.is_operator_supported(op) |
| 1199 | return op |
| 1200 | |
| 1201 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 1202 | def _record_optimised(op, arch): |
| 1203 | if op.type != Op.Const: |
| 1204 | DebugDatabase.add_optimised(op, op) |
| 1205 | |
| 1206 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1207 | def optimise_graph_a(nng, arch, verbose_graph=False): |
| 1208 | if verbose_graph: |
| 1209 | nng.print_graph() |
| 1210 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 1211 | pre_process_list = [ |
| 1212 | supported_operator_check, |
| 1213 | set_ifm_ofm_op_shapes, |
| 1214 | # TODO: memory-only Op removal |
| 1215 | ] |
| 1216 | |
| 1217 | for idx, sg in enumerate(nng.subgraphs): |
| 1218 | # rewrite graph pass |
| 1219 | nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order( |
| 1220 | nng, sg, arch, [], pre_process_list, rewrite_unsupported=False, |
| 1221 | ) |
| 1222 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1223 | op_rewrite_list = [ |
Tim Hall | 4e12776 | 2020-05-15 16:05:49 +0100 | [diff] [blame] | 1224 | set_tensor_equivalence, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1225 | convert_depthwise_to_conv, |
Michael McGeagh | 8d939c0 | 2020-07-29 13:11:43 +0100 | [diff] [blame] | 1226 | convert_conv_to_fc, |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 1227 | convert_softmax, |
Diqing Zhong | 016b827 | 2020-12-16 16:46:06 +0100 | [diff] [blame] | 1228 | optimise_strided_conv, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1229 | fixup_fully_connected_input, |
Diqing Zhong | 94457b1 | 2020-12-09 15:22:40 +0100 | [diff] [blame] | 1230 | convert_batched_fc_shape, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1231 | fixup_pack_input, |
Fredrik Svedberg | 0f98b36 | 2020-09-29 10:00:39 +0200 | [diff] [blame] | 1232 | unfuse_activation_function, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1233 | fixup_conv2d_backprop, |
Michael McGeagh | 8dbf8cf | 2020-09-08 11:09:48 +0100 | [diff] [blame] | 1234 | fixup_relus_with_differing_ifm_ofm_scaling, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1235 | fixup_act_reorder, |
Charles Xu | 7879222 | 2020-05-13 10:15:26 +0200 | [diff] [blame] | 1236 | fixup_elementwise_with_scalars, |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 1237 | reorder_depthwise_weights, |
Charles Xu | 9a03fdf | 2020-07-02 15:12:40 +0200 | [diff] [blame] | 1238 | fixup_resizebilinear, |
Jacob Bohlin | a41cd4d | 2020-08-26 18:21:28 +0200 | [diff] [blame] | 1239 | fixup_bias_tensors, |
Dwight Lidman | c3862c2 | 2020-09-14 15:22:33 +0200 | [diff] [blame] | 1240 | convert_nop_split_to_identity, |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1241 | convert_mul_max_to_abs_or_lrelu, |
Patrik Gustavsson | fa4cb29 | 2020-09-10 08:19:36 +0200 | [diff] [blame] | 1242 | remove_unwanted_reshapes, |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1243 | convert_lrelu, |
Louis Verhaard | f03bad3 | 2020-09-25 08:30:44 +0200 | [diff] [blame] | 1244 | convert_tanh_sigmoid_to_lut, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1245 | ] |
| 1246 | |
| 1247 | for idx, sg in enumerate(nng.subgraphs): |
| 1248 | # rewrite graph pass |
| 1249 | nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order( |
Dwight Lidman | 73320a4 | 2020-11-05 10:34:41 +0100 | [diff] [blame] | 1250 | nng, sg, arch, [], op_rewrite_list, rewrite_unsupported=False, |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1251 | ) |
| 1252 | |
| 1253 | for idx, sg in enumerate(nng.subgraphs): |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1254 | # remove passthrough tensors and attempt further optimizations |
| 1255 | nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order( |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame] | 1256 | nng, |
| 1257 | sg, |
| 1258 | arch, |
| 1259 | [remove_passthrough_tensor], |
| 1260 | [fuse_activation_function_with_prev, optimise_pad, add_padding_fields], |
Louis Verhaard | b9fc33c | 2020-08-13 11:47:36 +0200 | [diff] [blame] | 1261 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1262 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 1263 | # Post-optimisation operator debug tracing |
| 1264 | for sg in nng.subgraphs: |
| 1265 | rewrite_graph.visit_graph_post_order(sg.output_tensors, arch, [], [_record_optimised]) |
| 1266 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1267 | if verbose_graph: |
| 1268 | nng.print_graph() |
| 1269 | return nng |
| 1270 | |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 1271 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1272 | def optimise_graph_b(nng, arch, verbose_graph=False): |
| 1273 | if verbose_graph: |
| 1274 | nng.print_graph() |
| 1275 | |
| 1276 | for idx, sg in enumerate(nng.subgraphs): |
| 1277 | # combined rewrite graph pass |
Dwight Lidman | c6ac194 | 2020-10-02 14:55:45 +0200 | [diff] [blame] | 1278 | nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order( |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 1279 | nng, sg, arch, [fixup_unpack_output, fixup_stridedslice_output, rewrite_concat, rewrite_split], [], |
Dwight Lidman | c6ac194 | 2020-10-02 14:55:45 +0200 | [diff] [blame] | 1280 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1281 | |
| 1282 | if verbose_graph: |
| 1283 | nng.print_graph() |
| 1284 | return nng |