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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #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/meta_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. auto it_training = anf_graph->flags().find("training");
  90. if (it_training != anf_graph->flags().end()) {
  91. training_ = it_training->second;
  92. } else {
  93. training_ = false;
  94. }
  95. #else
  96. training_ = ENABLE_TRAIN;
  97. #endif
  98. auto it_distribute = anf_graph->flags().find("broadcast_flag");
  99. if (it_distribute != anf_graph->flags().end()) {
  100. ConfigManager::GetInstance().set_parallel_strategy(ParallelStrategy::DISTRIBUTION);
  101. distribute_ = it_distribute->second;
  102. } else {
  103. ConfigManager::GetInstance().set_parallel_strategy(ParallelStrategy::ONE_DEVICE);
  104. distribute_ = false;
  105. }
  106. MS_LOG(INFO) << "Create DfGraphConvertor with training: " << training_ << ", distribute: " << distribute_;
  107. }
  108. ~DfGraphConvertor() {}
  109. static void RegisterAdapter(const std::string &name, OpAdapterPtr adpt) {
  110. get_adpt_map()[name] = std::make_shared<OpAdapterDesc>(adpt);
  111. }
  112. static void RegisterAdapter(const std::string &name, OpAdapterPtr train_adpt, OpAdapterPtr infer_adpt) {
  113. get_adpt_map()[name] = std::make_shared<OpAdapterDesc>(train_adpt, infer_adpt);
  114. }
  115. void DrawComputeGraph(const std::string &name) {
  116. std::ofstream fout(name);
  117. if (!fout.is_open()) {
  118. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  119. return;
  120. }
  121. fout << compute_sout_.str();
  122. fout.close();
  123. }
  124. void DrawInitGraph(const std::string &name) {
  125. std::ofstream fout(name);
  126. if (!fout.is_open()) {
  127. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  128. return;
  129. }
  130. fout << init_sout_.str();
  131. fout.close();
  132. }
  133. void DrawSaveCheckpointGraph(const std::string &name) {
  134. std::ofstream fout(name);
  135. if (!fout.is_open()) {
  136. MS_LOG(ERROR) << "Open file '" << name << "' failed!";
  137. return;
  138. }
  139. fout << checkpoint_sout_.str();
  140. fout.close();
  141. }
  142. DfGraphConvertor &ConvertAllNode();
  143. DfGraphConvertor &BuildGraph();
  144. DfGraphConvertor &InitParam(const TensorOrderMap &tensors);
  145. DfGraphConvertor &GenerateCheckpointGraph();
  146. DfGraphConvertor &GenerateBroadcastGraph(const TensorOrderMap &tensors);
  147. void InitParamWithData(const TensorOrderMap &tensors);
  148. void SetOpInput(const OpAdapterPtr &adpt, const CNodePtr &node);
  149. void SetupBroadcast(const std::shared_ptr<HcomBroadcast> &broadcast, const std::vector<GeTensorDesc> &broadcast_desc,
  150. const DfGraphPtr &broadcast_graph, std::vector<ge::Operator> broadcast_input);
  151. void MakeDatasetHandler(const std::string &name, const size_t &input_idx, const AnfNodePtr &it);
  152. void SetupParamInitSubGraph(const TensorOrderMap &tensors, std::vector<ge::Operator> *init_input);
  153. void DrawParamInitSubGraph(const std::string &name, const AnfNodePtr &it);
  154. DfGraphPtr GetComputeGraph();
  155. DfGraphPtr GetInitGraph();
  156. DfGraphPtr GetSaveCheckpointGraph();
  157. DfGraphPtr GetBroadcastGraph();
  158. static OpAdapterPtr FindAdapter(const std::string &op_name, bool train = false);
  159. static OpAdapterPtr FindAdapter(AnfNodePtr node, bool train = false);
  160. int ErrCode() const { return static_cast<int>(error_); }
  161. static std::unordered_map<std::string, OpAdapterDescPtr> &get_adpt_map();
  162. bool is_training() const { return training_; }
  163. void set_training(bool is_training) { training_ = is_training; }
  164. protected:
  165. void InitLoopVar(std::vector<ge::Operator> *init_input);
  166. private:
  167. std::ostringstream compute_sout_;
  168. std::ostringstream init_sout_;
  169. std::ostringstream checkpoint_sout_;
  170. std::ostringstream restore_checkpoint_sout_;
  171. std::unordered_map<AnfNode *, std::string> op_draw_name_;
  172. AnfNodePtr TraceTupleGetItem(const CNodePtr &node, unsigned int *index);
  173. AnfNodePtr TraceMakeTuple(const CNodePtr &node, unsigned int index);
  174. AnfNodePtr TraceDepend(const CNodePtr &node);
  175. OutHandler TraceRealOp(AnfNodePtr node);
  176. OutHandler GetHandler(const AnfNodePtr &node, const std::stack<unsigned int> &index_stack, AnfNode *const draw_index);
  177. OperatorPtr Convert(AnfNodePtr node);
  178. OperatorPtr ConvertCNode(CNodePtr node);
  179. std::vector<OperatorPtr> ConvertDependNode(AnfNodePtr node);
  180. AnfNodePtr GetRealOpNode(AnfNodePtr node);
  181. std::vector<AnfNodePtr> GetDependNodes(const AnfNodePtr &node);
  182. OperatorPtr ConvertParameter(AnfNodePtr node);
  183. Status TryConvertValueNodeToMultiConst(const ValueNodePtr node);
  184. OperatorPtr ConvertValueNode(ValueNodePtr node);
  185. void ConvertTupleGetItem(const CNodePtr node);
  186. void GetDependOnParameterUse(const CNodePtr &node, const AnfNodePtr &src_node, const AnfNodePtr &dest_node,
  187. const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  188. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list);
  189. bool GetControlDependList(const CNodePtr &node, const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  190. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list);
  191. void DrawControlDepend(const AnfNodePtr &src_node, const AnfNodePtr &dest_node);
  192. void ConvertControlDependNode(const CNodePtr node);
  193. void ConvertMakeTuple(const CNodePtr node);
  194. bool CheckCNode(const std::string &name, const CNodePtr node);
  195. void TraceOutput(AnfNodePtr node);
  196. void TraceOutputFromParameter(const AnfNodePtr &anf_out);
  197. void TraceOutputFromTupleGetItem(const AnfNodePtr &anf_out);
  198. void SetNodeInput(AnfNodePtr node);
  199. void SetOpControlInput(const AnfNodePtr node);
  200. void UpdateOpDesc(AnfNodePtr node);
  201. void BuildSaveCheckpointGraph();
  202. void DrawCNode(const CNodePtr node, const OpAdapterPtr adpt);
  203. void UpdateDataOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const;
  204. void AddGraphConstInput(const OperatorPtr &op);
  205. std::shared_ptr<AnfGraph> anf_graph_{nullptr};
  206. std::shared_ptr<DfGraph> df_graph_{nullptr};
  207. std::shared_ptr<DfGraph> init_graph_{nullptr};
  208. std::shared_ptr<DfGraph> save_ckp_graph_{nullptr};
  209. std::shared_ptr<DfGraph> restore_ckp_graph_{nullptr};
  210. std::shared_ptr<DfGraph> broadcast_graph_{nullptr};
  211. std::unordered_map<AnfNode *, OperatorPtr> op_cache_;
  212. std::unordered_map<AnfNode *, std::vector<ControlEdge>> control_depend_cache_;
  213. /* record "tuple_getitem"<->"out_handler" mapping */
  214. std::unordered_map<AnfNode *, OutHandler> out_handle_cache_;
  215. /* record "make_tuple"<->"out_handler vector" mapping */
  216. std::unordered_map<AnfNode *, std::shared_ptr<std::vector<OutHandler>>> tuple_out_handle_cache_;
  217. std::unordered_map<std::string, AnfNodePtr> params_;
  218. std::unordered_map<std::string, OperatorPtr> vars_;
  219. std::vector<std::pair<ge::Operator, std::string>> graph_outputs_;
  220. std::vector<OperatorPtr> graph_const_inputs_;
  221. std::vector<OperatorPtr> init_ops_;
  222. std::vector<OperatorPtr> broadcast_ops_;
  223. OperatorPtr dataset_iter_getnext_;
  224. Status error_ = SUCCESS;
  225. bool training_ = false;
  226. bool distribute_ = false;
  227. };
  228. } // namespace transform
  229. } // namespace mindspore
  230. #endif // MINDSPORE_CCSRC_TRANSFORM_CONVERT_H_