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.

cce_task.cc 5.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/cce_task.h"
  17. #include "ge_runtime/task/task_factory.h"
  18. namespace ge {
  19. namespace model_runner {
  20. CceTask::CceTask(const ModelContext &model_context, const std::shared_ptr<CceTaskInfo> &task_info)
  21. : TaskRepeater<CceTaskInfo>(model_context, task_info),
  22. task_info_(task_info),
  23. stream_(nullptr),
  24. stub_func_(nullptr),
  25. args_(nullptr),
  26. sm_desc_(nullptr),
  27. flowtable_(nullptr),
  28. is_flowtable_(false) {
  29. if (task_info_ == nullptr) {
  30. GELOGW("task_info_ is null!");
  31. return;
  32. }
  33. auto stream_list = model_context.stream_list();
  34. if (stream_list.size() == 1) {
  35. stream_ = stream_list[0];
  36. } else if (stream_list.size() > task_info->stream_id()) {
  37. stream_ = stream_list[task_info->stream_id()];
  38. } else {
  39. GELOGW("index: %u >= stream_list.size(): %zu.", task_info->stream_id(), stream_list.size());
  40. }
  41. }
  42. CceTask::~CceTask() {
  43. FreeRtMem(&args_);
  44. FreeRtMem(&flowtable_);
  45. rtError_t ret = (sm_desc_ != nullptr) ? rtMemFreeManaged(sm_desc_) : RT_ERROR_NONE;
  46. if (ret != RT_ERROR_NONE) {
  47. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", ret);
  48. }
  49. sm_desc_ = nullptr;
  50. }
  51. void CceTask::FreeRtMem(void **ptr) noexcept {
  52. if (ptr == nullptr || *ptr == nullptr) {
  53. return;
  54. }
  55. rtError_t ret = rtFree(*ptr);
  56. if (ret != RT_ERROR_NONE) {
  57. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", ret);
  58. }
  59. *ptr = nullptr;
  60. }
  61. bool CceTask::Distribute() {
  62. GELOGI("Distribute CCETask start.");
  63. if (stream_ == nullptr) {
  64. GELOGE(PARAM_INVALID, "stream_ is null!");
  65. return false;
  66. }
  67. // Get stub_func
  68. if (task_info_->stub_func().empty()) {
  69. GELOGE(PARAM_INVALID, "kernel_info->stub_func is empty!");
  70. return false;
  71. }
  72. rtError_t rt_ret = rtGetFunctionByName(const_cast<char *>(task_info_->stub_func().c_str()), &stub_func_);
  73. if (rt_ret != RT_ERROR_NONE) {
  74. GELOGE(RT_FAILED, "rtGetFunctionByName failed, ret: 0x%X", rt_ret);
  75. stub_func_ = nullptr;
  76. return false;
  77. }
  78. GELOGI("CCETask: stub_func = %s [%p].", task_info_->stub_func().c_str(), stub_func_);
  79. // Flowtable
  80. if (is_flowtable_) {
  81. rt_ret = rtMalloc(&flowtable_, task_info_->flow_table().size(), RT_MEMORY_HBM);
  82. if (rt_ret != RT_ERROR_NONE) {
  83. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  84. return false;
  85. }
  86. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, "task information.", task_info_->flow_table().size())
  87. rt_ret = rtMemcpy(flowtable_, task_info_->flow_table().size(), task_info_->flow_table().data(),
  88. task_info_->flow_table().size(), RT_MEMCPY_HOST_TO_DEVICE);
  89. if (rt_ret != RT_ERROR_NONE) {
  90. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  91. return false;
  92. }
  93. // Modify flowtable addr in args
  94. auto args = const_cast<uint8_t *>(task_info_->args().data());
  95. auto task_offset = reinterpret_cast<uint16_t *>(const_cast<uint8_t *>(task_info_->args_offset().data()));
  96. if (task_info_->args().size() < (task_offset[0] + sizeof(uint64_t))) {
  97. GELOGE(FAILED, "(context.args_offset().data()))[0]:%u + sizeof(uint64_t):%zu > kernelDef.args().size():%zu",
  98. static_cast<uint32_t>(task_offset[0]), sizeof(uint64_t), task_info_->args().size());
  99. return false;
  100. }
  101. *(reinterpret_cast<uintptr_t *>(args + task_offset[0])) = reinterpret_cast<uintptr_t>(flowtable_);
  102. }
  103. // Args
  104. rt_ret = rtMalloc(&args_, task_info_->args_size(), RT_MEMORY_HBM);
  105. if (rt_ret != RT_ERROR_NONE) {
  106. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  107. return false;
  108. }
  109. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, "task information.", task_info_->args_size())
  110. rt_ret = rtMemcpy(args_, task_info_->args_size(), task_info_->args().data(), task_info_->args_size(),
  111. RT_MEMCPY_HOST_TO_DEVICE);
  112. if (rt_ret != RT_ERROR_NONE) {
  113. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  114. return false;
  115. }
  116. // L2 sm_desc
  117. if (!task_info_->sm_desc().empty()) {
  118. rt_ret = rtMemAllocManaged(&sm_desc_, task_info_->sm_desc().size(), RT_MEMORY_SPM);
  119. if (rt_ret != RT_ERROR_NONE) {
  120. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  121. return false;
  122. }
  123. rt_ret = rtMemcpy(sm_desc_, task_info_->sm_desc().size(), task_info_->sm_desc().data(),
  124. task_info_->sm_desc().size(), RT_MEMCPY_HOST_TO_DEVICE);
  125. if (rt_ret != RT_ERROR_NONE) {
  126. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  127. return false;
  128. }
  129. }
  130. // Kernel launch
  131. rt_ret = rtKernelLaunch(stub_func_, task_info_->block_dim(), args_, task_info_->args_size(),
  132. static_cast<rtSmDesc_t *>(sm_desc_), stream_);
  133. if (rt_ret != RT_ERROR_NONE) {
  134. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  135. return false;
  136. }
  137. return true;
  138. }
  139. REGISTER_TASK(TaskInfoType::CCE, CceTask, CceTaskInfo);
  140. } // namespace model_runner
  141. } // namespace ge

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