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

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