diff --git a/src/finn/transformation/streamline/absorb.py b/src/finn/transformation/streamline/absorb.py index f04c6ba9a79457ff23bd84fbb92a37756be86fe8..8398a277443530e84632d26fbfca6d90ea4b0b9e 100644 --- a/src/finn/transformation/streamline/absorb.py +++ b/src/finn/transformation/streamline/absorb.py @@ -360,6 +360,7 @@ class AbsorbTransposeIntoMultiThreshold(Transformation): model = model.transform(InferDataTypes()) return (model, graph_modified) + class AbsorbTransposeIntoFlatten(Transformation): """Absorb transpose node into succeeding flatten node, if H=W=1 and the first dimension stays the same. Can also be applied if flatten is implemented implicitly @@ -419,9 +420,10 @@ class AbsorbTransposeIntoFlatten(Transformation): graph.node.insert(node_ind, node) graph_modified = True if graph_modified: - model = model.transform(InferDataTypes()) + model = model.transform(InferDataTypes()) return (model, graph_modified) - + + class AbsorbScalarMulIntoTopK(Transformation): """Absorb a mul node into a suceeding topk node if the mul is scalar.""" @@ -455,3 +457,84 @@ class AbsorbScalarMulIntoTopK(Transformation): model = model.transform(InferShapes()) model = model.transform(InferDataTypes()) return (model, graph_modified) + + +class AbsorbConsecutiveTransposes(Transformation): + """Remove (Transpose -> Transpose) patterns when the input and output + of the pattern have the same layout.""" + + def Are_opposite_permutations(self, perms1, perms2): + if len(perms1) != len(perms2): + return False + assert 0 <= max(perms2) < len(perms2), "invalid permutation" + assert 0 <= max(perms1) < len(perms1), "invalid permutation" + + for i, p in enumerate(perms2): + if perms1[p] != i: + return False + + return True + + def apply(self, model): + graph = model.graph + graph_modified = False + for n in graph.node: + if n.op_type == "Transpose": + if model.is_fork_node(n): + next_nodes = model.find_direct_successors(n) + perms1 = list(get_by_name(n.attribute, "perm").ints) + + # check if all nodes after fork are opposite transposes + all_opposite_transposes = True + for next_node in next_nodes: + if next_node is not None and next_node.op_type == "Transpose": + perms2 = list(get_by_name(next_node.attribute, "perm").ints) + if not self.Are_opposite_permutations(perms1, perms2): + all_opposite_transposes = False + break + else: + all_opposite_transposes = False + break + + if not all_opposite_transposes: + continue + + prod = model.find_producer(n.input[0]) + for next_node in next_nodes: + # connect next_node's consumer input to n's producer output + # TODO implement this to allow for forks as producers and + # joins as consumers + cons = model.find_consumer(next_node.output[0]) + cons.input[0] = prod.output[0] + + # remove consumer transpose + graph.node.remove(next_node) + + # remove producer transpose + graph.node.remove(n) + graph_modified = True + + else: + next_node = model.find_consumer(n.output[0]) + if next_node is not None and next_node.op_type == "Transpose": + perms1 = list(get_by_name(n.attribute, "perm").ints) + perms2 = list(get_by_name(next_node.attribute, "perm").ints) + if self.Are_opposite_permutations(perms1, perms2): + + # connect next_node's consumer input to n's producer output + # TODO implement this to allow for forks as producers + consumers = model.find_direct_successors(next_node) + prod = model.find_producer(n.input[0]) + for cons in consumers: + for cons_in in cons.input: + if cons_in == next_node.output[0]: + prod.output[0] = cons_in + break + # remove both transposes + graph.node.remove(n) + graph.node.remove(next_node) + + graph_modified = True + if graph_modified: + model = model.transform(InferDataTypes()) + return (model, graph_modified) diff --git a/tests/transformation/test_absorb_opposite_transposes.py b/tests/transformation/test_absorb_opposite_transposes.py new file mode 100644 index 0000000000000000000000000000000000000000..859e691277a261f01b559e2e166763e402c5d689 --- /dev/null +++ b/tests/transformation/test_absorb_opposite_transposes.py @@ -0,0 +1,76 @@ +# Copyright (c) 2020, Xilinx +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# * Neither the name of FINN nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import numpy as np +import onnx.helper as oh +from onnx import TensorProto + +import finn.core.onnx_exec as ox +from finn.core.modelwrapper import ModelWrapper +from finn.transformation.infer_shapes import InferShapes +from finn.transformation.streamline.absorb import AbsorbConsecutiveTransposes + + +def test_absorb_opposite_transposes(): + np.random.seed(0) + input_shape = [1, 3, 4, 2] + top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, input_shape) + top_out = oh.make_tensor_value_info("top_out", TensorProto.FLOAT, input_shape) + value_info = [oh.make_tensor_value_info("add_param_0", TensorProto.FLOAT, [1])] + value_info += [oh.make_tensor_value_info("add_param_1", TensorProto.FLOAT, [1])] + value_info += [oh.make_tensor_value_info("mul_param_0", TensorProto.FLOAT, [1])] + modelproto = oh.make_model( + oh.make_graph( + name="test", + inputs=[top_in], + outputs=[top_out], + value_info=value_info, + nodes=[ + oh.make_node("Add", ["top_in", "add_param_0"], ["t0"]), + oh.make_node("Transpose", ["t0"], ["t1"], perm=[0, 2, 3, 1]), + oh.make_node("Transpose", ["t1"], ["t2"], perm=[0, 3, 1, 2]), + oh.make_node("Add", ["t2", "add_param_1"], ["t3"]), + oh.make_node("Transpose", ["t3"], ["t4"], perm=[0, 2, 3, 1]), + oh.make_node("Transpose", ["t4"], ["t5"], perm=[0, 3, 1, 2]), + oh.make_node("Add", ["t5", "t2"], ["t6"]), + oh.make_node("Mul", ["t6", "mul_param_0"], ["top_out"]), + ], + ) + ) + model = ModelWrapper(modelproto) + model = model.transform(InferShapes()) + model.set_initializer("add_param_0", np.asarray([1], dtype=np.float32)) + model.set_initializer("add_param_1", np.asarray([3], dtype=np.float32)) + model.set_initializer("mul_param_0", np.asarray([2], dtype=np.float32)) + new_model = model.transform(AbsorbConsecutiveTransposes()) + new_model = new_model.transform(InferShapes()) + inp_dict = {"top_in": np.random.rand(*input_shape).astype(np.float32)} + assert ox.compare_execution(model, model, inp_dict) + assert len(new_model.graph.node) == 4 + for n in new_model.graph.node: + assert new_model.graph.node[0].op_type != "Transpose"