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
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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(), index, outputs_start_[index].GetSize());
  203. }
  204. }
  205. }
  206. if (execution_context_->trace_enabled) {
  207. outputs_start_[index].SetName(node_item_->NodeName() + "_out_" + std::to_string(index));
  208. }
  209. if (tensor != nullptr) {
  210. *tensor = outputs_start_ + index;
  211. }
  212. return SUCCESS;
  213. }
  214. Status TaskContext::AllocateOutputs(AllocationAttr *attr) {
  215. for (int i = 0; i < node_item_->num_outputs; ++i) {
  216. const auto &output_desc = node_item_->MutableOutputDesc(i);
  217. GE_CHECK_NOTNULL(output_desc);
  218. uint32_t mem_type = 0;
  219. (void)AttrUtils::GetInt(output_desc, ATTR_OUTPUT_MEMORY_TYPE, mem_type);
  220. if (attr == nullptr) {
  221. auto tmp_attr = AllocationAttr(0, nullptr, static_cast<MemStorageType>(mem_type));
  222. GE_CHK_STATUS_RET_NOLOG(AllocateOutput(i, *output_desc, nullptr, &tmp_attr));
  223. } else {
  224. attr->SetMemType(static_cast<MemStorageType>(mem_type));
  225. GE_CHK_STATUS_RET_NOLOG(AllocateOutput(i, *output_desc, nullptr, attr));
  226. }
  227. }
  228. return SUCCESS;
  229. }
  230. Status TaskContext::AllocateTensor(size_t size, TensorValue &tensor, AllocationAttr *attr) {
  231. auto buffer = TensorBuffer::Create(execution_context_->allocator, size, attr);
  232. if (buffer == nullptr) {
  233. GELOGE(MEMALLOC_FAILED, "Failed to allocate buffer of size: %zu", size);
  234. return MEMALLOC_FAILED;
  235. }
  236. tensor = TensorValue(shared_ptr<TensorBuffer>(buffer.release()));
  237. return SUCCESS;
  238. }
  239. const NodeItem &TaskContext::GetNodeItem() const {
  240. return *node_item_;
  241. }
  242. Status TaskContext::SetOutput(int index, const TensorValue &tensor) {
  243. if (index < 0 || index >= node_item_->num_outputs) {
  244. GELOGE(PARAM_INVALID, "output index out of range. num_output = %d, index = %d", node_item_->num_outputs, index);
  245. return PARAM_INVALID;
  246. }
  247. GELOGD("Set %s:%d with tensor: %s",
  248. node_item_->NodeName().c_str(),
  249. index,
  250. tensor.DebugString().c_str());
  251. outputs_start_[index] = tensor;
  252. return SUCCESS;
  253. }
  254. rtStream_t TaskContext::GetStream() {
  255. return execution_context_->stream;
  256. }
  257. int64_t TaskContext::GetSessionId() const {
  258. return execution_context_->session_id;
  259. }
  260. Status TaskContext::GetStatus() const {
  261. return status_;
  262. }
  263. void TaskContext::SetStatus(Status status) {
  264. status_ = status;
  265. if (status != SUCCESS) {
  266. execution_context_->SetErrorCode(status);
  267. }
  268. }
  269. Status TaskContext::AllocateWorkspace(size_t size, void **buffer, void *ori_addr) {
  270. GE_CHECK_NOTNULL(buffer);
  271. if (ori_addr == nullptr) {
  272. *buffer = execution_context_->allocator->Allocate(size, nullptr);
  273. } else {
  274. AllocationAttr attr(ori_addr);
  275. *buffer = execution_context_->allocator->Allocate(size, &attr);
  276. }
  277. if (*buffer == nullptr) {
  278. GELOGE(MEMALLOC_FAILED, "Failed to allocate workspace of size = %zu", size);
  279. return MEMALLOC_FAILED;
  280. }
  281. GELOGD("Allocating workspace of size = %zu successfully", size);
  282. workspaces_.emplace_back(*buffer);
  283. return SUCCESS;
  284. }
  285. Status TaskContext::PropagateOutputs() {
  286. // propagate outputs
  287. for (int i = 0; i < NumOutputs(); ++i) {
  288. auto tensor = MutableOutput(i);
  289. GE_CHECK_NOTNULL(tensor);
  290. if (tensor->GetData() == nullptr) {
  291. GELOGD("[%s] Node output[%d] is null.", node_item_->NodeName().c_str(), i);
  292. }
  293. auto &output_nodes = node_item_->outputs[i];
  294. for (auto &dst_input_index_and_node : output_nodes) {
  295. auto dst_input_idx = dst_input_index_and_node.first;
  296. auto dst_node_item = dst_input_index_and_node.second;
  297. auto input_offset = dst_node_item->input_start + dst_input_idx;
  298. GELOGD(
  299. "Propagate output of node %s, output index = %d, dst node = %s, "
  300. "dst_input_index = %d, dst_input_offset = %d.",
  301. node_item_->NodeName().c_str(),
  302. i,
  303. dst_node_item->NodeName().c_str(),
  304. dst_input_idx,
  305. input_offset);
  306. if (subgraph_context_->all_inputs_.size() <= static_cast<size_t>(input_offset)) {
  307. GELOGE(INTERNAL_ERROR,
  308. "[%s] input index out of range. index = %d, total input num = %zu",
  309. GetNodeName(),
  310. input_offset,
  311. subgraph_context_->all_inputs_.size());
  312. return INTERNAL_ERROR;
  313. }
  314. subgraph_context_->all_inputs_[input_offset] = *tensor;
  315. if (execution_context_->trace_enabled) {
  316. subgraph_context_->all_inputs_[input_offset].SetName(
  317. node_item_->NodeName() + "_in_" + std::to_string(dst_input_idx));
  318. }
  319. }
  320. }
  321. return SUCCESS;
  322. }
  323. const void *TaskContext::GetVarBaseAddr() {
  324. return execution_context_->model->GetVarMemBase();
  325. }
  326. const char *TaskContext::GetNodeName() const {
  327. return node_item_->NodeName().c_str();
  328. }
  329. void TaskContext::ReleaseInput(int index) {
  330. auto input_tensor = MutableInput(index);
  331. if (input_tensor != nullptr) {
  332. input_tensor->Destroy();
  333. GELOGD("[%s] Tensor of input[%d] released", GetNodeName(), index);
  334. }
  335. }
  336. ConstGeTensorDescPtr TaskContext::GetOutputDesc(int index) const {
  337. return node_item_->MutableOutputDesc(static_cast<uint32_t>(index));
  338. }
  339. ConstGeTensorDescPtr TaskContext::GetInputDesc(int index) const {
  340. return node_item_->MutableInputDesc(index);
  341. }
  342. GeTensorDescPtr TaskContext::MutableInputDesc(int index) const {
  343. return node_item_->MutableInputDesc(index);
  344. }
  345. GeTensorDescPtr TaskContext::MutableOutputDesc(int index) const {
  346. return node_item_->MutableOutputDesc(static_cast<uint32_t>(index));
  347. }
  348. bool TaskContext::IsForceInferShape() const {
  349. return force_infer_shape_;
  350. }
  351. void TaskContext::SetForceInferShape(bool force_infer_shape) {
  352. force_infer_shape_ = force_infer_shape;
  353. }
  354. void TaskContext::NodeDone() {
  355. subgraph_context_->NodeDone(node_item_->node);
  356. }
  357. void TaskContext::OnError(Status error) {
  358. subgraph_context_->OnError(error);
  359. execution_context_->SetErrorCode(error);
  360. }
  361. bool TaskContext::IsTraceEnabled() const {
  362. return execution_context_->trace_enabled;
  363. }
  364. TensorValue *TaskContext::GetVariable(const std::string &name) {
  365. return execution_context_->model->GetVariable(name);
  366. }
  367. uint64_t TaskContext::GetIterationNumber() const {
  368. return iteration_;
  369. }
  370. bool TaskContext::IsDumpEnabled() const {
  371. return execution_context_->dump_enabled;
  372. }
  373. Status TaskContext::TryExecuteCallback(const function<void()> &callback_fun) const {
  374. if (!callback_fun) {
  375. return SUCCESS;
  376. }
  377. if (node_item_->has_observer) {
  378. return RegisterCallback(callback_fun);
  379. }
  380. callback_fun();
  381. return SUCCESS;
  382. }
  383. const DumpProperties &TaskContext::GetDumpProperties() const {
  384. return execution_context_->dump_properties;
  385. }
  386. } // namespace hybrid
  387. } // namespace ge

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