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

6 years ago
6 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  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. #include "transform/graph_ir/convert.h"
  17. #include <inttypes.h>
  18. #include <algorithm>
  19. #include <stack>
  20. #include "utils/utils.h"
  21. #include "base/core_ops.h"
  22. #include "frontend/operator/ops.h"
  23. #include "utils/log_adapter.h"
  24. #include "ir/graph_utils.h"
  25. #include "utils/symbolic.h"
  26. #include "utils/config_manager.h"
  27. #include "utils/convert_utils.h"
  28. #include "utils/ms_context.h"
  29. #include "transform/graph_ir/op_adapter_map.h"
  30. #include "ops/state_ops.h"
  31. #include "ops/array_ops.h"
  32. #include "ops/elewise_calculation_ops.h"
  33. #include "ops/math_ops.h"
  34. #ifdef ENABLE_GE
  35. #include "ops/save_ops.h"
  36. #endif
  37. namespace mindspore {
  38. namespace transform {
  39. using std::endl;
  40. using ge::Operator;
  41. using mindspore::kAnyValue;
  42. using std::make_shared;
  43. using std::shared_ptr;
  44. using std::string;
  45. using std::vector;
  46. using Variable = ge::op::Variable;
  47. using Constant = ge::op::Constant;
  48. using Assign = ge::op::Assign;
  49. using Data = ge::op::Data;
  50. namespace {
  51. std::vector<AnfNodePtr> GetOrderedCNodes(const FuncGraphPtr fg) {
  52. auto BelongSameGraph = std::bind(IncludeBelongGraph, fg, std::placeholders::_1);
  53. auto succ_include_fv = [&fg](const AnfNodePtr &node) -> std::vector<AnfNodePtr> {
  54. std::vector<AnfNodePtr> vecs;
  55. if (node == nullptr) {
  56. return vecs;
  57. }
  58. if (node->isa<CNode>()) {
  59. auto cnode = node->cast<CNodePtr>();
  60. auto &inputs = cnode->inputs();
  61. // Check if free variables used.
  62. for (const auto &input : inputs) {
  63. auto input_fg = GetValueNode<FuncGraphPtr>(input);
  64. if (input_fg) {
  65. for (auto &fv : input_fg->free_variables_nodes()) {
  66. if (fv->func_graph() == fg && fg->nodes().contains(fv)) {
  67. vecs.push_back(fv);
  68. }
  69. }
  70. }
  71. }
  72. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  73. }
  74. return vecs;
  75. };
  76. return TopoSort(fg->get_return(), succ_include_fv, BelongSameGraph);
  77. }
  78. } // namespace
  79. // ---------------implement of DfGraphConvertor-------------
  80. PrimType GetCNodeFuncType(const CNodePtr cnode) {
  81. if (cnode->inputs().empty()) {
  82. return kPrimTypeUnknown;
  83. }
  84. AnfNodePtr valuenode = cnode->input(0);
  85. if (IsValueNode<Primitive>(valuenode)) {
  86. // check whether the valuenode is primitive
  87. return GetValueNode<PrimitivePtr>(valuenode)->prim_type();
  88. }
  89. return kPrimTypeUnknown;
  90. }
  91. bool IsCaseNode(const CNodePtr node) {
  92. if (!node->inputs().empty() && node->input(0)->isa<CNode>() &&
  93. GetCNodeFuncName(node->input(0)->cast<CNodePtr>()) == "switch_layer") {
  94. return true;
  95. }
  96. return false;
  97. }
  98. std::string GetCNodeTargetFuncName(const CNodePtr cnode) {
  99. if (IsCaseNode(cnode)) {
  100. return string(kNameCase);
  101. }
  102. auto name = GetCNodeFuncName(cnode);
  103. if (name == "switch_layer") {
  104. name = "";
  105. }
  106. return name;
  107. }
  108. OpAdapterPtr DfGraphConvertor::FindAdapter(const AnfNodePtr node, bool train) {
  109. if (node->isa<CNode>()) {
  110. auto cnode = node->cast<CNodePtr>();
  111. std::string name = kNameCustomOp;
  112. if (!IsCustomCNode(cnode)) {
  113. name = GetCNodeTargetFuncName(cnode);
  114. }
  115. auto it_adpt = OpAdapterMap::get().find(name);
  116. if (it_adpt != OpAdapterMap::get().end()) {
  117. return it_adpt->second->Get(train);
  118. }
  119. MS_LOG(EXCEPTION) << "Can't find OpAdapter for " << name;
  120. }
  121. if (node->isa<ValueNode>()) {
  122. return OpAdapterMap::get()[kNameConst]->Get(train);
  123. }
  124. if (node->isa<Parameter>()) {
  125. return OpAdapterMap::get()[kNameParam]->Get(train);
  126. }
  127. return OpAdapterPtr(nullptr);
  128. }
  129. void DfGraphConvertor::InitLoopVar(std::vector<ge::Operator> *init_input) {
  130. if (this->training_) {
  131. GeTensorDesc desc(GeShape(), ge::FORMAT_NCHW, ge::DT_INT64);
  132. auto var_iter_num = std::make_shared<Variable>("npu_runconfig/iterations_per_loop");
  133. auto var_loop_cond = std::make_shared<Variable>("npu_runconfig/loop_cond");
  134. auto var_one = std::make_shared<Variable>("npu_runconfig/one");
  135. auto var_zero = std::make_shared<Variable>("npu_runconfig/zero");
  136. (void)var_iter_num->update_output_desc_y(desc);
  137. (void)var_loop_cond->update_output_desc_y(desc);
  138. (void)var_one->update_output_desc_y(desc);
  139. (void)var_zero->update_output_desc_y(desc);
  140. vars_["npu_runconfig/iterations_per_loop"] = var_iter_num;
  141. vars_["npu_runconfig/loop_cond"] = var_loop_cond;
  142. vars_["npu_runconfig/one"] = var_one;
  143. vars_["npu_runconfig/zero"] = var_zero;
  144. int64_t value = 0;
  145. auto const_iter_num = std::make_shared<Constant>("const/npu_runconfig/iterations_per_loop");
  146. if (ConfigManager::GetInstance().dataset_mode() == DS_SINK_MODE) {
  147. value = ConfigManager::GetInstance().iter_num();
  148. } else {
  149. MS_LOG(INFO) << "Run with normal(non-sink) mode, the iterator number will always be 1";
  150. value = 1;
  151. ConfigManager::GetInstance().set_iter_num(value);
  152. }
  153. value -= 1; // iteration start from 0, the max iteration number for n loop should be n-1
  154. (void)const_iter_num->set_attr_value(GeTensor(desc, reinterpret_cast<uint8_t *>(&value), sizeof(int64_t)));
  155. auto const_loop_cond = std::make_shared<Constant>("const/npu_runconfig/loop_cond");
  156. value = 0;
  157. (void)const_loop_cond->set_attr_value(GeTensor(desc, reinterpret_cast<uint8_t *>(&value), sizeof(int64_t)));
  158. auto const_one = std::make_shared<Constant>("const/npu_runconfig/one");
  159. value = 1;
  160. (void)const_one->set_attr_value(GeTensor(desc, reinterpret_cast<uint8_t *>(&value), sizeof(int64_t)));
  161. auto const_zero = std::make_shared<Constant>("const/npu_runconfig/zero");
  162. value = 0;
  163. (void)const_zero->set_attr_value(GeTensor(desc, reinterpret_cast<uint8_t *>(&value), sizeof(int64_t)));
  164. (void)const_iter_num->update_output_desc_y(desc);
  165. (void)const_loop_cond->update_output_desc_y(desc);
  166. (void)const_one->update_output_desc_y(desc);
  167. (void)const_zero->update_output_desc_y(desc);
  168. auto assign_iter_num = std::make_shared<Assign>("assign/npu_runconfig/iterations_per_loop");
  169. (void)assign_iter_num->set_input_ref(*var_iter_num).set_input_value(*const_iter_num);
  170. auto assign_loop_cond = std::make_shared<Assign>("assign/npu_runconfig/loop_cond");
  171. (void)assign_loop_cond->set_input_ref(*var_loop_cond).set_input_value(*const_loop_cond);
  172. auto assign_one = std::make_shared<Assign>("assign/npu_runconfig/one");
  173. (void)assign_one->set_input_ref(*var_one).set_input_value(*const_one);
  174. auto assign_zero = std::make_shared<Assign>("assign/npu_runconfig/zero");
  175. (void)assign_zero->set_input_ref(*var_zero).set_input_value(*const_zero);
  176. init_input->push_back(*var_iter_num);
  177. init_input->push_back(*var_loop_cond);
  178. init_input->push_back(*var_one);
  179. init_input->push_back(*var_zero);
  180. init_ops_.push_back(var_iter_num);
  181. init_ops_.push_back(var_loop_cond);
  182. init_ops_.push_back(var_one);
  183. init_ops_.push_back(var_zero);
  184. init_ops_.push_back(const_iter_num);
  185. init_ops_.push_back(const_loop_cond);
  186. init_ops_.push_back(const_one);
  187. init_ops_.push_back(const_zero);
  188. init_ops_.push_back(assign_iter_num);
  189. init_ops_.push_back(assign_loop_cond);
  190. init_ops_.push_back(assign_one);
  191. init_ops_.push_back(assign_zero);
  192. }
  193. }
  194. OpAdapterPtr DfGraphConvertor::FindAdapter(const std::string &name, bool train) {
  195. auto it = OpAdapterMap::get().find(name);
  196. if (it != OpAdapterMap::get().end()) {
  197. return it->second->Get(train);
  198. }
  199. MS_LOG(EXCEPTION) << "Can't find OpAdapter for " << name;
  200. }
  201. void DfGraphConvertor::DrawParamInitSubGraph(const std::string &name, const AnfNodePtr &it) {
  202. // draw init subgraph
  203. init_sout_ << "op_assign" << it.get() << "[label=<";
  204. init_sout_ << "<table border='1' cellborder='1'>" << endl;
  205. init_sout_ << "<tr>";
  206. init_sout_ << "<td port='1'>resource</td>";
  207. init_sout_ << "<td port='2'>value</td>";
  208. init_sout_ << "</tr>" << endl;
  209. init_sout_ << "<tr><td colspan=\"2\">"
  210. << "\"assign_" << name << "\"</td></tr>" << endl;
  211. init_sout_ << "</table>> shape=plaintext]" << endl;
  212. init_sout_ << "param" << it.get() << "[shape=octagon, label=\"" << name << "\"]" << endl;
  213. init_sout_ << "const" << it.get() << "[label= \"" << name << "_const"
  214. << "\" shape=ellipse]" << endl;
  215. init_sout_ << "param" << it.get() << "->"
  216. << "op_assign" << it.get() << ":1" << endl;
  217. init_sout_ << "const" << it.get() << "->"
  218. << "op_assign" << it.get() << ":2" << endl;
  219. }
  220. void DfGraphConvertor::SetupParamInitSubGraph(const TensorOrderMap &tensors, std::vector<ge::Operator> *init_input) {
  221. DfGraphPtr init_graph = std::make_shared<DfGraph>("init");
  222. std::vector<AnfNodePtr> nodes = GetOrderedCNodes(anf_graph_);
  223. for (auto &it : nodes) {
  224. if (it->isa<ValueNode>()) {
  225. if (IsValueNode<SymbolicKeyInstance>(it)) {
  226. auto symbolic = GetValueNode<SymbolicKeyInstancePtr>(it);
  227. auto name = std::static_pointer_cast<Parameter>(symbolic->node())->name();
  228. auto iter = vars_.find(name); // get corresponding variable op
  229. if (iter != vars_.end()) {
  230. op_cache_[it.get()] = iter->second;
  231. // #ifdef DRAW_GE_GRAPH
  232. compute_sout_ << op_draw_name_[params_[name].get()] << " -> " << op_draw_name_[it.get()]
  233. << "[style=\"dotted\"]" << endl;
  234. // #endif
  235. }
  236. } else if (IsValueNode<RefKey>(it)) {
  237. auto refkey = GetValueNode<RefKeyPtr>(it);
  238. auto name = refkey->tag();
  239. auto iter = vars_.find(name); // get corresponding variable op
  240. if (iter != vars_.end()) {
  241. op_cache_[it.get()] = iter->second;
  242. compute_sout_ << op_draw_name_[params_[name].get()] << " -> " << op_draw_name_[it.get()]
  243. << "[style=\"dotted\"]" << endl;
  244. }
  245. }
  246. }
  247. }
  248. for (auto &it : tensors) {
  249. if (vars_.find(it.first) == vars_.end()) {
  250. MS_LOG(WARNING) << "Init parameter " << it.first << " didn't appear in graph.";
  251. vars_[it.first] = nullptr;
  252. }
  253. }
  254. // set up init sub graph
  255. if (init_input->size()) {
  256. // init sub graph needs no input
  257. MS_LOG(INFO) << "Build data init subgraph.";
  258. (void)init_graph->SetInputs(*init_input);
  259. this->init_graph_ = init_graph;
  260. } else {
  261. this->init_graph_ = nullptr;
  262. }
  263. }
  264. void DfGraphConvertor::MakeDatasetHandler(const std::string &name, const size_t &input_idx, const AnfNodePtr &it) {
  265. MS_LOG(INFO) << "The " << name << " is the " << input_idx << "(st/nd/th) input";
  266. if (ConfigManager::GetInstance().dataset_mode() == DS_SINK_MODE) {
  267. auto getnext_idx = static_cast<int64_t>(input_idx);
  268. DatasetGraphParam param = ConfigManager::GetInstance().dataset_param();
  269. if (!param.input_indexes().empty() && input_idx <= param.input_indexes().size()) {
  270. getnext_idx = param.input_indexes()[input_idx] - 1; // input_idx start from 0.
  271. MS_LOG(INFO) << "remap input_index:" << input_idx << " to getnext_index:" << getnext_idx << ".";
  272. }
  273. // use iterator_getnext op with output_name instead of data op in BuildGraph.
  274. out_handle_cache_[it.get()] = OutHandler(dataset_iter_getnext_, "y" + std::to_string(getnext_idx));
  275. }
  276. }
  277. void DfGraphConvertor::SetupBroadcast(const std::shared_ptr<HcomBroadcast> &broadcast,
  278. const std::vector<GeTensorDesc> &broadcast_desc,
  279. const DfGraphPtr &broadcast_graph, std::vector<ge::Operator> broadcast_input) {
  280. MS_LOG(INFO) << "build broadcast subgraph";
  281. if (broadcast_desc.size() != broadcast_input.size()) {
  282. MS_LOG(EXCEPTION) << "Desc number of BroadCast is not equal to number of Input";
  283. }
  284. (void)broadcast->create_dynamic_input_x(static_cast<unsigned int>(broadcast_input.size()));
  285. (void)broadcast->create_dynamic_output_y(static_cast<unsigned int>(broadcast_desc.size()));
  286. for (unsigned int i = 0; i < broadcast_input.size(); i++) {
  287. (void)broadcast->set_dynamic_input_x(i, broadcast_input[i]);
  288. (void)broadcast->update_dynamic_output_desc_y(i, broadcast_desc[i]);
  289. }
  290. (void)broadcast_graph->SetInputs(broadcast_input);
  291. this->broadcast_graph_ = broadcast_graph;
  292. }
  293. void DfGraphConvertor::InitParamWithData(const TensorOrderMap &tensors) {
  294. int index = 0;
  295. std::vector<Operator> init_input;
  296. for (auto it : tensors) {
  297. std::string name = it.first;
  298. auto node_itor = params_.find(name);
  299. // if name not in params_, create a node in graph
  300. if (node_itor == params_.end()) {
  301. MS_LOG(WARNING) << name << " is not in params, and create a new node.";
  302. ParameterPtr param = std::make_shared<Parameter>(nullptr);
  303. name = name + "_temp";
  304. param->set_name(name);
  305. (void)ConvertParameter(param);
  306. node_itor = params_.find(name);
  307. }
  308. auto node = node_itor->second;
  309. auto op_itor = op_cache_.find(node.get());
  310. if (op_itor == op_cache_.end()) {
  311. MS_LOG(EXCEPTION) << "Can not find op for node " << node->ToString() << ".";
  312. }
  313. auto adpt = FindAdapter(kNameParam, training_);
  314. if (adpt == nullptr) continue;
  315. auto param_op = adpt->generate(name + "_data");
  316. MS_LOG(INFO) << "Add parameter " << name << " as input, index " << index << ".";
  317. if (!training_) {
  318. auto adpt_const = FindAdapter(kNameConst, training_);
  319. if (adpt_const == nullptr) continue;
  320. auto const_op = adpt_const->generate(name + "_const");
  321. (void)adpt_const->setAttr(const_op, "value", it.second);
  322. auto const_op_desc = TransformUtil::GetGeTensorDesc(it.second->shape_c(), it.second->data_type(), kOpFormat_NCHW);
  323. if (const_op_desc == nullptr) {
  324. MS_LOG(ERROR) << "Create variable " << name << " output descriptor failed!";
  325. continue;
  326. }
  327. (void)std::static_pointer_cast<Constant>(const_op)->update_output_desc_y(*const_op_desc);
  328. vars_[name] = const_op;
  329. op_itor->second = const_op;
  330. continue;
  331. }
  332. // create tensor descriptor for output descriptor
  333. auto desc = TransformUtil::GetGeTensorDesc(it.second->shape_c(), it.second->data_type(), kOpFormat_NCHW);
  334. if (desc == nullptr) {
  335. MS_LOG(ERROR) << "Create variable " << name << " output descriptor failed!";
  336. continue;
  337. }
  338. // we need three variable ops for each graph with same name
  339. // build init subgraph
  340. if (it.second->is_init() == 0) {
  341. (void)std::static_pointer_cast<Data>(param_op)->set_attr_index(index++);
  342. auto init_var = std::make_shared<Variable>(name);
  343. auto assign_op = std::make_shared<Assign>("assign_" + name);
  344. (void)init_var->update_output_desc_y(*desc);
  345. (void)assign_op->set_input_ref(*init_var).set_input_value(*param_op);
  346. init_input.push_back(*init_var);
  347. init_ops_.push_back(param_op);
  348. init_ops_.push_back(assign_op);
  349. init_ops_.push_back(init_var);
  350. }
  351. auto variable = std::make_shared<Variable>(name);
  352. (void)variable->update_output_desc_y(*desc);
  353. // do not use read variable while variable sink
  354. MS_LOG(DEBUG) << "InitParam, op_name = " << name << ", var = " << variable->GetName() << ".";
  355. op_itor->second = variable; // replace parameter with variable
  356. vars_[name] = variable; // prevent the variable operator from being freed
  357. DrawParamInitSubGraph(name, node);
  358. }
  359. InitLoopVar(&init_input);
  360. SetupParamInitSubGraph(tensors, &init_input);
  361. }
  362. // convert all parameter need initialize to variable
  363. DfGraphConvertor &DfGraphConvertor::InitParam(const TensorOrderMap &tensors) {
  364. size_t input_idx = 0;
  365. if (error_ != 0) {
  366. return *this;
  367. }
  368. if (anf_graph_ == nullptr || anf_graph_->output() == nullptr) {
  369. error_ = INVALID_ARGUMENT;
  370. MS_LOG(ERROR) << "Invalid AnfGraph in InitParam.";
  371. return *this;
  372. }
  373. // Processing input with MakeDatasetHandler
  374. for (auto &it : anf_graph_->parameters()) {
  375. auto op_itor = op_cache_.find(it.get()); // converted node
  376. if (it->isa<Parameter>() && op_itor != op_cache_.end()) {
  377. string name = std::static_pointer_cast<Parameter>(it)->name();
  378. auto tensor_itor = tensors.find(name); // in init value map
  379. if (tensor_itor == tensors.end()) {
  380. DfGraphConvertor::MakeDatasetHandler(name, input_idx, it);
  381. input_idx++;
  382. }
  383. }
  384. }
  385. InitParamWithData(tensors);
  386. init_sout_ << "}" << endl;
  387. return *this;
  388. }
  389. #if (defined ENABLE_GE)
  390. void DfGraphConvertor::BuildSaveCheckpointGraph() {
  391. std::vector<Operator> graph_inputs;
  392. ge::op::Save save_op("save_parms");
  393. int save_op_is_active = 0;
  394. size_t index = 0;
  395. string name;
  396. int32_t count_size = std::count_if(vars_.begin(), vars_.end(), [](const std::pair<std::string, OperatorPtr> &it) {
  397. return (it.second == nullptr || it.first.find("/") != std::string::npos);
  398. });
  399. (void)save_op.create_dynamic_input_tensors(vars_.size() - static_cast<size_t>(count_size));
  400. // for each "parameter" in anf graph excluding "input"
  401. for (const auto &it : vars_) {
  402. name = it.first;
  403. if (it.second == nullptr || name.find("/") != std::string::npos) continue;
  404. Variable variable(name);
  405. (void)variable.update_output_desc_y(it.second->GetOutputDesc(0));
  406. (void)save_op.set_dynamic_input_tensors(index++, variable);
  407. graph_inputs.push_back(variable);
  408. if (save_op_is_active == 0) {
  409. checkpoint_sout_ << "op_save" << &save_op << "[label=<";
  410. checkpoint_sout_ << "<table border='1' cellborder='1'>" << endl;
  411. checkpoint_sout_ << "<tr><td port='1'>tensor</td></tr>" << endl;
  412. checkpoint_sout_ << "<tr><td colspan=\"1\">"
  413. << "\"saveop"
  414. << "\"</td></tr>" << endl;
  415. checkpoint_sout_ << "</table>> shape=plaintext]" << endl;
  416. }
  417. checkpoint_sout_ << "param" << it.second << "[shape=octagon, label=\"" << name << "\"]" << endl;
  418. checkpoint_sout_ << "param" << it.second << "->"
  419. << "op_save" << &save_op << ":1" << endl;
  420. save_op_is_active = 1;
  421. }
  422. if (save_op_is_active) {
  423. std::vector<Operator> graph_output;
  424. graph_output.emplace_back(save_op);
  425. DfGraphPtr checkpoint_graph = std::make_shared<DfGraph>("checkpoint");
  426. (void)checkpoint_graph->SetInputs(graph_inputs);
  427. (void)checkpoint_graph->SetOutputs(graph_output);
  428. this->save_ckp_graph_ = checkpoint_graph;
  429. } else {
  430. this->save_ckp_graph_ = nullptr;
  431. }
  432. checkpoint_sout_ << "}" << endl;
  433. return;
  434. }
  435. #endif
  436. DfGraphConvertor &DfGraphConvertor::GenerateBroadcastGraph(const TensorOrderMap &tensors) {
  437. if (error_ != 0) {
  438. return *this;
  439. }
  440. if (anf_graph_ == nullptr || anf_graph_->output() == nullptr) {
  441. error_ = INVALID_ARGUMENT;
  442. MS_LOG(ERROR) << "Invalid AnfGraph in generate broadcast graph";
  443. return *this;
  444. }
  445. DfGraphPtr broadcast_graph = std::make_shared<DfGraph>("broadcast");
  446. // collect the operators create for broadcast sub graph, in order to avoid auto release
  447. std::vector<Operator> broadcast_input;
  448. std::vector<GeTensorDesc> broadcast_desc;
  449. auto broadcast = std::make_shared<HcomBroadcast>("broadcast_parameter");
  450. (void)broadcast->set_attr_root_rank(0);
  451. (void)broadcast->set_attr_group("hccl_world_group");
  452. broadcast_ops_.push_back(broadcast);
  453. // find every parameter, build broadcast subgraph (or initialize the parameter with constant)
  454. for (auto &it : anf_graph_->parameters()) {
  455. auto op_itor = op_cache_.find(it.get()); // converted node
  456. if (it->isa<Parameter>() && op_itor != op_cache_.end()) {
  457. string name = std::static_pointer_cast<Parameter>(it)->name();
  458. auto tensor_itor = tensors.find(name); // in init tensor map
  459. if (tensor_itor != tensors.end()) {
  460. auto tensor = tensor_itor->second;
  461. auto shape_ge = tensor->shape_c();
  462. // create tensor descriptor for output descriptor
  463. auto desc = TransformUtil::GetGeTensorDesc(shape_ge, tensor->data_type(), kOpFormat_NCHW);
  464. if (desc == nullptr) {
  465. MS_LOG(ERROR) << "Create variable " << name << " output descriptor failed!";
  466. continue;
  467. }
  468. // build broadcast subgraph
  469. if (distribute_) {
  470. auto broadcast_var = std::make_shared<Variable>(name);
  471. (void)broadcast_var->update_output_desc_y(*desc);
  472. broadcast_input.push_back(*broadcast_var);
  473. broadcast_desc.push_back(*desc);
  474. broadcast_ops_.push_back(broadcast_var);
  475. }
  476. }
  477. }
  478. }
  479. // set up broadcast sub graph
  480. if (!broadcast_input.empty()) {
  481. DfGraphConvertor::SetupBroadcast(broadcast, broadcast_desc, broadcast_graph, broadcast_input);
  482. } else {
  483. this->broadcast_graph_ = nullptr;
  484. }
  485. return *this;
  486. }
  487. DfGraphConvertor &DfGraphConvertor::GenerateCheckpointGraph() {
  488. if (error_ != 0) {
  489. MS_LOG(ERROR) << "Generate checkpoint graph failed, found error code " << error_ << ".";
  490. return *this;
  491. }
  492. if (anf_graph_ == nullptr || anf_graph_->output() == nullptr) {
  493. error_ = INVALID_ARGUMENT;
  494. MS_LOG(ERROR) << "Invalid AnfGraph in GenerateCheckpointGraph";
  495. return *this;
  496. }
  497. #if (defined ENABLE_GE)
  498. BuildSaveCheckpointGraph();
  499. // Restoring from checkpoint file is done by pyfront, not in graph now.
  500. #endif
  501. return *this;
  502. }
  503. DfGraphConvertor &DfGraphConvertor::ConvertAllNode() {
  504. if (error_ != 0) {
  505. return *this;
  506. }
  507. if (anf_graph_ == nullptr || anf_graph_->output() == nullptr) {
  508. MS_LOG(ERROR) << "Invalid AnfGraph";
  509. error_ = FAILED;
  510. return *this;
  511. }
  512. compute_sout_.clear();
  513. compute_sout_ << "digraph {" << endl;
  514. init_sout_.clear();
  515. init_sout_ << "digraph {" << endl;
  516. #if (defined ENABLE_GE)
  517. checkpoint_sout_.clear();
  518. checkpoint_sout_ << "digraph {" << endl;
  519. #endif
  520. restore_checkpoint_sout_.clear();
  521. restore_checkpoint_sout_ << "digraph {" << endl;
  522. // Convert all anf node to Operator
  523. MS_LOG(DEBUG) << "convert all node";
  524. std::vector<AnfNodePtr> nodes = GetOrderedCNodes(anf_graph_);
  525. for (auto &it : nodes) {
  526. (void)Convert(it);
  527. if (this->error_ != 0) {
  528. MS_LOG(ERROR) << "failed to convert node: " << it->DebugString() << ".";
  529. }
  530. }
  531. // Create dataset iterator and iterator_getnext node
  532. if (ConfigManager::GetInstance().dataset_mode() == DS_SINK_MODE) {
  533. DatasetGraphParam param = ConfigManager::GetInstance().dataset_param();
  534. MS_LOG(INFO) << "Dataset param is " << param.ToString() << ".";
  535. // GetNext
  536. auto iter_getnext_op = make_shared<ge::op::GetNext>("get_next_tmp");
  537. (void)iter_getnext_op->set_attr_output_types(param.ge_types());
  538. (void)iter_getnext_op->set_attr_output_shapes(param.shapes());
  539. (void)iter_getnext_op->set_attr_channel_name(param.queue_name());
  540. // save iter_getnext_op for later use
  541. dataset_iter_getnext_ = iter_getnext_op;
  542. }
  543. // return the data flow graph
  544. return *this;
  545. }
  546. void DfGraphConvertor::TraceOutputFromTupleGetItem(const AnfNodePtr &anf_out) {
  547. auto it = out_handle_cache_.find(anf_out.get());
  548. if (it != out_handle_cache_.end()) {
  549. OutHandler handle = it->second;
  550. auto op = handle.op;
  551. if (op != nullptr) {
  552. MS_LOG(INFO) << "op name: " << op->GetName() << ", op type: " << op->GetOpType() << ", out_name: " << handle.out;
  553. graph_outputs_.emplace_back(std::make_pair(*op, handle.out));
  554. } else {
  555. MS_LOG(EXCEPTION) << "tuple_getitem: " << anf_out->fullname_with_scope() << " is not converted";
  556. }
  557. } else {
  558. // invalid tuple_getitem e.g. tuple_getitem(tuple_getitem())/tuple_getitem(depend())/tuple_getitem(make_tuple())
  559. MS_LOG(WARNING) << "Invalid tuple_getitem: " << anf_out->fullname_with_scope();
  560. }
  561. }
  562. void DfGraphConvertor::TraceOutput(const AnfNodePtr node) {
  563. AnfNodePtr anf_out = node;
  564. AnfNodePtr pre_node = nullptr;
  565. // Trace value node
  566. if (node->isa<ValueNode>()) {
  567. auto op = Convert(anf_out);
  568. if (op != nullptr) {
  569. graph_outputs_.emplace_back(std::make_pair(*op, ""));
  570. AddGraphConstInput(op);
  571. }
  572. return;
  573. }
  574. // Trace Parameter node
  575. TraceOutputFromParameter(anf_out);
  576. // Then trace cnode
  577. if (!node->isa<CNode>()) {
  578. return;
  579. }
  580. // trace tuple_getitem
  581. while (anf_out->isa<CNode>() && IsPrimitiveCNode(anf_out, prim::kPrimTupleGetItem)) {
  582. pre_node = anf_out;
  583. anf_out = anf_out->cast<CNodePtr>()->input(1);
  584. }
  585. // trace every element of make_tuple
  586. auto c = anf_out->cast<CNodePtr>();
  587. std::string name = "";
  588. if (anf_out->isa<CNode>()) {
  589. name = GetCNodeTargetFuncName(c);
  590. }
  591. if (name == "MakeTuple") {
  592. for (unsigned int i = 1; i < c->inputs().size(); i++) {
  593. TraceOutput(c->input(i));
  594. }
  595. } else if (name == prim::kPrimDepend->name()) {
  596. if (c->inputs().size() < 3) { // "Depend" primitive have 3 inputs
  597. MS_LOG(EXCEPTION) << "length of inputs is " << c->inputs().size() << ", which is less than 3";
  598. }
  599. TraceOutput(c->input(1));
  600. } else if (name == prim::kTupleGetItem) {
  601. TraceOutputFromTupleGetItem(anf_out);
  602. } else {
  603. // add outputs;
  604. auto op = Convert(anf_out);
  605. std::string index;
  606. if (op != nullptr) {
  607. if ((pre_node != nullptr) && IsPrimitiveCNode(pre_node, prim::kPrimTupleGetItem)) {
  608. auto item = out_handle_cache_.find(pre_node.get());
  609. if (item != out_handle_cache_.end()) {
  610. index = item->second.out;
  611. } else {
  612. MS_LOG(WARNING) << "Can't get operator: " << anf_out->fullname_with_scope() << " 's output item";
  613. }
  614. }
  615. MS_LOG(INFO) << "Add graph output: " << anf_out->fullname_with_scope() << ":" << index;
  616. graph_outputs_.emplace_back(make_pair(*op, index));
  617. }
  618. }
  619. }
  620. void DfGraphConvertor::TraceOutputFromParameter(const AnfNodePtr &anf_out) {
  621. if (anf_out->isa<Parameter>()) {
  622. MS_LOG(INFO) << "Add graph output: " << anf_out->fullname_with_scope();
  623. auto it = out_handle_cache_.find(anf_out.get());
  624. if (it != out_handle_cache_.end()) {
  625. // For dataset graph mode, input parameter is converted to a "iterator_get_next:yn" OutHandler.
  626. OutHandler handle = it->second;
  627. auto op = handle.op;
  628. MS_LOG(INFO) << "op name: " << op->GetName() << ", op type: " << op->GetOpType() << ", out_name: " << handle.out;
  629. graph_outputs_.emplace_back(make_pair(*op, handle.out));
  630. } else {
  631. // common parameter case
  632. auto op = Convert(anf_out);
  633. if (op != nullptr) {
  634. MS_LOG(INFO) << "op name: " << op->GetName() << ", op type: " << op->GetOpType();
  635. graph_outputs_.emplace_back(std::make_pair(*op, ""));
  636. }
  637. }
  638. }
  639. }
  640. void SetupDatasetIterGetNextNode(const OperatorPtr &op) {
  641. if (ConfigManager::GetInstance().dataset_mode() == DS_SINK_MODE) {
  642. DatasetGraphParam param = ConfigManager::GetInstance().dataset_param();
  643. size_t output_num = param.ge_types().size();
  644. MS_LOG(INFO) << "Set iterator_getnext op's output num = " << output_num << ".";
  645. // set iterator_getnext op's output num
  646. shared_ptr<ge::op::GetNext> iter_getnext = std::static_pointer_cast<ge::op::GetNext>(op);
  647. (void)iter_getnext->create_dynamic_output_y(static_cast<unsigned int>(output_num));
  648. for (uint32_t i = 0; i < output_num; i++) {
  649. ge::TensorDesc desc(GeShape(param.shapes()[i]), ge::FORMAT_NCHW, (ge::DataType)param.ge_types()[i]);
  650. // we don't SetRealDimCnt here since GE do not use this output's real-dim
  651. (void)iter_getnext->update_dynamic_output_desc_y((i), desc);
  652. }
  653. }
  654. return;
  655. }
  656. void DfGraphConvertor::SetSubgraph(AnfNodePtr node) {
  657. if (!node->isa<CNode>()) {
  658. return;
  659. }
  660. auto cnode = node->cast<CNodePtr>();
  661. if (!IsCaseNode(cnode)) {
  662. return;
  663. }
  664. std::vector<AnfNodePtr> case_inputs;
  665. for (size_t i = 1; i < cnode->inputs().size(); i++) {
  666. case_inputs.emplace_back(cnode->input(i));
  667. }
  668. std::shared_ptr<std::vector<DfGraph>> branches = std::make_shared<std::vector<DfGraph>>();
  669. auto bnode = cnode->input(0)->cast<CNodePtr>()->input(2)->cast<CNodePtr>();
  670. for (size_t i = 1; i < bnode->inputs().size(); i++) {
  671. auto branch_node = bnode->input(i)->cast<CNodePtr>();
  672. for (size_t j = 2; j < branch_node->inputs().size(); j++) {
  673. if (std::find(case_inputs.begin(), case_inputs.end(), branch_node->input(j)) == case_inputs.end()) {
  674. case_inputs.emplace_back(branch_node->input(j));
  675. }
  676. }
  677. }
  678. for (size_t i = 1; i < bnode->inputs().size(); i++) {
  679. ProcessSubgraph(bnode->input(i), case_inputs);
  680. }
  681. for (size_t i = 1; i < bnode->inputs().size(); i++) {
  682. branches->emplace_back(branches_map_[bnode->input(i).get()]);
  683. }
  684. if (op_cache_.find(node.get()) == op_cache_.end()) {
  685. return;
  686. }
  687. OpAdapterPtr adpt = FindAdapter(node, training_);
  688. if (nullptr == adpt) {
  689. MS_LOG(DEBUG) << "Not found adapter";
  690. return;
  691. }
  692. OperatorPtr op = Convert(node);
  693. adpt->setSubgraph(op, 0, branches);
  694. return;
  695. }
  696. void DfGraphConvertor::GetCaseNodeInput(const CNodePtr node, const CNodePtr input_node) {
  697. std::vector<AnfNodePtr> case_inputs;
  698. for (size_t i = 1; i < node->inputs().size(); i++) {
  699. case_inputs.emplace_back(node->input(i));
  700. }
  701. auto bnode = input_node->input(2)->cast<CNodePtr>();
  702. for (size_t i = 1; i < bnode->inputs().size(); i++) {
  703. auto branch_node = bnode->input(i)->cast<CNodePtr>();
  704. for (size_t j = 2; j < branch_node->inputs().size(); j++) {
  705. if (std::find(case_inputs.begin(), case_inputs.end(), branch_node->input(j)) == case_inputs.end()) {
  706. case_inputs.emplace_back(branch_node->input(j));
  707. }
  708. }
  709. }
  710. const size_t case_index = 1;
  711. const size_t make_tuple_index = 2;
  712. AnfNodePtr case_index_iter = input_node->input(case_index);
  713. AnfNodePtr make_tuple_iter = input_node->input(make_tuple_index);
  714. auto make_tuple_node = make_tuple_iter->cast<CNodePtr>();
  715. std::shared_ptr<std::vector<OutHandler>> tuple_items = std::make_shared<std::vector<OutHandler>>();
  716. for (size_t i = 0; i < case_inputs.size(); i++) {
  717. auto item = case_inputs[i];
  718. auto op = Convert(item);
  719. if (op != nullptr) {
  720. tuple_items->emplace_back(OutHandler(op, "", item));
  721. } else if (out_handle_cache_.find(item.get()) != out_handle_cache_.end()) {
  722. tuple_items->push_back(out_handle_cache_[item.get()]);
  723. } else {
  724. MS_LOG(DEBUG) << "Add an empty out handler: " << item->ToString();
  725. tuple_items->push_back(OutHandler());
  726. }
  727. }
  728. tuple_out_handle_cache_[make_tuple_node.get()] = tuple_items;
  729. std::shared_ptr<std::vector<AnfNodePtr>> case_input_items = std::make_shared<std::vector<AnfNodePtr>>();
  730. case_input_items->emplace_back(case_index_iter);
  731. case_input_items->emplace_back(make_tuple_iter);
  732. case_input_handle_cache_[node.get()] = case_input_items;
  733. }
  734. void DfGraphConvertor::UpdateTupleOutCache() {
  735. for (auto &it : tuple_out_handle_cache_) {
  736. std::size_t len = it.second->size();
  737. for (std::size_t i = 0; i < len; i++) {
  738. OutHandler handle = (*it.second)[i];
  739. if (handle.op == nullptr) {
  740. continue;
  741. }
  742. string name = handle.op->GetName();
  743. if (vars_.count(name) && (vars_[name] != nullptr)) {
  744. (*it.second)[i] = OutHandler(vars_[name], handle.out, handle.node);
  745. MS_LOG(INFO) << "update tuple_out_handle_cache_ " << name;
  746. }
  747. }
  748. }
  749. }
  750. DfGraphConvertor &DfGraphConvertor::BuildGraph() {
  751. SetupDatasetIterGetNextNode(dataset_iter_getnext_);
  752. if (error_ != 0) {
  753. return *this;
  754. }
  755. // Case node set input.
  756. std::vector<AnfNodePtr> nodes = GetOrderedCNodes(anf_graph_);
  757. for (auto &it : nodes) {
  758. if (it->isa<CNode>() && IsCaseNode(it->cast<CNodePtr>())) {
  759. auto node = it->cast<CNodePtr>();
  760. auto input_node = node->input(0)->cast<CNodePtr>();
  761. GetCaseNodeInput(node, input_node);
  762. }
  763. }
  764. // update tuple_out_handle_cache_
  765. UpdateTupleOutCache();
  766. // set up dependencies
  767. MS_LOG(DEBUG) << "set up dependencies";
  768. nodes = GetOrderedCNodes(anf_graph_);
  769. for (auto &it : nodes) {
  770. SetNodeInput(it);
  771. SetOpControlInput(it);
  772. SetSubgraph(it);
  773. UpdateOpDesc(it);
  774. }
  775. if (error_ == 0) {
  776. df_graph_ = make_shared<DfGraph>(anf_graph_->ToString());
  777. } else {
  778. return *this;
  779. }
  780. // set graph input according to the order from anf graph
  781. std::vector<Operator> inputs;
  782. if (ConfigManager::GetInstance().dataset_mode() == DS_SINK_MODE) {
  783. inputs.push_back(*dataset_iter_getnext_);
  784. } else {
  785. auto params = anf_graph_->parameters();
  786. if (use_inputs_) {
  787. params = inputs_;
  788. auto anf_params = anf_graph_->parameters();
  789. for (size_t i = 0; i < params.size(); i++) {
  790. for (size_t j = 0; j < anf_params.size(); j++) {
  791. if (params[i]->ToString() == anf_params[j]->ToString()) {
  792. params[i] = anf_params[j];
  793. }
  794. }
  795. }
  796. }
  797. int index = 0;
  798. for (auto &it : params) {
  799. auto name = std::static_pointer_cast<Parameter>(it)->name();
  800. // the parameters which has not been converted to var
  801. if (vars_.find(name) == vars_.end()) {
  802. auto op = Convert(it);
  803. MS_EXCEPTION_IF_NULL(op);
  804. MS_LOG(INFO) << "add not var input " << it->ToString() << ", index " << index;
  805. if (op == nullptr) {
  806. MS_LOG(ERROR) << "Convert graph failed!";
  807. return *this;
  808. }
  809. UpdateDataOpDesc(it, op);
  810. MS_LOG(INFO) << "add input " << it->ToString() << ", index " << index;
  811. (void)std::static_pointer_cast<Data>(op)->set_attr_index(index++);
  812. inputs.push_back(*op);
  813. } else if (vars_[name] != nullptr) {
  814. MS_LOG(INFO) << "add var input " << it->ToString();
  815. auto op = Convert(it);
  816. MS_EXCEPTION_IF_NULL(op);
  817. inputs.push_back(*op);
  818. }
  819. }
  820. }
  821. MS_LOG(DEBUG) << "trace output";
  822. graph_outputs_.clear();
  823. TraceOutput(anf_graph_->get_return()->input(1));
  824. // Add const nodes as graph input for some operator work with constant
  825. MS_LOG(INFO) << "graph const input size: " << graph_const_inputs_.size();
  826. std::transform(graph_const_inputs_.begin(), graph_const_inputs_.end(), std::back_inserter(inputs),
  827. [](OperatorPtr x) { return *x; });
  828. MS_LOG(INFO) << "set graph input num: " << inputs.size();
  829. (void)df_graph_->SetInputs(inputs);
  830. // set graph output
  831. // set the value of finale return apply node as the output of dataflow graph
  832. MS_LOG(DEBUG) << "set output";
  833. MS_LOG(INFO) << "set graph output num: " << graph_outputs_.size();
  834. (void)df_graph_->SetOutputs(graph_outputs_);
  835. compute_sout_ << "}" << endl;
  836. // For the graph(e.g. eval_subgraph) whose IterNum is 1, donot set NeedIteration flag.
  837. if (ConfigManager::GetInstance().iter_num() > 1) {
  838. df_graph_->SetNeedIteration(true);
  839. }
  840. return *this;
  841. }
  842. void DfGraphConvertor::UpdateDataOpDesc(const AnfNodePtr &it, const OperatorPtr &op) const {
  843. auto node = std::static_pointer_cast<AnfNode>(it);
  844. if (node == nullptr) {
  845. MS_LOG(ERROR) << "Update data op descriptor failed! Invalid node.";
  846. return;
  847. }
  848. auto normal_shape_ptr = dyn_cast<abstract::Shape>(node->Shape());
  849. std::vector<int64_t> shape;
  850. if (normal_shape_ptr == nullptr) {
  851. MS_LOG(INFO) << "Invalid shape to update data op descriptor.";
  852. return;
  853. }
  854. shape = normal_shape_ptr->shape();
  855. if (node->Type() == nullptr) {
  856. MS_LOG(INFO) << "Invalid type to update data op descriptor.";
  857. return;
  858. }
  859. TypeId me_type = node->Type()->type_id();
  860. if (kObjectTypeTensorType == me_type) {
  861. me_type = dyn_cast<TensorType>(node->Type())->element()->type_id();
  862. }
  863. std::ostringstream buf;
  864. buf << "[" << shape << "]";
  865. MS_LOG(INFO) << "input shape is " << buf.str() << ", type is " << me_type;
  866. auto desc = TransformUtil::GetGeTensorDesc(shape, me_type, "NCHW");
  867. if (desc == nullptr) {
  868. MS_LOG(ERROR) << "Update data op descriptor failed! TensorDesc is null.";
  869. } else {
  870. (void)std::static_pointer_cast<Data>(op)->update_input_desc_x(*desc);
  871. (void)std::static_pointer_cast<Data>(op)->update_output_desc_y(*desc);
  872. }
  873. }
  874. DfGraphPtr DfGraphConvertor::GetComputeGraph() { return df_graph_; }
  875. DfGraphPtr DfGraphConvertor::GetInitGraph() { return init_graph_; }
  876. DfGraphPtr DfGraphConvertor::GetSaveCheckpointGraph() { return save_ckp_graph_; }
  877. DfGraphPtr DfGraphConvertor::GetBroadcastGraph() { return broadcast_graph_; }
  878. bool DfGraphConvertor::IsSourceEdgeNode(const AnfNodePtr &node) {
  879. if (!node->isa<CNode>()) {
  880. return false;
  881. }
  882. auto cnode = node->cast<CNodePtr>();
  883. if (!IsCustomCNode(cnode)) {
  884. std::string name = GetCNodeTargetFuncName(cnode);
  885. if (name.empty()) {
  886. return false;
  887. }
  888. // Ignore apply node Depend, UpdateState, make_tuple. make_tuple in ge pipeline.
  889. if ((name == prim::kPrimDepend->name()) || (name == prim::kPrimUpdateState->name()) ||
  890. (name == prim::kPrimReturn->name()) || (name == prim::kPrimMakeTuple->name())) {
  891. return false;
  892. }
  893. }
  894. // Load and other normal primitives which contain monad node.
  895. auto has_monad = std::any_of(cnode->inputs().begin(), cnode->inputs().end(),
  896. [](const AnfNodePtr &node) -> bool { return HasAbstractMonad(node); });
  897. if (has_monad) {
  898. return true;
  899. }
  900. // primitive with make_tuple as input
  901. for (auto &input : cnode->inputs()) {
  902. if (IsPrimitiveCNode(input, prim::kPrimMakeTuple)) {
  903. auto tuple = input->cast<CNodePtr>();
  904. auto ret = std::any_of(tuple->inputs().begin(), tuple->inputs().end(),
  905. [](const AnfNodePtr &node) -> bool { return HasAbstractMonad(node); });
  906. if (ret) {
  907. return true;
  908. }
  909. }
  910. }
  911. return false;
  912. }
  913. bool DfGraphConvertor::IsControlEdgeNode(const AnfNodePtr &node) {
  914. if (!node->isa<CNode>()) {
  915. return false;
  916. }
  917. auto cnode = node->cast<CNodePtr>();
  918. if (!IsCustomCNode(cnode)) {
  919. std::string name = GetCNodeTargetFuncName(cnode);
  920. if (name.empty()) {
  921. return false;
  922. }
  923. // Ignore apply node of Load, Depend, UpdateState, make_tuple, return
  924. if ((name == prim::kPrimLoad->name()) || (name == prim::kPrimDepend->name()) ||
  925. (name == prim::kPrimUpdateState->name()) || (name == prim::kPrimMakeTuple->name()) ||
  926. (name == prim::kPrimReturn->name())) {
  927. return false;
  928. }
  929. }
  930. return true;
  931. }
  932. OperatorPtr DfGraphConvertor::ToOperatorPtr(const AnfNodePtr &node) {
  933. auto op = Convert(GetRealOpNode(node));
  934. if (op == nullptr) {
  935. MS_LOG(ERROR) << "Convert control depend node to operator failed, " << node->ToString();
  936. error_ = FAILED;
  937. return nullptr;
  938. }
  939. return op;
  940. }
  941. void DfGraphConvertor::AddEdgeToCache(const AnfNodePtr &src, const AnfNodePtr &dest) {
  942. auto item = monad_control_edge_cache_.find(src);
  943. if (item == monad_control_edge_cache_.end()) {
  944. monad_control_edge_cache_[src] = std::set<AnfNodePtr>{dest};
  945. } else {
  946. item->second.insert(dest);
  947. }
  948. }
  949. void DfGraphConvertor::AddEdgeForLoad(const AnfNodePtr &node) {
  950. auto func_graph = node->func_graph();
  951. MS_EXCEPTION_IF_NULL(func_graph);
  952. auto mng = func_graph->manager();
  953. if (mng == nullptr) {
  954. mng = Manage(func_graph, true);
  955. func_graph->set_manager(mng);
  956. }
  957. auto manager = func_graph->manager();
  958. MS_EXCEPTION_IF_NULL(manager);
  959. auto &users = manager->node_users()[node];
  960. std::shared_ptr<std::vector<AnfNodePtr>> src_node_list = std::make_shared<std::vector<AnfNodePtr>>();
  961. std::shared_ptr<std::vector<AnfNodePtr>> dst_node_list = std::make_shared<std::vector<AnfNodePtr>>();
  962. for (const auto &iter : users) {
  963. auto user_node = iter.first;
  964. auto name = GetCNodeTargetFuncName(user_node->cast<CNodePtr>());
  965. if (name == prim::kPrimUpdateState->name()) {
  966. FindDestOps(user_node, dst_node_list, false);
  967. continue;
  968. }
  969. if (IsControlEdgeNode(user_node)) {
  970. src_node_list->push_back(user_node);
  971. continue;
  972. }
  973. FindDestOps(user_node, src_node_list, false);
  974. }
  975. // add to cache
  976. for (auto &dest : *dst_node_list) {
  977. for (auto &src : *src_node_list) {
  978. AddEdgeToCache(src, dest);
  979. }
  980. }
  981. }
  982. void DfGraphConvertor::FindDestOps(const AnfNodePtr &node, const std::shared_ptr<std::vector<AnfNodePtr>> &node_list,
  983. bool top) {
  984. auto func_graph = node->func_graph();
  985. MS_EXCEPTION_IF_NULL(func_graph);
  986. auto mng = func_graph->manager();
  987. if (mng == nullptr) {
  988. mng = Manage(func_graph, true);
  989. func_graph->set_manager(mng);
  990. }
  991. auto manager = func_graph->manager();
  992. MS_EXCEPTION_IF_NULL(manager);
  993. auto users = manager->node_users()[node];
  994. for (const auto &iter : users) {
  995. auto user_node = iter.first;
  996. if (IsControlEdgeNode(user_node)) {
  997. if (!top) {
  998. node_list->push_back(user_node);
  999. }
  1000. } else {
  1001. FindDestOps(user_node, node_list, false);
  1002. }
  1003. }
  1004. }
  1005. void DfGraphConvertor::AutoMonadCollectInput(const AnfNodePtr &node) {
  1006. if (!IsSourceEdgeNode(node)) {
  1007. return;
  1008. }
  1009. // Add control edge if contain monad input.
  1010. std::string name = GetCNodeTargetFuncName(node->cast<CNodePtr>());
  1011. if (name == prim::kPrimLoad->name()) {
  1012. AddEdgeForLoad(node);
  1013. } else {
  1014. auto src_ops = ToOperatorPtr(node);
  1015. if (src_ops != nullptr) {
  1016. // Find dest ops list
  1017. std::shared_ptr<std::vector<AnfNodePtr>> dst_node_list = std::make_shared<std::vector<AnfNodePtr>>();
  1018. FindDestOps(node, dst_node_list, true);
  1019. for (auto &dest : *dst_node_list) {
  1020. AddEdgeToCache(node, dest);
  1021. }
  1022. }
  1023. }
  1024. }
  1025. void DfGraphConvertor::AutoMonadSetInput(const AnfNodePtr &node) {
  1026. if (monad_control_edge_cache_.find(node) == monad_control_edge_cache_.end()) {
  1027. return;
  1028. }
  1029. auto src_ops = ToOperatorPtr(node);
  1030. if (src_ops != nullptr) {
  1031. for (auto &dest : monad_control_edge_cache_[node]) {
  1032. auto dest_ops = ToOperatorPtr(dest);
  1033. if (dest_ops == nullptr) {
  1034. continue;
  1035. }
  1036. (void)dest_ops->AddControlInput(*src_ops);
  1037. #ifdef DRAW_GE_GRAPH
  1038. compute_sout_ << op_draw_name_[node.get()] << " -> " << op_draw_name_[dest.get()] << "[style=\"dotted\"]" << endl;
  1039. #endif
  1040. }
  1041. }
  1042. }
  1043. void DfGraphConvertor::AutoMonadSetControlInput(const AnfNodePtr &node) {
  1044. AutoMonadCollectInput(node);
  1045. AutoMonadSetInput(node);
  1046. }
  1047. void DfGraphConvertor::SetOpControlInput(const AnfNodePtr &node) {
  1048. AutoMonadSetControlInput(node);
  1049. if (control_depend_cache_.find(node.get()) == control_depend_cache_.end()) {
  1050. return;
  1051. }
  1052. std::vector<ControlEdge> control_edges = control_depend_cache_[node.get()];
  1053. if ((control_edges.empty())) {
  1054. MS_LOG(ERROR) << "Get control depend node's src or dest operator failed";
  1055. return;
  1056. }
  1057. for (auto &item : control_edges) {
  1058. (void)item.dest_op->AddControlInput(*item.src_op);
  1059. }
  1060. }
  1061. const std::vector<std::string> trans_var_list = {string(kNameAssign), string(kNameAssignAdd), string(kNameAssignSub)};
  1062. AnfNodePtr DfGraphConvertor::ParseLoadInput(const CNodePtr &cnode) {
  1063. if (cnode->inputs().size() < 3) {
  1064. MS_LOG(EXCEPTION) << "input size error, " << cnode->ToString();
  1065. }
  1066. const size_t para_index = 1;
  1067. return cnode->input(para_index);
  1068. }
  1069. void DfGraphConvertor::SetTupleOpInput(const OpAdapterPtr &adpt, const CNodePtr &node, const AnfNodePtr &pred,
  1070. const OperatorPtr &src, int index) {
  1071. std::shared_ptr<std::vector<OutHandler>> handler_vec = tuple_out_handle_cache_[pred.get()];
  1072. std::shared_ptr<std::vector<OutHandler>> handler_vec_without_monad = std::make_shared<std::vector<OutHandler>>();
  1073. bool with_monad = false;
  1074. for (auto &handler : *handler_vec) {
  1075. // when tuple with monad type element, the handler operator is nullptr, should be ignored.
  1076. if (handler.op == nullptr) {
  1077. if ((handler.node != nullptr) && !HasAbstractMonad(handler.node)) {
  1078. MS_LOG(WARNING) << "Unsupported node in tuple : " << node->ToString();
  1079. }
  1080. continue;
  1081. }
  1082. with_monad = true;
  1083. handler_vec_without_monad->push_back(handler);
  1084. }
  1085. int ret = adpt->setInput(src, index, handler_vec_without_monad);
  1086. if ((ret == 0) && pred->isa<CNode>() && (pred->cast<CNodePtr>()->inputs().size() == handler_vec->size() + 1)) {
  1087. for (unsigned int j = 0; j < handler_vec_without_monad->size(); j++) {
  1088. AnfNodePtr input_node = pred->cast<CNodePtr>()->input(j + 1);
  1089. if (with_monad) {
  1090. input_node = handler_vec_without_monad->at(j).node;
  1091. }
  1092. compute_sout_ << op_draw_name_[input_node.get()] << " -> " << op_draw_name_[node.get()] << ":" << index << endl;
  1093. AddGraphConstInput(handler_vec_without_monad->at(j).op);
  1094. }
  1095. return;
  1096. }
  1097. MS_LOG(WARNING) << "This anf node is not supported as a tuple item : " << node->ToString();
  1098. }
  1099. AnfNodePtr DfGraphConvertor::GetRealInputNode(const CNodePtr &node, const AnfNodePtr &input) {
  1100. if (input == nullptr || node == nullptr) {
  1101. return nullptr;
  1102. }
  1103. AnfNodePtr pred = input;
  1104. while (pred->isa<CNode>() && GetCNodeTargetFuncName(pred->cast<CNodePtr>()) == prim::kPrimDepend->name()) {
  1105. pred = pred->cast<CNodePtr>()->input(1);
  1106. }
  1107. // skip input of UMonad, IOMonad
  1108. if (IsValueNode<UMonad>(pred) || IsValueNode<IOMonad>(pred)) {
  1109. return nullptr;
  1110. }
  1111. // skip input of the None, UpdateState
  1112. if (IsValueNode<None>(pred) || IsPrimitiveCNode(pred, prim::kPrimUpdateState)) {
  1113. return nullptr;
  1114. }
  1115. if (IsPrimitiveCNode(pred, prim::kPrimLoad)) {
  1116. pred = ParseLoadInput(pred->cast<CNodePtr>());
  1117. }
  1118. // transform "Const" op to "Variable" op when the next node is "Assign" op.
  1119. std::string c_name = GetCNodeTargetFuncName(node);
  1120. auto pos = std::find(trans_var_list.begin(), trans_var_list.end(), c_name);
  1121. if (!training_ && pos != trans_var_list.end() && pred->isa<Parameter>()) {
  1122. std::string name = std::static_pointer_cast<Parameter>(pred)->name();
  1123. auto op_itor = op_cache_.find(pred.get());
  1124. if (op_itor == op_cache_.end()) {
  1125. MS_LOG(EXCEPTION) << "Can not find op for node " << pred->ToString() << ".";
  1126. }
  1127. if (op_itor->second != nullptr &&
  1128. (op_itor->second->GetOpType() == "Constant" || op_itor->second->GetOpType() == "Const") &&
  1129. vars_.find(name) != vars_.end()) {
  1130. auto variable = std::make_shared<Variable>(name);
  1131. auto desc = vars_[name]->GetOutputDesc("y");
  1132. (void)variable->update_output_desc_y(desc);
  1133. MS_LOG(DEBUG) << "Trans to variable, var = " << variable->GetName() << ".";
  1134. op_itor->second = variable; // replace parameter with variable
  1135. vars_[name] = variable;
  1136. }
  1137. }
  1138. return pred;
  1139. }
  1140. void DfGraphConvertor::SetOpInput(const OpAdapterPtr &adpt, const CNodePtr &node) {
  1141. OperatorPtr src = Convert(node);
  1142. int case_flag = 0;
  1143. auto &inputs = node->inputs();
  1144. size_t input_size = inputs.size();
  1145. if (case_input_handle_cache_.find(node.get()) != case_input_handle_cache_.end()) {
  1146. case_flag = 1;
  1147. input_size = case_input_handle_cache_[node.get()]->size() + 1;
  1148. }
  1149. for (size_t i = 1; i < input_size; i++) {
  1150. AnfNodePtr pred = nullptr;
  1151. if (case_flag != 0) {
  1152. pred = case_input_handle_cache_[node.get()]->at(i - 1);
  1153. } else {
  1154. pred = inputs[i];
  1155. }
  1156. pred = GetRealInputNode(node, pred);
  1157. if (pred == nullptr) {
  1158. continue;
  1159. }
  1160. int index = SizeToInt(i);
  1161. // find in out_hadnle_cache_ first
  1162. auto it = out_handle_cache_.find(pred.get());
  1163. if (it != out_handle_cache_.end()) {
  1164. int ret = adpt->setInput(src, index, it->second);
  1165. if (ret == 0) {
  1166. if (pred->isa<CNode>() && GetCNodeTargetFuncName(pred->cast<CNodePtr>()) == prim::kTupleGetItem) {
  1167. compute_sout_ << op_draw_name_[pred->cast<CNodePtr>()->input(1).get()] << " -> " << op_draw_name_[node.get()]
  1168. << ":" << i << endl;
  1169. } else if (pred->isa<Parameter>()) {
  1170. compute_sout_ << op_draw_name_[pred.get()] << " -> " << op_draw_name_[node.get()] << ":" << i << endl;
  1171. } else {
  1172. // don't draw anything.
  1173. MS_LOG(INFO) << "DRAW_GE_GRAPH: Shouldn't have this case.";
  1174. }
  1175. AddGraphConstInput(it->second.op);
  1176. }
  1177. } else if (tuple_out_handle_cache_.find(pred.get()) != tuple_out_handle_cache_.end()) {
  1178. SetTupleOpInput(adpt, node, pred, src, index);
  1179. } else {
  1180. auto op = Convert(pred);
  1181. int ret = adpt->setInput(src, index, op);
  1182. if (ret == 0) {
  1183. compute_sout_ << op_draw_name_[pred.get()] << " -> " << op_draw_name_[node.get()] << ":" << i << endl;
  1184. AddGraphConstInput(op);
  1185. }
  1186. }
  1187. }
  1188. }
  1189. void DfGraphConvertor::AddGraphConstInput(const OperatorPtr &op) {
  1190. if (op->GetOpType() == "Constant" || op->GetOpType() == "Const") {
  1191. graph_const_inputs_.push_back(op);
  1192. }
  1193. }
  1194. void DfGraphConvertor::SetNodeInput(const AnfNodePtr node) {
  1195. if (!node->isa<CNode>()) {
  1196. return;
  1197. }
  1198. if (op_cache_.find(node.get()) == op_cache_.end()) {
  1199. return;
  1200. }
  1201. auto cnode = node->cast<CNodePtr>();
  1202. OpAdapterPtr adpt = FindAdapter(cnode, training_);
  1203. if (adpt == nullptr) {
  1204. error_ = NOT_FOUND;
  1205. return;
  1206. }
  1207. // get Operator from op_cache_, use adapter to set Inputs
  1208. DfGraphConvertor::SetOpInput(adpt, cnode);
  1209. }
  1210. void DfGraphConvertor::ProcessSubgraph(AnfNodePtr node, const std::vector<AnfNodePtr> &inputs) {
  1211. if (!node->isa<CNode>() || GetCNodeFuncName(node->cast<CNodePtr>()) != "Partial") {
  1212. return;
  1213. }
  1214. auto graph_node = node->cast<CNodePtr>()->input(1)->cast<ValueNodePtr>();
  1215. FuncGraphPtr anf_graph = graph_node->value()->cast<FuncGraphPtr>();
  1216. DfGraphConvertor converter(anf_graph);
  1217. converter.use_inputs_ = true;
  1218. converter.inputs_ = inputs;
  1219. (void)converter.ConvertAllNode().BuildGraph();
  1220. std::string name = graph_node->ToString() + "_ge_graph.dot";
  1221. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG)) {
  1222. converter.DrawComputeGraph(name);
  1223. }
  1224. branches_map_[node.get()] = *(converter.df_graph_);
  1225. }
  1226. // Update GE op's shape and type info
  1227. void DfGraphConvertor::UpdateOpDesc(const AnfNodePtr node) {
  1228. if (nullptr == node || !node->isa<CNode>()) {
  1229. return;
  1230. }
  1231. if (op_cache_.find(node.get()) == op_cache_.end()) {
  1232. return;
  1233. }
  1234. OpAdapterPtr adpt = FindAdapter(node, training_);
  1235. if (adpt == nullptr) {
  1236. error_ = NOT_FOUND;
  1237. return;
  1238. }
  1239. // get Operator from op_cache_
  1240. OperatorPtr op = Convert(node);
  1241. adpt->updateOutputDesc(op, node->Shape(), node->Type(), node);
  1242. }
  1243. OperatorPtr DfGraphConvertor::Convert(const AnfNodePtr node) {
  1244. if (node == nullptr) {
  1245. MS_LOG(ERROR) << "node is nullptr";
  1246. error_ = NOT_FOUND;
  1247. return nullptr;
  1248. }
  1249. // find in cache
  1250. if (op_cache_.count(node.get())) {
  1251. return op_cache_[node.get()];
  1252. }
  1253. // do not convert primitive node, Load, UpdateState
  1254. if (IsValueNode<Primitive>(node) || IsPrimitiveCNode(node, prim::kPrimLoad) ||
  1255. IsPrimitiveCNode(node, prim::kPrimUpdateState)) {
  1256. return nullptr;
  1257. }
  1258. // convert a new one
  1259. if (node->isa<CNode>()) {
  1260. return ConvertCNode(node->cast<CNodePtr>());
  1261. }
  1262. if (node->isa<Parameter>()) {
  1263. return ConvertParameter(node);
  1264. }
  1265. if (node->isa<ValueNode>()) {
  1266. if (IsValueNode<Monad>(node)) {
  1267. return nullptr;
  1268. }
  1269. return ConvertValueNode(node->cast<ValueNodePtr>());
  1270. }
  1271. MS_LOG(ERROR) << "Invalid AnfNode";
  1272. error_ = INVALID_ARGUMENT;
  1273. return nullptr;
  1274. }
  1275. void DfGraphConvertor::ConvertMakeTuple(const CNodePtr node) {
  1276. std::shared_ptr<std::vector<OutHandler>> tuple_items = std::make_shared<std::vector<OutHandler>>();
  1277. // convert each tuple item to a OutHandler
  1278. for (size_t i = 1; i < node->inputs().size(); i++) {
  1279. AnfNodePtr item = node->input(i);
  1280. if (IsPrimitiveCNode(item, prim::kPrimLoad)) {
  1281. item = ParseLoadInput(item->cast<CNodePtr>());
  1282. }
  1283. OperatorPtr op = Convert(item);
  1284. if (op != nullptr) {
  1285. tuple_items->emplace_back(OutHandler(op, "", item));
  1286. } else if (out_handle_cache_.find(item.get()) != out_handle_cache_.end()) {
  1287. tuple_items->push_back(out_handle_cache_[item.get()]);
  1288. } else {
  1289. tuple_items->push_back(OutHandler(nullptr, "", item));
  1290. }
  1291. }
  1292. MS_LOG(DEBUG) << "ConvertMakeTuple: " << node.get() << " " << tuple_items->size();
  1293. tuple_out_handle_cache_[node.get()] = tuple_items;
  1294. }
  1295. AnfNodePtr DfGraphConvertor::TraceTupleGetItem(const CNodePtr &node, uint64_t *index) {
  1296. const int TUPLE_GET_ITEM_INDEX = 2;
  1297. if (node->inputs().size() < 3) { // "tuple_getitem" primitive must have 3 inputs
  1298. MS_LOG(EXCEPTION) << "length of inputs of TupleGetItem is less than 3";
  1299. }
  1300. auto index_node = node->inputs()[TUPLE_GET_ITEM_INDEX];
  1301. if (!index_node->isa<ValueNode>()) {
  1302. error_ = INVALID_ARGUMENT;
  1303. MS_LOG(EXCEPTION) << "can't convert get item with non-constant index";
  1304. }
  1305. *index = LongToUlong(GetValue<int64_t>(GetValueNode(index_node)));
  1306. return node->inputs()[1];
  1307. }
  1308. AnfNodePtr DfGraphConvertor::TraceDepend(const CNodePtr &node) {
  1309. auto cnode = node->cast<CNodePtr>();
  1310. if (cnode->inputs().size() < 3) { // "Depend" primitive have 3 inputs
  1311. MS_LOG(EXCEPTION) << "length of inputs of depend is less than 3";
  1312. }
  1313. return cnode->inputs()[1];
  1314. }
  1315. AnfNodePtr DfGraphConvertor::TraceMakeTuple(const CNodePtr &node, uint64_t index) {
  1316. if (index + 1 >= node->inputs().size()) {
  1317. MS_LOG(EXCEPTION) << "length of make_tuple is less than index: " << index;
  1318. }
  1319. return node->inputs()[index + 1];
  1320. }
  1321. OutHandler DfGraphConvertor::GetHandler(const AnfNodePtr &node, const std::stack<uint64_t> &index_stack,
  1322. AnfNode *const draw_index) {
  1323. if (node == nullptr) {
  1324. MS_LOG(ERROR) << "Get nullptr while trace real op";
  1325. return OutHandler(nullptr, "");
  1326. }
  1327. std::ostringstream ss;
  1328. ss << "op" << node.get();
  1329. if (index_stack.empty()) {
  1330. op_draw_name_[draw_index] = ss.str();
  1331. return OutHandler(Convert(node), "");
  1332. } else {
  1333. OpAdapterPtr adpt = FindAdapter(node, training_);
  1334. if (nullptr == adpt) {
  1335. MS_LOG(ERROR) << "Can not get node output as adpt is nullptr!";
  1336. error_ = NOT_FOUND;
  1337. return OutHandler(nullptr, "");
  1338. }
  1339. OperatorPtr op = Convert(node);
  1340. if (op == nullptr) {
  1341. error_ = NOT_FOUND;
  1342. MS_LOG(ERROR) << "Can not convert node for trace real op";
  1343. return OutHandler(nullptr, "");
  1344. }
  1345. op_draw_name_[draw_index] = ss.str();
  1346. return adpt->getOutput(Convert(node), UintToInt(index_stack.top()));
  1347. }
  1348. }
  1349. // get the real operator through maketuple tuple_getitem depend
  1350. OutHandler DfGraphConvertor::TraceRealOp(AnfNodePtr node) {
  1351. bool flag = IsPrimitiveCNode(node, prim::kPrimTupleGetItem) || IsPrimitiveCNode(node, prim::kPrimMakeTuple) ||
  1352. IsPrimitiveCNode(node, prim::kPrimDepend);
  1353. std::stack<uint64_t> index_stack;
  1354. auto draw_index = node.get();
  1355. while (flag) {
  1356. flag = false;
  1357. if (IsPrimitiveCNode(node, prim::kPrimTupleGetItem)) {
  1358. uint64_t index;
  1359. node = TraceTupleGetItem(node->cast<CNodePtr>(), &index);
  1360. index_stack.push(index);
  1361. flag = true;
  1362. } else if (IsPrimitiveCNode(node, prim::kPrimMakeTuple)) {
  1363. if (index_stack.empty()) {
  1364. MS_LOG(ERROR) << "TraceRealOp find a make_tuple node";
  1365. return OutHandler(nullptr, "");
  1366. } else {
  1367. node = TraceMakeTuple(node->cast<CNodePtr>(), index_stack.top());
  1368. index_stack.pop();
  1369. flag = true;
  1370. }
  1371. } else if (IsPrimitiveCNode(node, prim::kPrimDepend)) {
  1372. node = TraceDepend(node->cast<CNodePtr>());
  1373. flag = true;
  1374. }
  1375. }
  1376. return GetHandler(node, index_stack, draw_index);
  1377. }
  1378. void DfGraphConvertor::ConvertTupleGetItem(const CNodePtr node) {
  1379. auto handle = TraceRealOp(node);
  1380. if (handle.op == nullptr) {
  1381. MS_LOG(ERROR) << "Failed to trace tuple get item";
  1382. return;
  1383. }
  1384. out_handle_cache_[node.get()] = handle;
  1385. }
  1386. // Get the real op for tuple_getitem through make tuple, or depend
  1387. AnfNodePtr DfGraphConvertor::GetRealOpNode(AnfNodePtr node) {
  1388. const int TUPLE_GET_ITEM_INDEX = 2;
  1389. if (IsPrimitiveCNode(node, prim::kPrimTupleGetItem)) {
  1390. auto node_inputs = node->cast<CNodePtr>()->inputs();
  1391. if (node_inputs.size() != 3) { // "tuple_getitem" primitive must have 3 inputs
  1392. MS_LOG(ERROR) << "tuple get item node not correct!";
  1393. error_ = FAILED;
  1394. return node;
  1395. }
  1396. MS_EXCEPTION_IF_NULL(node_inputs[TUPLE_GET_ITEM_INDEX]);
  1397. if (!node_inputs[TUPLE_GET_ITEM_INDEX]->isa<ValueNode>()) {
  1398. error_ = INVALID_ARGUMENT;
  1399. MS_LOG(EXCEPTION) << "can't convert get item with non-constant index";
  1400. }
  1401. auto value_ptr = GetValueNode(node_inputs[TUPLE_GET_ITEM_INDEX])->cast<Int32ImmPtr>();
  1402. if (value_ptr == nullptr) {
  1403. MS_LOG(ERROR) << "Can not convert get item as value is nullptr!";
  1404. error_ = FAILED;
  1405. return node;
  1406. }
  1407. int64_t index = value_ptr->value();
  1408. // make_tuple apply inputs:make_tuple, [tuple_items,]
  1409. if (IsPrimitiveCNode(node_inputs[1], prim::kPrimMakeTuple)) {
  1410. auto tuple_inputs = node->cast<CNodePtr>()->inputs();
  1411. if (tuple_inputs.size() < IntToSize(index + 1)) {
  1412. MS_LOG(ERROR) << "make tuple input items node not correct! size:" << tuple_inputs.size()
  1413. << ", item index:" << index;
  1414. error_ = FAILED;
  1415. return node;
  1416. }
  1417. return GetRealOpNode(tuple_inputs[IntToSize(index + 1)]);
  1418. }
  1419. return GetRealOpNode(node_inputs[1]);
  1420. }
  1421. // depend apply inputs: depend,output,depended_node
  1422. if (IsPrimitiveCNode(node, prim::kPrimDepend)) {
  1423. auto depend_inputs = node->cast<CNodePtr>()->inputs();
  1424. if (depend_inputs.size() != 3) { // "Depend" primitive have 3 inputs
  1425. MS_LOG(ERROR) << "depend input items not correct";
  1426. error_ = FAILED;
  1427. return node;
  1428. }
  1429. return GetRealOpNode(depend_inputs[1]);
  1430. }
  1431. return node;
  1432. }
  1433. // convert the anf node to corresponding operator list
  1434. std::vector<OperatorPtr> DfGraphConvertor::ConvertDependNode(const AnfNodePtr node) {
  1435. if (IsPrimitiveCNode(node, prim::kPrimMakeTuple)) {
  1436. std::vector<OperatorPtr> op_lists;
  1437. auto node_inputs = node->cast<CNodePtr>()->inputs();
  1438. for (size_t index = 1; index < node_inputs.size(); index++) {
  1439. auto op = Convert(GetRealOpNode(node_inputs[index]));
  1440. if (op == nullptr) {
  1441. MS_LOG(ERROR) << "Convert control depend node to operator failed";
  1442. error_ = FAILED;
  1443. return std::vector<OperatorPtr>({});
  1444. }
  1445. op_lists.push_back(op);
  1446. }
  1447. return op_lists;
  1448. }
  1449. auto op = Convert(GetRealOpNode(node));
  1450. if (op == nullptr) {
  1451. MS_LOG(ERROR) << "Convert control depend node to operator failed";
  1452. error_ = FAILED;
  1453. return std::vector<OperatorPtr>({});
  1454. }
  1455. return std::vector<OperatorPtr>({op});
  1456. }
  1457. // get the anf node list for depend
  1458. std::vector<AnfNodePtr> DfGraphConvertor::GetDependNodes(const AnfNodePtr &node) {
  1459. std::vector<AnfNodePtr> nodes;
  1460. // for make tuple, should control depend on the tuple items
  1461. if (IsPrimitiveCNode(node, prim::kPrimMakeTuple)) {
  1462. auto node_inputs = node->cast<CNodePtr>()->inputs();
  1463. for (size_t index = 1; index < node_inputs.size(); index++) {
  1464. nodes.push_back(GetRealOpNode(node_inputs[index]));
  1465. }
  1466. return nodes;
  1467. }
  1468. // for parameter ,find the apply that used the parameter as the control depended node
  1469. if (node->isa<Parameter>()) {
  1470. auto uses = node->func_graph()->manager()->node_users()[node];
  1471. for (auto &use : uses) {
  1472. auto use_node = use.first;
  1473. if ((use_node->isa<CNode>()) && (!IsPrimitiveCNode(use_node, prim::kPrimControlDepend))) {
  1474. nodes.push_back(GetRealOpNode(use_node));
  1475. }
  1476. }
  1477. return nodes;
  1478. }
  1479. nodes.push_back(GetRealOpNode(node));
  1480. return nodes;
  1481. }
  1482. void DfGraphConvertor::DrawControlDepend(const AnfNodePtr &src_node, const AnfNodePtr &dest_node) {
  1483. #ifdef DRAW_GE_GRAPH
  1484. auto src_depend_nodes = GetDependNodes(src_node);
  1485. auto dst_depend_nodes = GetDependNodes(dest_node);
  1486. if (src_depend_nodes.size() == 1 && dst_depend_nodes.size() > 1) {
  1487. for (auto &item : dst_depend_nodes) {
  1488. compute_sout_ << op_draw_name_[src_depend_nodes[0].get()] << " -> " << op_draw_name_[item.get()]
  1489. << "[style=\"dotted\"]" << endl;
  1490. }
  1491. } else if (src_depend_nodes.size() > 1 && dst_depend_nodes.size() == 1) {
  1492. for (auto &item : src_depend_nodes) {
  1493. compute_sout_ << op_draw_name_[item.get()] << " -> " << op_draw_name_[dst_depend_nodes[0].get()]
  1494. << "[style=\"dotted\"]" << endl;
  1495. }
  1496. } else if (src_depend_nodes.size() == 1 && dst_depend_nodes.size() == 1) {
  1497. compute_sout_ << op_draw_name_[src_depend_nodes[0].get()] << " -> " << op_draw_name_[dst_depend_nodes[0].get()]
  1498. << "[style=\"dotted\"]" << endl;
  1499. }
  1500. #endif
  1501. }
  1502. void DfGraphConvertor::GetDependOnParameterUse(const CNodePtr &node, const AnfNodePtr &src_node,
  1503. const AnfNodePtr &dest_node,
  1504. const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  1505. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list) {
  1506. if (src_node->isa<Parameter>()) {
  1507. auto uses = node->func_graph()->manager()->node_users()[src_node];
  1508. for (auto &use : uses) {
  1509. auto use_node = use.first;
  1510. if ((use_node->isa<CNode>()) && (!IsPrimitiveCNode(use_node, prim::kPrimControlDepend)) &&
  1511. (!IsPrimitiveCNode(use_node, prim::kPrimMakeTuple))) {
  1512. auto converted_list = ConvertDependNode(use_node);
  1513. src_ops_list->insert(src_ops_list->end(), converted_list.begin(), converted_list.end());
  1514. }
  1515. }
  1516. }
  1517. if (dest_node->isa<Parameter>()) {
  1518. auto uses = node->func_graph()->manager()->node_users()[dest_node];
  1519. for (auto &use : uses) {
  1520. auto use_node = use.first;
  1521. if ((use_node->isa<CNode>()) && (!IsPrimitiveCNode(use_node, prim::kPrimControlDepend)) &&
  1522. (!IsPrimitiveCNode(use_node, prim::kPrimMakeTuple))) {
  1523. auto converted_list = ConvertDependNode(use_node);
  1524. dst_ops_list->insert(dst_ops_list->end(), converted_list.begin(), converted_list.end());
  1525. }
  1526. }
  1527. }
  1528. }
  1529. bool DfGraphConvertor::GetControlDependList(const CNodePtr &node,
  1530. const std::shared_ptr<std::vector<OperatorPtr>> &src_ops_list,
  1531. const std::shared_ptr<std::vector<OperatorPtr>> &dst_ops_list) {
  1532. const int CONTROL_DEPEND_INDEX = 0;
  1533. const int SRC_NODE_INDEX = 1;
  1534. const int DEST_NODE_INDEX = 2;
  1535. const int DEPEND_MODE_NORMAL_USE = 0;
  1536. const int DEPEND_MODE_ON_PARAMETER_USE = 1;
  1537. auto node_inputs = node->inputs();
  1538. if (node_inputs.size() <= DEST_NODE_INDEX) {
  1539. MS_LOG(WARNING) << "Control depend node input size error";
  1540. return false;
  1541. }
  1542. auto src_node = node_inputs[SRC_NODE_INDEX];
  1543. auto dest_node = node_inputs[DEST_NODE_INDEX];
  1544. if ((src_node == nullptr) || (dest_node == nullptr)) {
  1545. MS_LOG(ERROR) << "Control depend node miss src or dest node";
  1546. error_ = FAILED;
  1547. return false;
  1548. }
  1549. AnfNodePtr fn = node_inputs[CONTROL_DEPEND_INDEX];
  1550. PrimitivePtr prim_ptr = GetValueNode<PrimitivePtr>(fn);
  1551. ValuePtr mode_ptr = prim_ptr->GetAttr("depend_mode");
  1552. int depend_mode = DEPEND_MODE_NORMAL_USE;
  1553. if (mode_ptr != nullptr) {
  1554. auto mode_int = mode_ptr->cast<Int64ImmPtr>();
  1555. MS_EXCEPTION_IF_NULL(mode_int);
  1556. depend_mode = mode_int->value();
  1557. MS_LOG(DEBUG) << "depend_mode = " << depend_mode;
  1558. }
  1559. if (depend_mode == DEPEND_MODE_ON_PARAMETER_USE) {
  1560. GetDependOnParameterUse(node, src_node, dest_node, src_ops_list, dst_ops_list);
  1561. }
  1562. if (src_node->isa<CNode>()) {
  1563. auto converted_list = ConvertDependNode(src_node);
  1564. src_ops_list->insert(src_ops_list->end(), converted_list.begin(), converted_list.end());
  1565. }
  1566. if (dest_node->isa<CNode>()) {
  1567. auto converted_list = ConvertDependNode(dest_node);
  1568. dst_ops_list->insert(dst_ops_list->end(), converted_list.begin(), converted_list.end());
  1569. }
  1570. if (src_ops_list->empty() || dst_ops_list->empty()) {
  1571. MS_LOG(DEBUG) << "Control depend node's src or dest node is not a CNode, ignore it";
  1572. error_ = SUCCESS;
  1573. }
  1574. return true;
  1575. }
  1576. void DfGraphConvertor::ConvertControlDependNode(const CNodePtr node) {
  1577. const int SRC_NODE_INDEX = 1;
  1578. const int DEST_NODE_INDEX = 2;
  1579. if (control_depend_cache_.find(node.get()) != control_depend_cache_.end()) {
  1580. return;
  1581. }
  1582. auto node_inputs = node->inputs();
  1583. if (node_inputs.size() <= DEST_NODE_INDEX) {
  1584. MS_LOG(WARNING) << "Control depend node input size error";
  1585. return;
  1586. }
  1587. auto src_node = node_inputs[SRC_NODE_INDEX];
  1588. auto dest_node = node_inputs[DEST_NODE_INDEX];
  1589. if ((src_node == nullptr) || (dest_node == nullptr)) {
  1590. MS_LOG(ERROR) << "Control depend node miss src or dest node";
  1591. error_ = FAILED;
  1592. return;
  1593. }
  1594. std::shared_ptr<std::vector<OperatorPtr>> src_ops_list = std::make_shared<std::vector<OperatorPtr>>();
  1595. std::shared_ptr<std::vector<OperatorPtr>> dst_ops_list = std::make_shared<std::vector<OperatorPtr>>();
  1596. if (!GetControlDependList(node, src_ops_list, dst_ops_list)) {
  1597. MS_LOG(ERROR) << "Get depend list failed";
  1598. error_ = FAILED;
  1599. return;
  1600. }
  1601. std::vector<ControlEdge> control_edges;
  1602. if (src_ops_list->size() == 1 && dst_ops_list->size() > 1) {
  1603. (void)std::transform(dst_ops_list->begin(), dst_ops_list->end(), std::back_inserter(control_edges),
  1604. [src_ops_list](const OperatorPtr &op) -> ControlEdge {
  1605. return {(*src_ops_list)[0], op};
  1606. });
  1607. } else if (src_ops_list->size() > 1 && dst_ops_list->size() == 1) {
  1608. (void)std::transform(src_ops_list->begin(), src_ops_list->end(), std::back_inserter(control_edges),
  1609. [dst_ops_list](const OperatorPtr &op) -> ControlEdge {
  1610. return {op, (*dst_ops_list)[0]};
  1611. });
  1612. } else if (src_ops_list->size() == 1 && dst_ops_list->size() == 1) {
  1613. control_edges.push_back({(*src_ops_list)[0], (*dst_ops_list)[0]});
  1614. } else if (src_ops_list->empty() || dst_ops_list->empty()) {
  1615. MS_LOG(DEBUG) << "Depend list of src or dst is empty, ignore it";
  1616. } else {
  1617. MS_LOG(ERROR) << "Convert control depend node to operator failed, depend src:" << src_ops_list->size()
  1618. << " -> dst:" << dst_ops_list->size();
  1619. error_ = FAILED;
  1620. return;
  1621. }
  1622. control_depend_cache_[node.get()] = control_edges;
  1623. #ifdef DRAW_GE_GRAPH
  1624. DrawControlDepend(src_node, dest_node);
  1625. #endif
  1626. }
  1627. bool DfGraphConvertor::CheckCNode(const std::string &name, const CNodePtr node) {
  1628. // ignore apply node of return
  1629. if (name == "" || name == prim::kPrimReturn->name() || name == prim::kPrimDepend->name() ||
  1630. name == prim::kPrimSwitchLayer->name() || name == prim::kPrimPartial->name()) {
  1631. return false;
  1632. }
  1633. // make_tuple is used for a dynamic_input, convert it to a vector of OutHandlers
  1634. if (name == prim::kPrimMakeTuple->name()) {
  1635. ConvertMakeTuple(node);
  1636. return false;
  1637. }
  1638. // As for nodes with multi outputs, convert tuple_getitem to OutHandle
  1639. if (name == prim::kPrimTupleGetItem->name()) {
  1640. ConvertTupleGetItem(node);
  1641. return false;
  1642. }
  1643. // ControlDepend
  1644. if (name == prim::kPrimControlDepend->name()) {
  1645. ConvertControlDependNode(node);
  1646. return false;
  1647. }
  1648. return true;
  1649. }
  1650. OperatorPtr DfGraphConvertor::ConvertCNode(const CNodePtr node) {
  1651. std::string name = GetCNodeTargetFuncName(node);
  1652. if (!CheckCNode(name, node)) {
  1653. return nullptr;
  1654. }
  1655. // get corresponding OpAdapter
  1656. OpAdapterPtr adpt = FindAdapter(node, training_);
  1657. if (adpt == nullptr) {
  1658. error_ = NOT_FOUND;
  1659. return nullptr;
  1660. }
  1661. // get operator
  1662. OperatorPtr op = nullptr;
  1663. auto it_op = op_cache_.find(node.get());
  1664. if (it_op != op_cache_.end()) {
  1665. op = it_op->second;
  1666. } else {
  1667. op = adpt->generate(node);
  1668. }
  1669. // set attribute for primitive
  1670. (void)adpt->setAttr(op, node);
  1671. // add into cache
  1672. (void)op_cache_.insert(std::make_pair(node.get(), op));
  1673. DrawCNode(node, adpt);
  1674. return op_cache_[node.get()];
  1675. }
  1676. OperatorPtr DfGraphConvertor::ConvertParameter(const AnfNodePtr node) {
  1677. // convert Parameter in ANF to variable in DataFlow
  1678. auto op = FindAdapter(node, training_)->generate(node);
  1679. op_cache_[node.get()] = op;
  1680. // build index for parameter using name
  1681. std::string name = std::static_pointer_cast<Parameter>(node)->name();
  1682. params_[name] = node;
  1683. std::ostringstream ss;
  1684. ss << "op" << node.get();
  1685. op_draw_name_[node.get()] = ss.str();
  1686. compute_sout_ << ss.str() << "[shape=octagon, label=\"" << name << "\"]" << endl;
  1687. return op_cache_[node.get()];
  1688. }
  1689. Status DfGraphConvertor::TryConvertValueNodeToMultiConst(const ValueNodePtr node) {
  1690. MS_EXCEPTION_IF_NULL(node);
  1691. ValuePtr value = node->value();
  1692. MS_EXCEPTION_IF_NULL(value);
  1693. if (!value->isa<ValueList>() && !value->isa<ValueTuple>()) {
  1694. return FAILED;
  1695. }
  1696. auto vec = value->isa<ValueTuple>() ? value->cast<ValueTuplePtr>()->value() : value->cast<ValueListPtr>()->value();
  1697. if (vec.empty()) {
  1698. return FAILED;
  1699. }
  1700. std::shared_ptr<std::vector<OutHandler>> tuple_items = std::make_shared<std::vector<OutHandler>>();
  1701. for (size_t i = 0; i < vec.size(); i++) {
  1702. MS_EXCEPTION_IF_NULL(vec[i]);
  1703. if (vec[i]->isa<MeTensor>()) {
  1704. GeTensorPtr ge_tensor = transform::TransformUtil::ConvertTensor(vec[i]->cast<MeTensorPtr>(), kOpFormat_NCHW);
  1705. auto const_op = std::make_shared<Constant>(node->fullname_with_scope() + "/const/inputs/" + std::to_string(i));
  1706. (void)const_op->set_attr_value(*ge_tensor);
  1707. (void)const_op->update_output_desc_y(ge_tensor->GetTensorDesc());
  1708. tuple_items->emplace_back(OutHandler(const_op, ""));
  1709. } else {
  1710. return FAILED;
  1711. }
  1712. }
  1713. if (tuple_items->empty()) {
  1714. return FAILED;
  1715. }
  1716. tuple_out_handle_cache_[node.get()] = tuple_items;
  1717. return SUCCESS;
  1718. }
  1719. OperatorPtr DfGraphConvertor::ConvertValueNode(const ValueNodePtr node) {
  1720. // convert valuenode in ANF to Const in DataFlow
  1721. // find paramerte referenced by SymbolicKeyInstance of valuenode
  1722. std::ostringstream ss;
  1723. ss << "op" << node.get();
  1724. op_draw_name_[node.get()] = ss.str();
  1725. compute_sout_ << ss.str() << "[label= \"" << node->value()->ToString() << "\" shape=ellipse]" << endl;
  1726. if (TryConvertValueNodeToMultiConst(node) == SUCCESS) {
  1727. MS_LOG(INFO) << "Convert value node to multi Constant OP success";
  1728. return nullptr;
  1729. }
  1730. OpAdapterPtr adpt = FindAdapter(node, training_);
  1731. if (adpt == nullptr) {
  1732. error_ = NOT_FOUND;
  1733. return nullptr;
  1734. }
  1735. auto op = adpt->generate(node);
  1736. // set const's attrs
  1737. if (adpt->setAttr(op, "value", node->value()) != 0) {
  1738. MS_LOG(WARNING) << "set attr value for const failed";
  1739. }
  1740. #if (defined ENABLE_GE)
  1741. auto const_op = std::static_pointer_cast<Constant>(op);
  1742. if (const_op == nullptr) {
  1743. MS_LOG(ERROR) << "Get Constant operator failed";
  1744. return nullptr;
  1745. }
  1746. auto ge_tensor = const_op->get_attr_value();
  1747. auto ge_desc = ge_tensor.GetTensorDesc();
  1748. (void)const_op->update_output_desc_y(ge_desc);
  1749. #endif
  1750. op_cache_[node.get()] = op;
  1751. return op_cache_[node.get()];
  1752. }
  1753. void DfGraphConvertor::DrawCNode(const CNodePtr node, const OpAdapterPtr adpt) {
  1754. if (nullptr == adpt || nullptr == node) {
  1755. MS_LOG(ERROR) << "Failed to draw apply node as adpt or node is nullptr!";
  1756. return;
  1757. }
  1758. std::ostringstream ss;
  1759. ss << "op" << node.get();
  1760. op_draw_name_[node.get()] = ss.str();
  1761. compute_sout_ << ss.str() << "[label=<";
  1762. compute_sout_ << "<table border='1' cellborder='1'>" << endl;
  1763. auto input_map = adpt->getInputMap();
  1764. auto dyn_input_map = adpt->getDynInputMap();
  1765. if (input_map.size() + dyn_input_map.size() > 0) {
  1766. compute_sout_ << "<tr>";
  1767. for (auto &it : input_map) {
  1768. compute_sout_ << "<td port='" << it.first << "'>" << it.second.name << "</td>";
  1769. }
  1770. for (auto &it : dyn_input_map) {
  1771. compute_sout_ << "<td port='" << it.first << "'>" << it.second.name << "</td>";
  1772. }
  1773. compute_sout_ << "</tr>" << endl;
  1774. }
  1775. compute_sout_ << "<tr><td colspan=\"" << (input_map.size() + dyn_input_map.size()) << "\">\"" << node->ToString()
  1776. << ":" << GetCNodeTargetFuncName(node) << "\"</td></tr>" << endl;
  1777. // print attrs' values
  1778. auto atts = adpt->GetAttrsFromDrawGraph();
  1779. for (auto &it : atts) {
  1780. compute_sout_ << "<tr><td colspan=\"" << (input_map.size() + dyn_input_map.size()) << "\">\"" << it
  1781. << "\"</td></tr>";
  1782. }
  1783. adpt->clearAttrVect();
  1784. compute_sout_ << "</table>> shape=plaintext]" << endl;
  1785. }
  1786. void DfGraphConvertor::RegisterAdapter(const std::string &name, OpAdapterPtr adpt) {
  1787. OpAdapterMap::get()[name] = std::make_shared<OpAdapterDesc>(adpt);
  1788. }
  1789. void DfGraphConvertor::RegisterAdapter(const std::string &name, OpAdapterPtr train_adpt, OpAdapterPtr infer_adpt) {
  1790. OpAdapterMap::get()[name] = std::make_shared<OpAdapterDesc>(train_adpt, infer_adpt);
  1791. }
  1792. } // namespace transform
  1793. } // namespace mindspore