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 5.1 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. void GraphNode::IncreaseLoadCount() {
  55. std::unique_lock<std::mutex> lock(load_count_mu_);
  56. if (load_record_ == kMaxLoadNum) {
  57. GELOGW("Reach the maximum of load_count:%u", kMaxLoadNum);
  58. return;
  59. }
  60. ++load_count_;
  61. }
  62. SubGraphInfo::SubGraphInfo() : subgraph_ptr_(nullptr), ge_model_ptr_(nullptr), malloc_flag_(false) {}
  63. SubGraphInfo::~SubGraphInfo() {
  64. if (malloc_flag_) {
  65. for (auto &buffer_addr : buffer_addr_) {
  66. if (buffer_addr == nullptr) {
  67. continue;
  68. }
  69. rtError_t rt_ret;
  70. rt_ret = rtFreeHost(buffer_addr);
  71. buffer_addr = nullptr;
  72. if (rt_ret != RT_ERROR_NONE) {
  73. GELOGE(rt_ret, "[GraphManager] subgraph free buffer failed, modelId = %u", model_id_info_.model_id);
  74. }
  75. }
  76. }
  77. }
  78. Status SubGraphInfo::FreeInOutBuffer() {
  79. if (malloc_flag_) {
  80. for (auto iter = buffer_addr_.begin(); iter != buffer_addr_.end(); ++iter) {
  81. rtError_t rt_ret;
  82. rt_ret = rtFreeHost(*iter);
  83. if (rt_ret != RT_ERROR_NONE) {
  84. GELOGE(rt_ret, "[GraphManager] subgraph free buffer failed, modelId = %u", model_id_info_.model_id);
  85. buffer_addr_.erase(buffer_addr_.begin(), iter);
  86. return GE_GRAPH_FREE_FAILED;
  87. }
  88. }
  89. buffer_addr_.clear();
  90. malloc_flag_ = false;
  91. return SUCCESS;
  92. } else {
  93. GELOGI("[GraphManager] not malloc buffer, modelId = %u", model_id_info_.model_id);
  94. return SUCCESS;
  95. }
  96. }
  97. GraphModelListener::GraphModelListener(std::mutex &mutex, std::condition_variable &cond)
  98. : result_code_(0), is_finished_(false), mutex_(mutex), condition_(cond) {}
  99. Status GraphModelListener::OnComputeDone(uint32_t model_id, uint32_t task_id, uint32_t result,
  100. std::vector<ge::OutputTensorInfo> &outputs) {
  101. GELOGI(
  102. "[GraphManager] graph compute call back, model_id:%u, task_id:%u, "
  103. "resultCode:%u.",
  104. model_id, task_id, result);
  105. std::lock_guard<std::mutex> lock(mutex_);
  106. result_code_ = result;
  107. is_finished_ = true;
  108. condition_.notify_all();
  109. return SUCCESS;
  110. }
  111. uint32_t GraphModelListener::GetResultCode() const {
  112. if (!is_finished_) {
  113. GELOGE(INTERNAL_ERROR, "[GraphManager] model not run finish.");
  114. return INTERNAL_ERROR;
  115. }
  116. return result_code_;
  117. }
  118. Status GraphModelListener::ResetResult() {
  119. std::lock_guard<std::mutex> lock(mutex_);
  120. result_code_ = 0;
  121. is_finished_ = false;
  122. return SUCCESS;
  123. }
  124. void RunAsyncListener::SetCallback(const RunAsyncCallback &callback) {
  125. sem_.Push(0);
  126. callback_ = callback;
  127. }
  128. Status RunAsyncListener::OnComputeDone(uint32_t model_id, uint32_t task_id, uint32_t result,
  129. std::vector<ge::OutputTensorInfo> &outputs) {
  130. GELOGI("[GraphManager] run graph async call back, modelId:%u, taskId:%u, resultCode:%u.",
  131. model_id, task_id, result);
  132. GE_CHECK_NOTNULL(callback_);
  133. callback_(result, outputs);
  134. uint8_t unused;
  135. sem_.Pop(unused);
  136. return SUCCESS;
  137. }
  138. bool HasCalcOp(const ComputeGraphPtr &graph) {
  139. if (graph == nullptr) {
  140. return false;
  141. }
  142. static const std::set<std::string> calc_op_type = {CONVOLUTION, DECONVOLUTION, FULL_CONNECTION};
  143. for (const auto &node : graph->GetAllNodes()) {
  144. OpDescPtr op_desc = node->GetOpDesc();
  145. GE_IF_BOOL_EXEC(op_desc == nullptr, GELOGE(FAILED, "Node GetOpDesc is nullptr"); return false);
  146. if (calc_op_type.find(op_desc->GetType()) != calc_op_type.end()) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. } // namespace ge

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