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/conv2dbackprop_eltwise_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
23 namespace mindspore {
24 namespace opt {
MatchConv2DBackpropInputEltwise(const CNodePtr & cnode,const session::KernelGraph &,FusedNodeRecord * candidate_fusion)25 void Conv2DBackpropEltwiseFusionPass::MatchConv2DBackpropInputEltwise(const CNodePtr &cnode,
26 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 (!eltwise_input->isa<CNode>() || !AnfAlgo::IsRealCNodeKernel(eltwise_input) ||
34 fusion_id_allocator->HasFusionIdAttr(eltwise_input)) {
35 return;
36 }
37 if (AnfAlgo::CheckPrimitiveType(eltwise_input, prim::kPrimConv2DBackpropInput)) {
38 (void)record.insert(eltwise_input);
39 candidate_fusion->push_back(record);
40 SetRecordFusionId(record);
41 }
42 }
43
MatchSingleFusionPattern(const session::KernelGraph & kernel_graph,FusedNodeRecord * candidate_fusion)44 void Conv2DBackpropEltwiseFusionPass::MatchSingleFusionPattern(const session::KernelGraph &kernel_graph,
45 FusedNodeRecord *candidate_fusion) {
46 if (!LicManager::GetInstance().GetPassSwitch(OptPassEnum::Conv2DBackpropEltwiseFusionPass)) {
47 return;
48 }
49 MS_EXCEPTION_IF_NULL(candidate_fusion);
50 std::vector<AnfNodePtr> node_list = TopoSort(kernel_graph.get_return());
51 for (auto &node : node_list) {
52 if (!AnfAlgo::IsRealCNodeKernel(node) || fusion_id_allocator->HasFusionIdAttr(node) ||
53 AnfAlgo::CheckPrimitiveType(node, prim::kPrimReturn)) {
54 continue;
55 }
56 auto cnode = node->cast<CNodePtr>();
57 MS_EXCEPTION_IF_NULL(cnode);
58 if (AnfAlgo::GetCNodeName(cnode) == kReluGradV2OpName) {
59 MatchConv2DBackpropInputEltwise(cnode, kernel_graph, candidate_fusion);
60 }
61 }
62 }
63 } // namespace opt
64 } // namespace mindspore
65