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.3 kB

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

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