Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "adapter/tbe_adapter/kernel_launch/tbe_kernel_launch.h"
12 : #include "framework/common/runtime_model_ge.h"
13 : #include "common/fe_log.h"
14 : #include "common/aicore_util_attr_define.h"
15 : #include "common/aicore_util_constants.h"
16 : #include "common/fe_inner_error_codes.h"
17 : #include "rt_error_codes.h"
18 : #include "rt_external_mem.h"
19 : #include "common/platform_utils.h"
20 :
21 : namespace fe {
22 : namespace {
23 : const uint32_t MIN_ARG_SIZE = 4;
24 : const uint32_t ARG_ENTRY_SIZE = 2624; // 1536 + 1024(tiling) + 64(host mem)
25 : } // namespace
26 :
27 : uint32_t g_args_count = 1;
28 : uint16_t g_args_offset[ARG_ENTRY_SIZE / MIN_ARG_SIZE];
29 :
30 : Status TbeKernelLaunch::DealKernelLaunch(const ge::Node &node, const void *args, const uint32_t &args_size,
31 : const std::string &stub_func, const uint32_t &core_dim,
32 : domi::TaskDef &task_def) {
33 : string op_name = node.GetName();
34 : string op_type = node.GetType();
35 : auto op_desc = node.GetOpDesc();
36 : // malloc args_size + append_args_size
37 : size_t append_args_size = GetAppendArgsSizeOf() * GetAppendArgsNum();
38 : uint32_t total_args_size = args_size + append_args_size;
39 :
40 : std::vector<uint8_t> args_buff(total_args_size, 0);
41 : if (memcpy_s(args_buff.data(), total_args_size, args, args_size) != EOK) {
42 : FE_LOGE("[GenTask][KernelLaunch] Copy args data failed.");
43 : return TASK_BUILDER_STATUS_INTERNAL_ERROR;
44 : }
45 : // memcpy form append_args
46 : if (AddAppendArgs(node, args_buff.data(), args_size) != SUCCESS) {
47 : return TASK_BUILDER_STATUS_INTERNAL_ERROR;
48 : }
49 : // 5. call KernelLaunch
50 : FE_LOGD("Op[name=%s, type=%s]: args_size: %u bytes, append_args_size: %zu bytes, total_args_size: %u bytes.",
51 : op_name.c_str(), op_type.c_str(), args_size, append_args_size, total_args_size);
52 : if (append_args_size > 0) {
53 : PrintAllArgs(op_name, op_type, args_buff.data(), args_size);
54 : }
55 :
56 : bool ret = false;
57 : std::string first_kernel_name;
58 : if (ge::AttrUtils::GetStr(op_desc, ATTR_NAME_KERNEL_LIST_FIRST_NAME, first_kernel_name)) {
59 : FE_LOGD("Node name is [%s], first kernel name is [%s].", op_name.c_str(), first_kernel_name.c_str());
60 : ret = KernelLaunchWithHandle(core_dim, args_buff.data(), total_args_size, nullptr, task_def);
61 : } else {
62 : ret = KernelLaunch(stub_func, core_dim, args_buff.data(), total_args_size, nullptr, task_def);
63 : }
64 :
65 : if (!ret) {
66 : return TASK_BUILDER_STATUS_RUNTIME_ERROR;
67 : }
68 : return SUCCESS;
69 : }
70 :
71 : void TbeKernelLaunch::PrintAllArgs(const string &op_name, const string &op_type, const void *all_args_buff,
72 : uint32_t args_size) {
73 : for (size_t i = 0; i != args_size / sizeof(uint64_t); ++i) {
74 : uint64_t value = *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(all_args_buff) + i * sizeof(uint64_t)));
75 : FE_LOGD("Op[name=%s, type=%s]: args[%zu]=[%lu].", op_name.c_str(), op_type.c_str(), i, value);
76 : }
77 :
78 : for (size_t i = 0; i != GetAppendArgsNum(); ++i) {
79 : uint64_t value = *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(all_args_buff) + args_size +
80 : i * GetAppendArgsSizeOf()));
81 : FE_LOGD("Op[name=%s, type=%s]: append_args[%zu]=[%lu].", op_name.c_str(), op_type.c_str(), i, value);
82 : }
83 : }
84 :
85 2 : size_t TbeKernelLaunch::GetAppendArgsSizeOf() const {
86 : return 0;
87 : }
88 2 : size_t TbeKernelLaunch::GetAppendArgsNum() const {
89 : return 0;
90 : }
91 : Status TbeKernelLaunch::AddAppendArgs(const ge::Node &node, void *all_args_buff, const uint32_t &args_size) {
92 : (void)node;
93 : (void)all_args_buff;
94 : (void)args_size;
95 : return SUCCESS;
96 : }
97 :
98 : bool TbeKernelLaunch::KernelLaunch(const std::string &stub_func, const uint32_t block_dim, const void *args,
99 : uint32_t args_size, const rtSmDesc_t *sm_desc, domi::TaskDef &task_def) {
100 : task_def.set_type(static_cast<uint32_t>(ACL_RT_MODEL_TASK_KERNEL));
101 : domi::KernelDef *kernel_def = task_def.mutable_kernel();
102 : if (kernel_def == nullptr) {
103 : FE_LOGE("[GenTask][KernelLaunch] kernel_def is nullptr.");
104 : return false;
105 : }
106 :
107 : FE_LOGD("[GenTask][KernelLaunch] stub_func_name is [%s]", stub_func.c_str());
108 : kernel_def->set_stub_func(stub_func);
109 : if (sm_desc != nullptr) {
110 : uintptr_t sm_desc_data = reinterpret_cast<uintptr_t>(sm_desc);
111 : uint8_t *sm_desc_ptr = reinterpret_cast<uint8_t *>(sm_desc_data);
112 : kernel_def->set_sm_desc(sm_desc_ptr, sizeof(rtSmDesc_t));
113 : }
114 :
115 : kernel_def->set_block_dim(block_dim);
116 : kernel_def->set_args_size(args_size);
117 : kernel_def->set_args(args, args_size);
118 :
119 : domi::KernelContext *kernel_context = kernel_def->mutable_context();
120 : if (kernel_context == nullptr) {
121 : REPORT_FE_ERROR("[GenTask][KernelLaunch] kernel_context is nullptr.");
122 : return false;
123 : }
124 : g_args_offset[0] = 0;
125 : kernel_context->set_args_count(g_args_count);
126 : kernel_context->set_args_offset(g_args_offset, g_args_count * sizeof(uint16_t));
127 : return true;
128 : }
129 :
130 : bool TbeKernelLaunch::KernelLaunchWithHandle(const uint32_t block_dim, const void *args, uint32_t args_size,
131 : const rtSmDesc_t *sm_desc, domi::TaskDef &task_def) {
132 : task_def.set_type(static_cast<uint32_t>(ACL_RT_MODEL_TASK_ALL_KERNEL));
133 : domi::KernelDefWithHandle *kernel_def_with_handle = task_def.mutable_kernel_with_handle();
134 : if (kernel_def_with_handle == nullptr) {
135 : FE_LOGE("[GenTask][KernelLaunchWithHandle] kernel_def_with_handle is nullptr.");
136 : return false;
137 : }
138 :
139 : if (sm_desc != nullptr) {
140 : uintptr_t sm_desc_data = reinterpret_cast<uintptr_t>(sm_desc);
141 : uint8_t *sm_desc_ptr = reinterpret_cast<uint8_t *>(sm_desc_data);
142 : kernel_def_with_handle->set_sm_desc(sm_desc_ptr, sizeof(rtSmDesc_t));
143 : }
144 : kernel_def_with_handle->set_block_dim(block_dim);
145 : kernel_def_with_handle->set_args_size(args_size);
146 : kernel_def_with_handle->set_args(args, args_size);
147 :
148 : domi::KernelContext *kernel_context = kernel_def_with_handle->mutable_context();
149 : if (kernel_context == nullptr) {
150 : FE_LOGE("[GenTask][KernelLaunch] kernel_context is nullptr.");
151 : return false;
152 : }
153 : g_args_offset[0] = 0;
154 : kernel_context->set_args_count(g_args_count);
155 : kernel_context->set_args_offset(g_args_offset, g_args_count * sizeof(uint16_t));
156 : return true;
157 : }
158 : } // namespace fe
|