• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/train/graph_fusion.h"
18 #include <vector>
19 #include <algorithm>
20 #include <memory>
21 #include "tools/converter/optimizer.h"
22 #include "tools/converter/legacy_optimizer/fusion/matmul_biasadd_fusion_pass.h"
23 #include "src/train/optimizer/fusion/gru_fusion_pass.h"
24 #include "src/train/optimizer/fusion/matmul_activation_fusion_pass.h"
25 #include "src/train/optimizer/fusion/reshape_gather_reshape_fusion_pass.h"
26 #include "src/train/optimizer/fusion/matmul_matmul_add_fusion_pass.h"
27 #include "tools/converter/legacy_optimizer/graph/isolated_node_remove_pass.h"
28 #include "tools/converter/legacy_optimizer/graph/subgraph_node_pass.h"
29 #include "src/train/optimizer/fusion/matmul_add_fusion_pass.h"
30 #include "src/train/optimizer/fusion/remove_redundant_tensor.h"
31 
32 namespace mindspore {
33 namespace lite {
34 namespace {
GetGraphNodes(const schema::MetaGraphT & graph_defT)35 std::vector<schema::CNodeT *> GetGraphNodes(const schema::MetaGraphT &graph_defT) {
36   std::vector<schema::CNodeT *> old_nodes{};
37   old_nodes.resize(graph_defT.nodes.size());
38   std::transform(graph_defT.nodes.begin(), graph_defT.nodes.end(), old_nodes.begin(),
39                  [](const std::unique_ptr<schema::CNodeT> &node) { return node.get(); });
40   return old_nodes;
41 }
42 }  // namespace
Run(schema::MetaGraphT * graph)43 STATUS GraphFusion::Run(schema::MetaGraphT *graph) {
44   if (graph == nullptr) {
45     MS_LOG(ERROR) << "graph is nullptr.";
46     return RET_ERROR;
47   }
48   auto gru_fusion = std::make_shared<GruFusionPass>();
49   MS_CHECK_TRUE_MSG(gru_fusion != nullptr, RET_NULL_PTR, "Create GruFusion object failed.");
50   if (gru_fusion->Run(graph) != RET_OK) {
51     MS_LOG(ERROR) << "Do GruFusion failed.";
52     return RET_ERROR;
53   }
54   auto old_nodes = GetGraphNodes(*graph);
55   Optimizer fusion_optimizer;
56   fusion_optimizer.AddPass(new (std::nothrow) ReshapeGatherReshapeFusionPass());
57   fusion_optimizer.AddPass(new (std::nothrow) MatMulAddFusionPass());
58   fusion_optimizer.AddPass(new (std::nothrow) MatMulBiasAddFusionPass());
59   fusion_optimizer.AddPass(new (std::nothrow) MatMulActivationFusionPass());
60   fusion_optimizer.AddPass(new (std::nothrow) MatMulMatMulAddFusionPass());
61   fusion_optimizer.AddPass(new (std::nothrow) IsolatedNodeRemovePass());
62   fusion_optimizer.AddPass(new (std::nothrow) SubgraphNodePass(old_nodes));
63   auto status = fusion_optimizer.Run(graph);
64   if (status != RET_OK && status != RET_NO_CHANGE) {
65     MS_LOG(ERROR) << "graph fusion failed.";
66     return RET_ERROR;
67   }
68   auto opt_tensor = new (std::nothrow) RemoveRedundantTensor();
69   MS_CHECK_TRUE_MSG(opt_tensor != nullptr, RET_NULL_PTR, "Create RemoveRedundantTensor failed.");
70   if (opt_tensor->Run(graph) != RET_OK) {
71     MS_LOG(ERROR) << "Do RemoveRedundantTensor failed.";
72     return RET_ERROR;
73   }
74   return RET_OK;
75 }
76 }  // namespace lite
77 }  // namespace mindspore
78