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

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