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 8.6 kB

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