From 94457b175b8646bce089c9924e99686587de8992 Mon Sep 17 00:00:00 2001 From: Diqing Zhong Date: Wed, 9 Dec 2020 15:22:40 +0100 Subject: Revert "Revert "MLMBED-3450: Do not convert batched fully connected to conv"" - We have combined estimates for conv and fc, add the fix back Change-Id: I49a29c716189b37b387df4b46efab5f4e6125994 Signed-off-by: Diqing Zhong --- ethosu/vela/graph_optimiser.py | 20 ++--------- ethosu/vela/insert_dma.py | 4 +-- ethosu/vela/test/test_graph_optimiser.py | 61 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 ethosu/vela/test/test_graph_optimiser.py diff --git a/ethosu/vela/graph_optimiser.py b/ethosu/vela/graph_optimiser.py index d0d3d7c8..13f08f26 100644 --- a/ethosu/vela/graph_optimiser.py +++ b/ethosu/vela/graph_optimiser.py @@ -317,7 +317,7 @@ def fixup_fully_connected_input(op, arch, nng): return op -def convert_batched_fc_to_conv(op, arch, nng): +def convert_batched_fc_shape(op, arch, nng): if op.type == Op.FullyConnected: ifm = op.inputs[0] ofm = op.outputs[0] @@ -327,20 +327,6 @@ def convert_batched_fc_to_conv(op, arch, nng): batching_split = {4: (2, 2), 8: (2, 4), 16: (4, 4)} h, w = batching_split.get(n, (1, n)) - # Convert to convolution - op.name += "_conv" - op.type = Op.Conv2DBias - op.attrs = { - "dilation": (1, 1, 1, 1), - "dilation_h_factor": 1, - "dilation_w_factor": 1, - "padding": b"SAME", - "stride_h": 1, - "stride_w": 1, - "strides": (1, 1, 1, 1), - "is_converted_fc": True, - } - prev_op = ifm.ops[0] desired_shape = [1, h, w, ifm.shape[-1]] if len(ifm.consumer_list) == 1 and prev_op is not None and prev_op.type == Op.Reshape: @@ -381,7 +367,7 @@ def convert_batched_fc_to_conv(op, arch, nng): else: op.outputs[0].set_all_shapes(desired_shape) else: - # Add rehape op to the output + # Add reshape op to the output op.set_output_tensor(create_reshape_tensor(ofm, desired_shape, False)) return op @@ -1096,7 +1082,7 @@ def optimise_graph_a(nng, arch, verbose_graph=False): convert_conv_to_fc, convert_softmax, fixup_fully_connected_input, - convert_batched_fc_to_conv, + convert_batched_fc_shape, fixup_pack_input, unfuse_activation_function, fixup_conv2d_backprop, diff --git a/ethosu/vela/insert_dma.py b/ethosu/vela/insert_dma.py index f02039cb..fc1e7986 100644 --- a/ethosu/vela/insert_dma.py +++ b/ethosu/vela/insert_dma.py @@ -77,9 +77,7 @@ def insert_dma_cmd(op, arch, nng): ): only_vector_product_consumers = True for oper in tens.consumers(): - if oper is None or not ( - oper.type.npu_block_type == NpuBlockType.VectorProduct or "is_converted_fc" in oper.attrs - ): + if oper is None or oper.type.npu_block_type != NpuBlockType.VectorProduct: only_vector_product_consumers = False break diff --git a/ethosu/vela/test/test_graph_optimiser.py b/ethosu/vela/test/test_graph_optimiser.py new file mode 100644 index 00000000..62a1b763 --- /dev/null +++ b/ethosu/vela/test/test_graph_optimiser.py @@ -0,0 +1,61 @@ +# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the License); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an AS IS BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Description: +# Unit tests for graph_optimiser +import numpy as np + +from ethosu.vela.graph_optimiser import convert_batched_fc_shape +from ethosu.vela.operation import Op +from ethosu.vela.tensor import create_const_tensor +from ethosu.vela.tensor import Tensor +from ethosu.vela.test import testutil + + +def test_convert_batched_fc(): + """Tests shape conversion of batched fully connected""" + shape = [4, 8] + ifm = create_const_tensor("test_in", shape, np.uint8, np.zeros(shape)) + weights = create_const_tensor("weight_in", shape, np.uint8, np.zeros(shape)) + ofm = Tensor(ifm.shape, np.uint8, "test_out") + op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm) + ifm.consumer_list.append(op) + + prev_op = op.clone() + conv_op = convert_batched_fc_shape(op, None, None) + + assert conv_op.ifm != prev_op.ifm + assert conv_op.ofm != prev_op.ofm + assert conv_op.type == Op.FullyConnected + assert len(conv_op.ifm.shape) == 4 + assert conv_op.ifm.shape == conv_op.ofm.shape + assert conv_op.ifm.ops[0].type == Op.Reshape + + shape = [1, 8] + ifm.shape = shape + weights.shape = shape + ofm.shape = shape + op = testutil.create_op(Op.FullyConnected, [ifm, weights], ofm) + ifm.consumer_list.append(op) + + prev_op = op.clone() + conv_op = convert_batched_fc_shape(op, None, None) + + assert conv_op.ifm == prev_op.ifm + assert conv_op.ofm == prev_op.ofm + assert conv_op.type == Op.FullyConnected + assert len(conv_op.ifm.shape) == 2 + assert conv_op.ifm.shape == conv_op.ofm.shape -- cgit v1.2.1