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.

graph_manager_utils.cc 4.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 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
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "graph/manager/graph_manager_utils.h"
  17. #include <set>
  18. #include <utility>
  19. #include "framework/common/debug/ge_log.h"
  20. #include "common/ge/ge_util.h"
  21. #include "common/string_util.h"
  22. #include "graph/debug/ge_attr_define.h"
  23. #include "graph/compute_graph.h"
  24. #include "graph/op_desc.h"
  25. #include "graph/optimize/common/params.h"
  26. #include "omg/omg_inner_types.h"
  27. #include "runtime/mem.h"
  28. namespace ge {
  29. using OpDescPtr = std::shared_ptr<OpDesc>;
  30. GraphNode::GraphNode(GraphId graph_id)
  31. : graph_id_(graph_id),
  32. run_flag_(false),
  33. subgraph_ptr_list_(),
  34. graph_(nullptr),
  35. compute_graph_(nullptr),
  36. build_flag_(false),
  37. load_flag_(false),
  38. async_(false),
  39. ge_model_(nullptr),
  40. sem_(1) {
  41. graph_run_async_listener_ = MakeShared<RunAsyncListener>();
  42. if (graph_run_async_listener_ == nullptr) {
  43. GELOGE(MEMALLOC_FAILED, "Make shared failed");
  44. }
  45. }
  46. GraphNode::~GraphNode() = default;
  47. void GraphNode::Lock() {
  48. sem_.Push(0);
  49. }
  50. void GraphNode::Unlock() {
  51. uint8_t unused;
  52. sem_.Pop(unused);
  53. }
  54. SubGraphInfo::SubGraphInfo() : subgraph_ptr_(nullptr), ge_model_ptr_(nullptr), malloc_flag_(false) {}
  55. SubGraphInfo::~SubGraphInfo() {
  56. if (malloc_flag_) {
  57. for (auto &buffer_addr : buffer_addr_) {
  58. if (buffer_addr == nullptr) {
  59. continue;
  60. }
  61. rtError_t rt_ret;
  62. rt_ret = rtFreeHost(buffer_addr);
  63. buffer_addr = nullptr;
  64. if (rt_ret != RT_ERROR_NONE) {
  65. GELOGE(rt_ret, "[GraphManager] subgraph free buffer failed, modelId = %u", model_id_info_.model_id);
  66. }
  67. }
  68. }
  69. }
  70. Status SubGraphInfo::FreeInOutBuffer() {
  71. if (malloc_flag_) {
  72. for (auto iter = buffer_addr_.begin(); iter != buffer_addr_.end(); ++iter) {
  73. rtError_t rt_ret;
  74. rt_ret = rtFreeHost(*iter);
  75. if (rt_ret != RT_ERROR_NONE) {
  76. GELOGE(rt_ret, "[GraphManager] subgraph free buffer failed, modelId = %u", model_id_info_.model_id);
  77. buffer_addr_.erase(buffer_addr_.begin(), iter);
  78. return GE_GRAPH_FREE_FAILED;
  79. }
  80. }
  81. buffer_addr_.clear();
  82. malloc_flag_ = false;
  83. return SUCCESS;
  84. } else {
  85. GELOGI("[GraphManager] not malloc buffer, modelId = %u", model_id_info_.model_id);
  86. return SUCCESS;
  87. }
  88. }
  89. GraphModelListener::GraphModelListener(std::mutex &mutex, std::condition_variable &cond)
  90. : result_code_(0), is_finished_(false), mutex_(mutex), condition_(cond) {}
  91. Status GraphModelListener::OnComputeDone(uint32_t model_id, uint32_t task_id, uint32_t result,
  92. std::vector<ge::OutputTensorInfo> &outputs) {
  93. GELOGI(
  94. "[GraphManager] graph compute call back, model_id:%u, task_id:%u, "
  95. "resultCode:%u.",
  96. model_id, task_id, result);
  97. std::lock_guard<std::mutex> lock(mutex_);
  98. result_code_ = result;
  99. is_finished_ = true;
  100. condition_.notify_all();
  101. return SUCCESS;
  102. }
  103. uint32_t GraphModelListener::GetResultCode() const {
  104. if (!is_finished_) {
  105. GELOGE(INTERNAL_ERROR, "[GraphManager] model not run finish.");
  106. return INTERNAL_ERROR;
  107. }
  108. return result_code_;
  109. }
  110. Status GraphModelListener::ResetResult() {
  111. std::lock_guard<std::mutex> lock(mutex_);
  112. result_code_ = 0;
  113. is_finished_ = false;
  114. return SUCCESS;
  115. }
  116. void RunAsyncListener::SetCallback(const RunAsyncCallback &callback) {
  117. sem_.Push(0);
  118. callback_ = callback;
  119. }
  120. Status RunAsyncListener::OnComputeDone(uint32_t model_id, uint32_t task_id, uint32_t result,
  121. std::vector<ge::OutputTensorInfo> &outputs) {
  122. GELOGI("[GraphManager] run graph async call back, modelId:%u, taskId:%u, resultCode:%u.",
  123. model_id, task_id, result);
  124. GE_CHECK_NOTNULL(callback_);
  125. callback_(result, outputs);
  126. uint8_t unused;
  127. sem_.Pop(unused);
  128. return SUCCESS;
  129. }
  130. bool HasCalcOp(const ComputeGraphPtr &graph) {
  131. if (graph == nullptr) {
  132. return false;
  133. }
  134. static const std::set<std::string> calc_op_type = {CONVOLUTION, DECONVOLUTION, FULL_CONNECTION};
  135. for (const auto &node : graph->GetAllNodes()) {
  136. OpDescPtr op_desc = node->GetOpDesc();
  137. GE_IF_BOOL_EXEC(op_desc == nullptr, GELOGE(FAILED, "Node GetOpDesc is nullptr"); return false);
  138. if (calc_op_type.find(op_desc->GetType()) != calc_op_type.end()) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. } // namespace ge

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