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.

transform.cc 19 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "vm/transform.h"
  19. #include <algorithm>
  20. #include <map>
  21. #include <queue>
  22. #include <string>
  23. #include <vector>
  24. #include "abstract/abstract_value.h"
  25. #ifdef ENABLE_GE
  26. #include "transform/graph_ir/convert.h"
  27. #endif
  28. #include "ir/graph_utils.h"
  29. #include "utils/ms_context.h"
  30. #include "debug/trace.h"
  31. #include "debug/anf_ir_dump.h"
  32. namespace mindspore {
  33. namespace compile {
  34. using mindspore::abstract::AbstractFunction;
  35. using mindspore::abstract::AbstractFunctionPtr;
  36. using PrimTypePair = std::pair<PrimitivePtr, AbstractFunctionPtr>;
  37. using MapPrimTypeFuncGraph = std::map<PrimTypePair, FuncGraphPtr>;
  38. using TypedPrimitiveAbstractClosurePtr = std::shared_ptr<abstract::TypedPrimitiveAbstractClosure>;
  39. std::vector<PrimitivePtr> nonlinear_ops = {prim::kPrimReturn, prim::kPrimPartial, prim::kPrimSwitch,
  40. prim::kPrimMakeTuple, prim::kPrimBpropCut};
  41. const std::vector<PrimitivePtr> &GetMsNonlinearOps() {
  42. static const std::vector<PrimitivePtr> ms_nonlinear_ops = {prim::kPrimReturn, prim::kPrimPartial,
  43. prim::kPrimSwitch, prim::kPrimMakeTuple,
  44. prim::kPrimBpropCut, prim::kPrimSwitchLayer};
  45. return ms_nonlinear_ops;
  46. }
  47. CompileGraph::CompileGraph(const BackendPtr &backend, const std::vector<PrimitivePtr> &cut_list) : backend_(backend) {
  48. MS_EXCEPTION_IF_NULL(backend_);
  49. lin_convert_ = backend_->convert_fn();
  50. if (lin_convert_ == nullptr) {
  51. MS_LOG(EXCEPTION) << "Attribute 'lin_convert' is null.: " << backend->name();
  52. }
  53. graph_partition_ = std::make_shared<GraphPartition>(cut_list, backend->name());
  54. }
  55. // Push the value node on the stack.
  56. void CompileGraph::Push(const AnfNodePtr &node) {
  57. MS_EXCEPTION_IF_NULL(node);
  58. if (slots_.count(node) > 0) {
  59. MS_LOG(WARNING) << "Push failed node in slots:" << node->DebugString()
  60. << " NodeInfo: " << trace::GetDebugInfo(node->debug_info());
  61. return;
  62. }
  63. MS_LOG(DEBUG) << "Push node: " << node->DebugString(true) << " height_: " << height_
  64. << " is parameter: " << node->isa<Parameter>();
  65. slots_[node] = height_;
  66. set_height(height_ + 1);
  67. }
  68. void CompileGraph::AddInst(const Instruction &inst, const int64_t &arg) {
  69. VectorRef args;
  70. args.push_back(arg);
  71. AddInst(inst, args);
  72. }
  73. void CompileGraph::AddInst(const Instruction &inst, const ValuePtr &arg) {
  74. VectorRef args;
  75. args.push_back(arg);
  76. AddInst(inst, args);
  77. }
  78. void CompileGraph::AddInst(const Instruction &inst, const VectorRef &args) {
  79. inst_.push_back(std::make_pair(inst, args));
  80. }
  81. // Gets the stack reference for the node value. If the node is a constant,
  82. // it may actually cause the push in to not be mentioned before.
  83. int64_t CompileGraph::Ref(const AnfNodePtr &node) {
  84. MS_EXCEPTION_IF_NULL(node);
  85. MS_LOG(DEBUG) << "Start Ref node " << node->DebugString(true) << " height_: " << height_;
  86. if (slots_.count(node) == 0 && node->isa<ValueNode>()) {
  87. if (IsValueNode<FuncGraph>(node)) {
  88. MS_LOG(DEBUG) << "Push graph.";
  89. AddInst(Instruction::kGraph, GetValueNode(node));
  90. } else {
  91. MS_LOG(DEBUG) << "Push.";
  92. if (IsValueNode<Primitive>(node)) {
  93. MS_LOG(EXCEPTION) << "must not be primitive in here NodeInfo: " << trace::GetDebugInfo(node->debug_info());
  94. } else {
  95. AddInst(Instruction::kPush, GetValueNode(node));
  96. }
  97. }
  98. Push(node);
  99. }
  100. MS_LOG(DEBUG) << "End Ref node end height_: " << height_ << ", slots: " << slots_[node]
  101. << ", return: " << slots_[node] - height_;
  102. return slots_[node] - height_;
  103. }
  104. // Make sure the value of node is at the top of the stack.
  105. void CompileGraph::AddInput(const AnfNodePtr &node) {
  106. MS_EXCEPTION_IF_NULL(node);
  107. if (slots_.count(node) == 0) {
  108. MS_LOG(DEBUG) << "Input node is null " << node->DebugString(true);
  109. (void)Ref(node);
  110. return;
  111. }
  112. AddInst(Instruction::kInput, Ref(node));
  113. set_height(height_ + 1);
  114. }
  115. // Call back effect in stack
  116. void CompileGraph::Ret(int64_t nargs) { set_height(height_ - nargs); }
  117. void CompileGraph::PushParameters(const FuncGraphPtr &graph) {
  118. MS_EXCEPTION_IF_NULL(graph);
  119. std::vector<AnfNodePtr> parameters = graph->parameters();
  120. for (size_t i = parameters.size(); i != 0; i--) {
  121. Push(parameters[i - 1]);
  122. MS_LOG(DEBUG) << "Push parameter " << i - 1 << ": " << parameters[i - 1]->DebugString(true);
  123. }
  124. }
  125. int64_t CompileGraph::LinConvert(const FuncGraphPtr &graph, const GraphSegmentPtr &segment, const std::string &target) {
  126. MS_EXCEPTION_IF_NULL(segment);
  127. MS_LOG(DEBUG) << "LinConvert start";
  128. LinConvertResult result;
  129. result = lin_convert_(segment, target);
  130. if (result.run == nullptr) {
  131. MS_LOG(ERROR) << "LinConvert failed";
  132. return RET_FAILED;
  133. }
  134. if (!(*result.run)) {
  135. if (result.inputs.size() != result.outputs.size()) {
  136. MS_EXCEPTION_IF_NULL(graph);
  137. MS_LOG(EXCEPTION) << "must inputs equal outputs NodeInfo: " << trace::GetDebugInfo(graph->debug_info());
  138. } else {
  139. size_t size = result.inputs.size();
  140. for (size_t i = 0; i < size; i++) {
  141. Tie(result.inputs[i], result.outputs[i]);
  142. }
  143. return RET_CONTINUE;
  144. }
  145. }
  146. AddExternal(result);
  147. return RET_SUCCESS;
  148. }
  149. int64_t CompileGraph::InterpretNode(const FuncGraphPtr &graph, const CNodePtr &node) {
  150. MS_EXCEPTION_IF_NULL(node);
  151. MS_LOG(DEBUG) << "Interpret node: " << node->DebugString(true);
  152. std::vector<AnfNodePtr> node_inputs = node->inputs();
  153. if (node_inputs.empty()) {
  154. MS_LOG(EXCEPTION) << "The node->inputs() is empty";
  155. }
  156. AnfNodePtr fn = node_inputs[0];
  157. if (IsValueNode<Primitive>(fn)) {
  158. PrimitivePtr value = GetValueNode<PrimitivePtr>(fn);
  159. MS_LOG(DEBUG) << "The fn is primitive " << (*value).name();
  160. for (size_t i = node_inputs.size() - 1; i > 0; i--) {
  161. AddInput(node->input(i));
  162. }
  163. if (IsPrimitive(fn, prim::kPrimReturn)) {
  164. AddReturn(node);
  165. return RET_BREAK;
  166. }
  167. if (IsPrimitive(fn, prim::kPrimPartial)) {
  168. AddPartial(node);
  169. } else if (IsPrimitive(fn, prim::kPrimSwitch)) {
  170. AddSwitch(node);
  171. } else if (IsPrimitive(fn, prim::kPrimSwitchLayer)) {
  172. AddSwitchLayer(node);
  173. } else if (IsPrimitive(fn, prim::kPrimMakeTuple)) {
  174. AddMakeTuple(node);
  175. } else {
  176. AddPrimitive(node, value);
  177. }
  178. } else {
  179. int64_t ret = AddCall(graph, node);
  180. if (ret == RET_BREAK) {
  181. return ret;
  182. }
  183. }
  184. Push(node);
  185. return RET_SUCCESS;
  186. }
  187. bool CompileGraph::Compile(const FuncGraphPtr &graph) {
  188. MS_LOG(DEBUG) << "Start split graph";
  189. MS_EXCEPTION_IF_NULL(graph);
  190. MS_EXCEPTION_IF_NULL(graph_partition_);
  191. auto segments = graph_partition_->Partition(graph);
  192. MS_LOG(DEBUG) << "Split nodes size:" << segments.size();
  193. for (auto &segment : segments) {
  194. MS_EXCEPTION_IF_NULL(segment);
  195. int64_t ret = RET_SUCCESS;
  196. if (!segment->is_cut_) {
  197. MS_LOG(DEBUG) << "Start a extern LinConvert";
  198. if (segment->nodes_.size() > 0) {
  199. std::string cur_target = GetCNodeTarget(segment->nodes_[0]);
  200. ret = LinConvert(graph, segment, cur_target);
  201. } else {
  202. ret = LinConvert(graph, segment);
  203. }
  204. MS_LOG(DEBUG) << "End a extern LinConvert";
  205. if (ret == RET_FAILED) {
  206. return false;
  207. }
  208. if (ret == RET_CONTINUE) {
  209. continue;
  210. }
  211. } else {
  212. MS_LOG(DEBUG) << "Start a cut node";
  213. auto &cut_node = segment->nodes_[0];
  214. if (!cut_node->isa<CNode>()) {
  215. MS_LOG(EXCEPTION) << "must be anfnode here NodeInfo: " << trace::GetDebugInfo(graph->debug_info());
  216. }
  217. CNodePtr node = cut_node->cast<CNodePtr>();
  218. ret = InterpretNode(graph, node);
  219. MS_LOG(DEBUG) << "End a cut node";
  220. if (ret == RET_BREAK) {
  221. break;
  222. }
  223. }
  224. }
  225. MS_LOG(DEBUG) << "End split graph";
  226. return true;
  227. }
  228. InstSet CompileGraph::Run(const FuncGraphPtr &graph) {
  229. MS_EXCEPTION_IF_NULL(graph);
  230. Reset();
  231. PushParameters(graph);
  232. int64_t param_height = height_;
  233. MS_LOG(DEBUG) << "'param_height': " << height_ << " to split graph: " << graph->get_return()->DebugString(true);
  234. if (!Compile(graph)) {
  235. return inst_;
  236. }
  237. AddPadStack(param_height);
  238. auto ret = inst_;
  239. Reset();
  240. return ret;
  241. }
  242. void CompileGraph::AddPadStack(int64_t param_height) {
  243. int64_t stack_sizes = max_height_ - param_height;
  244. MS_LOG(DEBUG) << "Pad stack max_height_:" << max_height_ << " param:" << param_height
  245. << " need_stack:" << stack_sizes;
  246. if (stack_sizes > 0) {
  247. VectorRef need_stacks({stack_sizes});
  248. (void)inst_.insert(inst_.begin(), std::make_pair(Instruction::kPadStack, need_stacks));
  249. }
  250. }
  251. void CompileGraph::AddTailCall(const AnfNodePtr &fn, size_t size) {
  252. VectorRef args;
  253. args.emplace_back(Ref(fn));
  254. args.emplace_back(height_);
  255. args.emplace_back(static_cast<int64_t>(size - 1));
  256. MS_LOG(DEBUG) << "Tail call:" << Ref(fn) << ", " << height_ << ", " << size - 1;
  257. AddInst(Instruction::kTailCall, args);
  258. }
  259. void CompileGraph::AddPartial(const CNodePtr &node) {
  260. auto inputs = node->inputs();
  261. VectorRef args;
  262. auto fn = inputs[1];
  263. if (!IsValueNode<FuncGraph>(fn)) {
  264. MS_LOG(EXCEPTION) << "The type of 1st input of node must be FuncGraph";
  265. }
  266. for (size_t i = 1; i < inputs.size(); i++) {
  267. args.emplace_back(Ref(inputs[i]));
  268. }
  269. AddInst(Instruction::kPartial, args);
  270. }
  271. void CompileGraph::AddMakeTuple(const CNodePtr &node) {
  272. auto inputs = node->inputs();
  273. VectorRef args;
  274. for (size_t i = 1; i < inputs.size(); i++) {
  275. args.emplace_back(Ref(inputs[i]));
  276. }
  277. AddInst(Instruction::kTuple, args);
  278. }
  279. void CompileGraph::AddSwitch(const CNodePtr &node) {
  280. auto inputs = node->inputs();
  281. if (inputs.size() < 4) {
  282. MS_LOG(EXCEPTION) << "Length of inputs of primitive " << prim::kPrimSwitch->name() << " is less than 4";
  283. }
  284. VectorRef args;
  285. args.emplace_back(Ref(inputs[1]));
  286. args.emplace_back(Ref(inputs[2]));
  287. args.emplace_back(Ref(inputs[3]));
  288. AddInst(Instruction::kSwitch, args);
  289. }
  290. void CompileGraph::AddSwitchLayer(const CNodePtr &node) {
  291. auto inputs = node->inputs();
  292. if (inputs.size() != 3) {
  293. MS_LOG(EXCEPTION) << "Switch layer must have index and branches.";
  294. }
  295. VectorRef args;
  296. args.emplace_back(Ref(inputs[1]));
  297. args.emplace_back(Ref(inputs[2]));
  298. AddInst(Instruction::kSwitchLayer, args);
  299. }
  300. void CompileGraph::AddReturn(const CNodePtr &node) {
  301. VectorRef args;
  302. args.emplace_back(Ref(node->input(1)));
  303. args.emplace_back(height_);
  304. AddInst(Instruction::kReturn, args);
  305. }
  306. void CompileGraph::AddPrimitive(const CNodePtr &node, const PrimitivePtr &prim) {
  307. auto inputs = node->inputs();
  308. VectorRef args;
  309. args.push_back(prim);
  310. for (size_t i = 1; i < inputs.size(); i++) {
  311. args.emplace_back(Ref(inputs[i]));
  312. }
  313. AddInst(Instruction::kPrim, args);
  314. }
  315. int64_t CompileGraph::AddCall(const FuncGraphPtr &graph, const CNodePtr &node) {
  316. auto inputs = node->inputs();
  317. AnfNodePtr fn = inputs[0];
  318. (void)Ref(fn);
  319. size_t size = inputs.size();
  320. for (size_t i = size - 1; i > 0; i--) {
  321. AddInput(inputs[i]);
  322. }
  323. if (node == graph->output()) {
  324. AddTailCall(fn, size);
  325. return RET_BREAK;
  326. }
  327. MS_LOG(DEBUG) << "Call:" << Ref(fn) << ", " << height_ << ", " << size - 1;
  328. AddInst(Instruction::kCall, Ref(fn));
  329. Ret(static_cast<int64_t>(size - 1));
  330. return RET_SUCCESS;
  331. }
  332. void CompileGraph::AddExternal(const LinConvertResult &result) {
  333. VectorRef args;
  334. args.push_back(result.run);
  335. args.push_back(result.simu_run);
  336. size_t size = result.inputs.size();
  337. for (size_t i = 0; i < size; i++) {
  338. args.emplace_back(Ref(result.inputs[i]));
  339. }
  340. AddInst(Instruction::kExternal, args);
  341. for (auto &out : result.outputs) {
  342. Push(out);
  343. }
  344. }
  345. void TraverseGraphMap(
  346. const FuncGraphManagerPtr &manager_ptr, FuncGraphTransaction *const tr, const FuncGraphSet &fgs,
  347. const std::function<std::shared_ptr<FuncGraph>(const PrimitivePtr, const AbstractFunctionPtr)> &get_prim_graph) {
  348. MS_EXCEPTION_IF_NULL(manager_ptr);
  349. MS_EXCEPTION_IF_NULL(tr);
  350. for (const auto &fg : fgs) {
  351. for (const auto &ct_any : fg->value_nodes()) {
  352. AnfNodePtr const_primitive_node = ct_any.first;
  353. if (const_primitive_node != nullptr && IsValueNode<Primitive>(const_primitive_node)) {
  354. auto users = manager_ptr->node_users()[const_primitive_node];
  355. for (auto &use : users) {
  356. CNodePtr node = use.first->cast<CNodePtr>();
  357. MS_EXCEPTION_IF_NULL(node);
  358. if (node->func_graph() != fg) {
  359. continue;
  360. }
  361. int64_t key = use.second;
  362. if (key != 0) {
  363. MS_EXCEPTION_IF_NULL(node->input(0));
  364. bool key_is_const = node->input(0)->isa<ValueNode>();
  365. PrimitivePtr value = GetValueNode<PrimitivePtr>(node->input(0));
  366. if (value != nullptr) {
  367. bool is_prim_array_map = !(prim::kPrimArrayMap->name().compare(value->name()));
  368. bool is_prim_array_reduce = !(prim::kPrimArrayReduce->name().compare(value->name()));
  369. if (key == 1 && key_is_const && (is_prim_array_map || is_prim_array_reduce)) {
  370. continue;
  371. }
  372. }
  373. FuncGraphPtr g = get_prim_graph(GetValueNode<PrimitivePtr>(const_primitive_node),
  374. dyn_cast<AbstractFunction>(const_primitive_node->abstract()));
  375. tr->SetEdge(node, key, NewValueNode(g));
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. FuncGraphPtr WrapPrimitives(const FuncGraphPtr &graph) {
  383. MS_EXCEPTION_IF_NULL(graph);
  384. FuncGraphManagerPtr manager_ptr = graph->manager();
  385. MS_EXCEPTION_IF_NULL(manager_ptr);
  386. MapPrimTypeFuncGraph prim_graphs;
  387. auto get_prim_graph = [&](const PrimitivePtr &prim, const AbstractFunctionPtr &type) {
  388. PrimTypePair prim_type = std::make_pair(prim, type);
  389. if (prim_graphs.end() == prim_graphs.find(prim_type)) {
  390. FuncGraphPtr g = std::make_shared<FuncGraph>();
  391. std::vector<AnfNodePtr> args;
  392. ValueNodePtr prim_ct = NewValueNode(prim);
  393. MS_EXCEPTION_IF_NULL(prim_ct);
  394. prim_ct->set_abstract(type);
  395. args.push_back(prim_ct);
  396. MS_EXCEPTION_IF_NULL(type);
  397. TypedPrimitiveAbstractClosurePtr tp = dyn_cast<abstract::TypedPrimitiveAbstractClosure>(type->GetUnique());
  398. MS_EXCEPTION_IF_NULL(tp);
  399. MS_EXCEPTION_IF_NULL(g);
  400. for (auto t : tp->args_spec_list()) {
  401. ParameterPtr p = g->add_parameter();
  402. p->set_abstract(t);
  403. args.push_back(p);
  404. }
  405. AnfNodePtr out = g->NewCNode(args);
  406. out->set_abstract(tp->output());
  407. g->set_output(out);
  408. prim_graphs[prim_type] = g;
  409. }
  410. return prim_graphs[prim_type];
  411. };
  412. FuncGraphTransaction tr = manager_ptr->Transact();
  413. auto &fgs = manager_ptr->func_graphs();
  414. TraverseGraphMap(manager_ptr, &tr, fgs, get_prim_graph);
  415. tr.Commit();
  416. return graph;
  417. }
  418. CompileGraphs::CompileGraphs(const BackendPtr &backend, const std::vector<PrimitivePtr> &cut_list) : backend_(backend) {
  419. MS_EXCEPTION_IF_NULL(backend);
  420. MS_LOG(DEBUG) << "Start vm: " << backend->name();
  421. transform_ = std::make_shared<CompileGraph>(backend, cut_list);
  422. Reset();
  423. }
  424. // Convert graphs to unlinked instructions.
  425. void CompileGraphs::Compile(const FuncGraphPtr &graph) {
  426. MS_LOG(DEBUG) << "Start";
  427. mapping_[graph] = static_cast<int64_t>(insts_.size());
  428. if (transform_ != nullptr) {
  429. InstSet insts = transform_->Run(graph);
  430. if (!insts.empty()) {
  431. (void)insts_.insert(insts_.end(), insts.begin(), insts.end());
  432. }
  433. }
  434. MS_LOG(DEBUG) << "End";
  435. }
  436. // Link instructions from multiple function graphs together.
  437. FinalVMPtr CompileGraphs::Link(const FuncGraphPtr &graph) {
  438. MS_LOG(DEBUG) << "Start";
  439. for (std::size_t i = 0; i < insts_.size(); i++) {
  440. InstType inst = insts_[i];
  441. MS_LOG(DEBUG) << "Link point:" << inst_str[inst.first];
  442. if (Instruction::kGraph == inst.first) {
  443. if (inst.second.empty()) {
  444. MS_LOG(EXCEPTION) << "The second element of inst is empty";
  445. }
  446. FuncGraphPtr func_graph = utils::cast<ValuePtr>(inst.second[0])->cast<FuncGraphPtr>();
  447. MS_LOG(DEBUG) << "Link graph:" << func_graph->ToString();
  448. insts_[i] = std::make_pair(Instruction::kPush, VectorRef(std::vector<BaseRef>{mapping_[func_graph]}));
  449. }
  450. }
  451. FinalVMPtr rt = std::make_shared<FinalVM>(insts_, backend_);
  452. MS_LOG(DEBUG) << "End";
  453. return rt;
  454. }
  455. // Convert all graphs to unlinked instructions and link them.
  456. FinalVMPtr CompileGraphs::CompileAndLink(const FuncGraphPtr &graph) {
  457. MS_EXCEPTION_IF_NULL(graph);
  458. MS_LOG(DEBUG) << "Start";
  459. Reset();
  460. MS_LOG(DEBUG) << "Begin parameter:" << graph->parameters().size();
  461. FuncGraphPtr prim_graph = WrapPrimitives(graph);
  462. Compile(prim_graph);
  463. MS_EXCEPTION_IF_NULL(prim_graph);
  464. FuncGraphSet graphs = prim_graph->manager()->func_graphs();
  465. for (auto g : graphs) {
  466. if (g != graph && g != nullptr && !(g->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL))) {
  467. Compile(g);
  468. }
  469. }
  470. FinalVMPtr rt = Link(graph);
  471. Reset();
  472. MS_LOG(DEBUG) << "End";
  473. return rt;
  474. }
  475. BackendPtr CreateBackend() {
  476. auto context_ptr = MsContext::GetInstance();
  477. MS_EXCEPTION_IF_NULL(context_ptr);
  478. std::string name = context_ptr->backend_policy();
  479. MS_LOG(INFO) << "CreateBackend is: " << name;
  480. if (backend_list.count(name) == 0) {
  481. MS_LOG(EXCEPTION) << "Backend is error: " << name;
  482. }
  483. if (name == kMsConvert) {
  484. std::string target = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  485. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  486. auto backend = std::make_shared<MsBackend>(name, target, device_id);
  487. std::string device_target = MsContext::GetInstance()->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  488. if (device_target == kAscendDevice) {
  489. if (MsContext::GetInstance()->get_param<int>(MS_CTX_EXECUTION_MODE) == kPynativeMode) {
  490. backend->set_is_multi_graph_sink(false);
  491. context_ptr->set_param<bool>(MS_CTX_IS_MULTI_GRAPH_SINK, false);
  492. } else {
  493. backend->set_is_multi_graph_sink(true);
  494. context_ptr->set_param<bool>(MS_CTX_IS_MULTI_GRAPH_SINK, true);
  495. }
  496. }
  497. return backend;
  498. }
  499. return std::make_shared<Backend>(name);
  500. }
  501. } // namespace compile
  502. } // namespace mindspore