From c0218bcc93a98e9381c8e11f199b94a900cfc2d2 Mon Sep 17 00:00:00 2001 From: daquexian Date: Mon, 7 Sep 2020 16:10:03 +0800 Subject: [PATCH 1/2] Fix #3 --- onnxoptimizer/passes/fuse_add_bias_into_conv.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/onnxoptimizer/passes/fuse_add_bias_into_conv.h b/onnxoptimizer/passes/fuse_add_bias_into_conv.h index 749916aa8..011f9a04d 100644 --- a/onnxoptimizer/passes/fuse_add_bias_into_conv.h +++ b/onnxoptimizer/passes/fuse_add_bias_into_conv.h @@ -98,6 +98,12 @@ struct FuseAddBiasIntoConv final : public PredicateBasedPass { squeeze->addInput(conv_3rd_input); conv_3rd_input = squeeze->output(); squeeze->insertBefore(orig_conv->node()); + } else if (bias_shape.size() == 0) { + Node* unsqueeze = graph.create(kUnsqueeze, 1); + unsqueeze->is_(kaxes, {0}); + unsqueeze->addInput(conv_3rd_input); + conv_3rd_input = unsqueeze->output(); + unsqueeze->insertBefore(orig_conv->node()); } if (M > 1) { Node* constant = graph.create(kConstant, 1); From b4ca6ccb80a0b9b2f1d34be93b643cc80f1c43ec Mon Sep 17 00:00:00 2001 From: daquexian Date: Fri, 11 Sep 2020 22:28:11 +0800 Subject: [PATCH 2/2] Add test --- onnxoptimizer/test/optimizer_test.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/onnxoptimizer/test/optimizer_test.py b/onnxoptimizer/test/optimizer_test.py index e77c4751d..d5a7c502f 100644 --- a/onnxoptimizer/test/optimizer_test.py +++ b/onnxoptimizer/test/optimizer_test.py @@ -599,6 +599,28 @@ def test_fuse_transpose_into_gemm(self): # type: () -> None assert len(optimized_model.graph.node[3].attribute[0].g.node) == 1 assert optimized_model.graph.node[3].attribute[0].g.node[0].op_type == "Gemm" + def test_fuse_add_bias_into_conv_with_scalar_bias(self): # type: () -> None + nodes = [helper.make_node("Conv", ["X", "Y"], ["Z"]), + helper.make_node("Add", ["Z", "A"], ["B"])] + graph = helper.make_graph( + nodes, + "test", + [helper.make_tensor_value_info("X", TensorProto.FLOAT, (1, 5, 3, 3)), + helper.make_tensor_value_info( + "Y", TensorProto.FLOAT, (16, 5, 3, 3)), + helper.make_tensor_value_info("A", TensorProto.FLOAT, ())], + [helper.make_tensor_value_info( + "B", TensorProto.FLOAT, (1, 16, 1, 1))], + ) + optimized_model = self._optimized(graph, ["fuse_add_bias_into_conv"]) + + # Unsqueeze, Conv + assert len(optimized_model.graph.node) == 4 + assert optimized_model.graph.node[0].op_type == 'Unsqueeze' + assert optimized_model.graph.node[1].op_type == 'Constant' + assert optimized_model.graph.node[2].op_type == 'Tile' + assert optimized_model.graph.node[3].op_type == 'Conv' + def test_fuse_add_bias_into_conv_use_weight_shape(self): # type: () -> None nodes = [helper.make_node("Conv", ["X", "Y"], ["Z"]), helper.make_node("Add", ["Z", "A"], ["B"])]