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.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. namespace ge {
  20. AiCpuCCTaskBuilder::AiCpuCCTaskBuilder(const OpDescPtr &op_desc, const domi::KernelDef &kernel_def)
  21. : op_desc_(op_desc), kernel_def_(kernel_def) {}
  22. Status AiCpuCCTaskBuilder::SetKernelArgs(AiCpuCCTask &task) {
  23. size_t aicpu_arg_size = kernel_def_.args_size();
  24. if (aicpu_arg_size <= sizeof(aicpu::AicpuParamHead)) {
  25. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "aicpu_arg_size is invalid, value = %zu", aicpu_arg_size);
  26. return ACL_ERROR_GE_PARAM_INVALID;
  27. }
  28. task.io_addr_num_ = op_desc_->GetInputsSize() + op_desc_->GetOutputsSize();
  29. GE_CHECK_GE(aicpu_arg_size - sizeof(aicpu::AicpuParamHead), task.io_addr_num_ * sizeof(void *));
  30. std::unique_ptr<uint8_t[]> aicpu_args;
  31. aicpu_args.reset(new(std::nothrow) uint8_t[aicpu_arg_size]());
  32. if (aicpu_args == nullptr) {
  33. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "malloc failed, size = %zu", aicpu_arg_size);
  34. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  35. }
  36. auto err = memcpy_s(aicpu_args.get(), aicpu_arg_size, kernel_def_.args().data(), aicpu_arg_size);
  37. if (err != EOK) {
  38. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "memcpy_s args failed, size = %zu, err = %d", aicpu_arg_size, err);
  39. return ACL_ERROR_GE_INTERNAL_ERROR;
  40. }
  41. task.SetIoAddr(reinterpret_cast<uintptr_t *>(aicpu_args.get() + sizeof(aicpu::AicpuParamHead)));
  42. task.SetKernelArgs(std::move(aicpu_args), aicpu_arg_size);
  43. return SUCCESS;
  44. }
  45. Status AiCpuCCTaskBuilder::BuildTask(AiCpuCCTask &task, uint64_t kernel_id) {
  46. auto ret = SetKernelArgs(task);
  47. if (ret != SUCCESS) {
  48. return ret;
  49. }
  50. const std::string &so_name = kernel_def_.so_name();
  51. const std::string &kernel_name = kernel_def_.kernel_name();
  52. task.SetSoName(so_name);
  53. task.SetkernelName(kernel_name);
  54. task.op_desc_ = op_desc_;
  55. const auto &context = kernel_def_.context();
  56. auto kernel_type = static_cast<ccKernelType>(context.kernel_type());
  57. if (kernel_type == ccKernelType::CUST_AI_CPU) {
  58. task.is_custom_ = true;
  59. task.dump_flag_ |= RT_KERNEL_CUSTOM_AICPU;
  60. bool loaded = false;
  61. GE_CHK_STATUS_RET(ModelManager::GetInstance()->LoadCustAicpuSo(op_desc_, so_name, loaded),
  62. "launch cust aicpu so failed");
  63. if (!loaded) {
  64. GE_CHK_STATUS_RET(ModelManager::GetInstance()->LaunchCustAicpuSo(), "launch cust aicpu so failed.");
  65. }
  66. }
  67. task.num_inputs_ = op_desc_->GetInputsSize();
  68. task.num_outputs_ = op_desc_->GetOutputsSize();
  69. // get kernel_ext_info
  70. auto &kernel_ext_info = kernel_def_.kernel_ext_info();
  71. auto kernel_ext_info_size = kernel_def_.kernel_ext_info_size();
  72. GE_CHK_BOOL_RET_STATUS(kernel_ext_info.size() == kernel_ext_info_size, FAILED,
  73. "task def kernel_ext_info.size=%zu, but kernel_ext_info_size=%u.",
  74. kernel_ext_info.size(), kernel_ext_info_size);
  75. ret = task.SetExtInfoAndType(kernel_ext_info, kernel_id);
  76. if (ret != SUCCESS) {
  77. GELOGE(ret, "Init ext info failed.");
  78. return ret;
  79. }
  80. auto aicpu_param_head = reinterpret_cast<aicpu::AicpuParamHead *>(task.args_.get());
  81. if (task.ext_info_addr_dev_ != nullptr) {
  82. aicpu_param_head->extInfoLength = kernel_ext_info.size();
  83. aicpu_param_head->extInfoAddr = reinterpret_cast<uintptr_t>(task.ext_info_addr_dev_);
  84. }
  85. return SUCCESS;
  86. }
  87. } // namespace ge

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