• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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 #include "backend/optimizer/ascend/buffer_fusion/multi_output_fusion_pass.h"
17 #include "backend/kernel_compiler/kernel_fusion.h"
18 #include "backend/session/anf_runtime_algorithm.h"
19 #include "base/core_ops.h"
20 #include "utils/ms_context.h"
21 #include "backend/optimizer/common/fusion_id_allocator.h"
22 #include "runtime/device/ascend/lic_manager.h"
23 
24 namespace mindspore {
25 namespace opt {
MatchMultiOutputEltwise(const CNodePtr & cnode,const session::KernelGraph & kernel_graph,FusedNodeRecord * candidate_fusion)26 void MultiOutputFusionPass::MatchMultiOutputEltwise(const CNodePtr &cnode, const session::KernelGraph &kernel_graph,
27                                                     FusedNodeRecord *candidate_fusion) {
28   MS_EXCEPTION_IF_NULL(cnode);
29   MS_EXCEPTION_IF_NULL(candidate_fusion);
30   std::unordered_set<AnfNodePtr> record{cnode};
31   auto eltwise_input = cnode->input(kIndex1);
32   MS_EXCEPTION_IF_NULL(eltwise_input);
33   if (CheckMultiOutputEltWiseNode(kernel_graph, eltwise_input)) {
34     (void)record.insert(eltwise_input);
35     auto input_cnode = eltwise_input->cast<CNodePtr>();
36     MS_EXCEPTION_IF_NULL(input_cnode);
37     eltwise_input = input_cnode->input(kIndex1);
38   } else {
39     return;
40   }
41   while (CheckEltWiseNode(kernel_graph, eltwise_input)) {
42     (void)record.insert(eltwise_input);
43     if (record.size() == MULTI_ELTWISE_SIZE) {
44       break;
45     }
46     auto input_cnode = eltwise_input->cast<CNodePtr>();
47     MS_EXCEPTION_IF_NULL(input_cnode);
48     eltwise_input = input_cnode->input(kIndex1);
49   }
50   if (record.size() != MULTI_ELTWISE_SIZE) {
51     return;
52   }
53   candidate_fusion->push_back(record);
54   SetRecordFusionId(record);
55 }
56 
MatchSingleFusionPattern(const session::KernelGraph & kernel_graph,FusedNodeRecord * candidate_fusion)57 void MultiOutputFusionPass::MatchSingleFusionPattern(const session::KernelGraph &kernel_graph,
58                                                      FusedNodeRecord *candidate_fusion) {
59   MS_EXCEPTION_IF_NULL(candidate_fusion);
60   if (!LicManager::GetInstance().GetPassSwitch(OptPassEnum::MultiOutputFusionPass)) {
61     return;
62   }
63   std::vector<AnfNodePtr> node_list = TopoSort(kernel_graph.get_return());
64   std::reverse(node_list.begin(), node_list.end());
65   for (auto &node : node_list) {
66     if (!AnfAlgo::IsRealCNodeKernel(node) || fusion_id_allocator->HasFusionIdAttr(node) ||
67         AnfAlgo::CheckPrimitiveType(node, prim::kPrimReturn)) {
68       continue;
69     }
70     auto cnode = node->cast<CNodePtr>();
71     MS_EXCEPTION_IF_NULL(cnode);
72     if (AnfAlgo::GetKernelType(cnode) == KernelType::TBE_KERNEL &&
73         AnfAlgo::GetFusionType(cnode) == kernel::FusionType::ELEMWISE && cnode->inputs().size() == ELTWISE_INPUT_SIZE) {
74       MatchMultiOutputEltwise(cnode, kernel_graph, candidate_fusion);
75     }
76   }
77 }
78 }  // namespace opt
79 }  // namespace mindspore
80