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

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