You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert.h 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright 2019-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_TRANSFORM_GRAPH_IR_CONVERT_H_
  17. #define MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_CONVERT_H_
  18. #define DRAW_GE_GRAPH
  19. #include <memory>
  20. #include <map>
  21. #include <set>
  22. #include <vector>
  23. #include <unordered_map>
  24. #include <string>
  25. #include <utility>
  26. #include <stack>
  27. #include <fstream>
  28. #include <sstream>
  29. #include "ir/anf.h"
  30. #include "ir/func_graph.h"
  31. #include "transform/graph_ir/util.h"
  32. #include "ir/tensor.h"
  33. #include "transform/graph_ir/df_graph_manager.h"
  34. #include "utils/config_manager.h"
  35. #include "transform/graph_ir/op_adapter.h"
  36. #include "graph/operator_reg.h"
  37. #ifdef OPEN_SOURCE
  38. #include "ge/client/ge_api.h"
  39. #else
  40. #include "external/ge/ge_api.h"
  41. #endif
  42. #include "graph/tensor.h"
  43. #include "ops/hcom_ops.h"
  44. namespace mindspore {
  45. namespace transform {
  46. using TensorOrderMap = std::map<std::string, std::shared_ptr<tensor::Tensor>>;
  47. using HcomBroadcast = ge::op::HcomBroadcast;
  48. class DfGraphConvertor {
  49. public:
  50. explicit DfGraphConvertor(const AnfGraphPtr &anf_graph)
  51. : anf_graph_(anf_graph), df_graph_(std::make_shared<DfGraph>(anf_graph_->ToString())) {
  52. #if (!defined ENABLE_GE) || (defined ENABLE_INFER)
  53. training_ = anf_graph->has_flag("training");
  54. #else
  55. training_ = ENABLE_TRAIN;
  56. #endif
  57. distribute_ = anf_graph->has_flag("broadcast_flag");
  58. if (anf_graph->has_flag("broadcast_flag")) {
  59. ConfigManager::GetInstance().set_parallel_strategy(ParallelStrategy::DISTRIBUTION);
  60. } else {
  61. ConfigManager::GetInstance().set_parallel_strategy(ParallelStrategy::ONE_DEVICE);
  62. }
  63. MS_LOG(INFO) << "Create DfGraphConvertor with training: " << training_ << ", distribute: " << distribute_;
  64. }
  65. ~DfGraphConvertor() {}
  66. static void RegisterAdapter(const std::string &name, OpAdapterPtr adpt);
  67. static void RegisterAdapter(const std::string &name, OpAdapterPtr train_adpt, OpAdapterPtr infer_adpt);
  68. void DrawComputeGraph(const std::string &name) {
  69. std::ofstream fout(name);
  70. if (!fout.is_open()) {
  71. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  72. return;
  73. }
  74. fout << compute_sout_.str();
  75. fout.close();
  76. }
  77. void DrawInitGraph(const std::string &name) {
  78. std::ofstream fout(name);
  79. if (!fout.is_open()) {
  80. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  81. return;
  82. }
  83. fout << init_sout_.str();
  84. fout.close();
  85. }
  86. void DrawSaveCheckpointGraph(const std::string &name) {
  87. std::ofstream fout(name);
  88. if (!fout.is_open()) {
  89. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  90. return;
  91. }
  92. fout << checkpoint_sout_.str();
  93. fout.close();
  94. }
  95. DfGraphConvertor &ConvertAllNode();
  96. DfGraphConvertor &BuildGraph();
  97. DfGraphConvertor &InitParam(const TensorOrderMap &tensors);
  98. DfGraphConvertor &GenerateCheckpointGraph();
  99. DfGraphConvertor &GenerateBroadcastGraph(const TensorOrderMap &tensors);
  100. void InitParamWithData(const TensorOrderMap &tensors);
  101. void SetOpInput(const OpAdapterPtr &adpt, const CNodePtr &node);
  102. void SetupBroadcast(const std::shared_ptr<HcomBroadcast> &broadcast, const std::vector<GeTensorDesc> &broadcast_desc,
  103. const DfGraphPtr &broadcast_graph, std::vector<ge::Operator> broadcast_input);
  104. void MakeDatasetHandler(const std::string &name, const size_t &input_idx, const AnfNodePtr &it);
  105. void SetupParamInitSubGraph(const TensorOrderMap &tensors, std::vector<ge::Operator> *init_input);
  106. void DrawParamInitSubGraph(const std::string &name, const AnfNodePtr &it);
  107. DfGraphPtr GetComputeGraph();
  108. DfGraphPtr GetInitGraph();
  109. DfGraphPtr GetSaveCheckpointGraph();
  110. DfGraphPtr GetBroadcastGraph();
  111. static OpAdapterPtr FindAdapter(const std::string &op_name, bool train = false);
  112. static OpAdapterPtr FindAdapter(AnfNodePtr node, bool train = false);
  113. int ErrCode() const { return static_cast<int>(error_); }
  114. bool is_training() const { return training_; }
  115. void set_training(bool is_training) { training_ = is_training; }
  116. protected:
  117. void InitLoopVar(std::vector<ge::Operator> *init_input);
  118. private:
  119. std::ostringstream compute_sout_;
  120. std::ostringstream init_sout_;
  121. std::ostringstream checkpoint_sout_;
  122. std::ostringstream restore_checkpoint_sout_;
  123. std::unordered_map<AnfNode *, std::string> op_draw_name_;
  124. AnfNodePtr TraceTupleGetItem(const CNodePtr &node, uint64_t *index);
  125. AnfNodePtr TraceMakeTuple(const CNodePtr &node, uint64_t index);
  126. AnfNodePtr TraceDepend(const CNodePtr &node);
  127. OutHandler TraceRealOp(AnfNodePtr node);
  128. OutHandler GetHandler(const AnfNodePtr &node, const std::stack<uint64_t> &index_stack, AnfNode *const draw_index);
  129. OperatorPtr Convert(AnfNodePtr node);
  130. OperatorPtr ConvertCNode(CNodePtr node);
  131. std::vector<OperatorPtr> ConvertDependNode(AnfNodePtr node);
  132. AnfNodePtr GetRealOpNode(AnfNodePtr node);
  133. std::vector<AnfNodePtr> GetDependNodes(const AnfNodePtr &node);
  134. OperatorPtr ConvertParameter(AnfNodePtr node);
  135. Status TryConvertValueNodeToMultiConst(const ValueNodePtr node);
  136. OperatorPtr ConvertValueNode(ValueNodePtr node);
  137. void GetCaseNodeInput(const CNodePtr node, const CNodePtr input_node);
  138. void ConvertTupleGetItem(const CNodePtr node);
  139. void GetDependOnParameterUse(const CNodePtr &node, const AnfNodePtr &src_node, const AnfNodePtr &dest_node,
  140. const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  141. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list);
  142. bool GetControlDependList(const CNodePtr &node, const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  143. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list);
  144. void DrawControlDepend(const AnfNodePtr &src_node, const AnfNodePtr &dest_node);
  145. void ConvertControlDependNode(const CNodePtr node);
  146. void ConvertMakeTuple(const CNodePtr node);
  147. bool CheckCNode(const std::string &name, const CNodePtr node);
  148. void TraceOutput(AnfNodePtr node);
  149. void TraceOutputFromParameter(const AnfNodePtr &anf_out);
  150. void TraceOutputFromTupleGetItem(const AnfNodePtr &anf_out);
  151. void SetNodeInput(AnfNodePtr node);
  152. void SetOpControlInput(const AnfNodePtr &node);
  153. void UpdateOpDesc(AnfNodePtr node);
  154. void SetSubgraph(AnfNodePtr node);
  155. void ProcessSubgraph(AnfNodePtr node, const std::vector<AnfNodePtr> &inputs);
  156. void BuildSaveCheckpointGraph();
  157. void DrawCNode(const CNodePtr node, const OpAdapterPtr adpt);
  158. void UpdateDataOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const;
  159. void AddGraphConstInput(const OperatorPtr &op);
  160. OperatorPtr ToOperatorPtr(const AnfNodePtr &node);
  161. bool IsSourceEdgeNode(const AnfNodePtr &node);
  162. bool IsControlEdgeNode(const AnfNodePtr &node);
  163. void AddEdgeForLoad(const AnfNodePtr &node);
  164. void AddEdgeToCache(const AnfNodePtr &src, const AnfNodePtr &dest);
  165. void FindDestOps(const AnfNodePtr &node, const std::shared_ptr<std::vector<AnfNodePtr>> &node_list, bool top);
  166. AnfNodePtr ParseLoadInput(const CNodePtr &cnode);
  167. void AutoMonadSetControlInput(const AnfNodePtr &node);
  168. void AutoMonadCollectInput(const AnfNodePtr &node);
  169. void AutoMonadSetInput(const AnfNodePtr &node);
  170. void SetTupleOpInput(const OpAdapterPtr &adpt, const CNodePtr &node, const AnfNodePtr &pred, const OperatorPtr &src,
  171. int index);
  172. void UpdateTupleOutCache(void);
  173. std::shared_ptr<AnfGraph> anf_graph_{nullptr};
  174. std::shared_ptr<DfGraph> df_graph_{nullptr};
  175. std::shared_ptr<DfGraph> init_graph_{nullptr};
  176. std::shared_ptr<DfGraph> save_ckp_graph_{nullptr};
  177. std::shared_ptr<DfGraph> restore_ckp_graph_{nullptr};
  178. std::shared_ptr<DfGraph> broadcast_graph_{nullptr};
  179. std::unordered_map<AnfNode *, DfGraph> branches_map_;
  180. std::unordered_map<AnfNode *, OperatorPtr> op_cache_;
  181. std::unordered_map<AnfNode *, std::vector<ControlEdge>> control_depend_cache_;
  182. std::unordered_map<AnfNodePtr, std::set<AnfNodePtr>> monad_control_edge_cache_;
  183. /* record "tuple_getitem"<->"out_handler" mapping */
  184. std::unordered_map<AnfNode *, OutHandler> out_handle_cache_;
  185. /* record "make_tuple"<->"out_handler vector" mapping */
  186. std::unordered_map<AnfNode *, std::shared_ptr<std::vector<OutHandler>>> tuple_out_handle_cache_;
  187. std::unordered_map<AnfNode *, std::shared_ptr<std::vector<AnfNodePtr>>> case_input_handle_cache_;
  188. std::unordered_map<std::string, AnfNodePtr> params_;
  189. std::unordered_map<std::string, OperatorPtr> vars_;
  190. std::vector<std::pair<ge::Operator, std::string>> graph_outputs_;
  191. std::vector<OperatorPtr> graph_const_inputs_;
  192. std::vector<OperatorPtr> init_ops_;
  193. std::vector<OperatorPtr> broadcast_ops_;
  194. std::vector<AnfNodePtr> inputs_;
  195. OperatorPtr dataset_iter_getnext_;
  196. Status error_ = SUCCESS;
  197. bool training_ = false;
  198. bool distribute_ = false;
  199. bool use_inputs_ = false;
  200. };
  201. } // namespace transform
  202. } // namespace mindspore
  203. #endif // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_CONVERT_H_