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.

subgraph_executor.cc 21 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "hybrid/executor/subgraph_executor.h"
  17. #include "graph/ge_context.h"
  18. #include "hybrid/executor/worker/task_compile_engine.h"
  19. #include "hybrid/executor/worker/execution_engine.h"
  20. #include "hybrid/node_executor/node_executor.h"
  21. namespace ge {
  22. namespace hybrid {
  23. namespace {
  24. constexpr int kDefaultThreadNum = 4;
  25. constexpr int kDefaultQueueSize = 16;
  26. constexpr int kDataInputIndex = 0;
  27. }
  28. SubgraphExecutor::SubgraphExecutor(const GraphItem *graph_item, GraphExecutionContext *context, bool force_infer_shape)
  29. : graph_item_(graph_item),
  30. context_(context),
  31. force_infer_shape_(force_infer_shape),
  32. pre_run_pool_(kDefaultThreadNum),
  33. ready_queue_(kDefaultQueueSize) {
  34. }
  35. SubgraphExecutor::~SubgraphExecutor() {
  36. GELOGD("[%s] SubgraphExecutor destroyed.", graph_item_->GetName().c_str());
  37. }
  38. Status SubgraphExecutor::Init(const std::vector<TensorValue> &inputs,
  39. const std::vector<ConstGeTensorDescPtr> &input_desc) {
  40. subgraph_context_.reset(new(std::nothrow)SubgraphContext(graph_item_, context_));
  41. GE_CHECK_NOTNULL(subgraph_context_);
  42. GE_CHK_STATUS_RET(subgraph_context_->Init(), "[%s] Failed to init subgraph context.", graph_item_->GetName().c_str());
  43. shape_inference_engine_.reset(new(std::nothrow) ShapeInferenceEngine(context_, subgraph_context_.get()));
  44. GE_CHECK_NOTNULL(shape_inference_engine_);
  45. if (graph_item_->IsDynamic()) {
  46. GE_CHK_STATUS_RET(InitInputsForUnknownShape(inputs, input_desc),
  47. "[%s] Failed to set inputs.",
  48. graph_item_->GetName().c_str());
  49. } else {
  50. GE_CHK_STATUS_RET(InitInputsForKnownShape(inputs),
  51. "[%s] Failed to init subgraph executor for known shape subgraph.",
  52. graph_item_->GetName().c_str());
  53. }
  54. return SUCCESS;
  55. }
  56. Status SubgraphExecutor::InitInputsForUnknownShape(const std::vector<TensorValue> &inputs,
  57. const std::vector<ConstGeTensorDescPtr> &input_desc) {
  58. // Number of inputs of parent node should be greater or equal than that of subgraph
  59. auto input_nodes = graph_item_->GetInputNodes();
  60. if (inputs.size() < input_nodes.size()) {
  61. GELOGE(INTERNAL_ERROR, "[%s] Number of inputs [%zu] is not sufficient for subgraph which needs [%zu] inputs.",
  62. graph_item_->GetName().c_str(), inputs.size(), input_nodes.size());
  63. return INTERNAL_ERROR;
  64. }
  65. for (size_t i = 0; i < input_nodes.size(); ++i) {
  66. auto &input_node = input_nodes[i];
  67. if (input_node == nullptr) {
  68. GELOGD("[%s] Input[%zu] is not needed by subgraph, skip it.", graph_item_->GetName().c_str(), i);
  69. continue;
  70. }
  71. auto &input_tensor = inputs[i];
  72. GELOGD("[%s] Set input tensor[%zu] to inputs with index = %d, tensor = %s",
  73. graph_item_->GetName().c_str(),
  74. i,
  75. input_node->input_start,
  76. input_tensor.DebugString().c_str());
  77. GE_CHK_STATUS_RET(subgraph_context_->SetInput(*input_node, kDataInputIndex, input_tensor),
  78. "[%s] Failed to set input tensor[%zu]",
  79. graph_item_->GetName().c_str(),
  80. i);
  81. if (force_infer_shape_ || input_node->is_dynamic) {
  82. GELOGD("[%s] Start to update input[%zu] for subgraph data node.", graph_item_->GetName().c_str(), i);
  83. GE_CHECK_LE(i + 1, input_desc.size());
  84. const auto &tensor_desc = input_desc[i];
  85. GE_CHECK_NOTNULL(tensor_desc);
  86. auto node_state = subgraph_context_->GetOrCreateNodeState(input_node);
  87. GE_CHECK_NOTNULL(node_state);
  88. node_state->GetShapeInferenceState().UpdateInputShape(0, *tensor_desc);
  89. }
  90. }
  91. GELOGD("[%s] Done setting inputs.", graph_item_->GetName().c_str());
  92. return SUCCESS;
  93. }
  94. Status SubgraphExecutor::InitInputsForKnownShape(const std::vector<TensorValue> &inputs) {
  95. auto &input_index_mapping = graph_item_->GetInputIndexMapping();
  96. for (size_t i = 0; i < input_index_mapping.size(); ++i) {
  97. auto &parent_input_index = input_index_mapping[i];
  98. if (static_cast<size_t>(parent_input_index) >= inputs.size()) {
  99. GELOGE(INTERNAL_ERROR,
  100. "[%s] Number of inputs [%zu] is not sufficient for subgraph which needs at lease [%d] inputs",
  101. graph_item_->GetName().c_str(),
  102. inputs.size(),
  103. parent_input_index + 1);
  104. return INTERNAL_ERROR;
  105. }
  106. auto &input_tensor = inputs[parent_input_index];
  107. subgraph_context_->SetInput(static_cast<int>(i), input_tensor);
  108. GELOGD("[%s] Set input tensor[%zu] with inputs with index = %d, tensor = %s",
  109. graph_item_->GetName().c_str(),
  110. i,
  111. parent_input_index,
  112. input_tensor.DebugString().c_str());
  113. }
  114. return SUCCESS;
  115. }
  116. Status SubgraphExecutor::ExecuteAsync(const std::vector<TensorValue> &inputs,
  117. const std::vector<ConstGeTensorDescPtr> &input_desc,
  118. const std::vector<TensorValue> &outputs) {
  119. GELOGD("[%s] is dynamic = %s", graph_item_->GetName().c_str(), graph_item_->IsDynamic() ? "true" : "false");
  120. GE_CHK_STATUS_RET(Init(inputs, input_desc), "[%s] Failed to init executor.", graph_item_->GetName().c_str());
  121. if (!outputs.empty()) {
  122. GE_CHK_STATUS_RET(EnableOutputZeroCopy(outputs),
  123. "Failed to enable output zero copy by user provided outputs.");
  124. }
  125. if (!graph_item_->IsDynamic()) {
  126. return ExecuteAsyncForKnownShape(inputs);
  127. }
  128. HYBRID_CHK_STATUS_RET(ScheduleTasks(), "[%s] Failed to execute tasks.", graph_item_->GetName().c_str());
  129. GELOGD("[%s] Done executing subgraph successfully.", graph_item_->GetName().c_str());
  130. return SUCCESS;
  131. }
  132. Status SubgraphExecutor::ExecuteAsync(const std::vector<TensorValue> &inputs,
  133. const std::vector<ConstGeTensorDescPtr> &input_desc) {
  134. return ExecuteAsync(inputs, input_desc, {});
  135. }
  136. Status SubgraphExecutor::ExecuteAsyncForKnownShape(const std::vector<TensorValue> &inputs) {
  137. GELOGD("[%s] subgraph is not dynamic.", graph_item_->GetName().c_str());
  138. if (graph_item_->GetAllNodes().size() != 1) {
  139. GELOGE(INTERNAL_ERROR,
  140. "[%s] Invalid known shape subgraph. node size = %zu",
  141. graph_item_->GetName().c_str(),
  142. graph_item_->GetAllNodes().size());
  143. return INTERNAL_ERROR;
  144. }
  145. auto node_item = graph_item_->GetAllNodes()[0];
  146. GE_CHECK_NOTNULL(node_item);
  147. auto node_state = subgraph_context_->GetOrCreateNodeState(node_item);
  148. GE_CHECK_NOTNULL(node_state);
  149. node_state->SetKernelTask(node_item->kernel_task);
  150. known_shape_task_context_ = TaskContext::Create(node_state.get(), context_, subgraph_context_.get());
  151. GE_CHECK_NOTNULL(known_shape_task_context_);
  152. HYBRID_CHK_STATUS_RET(ExecutionEngine::ExecuteAsync(*node_state, known_shape_task_context_, *context_),
  153. "[%s] Failed to execute node [%s] for known subgraph.",
  154. graph_item_->GetName().c_str(),
  155. known_shape_task_context_->GetNodeName());
  156. GELOGD("[%s] Done execute non-dynamic subgraph successfully.", graph_item_->GetName().c_str());
  157. return SUCCESS;
  158. }
  159. Status SubgraphExecutor::ExecuteAsync(TaskContext &task_context) {
  160. std::vector<TensorValue> inputs;
  161. std::vector<ConstGeTensorDescPtr> input_desc;
  162. for (int i = 0; i < task_context.NumInputs(); ++i) {
  163. auto tensor = task_context.GetInput(i);
  164. GE_CHECK_NOTNULL(tensor);
  165. inputs.emplace_back(*tensor);
  166. input_desc.emplace_back(task_context.GetInputDesc(i));
  167. }
  168. GE_CHK_STATUS_RET(ExecuteAsync(inputs, input_desc),
  169. "[%s] Failed to execute subgraph.",
  170. graph_item_->GetName().c_str());
  171. GE_CHK_STATUS_RET(SetOutputsToParentNode(task_context),
  172. "[%s] Failed to set output shapes to parent node.",
  173. graph_item_->GetName().c_str());
  174. return SUCCESS;
  175. }
  176. Status SubgraphExecutor::PrepareNodes(int group) {
  177. GELOGD("[%s] Start to prepare nodes. group = %d",
  178. graph_item_->GetName().c_str(),
  179. group);
  180. auto &all_nodes = graph_item_->GetAllNodes(group);
  181. for (auto all_node : all_nodes) {
  182. auto &node_item = *all_node;
  183. // for while op
  184. if (force_infer_shape_ && !node_item.is_dynamic) {
  185. GELOGD("[%s] Force infer shape is set, updating node to dynamic.", node_item.NodeName().c_str());
  186. auto &mutable_node_item = const_cast<NodeItem &>(node_item);
  187. mutable_node_item.SetToDynamic();
  188. }
  189. GELOGD("[%s] Start to prepare node [%s].", graph_item_->GetName().c_str(), node_item.NodeName().c_str());
  190. auto node_state = subgraph_context_->GetOrCreateNodeState(&node_item);
  191. GE_CHECK_NOTNULL(node_state);
  192. auto p_node_state = node_state.get();
  193. if (node_item.node_type != NETOUTPUT) {
  194. // only do shape inference and compilation for nodes with dynamic shapes.
  195. if (node_item.is_dynamic) {
  196. auto prepare_future = pre_run_pool_.commit([this, p_node_state]() -> Status {
  197. GetContext().SetSessionId(context_->session_id);
  198. GetContext().SetContextId(context_->context_id);
  199. GE_CHK_STATUS_RET_NOLOG(InferShape(shape_inference_engine_.get(), *p_node_state));
  200. return PrepareForExecution(context_, *p_node_state);
  201. });
  202. p_node_state->SetPrepareFuture(std::move(prepare_future));
  203. } else {
  204. GELOGD("[%s] Skipping shape inference and compilation for node with static shape.",
  205. node_item.NodeName().c_str());
  206. if (node_item.kernel_task == nullptr) {
  207. GELOGW("[%s] Node of static shape got no task.", node_item.NodeName().c_str());
  208. GE_CHK_STATUS_RET(TaskCompileEngine::Compile(*p_node_state, context_),
  209. "[%s] Failed to create task.", p_node_state->GetName().c_str());
  210. } else {
  211. node_state->SetKernelTask(node_item.kernel_task);
  212. }
  213. auto unique_task_context =
  214. TaskContext::Create(node_state.get(), context_, subgraph_context_.get());
  215. GE_CHECK_NOTNULL(unique_task_context);
  216. const auto &task = node_state->GetKernelTask();
  217. if (task == nullptr) {
  218. GELOGE(INTERNAL_ERROR, "[%s] NodeTask is null.", node_state->GetName().c_str());
  219. return INTERNAL_ERROR;
  220. }
  221. auto shared_task_context = std::shared_ptr<TaskContext>(unique_task_context.release());
  222. node_state->SetTaskContext(shared_task_context);
  223. }
  224. }
  225. if (!ready_queue_.Push(p_node_state)) {
  226. if (context_->is_eos_) {
  227. GELOGD("Got end of sequence");
  228. return SUCCESS;
  229. }
  230. GELOGE(INTERNAL_ERROR, "[%s] Error occurs while launching tasks. quit from preparing nodes.",
  231. graph_item_->GetName().c_str());
  232. return INTERNAL_ERROR;
  233. }
  234. GELOGD("[%s] Push node [%s] to queue.", graph_item_->GetName().c_str(), node_item.NodeName().c_str());
  235. }
  236. GELOGD("[%s] Done preparing nodes successfully.", graph_item_->GetName().c_str());
  237. return SUCCESS;
  238. }
  239. Status SubgraphExecutor::InferShape(ShapeInferenceEngine *shape_inference_engine, NodeState &node_state) const {
  240. HYBRID_CHK_STATUS_RET(shape_inference_engine->InferShape(node_state),
  241. "[%s] Failed to InferShape.", node_state.GetName().c_str());
  242. HYBRID_CHK_STATUS_RET(shape_inference_engine->PropagateOutputShapes(node_state),
  243. "[%s] Failed to PropagateOutputShapes.", node_state.GetName().c_str());
  244. return SUCCESS;
  245. }
  246. Status SubgraphExecutor::PrepareForExecution(GraphExecutionContext *ctx, NodeState &node_state) {
  247. auto &node_item = *node_state.GetNodeItem();
  248. if (node_item.kernel_task == nullptr) {
  249. GE_CHK_STATUS_RET(TaskCompileEngine::Compile(node_state, ctx),
  250. "Failed to create task for node[%s]", node_state.GetName().c_str());
  251. } else {
  252. node_state.SetKernelTask(node_item.kernel_task);
  253. }
  254. auto unique_task_context = TaskContext::Create(&node_state, context_, subgraph_context_.get());
  255. GE_CHECK_NOTNULL(unique_task_context);
  256. const auto &task = node_state.GetKernelTask();
  257. if (task == nullptr) {
  258. GELOGE(INTERNAL_ERROR, "[%s] NodeTask is null.", node_state.GetName().c_str());
  259. return INTERNAL_ERROR;
  260. }
  261. auto shared_task_context = std::shared_ptr<TaskContext>(unique_task_context.release());
  262. node_state.SetTaskContext(shared_task_context);
  263. GE_CHK_RT_RET(rtCtxSetCurrent(ctx->rt_context));
  264. RECORD_COMPILE_EVENT(ctx, node_item.NodeName().c_str(), "[UpdateTilingData] start");
  265. GE_CHK_STATUS_RET_NOLOG(task->UpdateTilingData(*shared_task_context)); // update op_desc before alloc ws
  266. RECORD_COMPILE_EVENT(ctx, node_item.NodeName().c_str(), "[UpdateTilingData] end");
  267. return SUCCESS;
  268. }
  269. Status SubgraphExecutor::LaunchTasks() {
  270. while (true) {
  271. NodeState *node_state = nullptr;
  272. if (!ready_queue_.Pop(node_state)) {
  273. GELOGE(INTERNAL_ERROR, "[%s] Failed to pop node.", graph_item_->GetName().c_str());
  274. return INTERNAL_ERROR;
  275. }
  276. if (node_state == nullptr) {
  277. GELOGD("[%s] Got EOF from queue.", graph_item_->GetName().c_str());
  278. return SUCCESS;
  279. }
  280. if (node_state->GetType() == NETOUTPUT) {
  281. // Wait for all inputs become valid
  282. // after PrepareNodes returned. all output tensors and shapes are valid
  283. GE_CHK_STATUS_RET_NOLOG(node_state->GetShapeInferenceState().AwaitShapesReady(*context_));
  284. GE_CHK_STATUS_RET_NOLOG(node_state->AwaitInputTensors(*context_));
  285. GELOGD("[%s] Done executing node successfully.", node_state->GetName().c_str());
  286. continue;
  287. }
  288. GE_CHK_STATUS_RET_NOLOG(node_state->WaitForPrepareDone());
  289. GELOGD("[%s] Start to execute.", node_state->GetName().c_str());
  290. auto shared_task_context = node_state->GetTaskContext();
  291. GE_CHECK_NOTNULL(shared_task_context);
  292. shared_task_context->SetForceInferShape(force_infer_shape_);
  293. HYBRID_CHK_STATUS_RET(ExecutionEngine::ExecuteAsync(*node_state, shared_task_context, *context_),
  294. "[%s] Execute node failed.",
  295. node_state->GetName().c_str());
  296. GELOGD("[%s] Done executing node successfully.", node_state->GetName().c_str());
  297. }
  298. }
  299. Status SubgraphExecutor::ScheduleTasks(int group) {
  300. GELOGD("[%s] Start to schedule prepare workers.", graph_item_->GetName().c_str());
  301. auto prepare_future = std::async(std::launch::async, [&]() -> Status {
  302. GetContext().SetSessionId(context_->session_id);
  303. GetContext().SetContextId(context_->context_id);
  304. auto ret = PrepareNodes(group);
  305. ready_queue_.Push(nullptr);
  306. return ret;
  307. });
  308. GELOGD("[%s] Start to execute subgraph.", graph_item_->GetName().c_str());
  309. auto ret = LaunchTasks();
  310. if (ret != SUCCESS) {
  311. subgraph_context_->OnError(ret);
  312. context_->SetErrorCode(ret);
  313. ready_queue_.Stop();
  314. prepare_future.wait();
  315. return ret;
  316. }
  317. GE_CHK_STATUS_RET(prepare_future.get(),
  318. "[%s] Error occurred in task preparation.",
  319. graph_item_->GetName().c_str());
  320. GELOGD("[%s] Done launching all tasks successfully.", graph_item_->GetName().c_str());
  321. return SUCCESS;
  322. }
  323. Status SubgraphExecutor::GetOutputs(vector<TensorValue> &outputs) {
  324. return subgraph_context_->GetOutputs(outputs);
  325. }
  326. Status SubgraphExecutor::GetOutputs(vector<TensorValue> &outputs, std::vector<ConstGeTensorDescPtr> &output_desc) {
  327. GE_CHK_STATUS_RET(GetOutputs(outputs), "[%s] Failed to get output tensors.", graph_item_->GetName().c_str());
  328. // copy output data from op to designated position
  329. GE_CHK_STATUS_RET(graph_item_->GetOutputDescList(output_desc),
  330. "[%s] Failed to get output tensor desc.",
  331. graph_item_->GetName().c_str());
  332. if (outputs.size() != output_desc.size()) {
  333. GELOGE(INTERNAL_ERROR,
  334. "Number of output tensors(%zu) mismatch number of output tensor desc(%zu).",
  335. outputs.size(),
  336. output_desc.size());
  337. return INTERNAL_ERROR;
  338. }
  339. return SUCCESS;
  340. }
  341. Status SubgraphExecutor::Synchronize() {
  342. GELOGD("[%s] Synchronize start.", graph_item_->GetName().c_str());
  343. GE_CHK_STATUS_RET_NOLOG(context_->Synchronize(context_->stream));
  344. GELOGD("[%s] Done synchronizing successfully.", graph_item_->GetName().c_str());
  345. return SUCCESS;
  346. }
  347. Status SubgraphExecutor::SetOutputsToParentNode(TaskContext &task_context) {
  348. // get output tensors and tensor desc list
  349. std::vector<TensorValue> outputs;
  350. std::vector<ConstGeTensorDescPtr> output_desc_list;
  351. GE_CHK_STATUS_RET(subgraph_context_->GetOutputs(outputs),
  352. "[%s] Failed to get output tensors.",
  353. graph_item_->GetName().c_str());
  354. GE_CHK_STATUS_RET(graph_item_->GetOutputDescList(output_desc_list),
  355. "[%s] Failed to get output tensor desc.",
  356. graph_item_->GetName().c_str());
  357. if (outputs.size() != output_desc_list.size()) {
  358. GELOGE(INTERNAL_ERROR, "[%s] num output tensors = %zu, num output tensor desc = %zu",
  359. graph_item_->GetName().c_str(),
  360. outputs.size(),
  361. output_desc_list.size());
  362. return INTERNAL_ERROR;
  363. }
  364. // mapping to parent task context
  365. for (size_t i = 0; i < outputs.size(); ++i) {
  366. int parent_output_index = graph_item_->GetParentOutputIndex(i);
  367. GE_CHECK_GE(parent_output_index, 0);
  368. // update tensor
  369. GELOGD("[%s] Updating output[%zu] to parent output[%d]",
  370. graph_item_->GetName().c_str(),
  371. i,
  372. parent_output_index);
  373. GELOGD("[%s] Updating output tensor, index = %d, tensor = %s",
  374. graph_item_->GetName().c_str(),
  375. parent_output_index,
  376. outputs[i].DebugString().c_str());
  377. GE_CHK_STATUS_RET(task_context.SetOutput(parent_output_index, outputs[i]));
  378. // updating shapes. dynamic format/dtype is not supported.
  379. // It should be noted that even the subgraph is of known shape, it is also necessary to update parent output desc,
  380. // for instance, IfOp may have two known-shaped subgraphs of different output shapes
  381. const auto &output_desc = output_desc_list[i];
  382. auto parent_output_desc = task_context.MutableOutputDesc(parent_output_index);
  383. GE_CHECK_NOTNULL(parent_output_desc);
  384. GELOGD("[%s] Updating output shape[%d] from [%s] to [%s]",
  385. graph_item_->GetName().c_str(),
  386. parent_output_index,
  387. parent_output_desc->MutableShape().ToString().c_str(),
  388. output_desc->GetShape().ToString().c_str());
  389. parent_output_desc->SetShape(output_desc->GetShape());
  390. GELOGD("[%s] Updating output original shape[%d] from [%s] to [%s]",
  391. graph_item_->GetName().c_str(),
  392. parent_output_index,
  393. parent_output_desc->GetOriginShape().ToString().c_str(),
  394. output_desc->GetOriginShape().ToString().c_str());
  395. parent_output_desc->SetOriginShape(output_desc->GetOriginShape());
  396. }
  397. return SUCCESS;
  398. }
  399. Status SubgraphExecutor::EnableOutputZeroCopy(const vector<TensorValue> &outputs) {
  400. GELOGD("To enable zero copy, output number = %zu", outputs.size());
  401. const auto &output_edges = graph_item_->GetOutputEdges();
  402. // Op -> MetOutput, set the output tensor of Op that output to the NetOutput node
  403. if (outputs.size() != output_edges.size()) {
  404. GELOGE(PARAM_INVALID, "Output number mismatches, expect = %zu, but given = %zu",
  405. output_edges.size(),
  406. outputs.size());
  407. return PARAM_INVALID;
  408. }
  409. for (size_t i = 0; i < outputs.size(); ++i) {
  410. auto &output_tensor = outputs[i];
  411. auto &output_node = output_edges[i].first;
  412. int output_idx = output_edges[i].second;
  413. GELOGD("[%s] Set output tensor[%zu] to [%s]'s output[%d], tensor = %s",
  414. graph_item_->GetName().c_str(),
  415. i,
  416. output_node->NodeName().c_str(),
  417. output_idx,
  418. output_tensor.DebugString().c_str());
  419. GE_CHK_STATUS_RET(subgraph_context_->SetOutput(*output_node, output_idx, output_tensor),
  420. "[%s] Failed to set input tensor[%zu]",
  421. graph_item_->GetName().c_str(),
  422. i);
  423. }
  424. GELOGD("Done enabling zero copy for outputs successfully.");
  425. return SUCCESS;
  426. }
  427. Status SubgraphExecutor::PartialExecuteAsync(int task_group) {
  428. return ScheduleTasks(task_group);
  429. }
  430. Status SubgraphExecutor::InitForPartialExecution(const vector<TensorValue> &inputs,
  431. const vector<ConstGeTensorDescPtr> &input_desc) {
  432. return Init(inputs, input_desc);
  433. }
  434. } // namespace hybrid
  435. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示