Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #3 #4

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions onnxoptimizer/passes/fuse_add_bias_into_conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions onnxoptimizer/test/optimizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])]
Expand Down