1 /** 2 * Copyright 2019 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_STEP_PARALLEL_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_STEP_PARALLEL_H_ 19 20 #include <vector> 21 22 #include <map> 23 #include <memory> 24 #include <set> 25 #include <string> 26 #include <unordered_map> 27 #include <utility> 28 29 #include "frontend/optimizer/opt.h" 30 #include "frontend/parallel/strategy.h" 31 #include "frontend/parallel/tensor_layout/tensor_redistribution.h" 32 #include "pipeline/jit/pipeline.h" 33 #include "frontend/parallel/ops_info/ops_utils.h" 34 #include "frontend/parallel/auto_parallel/operator_costmodel.h" 35 #include "frontend/parallel/strategy_checkpoint/parallel_strategy_checkpoint.h" 36 37 using OperatorInfoPtr = std::shared_ptr<mindspore::parallel::OperatorInfo>; 38 39 namespace mindspore { 40 namespace parallel { 41 const uint64_t kUSecondInSecond = 1000000; 42 const int32_t RECURSION_LIMIT = 3; 43 44 struct LossNodeInfo { 45 bool has_tuple_getitem = false; 46 int64_t dout_index = 0; // now don't support the sens is a tuple 47 CNodePtr loss_node = nullptr; 48 }; 49 50 struct CommInfo { 51 int64_t device_num = 1; 52 int64_t global_rank = 0; 53 std::string world_group; 54 std::string communication_backend; 55 }; 56 57 std::vector<AnfNodePtr> CreateInput(const Operator &op, const AnfNodePtr &node, const std::string &instance_name); 58 void ForwardCommunication(OperatorVector forward_op, const CNodePtr &node); 59 60 void InsertRedistribution(const RedistributionOpListPtr &redistribution_oplist_ptr, const CNodePtr &node, 61 const FuncGraphPtr &func_graph, int64_t pos, const CNodePtr &pre_node); 62 63 TensorLayout GetTensorInLayout(const CNodePtr &pre_node, const PrimitivePtr &pre_prim, 64 const OperatorInfoPtr &distribute_operator_pre); 65 66 OperatorInfoPtr GetDistributeOperator(const CNodePtr &node); 67 68 void Redistribution(const std::pair<AnfNodePtr, int64_t> &node_pair, const OperatorInfoPtr &distribute_operator, 69 const CNodePtr &middle_node, int64_t index, TensorRedistribution tensor_redistribution, 70 const CNodePtr &pre_node); 71 72 bool StrategyFound(std::unordered_map<std::string, ValuePtr> attrs); 73 74 void MarkForwardCNode(const FuncGraphPtr &root); 75 76 bool FindCommunicationOp(const std::vector<AnfNodePtr> &all_nodes); 77 78 void StepRedistribution(const CNodePtr &node, const OperatorInfoPtr &distribute_operator, const CNodePtr &insert_node, 79 const TensorRedistribution &tensor_redistribution, const CNodePtr &pre_node); 80 81 void StepReplaceOp(OperatorVector replace_op, const CNodePtr &node); 82 83 void InsertVirtualDivOp(const VirtualDivOp &virtual_div_op, const CNodePtr &node); 84 85 std::pair<AnfNodePtr, bool> FindParameter(const AnfNodePtr &node, const FuncGraphPtr &func_graph); 86 87 std::pair<bool, CNodePtr> FindCNode(const AnfNodePtr &anode, const std::string &name, const FuncGraphPtr &func_graph, 88 size_t max_depth); 89 90 // Generate and init parallel operator 91 OperatorInfoPtr OperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs, 92 const std::vector<Shapes> &shape_list); 93 94 // Generate without initing parallel operator 95 OperatorInfoPtr NewOperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs, 96 std::vector<Shapes> shape_list); 97 98 // Extract strategy from attr 99 StrategyPtr ExtractStrategy(const ValuePtr &strategy); 100 101 // Extract shape from anfnode 102 std::vector<Shapes> ExtractShape(const CNodePtr &node); 103 104 // Find finally sub graph 105 std::pair<AnfNodePtr, int64_t> FindSubGraph(const FuncGraphPtr &func_graph, const AnfNodePtr ¶meter); 106 107 // Set distribute shape for parameters abstract 108 std::string SetParallelShape(const AnfNodePtr ¶meter, const std::pair<AnfNodePtr, int64_t> &res); 109 110 // change parameters'shape in resource 111 void CoverSliceShape(const FuncGraphPtr &root); 112 113 void SetVirtualDatasetStrategy(const CNodePtr &node); 114 bool IsInsertVirtualOutput(const FuncGraphPtr &root); 115 // Create parallel operator for primitive node(has strategy) 116 void ExtractInformation(const std::vector<AnfNodePtr> &all_nodes); 117 118 TensorLayout GetInputLayoutFromCNode(const std::pair<AnfNodePtr, int64_t> &node_pair); 119 120 std::shared_ptr<TensorLayout> FindNextLayout(const CNodePtr &node); 121 122 std::shared_ptr<TensorLayout> GetOutputLayoutFromCNode(const CNodePtr &cnode, size_t output_index); 123 124 std::shared_ptr<TensorLayout> FindPrevParallelCareNodeLayout(const AnfNodePtr &node, size_t output_index); 125 126 std::shared_ptr<TensorLayout> FindPrevLayout(const AnfNodePtr &node); 127 128 void ReshapeInit(const std::vector<AnfNodePtr> &all_nodes); 129 130 StrategyPtr GenerateBatchParallelStrategy(const OperatorInfoPtr operator_, const PrimitivePtr prim); 131 132 // Add node for whole graph 133 void ParallelCommunication(const FuncGraphPtr &root, const std::vector<AnfNodePtr> &all_nodes, 134 const FuncGraphManagerPtr &manager); 135 136 ParameterMap NodeParameterName(const CNodePtr &node, int64_t index, size_t curr_depth); 137 138 void CheckpointStrategy(const std::vector<AnfNodePtr> &all_nodes, const FuncGraphPtr &root); 139 140 // main step of Parallel 141 bool StepParallel(const FuncGraphPtr &func_graph, const opt::OptimizerPtr &optimizer); 142 143 int64_t GetTupleGetItemIndex(const CNodePtr &cnode); 144 145 Status ParallelInit(); 146 147 std::set<FuncGraphPtr> ForwardGraph(const FuncGraphPtr &root); 148 149 std::vector<std::string> ExtractInputsTensorName(const CNodePtr &node); 150 151 std::shared_ptr<TensorLayout> FindParameterNextLayout(const AnfNodePtr &node); 152 153 bool IsUsedParameter(const FuncGraphPtr &graph, const AnfNodePtr ¶meter); 154 155 void ApplyParallelOptOnParam(TensorLayout *tensor_layout, const OperatorInfoPtr &distribute_operator, 156 const CNodePtr &cnode, const AnfNodePtr ¶meter, size_t index); 157 158 void SetLastNodeStrategy(const StrategyPtr strategyPtr); 159 160 bool CreateGroupsByCkptFile(const std::string &file); 161 162 void FindLastNodesUniqueId(const FuncGraphPtr &root, std::vector<std::string> *unique_ids, 163 std::vector<size_t> *indexes); 164 165 void InsertVirtualOutput(const FuncGraphPtr &root, const std::vector<AnfNodePtr> &all_nodes); 166 167 std::string MirrorOpName(); 168 169 CommInfo GetCommInfo(); 170 171 std::string GetPrimName(const CNodePtr &node); 172 173 void ReorderForPipelineSplit(const FuncGraphPtr &root, const FuncGraphManagerPtr &manager, int64_t pipeline_stages); 174 } // namespace parallel 175 } // namespace mindspore 176 177 #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_STEP_PARALLEL_H_ 178