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 4.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/new_model_manager/model_manager.h"
  19. #include "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, "aicpu_arg_size is invalid, value = %zu", aicpu_arg_size);
  27. return ACL_ERROR_GE_PARAM_INVALID;
  28. }
  29. task.io_addr_num_ = op_desc_->GetInputsSize() + op_desc_->GetOutputsSize();
  30. GE_CHECK_GE(aicpu_arg_size - sizeof(aicpu::AicpuParamHead), task.io_addr_num_ * sizeof(void *));
  31. std::unique_ptr<uint8_t[]> aicpu_args;
  32. aicpu_args.reset(new(std::nothrow) uint8_t[aicpu_arg_size]());
  33. if (aicpu_args == nullptr) {
  34. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "malloc failed, size = %zu", aicpu_arg_size);
  35. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  36. }
  37. auto err = memcpy_s(aicpu_args.get(), aicpu_arg_size, kernel_def_.args().data(), aicpu_arg_size);
  38. if (err != EOK) {
  39. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "memcpy_s args failed, size = %zu, err = %d", aicpu_arg_size, err);
  40. return ACL_ERROR_GE_INTERNAL_ERROR;
  41. }
  42. task.SetIoAddr(reinterpret_cast<uintptr_t *>(aicpu_args.get() + sizeof(aicpu::AicpuParamHead)));
  43. task.SetKernelArgs(std::move(aicpu_args), aicpu_arg_size);
  44. auto addresses = BuildTaskUtils::GetKernelArgs(op_desc_, param);
  45. GE_CHECK_GE(addresses.size(), task.io_addr_num_);
  46. for (size_t i = 0; i < task.io_addr_num_; ++i) {
  47. task.io_addr_[i] = reinterpret_cast<uintptr_t>(addresses[i]);
  48. }
  49. return SUCCESS;
  50. }
  51. Status AiCpuCCTaskBuilder::BuildTask(AiCpuCCTask &task, uint64_t kernel_id, const SingleOpModelParam &param) {
  52. auto ret = SetKernelArgs(task, param);
  53. if (ret != SUCCESS) {
  54. return ret;
  55. }
  56. const std::string &so_name = kernel_def_.so_name();
  57. const std::string &kernel_name = kernel_def_.kernel_name();
  58. task.SetSoName(so_name);
  59. task.SetkernelName(kernel_name);
  60. GE_CHECK_NOTNULL(op_desc_);
  61. task.op_desc_ = op_desc_;
  62. const auto &context = kernel_def_.context();
  63. auto kernel_type = static_cast<ccKernelType>(context.kernel_type());
  64. if (kernel_type == ccKernelType::CUST_AI_CPU) {
  65. task.is_custom_ = true;
  66. task.dump_flag_ |= RT_KERNEL_CUSTOM_AICPU;
  67. bool loaded = false;
  68. GE_CHK_STATUS_RET(ModelManager::GetInstance()->LoadCustAicpuSo(op_desc_, so_name, loaded),
  69. "launch cust aicpu so failed");
  70. if (!loaded) {
  71. GE_CHK_STATUS_RET(ModelManager::GetInstance()->LaunchCustAicpuSo(), "launch cust aicpu so failed.");
  72. }
  73. }
  74. task.num_inputs_ = op_desc_->GetInputsSize();
  75. task.num_outputs_ = op_desc_->GetOutputsSize();
  76. // get kernel_ext_info
  77. auto &kernel_ext_info = kernel_def_.kernel_ext_info();
  78. auto kernel_ext_info_size = kernel_def_.kernel_ext_info_size();
  79. GE_CHK_BOOL_RET_STATUS(kernel_ext_info.size() == kernel_ext_info_size, FAILED,
  80. "task def kernel_ext_info.size=%zu, but kernel_ext_info_size=%u.",
  81. kernel_ext_info.size(), kernel_ext_info_size);
  82. ret = task.SetExtInfoAndType(kernel_ext_info, kernel_id);
  83. if (ret != SUCCESS) {
  84. GELOGE(ret, "Init ext info failed.");
  85. return ret;
  86. }
  87. GE_CHK_STATUS_RET(task.SetInputConst(), "AiCpuCCTask set input_const failed.");
  88. if (task.GetUnknownType() == DEPEND_COMPUTE) {
  89. GELOGE(FAILED, "AiCpuCCTask unknown type is depend compute, it's not supported now.");
  90. return FAILED;
  91. }
  92. auto aicpu_param_head = reinterpret_cast<aicpu::AicpuParamHead *>(task.args_.get());
  93. if (task.ext_info_addr_dev_ != nullptr) {
  94. aicpu_param_head->extInfoLength = kernel_ext_info.size();
  95. aicpu_param_head->extInfoAddr = reinterpret_cast<uintptr_t>(task.ext_info_addr_dev_);
  96. }
  97. task.op_type_ = op_desc_->GetName();
  98. task.kernel_id_ = kernel_id;
  99. auto debug_info = BuildTaskUtils::GetTaskInfo(op_desc_);
  100. GELOGI("[TASK_INFO] %lu/%s %s", kernel_id, task.op_type_.c_str(), debug_info.c_str());
  101. return SUCCESS;
  102. }
  103. } // namespace ge

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