blob: 32f97d2f2592867c469914c97994a2a6e6aacdb5 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# 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 Hall79d07d22020-04-27 18:20:16 +010016# 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 Hall79d07d22020-04-27 18:20:16 +010019import math
Diego Russoea6111a2020-04-14 18:41:58 +010020
21import numpy as np
22
Louis Verhaardd7911c42020-08-25 13:36:41 +020023from . import fp_math
Louis Verhaardb9fc33c2020-08-13 11:47:36 +020024from . import lut
Diego Russoea6111a2020-04-14 18:41:58 +010025from . import rewrite_graph
Louis Verhaardd7911c42020-08-25 13:36:41 +020026from . import scaling
Diego Russoea6111a2020-04-14 18:41:58 +010027from .data_type import DataType
Louis Verhaard7db78962020-05-25 15:05:26 +020028from .errors import UnsupportedFeatureError
Dwight Lidman42fed942020-05-29 09:37:03 +020029from .ethos_u55_regs.ethos_u55_regs import resampling_mode
Louis Verhaard8912c532020-09-30 12:11:49 +020030from .numeric_util import clamp_sigmoid
Louis Verhaarde0ef2732020-06-03 08:56:44 +020031from .numeric_util import full_shape
Louis Verhaardf03bad32020-09-25 08:30:44 +020032from .numeric_util import round_away_zero
Michael McGeagh8dbf8cf2020-09-08 11:09:48 +010033from .operation import create_avgpool_nop
Diego Russoe8a10452020-04-21 17:39:10 +010034from .operation import NpuBlockType
Louis Verhaardaee5d752020-09-30 09:01:52 +020035from .operation import Op
Diego Russoe8a10452020-04-21 17:39:10 +010036from .operation import Operation
Fredrik Svedberga0c36242020-06-03 15:43:31 +020037from .softmax import SoftMax
Tim Hall93582962020-09-09 21:58:15 +010038from .tensor import check_quantized_tens_scaling_equal
Michael McGeaghc5b549b2020-08-07 11:54:28 +010039from .tensor import create_const_tensor
40from .tensor import create_reshape_tensor
Charles Xu9a03fdf2020-07-02 15:12:40 +020041from .tensor import QuantizationParameters
Diego Russoe8a10452020-04-21 17:39:10 +010042from .tensor import Tensor
Tim Hall79d07d22020-04-27 18:20:16 +010043
Louis Verhaardaee5d752020-09-30 09:01:52 +020044passthrough_nodes = set((Op.Identity,))
Tim Hall79d07d22020-04-27 18:20:16 +010045
Louis Verhaardaee5d752020-09-30 09:01:52 +020046memory_only_ops = set((Op.Reshape,))
Michael McGeagh11b0bdb2020-09-08 11:07:35 +010047
Tim Hall79d07d22020-04-27 18:20:16 +010048
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +020049def remove_passthrough_tensor(tens, arch, nng):
Tim Hall79d07d22020-04-27 18:20:16 +010050 if len(tens.ops) == 1 and tens.ops[0].type in passthrough_nodes:
51 assert len(tens.ops[0].inputs) == 1
52 tens = tens.ops[0].inputs[0]
53 return tens
54
55
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +020056def rewrite_concat(tens, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +020057 if len(tens.ops) == 1 and tens.ops[0].type.is_concat_op():
Tim Hall79d07d22020-04-27 18:20:16 +010058 concat_op = tens.ops[0]
59 if tens != concat_op.outputs[0]:
60 return tens # don't attempt to rewrite the min/max outputs of QuantizedConcat
61
62 # Not supported so leave it and run on CPU
63 if not concat_op.run_on_npu:
64 return tens
65
66 inputs, axis = concat_op.get_concat_inputs_axis()
67
68 tens.ops = []
69 offset = 0
70 for idx, inp in enumerate(inputs):
Louis Verhaardaee5d752020-09-30 09:01:52 +020071 new_op = Operation(Op.ConcatSliceWrite, concat_op.name + str(idx))
Tim Hall79d07d22020-04-27 18:20:16 +010072 new_op.inputs = [inp]
73 new_op.outputs = [tens]
74 new_op.attrs["concat_axis"] = axis
75 new_op.attrs["concat_start"] = offset
76 offset += inp.shape[axis]
77 new_op.attrs["concat_end"] = offset
78 new_op.run_on_npu = True
79 tens.ops.append(new_op)
80 assert tens.shape[axis] == offset
81
Patrik Gustavsson29d568e2020-08-18 10:11:21 +020082 # If axis corresponds to C-dimension, NHCWB16 can only be used in the output if all the concat_start's are a
83 # multiple of 16. This as, it is only then the address offset for the ofm, for all operations, will be 16 byte
84 # aligned. For other values of axis the address offsets will be 16 byte aligned, as they are all based on c = 0
Patrik Gustavsson458a2082020-08-13 13:41:05 +020085 # and those addresses are always 16 byte aligned due to the NHCWB16 format.
Patrik Gustavsson6c97e9a2020-09-23 11:02:18 +020086 if axis == -1 or axis == (len(tens.shape) - 1):
Patrik Gustavsson458a2082020-08-13 13:41:05 +020087 for op in tens.ops:
88 if op.attrs["concat_start"] % 16 != 0:
89 tens.avoid_NHCWB16 = True
90 break
91
Tim Hall79d07d22020-04-27 18:20:16 +010092 return tens
93
94
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +020095def rewrite_split(tens, arch, nng):
Tim Hall79d07d22020-04-27 18:20:16 +010096
Louis Verhaardaee5d752020-09-30 09:01:52 +020097 if len(tens.ops) == 1 and tens.ops[0].type.is_split_op():
Tim Hall79d07d22020-04-27 18:20:16 +010098 split_op = tens.ops[0]
99
100 # Not supported so leave it and run on CPU
101 if not split_op.run_on_npu:
102 return tens
103
104 inp, outputs, axis, offset_start, offset_end = split_op.get_split_inputs_axis()
105
106 tens.ops = []
Louis Verhaardaee5d752020-09-30 09:01:52 +0200107 new_op = Operation(Op.SplitSliceRead, split_op.name)
Tim Hall79d07d22020-04-27 18:20:16 +0100108 new_op.inputs = [inp]
Tim Hall79d07d22020-04-27 18:20:16 +0100109
110 # For Split the offset cannot be extracted from the tensor so it has to
111 # be calculated from the index of the output tensor
Diego Russoea6111a2020-04-14 18:41:58 +0100112 if axis is not None:
Tim Hall79d07d22020-04-27 18:20:16 +0100113 # Get the start and end of the split
114 offset_start = [0] * len(tens.shape)
115 offset_end = [0] * len(tens.shape)
116 for out in outputs:
117 if out == tens:
118 break
119 offset_start[axis] += out.shape[axis]
120
Patrik Gustavssoneebb1c22020-08-18 15:03:04 +0200121 # If start offset is not a multiple of 16 in the C-dimension, NHCWB16 need to be avoided in the input
122 if (offset_start[-1] % 16) != 0:
123 inp.avoid_NHCWB16 = True
124
Tim Hall79d07d22020-04-27 18:20:16 +0100125 offset_end[axis] = offset_start[axis] + tens.shape[axis]
126
127 new_op.attrs["split_start"] = offset_start
128 new_op.attrs["split_end"] = offset_end
129 new_op.run_on_npu = True
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100130 new_op.set_output_tensor(tens)
Tim Hall79d07d22020-04-27 18:20:16 +0100131
132 return tens
133
134
135def needed_total_padding(input_size, stride, filter_size):
136 out_size = (input_size + stride - 1) // stride
137 needed_input = (out_size - 1) * stride + filter_size
138 total_padding = max(0, needed_input - input_size)
139 return total_padding
140
141
142def calc_padding_and_skirt(padding_type, kernel_size, stride, input_dims):
143 ypad = needed_total_padding(int(input_dims[1]), int(stride[1]), int(kernel_size[0]))
144 xpad = needed_total_padding(int(input_dims[2]), int(stride[2]), int(kernel_size[1]))
145 if padding_type == b"SAME":
146 left_pad = (xpad + 0) // 2
147 right_pad = (xpad + 1) // 2
148 top_pad = (ypad + 0) // 2
149 bottom_pad = (ypad + 1) // 2
150 elif padding_type == b"VALID":
151 left_pad = 0
152 right_pad = 0
153 top_pad = 0
154 bottom_pad = 0
155 else:
Louis Verhaard7db78962020-05-25 15:05:26 +0200156 raise UnsupportedFeatureError("Unknown padding {}".format(str(padding_type)))
Tim Hall79d07d22020-04-27 18:20:16 +0100157 padding = (top_pad, left_pad, bottom_pad, right_pad)
158 skirt = (top_pad, left_pad, ypad - top_pad, xpad - left_pad)
159 return padding, skirt
160
Tim Hallc30f4952020-06-15 20:47:35 +0100161
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200162def calc_upscaled_padding_and_skirt(padding_type, kernel_size, stride, input_dims, upscaling_factor):
163 kernel_height, kernel_width = kernel_size[0], kernel_size[1]
Jacob Bohlincf7da102020-05-20 09:03:40 +0200164 if padding_type == b"SAME":
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200165 ypad = needed_total_padding(int(input_dims[1]) * upscaling_factor, int(stride[1]), int(kernel_height))
166 xpad = needed_total_padding(int(input_dims[2]) * upscaling_factor, int(stride[2]), int(kernel_width))
167
Jacob Bohlind47cc272020-08-24 11:42:14 +0200168 right_pad = max(((xpad + 1) // upscaling_factor) - 1, 0)
169 bottom_pad = max(((ypad + 1) // upscaling_factor) - 1, 0)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200170 left_pad = max(kernel_width - 1 - right_pad, 0)
171 top_pad = max(kernel_height - 1 - bottom_pad, 0)
172
Jacob Bohlincf7da102020-05-20 09:03:40 +0200173 elif padding_type == b"VALID":
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200174 right_pad = max(kernel_width - 2, 0)
175 bottom_pad = max(kernel_height - 2, 0)
176 left_pad = kernel_width - 1
177 top_pad = kernel_height - 1
Jacob Bohlincf7da102020-05-20 09:03:40 +0200178 else:
179 assert 0, "Unknown padding"
180
181 padding = (top_pad, left_pad, bottom_pad, right_pad)
Jacob Bohlin9b64ba02020-07-07 17:15:22 +0200182 skirt = padding
Jacob Bohlincf7da102020-05-20 09:03:40 +0200183 return padding, skirt
184
Tim Hall79d07d22020-04-27 18:20:16 +0100185
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200186def fixup_conv2d_backprop(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200187 if op.type == Op.Conv2DBackpropInput:
Tim Hall79d07d22020-04-27 18:20:16 +0100188 # flip the inputs
189 op.inputs[0], op.inputs[2] = op.inputs[2], op.inputs[0]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200190 op.type = Op.Conv2DBackpropInputSwitchedBias
Jacob Bohlincf7da102020-05-20 09:03:40 +0200191
192 # Update strides
Tim Hallc30f4952020-06-15 20:47:35 +0100193 op.attrs.update({"stride_w": 1, "stride_h": 1, "strides": (1, 1, 1, 1)})
Tim Hall79d07d22020-04-27 18:20:16 +0100194
195 return op
196
197
Charles Xu9a03fdf2020-07-02 15:12:40 +0200198# Convert the op to an elementwise add
199def convert_resizebilinear_1x1_to_add(op):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200200 op.type = Op.Add
Charles Xu9a03fdf2020-07-02 15:12:40 +0200201 op.name = op.name + "_add"
Charles Xu9a03fdf2020-07-02 15:12:40 +0200202 op.attrs["resizebilinear"] = True
203 # Create an input tensor filled with zeros
204 shape = op.outputs[0].shape
205 tens = Tensor(shape, op.inputs[0].dtype, op.inputs[1].name + "_add")
206 tens.values = np.zeros(shape)
207 tens.quant_values = np.zeros(shape, np.uint8)
208 tens.quantization = QuantizationParameters(0.0, 255.0)
209 tens.quantization.scale_f32 = 1.0
210 tens.quantization.zero_point = 0
211 tens.consumer_list = [op]
212 tens_op = op.inputs[1].ops[0]
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100213 tens_op.set_output_tensor(tens)
Charles Xu9a03fdf2020-07-02 15:12:40 +0200214 # Set the add inputs
215 op.inputs[1] = op.inputs[0]
216 op.inputs[0] = tens
217
218 return op
219
220
Charles Xu87c13502020-08-06 12:17:26 +0200221# Convert ResizeBilinear to a number of 2x2 pool ops
222def convert_resizebilinear_to_2x2_pool(op):
223 count = 0
224 pre_op = op
225 outputs = op.outputs
226
227 op.attrs.update({"strides": (1, 1, 1, 1), "ksize": (1, 2, 2, 1)})
228 if op.attrs["align_corners"]:
229 shape_modifier = 1
230 op.attrs["padding"] = b"VALID"
231 else:
232 shape_modifier = 0
233 op.attrs["padding"] = b"SAME"
234 op.inputs[0].resampling_mode = resampling_mode.NEAREST
235
236 upscaled_shape = np.array(op.inputs[0].shape[1:3])
237 out_shape = np.array(op.outputs[0].shape[1:3])
238 if (upscaled_shape == upscaled_shape * 2 - shape_modifier).all():
239 return op
240
241 while (upscaled_shape < out_shape).all():
242 if count == 0:
243 scaled_op = pre_op
244 else:
245 scaled_op = op.clone("_{}".format(count))
246 scaled_op.inputs[0] = pre_op.outputs[0]
247
248 upscaled_shape = upscaled_shape * 2 - shape_modifier
249
250 if (upscaled_shape == out_shape).all():
251 scaled_op.outputs = outputs
252 scaled_op.outputs[0].ops = [scaled_op]
253 else:
254 shape = outputs[0].shape.copy()
255 shape[1:3] = upscaled_shape[0:2]
256 out_tens = Tensor(shape, DataType.int16, "{}_{}".format(op.outputs[0].name, count))
257 out_tens.quantization = op.outputs[0].quantization.clone()
258 out_tens.quantization.quant_min = np.iinfo(np.int16).min
259 out_tens.quantization.quant_max = np.iinfo(np.int16).max
260 scaled_op.set_output_tensor(out_tens)
261 pre_op = scaled_op
262 count += 1
263
264 # Setup the scale value
265 if scaled_op.inputs[0].dtype.bits == 8 and scaled_op.outputs[0].dtype.bits == 16:
266 scaled_op.attrs["rescale"] = 128
267 elif scaled_op.inputs[0].dtype.bits == 16 and scaled_op.outputs[0].dtype.bits == 8:
268 scaled_op.attrs["rescale"] = 1 / 128
269 elif "rescale" in scaled_op.attrs:
270 del scaled_op.attrs["rescale"]
271
272 return op
273
274
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200275def fixup_resizebilinear(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200276 if op.type == Op.ResizeBilinear and op.run_on_npu:
Charles Xu87c13502020-08-06 12:17:26 +0200277 if op.inputs[0].shape == op.outputs[0].shape:
Charles Xu36ffaf32020-08-05 15:40:44 +0200278 # Bypass nop resizebilinear
279 op.inputs = op.inputs[:1]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200280 op.type = Op.Identity
Charles Xu87c13502020-08-06 12:17:26 +0200281 elif op.inputs[0].shape[1] == 1 and op.inputs[0].shape[2] == 1:
282 convert_resizebilinear_1x1_to_add(op)
283 else:
284 convert_resizebilinear_to_2x2_pool(op)
Charles Xu9a03fdf2020-07-02 15:12:40 +0200285
286 return op
287
288
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200289def convert_nop_split_to_identity(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200290 if op.type == Op.Split and op.attrs.get("num_splits") == 1:
Dwight Lidmanc3862c22020-09-14 15:22:33 +0200291 # the list comprehension should return a list with a single tensor
292 # if it shouldn't, remove_passthrough_tensor will fail appropriately
293 op.inputs = [i for i in op.inputs if i.shape == op.outputs[0].shape]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200294 op.type = Op.Identity
Dwight Lidmanc3862c22020-09-14 15:22:33 +0200295 return op
296
297
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200298def fixup_fully_connected_input(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200299 if op.type == Op.FullyConnected:
Tim Hall79d07d22020-04-27 18:20:16 +0100300 inp = op.inputs[0]
301 weights = op.inputs[1]
302
303 n_in_elems = weights.shape[-2]
304 elms = inp.elements()
305 batch_size = elms // n_in_elems
306 assert batch_size * n_in_elems == elms
307
308 desired_shape = [batch_size, n_in_elems]
309 if inp.shape != desired_shape:
310 # mismatch, insert a reshape to fix this.
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200311 op.set_input_tensor(create_reshape_tensor(inp, desired_shape), 0)
Tim Hall79d07d22020-04-27 18:20:16 +0100312
313 return op
314
315
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200316def convert_batched_fc_to_conv(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200317 if op.type == Op.FullyConnected:
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200318 ifm = op.inputs[0]
319 ofm = op.outputs[0]
320 # Check if the FC is 2D and first dimension indicates batching
321 if len(ifm.shape) == len(ofm.shape) == 2 and ifm.shape[0] != 1:
322 n = ifm.shape[0]
323 batching_split = {4: (2, 2), 8: (2, 4), 16: (4, 4)}
324 h, w = batching_split.get(n, (1, n))
325
326 # Convert to convolution
327 op.name += "_conv"
Louis Verhaardaee5d752020-09-30 09:01:52 +0200328 op.type = Op.Conv2DBias
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200329 op.attrs = {
330 "dilation": (1, 1, 1, 1),
331 "dilation_h_factor": 1,
332 "dilation_w_factor": 1,
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200333 "padding": b"SAME",
334 "stride_h": 1,
335 "stride_w": 1,
336 "strides": (1, 1, 1, 1),
337 }
338
339 prev_op = ifm.ops[0]
340 desired_shape = [1, h, w, ifm.shape[-1]]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200341 if len(ifm.consumer_list) == 1 and prev_op is not None and prev_op.type == Op.Reshape:
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200342 # There is a preceding Reshape
343 # Compare input of prev_op and input of op, to see if prev_op can be removed
344 ifm_prev_op = prev_op.inputs[0]
Tim Hall93582962020-09-09 21:58:15 +0100345 if ifm_prev_op.shape == ifm.shape and check_quantized_tens_scaling_equal(ifm_prev_op, ifm.quantization):
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200346 # prev_op can be removed
347 op.set_input_tensor(ifm_prev_op, 0)
348 else:
349 op.inputs[0].set_all_shapes(desired_shape)
350 prev_op.set_input_tensor(
351 create_const_tensor(prev_op.inputs[1].name, [1], DataType.int32, desired_shape), 1
352 )
353 prev_op.attrs["new_shape"] = desired_shape
354 else:
355 # Add reshape op to the input if there is no preceding reshape
356 ifm.consumer_list.remove(op)
357 op.set_input_tensor(create_reshape_tensor(ifm, desired_shape), 0)
358
359 # Reshape Weights to be 4D. IO becomes HWIO
360 weight_tensor = op.inputs[1]
361 weight_tensor.quant_values = np.expand_dims(np.expand_dims(weight_tensor.quant_values, axis=0), axis=0)
362 weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape))
363
364 desired_shape = [1, h, w, ofm.shape[-1]]
365 if (
366 len(ofm.consumer_list) == 1
367 and ofm.consumer_list[0] is not None
Louis Verhaardaee5d752020-09-30 09:01:52 +0200368 and ofm.consumer_list[0].type == Op.Reshape
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200369 ):
370 # There is a subsequent Reshape
371 # Compare desired shape and output of consumer op, to see if consumer op can be removed
372 ofm_cons_op = ofm.consumer_list[0].outputs[0]
Tim Hall93582962020-09-09 21:58:15 +0100373 if desired_shape == ofm_cons_op.shape and check_quantized_tens_scaling_equal(ofm, ofm_cons_op):
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200374 op.outputs[0] = ofm_cons_op
375 op.outputs[0].ops = [op]
376 else:
377 op.outputs[0].set_all_shapes(desired_shape)
378 else:
379 # Add rehape op to the output
380 op.set_output_tensor(create_reshape_tensor(ofm, desired_shape, False))
381 return op
382
383
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200384def fixup_pack_input(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200385 if op.type == Op.Pack:
Tim Hall79d07d22020-04-27 18:20:16 +0100386 # Pack is also referred to as Stack
387 # Requires the rewrite_concat function to be called on the op afterwards
388 axis = int(op.attrs["axis"])
389 desired_shape = op.inputs[0].shape[:axis] + [1] + op.inputs[0].shape[axis:]
390
391 # Construct 1 shape tensor to be used by all inserted reshape ops
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100392 new_shape_tens = create_const_tensor(op.name + "_reshape_shape", [1], DataType.int32, desired_shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100393
394 for idx, inp in enumerate(op.inputs):
Tim Hall79d07d22020-04-27 18:20:16 +0100395 reshape_out = inp.clone("_reshaped")
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100396 reshape_out.set_all_shapes(desired_shape)
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100397
Louis Verhaardaee5d752020-09-30 09:01:52 +0200398 reshape_op = Operation(Op.Reshape, "{}{}_reshape".format(op.name, idx))
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100399 reshape_op.attrs["new_shape"] = desired_shape
400 reshape_op.inputs = [inp, new_shape_tens]
401 reshape_op.set_output_tensor(reshape_out)
Tim Hall79d07d22020-04-27 18:20:16 +0100402
403 op.inputs[idx] = reshape_out
404
Louis Verhaardaee5d752020-09-30 09:01:52 +0200405 op.type = Op.PackReshaped
Tim Hall79d07d22020-04-27 18:20:16 +0100406
407 return op
408
409
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200410def unfuse_activation_function(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200411 if op.type == Op.ConcatTFLite and op.run_on_npu and op.activation is not None:
412 act_op = Operation(op.activation, op.name + op.activation.name)
413 op.activation = None
Fredrik Svedberg0f98b362020-09-29 10:00:39 +0200414 out_tens = op.outputs[0]
415 intermediate_tens = out_tens.clone("_act_intermediate")
416 act_op.set_output_tensor(out_tens)
417 act_op.add_input_tensor(intermediate_tens)
418 op.set_output_tensor(intermediate_tens)
419
420 return op
421
Louis Verhaard8912c532020-09-30 12:11:49 +0200422
Diqing Zhongc7c0b1b2020-10-26 11:45:25 +0100423def fixup_stridedslice_output(tens, arch, nng):
424 op = tens.ops[0]
425 if op.type == Op.StridedSlice:
426 reshape_input_shape = tens.shape
427 new_axis_mask = op.attrs["new_axis_mask"]
428 shrink_axis_mask = op.attrs["shrink_axis_mask"]
429 ellipsis_mask = op.attrs["ellipsis_mask"]
430
431 if (new_axis_mask != 0 and shrink_axis_mask != 0) or ellipsis_mask != 0:
432 # Not supported, will be put on CPU
433 return tens
434 if shrink_axis_mask == 0 and new_axis_mask == 0:
435 # Equal Rank StridedSlice, no need to insert reshape
436 return tens
437 elif shrink_axis_mask != 0:
438 n = 0
439 axis = 0
440 while shrink_axis_mask:
441 prev_mask = shrink_axis_mask
442 n += 1
443 shrink_axis_mask &= shrink_axis_mask - 1
444 axis = int(math.log2(prev_mask - shrink_axis_mask))
445 reshape_input_shape = reshape_input_shape[:axis] + [1] + reshape_input_shape[axis:]
446
447 assert len(tens.shape) == (len(op.inputs[0].shape) - n)
448 op.attrs["shrink_axis_mask"] = 0
449
450 elif new_axis_mask != 0:
451 n = 0
452 axis = 0
453 while new_axis_mask:
454 prev_mask = new_axis_mask
455 n += 1
456 new_axis_mask &= new_axis_mask - 1
457 axis = int(math.log2(prev_mask - new_axis_mask))
458 reshape_input_shape = reshape_input_shape[:axis] + reshape_input_shape[(axis + 1) :]
459 new_axis_mask >>= 1
460
461 assert len(tens.shape) == (len(op.inputs[0].shape) + n)
462 op.attrs["new_axis_mask"] = 0
463
464 # Construct 1 shape tensor to be used by all inserted reshape ops
465 new_shape_tens = create_const_tensor(op.name + "_reshape_shape", [1], DataType.int32, tens.shape)
466
467 for idx, out_tens in enumerate(op.outputs):
468 reshape_in = out_tens.clone("_reshaped")
469 reshape_in.set_all_shapes(reshape_input_shape)
470 reshape_in.ops = [op]
471
472 reshape_op = Operation(Op.Reshape, "{}{}_reshape".format(op.name, idx))
473 reshape_op.attrs["new_shape"] = reshape_input_shape
474 reshape_op.inputs = [reshape_in, new_shape_tens]
475 reshape_op.set_output_tensor(out_tens)
476
477 op.outputs[idx] = reshape_in
478
479 return tens
480
481
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200482def fixup_unpack_output(tens, arch, nng):
Tim Hall79d07d22020-04-27 18:20:16 +0100483 op = tens.ops[0]
Diqing Zhongc7c0b1b2020-10-26 11:45:25 +0100484 if op.run_on_npu and op.type == Op.Unpack:
Tim Hall79d07d22020-04-27 18:20:16 +0100485 # Unpack is also referred to as Unstack
486 # Requires the rewrite_split function to be called on the op afterwards
Diqing Zhongc7c0b1b2020-10-26 11:45:25 +0100487 axis = int(op.attrs["axis"])
488 op.type = Op.UnpackReshaped
489 reshape_input_shape = tens.shape[:axis] + [1] + tens.shape[axis:]
Tim Hall79d07d22020-04-27 18:20:16 +0100490
491 # Construct 1 shape tensor to be used by all inserted reshape ops
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100492 new_shape_tens = create_const_tensor(op.name + "_reshape_shape", [1], DataType.int32, tens.shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100493
494 for idx, out_tens in enumerate(op.outputs):
Tim Hall79d07d22020-04-27 18:20:16 +0100495 reshape_in = out_tens.clone("_reshaped")
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100496 reshape_in.set_all_shapes(reshape_input_shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100497 reshape_in.ops = [op]
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100498
Louis Verhaardaee5d752020-09-30 09:01:52 +0200499 reshape_op = Operation(Op.Reshape, "{}{}_reshape".format(op.name, idx))
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100500 reshape_op.attrs["new_shape"] = reshape_input_shape
Tim Hall79d07d22020-04-27 18:20:16 +0100501 reshape_op.inputs = [reshape_in, new_shape_tens]
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100502 reshape_op.set_output_tensor(out_tens)
Tim Hall79d07d22020-04-27 18:20:16 +0100503
504 op.outputs[idx] = reshape_in
505
506 return tens
507
508
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200509def add_padding_fields(op, arch, nng):
Jacob Bohlin90033f32020-08-28 15:45:44 +0200510 if op.run_on_npu:
511 if "padding" in op.attrs:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200512 if op.type.is_conv2d_op() or op.type.is_depthwise_conv2d_op():
Jacob Bohlin90033f32020-08-28 15:45:44 +0200513 kernel_size = op.inputs[1].shape[:2]
514 input_shape = op.inputs[0].shape
Louis Verhaardaee5d752020-09-30 09:01:52 +0200515 elif op.type.is_pool_op() or op.type.npu_block_type == NpuBlockType.ReduceSum:
Jacob Bohlin90033f32020-08-28 15:45:44 +0200516 kernel_size = op.attrs["ksize"][1:3]
517 input_shape = op.inputs[0].shape
Jacob Bohlin90033f32020-08-28 15:45:44 +0200518 else:
519 raise UnsupportedFeatureError("Unknown operation that uses padding: {}".format(op.type))
Tim Hall79d07d22020-04-27 18:20:16 +0100520
Louis Verhaardaee5d752020-09-30 09:01:52 +0200521 if op.type == Op.Conv2DBackpropInputSwitchedBias:
Jacob Bohlin90033f32020-08-28 15:45:44 +0200522 upscaling_factor = op.outputs[0].shape[1] // input_shape[1]
523 padding, skirt = calc_upscaled_padding_and_skirt(
524 op.attrs["padding"], kernel_size, op.attrs["strides"], input_shape, upscaling_factor
525 )
526 else:
527 dilation_h, dilation_w = op.get_dilation_h_w()
528 dilated_kernel_size = [dilation_h * (kernel_size[0] - 1) + 1, dilation_w * (kernel_size[1] - 1) + 1]
529 padding, skirt = calc_padding_and_skirt(
530 op.attrs["padding"], dilated_kernel_size, op.attrs["strides"], input_shape
531 )
Jacob Bohlincf7da102020-05-20 09:03:40 +0200532
Jacob Bohlin90033f32020-08-28 15:45:44 +0200533 op.attrs["explicit_padding"] = padding
534 op.attrs["skirt"] = skirt
Jacob Bohlincf7da102020-05-20 09:03:40 +0200535
Tim Hall79d07d22020-04-27 18:20:16 +0100536 return op
537
538
Tim Hall79d07d22020-04-27 18:20:16 +0100539# Check if the op can be reordered
540def get_prepend_op(op):
541 inp = op.inputs[0]
542 # The op should be reordered between prev_op and prep_op
543 prev_op = inp.ops[-1]
544 prep_op = None
545 while prev_op.type in memory_only_ops and len(prev_op.outputs) == 1 and len(prev_op.outputs[0].consumers()) == 1:
546 prep_op = prev_op
547 inp = prev_op.inputs[0]
548 prev_op = inp.ops[-1]
Diego Russoea6111a2020-04-14 18:41:58 +0100549 if prev_op is not None and len(prev_op.outputs) == 1 and len(prev_op.outputs[0].consumers()) == 1:
Tim Hall79d07d22020-04-27 18:20:16 +0100550 return prep_op
551
552 return None
553
554
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200555def convert_depthwise_to_conv(op, arch, nng):
Tim Hall79d07d22020-04-27 18:20:16 +0100556 # Depthwise is equivalent to a single conv2d if the ifm depth is 1 and
557 # the ofm depth equals the depth multipler.
558 # If those conditions are true, then we can perform a simple
559 # switch of the operator type (and weight order)
560
Louis Verhaardaee5d752020-09-30 09:01:52 +0200561 if op.type == Op.DepthwiseConv2DBias and (op.attrs["depth_multiplier"] != 1):
Tim Hall79d07d22020-04-27 18:20:16 +0100562 ifm_tensor = op.inputs[0]
563 weight_tensor = op.inputs[1]
564 ofm_tensor = op.outputs[0]
565 if (ifm_tensor.shape[3] == 1) and (ofm_tensor.shape[3] == op.attrs["depth_multiplier"]):
566 # Change op type to Conv2d
Louis Verhaardaee5d752020-09-30 09:01:52 +0200567 op.type = Op.Conv2DBias
Tim Hall79d07d22020-04-27 18:20:16 +0100568 del op.attrs["channel_multiplier"]
569 del op.attrs["depth_multiplier"]
570
571 weight_tensor.quant_values = np.transpose(weight_tensor.quant_values, (0, 1, 3, 2))
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100572 weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape))
Tim Hall79d07d22020-04-27 18:20:16 +0100573 else:
Louis Verhaard7db78962020-05-25 15:05:26 +0200574 raise UnsupportedFeatureError(
575 "Unsupported DepthwiseConv2d with depth_multiplier = {}, ifm channels = {}, ofm channels = {}".format(
Tim Hall79d07d22020-04-27 18:20:16 +0100576 op.attrs["depth_multiplier"], ifm_tensor.shape[3], ofm_tensor.shape[3]
577 )
578 )
Tim Hall79d07d22020-04-27 18:20:16 +0100579 return op
580
581
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200582def reorder_depthwise_weights(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200583 if op.type.is_depthwise_conv2d_op():
Jacob Bohline843d332020-06-23 12:12:56 +0200584 weight_tensor = op.inputs[1]
585 weight_tensor.quant_values = np.transpose(weight_tensor.quant_values, (0, 1, 3, 2))
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100586 weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape))
Jacob Bohline843d332020-06-23 12:12:56 +0200587 weight_tensor.weight_transpose_depthwise = True
588
589 return op
590
591
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200592def convert_conv_to_fc(op, arch, nng):
Michael McGeagh8d939c02020-07-29 13:11:43 +0100593 # Conv 1x1 can be equivalent to Fully Connected.
594 # By representing certain convs as fully connected layers, Vela can better determine wether or not to use
595 # caching/double buffering for the weights.
596 # (Weights dont need to be reloaded for convs when IFM H and W are 1)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200597 if op.type == Op.Conv2DBias:
Michael McGeagh8d939c02020-07-29 13:11:43 +0100598 _, h, w, _ = op.inputs[0].shape
599 kh, kw, _, _ = op.inputs[1].shape
600 if h == 1 and w == 1 and kh == 1 and kw == 1:
601 # Overwrite this op as a Fully Connected Op
602 op.name += "_fc"
Louis Verhaardaee5d752020-09-30 09:01:52 +0200603 op.type = Op.FullyConnected
Michael McGeagh8d939c02020-07-29 13:11:43 +0100604 op.attrs = {
Michael McGeagh8d939c02020-07-29 13:11:43 +0100605 "weights_format": 0,
Michael McGeagh8d939c02020-07-29 13:11:43 +0100606 }
607 # Reshape Weights to be 2D. HWIO becomes just IO (as H and W are 1, they can just be dropped)
608 weight_tensor = op.inputs[1]
609 weight_tensor.quant_values = weight_tensor.quant_values.squeeze(axis=(0, 1))
610 weight_tensor.set_all_shapes(list(weight_tensor.quant_values.shape))
611 # The output from a fully connected is expected to be 2D so we need to add a reshape layer to convert it
612 # back to 4D afterwards as the next layer is expecting that shape
613 orig_ofm_tensor = op.outputs[0]
614 # 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})
615 fc_ofm_tensor = orig_ofm_tensor.clone("_fc")
616 fc_ofm_tensor.set_all_shapes([1, fc_ofm_tensor.shape[-1]])
617 fc_ofm_tensor.ops = [op]
618 # Add a reshape after the new OFM to convert it back to the original 4D shape
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100619 reshape_name = op.name + "_reshape"
620 new_shape_tens = create_const_tensor(reshape_name + "_shape", [1], DataType.int32, orig_ofm_tensor.shape)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200621 reshape_op = Operation(Op.Reshape, reshape_name)
Michael McGeagh8d939c02020-07-29 13:11:43 +0100622 reshape_op.attrs["new_shape"] = orig_ofm_tensor.shape
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100623 reshape_op.inputs = [fc_ofm_tensor, new_shape_tens]
624 reshape_op.set_output_tensor(orig_ofm_tensor)
Michael McGeagh8d939c02020-07-29 13:11:43 +0100625 # Replace this ops OFM to point to the 2D tensor
626 op.outputs[0] = fc_ofm_tensor
627 return op
628
629
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200630def fixup_relus_with_differing_ifm_ofm_scaling(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200631 if op.run_on_npu and op.type.is_relu_op():
Michael McGeagh8dbf8cf2020-09-08 11:09:48 +0100632 ifm = op.inputs[0]
633 ofm = op.outputs[0]
634 # Relu with differing IFM and OFM scaling cannot be fused with another primary op
635 # and requires its own to be inserted
Tim Hall93582962020-09-09 21:58:15 +0100636 if not check_quantized_tens_scaling_equal(ifm, ofm):
Michael McGeagh8dbf8cf2020-09-08 11:09:48 +0100637 # Override this op with its own primary op (avgpool)
638 relu_fused_op = create_avgpool_nop(op.name + "_avgpool")
639 # And fuse the original activation function to it
Louis Verhaardaee5d752020-09-30 09:01:52 +0200640 relu_fused_op.activation = op.type
Michael McGeagh8dbf8cf2020-09-08 11:09:48 +0100641 # Tidy up and assign the ifm and ofm to the new op
642 ifm.consumer_list.remove(op)
Andreas Nevalainenf3d737e2020-09-25 14:12:43 +0200643
644 # if not 4d, reshape ifm/ofm
645 if len(ifm.shape) < 4:
646 ifm_shaped = create_reshape_tensor(ifm, full_shape(4, ifm.shape, 1))
647 ifm = ifm_shaped
648 if len(ofm.shape) < 4:
649 ofm_shaped = create_reshape_tensor(ofm, full_shape(4, ofm.shape, 1), False)
650 ofm = ofm_shaped
651
Michael McGeagh8dbf8cf2020-09-08 11:09:48 +0100652 relu_fused_op.add_input_tensor(ifm)
653 relu_fused_op.set_output_tensor(ofm)
654 op = relu_fused_op
655 return op
656
657
Tim Hall79d07d22020-04-27 18:20:16 +0100658# Reorder activation op if it's after the memory only operations
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200659def fixup_act_reorder(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200660 if op.type.is_relu_op() or op in set((Op.Sigmoid, Op.Tanh)):
Tim Hall79d07d22020-04-27 18:20:16 +0100661 prep_op = get_prepend_op(op)
Diego Russoea6111a2020-04-14 18:41:58 +0100662 if prep_op is not None:
Tim Hall79d07d22020-04-27 18:20:16 +0100663 act_op = op.clone("_reordered")
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200664
665 # There is only one input tensor, overwrite it
666 act_op.set_input_tensor(prep_op.inputs[0], 0)
667
Tim Hall79d07d22020-04-27 18:20:16 +0100668 act_op_out = act_op.inputs[0].clone("_acted")
669 act_op_out.quantization = op.outputs[0].quantization.clone()
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100670 act_op.set_output_tensor(act_op_out)
Patrik Gustavssoncb337042020-09-16 14:55:40 +0200671
672 # Update the consumer list
673 act_op_out.consumer_list = op.outputs[0].consumer_list.copy()
674 act_op_out.consumer_list.append(prep_op)
675
Tim Hall79d07d22020-04-27 18:20:16 +0100676 prep_op.inputs[0] = act_op_out
677 prep_op.outputs[0].quantization = act_op_out.quantization.clone()
678
679 # Mark the op so that it will be removed as passthrough later on
Louis Verhaardaee5d752020-09-30 09:01:52 +0200680 op.type = Op.Identity
Tim Hall79d07d22020-04-27 18:20:16 +0100681 return op
682
Louis Verhaarde0ef2732020-06-03 08:56:44 +0200683
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200684def fixup_elementwise_with_scalars(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200685 if op.type.is_binary_elementwise_op():
Louis Verhaarde0ef2732020-06-03 08:56:44 +0200686 ifm_tensor, ifm2_tensor, _, _ = op.get_ifm_ifm2_weights_ofm()
Charles Xu78792222020-05-13 10:15:26 +0200687 if ifm2_tensor.shape != [] and ifm_tensor.shape != []:
688 diff = len(ifm_tensor.shape) - len(ifm2_tensor.shape)
689 if diff > 0:
690 ifm2_tensor.shape = full_shape(len(ifm_tensor.shape), ifm2_tensor.shape, 1)
691 elif diff < 0:
692 ifm_tensor.shape = full_shape(len(ifm2_tensor.shape), ifm_tensor.shape, 1)
Louis Verhaarde0ef2732020-06-03 08:56:44 +0200693 elif ifm_tensor.shape == [] and ifm_tensor.quant_values is None:
694 # IFM is marked as a scalar, but is a result of an operation; change it to a shape of size 1
695 ifm_tensor.shape = len(ifm2_tensor.shape) * [1]
696 ifm_tensor.storage_shape = ifm_tensor.shape
697 elif ifm2_tensor.shape == [] and ifm2_tensor.quant_values is None:
698 # IFM2 is marked as a scalar, but is a result of an operation; change it to a shape of size 1
699 ifm2_tensor.shape = len(ifm_tensor.shape) * [1]
700 ifm2_tensor.storage_shape = ifm2_tensor.shape
Charles Xu78792222020-05-13 10:15:26 +0200701 return op
Tim Hall79d07d22020-04-27 18:20:16 +0100702
Louis Verhaarde0ef2732020-06-03 08:56:44 +0200703
Tim Hall4e127762020-05-15 16:05:49 +0100704# Set input/output tensor equivalence to the same id for memory operations
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200705def set_tensor_equivalence(op, arch, nng):
Michael McGeagh11b0bdb2020-09-08 11:07:35 +0100706 if op.type in memory_only_ops:
Tim Hall4e127762020-05-15 16:05:49 +0100707 eid = op.outputs[0].equivalence_id
708 for inp in op.inputs:
709 inp.equivalence_id = eid
710 return op
711
712
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200713def convert_softmax(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200714 if op.type == Op.Softmax and op.run_on_npu:
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200715 softmax = SoftMax(op)
716 op = softmax.get_graph()
717 return op
718
719
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200720def convert_mul_max_to_abs_or_lrelu(op, arch, nng):
Diego Russoea6111a2020-04-14 18:41:58 +0100721 r"""Whenever there is a subgraph with this topology:
Tim Hall79d07d22020-04-27 18:20:16 +0100722
723 Input X For X = -1 or X > 0
724 | \ / This subgraph can be replaced with either
725 | Mul an Abs (if X = -1) or a LeakyReLU (if X > 0)
726 | /
727 Max
728 """
729
Louis Verhaardaee5d752020-09-30 09:01:52 +0200730 if op.type == Op.Maximum:
Tim Hall79d07d22020-04-27 18:20:16 +0100731 # finds the Mul input(s) to the Max
Louis Verhaardaee5d752020-09-30 09:01:52 +0200732 muls = [i for i in op.inputs if i.ops[0].type == Op.Mul]
Tim Hall79d07d22020-04-27 18:20:16 +0100733 if len(muls) == 1:
734 mul = muls[0].ops[0]
735 elif len(muls) == 2:
736 # In the case both inputs are Muls, find the one with the same input as the Max
737 mul = [m for m in muls if len(set(op.inputs + m.ops[0].inputs)) == 1][0].ops[0]
738 else:
739 # No Mul inputs
740 return op
741
742 # make sure the Mul doesn't have any other consumers
Louis Verhaardd7911c42020-08-25 13:36:41 +0200743 mul_ofm = mul.outputs[0]
744 if len(mul_ofm.consumers()) != 1:
Tim Hall79d07d22020-04-27 18:20:16 +0100745 return op
Louis Verhaardaee5d752020-09-30 09:01:52 +0200746 # make sure the Mul doesn't have a fused activation function
747 if mul.activation:
Tim Hall79d07d22020-04-27 18:20:16 +0100748 return op
Louis Verhaardaee5d752020-09-30 09:01:52 +0200749 ifm, ofm = op.get_ifm_ofm()
Tim Hall93582962020-09-09 21:58:15 +0100750 if ifm is None or ofm is None:
751 return op
752
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200753 if ifm.dtype not in (DataType.uint8, DataType.int8) or ifm.dtype != ofm.dtype:
754 return op
Tim Hall93582962020-09-09 21:58:15 +0100755 if not check_quantized_tens_scaling_equal(ifm, ofm) or not check_quantized_tens_scaling_equal(ifm, mul_ofm):
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200756 # rewrite to LeakyRelu currently only makes sense if the quantization is identical
757 return op
Tim Hall79d07d22020-04-27 18:20:16 +0100758
759 # finds the branched input that goes to both the Max and the Mul
760 shared = set(op.inputs) & set(mul.inputs)
761 if len(shared) == 1:
762 shared_in = shared.pop()
763 # find the constant scalar input to the Mul
764 const_tens = (set(mul.inputs) - {shared_in}).pop()
765 # check that it is a scalar
766 if const_tens.shape != []:
767 return op
768 const = const_tens.ops[0]
769 # check that it is a constant
Louis Verhaardaee5d752020-09-30 09:01:52 +0200770 if const.type != Op.Const:
Tim Hall79d07d22020-04-27 18:20:16 +0100771 return op
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200772 # Remove the Mul from the shared input's consumers
773 shared_in.consumer_list.remove(mul)
Tim Hall79d07d22020-04-27 18:20:16 +0100774 else:
775 return op
776
777 val = const.outputs[0].values
778 if val >= 0:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200779 new_op = Op.LeakyRelu
Tim Hall79d07d22020-04-27 18:20:16 +0100780 op.attrs["alpha"] = val
Louis Verhaardd7911c42020-08-25 13:36:41 +0200781 # to produce bit exact results, the alpha is not enough;
782 # save additional scaling info in attr "alpha_scale", to be used as input
783 # to the LUT construction
784 alpha_scalar = const_tens.quant_values - const_tens.quantization.zero_point
785 mul_ifm_scale = np.double(ifm.quantization.scale_f32)
786 mul_ifm2_scale = np.double(const_tens.quantization.scale_f32)
787 mul_ofm_scale = np.double(mul_ofm.quantization.scale_f32)
788 alpha_scale, alpha_shift = scaling.elementwise_mul_scale(mul_ifm_scale, mul_ifm2_scale, mul_ofm_scale)
789 op.attrs["alpha_scaling"] = (alpha_scalar, alpha_scale, alpha_shift)
Tim Hall79d07d22020-04-27 18:20:16 +0100790 elif val == -1:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200791 new_op = Op.Abs
Tim Hall79d07d22020-04-27 18:20:16 +0100792 else:
793 return op
794
Louis Verhaardaee5d752020-09-30 09:01:52 +0200795 op.type = new_op
796 op.name = op.name.replace("Maximum", new_op.name)
797 op.outputs[0].name = op.outputs[0].name.replace("Maximum", new_op.name)
Tim Hall79d07d22020-04-27 18:20:16 +0100798 op.inputs = [shared_in]
799 return op
800
801
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200802def convert_lrelu_to_mul_max(op, arch):
803 # Converts LeakyRelu to Max(alpha * IFM, identity * IFM)
804 # (the opposite of convert_mul_max_to_abs_or_lrelu)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200805 ifm, ofm = op.get_ifm_ofm()
Tim Hall93582962020-09-09 21:58:15 +0100806 if ifm is None or ofm is None:
807 return op
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200808
809 # Add multiplication with alpha
Louis Verhaardaee5d752020-09-30 09:01:52 +0200810 mul_alpha = Operation(Op.Mul, op.name + "_mul_alpha")
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200811 mul_alpha.add_input_tensor(ifm)
812 # Create const tensor containing alpha as scalar
813 alpha = op.attrs["alpha"]
814 quantization = ifm.quantization.clone()
815 quantization.min = 0
816 quantization.max = alpha * (quantization.quant_max - quantization.quant_min)
817 quantization.scale_f32 = alpha
818 quantization.zero_point = 0
819 alpha_tens = create_const_tensor(op.name + "_alpha_scalar", [], ifm.dtype, [1], np.int8, quantization=quantization)
820 mul_alpha.add_input_tensor(alpha_tens)
821 fm_alpha = ofm.clone(op.name + "_alpha")
822 mul_alpha.set_output_tensor(fm_alpha)
823
Tim Hall93582962020-09-09 21:58:15 +0100824 if check_quantized_tens_scaling_equal(ifm, ofm):
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200825 # No identity multiplication is needed
826 fm_id = ifm
827 else:
828 # Add multiplication with identity
Louis Verhaardaee5d752020-09-30 09:01:52 +0200829 mul_identity = Operation(Op.Mul, op.name + "_mul_identity")
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200830 mul_identity.add_input_tensor(ifm)
831 # Create const tensor containing identity as scalar
832 quantization = ifm.quantization.clone()
833 quantization.min = 0
834 quantization.max = quantization.quant_max - quantization.quant_min
835 quantization.scale_f32 = 1
836 quantization.zero_point = 0
837 identity_tens = create_const_tensor(
838 op.name + "_id_scalar", [], ifm.dtype, [1], np.uint8, quantization=quantization
839 )
840 mul_identity.add_input_tensor(identity_tens)
841 fm_id = ofm.clone(op.name + "_id")
842 mul_identity.set_output_tensor(fm_id)
843
844 # Convert LeakyRelu to Max, add the results of the multiplication(s) as inputs
Louis Verhaardaee5d752020-09-30 09:01:52 +0200845 op.type = Op.Maximum
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200846 op.name = op.name.replace("LeakyRelu", "Maximum")
847 op.inputs = []
848 ifm.consumer_list.remove(op)
849 op.add_input_tensor(fm_alpha)
850 op.add_input_tensor(fm_id)
851 return op
852
853
Louis Verhaard2e186c72020-10-09 10:47:04 +0200854def convert_to_lut(op, lut_values, lut_name):
Louis Verhaardf03bad32020-09-25 08:30:44 +0200855 # Rewrite the operation by Add with scalar 0 + LUT activation
856 ifm = op.inputs[0]
Tim Hall93582962020-09-09 21:58:15 +0100857 if ifm is None:
858 return op
Louis Verhaard58520b92020-08-24 16:45:38 +0200859 assert ifm.dtype.size_in_bytes() == 1
Louis Verhaardaee5d752020-09-30 09:01:52 +0200860 op.type = Op.Add
Louis Verhaard2e186c72020-10-09 10:47:04 +0200861 op.name = op.name + "_lut_" + lut_name
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200862 # Mark as no-op to enable potential fusing optimizations
863 op.attrs["is_nop"] = True
864 # Create an input tensor containing scalar zero
865 quantization = QuantizationParameters(0.0, 255.0)
Louis Verhaardd7911c42020-08-25 13:36:41 +0200866 quantization.scale_f32 = ifm.quantization.scale_f32
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200867 quantization.zero_point = 0
Louis Verhaard2e186c72020-10-09 10:47:04 +0200868 tens = create_const_tensor(op.inputs[0].name + "_scalar0", [], ifm.dtype, [0], np.uint8, quantization=quantization)
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200869 op.add_input_tensor(tens)
Louis Verhaardf03bad32020-09-25 08:30:44 +0200870 # The LUT must be applied without any preceding rescaling (the LUT itself performs the rescale),
871 # so even if the OFM has a different scale than the IFM, the generated OFM scale instructions
872 # should be the same as the IFM
Louis Verhaardaee5d752020-09-30 09:01:52 +0200873 op.forced_output_quantization = ifm.quantization
Louis Verhaard2e186c72020-10-09 10:47:04 +0200874 lut_tensor = lut.create_lut_tensor(op.name + "_values", lut_values, DataType.int8)
Louis Verhaardf03bad32020-09-25 08:30:44 +0200875 op.set_activation_lut(lut_tensor)
876 return op
877
878
Louis Verhaard2e186c72020-10-09 10:47:04 +0200879def convert_to_lut8(op, fn, fn_name):
Louis Verhaardf03bad32020-09-25 08:30:44 +0200880 # Converts op to a no-op + int8/uint8 LUT which is generated with the given function.
881 # fn is a function(real) -> real
Louis Verhaardaee5d752020-09-30 09:01:52 +0200882 ifm, ofm = op.get_ifm_ofm()
Louis Verhaardf03bad32020-09-25 08:30:44 +0200883 if ifm.dtype not in (DataType.uint8, DataType.int8) or ifm.dtype != ofm.dtype:
884 return op
885 # Generate the LUT
886 ifm_scale = np.double(ifm.quantization.scale_f32)
887 ofm_scale = np.double(ofm.quantization.scale_f32)
888 zp_in = ifm.quantization.zero_point
889 zp_out = ofm.quantization.zero_point
890 values = []
891 ix = range(256) if ifm.dtype == DataType.uint8 else range(-128, 128)
892 quantized_min = min(ix)
893 quantized_max = max(ix)
894 for x in ix:
895 x_real = ifm_scale * (x - zp_in)
896 y_real = fn(x_real)
897 lut_result = round_away_zero(zp_out + y_real / ofm_scale)
898 lut_result = min(quantized_max, max(quantized_min, lut_result))
899 values.append(lut_result)
Louis Verhaard2e186c72020-10-09 10:47:04 +0200900 return convert_to_lut(op, values, fn_name)
Louis Verhaardf03bad32020-09-25 08:30:44 +0200901
902
903def convert_lrelu_to_lut(op, arch):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200904 ifm, ofm = op.get_ifm_ofm()
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200905 # Generate the LUT
Louis Verhaardd7911c42020-08-25 13:36:41 +0200906 alpha = op.attrs["alpha"]
907 ifm_scale = np.double(ifm.quantization.scale_f32)
908 ofm_scale = np.double(ofm.quantization.scale_f32)
909 zp_in = ifm.quantization.zero_point
910 zp_out = ofm.quantization.zero_point
911 identity_scale, identity_shift = scaling.elementwise_mul_scale(ifm_scale, 1, ofm_scale)
912 alpha_scalar = 1
913 alpha_scale, alpha_shift = scaling.elementwise_mul_scale(ifm_scale, alpha, ofm_scale)
914 if "alpha_scaling" in op.attrs:
915 # The LeakyRelu was the result from convert_mul_max_to_abs_or_lrelu
916 alpha_scalar, alpha_scale, alpha_shift = op.attrs["alpha_scaling"]
917 values = []
Louis Verhaard58520b92020-08-24 16:45:38 +0200918 ix = range(256) if ifm.dtype == DataType.uint8 else range(-128, 128)
Louis Verhaardd7911c42020-08-25 13:36:41 +0200919 quantized_min = min(ix)
920 quantized_max = max(ix)
921 for x in ix:
922 if x < zp_in:
923 lut_result = zp_out + fp_math.multiply_by_quantized_multiplier(
924 alpha_scalar * (x - zp_in), alpha_scale, alpha_shift
925 )
926 else:
927 lut_result = zp_out + fp_math.multiply_by_quantized_multiplier(x - zp_in, identity_scale, identity_shift)
928 lut_result = min(quantized_max, max(quantized_min, lut_result))
929 values.append(lut_result)
Louis Verhaard2e186c72020-10-09 10:47:04 +0200930 return convert_to_lut(op, values, "lrelu")
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200931
932
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200933def convert_lrelu(op, arch, nng):
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200934 # Converts LeakyRelu to a LUT based solution if possible, otherwise a mul + max
Louis Verhaardaee5d752020-09-30 09:01:52 +0200935 if op.type != Op.LeakyRelu:
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200936 return op
Louis Verhaardaee5d752020-09-30 09:01:52 +0200937 ifm, ofm = op.get_ifm_ofm()
Tim Hall93582962020-09-09 21:58:15 +0100938 if ifm is None or ofm is None:
939 return op
Louis Verhaardd7911c42020-08-25 13:36:41 +0200940 if ifm.dtype in (DataType.uint8, DataType.int8) and ifm.dtype == ofm.dtype:
941 # use LUT for int8/uint8
942 return convert_lrelu_to_lut(op, arch)
Tim Hall93582962020-09-09 21:58:15 +0100943 if check_quantized_tens_scaling_equal(ifm, ofm) and ifm.dtype == ofm.dtype == DataType.int16:
Louis Verhaardd7911c42020-08-25 13:36:41 +0200944 # use LeakyRelu unmodified for int16 with equal input/output scaling
945 return op
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200946 return convert_lrelu_to_mul_max(op, arch)
947
948
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200949def convert_tanh_sigmoid_to_lut(op, arch, nng):
Louis Verhaardf03bad32020-09-25 08:30:44 +0200950 # Converts int8/uint8 Sigmoid and Tanh to a LUT based solution
Louis Verhaardaee5d752020-09-30 09:01:52 +0200951 if op.type == Op.Sigmoid:
Louis Verhaard2e186c72020-10-09 10:47:04 +0200952 return convert_to_lut8(op, clamp_sigmoid, "sigmoid")
Louis Verhaardaee5d752020-09-30 09:01:52 +0200953 elif op.type == Op.Tanh:
Louis Verhaard2e186c72020-10-09 10:47:04 +0200954 return convert_to_lut8(op, math.tanh, "tanh")
Louis Verhaardf03bad32020-09-25 08:30:44 +0200955 return op
956
957
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200958def remove_unwanted_reshapes(op, arch, nng):
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +0200959 # Try to remove reshapes enclosing ElementWise operator with only one non-constant input
Louis Verhaardaee5d752020-09-30 09:01:52 +0200960 if not op.run_on_npu or not op.type.is_elementwise_op():
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +0200961 return op
962
963 # Check if the ElementWise operator only have one non-constant input
Louis Verhaardaee5d752020-09-30 09:01:52 +0200964 non_const_tens = [x for x in op.inputs if x.ops[0].type != Op.Const]
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +0200965 if len(non_const_tens) != 1:
966 return op
967 ifm = non_const_tens[0]
968
969 # Check if operation is enclosed by Reshapes that can be removed
970 ofm = op.outputs[0]
971 prev_op = ifm.ops[0]
972 if (
973 len(ifm.consumer_list) == 1
Louis Verhaardaee5d752020-09-30 09:01:52 +0200974 and prev_op.type == Op.Reshape
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +0200975 and len(ofm.consumer_list) == 1
Louis Verhaardaee5d752020-09-30 09:01:52 +0200976 and ofm.consumer_list[0].type == Op.Reshape
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +0200977 ):
978 # Operation is enclosed by reshapes, check if they can be removed
Louis Verhaardaee5d752020-09-30 09:01:52 +0200979 prev_op_ifm, prev_op_ofm = prev_op.get_ifm_ofm()
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +0200980 cons_op = ofm.consumer_list[0]
981 cons_op_ifm = ofm
982 cons_op_ofm = cons_op.outputs[0]
983 if len(prev_op_ifm.shape) == len(cons_op_ofm.shape):
984 # Check if quantization is the same in the input and output for the reshape ops
Tim Hall93582962020-09-09 21:58:15 +0100985 if check_quantized_tens_scaling_equal(prev_op_ifm, prev_op_ofm) and check_quantized_tens_scaling_equal(
986 cons_op_ifm, cons_op_ofm
987 ):
Patrik Gustavsson7ad862a2020-09-29 14:09:43 +0200988 op.set_input_tensor(prev_op_ifm, 0)
989 op.set_output_tensor(cons_op_ofm)
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +0200990 return op
991
992
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +0200993def fuse_activation_function_with_prev(op, arch, nng):
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200994 # if op is a no-op: attempts to move the activation function to the preceding op
Louis Verhaardaee5d752020-09-30 09:01:52 +0200995 if not op.attrs.get("is_nop", False) or op.activation is None:
Louis Verhaardb9fc33c2020-08-13 11:47:36 +0200996 return op
Louis Verhaardaee5d752020-09-30 09:01:52 +0200997 ifm, ofm = op.get_ifm_ofm()
Tim Hall93582962020-09-09 21:58:15 +0100998 if ifm is None or ofm is None:
999 return op
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001000 # finds the input(s) to the operation
1001 prev_op = ifm.ops[0]
1002 # Note: the below checks on prev_op require that a first optimize pass on the full graph has been performed
1003 fuse = (
1004 prev_op.run_on_npu
Louis Verhaardaee5d752020-09-30 09:01:52 +02001005 and prev_op.type.npu_block_type != NpuBlockType.Default
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001006 and len(ifm.ops) == 1
1007 and len(prev_op.outputs[0].consumers()) == 1
Louis Verhaardaee5d752020-09-30 09:01:52 +02001008 and prev_op.activation is None
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001009 )
1010 if op.activation_lut is not None and arch.shram_reserved_unused_banks == 0:
1011 # TODO: if SHRAM LUT space is shared with SHRAM ACC (32, 64 MAC),
1012 # LUT currently only works correctly for elementwise ops
1013 fuse = False
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001014 if not fuse:
1015 return op
1016 # Move the fused activation function + corresponding info to prev_op
Louis Verhaardaee5d752020-09-30 09:01:52 +02001017 prev_op.activation = op.activation
1018 prev_op.forced_output_quantization = op.forced_output_quantization
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001019 if op.activation_lut is not None:
1020 prev_op.set_activation_lut(op.activation_lut)
1021 # Bypass op
Louis Verhaard98a34992020-09-01 10:39:04 +02001022 prev_op.set_output_tensor(ofm)
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001023 return op
1024
1025
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +02001026def add_attrs_to_resizebilinear(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +02001027 if op.type == Op.ResizeBilinear and op.run_on_npu:
Dwight Lidman42fed942020-05-29 09:37:03 +02001028 input_tensor = op.inputs[0]
1029 upscaled_shape = [input_tensor.shape[1] * 2, input_tensor.shape[2] * 2]
1030 out_shape = op.outputs[0].shape[1:3]
1031 if not op.attrs["align_corners"] and out_shape == upscaled_shape:
1032 # this means the output is supposed to be a x2 upscale,
1033 # so we need to do SAME padding
1034 op.attrs["padding"] = b"SAME"
1035 elif op.attrs["align_corners"] and out_shape == [upscaled_shape[0] - 1, upscaled_shape[1] - 1]:
1036 # here we can just run the avg pool without padding and
1037 # produce a (M * 2 - 1, N * 2 - 1) sized output
1038 op.attrs["padding"] = b"VALID"
1039 else:
Charles Xu9a03fdf2020-07-02 15:12:40 +02001040 return op
Dwight Lidman42fed942020-05-29 09:37:03 +02001041 input_tensor.resampling_mode = resampling_mode.NEAREST
Tim Hallc30f4952020-06-15 20:47:35 +01001042 op.attrs.update({"strides": (1, 1, 1, 1), "ksize": (1, 2, 2, 1)})
Dwight Lidman42fed942020-05-29 09:37:03 +02001043 return op
1044
1045
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +02001046def fixup_bias_tensors(op, arch, nng):
Louis Verhaardaee5d752020-09-30 09:01:52 +02001047 if op.type.needs_bias() and op.bias is None:
Jacob Bohlina41cd4d2020-08-26 18:21:28 +02001048 # Op has no bias, add bias tensor filled with zeros
1049 nr_biases = op.inputs[1].shape[-1]
1050 bias_values = [0] * nr_biases
1051 bias_tensor = create_const_tensor(op.name + "_bias", [nr_biases], DataType.int32, bias_values)
1052 bias_tensor.quant_values = bias_tensor.values
1053 op.set_input_tensor(bias_tensor, -1)
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +02001054
1055 return op
1056
1057
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +02001058def supported_operator_check(op, arch, nng):
Tim Hall79d07d22020-04-27 18:20:16 +01001059 op.run_on_npu = arch.supported_operators.is_operator_supported(op)
1060 return op
1061
1062
1063def optimise_graph_a(nng, arch, verbose_graph=False):
1064 if verbose_graph:
1065 nng.print_graph()
1066
1067 op_rewrite_list = [
Tim Hall4e127762020-05-15 16:05:49 +01001068 set_tensor_equivalence,
Tim Hall79d07d22020-04-27 18:20:16 +01001069 supported_operator_check,
1070 # then do any rewrites of supported operators
1071 convert_depthwise_to_conv,
Michael McGeagh8d939c02020-07-29 13:11:43 +01001072 convert_conv_to_fc,
Fredrik Svedberga0c36242020-06-03 15:43:31 +02001073 convert_softmax,
Tim Hall79d07d22020-04-27 18:20:16 +01001074 fixup_fully_connected_input,
Patrik Gustavssoncb337042020-09-16 14:55:40 +02001075 convert_batched_fc_to_conv,
Tim Hall79d07d22020-04-27 18:20:16 +01001076 fixup_pack_input,
Fredrik Svedberg0f98b362020-09-29 10:00:39 +02001077 unfuse_activation_function,
Tim Hall79d07d22020-04-27 18:20:16 +01001078 fixup_conv2d_backprop,
Michael McGeagh8dbf8cf2020-09-08 11:09:48 +01001079 fixup_relus_with_differing_ifm_ofm_scaling,
Tim Hall79d07d22020-04-27 18:20:16 +01001080 fixup_act_reorder,
Charles Xu78792222020-05-13 10:15:26 +02001081 fixup_elementwise_with_scalars,
Jacob Bohline843d332020-06-23 12:12:56 +02001082 reorder_depthwise_weights,
Charles Xu9a03fdf2020-07-02 15:12:40 +02001083 fixup_resizebilinear,
Jacob Bohlina41cd4d2020-08-26 18:21:28 +02001084 fixup_bias_tensors,
Dwight Lidmanc3862c22020-09-14 15:22:33 +02001085 convert_nop_split_to_identity,
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001086 convert_mul_max_to_abs_or_lrelu,
Patrik Gustavssonfa4cb292020-09-10 08:19:36 +02001087 remove_unwanted_reshapes,
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001088 convert_lrelu,
Louis Verhaardf03bad32020-09-25 08:30:44 +02001089 convert_tanh_sigmoid_to_lut,
Tim Hall79d07d22020-04-27 18:20:16 +01001090 ]
1091
1092 for idx, sg in enumerate(nng.subgraphs):
1093 # rewrite graph pass
1094 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
Diqing Zhongc7c0b1b2020-10-26 11:45:25 +01001095 nng, sg, arch, [fixup_stridedslice_output], op_rewrite_list, rewrite_unsupported=False,
Tim Hall79d07d22020-04-27 18:20:16 +01001096 )
1097
1098 for idx, sg in enumerate(nng.subgraphs):
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001099 # remove passthrough tensors and attempt further optimizations
1100 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +02001101 nng, sg, arch, [remove_passthrough_tensor], [fuse_activation_function_with_prev, add_padding_fields]
Louis Verhaardb9fc33c2020-08-13 11:47:36 +02001102 )
Tim Hall79d07d22020-04-27 18:20:16 +01001103
1104 if verbose_graph:
1105 nng.print_graph()
1106 return nng
1107
Diego Russoea6111a2020-04-14 18:41:58 +01001108
Tim Hall79d07d22020-04-27 18:20:16 +01001109def optimise_graph_b(nng, arch, verbose_graph=False):
1110 if verbose_graph:
1111 nng.print_graph()
1112
1113 for idx, sg in enumerate(nng.subgraphs):
1114 # combined rewrite graph pass
Dwight Lidmanc6ac1942020-10-02 14:55:45 +02001115 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
1116 nng, sg, arch, [fixup_unpack_output, rewrite_concat, rewrite_split], []
1117 )
Tim Hall79d07d22020-04-27 18:20:16 +01001118
1119 if verbose_graph:
1120 nng.print_graph()
1121 return nng