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 15 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. auto ret = execution_context_->callback_manager->RegisterCallback(callback_fun);
  129. if (ret != SUCCESS) {
  130. GELOGE(ret, "[%s] Failed to register callback", GetNodeName());
  131. execution_context_->callback_manager->Destroy();
  132. return ret;
  133. }
  134. return SUCCESS;
  135. }
  136. string TaskContext::TensorDesc2String(const GeTensorDesc &desc) {
  137. std::stringstream ss;
  138. ss << "[TensorDesc] ";
  139. ss << "DataType = " << desc.GetDataType();
  140. ss << ", Format = " << desc.GetFormat();
  141. ss << ", Shape = [";
  142. for (auto dim : desc.GetShape().GetDims()) {
  143. ss << dim << ", ";
  144. }
  145. ss << "]";
  146. return ss.str();
  147. }
  148. Status TaskContext::AllocateTensor(const GeTensorDesc &tensor_desc, TensorValue &tensor, AllocationAttr *attr) {
  149. int64_t size = 0;
  150. if (ge::TensorUtils::GetSize(tensor_desc, size) != GRAPH_SUCCESS) {
  151. GELOGE(INTERNAL_ERROR, "Failed to get tensor size");
  152. return INTERNAL_ERROR;
  153. }
  154. if (size == 0) {
  155. GELOGW("size from tensor_desc == 0");
  156. }
  157. auto buffer = TensorBuffer::Create(execution_context_->allocator, size, attr);
  158. GE_CHECK_NOTNULL(buffer);
  159. tensor = TensorValue(shared_ptr<TensorBuffer>(buffer.release()));
  160. return SUCCESS;
  161. }
  162. Status TaskContext::AllocateOutput(int index,
  163. const GeTensorDesc &tensor_desc,
  164. TensorValue **tensor,
  165. AllocationAttr *attr) {
  166. GELOGI("To allocate output for node: %s. index = %d, tensor desc = %s",
  167. node_item_->NodeName().c_str(),
  168. index,
  169. TensorDesc2String(tensor_desc).c_str());
  170. if (index < 0 || index >= node_item_->num_outputs) {
  171. GELOGE(PARAM_INVALID, "output index out of range. num_output = %d, index = %d", node_item_->num_outputs, index);
  172. return PARAM_INVALID;
  173. }
  174. if (outputs_start_[index].GetData() != nullptr) {
  175. GELOGI("already allocated as net output");
  176. return SUCCESS;
  177. }
  178. auto it = node_item_->ref_outputs.find(index);
  179. if (it != node_item_->ref_outputs.end()) {
  180. auto &ref_node = it->second;
  181. GELOGD("source node of %s:%d = %s, op_type = %s",
  182. node_item_->NodeName().c_str(),
  183. index,
  184. ref_node->GetName().c_str(),
  185. ref_node->GetType().c_str());
  186. TensorValue *ref_tensor = execution_context_->model->GetVariable(ref_node->GetName());
  187. GE_CHECK_NOTNULL(ref_tensor);
  188. outputs_start_[index] = *ref_tensor;
  189. } else {
  190. auto reuse_output_it = node_item_->reuse_outputs.find(index);
  191. if (reuse_output_it != node_item_->reuse_outputs.end()) {
  192. GELOGD("[%s] reuse output [%d] with output [%d]", GetNodeName(), index, reuse_output_it->second);
  193. outputs_start_[index] = outputs_start_[reuse_output_it->second];
  194. } else {
  195. auto reuse_input = node_item_->reuse_inputs.find(index);
  196. if (reuse_input != node_item_->reuse_inputs.end()) {
  197. GELOGD("[%s] Output[%d] is referenced to input[%d]", GetNodeName(), index, reuse_input->second);
  198. outputs_start_[index] = inputs_start_[reuse_input->second];
  199. } else {
  200. GE_CHK_STATUS_RET_NOLOG(AllocateTensor(tensor_desc, outputs_start_[index], attr));
  201. GELOGD("Allocating output successfully. node: %s. index = %d, size = %zu",
  202. node_item_->NodeName().c_str(),
  203. index,
  204. outputs_start_[index].GetSize());
  205. }
  206. }
  207. }
  208. if (execution_context_->trace_enabled) {
  209. outputs_start_[index].SetName(node_item_->NodeName() + "_out_" + std::to_string(index));
  210. }
  211. if (tensor != nullptr) {
  212. *tensor = outputs_start_ + index;
  213. }
  214. return SUCCESS;
  215. }
  216. Status TaskContext::AllocateOutputs(AllocationAttr *attr) {
  217. for (int i = 0; i < node_item_->num_outputs; ++i) {
  218. const auto &output_desc = node_item_->MutableOutputDesc(i);
  219. GE_CHECK_NOTNULL(output_desc);
  220. uint32_t mem_type = 0;
  221. (void)AttrUtils::GetInt(output_desc, ATTR_OUTPUT_MEMORY_TYPE, mem_type);
  222. if (attr == nullptr) {
  223. auto tmp_attr = AllocationAttr(0, nullptr, static_cast<MemStorageType>(mem_type));
  224. GE_CHK_STATUS_RET_NOLOG(AllocateOutput(i, *output_desc, nullptr, &tmp_attr));
  225. } else {
  226. attr->SetMemType(static_cast<MemStorageType>(mem_type));
  227. GE_CHK_STATUS_RET_NOLOG(AllocateOutput(i, *output_desc, nullptr, attr));
  228. }
  229. }
  230. return SUCCESS;
  231. }
  232. Status TaskContext::AllocateTensor(size_t size, TensorValue &tensor, AllocationAttr *attr) {
  233. auto buffer = TensorBuffer::Create(execution_context_->allocator, size, attr);
  234. if (buffer == nullptr) {
  235. GELOGE(MEMALLOC_FAILED, "Failed to allocate buffer of size: %zu", size);
  236. return MEMALLOC_FAILED;
  237. }
  238. tensor = TensorValue(shared_ptr<TensorBuffer>(buffer.release()));
  239. return SUCCESS;
  240. }
  241. const NodeItem &TaskContext::GetNodeItem() const {
  242. return *node_item_;
  243. }
  244. Status TaskContext::SetOutput(int index, const TensorValue &tensor) {
  245. if (index < 0 || index >= node_item_->num_outputs) {
  246. GELOGE(PARAM_INVALID, "output index out of range. num_output = %d, index = %d", node_item_->num_outputs, index);
  247. return PARAM_INVALID;
  248. }
  249. GELOGD("Set %s:%d with tensor: %s",
  250. node_item_->NodeName().c_str(),
  251. index,
  252. tensor.DebugString().c_str());
  253. outputs_start_[index] = tensor;
  254. return SUCCESS;
  255. }
  256. rtStream_t TaskContext::GetStream() {
  257. return execution_context_->stream;
  258. }
  259. int64_t TaskContext::GetSessionId() const {
  260. return execution_context_->session_id;
  261. }
  262. Status TaskContext::GetStatus() const {
  263. return status_;
  264. }
  265. void TaskContext::SetStatus(Status status) {
  266. status_ = status;
  267. if (status != SUCCESS) {
  268. execution_context_->SetErrorCode(status);
  269. }
  270. }
  271. Status TaskContext::AllocateWorkspace(size_t size, void **buffer, void *ori_addr) {
  272. GE_CHECK_NOTNULL(buffer);
  273. if (ori_addr == nullptr) {
  274. *buffer = execution_context_->allocator->Allocate(size, nullptr);
  275. } else {
  276. AllocationAttr attr(ori_addr);
  277. *buffer = execution_context_->allocator->Allocate(size, &attr);
  278. }
  279. if (*buffer == nullptr) {
  280. GELOGE(MEMALLOC_FAILED, "Failed to allocate workspace of size = %zu", size);
  281. return MEMALLOC_FAILED;
  282. }
  283. GELOGD("Allocating workspace of size = %zu successfully", size);
  284. workspaces_.emplace_back(*buffer);
  285. return SUCCESS;
  286. }
  287. Status TaskContext::PropagateOutputs() {
  288. // propagate outputs
  289. for (int i = 0; i < NumOutputs(); ++i) {
  290. auto tensor = MutableOutput(i);
  291. GE_CHECK_NOTNULL(tensor);
  292. if (tensor->GetData() == nullptr) {
  293. GELOGD("[%s] Node output[%d] is null.", node_item_->NodeName().c_str(), i);
  294. }
  295. auto &output_nodes = node_item_->outputs[i];
  296. for (auto &dst_input_index_and_node : output_nodes) {
  297. auto dst_input_idx = dst_input_index_and_node.first;
  298. auto dst_node_item = dst_input_index_and_node.second;
  299. auto input_offset = dst_node_item->input_start + dst_input_idx;
  300. GELOGD(
  301. "Propagate output of node %s, output index = %d, dst node = %s, "
  302. "dst_input_index = %d, dst_input_offset = %d.",
  303. node_item_->NodeName().c_str(),
  304. i,
  305. dst_node_item->NodeName().c_str(),
  306. dst_input_idx,
  307. input_offset);
  308. if (subgraph_context_->all_inputs_.size() <= static_cast<size_t>(input_offset)) {
  309. GELOGE(INTERNAL_ERROR,
  310. "[%s] input index out of range. index = %d, total input num = %zu",
  311. GetNodeName(),
  312. input_offset,
  313. subgraph_context_->all_inputs_.size());
  314. return INTERNAL_ERROR;
  315. }
  316. subgraph_context_->all_inputs_[input_offset] = *tensor;
  317. if (execution_context_->trace_enabled) {
  318. subgraph_context_->all_inputs_[input_offset].SetName(
  319. node_item_->NodeName() + "_in_" + std::to_string(dst_input_idx));
  320. }
  321. }
  322. }
  323. return SUCCESS;
  324. }
  325. const void *TaskContext::GetVarBaseAddr() {
  326. return execution_context_->model->GetVarMemBase();
  327. }
  328. const char *TaskContext::GetNodeName() const {
  329. return node_item_->NodeName().c_str();
  330. }
  331. void TaskContext::ReleaseInput(int index) {
  332. auto input_tensor = MutableInput(index);
  333. if (input_tensor != nullptr) {
  334. input_tensor->Destroy();
  335. GELOGD("[%s] Tensor of input[%d] released", GetNodeName(), index);
  336. }
  337. }
  338. ConstGeTensorDescPtr TaskContext::GetOutputDesc(int index) const {
  339. return node_item_->MutableOutputDesc(static_cast<uint32_t>(index));
  340. }
  341. ConstGeTensorDescPtr TaskContext::GetInputDesc(int index) const {
  342. return node_item_->MutableInputDesc(index);
  343. }
  344. GeTensorDescPtr TaskContext::MutableInputDesc(int index) const {
  345. return node_item_->MutableInputDesc(index);
  346. }
  347. GeTensorDescPtr TaskContext::MutableOutputDesc(int index) const {
  348. return node_item_->MutableOutputDesc(static_cast<uint32_t>(index));
  349. }
  350. bool TaskContext::IsForceInferShape() const {
  351. return force_infer_shape_;
  352. }
  353. void TaskContext::SetForceInferShape(bool force_infer_shape) {
  354. force_infer_shape_ = force_infer_shape;
  355. }
  356. void TaskContext::NodeDone() {
  357. subgraph_context_->NodeDone(node_item_->node);
  358. }
  359. void TaskContext::OnError(Status error) {
  360. subgraph_context_->OnError(error);
  361. execution_context_->SetErrorCode(error);
  362. }
  363. bool TaskContext::IsTraceEnabled() const {
  364. return execution_context_->trace_enabled;
  365. }
  366. TensorValue *TaskContext::GetVariable(const std::string &name) {
  367. return execution_context_->model->GetVariable(name);
  368. }
  369. uint64_t TaskContext::GetIterationNumber() const {
  370. return iteration_;
  371. }
  372. bool TaskContext::IsDumpEnabled() const {
  373. return execution_context_->dump_enabled;
  374. }
  375. Status TaskContext::TryExecuteCallback(const function<void()> &callback_fun) const {
  376. if (!callback_fun) {
  377. return SUCCESS;
  378. }
  379. if (node_item_->has_observer) {
  380. return RegisterCallback(callback_fun);
  381. }
  382. callback_fun();
  383. return SUCCESS;
  384. }
  385. const DumpProperties &TaskContext::GetDumpProperties() const {
  386. return execution_context_->dump_properties;
  387. }
  388. } // namespace hybrid
  389. } // namespace ge

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