• 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 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_BUFFER_FUSION_PASS_FUSION_BASE_PASS_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_BUFFER_FUSION_PASS_FUSION_BASE_PASS_H_
18 #include <unordered_map>
19 #include <unordered_set>
20 #include <vector>
21 #include <string>
22 
23 #include "ir/anf.h"
24 #include "backend/optimizer/common/pass.h"
25 #include "backend/optimizer/common/fusion_id_allocator.h"
26 #include "runtime/device/kernel_info.h"
27 #include "backend/kernel_compiler/kernel.h"
28 #include "backend/session/kernel_graph.h"
29 #include "runtime/device/ascend/lic_manager.h"
30 
31 namespace mindspore {
32 namespace opt {
33 const int8_t MAX_ELTWISE_NUM = 3;
34 const int8_t MIN_ELTWISE_SIZE = 2;
35 const int8_t ELTWISE_INPUT_SIZE = 2;
36 const int8_t ELTWISE_DOUBLE_IN_INPUT_SIZE = 3;
37 const int8_t ELTWISE_SINGLE_OUTPUT_SIZE = 1;
38 const int8_t ELTWISE_DOUBLE_OUTPUT_SIZE = 2;
39 const int8_t CONV_DOUBLE_IN_INPUT_SIZE = 3;
40 const int8_t CONV_QUART_IN_INPUT_SIZE = 5;
41 const int8_t ELTWISE_USE = 1;
42 const int8_t ELTWISE_MULTI_USE = 2;
43 const int8_t MAX_ELTWISE_SIZE = 6;
44 const int8_t MULTI_ELTWISE_SIZE = 4;
45 
46 constexpr int64_t kBNTrainingUpdateOutputUsedTotalNum = 5;
47 constexpr int64_t kConvOutputUsedTotalNum = 4;
48 using FusedNodeRecord = std::vector<std::unordered_set<AnfNodePtr>>;
49 
50 struct BufferFusionInfo_t {
51   std::string full_name;
52   std::vector<AnfNodePtr> anf_nodes;
53   std::vector<AnfNodePtr> inputs_list;
54   std::vector<AnfNodePtr> outputs_list;
55   kernel::KernelBuildInfoPtr kernel_build_info;
56 };
57 
58 class FusionBasePass : public Pass {
59  public:
FusionBasePass(const std::string & name,FusionIdAllocatorPtr idAllocator)60   FusionBasePass(const std::string &name, FusionIdAllocatorPtr idAllocator)
61       : Pass(name), fusion_id_allocator(idAllocator) {}
62   ~FusionBasePass() override = default;
63   bool Run(const FuncGraphPtr &graph) override;
64   bool MatchUBFusionPattern(const session::KernelGraph &kernel_graph);
65 
66  protected:
67   virtual void MatchSingleFusionPattern(const session::KernelGraph &kernel_graph,
68                                         FusedNodeRecord *candidate_fusion) = 0;
69   void SetRecordFusionId(const std::unordered_set<AnfNodePtr> &record);
70   bool CheckEltWiseNode(const session::KernelGraph &kernel_graph, const AnfNodePtr &node);
71   bool CheckDoubleInEltWiseNode(const session::KernelGraph &kernel_graph, const AnfNodePtr &node);
72   bool CheckMultiOutputEltWiseNode(const session::KernelGraph &kernel_graph, const AnfNodePtr &node);
73   size_t GetNotUpdateStateUserNums(const session::KernelGraph &kernel_graph, const AnfNodePtr &node);
74   FusionIdAllocatorPtr fusion_id_allocator;
75 };
76 }  // namespace opt
77 }  // namespace mindspore
78 
79 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_BUFFER_FUSION_PASS_FUSION_BASE_PASS_H_
80