Fix the issue in matmul_add_fusion (#2407)

Fix the issue in matmul_add_fusion

If Muatmul + Add has shape [K] * [K, N], reset it to [1, K] * [K, N] will make the output shape to [1, N] will also requires a reshape on the output.
Fix: just remove the shape reset to not fuse it.

Add a negative test case for matmul+add fusion
This commit is contained in:
Hector Li 2019-11-18 10:50:44 -08:00 committed by GitHub
parent aa7c79eac9
commit 367361fc74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View file

@ -64,15 +64,6 @@ Status MatMulAddFusion::ApplyImpl(Graph& graph, bool& modified, int graph_level,
continue;
}
if (1 == matmul_a_shape->dim_size() && 2 == matmul_b_shape->dim_size()) {
// MatMul has shape [K] * [K, N], reset it to [1, K] * [K, N], so that it can work for Gemm
auto mutable_matmul_a_shape = const_cast<ONNX_NAMESPACE::TensorShapeProto*>(matmul_a_shape);
auto dim_0 = mutable_matmul_a_shape->mutable_dim(0);
auto dim_1 = (const_cast<ONNX_NAMESPACE::TensorShapeProto*>(matmul_a_shape))->add_dim();
(*dim_1) = (*dim_0);
dim_0->set_dim_value(1);
}
if (2 != matmul_a_shape->dim_size() || 2 != matmul_b_shape->dim_size()) {
// Gemm only support Matrix
continue;

View file

@ -542,6 +542,27 @@ TEST(GraphTransformationTests, MatMulAddFusion_three_input) {
ASSERT_TRUE(op_to_count["Gemm"] == 1);
}
// Matmul+Add with shape [k]*[k,N]+[N], won't do the fusion
// We can do the fusion by changing shape to [1,k]*[k,N]+[1,N], then add a reshape [1,N]=>[N]
// This will bring extra cost. And there's only very limited gain to fuse Matmul+Add to Gemm
// Since the basic implementation is almost same
TEST(GraphTransformationTests, MatMulAddFusion_negitive_case) {
auto model_uri = MODEL_FOLDER "matmul_add_fusion/3Input/neg_model.onnx";
std::shared_ptr<Model> p_model;
ASSERT_TRUE(Model::Load(model_uri, p_model, nullptr, DefaultLoggingManager().DefaultLogger()).IsOK());
Graph& graph = p_model->MainGraph();
onnxruntime::GraphTransformerManager graph_transformation_mgr{5};
graph_transformation_mgr.Register(onnxruntime::make_unique<MatMulAddFusion>(), TransformerLevel::Level1);
ASSERT_TRUE(graph_transformation_mgr.ApplyTransformers(graph, TransformerLevel::Level1, DefaultLoggingManager().DefaultLogger()).IsOK());
std::map<std::string, int> op_to_count = CountOpsInGraph(graph);
ASSERT_TRUE(op_to_count["MatMul"] == 1);
ASSERT_TRUE(op_to_count["Add"] == 1);
ASSERT_TRUE(op_to_count["Gemm"] == 0);
}
#ifndef DISABLE_CONTRIB_OPS
TEST(GraphTransformationTests, Gemm_Relu_three_input) {
auto model_uri = MODEL_FOLDER "matmul_add_fusion/3Input/gemm_relu.onnx";