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_goto_task.cc 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "ge_runtime/task/label_goto_task.h"
  17. #include "ge_runtime/task/task_factory.h"
  18. namespace ge {
  19. namespace model_runner {
  20. std::weak_ptr<LabelGotoTask::LabelManager> LabelGotoTask::LabelManager::instance_;
  21. std::mutex LabelGotoTask::LabelManager::instance_mutex_;
  22. LabelGotoTask::LabelGotoTask(const ModelContext &model_context, const std::shared_ptr<LabelGotoTaskInfo> &task_info)
  23. : TaskRepeater<LabelGotoTaskInfo>(model_context, task_info),
  24. task_info_(task_info),
  25. stream_(nullptr),
  26. label_(nullptr),
  27. index_value_(nullptr) {
  28. if (task_info_ == nullptr) {
  29. GELOGW("task_info_ is null!");
  30. return;
  31. }
  32. auto stream_list = model_context.stream_list();
  33. auto label_list = model_context.label_list();
  34. rt_model_handle_ = model_context.rt_model_handle();
  35. uint32_t stream_id = task_info->stream_id();
  36. label_id_ = task_info->label_id();
  37. GELOGI("Stream list size:%zu, stream id:%u.", stream_list.size(), stream_id);
  38. GELOGI("Label list size:%zu, label id:%u.", label_list.size(), label_id_);
  39. if (stream_id >= stream_list.size() || label_id_ >= label_list.size()) {
  40. GELOGW("Stream/Label id invalid.");
  41. return;
  42. }
  43. stream_ = stream_list[stream_id];
  44. label_ = label_list[label_id_];
  45. label_manager_ = LabelManager::GetInstance();
  46. if (label_manager_ == nullptr) {
  47. GELOGW("Get label manager instance failed.");
  48. return;
  49. }
  50. label_info_ = label_manager_->GetLabelInfo(rt_model_handle_, label_id_, label_);
  51. }
  52. LabelGotoTask::~LabelGotoTask() {
  53. if (index_value_ != nullptr) {
  54. rtError_t rt_ret = rtFree(index_value_);
  55. if (rt_ret != RT_ERROR_NONE) {
  56. GELOGE(RT_FAILED, "rtFree index_value_ failed! ret: 0x%X.", rt_ret);
  57. }
  58. index_value_ = nullptr;
  59. }
  60. }
  61. bool LabelGotoTask::Distribute() {
  62. GELOGI("LabelGotoTask Distribute start.");
  63. if (stream_ == nullptr) {
  64. GELOGE(PARAM_INVALID, "stream is null!");
  65. return false;
  66. }
  67. if (label_ == nullptr) {
  68. GELOGE(PARAM_INVALID, "label is null!");
  69. return false;
  70. }
  71. if (label_info_ == nullptr) {
  72. GELOGE(PARAM_INVALID, "label info is null!");
  73. return false;
  74. }
  75. if (index_value_ == nullptr) {
  76. rtError_t rt_ret = rtMalloc(&index_value_, sizeof(uint64_t), RT_MEMORY_HBM);
  77. if (rt_ret != RT_ERROR_NONE) {
  78. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  79. return false;
  80. }
  81. uint64_t index = 0;
  82. rt_ret = rtMemcpy(index_value_, sizeof(uint64_t), &index, sizeof(index), RT_MEMCPY_HOST_TO_DEVICE);
  83. if (rt_ret != RT_ERROR_NONE) {
  84. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  85. return false;
  86. }
  87. }
  88. void *label_info = label_info_->GetLabelInfo();
  89. rtError_t rt_ret = rtLabelSwitchByIndex(index_value_, 1, label_info, stream_);
  90. if (rt_ret != RT_ERROR_NONE) {
  91. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  92. return false;
  93. }
  94. GELOGI("DistributeTask end.");
  95. return true;
  96. }
  97. LabelGotoTask::LabelGuard::~LabelGuard() {
  98. void *label_info = GetLabelInfo();
  99. if (label_info != nullptr) {
  100. rtError_t rt_ret = rtFree(label_info);
  101. if (rt_ret != RT_ERROR_NONE) {
  102. GELOGE(RT_FAILED, "rtFree label_info failed! ret: 0x%X.", rt_ret);
  103. }
  104. }
  105. }
  106. std::shared_ptr<LabelGotoTask::LabelManager> LabelGotoTask::LabelManager::GetInstance() {
  107. std::lock_guard<std::mutex> lock(instance_mutex_);
  108. auto instance = instance_.lock();
  109. if (instance != nullptr) {
  110. return instance;
  111. }
  112. instance = std::make_shared<LabelManager>();
  113. instance_ = instance;
  114. return instance;
  115. }
  116. std::shared_ptr<LabelGotoTask::LabelGuard> LabelGotoTask::LabelManager::GetLabelInfo(rtModel_t model, uint32_t label_id,
  117. void *label) {
  118. std::lock_guard<std::mutex> lock(model_info_mapping_mutex_);
  119. rtError_t rt_ret;
  120. auto model_iter = model_info_mapping_.find(model);
  121. if (model_iter == model_info_mapping_.end()) {
  122. model_info_mapping_.emplace(model, std::map<uint32_t, std::weak_ptr<LabelGuard>>());
  123. model_iter = model_info_mapping_.find(model);
  124. }
  125. std::map<uint32_t, std::weak_ptr<LabelGuard>> &label_map = model_iter->second;
  126. auto label_iter = label_map.find(label_id);
  127. if (label_iter != label_map.end()) {
  128. auto label_guard = label_iter->second.lock();
  129. if (label_guard != nullptr) {
  130. GELOGI("model %p find same label id.", model, label_id);
  131. return label_guard;
  132. }
  133. }
  134. GELOGI("Alloc label id %u for model %p.", label_id, model);
  135. void *label_info;
  136. std::vector<void *> label_list = {label};
  137. uint32_t label_info_size = sizeof(rtLabelDevInfo) * label_list.size();
  138. rt_ret = rtMalloc(&label_info, label_info_size, RT_MEMORY_HBM);
  139. if (rt_ret != RT_ERROR_NONE) {
  140. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  141. return nullptr;
  142. }
  143. rt_ret = rtLabelListCpy(label_list.data(), label_list.size(), label_info, label_info_size);
  144. if (rt_ret != RT_ERROR_NONE) {
  145. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  146. return nullptr;
  147. }
  148. auto label_guard = std::make_shared<LabelGuard>(label_info);
  149. label_map.emplace(label_id, label_guard);
  150. return label_guard;
  151. }
  152. REGISTER_TASK(TaskInfoType::LABEL_GOTO, LabelGotoTask, LabelGotoTaskInfo);
  153. } // namespace model_runner
  154. } // namespace ge

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