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.

runtime_inference_context.cc 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "graph/runtime_inference_context.h"
  17. #include "graph/utils/tensor_adapter.h"
  18. #include <cstdint>
  19. #include "framework/common/debug/ge_log.h"
  20. namespace ge {
  21. std::map<std::string, std::unique_ptr<RuntimeInferenceContext>> RuntimeInferenceContext::contexts_;
  22. std::mutex RuntimeInferenceContext::ctx_mu_;
  23. graphStatus RuntimeInferenceContext::CreateContext(const std::string &context_id) {
  24. GELOGI("To create context. session id = %s", context_id.c_str());
  25. auto ctx = std::unique_ptr<RuntimeInferenceContext>(new (std::nothrow)RuntimeInferenceContext());
  26. if (ctx == nullptr) {
  27. GELOGE(GRAPH_FAILED,
  28. "Failed to create instance of RuntimeInferenceContext. context_id = %s",
  29. context_id.c_str());
  30. return GRAPH_FAILED;
  31. }
  32. std::lock_guard<std::mutex> lk(ctx_mu_);
  33. auto emplace_ret = contexts_.emplace(context_id, std::move(ctx));
  34. if (!emplace_ret.second) {
  35. GELOGE(GRAPH_FAILED, "Old context not destroyed");
  36. return GRAPH_FAILED;
  37. }
  38. return GRAPH_SUCCESS;
  39. }
  40. void RuntimeInferenceContext::DestroyContext(const std::string &context_id) {
  41. GELOGI("To destroy context. session id = %s", context_id.c_str());
  42. std::lock_guard<std::mutex> lk(ctx_mu_);
  43. contexts_.erase(context_id);
  44. }
  45. graphStatus RuntimeInferenceContext::GetContext(const std::string &context_id, RuntimeInferenceContext **ctx) {
  46. std::lock_guard<std::mutex> lk(ctx_mu_);
  47. auto it = contexts_.find(context_id);
  48. if (it != contexts_.end()) {
  49. *ctx = it->second.get();
  50. return GRAPH_SUCCESS;
  51. }
  52. GELOGD("Runtime inference context not created. session id = %s", context_id.c_str());
  53. return GRAPH_FAILED;
  54. }
  55. graphStatus RuntimeInferenceContext::SetTensor(int64_t node_id, int output_id, Tensor &&tensor) {
  56. std::lock_guard<std::mutex> lk(mu_);
  57. auto &output_tensors = tensors_[node_id];
  58. if (static_cast<uint32_t>(output_id) >= output_tensors.size()) {
  59. output_tensors.resize(output_id + 1);
  60. }
  61. GELOGD("Set tensor for node_id = %ld, output_id = %d", node_id, output_id);
  62. output_tensors[output_id] = std::move(tensor);
  63. auto &output_ge_tensors = ge_tensors_[node_id];
  64. if (static_cast<uint32_t>(output_id) >= output_ge_tensors.size()) {
  65. output_ge_tensors.resize(output_id + 1);
  66. }
  67. GELOGD("Set ge tensor for node_id = %ld, output_id = %d", node_id, output_id);
  68. output_ge_tensors[output_id] = TensorAdapter::AsGeTensorPtr(tensor);
  69. return GRAPH_SUCCESS;
  70. }
  71. graphStatus RuntimeInferenceContext::GetTensor(int64_t node_id, int output_id, Tensor &tensor) {
  72. if (output_id < 0) {
  73. GELOGE(GRAPH_PARAM_INVALID, "Invalid output index: %d", output_id);
  74. return GRAPH_PARAM_INVALID;
  75. }
  76. std::lock_guard<std::mutex> lk(mu_);
  77. auto iter = tensors_.find(node_id);
  78. if (iter == tensors_.end()) {
  79. GELOGE(INTERNAL_ERROR, "Node not register. Id = %ld", node_id);
  80. return INTERNAL_ERROR;
  81. }
  82. auto &output_tensors = iter->second;
  83. if (static_cast<uint32_t>(output_id) >= output_tensors.size()) {
  84. GELOGE(GRAPH_FAILED, "Node output is not registered. node_id = %ld, output index = %d", node_id, output_id);
  85. return GRAPH_FAILED;
  86. }
  87. GELOGD("Get tensor for node_id = %ld, output_id = %d", node_id, output_id);
  88. tensor = output_tensors[output_id];
  89. return GRAPH_SUCCESS;
  90. }
  91. graphStatus RuntimeInferenceContext::GetTensor(int64_t node_id, int output_id, GeTensorPtr &tensor) {
  92. if (output_id < 0) {
  93. GELOGE(GRAPH_PARAM_INVALID, "Invalid output index: %d", output_id);
  94. return GRAPH_PARAM_INVALID;
  95. }
  96. std::lock_guard<std::mutex> lk(mu_);
  97. auto iter = ge_tensors_.find(node_id);
  98. if (iter == ge_tensors_.end()) {
  99. GELOGE(INTERNAL_ERROR, "Node not register. Id = %ld", node_id);
  100. return INTERNAL_ERROR;
  101. }
  102. auto &output_tensors = iter->second;
  103. if (static_cast<uint32_t>(output_id) >= output_tensors.size()) {
  104. GELOGE(GRAPH_FAILED, "Node output is not registered. node_id = %ld, output index = %d", node_id, output_id);
  105. return GRAPH_FAILED;
  106. }
  107. GELOGD("Get ge tensor for node_id = %ld, output_id = %d", node_id, output_id);
  108. tensor = output_tensors[output_id];
  109. return GRAPH_SUCCESS;
  110. }
  111. } // namespace ge

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