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_kernel_task_builder.cc 5.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "single_op/task/aicpu_kernel_task_builder.h"
  17. #include "framework/common/taskdown_common.h"
  18. #include "graph/load/model_manager/model_manager.h"
  19. #include "single_op/task/build_task_utils.h"
  20. namespace ge {
  21. AiCpuCCTaskBuilder::AiCpuCCTaskBuilder(const OpDescPtr &op_desc, const domi::KernelDef &kernel_def)
  22. : op_desc_(op_desc), kernel_def_(kernel_def) {}
  23. Status AiCpuCCTaskBuilder::SetKernelArgs(AiCpuCCTask &task, const SingleOpModelParam &param) {
  24. size_t aicpu_arg_size = kernel_def_.args_size();
  25. if (aicpu_arg_size <= sizeof(aicpu::AicpuParamHead)) {
  26. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "[Check][Size]aicpu_arg_size is invalid, value = %zu", aicpu_arg_size);
  27. REPORT_INNER_ERROR("E19999", "aicpu_arg_size is invalid, value = %zu", aicpu_arg_size);
  28. return ACL_ERROR_GE_PARAM_INVALID;
  29. }
  30. task.io_addr_num_ = op_desc_->GetInputsSize() + op_desc_->GetOutputsSize();
  31. GE_CHECK_GE(aicpu_arg_size - sizeof(aicpu::AicpuParamHead), task.io_addr_num_ * sizeof(void *));
  32. std::unique_ptr<uint8_t[]> aicpu_args;
  33. aicpu_args.reset(new(std::nothrow) uint8_t[aicpu_arg_size]());
  34. if (aicpu_args == nullptr) {
  35. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[New][Memory] failed, size = %zu", aicpu_arg_size);
  36. REPORT_INNER_ERROR("E19999", "new Memory failed, size = %zu", aicpu_arg_size);
  37. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  38. }
  39. auto err = memcpy_s(aicpu_args.get(), aicpu_arg_size, kernel_def_.args().data(), aicpu_arg_size);
  40. if (err != EOK) {
  41. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "[Memcpy_s][Args] failed, size = %zu, err = %d", aicpu_arg_size, err);
  42. REPORT_INNER_ERROR("E19999", "memcpy_s aicpu_args failed, size = %zu, err = %d", aicpu_arg_size, err);
  43. return ACL_ERROR_GE_INTERNAL_ERROR;
  44. }
  45. task.SetIoAddr(reinterpret_cast<uintptr_t *>(aicpu_args.get() + sizeof(aicpu::AicpuParamHead)));
  46. task.SetKernelArgs(std::move(aicpu_args), aicpu_arg_size);
  47. auto addresses = BuildTaskUtils::GetKernelArgs(op_desc_, param);
  48. GE_CHECK_GE(addresses.size(), task.io_addr_num_);
  49. for (size_t i = 0; i < task.io_addr_num_; ++i) {
  50. task.io_addr_[i] = reinterpret_cast<uintptr_t>(addresses[i]);
  51. }
  52. return SUCCESS;
  53. }
  54. Status AiCpuCCTaskBuilder::BuildTask(AiCpuCCTask &task, uint64_t kernel_id, const SingleOpModelParam &param) {
  55. auto ret = SetKernelArgs(task, param);
  56. if (ret != SUCCESS) {
  57. return ret;
  58. }
  59. const std::string &so_name = kernel_def_.so_name();
  60. const std::string &kernel_name = kernel_def_.kernel_name();
  61. task.SetSoName(so_name);
  62. task.SetkernelName(kernel_name);
  63. GE_CHECK_NOTNULL(op_desc_);
  64. task.op_desc_ = op_desc_;
  65. const auto &context = kernel_def_.context();
  66. auto kernel_type = static_cast<ccKernelType>(context.kernel_type());
  67. if (kernel_type == ccKernelType::CUST_AI_CPU) {
  68. task.is_custom_ = true;
  69. task.dump_flag_ |= RT_KERNEL_CUSTOM_AICPU;
  70. bool loaded = false;
  71. GE_CHK_STATUS_RET(ModelManager::GetInstance()->LoadCustAicpuSo(op_desc_, so_name, loaded),
  72. "[Load][CustAicpuSo] failed.");
  73. if (!loaded) {
  74. GE_CHK_STATUS_RET(ModelManager::GetInstance()->LaunchCustAicpuSo(), "[Launch][CustAicpuSo] failed.");
  75. }
  76. }
  77. task.num_inputs_ = op_desc_->GetInputsSize();
  78. task.num_outputs_ = op_desc_->GetOutputsSize();
  79. // get kernel_ext_info
  80. auto &kernel_ext_info = kernel_def_.kernel_ext_info();
  81. auto kernel_ext_info_size = kernel_def_.kernel_ext_info_size();
  82. GE_CHK_BOOL_RET_STATUS(kernel_ext_info.size() == kernel_ext_info_size, FAILED,
  83. "[Check][Size]task def kernel_ext_info.size=%zu, but kernel_ext_info_size=%u.",
  84. kernel_ext_info.size(), kernel_ext_info_size);
  85. ret = task.SetExtInfoAndType(kernel_ext_info, kernel_id);
  86. if (ret != SUCCESS) {
  87. GELOGE(ret, "[Set][ExtInfoAndType]failed, kernel_id=%lu.", kernel_id);
  88. REPORT_CALL_ERROR("E19999", "SetExtInfoAndType failed, kernel_id=%lu.", kernel_id);
  89. return ret;
  90. }
  91. GE_CHK_STATUS_RET(task.SetInputConst(), "[Set][InputConst] failed.");
  92. if (task.GetUnknownType() == DEPEND_COMPUTE) {
  93. GELOGE(FAILED, "[Get][UnknownType] is depend compute, it's not supported now.");
  94. return FAILED;
  95. }
  96. auto aicpu_param_head = reinterpret_cast<aicpu::AicpuParamHead *>(task.args_.get());
  97. if (task.ext_info_addr_dev_ != nullptr) {
  98. aicpu_param_head->extInfoLength = kernel_ext_info.size();
  99. aicpu_param_head->extInfoAddr = reinterpret_cast<uintptr_t>(task.ext_info_addr_dev_);
  100. }
  101. task.op_type_ = op_desc_->GetName();
  102. task.kernel_id_ = kernel_id;
  103. auto debug_info = BuildTaskUtils::GetTaskInfo(op_desc_);
  104. GELOGI("[TASK_INFO] %lu/%s %s", kernel_id, task.op_type_.c_str(), debug_info.c_str());
  105. return SUCCESS;
  106. }
  107. } // namespace ge

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