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.

subgraph_context.cc 5.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright 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 "subgraph_context.h"
  17. #include "common/debug/log.h"
  18. #include "hybrid/executor/hybrid_model_executor.h"
  19. namespace ge {
  20. namespace hybrid {
  21. SubgraphContext::SubgraphContext(const GraphItem *graph_item, const GraphExecutionContext *execution_context)
  22. : graph_item_(graph_item), execution_context_(execution_context) {
  23. }
  24. Status SubgraphContext::Init() {
  25. GE_CHECK_NOTNULL(graph_item_);
  26. GELOGD("[%s] Start to init subgraph context. total inputs = %d, total outputs = %d",
  27. graph_item_->GetName().c_str(),
  28. graph_item_->TotalInputs(),
  29. graph_item_->TotalOutputs());
  30. all_inputs_.resize(static_cast<unsigned long>(graph_item_->TotalInputs()));
  31. all_outputs_.resize(static_cast<unsigned long>(graph_item_->TotalOutputs()));
  32. return SUCCESS;
  33. }
  34. void SubgraphContext::SetGroup(int group) {
  35. group_ = group;
  36. }
  37. void SubgraphContext::ResetContext(const NodePtr &node) {
  38. node_done_manager_.Reset(node);
  39. }
  40. NodeStatePtr SubgraphContext::GetOrCreateNodeState(const NodeItem *node_item) {
  41. std::lock_guard<std::mutex> lk(mu_);
  42. auto &node_state = node_states_[node_item];
  43. if (node_state == nullptr) {
  44. const auto &guard = node_item->MutexGuard("GetOrCreateNodeState");
  45. node_state.reset(new(std::nothrow)NodeState(*node_item, this));
  46. node_state->SetGroup(group_);
  47. (void)guard;
  48. }
  49. return node_state;
  50. }
  51. Status SubgraphContext::SetInput(int index, const TensorValue &tensor) {
  52. if (static_cast<size_t>(index) >= all_inputs_.size()) {
  53. GELOGE(INTERNAL_ERROR,
  54. "[Check][Param:index]input index out of range. all input num = %zu, input index = %d",
  55. all_inputs_.size(), index);
  56. REPORT_INNER_ERROR("E19999", "input param index out of range, all input num = %zu, input index = %d.",
  57. all_inputs_.size(), index);
  58. return INTERNAL_ERROR;
  59. }
  60. all_inputs_[index] = tensor;
  61. return SUCCESS;
  62. }
  63. Status SubgraphContext::SetInput(const NodeItem &node_item, int input_index, const TensorValue &tensor) {
  64. auto index = node_item.input_start + input_index;
  65. return SetInput(index, tensor);
  66. }
  67. Status SubgraphContext::SetOutput(const NodeItem &node_item, int output_index, const TensorValue &tensor) {
  68. auto index = node_item.output_start + output_index;
  69. if ((output_index >= node_item.num_outputs) || (static_cast<size_t>(index) >= all_outputs_.size())) {
  70. GELOGE(INTERNAL_ERROR, "[Check][Param:output_index]output index out of range. all output num = %zu,"
  71. "node_item = %s, output index = %d.",
  72. all_outputs_.size(), node_item.DebugString().c_str(), output_index);
  73. REPORT_INNER_ERROR("E19999", "output index out of range. all output num = %zu, node_item = %s, output index = %d.",
  74. all_outputs_.size(), node_item.DebugString().c_str(), output_index);
  75. return INTERNAL_ERROR;
  76. }
  77. all_outputs_[index] = tensor;
  78. return SUCCESS;
  79. }
  80. Status SubgraphContext::GetInput(int index, TensorValue &tensor) {
  81. GE_CHECK_GE(all_inputs_.size(), index + 1U);
  82. tensor = all_inputs_[index];
  83. return SUCCESS;
  84. }
  85. Status SubgraphContext::GetOutputs(std::vector<TensorValue> &outputs) {
  86. if (graph_item_->IsDynamic()) {
  87. GELOGD("[%s] graph is dynamic, get outputs from net output input tensors", graph_item_->GetName().c_str());
  88. // get from net output inputs
  89. auto output_node = graph_item_->GetOutputNode();
  90. if (output_node != nullptr) {
  91. for (int i = 0; i < output_node->num_inputs; ++i) {
  92. TensorValue tensor;
  93. GE_CHK_STATUS_RET_NOLOG(GetInput(output_node->input_start + i, tensor));
  94. GELOGD("[%s] Adding output tensor by input index [%d], tensor = %s",
  95. graph_item_->GetName().c_str(),
  96. output_node->input_start + i,
  97. tensor.DebugString().c_str());
  98. outputs.emplace_back(std::move(tensor));
  99. }
  100. }
  101. } else {
  102. GELOGD("[%s] graph is non-dynamic, get outputs from subgraph outputs", graph_item_->GetName().c_str());
  103. for (auto &tensor : all_outputs_) {
  104. GELOGD("[%s] Adding output tensor: %s", graph_item_->GetName().c_str(), tensor.DebugString().c_str());
  105. outputs.emplace_back(tensor);
  106. }
  107. }
  108. return SUCCESS;
  109. }
  110. Status SubgraphContext::Await(const NodePtr &node) {
  111. if (node_done_manager_.Await(node)) {
  112. return SUCCESS;
  113. }
  114. if (execution_context_->is_eos_) {
  115. return END_OF_SEQUENCE;
  116. }
  117. return FAILED;
  118. }
  119. void SubgraphContext::OnError(Status error) {
  120. if (error != END_OF_SEQUENCE) {
  121. GELOGE(error, "[Check][Param:error][%s] Error:%d occurred while executing graph.",
  122. graph_item_->GetName().c_str(), error);
  123. REPORT_INNER_ERROR("E19999", "[%s] Error:%d occurred while executing graph.",
  124. graph_item_->GetName().c_str(), error);
  125. }
  126. node_done_manager_.Destroy();
  127. }
  128. void SubgraphContext::NodeDone(const NodePtr &node) {
  129. node_done_manager_.NodeDone(node);
  130. }
  131. } // namespace hybrid
  132. } // namespace ge

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