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.

aicpu_task.cc 5.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/aicpu_task.h"
  17. #include <vector>
  18. #include "ge_runtime/task/task_factory.h"
  19. #include "aicpu/common/aicpu_task_struct.h"
  20. namespace ge {
  21. namespace model_runner {
  22. AicpuTask::AicpuTask(const ModelContext &model_context, const std::shared_ptr<AicpuTaskInfo> &task_info)
  23. : TaskRepeater<AicpuTaskInfo>(model_context, task_info),
  24. task_info_(task_info),
  25. stream_(nullptr),
  26. args_(nullptr),
  27. input_output_addr_(nullptr) {
  28. if (task_info_ == nullptr) {
  29. GELOGW("task_info_ is null!");
  30. }
  31. auto stream_list = model_context.stream_list();
  32. if (stream_list.size() == 1) {
  33. stream_ = stream_list[0];
  34. } else if (stream_list.size() > task_info->stream_id()) {
  35. stream_ = stream_list[task_info->stream_id()];
  36. } else {
  37. GELOGW("index: %u >= stream_list.size(): %zu.", task_info->stream_id(), stream_list.size());
  38. }
  39. }
  40. AicpuTask::~AicpuTask() { ReleaseRtMem(&args_); }
  41. bool AicpuTask::Distribute() {
  42. GELOGI("InitAicpuTask start.");
  43. vector<void *> io_addrs;
  44. io_addrs.insert(io_addrs.end(), task_info_->input_data_addrs().begin(), task_info_->input_data_addrs().end());
  45. io_addrs.insert(io_addrs.end(), task_info_->output_data_addrs().begin(), task_info_->output_data_addrs().end());
  46. auto io_addrs_num = static_cast<uint32_t>(io_addrs.size());
  47. auto io_addrs_size = static_cast<uint32_t>(io_addrs_num * sizeof(void *));
  48. constexpr uint32_t io_addr_offset = sizeof(aicpu::AicpuParamHead);
  49. uint32_t node_def_addr_offset = io_addr_offset + io_addrs_size;
  50. uint32_t args_size =
  51. sizeof(aicpu::AicpuParamHead) + io_addrs_size + static_cast<uint32_t>(task_info_->node_def().size());
  52. aicpu::AicpuParamHead aicpu_param_head = {args_size, io_addrs_num};
  53. // Malloc device memory for args
  54. rtError_t rt_ret = rtMalloc(&args_, args_size, RT_MEMORY_HBM);
  55. if (rt_ret != RT_ERROR_NONE) {
  56. GELOGE(RT_FAILED, "Call rt api(rtMalloc) failed, ret: 0x%X.", rt_ret);
  57. return false;
  58. }
  59. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, "task args data.", args_size)
  60. // Memcpy AicpuParamHead
  61. rt_ret = rtMemcpy(args_, sizeof(aicpu::AicpuParamHead), reinterpret_cast<void *>(&aicpu_param_head),
  62. sizeof(aicpu::AicpuParamHead), RT_MEMCPY_HOST_TO_DEVICE);
  63. if (rt_ret != RT_ERROR_NONE) {
  64. GELOGE(RT_FAILED, "Call rt api(rtMemcpy) failed, ret: 0x%X.", rt_ret);
  65. return false;
  66. }
  67. // Memcpy io addrs
  68. if (io_addrs_num != 0) {
  69. rt_ret = rtMemcpy(reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(args_) + io_addr_offset), io_addrs_size,
  70. reinterpret_cast<void *>(io_addrs.data()), io_addrs_size, RT_MEMCPY_HOST_TO_DEVICE);
  71. if (rt_ret != RT_ERROR_NONE) {
  72. GELOGE(RT_FAILED, "Call rt api(rtMemcpy) failed, ret: 0x%X.", rt_ret);
  73. return false;
  74. }
  75. }
  76. // Memcpy node def
  77. rt_ret = rtMemcpy(reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(args_) + node_def_addr_offset),
  78. task_info_->node_def().size(), reinterpret_cast<const void *>(task_info_->node_def().data()),
  79. task_info_->node_def().size(), RT_MEMCPY_HOST_TO_DEVICE);
  80. if (rt_ret != RT_ERROR_NONE) {
  81. GELOGE(RT_FAILED, "Call rt api(rtMemcpy) failed, ret: 0x%X.", rt_ret);
  82. return false;
  83. }
  84. input_output_addr_ = reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(args_) + io_addr_offset);
  85. auto dump_flag = task_info_->dump_flag() ? RT_KERNEL_DUMPFLAG : RT_KERNEL_DEFAULT;
  86. GELOGI(
  87. "Distribute AicpuTask start, args_size = %u, io_addrs_num = %u, so_name = %s, kernel_name = %s, dump_flag = %d.",
  88. args_size, io_addrs_num, task_info_->so_name().data(), task_info_->kernel_name().data(), dump_flag);
  89. rt_ret = rtCpuKernelLaunchWithFlag(reinterpret_cast<const void *>(task_info_->so_name().data()),
  90. reinterpret_cast<const void *>(task_info_->kernel_name().data()), 1, args_,
  91. args_size, nullptr, stream_, dump_flag);
  92. if (rt_ret != RT_ERROR_NONE) {
  93. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  94. return false;
  95. }
  96. GELOGI("Distribute AicpuTask end.");
  97. return true;
  98. }
  99. void AicpuTask::ReleaseRtMem(void **ptr) noexcept {
  100. if (ptr == nullptr || *ptr == nullptr) {
  101. return;
  102. }
  103. rtError_t rt_ret = rtFree(*ptr);
  104. if (rt_ret != RT_ERROR_NONE) {
  105. GELOGE(RT_FAILED, "ReleaseRtMem failed, ret: 0x%X", rt_ret);
  106. return;
  107. }
  108. *ptr = nullptr;
  109. }
  110. REGISTER_TASK(TaskInfoType::AICPU, AicpuTask, AicpuTaskInfo);
  111. } // namespace model_runner
  112. } // namespace ge

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