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 17 #ifndef MINDSPORE_CCSRC_FRONTEND_PARALLEL_ALLREDUCE_FUSION_ALLREDUCE_FUSION_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_ALLREDUCE_FUSION_ALLREDUCE_FUSION_H_ 19 20 #include <unordered_map> 21 #include <vector> 22 #include "ir/anf.h" 23 #include "frontend/parallel/allreduce_fusion/allreduce_graph.h" 24 #include "frontend/parallel/status.h" 25 #include "frontend/parallel/ops_info/ops_utils.h" 26 #include "frontend/parallel/step_parallel_utils.h" 27 28 namespace mindspore { 29 namespace parallel { 30 using CNodeCostMap = std::unordered_map<CNodePtr, double>; 31 32 constexpr int64_t DEFAULT_COST_MODEL_ALLREDUCE_FUSION_ALGORITHM = 0; 33 constexpr int64_t DEFAULT_COST_MODEL_ALLREDUCE_FUSION_TIMES = 0; 34 constexpr double DEFAULT_COST_MODEL_ALLREDUCE_FUSION_TAIL_PERCENT = 0.1; 35 constexpr double DEFAULT_COST_MODEL_ALLREDUCE_FUSION_TAIL_TIME = 0.1; 36 constexpr double DEFAULT_COST_MODEL_ALLREDUCE_FUSION_ALLREDUCE_INHERENT_TIME = 0.1; 37 constexpr double DEFAULT_COST_MODEL_ALLREDUCE_FUSION_ALLREDUCE_BANDWIDTH = 0.1; 38 constexpr double DEFAULT_COST_MODEL_ALLREDUCE_FUSION_COMPUTATION_TIME_PARAMETER = 0.1; 39 40 const uint64_t MAX_RECURSIVE_CALL_TIMES = 100; 41 class AllreduceFusion { 42 public: AllreduceFusion()43 AllreduceFusion() 44 : allreduce_graph_(), 45 ret_(nullptr), 46 forward_ret_(nullptr), 47 root_graph_(nullptr), 48 tail_time_(0), 49 allreduce_inherent_time_(0), 50 allreduce_bandwidth_(0), 51 computation_time_parameter_(0) {} 52 virtual ~AllreduceFusion() = default; 53 Status ProcessAllreduceFusion(const CNodePtr &ret); 54 55 private: 56 Status AddNodeToGraph(); 57 CNodeCostMap FindCNode(const AnfNodePtr &from, uint64_t recursive_times = 0) const; 58 CNodeCostMap FindNextCNodes(const CNodePtr &from, uint64_t recursive_times = 0) const; 59 Status AddEdgeToGraph(); 60 std::vector<double> GenerateCostMap(int64_t fusion_times, double tail_percent) const; 61 Status SetFusion(const std::vector<double> &cost_map); 62 Status SetFusionByAlgorithm(int64_t algorithm); 63 Status SetFusionByBackwardCompTime(); 64 Status SetFusionByBackwardCompAndAllreduceTime(); 65 Status GetSetFusionByBackwardCompAndAllreduceTimeParams(); 66 67 AllreduceGraph allreduce_graph_; 68 CNodePtr ret_; 69 CNodePtr forward_ret_; 70 FuncGraphPtr root_graph_; 71 double tail_time_; 72 double allreduce_inherent_time_; 73 double allreduce_bandwidth_; 74 double computation_time_parameter_; 75 }; 76 } // namespace parallel 77 } // namespace mindspore 78 79 #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_ALLREDUCE_FUSION_ALLREDUCE_FUSION_H_ 80