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.

task_context.cc 23 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 "task_context.h"
  17. #include "framework/common/ge_inner_error_codes.h"
  18. #include "framework/common/debug/log.h"
  19. #include "graph/utils/tensor_utils.h"
  20. #include "graph/types.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "hybrid/executor/hybrid_execution_context.h"
  23. #include "hybrid/executor/subgraph_executor.h"
  24. #include "common/profiling/profiling_manager.h"
  25. namespace ge {
  26. namespace hybrid {
  27. TaskContext::TaskContext(GraphExecutionContext *execution_context,
  28. NodeState *node_state,
  29. SubgraphContext *subgraph_context)
  30. : node_state_(node_state),
  31. node_item_(node_state->GetNodeItem()),
  32. execution_context_(execution_context),
  33. subgraph_context_(subgraph_context) {}
  34. TaskContext::~TaskContext() {
  35. GELOGD("[%s] TaskContext destroyed.", node_item_->NodeName().c_str());
  36. // release output
  37. for (int i = 0; i < NumOutputs(); ++i) {
  38. auto output_tensor = MutableOutput(i);
  39. if (output_tensor != nullptr) {
  40. output_tensor->Destroy();
  41. }
  42. }
  43. }
  44. void TaskContext::ReleaseWorkspace() {
  45. GELOGD("[%s] Start ReleaseWorkspace.", node_item_->NodeName().c_str());
  46. for (auto ws_addr : workspaces_) {
  47. execution_context_->allocator->Deallocate(ws_addr);
  48. }
  49. }
  50. std::unique_ptr<TaskContext> TaskContext::Create(NodeState *node_state, SubgraphContext *subgraph_context) {
  51. const NodeItem &node_item = *node_state->GetNodeItem();
  52. GELOGI("[%s] To create task context, input start = %d, num_inputs = %d, output start = %d, num_outputs = %d.",
  53. node_item.NodeName().c_str(),
  54. node_item.input_start,
  55. node_item.num_inputs,
  56. node_item.output_start,
  57. node_item.num_outputs);
  58. if (node_item.input_start < 0 || node_item.output_start < 0) {
  59. REPORT_INNER_ERROR("E19999", "NodeItem:%s(%s) not property initialized."
  60. "input_start:%d or output_start:%d less than 0",
  61. node_item.NodeName().c_str(), node_item.NodeType().c_str(),
  62. node_item.input_start, node_item.output_start);
  63. GELOGE(INTERNAL_ERROR,
  64. "[Check][Param]NodeItem:%s(%s) not property initialized. input_start = %d, output_start = %d",
  65. node_item.NodeName().c_str(), node_item.NodeType().c_str(),
  66. node_item.input_start, node_item.output_start);
  67. return nullptr;
  68. }
  69. auto task_context = std::unique_ptr<TaskContext>(
  70. new(std::nothrow)TaskContext(subgraph_context->execution_context_, node_state, subgraph_context));
  71. if (task_context == nullptr) {
  72. REPORT_CALL_ERROR("E19999", "Create TaskContext failed for [%s].", node_item.NodeName().c_str());
  73. GELOGE(MEMALLOC_FAILED, "[Create][TaskContext] failed for [%s].", node_item.NodeName().c_str());
  74. return nullptr;
  75. }
  76. task_context->node_item_ = &node_item;
  77. task_context->inputs_start_ = subgraph_context->all_inputs_.data() + node_item.input_start;
  78. task_context->outputs_start_ = subgraph_context->all_outputs_.data() + node_item.output_start;
  79. task_context->iteration_ = subgraph_context->execution_context_->iteration;
  80. return task_context;
  81. }
  82. int TaskContext::NumInputs() const {
  83. return node_item_->num_inputs;
  84. }
  85. int TaskContext::NumOutputs() const {
  86. return node_item_->num_outputs;
  87. }
  88. TensorValue *TaskContext::MutableInput(int index) {
  89. if (index < 0 || index >= node_item_->num_inputs) {
  90. REPORT_INNER_ERROR("E19999", "Index out of range, check invalid. index = %d, num_inputs = %d, node:%s(%s)",
  91. index, node_item_->num_inputs,
  92. node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  93. GELOGE(PARAM_INVALID, "[Check][Param]Index out of range. index = %d, num_inputs = %d, node:%s(%s)",
  94. index, node_item_->num_inputs,
  95. node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  96. return nullptr;
  97. }
  98. return inputs_start_ + index;
  99. }
  100. const TensorValue *TaskContext::GetOutput(int index) const {
  101. if (index < 0 || index >= node_item_->num_outputs) {
  102. REPORT_INNER_ERROR("E19999", "Index out of range, check invalid. index = %d, num_outputs = %d, node:%s(%s)",
  103. index, node_item_->num_outputs,
  104. node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  105. GELOGE(PARAM_INVALID, "[Check][Param]Index out of range. index = %d, num_outputs = %d, node:%s(%s)",
  106. index, node_item_->num_outputs,
  107. node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  108. return nullptr;
  109. }
  110. return outputs_start_ + index;
  111. }
  112. TensorValue *TaskContext::MutableOutput(int index) {
  113. if (index < 0 || index >= node_item_->num_outputs) {
  114. REPORT_INNER_ERROR("E19999", "Index out of range, check invalid. index = %d, num_outputs = %d, node:%s(%s)",
  115. index, node_item_->num_outputs,
  116. node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  117. GELOGE(PARAM_INVALID, "[Check][Param]Index out of range. index = %d, num_outputs = %d, node:%s(%s)",
  118. index, node_item_->num_outputs,
  119. node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  120. return nullptr;
  121. }
  122. return outputs_start_ + index;
  123. }
  124. std::size_t TaskContext::NumWorkspaces() const {
  125. return workspaces_.size();
  126. }
  127. void *TaskContext::MutableWorkspace(int index) {
  128. if (index < 0 || static_cast<size_t>(index) >= workspaces_.size()) {
  129. REPORT_INNER_ERROR("E19999", "Index:%d out of range, check invalid. number:%zu of workspaces_, node:%s(%s)",
  130. index, workspaces_.size(), node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  131. GELOGE(PARAM_INVALID, "[Check][Param]Index:%d out of range. number:%zu of workspaces_, node:%s(%s)",
  132. index, workspaces_.size(), node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  133. return nullptr;
  134. }
  135. return workspaces_[index];
  136. }
  137. const TensorValue *TaskContext::GetInput(int index) const {
  138. if (index < 0 || index >= node_item_->num_inputs) {
  139. REPORT_INNER_ERROR("E19999", "Index:%d out of range, check invalid. num_inputs:%d node:%s(%s)",
  140. index, node_item_->num_inputs, node_item_->NodeName().c_str(),
  141. node_item_->NodeType().c_str());
  142. GELOGE(PARAM_INVALID, "[Check][Param]Index:%d out of range. num_inputs:%d node:%s(%s)",
  143. index, node_item_->num_inputs, node_item_->NodeName().c_str(), node_item_->NodeType().c_str());
  144. return nullptr;
  145. }
  146. return inputs_start_ + index;
  147. }
  148. Status TaskContext::AllocateWorkspaces() {
  149. auto workspace_sizes = node_item_->node->GetOpDesc()->GetWorkspaceBytes();
  150. for (auto size : workspace_sizes) {
  151. void *workspace = execution_context_->allocator->Allocate(size);
  152. if (workspace == nullptr) {
  153. REPORT_CALL_ERROR("E19999", "node:%s(%s) Allocate workspace failed, size: %ld",
  154. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(), size);
  155. GELOGE(MEMALLOC_FAILED, "[Allocate][workspace] failed for node:%s(%s), size: %ld",
  156. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(), size);
  157. return MEMALLOC_FAILED;
  158. }
  159. workspaces_.emplace_back(workspace);
  160. }
  161. return SUCCESS;
  162. }
  163. Status TaskContext::RegisterCallback(const std::function<void()> &callback_fun) const {
  164. if (callback_fun == nullptr) {
  165. GELOGW("[%s] Callback is NULL", GetNodeName());
  166. return SUCCESS;
  167. }
  168. auto ret = execution_context_->callback_manager->RegisterCallback(GetStream(), callback_fun);
  169. if (ret != SUCCESS) {
  170. REPORT_CALL_ERROR("E19999", "RegisterCallback failed for [%s]", GetNodeName());
  171. GELOGE(ret, "[Register][Callback] failed for [%s]", GetNodeName());
  172. execution_context_->callback_manager->Destroy();
  173. return ret;
  174. }
  175. return SUCCESS;
  176. }
  177. string TaskContext::TensorDesc2String(const GeTensorDesc &desc) {
  178. std::stringstream ss;
  179. ss << "[TensorDesc] ";
  180. ss << "DataType = " << desc.GetDataType();
  181. ss << ", Format = " << desc.GetFormat();
  182. ss << ", Shape = [";
  183. for (auto dim : desc.GetShape().GetDims()) {
  184. ss << dim << ", ";
  185. }
  186. ss << "]";
  187. return ss.str();
  188. }
  189. Status TaskContext::AllocateTensor(const GeTensorDesc &tensor_desc, TensorValue &tensor, AllocationAttr *attr) {
  190. int64_t size = 0;
  191. if (ge::TensorUtils::GetSize(tensor_desc, size) != GRAPH_SUCCESS) {
  192. REPORT_CALL_ERROR("E19999", "Get TensorSize failed, tensor:%s", tensor_desc.GetName().c_str());
  193. GELOGE(INTERNAL_ERROR, "[Get][TensorSize] failed, tensor:%s", tensor_desc.GetName().c_str());
  194. return INTERNAL_ERROR;
  195. }
  196. if (size == 0) {
  197. GELOGW("size from tensor_desc == 0");
  198. }
  199. auto buffer = TensorBuffer::Create(execution_context_->allocator, size, attr);
  200. GE_CHECK_NOTNULL(buffer);
  201. tensor = TensorValue(shared_ptr<TensorBuffer>(buffer.release()));
  202. return SUCCESS;
  203. }
  204. Status TaskContext::AllocateOutput(int index,
  205. const GeTensorDesc &tensor_desc,
  206. TensorValue **tensor,
  207. AllocationAttr *attr) {
  208. GELOGI("To allocate output for node: %s. index = %d, tensor desc = %s",
  209. node_item_->NodeName().c_str(),
  210. index,
  211. TensorDesc2String(tensor_desc).c_str());
  212. if (index < 0 || index >= node_item_->num_outputs) {
  213. REPORT_INNER_ERROR("E19999", "%s(%s) output index out of range check invalid. num_output = %d, index = %d",
  214. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(),
  215. node_item_->num_outputs, index);
  216. GELOGE(PARAM_INVALID, "[Check][Param] %s(%s) output index out of range. num_output = %d, index = %d",
  217. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(),
  218. node_item_->num_outputs, index);
  219. return PARAM_INVALID;
  220. }
  221. if (outputs_start_[index].GetData() != nullptr) {
  222. GELOGI("already allocated as net output");
  223. return SUCCESS;
  224. }
  225. int32_t calc_type = 0;
  226. bool ret = ge::AttrUtils::GetInt(tensor_desc, ATTR_NAME_MEMORY_SIZE_CALC_TYPE, calc_type);
  227. if (ret && (calc_type == static_cast<int32_t>(ge::MemorySizeCalcType::ALWAYS_EMPTY))) {
  228. outputs_start_[index] = TensorValue();
  229. return SUCCESS;
  230. }
  231. auto it = node_item_->ref_outputs.find(index);
  232. if (it != node_item_->ref_outputs.end()) {
  233. auto &ref_node = it->second;
  234. GELOGD("source node of %s:%d = %s, op_type = %s",
  235. node_item_->NodeName().c_str(),
  236. index,
  237. ref_node->GetName().c_str(),
  238. ref_node->GetType().c_str());
  239. TensorValue *ref_tensor = execution_context_->model->GetTensor(ref_node);
  240. GE_CHECK_NOTNULL(ref_tensor);
  241. outputs_start_[index] = *ref_tensor;
  242. } else {
  243. auto reuse_output_it = node_item_->reuse_outputs.find(index);
  244. if (reuse_output_it != node_item_->reuse_outputs.end()) {
  245. GELOGD("[%s] reuse output [%d] with output [%d]", GetNodeName(), index, reuse_output_it->second);
  246. outputs_start_[index] = outputs_start_[reuse_output_it->second];
  247. } else {
  248. auto reuse_input = node_item_->reuse_inputs.find(index);
  249. if (reuse_input != node_item_->reuse_inputs.end()) {
  250. GELOGD("[%s] Output[%d] is referenced to input[%d]", GetNodeName(), index, reuse_input->second);
  251. outputs_start_[index] = inputs_start_[reuse_input->second];
  252. } else {
  253. GE_CHK_STATUS_RET_NOLOG(AllocateTensor(tensor_desc, outputs_start_[index], attr));
  254. GELOGD("Allocating output successfully. node: %s. index = %d, size = %zu",
  255. node_item_->NodeName().c_str(), index, outputs_start_[index].GetSize());
  256. }
  257. }
  258. }
  259. if (execution_context_->trace_enabled) {
  260. outputs_start_[index].SetName(node_item_->NodeName() + "_out_" + std::to_string(index));
  261. }
  262. if (tensor != nullptr) {
  263. *tensor = outputs_start_ + index;
  264. }
  265. return SUCCESS;
  266. }
  267. Status TaskContext::AllocateOutputs(AllocationAttr *attr) {
  268. for (int i = 0; i < node_item_->num_outputs; ++i) {
  269. const auto &output_desc = node_item_->MutableOutputDesc(i);
  270. GE_CHECK_NOTNULL(output_desc);
  271. uint32_t mem_type = 0;
  272. (void)AttrUtils::GetInt(output_desc, ATTR_OUTPUT_MEMORY_TYPE, mem_type);
  273. if (attr == nullptr) {
  274. auto tmp_attr = AllocationAttr(0, nullptr, static_cast<MemStorageType>(mem_type));
  275. GE_CHK_STATUS_RET_NOLOG(AllocateOutput(i, *output_desc, nullptr, &tmp_attr));
  276. } else {
  277. attr->SetMemType(static_cast<MemStorageType>(mem_type));
  278. GE_CHK_STATUS_RET_NOLOG(AllocateOutput(i, *output_desc, nullptr, attr));
  279. }
  280. }
  281. return SUCCESS;
  282. }
  283. Status TaskContext::AllocateTensor(size_t size, TensorValue &tensor, AllocationAttr *attr) {
  284. auto buffer = TensorBuffer::Create(execution_context_->allocator, size, attr);
  285. if (buffer == nullptr) {
  286. REPORT_CALL_ERROR("E19999", "%s(%s) Allocate buffer failed, size: %zu",
  287. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(), size);
  288. GELOGE(MEMALLOC_FAILED, "[Allocate][buffer] failed for %s(%s), size: %zu",
  289. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(), size);
  290. return MEMALLOC_FAILED;
  291. }
  292. tensor = TensorValue(shared_ptr<TensorBuffer>(buffer.release()));
  293. return SUCCESS;
  294. }
  295. const NodeItem &TaskContext::GetNodeItem() const {
  296. return *node_item_;
  297. }
  298. Status TaskContext::SetOutput(int index, const TensorValue &tensor) {
  299. if (index < 0 || index >= node_item_->num_outputs) {
  300. REPORT_INNER_ERROR("E19999", "%s(%s) output index out of range check invalid. num_output = %d, index = %d",
  301. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(),
  302. node_item_->num_outputs, index);
  303. GELOGE(PARAM_INVALID, "[Check][Param]%s(%s) output index out of range. num_output = %d, index = %d",
  304. node_item_->NodeName().c_str(), node_item_->NodeType().c_str(),
  305. node_item_->num_outputs, index);
  306. return PARAM_INVALID;
  307. }
  308. GELOGD("Set %s:%d with tensor: %s",
  309. node_item_->NodeName().c_str(),
  310. index,
  311. tensor.DebugString().c_str());
  312. outputs_start_[index] = tensor;
  313. return SUCCESS;
  314. }
  315. rtStream_t TaskContext::GetStream() const {
  316. return execution_context_->stream;
  317. }
  318. int64_t TaskContext::GetSessionId() const {
  319. return execution_context_->session_id;
  320. }
  321. Status TaskContext::GetStatus() const {
  322. return status_;
  323. }
  324. void TaskContext::SetStatus(Status status) {
  325. status_ = status;
  326. if (status != SUCCESS) {
  327. execution_context_->SetErrorCode(status);
  328. }
  329. }
  330. uint32_t TaskContext::GetTaskId() const {
  331. return task_id_;
  332. }
  333. void TaskContext::SetTaskId(uint32_t task_id) {
  334. task_id_ = task_id;
  335. }
  336. uint32_t TaskContext::GetStreamId() const {
  337. return stream_id_;
  338. }
  339. void TaskContext::SetStreamId(uint32_t stream_id) {
  340. stream_id_ = stream_id;
  341. }
  342. void TaskContext::SetOverFlow(bool is_over_flow) {
  343. is_over_flow_ = is_over_flow;
  344. }
  345. bool TaskContext::IsOverFlow() {
  346. return is_over_flow_;
  347. }
  348. Status TaskContext::AllocateWorkspace(size_t size, void **buffer, void *ori_addr) {
  349. GE_CHECK_NOTNULL(buffer);
  350. if (ori_addr == nullptr) {
  351. *buffer = execution_context_->allocator->Allocate(size, nullptr);
  352. } else {
  353. AllocationAttr attr(ori_addr);
  354. *buffer = execution_context_->allocator->Allocate(size, &attr);
  355. }
  356. if (*buffer == nullptr) {
  357. REPORT_CALL_ERROR("E19999", "Allocate Workspace failed, size = %zu", size);
  358. GELOGE(MEMALLOC_FAILED, "[Allocate][Workspace] failed, size = %zu", size);
  359. return MEMALLOC_FAILED;
  360. }
  361. GELOGD("[%s] Allocating workspace of size = %zu successfully", node_item_->NodeName().c_str(), size);
  362. workspaces_.emplace_back(*buffer);
  363. return SUCCESS;
  364. }
  365. Status TaskContext::PropagateOutputs() {
  366. // propagate outputs
  367. const auto &guard = node_item_->MutexGuard("PropagateOutputs");
  368. for (int i = 0; i < NumOutputs(); ++i) {
  369. auto tensor = MutableOutput(i);
  370. GE_CHECK_NOTNULL(tensor);
  371. if (tensor->GetData() == nullptr) {
  372. GELOGD("[%s] Node output[%d] is null.", node_item_->NodeName().c_str(), i);
  373. }
  374. auto &output_nodes = node_item_->outputs[i];
  375. for (auto &dst_input_index_and_node : output_nodes) {
  376. auto dst_input_idx = dst_input_index_and_node.first;
  377. auto dst_node_item = dst_input_index_and_node.second;
  378. auto input_offset = dst_node_item->input_start + dst_input_idx;
  379. GELOGD(
  380. "Propagate output of node %s, output index = %d, dst node = %s, "
  381. "dst_input_index = %d, dst_input_offset = %d.",
  382. node_item_->NodeName().c_str(),
  383. i,
  384. dst_node_item->NodeName().c_str(),
  385. dst_input_idx,
  386. input_offset);
  387. if (subgraph_context_->all_inputs_.size() <= static_cast<size_t>(input_offset)) {
  388. REPORT_INNER_ERROR("E19999",
  389. "[%s] input index out of range check invalid. index = %d, total input num = %zu",
  390. GetNodeName(), input_offset, subgraph_context_->all_inputs_.size());
  391. GELOGE(INTERNAL_ERROR, "[Check][Size][%s] input index out of range. index = %d, total input num = %zu",
  392. GetNodeName(), input_offset, subgraph_context_->all_inputs_.size());
  393. return INTERNAL_ERROR;
  394. }
  395. subgraph_context_->all_inputs_[input_offset] = *tensor;
  396. if (execution_context_->trace_enabled) {
  397. subgraph_context_->all_inputs_[input_offset].SetName(
  398. node_item_->NodeName() + "_in_" + std::to_string(dst_input_idx));
  399. }
  400. }
  401. }
  402. (void)guard;
  403. return SUCCESS;
  404. }
  405. const void *TaskContext::GetVarBaseAddr() {
  406. return execution_context_->model->GetVarMemBase();
  407. }
  408. const char *TaskContext::GetNodeName() const {
  409. return node_item_->NodeName().c_str();
  410. }
  411. void TaskContext::ReleaseInputsAndOutputs() {
  412. for (int i = 0; i < node_item_->num_inputs; ++i) {
  413. auto tensor = inputs_start_ + i;
  414. tensor->Destroy();
  415. GELOGD("[%s] Tensor of input[%d] released", GetNodeName(), i);
  416. }
  417. for (int i = 0; i < node_item_->num_outputs; ++i) {
  418. auto tensor = outputs_start_ + i;
  419. tensor->Destroy();
  420. GELOGD("[%s] Tensor of output[%d] released", GetNodeName(), i);
  421. }
  422. }
  423. void TaskContext::ReleaseInput(int index) {
  424. auto input_tensor = MutableInput(index);
  425. if (input_tensor != nullptr) {
  426. node_state_->SavePersistTensor(index, *input_tensor);
  427. input_tensor->Destroy();
  428. GELOGD("[%s] Tensor of input[%d] released", GetNodeName(), index);
  429. }
  430. }
  431. ConstGeTensorDescPtr TaskContext::GetOutputDesc(int index) const {
  432. return node_item_->MutableOutputDesc(static_cast<uint32_t>(index));
  433. }
  434. ConstGeTensorDescPtr TaskContext::GetInputDesc(int index) const {
  435. return node_item_->MutableInputDesc(index);
  436. }
  437. GeTensorDescPtr TaskContext::MutableInputDesc(int index) const {
  438. return node_item_->MutableInputDesc(index);
  439. }
  440. GeTensorDescPtr TaskContext::MutableOutputDesc(int index) const {
  441. return node_item_->MutableOutputDesc(static_cast<uint32_t>(index));
  442. }
  443. bool TaskContext::IsForceInferShape() const {
  444. return force_infer_shape_;
  445. }
  446. void TaskContext::SetForceInferShape(bool force_infer_shape) {
  447. force_infer_shape_ = force_infer_shape;
  448. }
  449. void TaskContext::NodeDone() {
  450. subgraph_context_->NodeDone(node_item_->node);
  451. }
  452. void TaskContext::OnError(Status error) {
  453. subgraph_context_->OnError(error);
  454. execution_context_->SetErrorCode(error);
  455. }
  456. bool TaskContext::IsTraceEnabled() const {
  457. return execution_context_->trace_enabled;
  458. }
  459. TensorValue *TaskContext::GetVariable(const std::string &name) {
  460. return execution_context_->model->GetVariable(name);
  461. }
  462. uint64_t TaskContext::GetIterationNumber() const {
  463. return iteration_;
  464. }
  465. bool TaskContext::IsDumpEnabled() const {
  466. return execution_context_->dump_enabled;
  467. }
  468. Status TaskContext::TryExecuteCallback(const function<void()> &callback_fun) const {
  469. if (!callback_fun) {
  470. return SUCCESS;
  471. }
  472. if (node_item_->has_observer) {
  473. return RegisterCallback(callback_fun);
  474. }
  475. callback_fun();
  476. return SUCCESS;
  477. }
  478. const DumpProperties &TaskContext::GetDumpProperties() const {
  479. return execution_context_->dump_properties;
  480. }
  481. bool TaskContext::NeedCallback() {
  482. return node_item_->has_observer || IsDumpEnabled() || GraphExecutionContext::profiling_level > 0 ||
  483. !execution_context_->model->IsSingleOp() || ProfilingManager::Instance().ProfilingModelLoadOn();
  484. }
  485. Status TaskContext::Synchronize() {
  486. return execution_context_->Synchronize(GetStream());
  487. }
  488. Status TaskContext::SaveProfilingTaskDescInfo(uint32_t task_id, uint32_t stream_id,
  489. const std::string &task_type, uint32_t block_dim) {
  490. if (ProfilingManager::Instance().ProfilingModelLoadOn()) {
  491. const NodeItem &node_item = GetNodeItem();
  492. auto op_desc = node_item.GetOpDesc();
  493. GE_CHECK_NOTNULL(op_desc);
  494. const GraphExecutionContext *graph_context = GetExecutionContext();
  495. GE_CHECK_NOTNULL(graph_context);
  496. const HybridModel *model = graph_context->model;
  497. GE_CHECK_NOTNULL(model);
  498. std::string dynamic_model_name = model->GetModelName();
  499. TaskDescInfo tmp_task_desc_info;
  500. tmp_task_desc_info.model_name = dynamic_model_name;
  501. tmp_task_desc_info.op_name = op_desc->GetName();
  502. tmp_task_desc_info.op_type = op_desc->GetType();
  503. tmp_task_desc_info.block_dim = block_dim;
  504. tmp_task_desc_info.task_type = task_type;
  505. tmp_task_desc_info.task_id = task_id;
  506. tmp_task_desc_info.stream_id = stream_id;
  507. tmp_task_desc_info.shape_type = "dynamic";
  508. tmp_task_desc_info.cur_iter_num = iteration_ + 1;
  509. task_desc_info.emplace_back(tmp_task_desc_info);
  510. }
  511. return SUCCESS;
  512. }
  513. NodeState *TaskContext::GetNodeState() const {
  514. return node_state_;
  515. }
  516. Status TaskContext::GetInputDesc(int index, GeTensorDesc &tensor_desc) const {
  517. return node_item_->GetInputDesc(index, tensor_desc);
  518. }
  519. Status TaskContext::UpdateInputDesc(int index, const GeTensorDesc &tensor_desc) {
  520. return const_cast<NodeItem *>(node_item_)->UpdateInputDesc(index, tensor_desc);
  521. }
  522. Status TaskContext::GetOutputDesc(int index, GeTensorDesc &tensor_desc) const {
  523. return node_item_->GetOutputDesc(index, tensor_desc);
  524. }
  525. } // namespace hybrid
  526. } // namespace ge

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