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

5 years ago
5 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
4 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
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "framework/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 "framework/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, "[New][RunAsyncListener] 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) {}
  64. SubGraphInfo::~SubGraphInfo() {
  65. }
  66. GraphModelListener::GraphModelListener(std::mutex &mutex, std::condition_variable &cond)
  67. : result_code_(0), is_finished_(false), mutex_(mutex), condition_(cond) {}
  68. Status GraphModelListener::OnComputeDone(uint32_t model_id, uint32_t task_id, uint32_t result,
  69. std::vector<ge::Tensor> &outputs) {
  70. GELOGI(
  71. "[GraphManager] graph compute call back, model_id:%u, task_id:%u, "
  72. "resultCode:%u.",
  73. model_id, task_id, result);
  74. std::lock_guard<std::mutex> lock(mutex_);
  75. result_code_ = result;
  76. is_finished_ = true;
  77. condition_.notify_all();
  78. return SUCCESS;
  79. }
  80. uint32_t GraphModelListener::GetResultCode() const {
  81. if (!is_finished_) {
  82. REPORT_CALL_ERROR("E19999", "Model not run finish");
  83. GELOGE(INTERNAL_ERROR, "[Check][Param] model not run finish.");
  84. return INTERNAL_ERROR;
  85. }
  86. return result_code_;
  87. }
  88. Status GraphModelListener::ResetResult() {
  89. std::lock_guard<std::mutex> lock(mutex_);
  90. result_code_ = 0;
  91. is_finished_ = false;
  92. return SUCCESS;
  93. }
  94. void RunAsyncListener::SetCallback(const RunAsyncCallback &callback) {
  95. sem_.Push(0);
  96. callback_ = callback;
  97. }
  98. Status RunAsyncListener::OnComputeDone(uint32_t model_id, uint32_t task_id, uint32_t result,
  99. std::vector<ge::Tensor> &outputs) {
  100. GELOGI("[GraphManager] run graph async call back, modelId:%u, taskId:%u, resultCode:%u.",
  101. model_id, task_id, result);
  102. GE_CHECK_NOTNULL(callback_);
  103. callback_(result, outputs);
  104. uint8_t unused;
  105. sem_.Pop(unused);
  106. return SUCCESS;
  107. }
  108. bool HasCalcOp(const ComputeGraphPtr &graph) {
  109. if (graph == nullptr) {
  110. return false;
  111. }
  112. static const std::set<std::string> calc_op_type = {CONVOLUTION, DECONVOLUTION, FULL_CONNECTION};
  113. for (const auto &node : graph->GetAllNodes()) {
  114. OpDescPtr op_desc = node->GetOpDesc();
  115. GE_IF_BOOL_EXEC(op_desc == nullptr,
  116. REPORT_INNER_ERROR("E19999", "GetOpDesc failed, Node GetOpDesc is nullptr");
  117. GELOGE(FAILED, "[Get][OpDesc] failed, Node GetOpDesc is nullptr"); return false);
  118. if (calc_op_type.find(op_desc->GetType()) != calc_op_type.end()) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. } // namespace ge

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