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.3 kB

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