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

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

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