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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_CONVERT_H_
  17. #define MINDSPORE_CCSRC_TRANSFORM_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/util.h"
  31. #include "ir/tensor.h"
  32. #include "transform/df_graph_manager.h"
  33. #include "utils/config_manager.h"
  34. #include "transform/op_declare.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/all_ops.h"
  43. namespace mindspore {
  44. namespace transform {
  45. class OpAdapterDesc {
  46. public:
  47. OpAdapterDesc() : train_(nullptr), infer_(nullptr) {}
  48. OpAdapterDesc(const OpAdapterPtr &train, const OpAdapterPtr &infer) : train_(train), infer_(infer) {}
  49. explicit OpAdapterDesc(const OpAdapterPtr &common) : train_(common), infer_(common) {}
  50. OpAdapterDesc(const OpAdapterDesc &desc) {
  51. this->train_ = desc.train_;
  52. this->infer_ = desc.infer_;
  53. }
  54. OpAdapterDesc(OpAdapterDesc &&desc) {
  55. this->train_ = desc.train_;
  56. this->infer_ = desc.infer_;
  57. desc.train_ = nullptr;
  58. desc.infer_ = nullptr;
  59. }
  60. ~OpAdapterDesc() = default;
  61. OpAdapterPtr Get(bool train) const { return train ? train_ : infer_; }
  62. OpAdapterDesc &operator=(const OpAdapterDesc &desc) {
  63. if (this != &desc) {
  64. this->train_ = desc.train_;
  65. this->infer_ = desc.infer_;
  66. }
  67. return *this;
  68. }
  69. OpAdapterDesc &operator=(OpAdapterDesc &&desc) {
  70. if (this != &desc) {
  71. this->train_ = desc.train_;
  72. this->infer_ = desc.infer_;
  73. desc.train_ = nullptr;
  74. desc.infer_ = nullptr;
  75. }
  76. return *this;
  77. }
  78. private:
  79. OpAdapterPtr train_;
  80. OpAdapterPtr infer_;
  81. };
  82. using OpAdapterDescPtr = std::shared_ptr<OpAdapterDesc>;
  83. using TensorOrderMap = std::map<std::string, std::shared_ptr<tensor::Tensor>>;
  84. class DfGraphConvertor {
  85. public:
  86. explicit DfGraphConvertor(const AnfGraphPtr &anf_graph)
  87. : anf_graph_(anf_graph), df_graph_(std::make_shared<DfGraph>(anf_graph_->ToString())) {
  88. #if (!defined ENABLE_GE) || (defined ENABLE_INFER)
  89. training_ = anf_graph->has_flag("training");
  90. #else
  91. training_ = ENABLE_TRAIN;
  92. #endif
  93. distribute_ = anf_graph->has_flag("broadcast_flag");
  94. if (anf_graph->has_flag("broadcast_flag")) {
  95. ConfigManager::GetInstance().set_parallel_strategy(ParallelStrategy::DISTRIBUTION);
  96. } else {
  97. ConfigManager::GetInstance().set_parallel_strategy(ParallelStrategy::ONE_DEVICE);
  98. }
  99. MS_LOG(INFO) << "Create DfGraphConvertor with training: " << training_ << ", distribute: " << distribute_;
  100. }
  101. ~DfGraphConvertor() {}
  102. static void RegisterAdapter(const std::string &name, OpAdapterPtr adpt) {
  103. get_adpt_map()[name] = std::make_shared<OpAdapterDesc>(adpt);
  104. }
  105. static void RegisterAdapter(const std::string &name, OpAdapterPtr train_adpt, OpAdapterPtr infer_adpt) {
  106. get_adpt_map()[name] = std::make_shared<OpAdapterDesc>(train_adpt, infer_adpt);
  107. }
  108. void DrawComputeGraph(const std::string &name) {
  109. std::ofstream fout(name);
  110. if (!fout.is_open()) {
  111. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  112. return;
  113. }
  114. fout << compute_sout_.str();
  115. fout.close();
  116. }
  117. void DrawInitGraph(const std::string &name) {
  118. std::ofstream fout(name);
  119. if (!fout.is_open()) {
  120. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  121. return;
  122. }
  123. fout << init_sout_.str();
  124. fout.close();
  125. }
  126. void DrawSaveCheckpointGraph(const std::string &name) {
  127. std::ofstream fout(name);
  128. if (!fout.is_open()) {
  129. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  130. return;
  131. }
  132. fout << checkpoint_sout_.str();
  133. fout.close();
  134. }
  135. DfGraphConvertor &ConvertAllNode();
  136. DfGraphConvertor &BuildGraph();
  137. DfGraphConvertor &InitParam(const TensorOrderMap &tensors);
  138. DfGraphConvertor &GenerateCheckpointGraph();
  139. DfGraphConvertor &GenerateBroadcastGraph(const TensorOrderMap &tensors);
  140. void InitParamWithData(const TensorOrderMap &tensors);
  141. void SetOpInput(const OpAdapterPtr &adpt, const CNodePtr &node);
  142. void SetupBroadcast(const std::shared_ptr<HcomBroadcast> &broadcast, const std::vector<GeTensorDesc> &broadcast_desc,
  143. const DfGraphPtr &broadcast_graph, std::vector<ge::Operator> broadcast_input);
  144. void MakeDatasetHandler(const std::string &name, const size_t &input_idx, const AnfNodePtr &it);
  145. void SetupParamInitSubGraph(const TensorOrderMap &tensors, std::vector<ge::Operator> *init_input);
  146. void DrawParamInitSubGraph(const std::string &name, const AnfNodePtr &it);
  147. DfGraphPtr GetComputeGraph();
  148. DfGraphPtr GetInitGraph();
  149. DfGraphPtr GetSaveCheckpointGraph();
  150. DfGraphPtr GetBroadcastGraph();
  151. static OpAdapterPtr FindAdapter(const std::string &op_name, bool train = false);
  152. static OpAdapterPtr FindAdapter(AnfNodePtr node, bool train = false);
  153. int ErrCode() const { return static_cast<int>(error_); }
  154. static std::unordered_map<std::string, OpAdapterDescPtr> &get_adpt_map();
  155. bool is_training() const { return training_; }
  156. void set_training(bool is_training) { training_ = is_training; }
  157. protected:
  158. void InitLoopVar(std::vector<ge::Operator> *init_input);
  159. private:
  160. std::ostringstream compute_sout_;
  161. std::ostringstream init_sout_;
  162. std::ostringstream checkpoint_sout_;
  163. std::ostringstream restore_checkpoint_sout_;
  164. std::unordered_map<AnfNode *, std::string> op_draw_name_;
  165. AnfNodePtr TraceTupleGetItem(const CNodePtr &node, unsigned int *index);
  166. AnfNodePtr TraceMakeTuple(const CNodePtr &node, unsigned int index);
  167. AnfNodePtr TraceDepend(const CNodePtr &node);
  168. OutHandler TraceRealOp(AnfNodePtr node);
  169. OutHandler GetHandler(const AnfNodePtr &node, const std::stack<unsigned int> &index_stack, AnfNode *const draw_index);
  170. OperatorPtr Convert(AnfNodePtr node);
  171. OperatorPtr ConvertCNode(CNodePtr node);
  172. std::vector<OperatorPtr> ConvertDependNode(AnfNodePtr node);
  173. AnfNodePtr GetRealOpNode(AnfNodePtr node);
  174. std::vector<AnfNodePtr> GetDependNodes(const AnfNodePtr &node);
  175. OperatorPtr ConvertParameter(AnfNodePtr node);
  176. Status TryConvertValueNodeToMultiConst(const ValueNodePtr node);
  177. OperatorPtr ConvertValueNode(ValueNodePtr node);
  178. void ConvertTupleGetItem(const CNodePtr node);
  179. void GetDependOnParameterUse(const CNodePtr &node, const AnfNodePtr &src_node, const AnfNodePtr &dest_node,
  180. const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  181. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list);
  182. bool GetControlDependList(const CNodePtr &node, const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  183. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list);
  184. void DrawControlDepend(const AnfNodePtr &src_node, const AnfNodePtr &dest_node);
  185. void ConvertControlDependNode(const CNodePtr node);
  186. void ConvertMakeTuple(const CNodePtr node);
  187. bool CheckCNode(const std::string &name, const CNodePtr node);
  188. void TraceOutput(AnfNodePtr node);
  189. void TraceOutputFromParameter(const AnfNodePtr &anf_out);
  190. void TraceOutputFromTupleGetItem(const AnfNodePtr &anf_out);
  191. void SetNodeInput(AnfNodePtr node);
  192. void SetOpControlInput(const AnfNodePtr node);
  193. void UpdateOpDesc(AnfNodePtr node);
  194. void BuildSaveCheckpointGraph();
  195. void DrawCNode(const CNodePtr node, const OpAdapterPtr adpt);
  196. void UpdateDataOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const;
  197. void AddGraphConstInput(const OperatorPtr &op);
  198. std::shared_ptr<AnfGraph> anf_graph_{nullptr};
  199. std::shared_ptr<DfGraph> df_graph_{nullptr};
  200. std::shared_ptr<DfGraph> init_graph_{nullptr};
  201. std::shared_ptr<DfGraph> save_ckp_graph_{nullptr};
  202. std::shared_ptr<DfGraph> restore_ckp_graph_{nullptr};
  203. std::shared_ptr<DfGraph> broadcast_graph_{nullptr};
  204. std::unordered_map<AnfNode *, OperatorPtr> op_cache_;
  205. std::unordered_map<AnfNode *, std::vector<ControlEdge>> control_depend_cache_;
  206. /* record "tuple_getitem"<->"out_handler" mapping */
  207. std::unordered_map<AnfNode *, OutHandler> out_handle_cache_;
  208. /* record "make_tuple"<->"out_handler vector" mapping */
  209. std::unordered_map<AnfNode *, std::shared_ptr<std::vector<OutHandler>>> tuple_out_handle_cache_;
  210. std::unordered_map<std::string, AnfNodePtr> params_;
  211. std::unordered_map<std::string, OperatorPtr> vars_;
  212. std::vector<std::pair<ge::Operator, std::string>> graph_outputs_;
  213. std::vector<OperatorPtr> graph_const_inputs_;
  214. std::vector<OperatorPtr> init_ops_;
  215. std::vector<OperatorPtr> broadcast_ops_;
  216. OperatorPtr dataset_iter_getnext_;
  217. Status error_ = SUCCESS;
  218. bool training_ = false;
  219. bool distribute_ = false;
  220. };
  221. } // namespace transform
  222. } // namespace mindspore
  223. #endif // MINDSPORE_CCSRC_TRANSFORM_CONVERT_H_