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

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

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