blob: 49fc997d131a80c0bbd3e3d3b067a6e6b8e852bb [file] [log] [blame]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +02001# Copyright (C) 2021 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.
16# Description:
17# Early optimisation of the TOSA based network graph, using the rewrite_graph module to do the traversal of the graph.
Patrik Gustavssonf366fb12021-09-07 13:30:29 +020018import numpy as np
19
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020020from . import rewrite_graph
21from .api import NpuRoundingMode
22from .data_type import DataType
23from .debug_database import DebugDatabase
Patrik Gustavssondf995102021-08-23 15:33:59 +020024from .graph_optimiser_util import bypass_reshape_and_squeeze_ops
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020025from .graph_optimiser_util import calc_explicit_padding
Patrik Gustavssondf995102021-08-23 15:33:59 +020026from .graph_optimiser_util import convert_depthwise_to_conv
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020027from .graph_optimiser_util import move_splitsliceread_to_consumer
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020028from .graph_optimiser_util import needed_total_padding
29from .graph_optimiser_util import set_ifm_ofm_op_shapes
30from .graph_optimiser_util import set_tensor_equivalence
31from .operation import ExplicitScaling
32from .operation import NpuBlockType
33from .operation import Op
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020034from .operation_util import create_add_nop
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020035from .operation_util import create_avgpool_nop
Patrik Gustavssonf1580f02021-09-01 12:43:02 +020036from .shape4d import Shape4D
37from .tensor import create_const_tensor
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +020038from .tensor import create_equivalence_id
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020039
40
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020041def replace_rescale_with_avg_pool(rescale_op):
42 assert rescale_op.type == Op.Rescale
43
44 avgpool_op = create_avgpool_nop(rescale_op.name + "_avgpool")
45 rescale_op_clone = rescale_op.clone()
46 op = rescale_op
47 op.attrs = avgpool_op.attrs.copy()
48 op.type = Op.AvgPool
49 DebugDatabase.add_optimised(rescale_op_clone, op)
50
51 return op
52
53
54def calc_skirt(kernel, input_shape, explicit_padding):
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020055 k_w, k_h = kernel.dilated_wh()
56 s_x, s_y = kernel.stride
57 ypad = needed_total_padding(int(input_shape.height), int(s_y), int(k_h))
58 xpad = needed_total_padding(int(input_shape.width), int(s_x), int(k_w))
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020059
60 top, left, bottom, right = explicit_padding
61 top_pad, bottom_pad = calc_explicit_padding(int(input_shape.height), int(s_y), int(k_h), int(top), int(bottom))
62 left_pad, right_pad = calc_explicit_padding(int(input_shape.width), int(s_x), int(k_w), int(left), int(right))
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020063
64 padding = (top_pad, left_pad, bottom_pad, right_pad)
65 skirt = (top_pad, left_pad, ypad - top_pad, xpad - left_pad)
66 return padding, skirt
67
68
69def add_padding_fields(op, arch, nng):
70 if op.run_on_npu:
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020071 if "explicit_padding" in op.attrs:
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020072 input_shape = op.ifm_shapes[0]
73
74 if op.type == Op.Conv2DBackpropInputSwitchedBias:
75 # TODO not yet supported, but there will be need for separate handling
76 assert False
77 else:
Patrik Gustavssonc74682c2021-08-17 14:26:38 +020078 padding, skirt = calc_skirt(op.kernel, input_shape, op.attrs.get("explicit_padding"))
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020079
80 op.attrs["explicit_padding"] = padding
81 op.attrs["skirt"] = skirt
82
83 return op
84
85
Patrik Gustavssonf366fb12021-09-07 13:30:29 +020086# Counts leading zeroes for a (int32)
87def count_leading_zeros(a):
88 lz = int(32)
89 if a != 0:
90 mask = 1 << (32 - 1)
91 lz = 0
92 while (mask & a) == 0:
93 mask = mask >> 1
94 lz = lz + 1
95 return lz
96
97
98def calc_scaling_avgpool(op, arch, nng):
99 if op.type == Op.AvgPool:
100 top, left, _, _ = op.attrs["explicit_padding"]
101 # TODO Only support for when global scaling can be used.
102 # That is when there is no padding
103 assert top == 0 and left == 0
104 assert op.explicit_scaling is None
105 multiplier = []
106 shift = []
107
108 kernel_wh = op.kernel.elements_wh()
109 k = 32 - count_leading_zeros(kernel_wh - 1)
110 numerator = np.int64(((1 << 30) + 1) << k)
111 multiplier.append(numerator // kernel_wh)
112 shift.append(30 + k)
113
114 op.rounding_mode = NpuRoundingMode.NATURAL
115 op.explicit_scaling = ExplicitScaling(False, shift, multiplier)
116 return op
117
118
Patrik Gustavssondf995102021-08-23 15:33:59 +0200119def remove_const_transpose(op, arch, nng):
120 if op.type == Op.Transpose:
121 removed = False
122 if len(op.ifm.ops) == 1:
123 prev_op = op.ifm.ops[0]
124 if prev_op.type == Op.Const:
125 # Transpose the Tensor and data and remove Transpose
126 # TODO move to Tensor?
127 reorder = op.attrs["perms"]
128 shape = op.ifm.shape.copy()
129 tens = op.ifm
130
131 tens.shape = [shape[idx] for idx in reorder]
132 tens.bandwidth_shape = tens.shape
133 tens.storage_shape = tens.shape
134
135 if tens.values is not None:
136 tens.values = tens.values.transpose(reorder)
137
138 op.ofm.values = tens.values
139 # Bypass the Transpose op
140 prev_op.set_output_tensor(op.ofm)
141 DebugDatabase.add_optimised(op, prev_op)
142 removed = True
143
144 if not removed:
Patrik Gustavssonf1580f02021-09-01 12:43:02 +0200145 print("Warning: Cannot remove Transpose, and handling of Transpose is not supported")
Patrik Gustavssondf995102021-08-23 15:33:59 +0200146 assert False
147
148 return op
149
150
Patrik Gustavssonf1580f02021-09-01 12:43:02 +0200151# TODO can we change to add for both TFLite and TOSA?
152def insert_add_copy_op_after_tens(tens):
153 tens_cons_list_copy = tens.consumer_list.copy()
154 copy_tens = tens.clone()
155
156 name = tens.name + "_add"
157 ifm2 = create_const_tensor(
158 name + "_zero_scalar",
159 [1],
160 copy_tens.dtype,
161 [0],
162 copy_tens.dtype.as_numpy_type(),
163 quantization=copy_tens.quantization,
164 )
165 copy_op = create_add_nop(name)
166 copy_op.add_input_tensor(tens)
167 copy_op.add_input_tensor(ifm2)
168 copy_op.set_output_tensor(copy_tens)
169 copy_op.set_ifm_ofm_shapes()
170 copy_op.run_on_npu = True
171
172 # Set copy_ifm consumers
173 for tens_cons in tens_cons_list_copy:
174 if tens_cons is not None:
175 for ifm_idx, cons_inp in enumerate(tens_cons.inputs):
176 if cons_inp == tens:
177 tens_cons.set_input_tensor(copy_tens, ifm_idx)
178
179 DebugDatabase.add_optimised(tens.ops[0], copy_op)
180
181
182def fix_sg_input_output_tosa(op, arch, nng):
183 if not op.run_on_npu or op.type != Op.Reshape:
184 return op
185
186 # For the Reshape operators we want to remove, tensors are removed.
187 # But in order to to do this, they cannot be outputs of the sg,
188 # this need to be fixed prior to the removal.
189 # Solution is to add a copy op, to maintain the original tensor.
190 # This is also valid when reshape ifm/ofm is produced respectively
191 # consumed by CPU
192
193 # Check if operator ifm/ofm are sg ifm/ofm
194 ifm_is_sg_ifm = op.ifm.ops[0].type in (Op.Placeholder, Op.SubgraphInput, Op.Const)
195 ifm_is_sg_ofm = any(ifm_cons is None for ifm_cons in op.ifm.consumer_list)
196 ofm_is_sg_ofm = any(ofm_cons is None for ofm_cons in op.ofm.consumer_list)
197 # Check if ifm/ofm is produced repectivly consumed by CPU
198 ifm_is_cpu_produced = any(ifm_prod is not None and not ifm_prod.run_on_npu for ifm_prod in op.ifm.ops)
199 ofm_is_cpu_consumed = any(ofm_cons is not None and not ofm_cons.run_on_npu for ofm_cons in op.ofm.consumer_list)
200
201 if (ifm_is_sg_ofm or ifm_is_sg_ifm or ifm_is_cpu_produced) and (ofm_is_sg_ofm or ofm_is_cpu_consumed):
202 # Both ifm and ofm need to persist, but only ifm need a copy, in order to remove the Reshape
203 insert_add_copy_op_after_tens(op.ifm)
204
205 return op
206
207
208def create_add_for_concat(concat_op, name, ifm, ifm_shape: Shape4D, write_offset: Shape4D):
209 """Creates an add op for the given concat op/input feature map"""
210 ofm = concat_op.ofm
211 ifm2 = create_const_tensor(
212 name + "_zero_scalar", [1], ofm.dtype, [0], ofm.dtype.as_numpy_type(), quantization=ofm.quantization
213 )
214 add_op = create_add_nop(name)
215
216 add_op.inputs = [ifm, ifm2]
217 add_op.outputs = [ofm]
218 add_op.write_offset = write_offset
219 add_op.write_shape = ifm_shape
220 ofm.ops.append(add_op)
221 DebugDatabase.add_optimised(concat_op, add_op)
222 add_op.ifm_shapes.append(ifm_shape)
223 add_op.ifm_shapes.append(Shape4D(ifm2.shape))
224 add_op.ofm_shapes.append(concat_op.ofm_shapes[0])
225 add_op.memory_function = Op.ConcatSliceWrite
226 return add_op
227
228
229# TODO Could be further optimized checking the type of the consumer,
230# rather than just mimic the TFLite behaviour depending on type.
231# TOSA bool_t not considered yet
232def remove_splitsliceread(op, arch):
233
234 if op.type == Op.SplitSliceRead:
235 # Check if it is possible to put the SplitSliceRead on the tensor consumer, or if an avgpool need to be inserted
236 if (
237 len(op.ofm.consumer_list) == 1
238 and op.ofm.consumer_list[0] is not None
239 and op.ofm.consumer_list[0].run_on_npu
240 and op.ofm.consumer_list[0].type != Op.Reshape
241 and op.ofm_shapes[0] == Shape4D.from_list(op.ofm.shape)
242 and op.ofm.dtype in (DataType.uint8, DataType.int8, DataType.int16)
243 ):
244 # SplitSliceRead can be performed by tensor consumer
245 cons_op = op.ofm.consumer_list[0]
246 move_splitsliceread_to_consumer(op, cons_op)
247 else:
248 name = op.name + "_add"
249 ofm = op.ofm
250 ifm2 = create_const_tensor(
251 name + "_zero_scalar", [1], ofm.dtype, [0], ofm.dtype.as_numpy_type(), quantization=ofm.quantization
252 )
253 add_op = create_add_nop(name)
254 add_op.inputs = [op.ifm, ifm2]
255 add_op.outputs = [ofm]
256 op.ofm.ops.remove(op)
257 op.ofm.ops.append(add_op)
258 add_op.ifm_shapes.append(op.ifm_shapes[0])
259 add_op.ifm_shapes.append(Shape4D(ifm2.shape))
260 add_op.ofm_shapes.append(op.ofm_shapes[0])
261 add_op.read_offsets[0] = op.read_offsets[0]
262 add_op.read_shapes[0] = op.read_shapes[0]
263
264 op.ifm.consumer_list.remove(op)
265 DebugDatabase.add_optimised(op, add_op)
266
267
268def rewrite_concat_ops(op, arch):
269 if not op.run_on_npu or not op.type == Op.Concat:
270 return
271
272 axis_4D = 0
273 ofm = op.ofm
274 ofm.ops = []
275 offset = 0
276
277 inputs = op.inputs
278 axis = op.attrs["axis"]
279
280 for idx, inp in enumerate(inputs):
281 op.ifm_shapes[idx] = Shape4D(inp.shape)
282 if axis >= 0:
283 axis_4D = axis + (4 - len(inp.shape))
284 else:
285 axis_4D = axis
286 write_offset = [0, 0, 0, 0]
287 write_offset[axis_4D] = offset
288 concat_end = offset + op.ifm_shapes[idx][axis_4D]
289 create_add_for_concat(op, op.name + str(idx) + "_add", inp, op.ifm_shapes[idx], Shape4D.from_list(write_offset))
290 offset = concat_end
291 assert ofm.shape[axis] == offset
292
293 return op
294
295
Patrik Gustavssondf995102021-08-23 15:33:59 +0200296def remove_reshapes(op, arch):
297 if op.run_on_npu and op.type == Op.Reshape:
298 bypass_reshape_and_squeeze_ops(op)
299
300
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200301def rewrite_activation(op, arch, nng):
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200302 if op.type not in (Op.ReluN, Op.Clamp):
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200303 return op
304
305 ifm = op.ifm
306 prev_op = ifm.ops[0]
307
308 # Note: the below checks on prev_op require that a first optimize pass on the full graph has been performed
309 fuseable = (
310 prev_op.run_on_npu
311 and prev_op.type.npu_block_type != NpuBlockType.Default
312 and len(ifm.ops) == 1
313 and len(prev_op.outputs[0].consumers()) == 1
314 and prev_op.activation is None
315 )
316 if not fuseable:
317 print("Warning: relu like op will not be possible to fuse, currently not supported")
318 assert False
319
320 zp = ifm.quantization.zero_point if ifm.quantization.zero_point else 0
321 if op.ofm.quantization.zero_point is None:
322 op.ofm.quantization.zero_point = zp
323
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200324 if op.type == Op.Clamp:
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200325 op.attrs["min"] = op.attrs["min_int"] - zp
326 op.attrs["max"] = op.attrs["max_int"] - zp
327 elif op.type == Op.ReluN:
328 op.attrs["max"] = op.attrs["max_int"] - zp
329 else:
330 print("Warning: Unknown TOSA activation Op")
331 assert False
332
333 return op
334
335
336def rewrite_rescale(op, arch, nng):
337 if op.type == Op.Rescale:
338 ifm = op.ifm
339 ofm = op.ofm
340
341 # some error checking
342 assert len(ifm.ops) == 1
343 prev_op = ifm.ops[0]
344
345 # TODO currently not supported
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200346 assert len(ifm.consumer_list) == 1
347
348 input_zp = op.attrs["input_zp"]
349 output_zp = op.attrs["output_zp"]
350 multiplier = op.attrs["multiplier"]
351 shift = op.attrs["shift"]
352 scale32 = op.attrs["scale32"]
353 double_round = op.attrs["double_round"]
354 per_channel = op.attrs["per_channel"]
355
356 assert ifm.dtype in (DataType.uint8, DataType.int8, DataType.int32)
357 assert ifm.dtype in (DataType.uint8, DataType.int8) or input_zp == 0
358 assert ofm.dtype in (DataType.uint8, DataType.int8) or output_zp == 0
359 assert (scale32 and ifm.dtype != DataType.int48) or (not scale32 and not double_round)
360
361 # Check that input tensor has the same zp or no zp
362 ifm_zp = ifm.quantization.zero_point
363 if ifm_zp is not None and ifm_zp != input_zp:
364 print("Error (fuse_rescale): zp of tensors producer/consumer differs unexpectedidly ")
365 assert False
366 ifm.quantization.zero_point = input_zp
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200367 ofm.quantization.zero_point = output_zp
368 for s, m in zip(shift, multiplier):
369 # TODO these are the TOSA limitations
370 assert m >= 0
371 assert 2 <= s <= 62
372 # TODO these are the HW limitations
373 assert 0 <= s < (1 << 6)
374 explicit_scaling = ExplicitScaling(per_channel, shift, multiplier)
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200375
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200376 if double_round and scale32:
377 rounding_mode = NpuRoundingMode.TFL
378 else:
379 rounding_mode = NpuRoundingMode.NATURAL
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200380
381 if prev_op.type.is_depthwise_conv2d_op() or prev_op.type.is_conv2d_op() or prev_op.type == Op.FullyConnected:
382 assert len(multiplier) == len(shift) == len(prev_op.bias.values)
383
384 if ifm.dtype == DataType.int32 and per_channel:
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200385 prev_op.explicit_scaling = explicit_scaling
386 prev_op.rounding_mode = rounding_mode
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200387
388 # Bypass op
389 prev_op.set_output_tensor(ofm)
390 DebugDatabase.add_optimised(op, prev_op)
391 return op
392 else:
393 print("Warning, unsupported fusing of TOSA Rescale previous operator is of type:", prev_op.type)
394 assert False
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200395 # TODO which are the cases we need to and can do standalone Rescale?
396 # TODO should we try to identify a conversion uint8<->int8 accomplished by 2 RESCALE ops?
397 # origin might be TFLite op QUANTIZE, should we look to see if they can be translated to QUANTIZE?
398 # limited to these at the moment:
399 elif (
400 (ifm.dtype == DataType.int8 and ofm.dtype == DataType.int8)
401 or (ifm.dtype == DataType.uint8 and ofm.dtype == DataType.int8)
402 or (ifm.dtype == DataType.int8 and ofm.dtype == DataType.uint8)
403 ):
404 # Create NOP performing the RESCALE
405 avgpool_op = replace_rescale_with_avg_pool(op)
406 avgpool_op.rounding_mode = rounding_mode
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200407
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200408 if per_channel:
409 # TODO
410 avgpool_op.explicit_scaling = explicit_scaling
411 print("Warning, unsupported TOSA Rescale")
412 assert False
413 else:
414 avgpool_op.explicit_scaling = explicit_scaling
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200415 else:
416 print("Warning, unsupported fusing of TOSA Rescale previous operator is of type:", prev_op.type)
417 assert False
418 return op
419
420
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200421# TODO modified copy of TFLite, solution for TOSA PAD will change so reuse has not been considered
422def convert_pad(op, arch, nng):
423 """
424 Rewrites PAD operator to an add that copies the IFM to the OFM
425 + up to 4 add operators that fill the OFM with zeros at the borders.
426 """
427
428 if op.type != Op.Pad:
429 return op
430
431 # TODO assuming rank <= 4 and N = 1 for rank ==4
432 # This is checked in tosa_supported_operators
433 ifm = op.ifm
434 assert ifm is not None
435 ifm_shape = Shape4D(ifm.shape)
436 ofm = op.ofm
437 assert ofm is not None
438 ofm.ops = []
439 ofm_shape = op.ofm_shapes[0]
440
441 rank = len(ifm.shape)
442 padding = op.inputs[1].values
443 pad_depth = padding[-1]
444 if not (pad_depth == 0).all():
445 print("Warning: For PAD, padding in depth not supported yet")
446 assert False
447
448 top, bottom = 0, 0
449 left, right = 0, 0
450 if rank > 1:
451 left, right = padding[-2][0], padding[-2][1]
452 if rank > 2:
453 top, bottom = padding[-3][0], padding[-3][1]
454 if rank == 4 and not (padding[-4] == 0).all():
455 print("Warning: For PAD, padding not supported in first dimension when rank == 4 yet")
456 assert False
457
458 # Add op that copies IFM to the right place inside the OFM
459 shp0 = Shape4D(0, 0, 0, 0)
460 shp_top = shp0.with_height(top)
461 add_op = create_add_for_concat(op, op.name + "_main", ifm, ifm_shape, shp_top.with_width(left))
462 add_op.activation = op.activation
463
464 quant = ofm.quantization
465 pad_value = ifm.quantization.zero_point
466 # Add operations that fill the borders of the OFM
467 if top > 0:
468 shape = Shape4D(1, top, ofm_shape.width, ofm_shape.depth)
469 zero_tens = create_const_tensor(
470 op.name + "_top",
471 shape.as_list(),
472 ofm.dtype,
473 shape.elements() * [pad_value],
474 np.uint8,
475 quantization=quant, # TODO
476 )
477 # If top/bottom or left/right are equal, the const tensors can be allocated to the same address
478 zero_tens.equivalence_id = create_equivalence_id(tuple(zero_tens.values))
479 create_add_for_concat(op, op.name + "_top", zero_tens, shape, shp0)
480 if bottom > 0:
481 shape = Shape4D(1, bottom, ofm_shape.width, ofm_shape.depth)
482 zero_tens = create_const_tensor(
483 op.name + "_bottom",
484 shape.as_list(),
485 ofm.dtype,
486 shape.elements() * [pad_value],
487 np.uint8,
488 quantization=quant,
489 )
490 zero_tens.equivalence_id = create_equivalence_id(tuple(zero_tens.values))
491 create_add_for_concat(op, op.name + "_bottom", zero_tens, shape, shp0.with_height(ofm_shape.height - bottom))
492 if left > 0:
493 shape = Shape4D(1, ifm_shape.height, left, ofm_shape.depth)
494 zero_tens = create_const_tensor(
495 op.name + "_left", shape.as_list(), ofm.dtype, shape.elements() * [pad_value], np.uint8, quantization=quant
496 )
497 zero_tens.equivalence_id = create_equivalence_id(tuple(zero_tens.values))
498 create_add_for_concat(op, op.name + "_left", zero_tens, shape, shp_top)
499 if right > 0:
500 shape = Shape4D(1, ifm_shape.height, right, ofm_shape.depth)
501 zero_tens = create_const_tensor(
502 op.name + "_right", shape.as_list(), ofm.dtype, shape.elements() * [pad_value], np.uint8, quantization=quant
503 )
504 zero_tens.equivalence_id = create_equivalence_id(tuple(zero_tens.values))
505 create_add_for_concat(op, op.name + "_right", zero_tens, shape, shp_top.with_width(ofm_shape.width - right))
506
507 op.type = Op.ConcatTFLite
508 return add_op
509
510
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200511def fixup_quantization(op, arch, nng):
512 if op.ifm and op.ifm.quantization.zero_point is None:
513 op.ifm.quantization.zero_point = 0
514 if op.ifm2 and op.ifm2.quantization.zero_point is None:
515 op.ifm.quantization.zero_point = 0
516 if op.ofm and op.ofm.quantization.zero_point is None:
517 op.ofm.quantization.zero_point = 0
518 return op
519
520
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200521def supported_operator_check(op, arch, nng):
522 op.run_on_npu = arch.tosa_supported_operators.is_operator_supported(op)
Patrik Gustavssondf995102021-08-23 15:33:59 +0200523 assert op.run_on_npu or op.type in (Op.Placeholder, Op.SubgraphInput, Op.Const)
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200524 return op
525
526
527def tosa_optimise_graph(nng, arch):
528 # Pre-processing step
529 pre_process_list = [
530 supported_operator_check,
531 set_ifm_ofm_op_shapes,
532 ]
533
534 for idx, sg in enumerate(nng.subgraphs):
535 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
536 nng, sg, arch, [], pre_process_list, rewrite_unsupported=False,
537 )
538
Patrik Gustavssondf995102021-08-23 15:33:59 +0200539 # Removal of Transpose
540 for idx, sg in enumerate(nng.subgraphs):
541 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
542 nng, sg, arch, [], [remove_const_transpose], rewrite_unsupported=False,
543 )
544
545 # Handle sg input output
546 for idx, sg in enumerate(nng.subgraphs):
547 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
Patrik Gustavssonf1580f02021-09-01 12:43:02 +0200548 nng, sg, arch, [], [fix_sg_input_output_tosa], rewrite_unsupported=False,
Patrik Gustavssondf995102021-08-23 15:33:59 +0200549 )
550
Patrik Gustavssonf1580f02021-09-01 12:43:02 +0200551 # Rewrite concat ops
552 for idx, sg in enumerate(nng.subgraphs):
553 rewrite_graph.visit_graph_post_order(sg.output_tensors, arch, [], [rewrite_concat_ops])
554 sg.refresh_after_modification()
555
Patrik Gustavssondf995102021-08-23 15:33:59 +0200556 # Removal of reshapes
557 for sg in nng.subgraphs:
558 rewrite_graph.visit_graph_post_order(sg.output_tensors, arch, [], [remove_reshapes])
559 sg.refresh_after_modification()
560
Patrik Gustavssonf366fb12021-09-07 13:30:29 +0200561 # TODO, when and where to best handle calc_scaling_avgpool
562 for idx, sg in enumerate(nng.subgraphs):
563 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
564 nng, sg, arch, [], [calc_scaling_avgpool], rewrite_unsupported=False,
565 )
566
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200567 # Rewite Operators step
Patrik Gustavssondf995102021-08-23 15:33:59 +0200568 op_rewrite_list = [set_tensor_equivalence, rewrite_rescale, convert_depthwise_to_conv]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200569
570 for idx, sg in enumerate(nng.subgraphs):
571 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
572 nng, sg, arch, [], op_rewrite_list, rewrite_unsupported=False,
573 )
574
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200575 # Post-processing step 1
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200576 for idx, sg in enumerate(nng.subgraphs):
577 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(
Patrik Gustavssone2bfa7e2021-09-08 15:04:11 +0200578 nng, sg, arch, [], [rewrite_activation, convert_pad, add_padding_fields],
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200579 )
580
Patrik Gustavssonf1580f02021-09-01 12:43:02 +0200581 # Removal of Slice, need to be done after optimisation has been performed,
582 # since ifm/ofm_shapes are of importance to this function
583 for sg in nng.subgraphs:
584 rewrite_graph.visit_graph_post_order(sg.output_tensors, arch, [], [remove_splitsliceread])
585 sg.refresh_after_modification()
586
Patrik Gustavssonc74682c2021-08-17 14:26:38 +0200587 # Post-processing step 2
588 for idx, sg in enumerate(nng.subgraphs):
589 nng.subgraphs[idx] = rewrite_graph.rewrite_graph_pre_order(nng, sg, arch, [], [fixup_quantization],)
590
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200591 return nng