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 3.5 kB

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

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