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.

label_manager.cc 4.1 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright 2021 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 "ge_runtime/task/label_manager.h"
  17. #include <algorithm>
  18. #include <string>
  19. #include "runtime/mem.h"
  20. #include "runtime/rt_model.h"
  21. #include "common/ge_inner_error_codes.h"
  22. #include "framework/common/debug/ge_log.h"
  23. namespace ge {
  24. namespace model_runner {
  25. std::weak_ptr<LabelManager> LabelManager::instance_;
  26. std::mutex LabelManager::instance_mutex_;
  27. template <class T>
  28. static std::string GetVectorString(const std::vector<T> &vec) {
  29. std::string ret;
  30. for (size_t i = 0; i < vec.size(); ++i) {
  31. if (i != 0) {
  32. ret.push_back(',');
  33. }
  34. ret += std::to_string(vec[i]);
  35. }
  36. return ret;
  37. }
  38. LabelGuard::~LabelGuard() {
  39. void *label_info = GetLabelInfo();
  40. if (label_info != nullptr) {
  41. rtError_t rt_ret = rtFree(label_info);
  42. if (rt_ret != RT_ERROR_NONE) {
  43. GELOGE(RT_FAILED, "rtFree label_info failed! ret: 0x%X.", rt_ret);
  44. }
  45. }
  46. }
  47. std::shared_ptr<LabelManager> LabelManager::GetInstance() {
  48. std::lock_guard<std::mutex> lock(instance_mutex_);
  49. auto instance = instance_.lock();
  50. if (instance != nullptr) {
  51. return instance;
  52. }
  53. instance = std::make_shared<LabelManager>();
  54. instance_ = instance;
  55. return instance;
  56. }
  57. std::shared_ptr<LabelGuard> LabelManager::GetLabelInfo(rtModel_t model, const std::vector<uint32_t> &label_ids,
  58. const std::vector<void *> &all_label) {
  59. std::lock_guard<std::mutex> lock(model_info_mapping_mutex_);
  60. rtError_t rt_ret;
  61. auto model_iter = model_info_mapping_.find(model);
  62. if (model_iter == model_info_mapping_.end()) {
  63. model_info_mapping_.emplace(model, std::map<std::string, std::weak_ptr<LabelGuard>>());
  64. model_iter = model_info_mapping_.find(model);
  65. }
  66. std::string label_id_str = GetVectorString(label_ids);
  67. auto &label_map = model_iter->second;
  68. auto label_iter = label_map.find(label_id_str);
  69. if (label_iter != label_map.end()) {
  70. auto label_guard = label_iter->second.lock();
  71. if (label_guard != nullptr) {
  72. GELOGI("model %p find same label id %s.", model, label_id_str.c_str());
  73. return label_guard;
  74. }
  75. }
  76. GELOGI("Alloc label id %s for model %p.", label_id_str.c_str(), model);
  77. void *label_info;
  78. std::vector<void *> label_list;
  79. bool status = true;
  80. std::transform(label_ids.begin(), label_ids.end(), std::back_inserter(label_list),
  81. [&all_label, &status](uint32_t idx) -> void * {
  82. if (idx >= all_label.size()) {
  83. GELOGE(PARAM_INVALID, "Invalid label id %u, all label list size %zu.", idx, all_label.size());
  84. status = false;
  85. return nullptr;
  86. }
  87. return all_label[idx];
  88. });
  89. if (!status) {
  90. GELOGE(PARAM_INVALID, "Get label info failed.");
  91. return nullptr;
  92. }
  93. uint32_t label_info_size = sizeof(rtLabelDevInfo) * label_list.size();
  94. rt_ret = rtMalloc(&label_info, label_info_size, RT_MEMORY_HBM);
  95. if (rt_ret != RT_ERROR_NONE) {
  96. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  97. return nullptr;
  98. }
  99. rt_ret = rtLabelListCpy(label_list.data(), label_list.size(), label_info, label_info_size);
  100. if (rt_ret != RT_ERROR_NONE) {
  101. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  102. return nullptr;
  103. }
  104. auto label_guard = std::make_shared<LabelGuard>(label_info);
  105. label_map.emplace(label_id_str, label_guard);
  106. return label_guard;
  107. }
  108. } // namespace model_runner
  109. } // namespace ge

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