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_switch_task.cc 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_switch_task.h"
  17. #include <vector>
  18. #include "ge_runtime/task/task_factory.h"
  19. namespace ge {
  20. namespace model_runner {
  21. LabelSwitchTask::LabelSwitchTask(const ModelContext &model_context,
  22. const std::shared_ptr<LabelSwitchTaskInfo> &task_info)
  23. : TaskRepeater<LabelSwitchTaskInfo>(model_context, task_info),
  24. task_info_(task_info),
  25. stream_(nullptr),
  26. all_label_resource_(),
  27. label_info_(nullptr) {
  28. if (task_info_ == nullptr) {
  29. GELOGW("task_info_ is null!");
  30. return;
  31. }
  32. all_label_resource_ = model_context.label_list();
  33. auto stream_list = model_context.stream_list();
  34. uint32_t stream_id = task_info->stream_id();
  35. GELOGI("Stream list size:%zu, stream id:%u.", stream_list.size(), stream_id);
  36. if (stream_id >= stream_list.size()) {
  37. GELOGW("Stream id invalid.");
  38. return;
  39. }
  40. stream_ = stream_list[stream_id];
  41. CopyLabelList();
  42. }
  43. LabelSwitchTask::~LabelSwitchTask() {
  44. if (label_info_ != nullptr) {
  45. rtError_t rt_ret = rtFree(label_info_);
  46. if (rt_ret != RT_ERROR_NONE) {
  47. GELOGE(RT_FAILED, "rtFree fwkOpBuf failed! ret: 0x%X.", rt_ret);
  48. }
  49. label_info_ = nullptr;
  50. }
  51. }
  52. bool LabelSwitchTask::Distribute() {
  53. GELOGI("LabelSwitchTask Distribute start.");
  54. if (!CheckParamValid()) {
  55. return false;
  56. }
  57. if (label_info_ == nullptr) {
  58. GELOGE(PARAM_INVALID, "CopyLabelList failed, label info is null.");
  59. return false;
  60. }
  61. rtError_t rt_ret = rtLabelSwitchByIndex(task_info_->cond(), task_info_->label_size(), label_info_, stream_);
  62. if (rt_ret != RT_ERROR_NONE) {
  63. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  64. return false;
  65. }
  66. GELOGI("DistributeTask end.");
  67. return true;
  68. }
  69. bool LabelSwitchTask::CheckParamValid() {
  70. if (stream_ == nullptr) {
  71. GELOGE(PARAM_INVALID, "stream is null!");
  72. return false;
  73. }
  74. if (task_info_->label_list().empty()) {
  75. GELOGE(PARAM_INVALID, "label_list is empty.");
  76. return false;
  77. }
  78. if (task_info_->label_size() != task_info_->label_list().size()) {
  79. GELOGE(PARAM_INVALID, "label_list size %zu but label_size is %u.", task_info_->label_list().size(),
  80. task_info_->label_size());
  81. return false;
  82. }
  83. if (task_info_->label_size() >= UINT32_MAX / sizeof(rtLabelDevInfo)) {
  84. GELOGE(PARAM_INVALID, "label_size %u will overflow.", task_info_->label_size());
  85. return false;
  86. }
  87. return true;
  88. }
  89. void LabelSwitchTask::CopyLabelList() {
  90. if (!CheckParamValid()) {
  91. return;
  92. }
  93. if (label_info_ != nullptr) {
  94. GELOGE(PARAM_INVALID, "label_info_ has dirty data.");
  95. return;
  96. }
  97. const std::vector<uint32_t> &label_index_list = task_info_->label_list();
  98. std::vector<void *> label_list(task_info_->label_size(), nullptr);
  99. for (size_t i = 0; i < task_info_->label_size(); ++i) {
  100. uint32_t label_index = label_index_list[i];
  101. if (label_index >= all_label_resource_.size()) {
  102. GELOGE(PARAM_INVALID, "label %zu index is %u, but there are %zu labels in total.", i, label_index,
  103. all_label_resource_.size());
  104. return;
  105. }
  106. label_list[i] = all_label_resource_[label_index];
  107. GELOGI("Case %zu: label id %zu.", i, label_index);
  108. }
  109. uint32_t label_info_size = sizeof(rtLabelDevInfo) * task_info_->label_size();
  110. rtError_t rt_ret = rtMalloc(&label_info_, label_info_size, RT_MEMORY_HBM);
  111. if (rt_ret != RT_ERROR_NONE) {
  112. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  113. return;
  114. }
  115. rt_ret = rtLabelListCpy(label_list.data(), label_list.size(), label_info_, label_info_size);
  116. if (rt_ret != RT_ERROR_NONE) {
  117. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  118. return;
  119. }
  120. }
  121. REGISTER_TASK(TaskInfoType::LABEL_SWITCH, LabelSwitchTask, LabelSwitchTaskInfo);
  122. } // namespace model_runner
  123. } // namespace ge

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