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/tbe_task_builder_adapter.h"
12 : #include "framework/common/runtime_model_ge.h"
13 : #include "adapter/tbe_adapter/kernel_launch/l2_cache_kernel_launch.h"
14 : #include "common/fe_log.h"
15 : #include "common/platform_utils.h"
16 : #include "common/configuration.h"
17 : #include "common/fe_context_utils.h"
18 : #include "common/graph_comm.h"
19 : #include "common/l2_stream_info.h"
20 : #include "common/lxfusion_json_util.h"
21 : #include "common/op_tensor_utils.h"
22 : #include "common/nn_engine_math_util.h"
23 : #include "common/string_utils.h"
24 : #include "common/fe_inner_attr_define.h"
25 : #include "common/aicore_util_constants.h"
26 : #include "common/fe_graph_common.h"
27 : #include "common/fe_inner_error_codes.h"
28 : #include "graph/debug/ge_attr_define.h"
29 : #include "graph/tuning_utils.h"
30 : #include "rt_error_codes.h"
31 : #include "securec.h"
32 : #include "register/graph_optimizer/fusion_common/unknown_shape_utils.h"
33 : #include "ops_kernel_builder/task_builder/args_format_constructor.h"
34 :
35 : namespace fe {
36 : namespace {
37 : const uint32_t MAX_L2_DATANUM = 8;
38 : // tiling data size to be reserved when generate task for unknown shape op
39 : const int RESERVED_TILING_DATA_SIZE = 8;
40 : const int RESERVED_GLOBALWORKSPACE_SIZE = 8;
41 : const uint32_t DEFAULT_KERNEL_BLOCK_DIM = 1;
42 : const int ONE_MEM_TYPE_SIZE = 1;
43 : const std::unordered_set<std::string> DEV_BINARY_MAGIC_TYPE{
44 : "RT_DEV_BINARY_MAGIC_ELF", "RT_DEV_BINARY_MAGIC_ELF_AIVEC", "RT_DEV_BINARY_MAGIC_ELF_AICUBE",
45 : "FFTS_BINARY_MAGIC_ELF_MIX_AIC", "FFTS_BINARY_MAGIC_ELF_MIX_AIV", "RT_DEV_BINARY_MAGIC_OM"};
46 : const std::string kStrValidBinaryMagic = "RT_DEV_BINARY_MAGIC_(ELF/ELF_AIVEC/ELF_AICUBE)";
47 : } // namespace
48 :
49 : thread_local rtL2Ctrl_t g_tel2ctrl;
50 :
51 : void TbeTaskBuilderAdapter::MemCpyForL2IdAndL2Addr(uint64_t &cur_ptr, uint32_t &l2_args_size, int64_t data_in_l2_id,
52 : uint64_t data_in_l2_addr) const {
53 : if (l2_args_size < sizeof(int64_t)) {
54 : REPORT_FE_ERROR("[GenTask][Memcpy][Node %s type %s] l2_args_size (which is %u) is smaller than size of int64_t.",
55 : op_desc_->GetName().c_str(), op_desc_->GetType().c_str(), l2_args_size);
56 : return;
57 : }
58 : errno_t ret = memcpy_s(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(cur_ptr)), l2_args_size, &data_in_l2_id,
59 : sizeof(int64_t));
60 : if (ret != EOK) {
61 : REPORT_FE_ERROR("[GenTask][Memcpy][Node %s type %s] Failed to memcpy data in l2 id, error num is %d.",
62 : op_desc_->GetName().c_str(), op_desc_->GetType().c_str(), ret);
63 : return;
64 : }
65 : l2_args_size = l2_args_size - sizeof(uint64_t);
66 : cur_ptr = cur_ptr + sizeof(uint64_t);
67 :
68 : if (l2_args_size < sizeof(int64_t)) {
69 : REPORT_FE_ERROR("[GenTask][Memcpy][Node %s type %s] l2_args_size (which is %u) is smaller than size of int64_t.",
70 : op_desc_->GetName().c_str(), op_desc_->GetType().c_str(), l2_args_size);
71 : return;
72 : }
73 : ret = memcpy_s(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(cur_ptr)), l2_args_size, &data_in_l2_addr,
74 : sizeof(uint64_t));
75 : if (ret != EOK) {
76 : REPORT_FE_ERROR("[GenTask][Memcpy][Node %s type %s] Failed to memcpy data in l2 addr, error num is %d.",
77 : op_desc_->GetName().c_str(), op_desc_->GetType().c_str(), ret);
78 : return;
79 : }
80 : l2_args_size = l2_args_size - sizeof(uint64_t);
81 : cur_ptr = cur_ptr + sizeof(uint64_t);
82 : }
83 :
84 : void TbeTaskBuilderAdapter::DealInputOutputWithDdr(int32_t data_num, uint64_t &cur_ptr, uint32_t &l2_args_size) const {
85 : for (int i = 0; i != data_num; ++i) {
86 : MemCpyForL2IdAndL2Addr(cur_ptr, l2_args_size, -1, 0);
87 : }
88 : }
89 :
90 : template <typename T>
91 : void TbeTaskBuilderAdapter::DealInputOutputL2DataMap(const T &l2datamap, int32_t data_num, const void *x[],
92 : const void *y[], uint64_t &cur_ptr, uint32_t &l2_args_size,
93 : bool is_input) const {
94 : for (int i = 0; i < data_num; ++i) {
95 : typename T::const_iterator iter;
96 : for (iter = l2datamap.begin(); iter != l2datamap.end(); ++iter) {
97 : const auto &flowdata = iter->second;
98 : int64_t data_in_l2_id = flowdata.l2Index;
99 : auto ddr_key = iter->first;
100 :
101 : if (is_input && static_cast<uint64_t>(reinterpret_cast<uintptr_t>(x[i])) == ddr_key) {
102 : FE_LOGD("iter->first value is %ld, (uint64_t)(uintptr_t)x[%d] value is %ld", ddr_key, i,
103 : static_cast<uint64_t>(reinterpret_cast<uintptr_t>(x[i])));
104 : MemCpyForL2IdAndL2Addr(cur_ptr, l2_args_size, data_in_l2_id, flowdata.l2Addr);
105 : break;
106 : }
107 :
108 : if (!is_input && static_cast<uint64_t>(reinterpret_cast<uintptr_t>(y[i])) == ddr_key) {
109 : FE_LOGD("iter->first value is %ld, (uint64_t)(uintptr_t)y[%d] value is %ld", ddr_key, i,
110 : static_cast<uint64_t>(reinterpret_cast<uintptr_t>(y[i])));
111 : MemCpyForL2IdAndL2Addr(cur_ptr, l2_args_size, data_in_l2_id, flowdata.l2Addr);
112 : break;
113 : }
114 : }
115 :
116 : if (iter == l2datamap.end()) {
117 : std::string input_or_output = is_input ? "input" : "output";
118 : FE_LOGD("Can not find anything in l2datamap for the %s %d, set l2_index=-1 and l2_offset=0.",
119 : input_or_output.c_str(), i);
120 : MemCpyForL2IdAndL2Addr(cur_ptr, l2_args_size, -1, 0);
121 : }
122 : }
123 : }
124 :
125 : Status TbeTaskBuilderAdapter::SaveTeCoreL2FlowDataForL2Buffer(int32_t input_num, int32_t output_num, uint64_t cur_ptr,
126 : const void *x[], const void *y[], rtL2Ctrl_t &tel2ctrl,
127 : uint32_t l2_args_size, uint32_t workspace_num) {
128 : TaskL2Info *l2_data = nullptr;
129 : (void)memset_s(&tel2ctrl, sizeof(rtL2Ctrl_t), 0, sizeof(rtL2Ctrl_t));
130 : std::string batch_label = "Batch_-1";
131 : (void)ge::AttrUtils::GetStr(node_.GetOpDesc(), ge::ATTR_NAME_BATCH_LABEL, batch_label);
132 : int64_t stream_id = node_.GetOpDesc()->GetStreamId();
133 : Status ret = StreamL2Info::Instance().GetStreamL2Info(stream_id, node_.GetName(), l2_data, batch_label);
134 : if ((ret == SUCCESS) && (l2_data != nullptr)) {
135 : FE_LOGI("Node[type=%s,name=%s]: find the l2 data from stream_l2_map.", node_.GetType().c_str(),
136 : node_.GetName().c_str());
137 : L2DataMap input = l2_data->input;
138 : L2DataMap output = l2_data->output;
139 : DealInputOutputL2DataMap(input, input_num, x, y, cur_ptr, l2_args_size, true);
140 : DealInputOutputL2DataMap(output, output_num, x, y, cur_ptr, l2_args_size, false);
141 : DealInputOutputWithDdr(workspace_num, cur_ptr, l2_args_size);
142 : tel2ctrl = l2_data->l2ctrl;
143 : return SUCCESS;
144 : } else { // Const/PlaceHolder/PlaceEnd/Data
145 : FE_LOGW("Node[type=%s,name=%s]: cannot find the l2 data from stream_l2_map.", node_.GetType().c_str(),
146 : node_.GetName().c_str());
147 : return FAILED;
148 : }
149 : }
150 :
151 : Status TbeTaskBuilderAdapter::SaveTeCoreL2FlowDataForL2Fusion(int32_t input_num, int32_t output_num, uint64_t cur_ptr,
152 : const void *x[], const void *y[], rtL2Ctrl_t &tel2ctrl,
153 : uint32_t l2_args_size, uint32_t workspace_num) {
154 : (void)memset_s(&tel2ctrl, sizeof(rtL2Ctrl_t), 0, sizeof(rtL2Ctrl_t));
155 :
156 : ge::OpDescPtr node_desc = node_.GetOpDesc();
157 : L2FusionInfoPtr l2_info = GetL2FusionInfoFromJson(node_desc);
158 : if (l2_info == nullptr) {
159 : FE_LOGD("Node[type=%s,name=%s]: the l2_fusion_info is nullptr.", node_desc->GetType().c_str(),
160 : node_desc->GetName().c_str());
161 : return PARAM_INVALID;
162 : }
163 :
164 : L2FusionDataMap_t &input = l2_info->input;
165 : L2FusionDataMap_t &output = l2_info->output;
166 : DealInputOutputL2DataMap(input, input_num, x, y, cur_ptr, l2_args_size, true);
167 : DealInputOutputL2DataMap(output, output_num, x, y, cur_ptr, l2_args_size, false);
168 : DealInputOutputWithDdr(workspace_num, cur_ptr, l2_args_size);
169 :
170 : tel2ctrl = l2_info->l2_info.l2ctrl;
171 : FE_LOGD("Node[type=%s,name=%s]: SaveL2DataFlow find L2 Alloc and do L2fusion success.", node_desc->GetType().c_str(),
172 : node_desc->GetName().c_str());
173 : return SUCCESS;
174 : }
175 :
176 : void TbeTaskBuilderAdapter::DisplayRtL2CtrlInfo(const rtL2Ctrl_t &l2ctrl, bool enable_l2) const {
177 : FE_LOGD("L2ctrl.size = %lu.", l2ctrl.size);
178 : FE_LOGD("L2 %s.", enable_l2 ? "enable" : "disable");
179 : for (uint32_t i = 0; i < MAX_L2_DATANUM; i++) {
180 : if (l2ctrl.data[i].L2_mirror_addr != 0) {
181 : FE_LOGD("data_index = %u.", i);
182 : FE_LOGD("L2_data_section_size = %u.", l2ctrl.data[i].L2_data_section_size);
183 : FE_LOGD("L2_mirror_addr = 0x%lx.", l2ctrl.data[i].L2_mirror_addr);
184 : FE_LOGD("L2_page_offset_base = %u.", l2ctrl.data[i].L2_page_offset_base);
185 : FE_LOGD("prev_L2_page_offset_base = %d.", l2ctrl.data[i].prev_L2_page_offset_base);
186 : FE_LOGD("L2_preload = %u.", l2ctrl.data[i].L2_preload);
187 : FE_LOGD("L2_load_to_ddr = %u.", l2ctrl.data[i].L2_load_to_ddr);
188 : FE_LOGD("modified = %u.", l2ctrl.data[i].modified);
189 : FE_LOGD("priority = %u.", l2ctrl.data[i].priority);
190 : }
191 : }
192 : }
193 :
194 : Status TbeTaskBuilderAdapter::CheckArrayValue(const void *array[], int32_t array_size, int32_t num,
195 : const string &name) const {
196 : if (array == nullptr) {
197 : FE_LOGD("%s is nullptr! Please check.", name.c_str());
198 : } else {
199 : int32_t check_size = num;
200 : if (array_size < num) {
201 : check_size = array_size;
202 : FE_LOGD("[GenTask][TbeFwd][Check][Node %s type %s] The %s array_size[%d] < num[%d].", op_desc_->GetName().c_str(),
203 : op_desc_->GetType().c_str(), name.c_str(), array_size, num);
204 : }
205 : for (int i = 0; i < check_size; i++) {
206 : if (array[i] == nullptr) {
207 : FE_LOGD("[GenTask][TbeFwd][Check][Node %s type %s] The %s[%d] now is nullptr.", op_desc_->GetName().c_str(),
208 : op_desc_->GetType().c_str(), name.c_str(), i);
209 : }
210 : }
211 : }
212 : return SUCCESS;
213 : }
214 :
215 : Status TbeTaskBuilderAdapter::CheckForForward(const void *args, const void *x[], int32_t x_array_size, const void *y[],
216 : int32_t input_num, int32_t output_num) const {
217 : FE_CHECK_NOTNULL(args);
218 : string x_name = "x";
219 : Status result = CheckArrayValue(x, x_array_size, input_num, x_name);
220 : if (result != SUCCESS) {
221 : return result;
222 : }
223 :
224 : string y_name = "y";
225 : result = CheckArrayValue(y, output_num, output_num, y_name);
226 : if (result != SUCCESS) {
227 : return result;
228 : }
229 :
230 : if (CheckUint32AddOverflow(static_cast<uint32_t>(input_num), static_cast<uint32_t>(output_num)) != SUCCESS) {
231 : REPORT_FE_ERROR("[GenTask][TbeFwd][Check] Unsigned Integer %u and %u addition can result in overflow!",
232 : static_cast<uint32_t>(input_num), static_cast<uint32_t>(output_num));
233 : return TASK_BUILDER_STATUS_BAD_PARAM;
234 : }
235 : if (CheckUint32MulOverflow((static_cast<uint32_t>(input_num) + static_cast<uint32_t>(output_num)),
236 : (static_cast<uint32_t>(sizeof(int64_t) + sizeof(uint64_t)))) != SUCCESS) {
237 : REPORT_FE_ERROR("[GenTask][TbeFwd][Check] Unsigned Integer %u and %u multiplication can result in overflow!",
238 : (static_cast<uint32_t>(input_num) + static_cast<uint32_t>(output_num)),
239 : (static_cast<uint32_t>(sizeof(int64_t) + sizeof(uint64_t))));
240 : return TASK_BUILDER_STATUS_BAD_PARAM;
241 : }
242 : return SUCCESS;
243 : }
244 :
245 : Status TbeTaskBuilderAdapter::DealKernelLaunchForL2Buffer(int32_t input_num, int32_t output_num, uint64_t cur_ptr,
246 : const void *x[], const void *y[], rtL2Ctrl_t &tel2ctrl,
247 : uint32_t args_size, uint32_t l2_args_size,
248 : const std::string &stub_func, const uint32_t core_dim,
249 : const void *tmp_buf, int32_t workspace_num,
250 : domi::TaskDef &task_def) {
251 : bool kernelRet = false;
252 : std::string first_kernel_name;
253 : Status ret =
254 : SaveTeCoreL2FlowDataForL2Buffer(input_num, output_num, cur_ptr, x, y, tel2ctrl, l2_args_size, workspace_num);
255 : if (ret == SUCCESS) {
256 : FE_LOGD("Node[type=%s,name=%s]: L2 alloc information get success, core_dim=%u, args_size=%u, l2_args_size=%u.",
257 : node_.GetTypePtr(), node_.GetNamePtr(), core_dim, args_size, l2_args_size);
258 :
259 : for (uint64_t idx = 0; idx < ((args_size) / sizeof(uint64_t)); ++idx) {
260 : uint64_t current_address =
261 : *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(tmp_buf) + idx * sizeof(uint64_t)));
262 : FE_LOGD("Node[type=%s,name=%s]: do distribute index[%lu], args=[%lu] is ddr address value.", node_.GetTypePtr(),
263 : node_.GetNamePtr(), idx, current_address);
264 : }
265 : uint32_t index_of_offset_base = 2;
266 : for (uint64_t idx = 0; idx < (l2_args_size / (sizeof(uint64_t))); ++idx) {
267 : if (idx % index_of_offset_base == 0) {
268 : uint64_t current_address =
269 : *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(tmp_buf) + args_size + idx * sizeof(int64_t)));
270 : FE_LOGD("Node[type=%s,name=%s]: do distribute index[%lu], args=[%lu] is l2 index value.", node_.GetTypePtr(),
271 : node_.GetNamePtr(), idx, current_address);
272 : } else {
273 : uint64_t current_address =
274 : *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(tmp_buf) + args_size + idx * sizeof(uint64_t)));
275 : FE_LOGD("Node[type=%s,name=%s]: do distribute index[%lu], args=[%lu] is l2 offset value.", node_.GetTypePtr(),
276 : node_.GetNamePtr(), idx, current_address);
277 : }
278 : }
279 : if (ge::AttrUtils::GetStr(node_.GetOpDesc(), ATTR_NAME_KERNEL_LIST_FIRST_NAME, first_kernel_name)) {
280 : kernelRet =
281 : TbeKernelLaunch::KernelLaunchWithHandle(core_dim, tmp_buf, args_size + l2_args_size, &tel2ctrl, task_def);
282 : } else {
283 : kernelRet =
284 : TbeKernelLaunch::KernelLaunch(stub_func, core_dim, tmp_buf, args_size + l2_args_size, &tel2ctrl, task_def);
285 : }
286 : } else {
287 : FE_LOGD("Node[type=%s,name=%s]: cannot find L2 alloc information and use the ddr address.", node_.GetTypePtr(),
288 : node_.GetNamePtr());
289 : if (ge::AttrUtils::GetStr(node_.GetOpDesc(), ATTR_NAME_KERNEL_LIST_FIRST_NAME, first_kernel_name)) {
290 : kernelRet = TbeKernelLaunch::KernelLaunchWithHandle(core_dim, tmp_buf, args_size, nullptr, task_def);
291 : } else {
292 : kernelRet = TbeKernelLaunch::KernelLaunch(stub_func, core_dim, tmp_buf, args_size, nullptr, task_def);
293 : }
294 : }
295 : if (!kernelRet) {
296 : return TASK_BUILDER_STATUS_RUNTIME_ERROR;
297 : }
298 : DisplayRtL2CtrlInfo(tel2ctrl, fe::GetFunctionState(fe::FuncParamType::FUSION_L2));
299 : return SUCCESS;
300 : }
301 :
302 : Status TbeTaskBuilderAdapter::DealKernelLaunchForL2Fusion(int32_t input_num, int32_t output_num, uint64_t cur_ptr,
303 : const void *x[], const void *y[], rtL2Ctrl_t &tel2ctrl,
304 : uint32_t args_size, uint32_t l2_args_size,
305 : const std::string &stub_func, const uint32_t core_dim,
306 : const void *tmp_buf, int32_t workspace_num,
307 : domi::TaskDef &task_def) {
308 : auto op_name = node_.GetName();
309 : auto op_type = node_.GetType();
310 : bool kernelRet = false;
311 : Status ret =
312 : SaveTeCoreL2FlowDataForL2Fusion(input_num, output_num, cur_ptr, x, y, tel2ctrl, l2_args_size, workspace_num);
313 : string first_kernel_name;
314 : if (ret == SUCCESS) {
315 : FE_LOGD("Node[type=%s,name=%s]: L2 alloc information get success, core_dim=%u, args_size=%u, l2_args_size=%u.",
316 : op_type.c_str(), op_name.c_str(), core_dim, args_size, l2_args_size);
317 :
318 : for (uint64_t idx = 0; idx < ((args_size) / sizeof(uint64_t)); ++idx) {
319 : uint64_t address = *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(tmp_buf) + idx * sizeof(uint64_t)));
320 : FE_LOGD("Node[type=%s,name=%s]: do distribute index[%lu], args[%lu] is ddr address value.", op_type.c_str(),
321 : op_name.c_str(), idx, address);
322 : }
323 : uint32_t index_offset_base = 2;
324 : for (uint64_t idx = 0; idx < (l2_args_size / (sizeof(uint64_t))); ++idx) {
325 : if (idx % index_offset_base == 0) {
326 : uint64_t address =
327 : *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(tmp_buf) + args_size + idx * sizeof(int64_t)));
328 : FE_LOGD("Node[type=%s,name=%s]: do distribute index[%lu], args=[%lu] is l2 index value.", op_type.c_str(),
329 : op_name.c_str(), idx, address);
330 : } else {
331 : uint64_t address =
332 : *(reinterpret_cast<uint64_t *>(reinterpret_cast<uintptr_t>(tmp_buf) + args_size + idx * sizeof(uint64_t)));
333 : FE_LOGD("Node[type=%s,name=%s]: do distribute index[%lu], args=[%lu] is l2 offset value.", op_type.c_str(),
334 : op_name.c_str(), idx, address);
335 : }
336 : }
337 : if (ge::AttrUtils::GetStr(node_.GetOpDesc(), ATTR_NAME_KERNEL_LIST_FIRST_NAME, first_kernel_name)) {
338 : kernelRet =
339 : TbeKernelLaunch::KernelLaunchWithHandle(core_dim, tmp_buf, args_size + l2_args_size, &tel2ctrl, task_def);
340 : } else {
341 : kernelRet =
342 : TbeKernelLaunch::KernelLaunch(stub_func, core_dim, tmp_buf, args_size + l2_args_size, &tel2ctrl, task_def);
343 : }
344 : } else {
345 : FE_LOGD("Node[type=%s,name=%s]: cannot find L2 Alloc information and use the ddr address(l2_index=-1,l2_offset=0).",
346 : op_type.c_str(), op_name.c_str());
347 : DealInputOutputWithDdr(input_num, cur_ptr, l2_args_size);
348 : DealInputOutputWithDdr(output_num, cur_ptr, l2_args_size);
349 : DealInputOutputWithDdr(workspace_num, cur_ptr, l2_args_size);
350 : if (ge::AttrUtils::GetStr(node_.GetOpDesc(), ATTR_NAME_KERNEL_LIST_FIRST_NAME, first_kernel_name)) {
351 : kernelRet =
352 : TbeKernelLaunch::KernelLaunchWithHandle(core_dim, tmp_buf, args_size + l2_args_size, nullptr, task_def);
353 : } else {
354 : kernelRet =
355 : TbeKernelLaunch::KernelLaunch(stub_func, core_dim, tmp_buf, args_size + l2_args_size, nullptr, task_def);
356 : }
357 : }
358 : if (!kernelRet) {
359 : return TASK_BUILDER_STATUS_RUNTIME_ERROR;
360 : }
361 : DisplayRtL2CtrlInfo(tel2ctrl, true);
362 : return SUCCESS;
363 : }
364 :
365 : Status TbeTaskBuilderAdapter::TbeForward(const uint32_t core_dim, const void *args, uint32_t args_size,
366 : int32_t input_num, const void *x[], int32_t x_array_size, int32_t output_num,
367 : const void *y[], int32_t workspace_num, domi::TaskDef &task_def) {
368 : Status ret = CheckForForward(args, x, x_array_size, const_cast<const void **>(y), input_num, output_num);
369 : if (ret != SUCCESS) {
370 : return ret;
371 : }
372 :
373 : std::string stub_func;
374 : if (!ge::AttrUtils::GetStr(op_desc_, ATTR_NAME_KERNEL_LIST_FIRST_NAME, stub_func)) {
375 : stub_func = GetUniqueGraphIdForNode();
376 : }
377 : FE_LOGD("Generate stub func string[%s] of node[%s, %s].", stub_func.c_str(), op_desc_->GetNamePtr(),
378 : op_desc_->GetTypePtr());
379 :
380 : // l2 buffer or l2 fusion
381 : bool is_l2_buffer = fe::GetFunctionState(fe::FuncParamType::FUSION_L2);
382 : bool lx_fusion_pass = false;
383 : (void)ge::AttrUtils::GetBool(op_desc_, ATTR_NAME_LX_FUSION_PASS, lx_fusion_pass);
384 : bool is_l2_fusion = (Configuration::Instance(AI_CORE_NAME).EnableL2Fusion() && lx_fusion_pass) ||
385 : (FEContextUtils::GetBuildMode() == ge::BUILD_MODE_TUNING);
386 : if (is_l2_fusion || is_l2_buffer) {
387 : if (CheckUint32AddOverflow(static_cast<uint32_t>(input_num), static_cast<uint32_t>(output_num)) != SUCCESS) {
388 : REPORT_FE_ERROR(
389 : "[GenTask][TbeFwd][L2Check][Node %s type %s] Unsigned Integer %u and %u addition can result in overflow!",
390 : op_desc_->GetName().c_str(), op_desc_->GetType().c_str(), static_cast<uint32_t>(input_num),
391 : static_cast<uint32_t>(output_num));
392 : return TASK_BUILDER_STATUS_BAD_PARAM;
393 : }
394 : if (CheckUint32AddOverflow(static_cast<uint32_t>(input_num + output_num), static_cast<uint32_t>(workspace_num)) !=
395 : SUCCESS) {
396 : REPORT_FE_ERROR(
397 : "[GenTask][TbeFwd][L2Check][Node %s type %s] Unsigned Integer %u and %u addition can result in overflow!",
398 : op_desc_->GetName().c_str(), op_desc_->GetType().c_str(), static_cast<uint32_t>(input_num + output_num),
399 : static_cast<uint32_t>(workspace_num));
400 : return TASK_BUILDER_STATUS_BAD_PARAM;
401 : }
402 : uint32_t l2_args_size =
403 : static_cast<uint32_t>(input_num + output_num + workspace_num) * (sizeof(int64_t) + sizeof(uint64_t));
404 : std::vector<uint8_t> tmp_buf(args_size + l2_args_size, 0);
405 : if (memcpy_s(tmp_buf.data(), args_size, args, args_size) != EOK) {
406 : FE_LOGE("[GenTask][TbeForward] Copy args data failed.");
407 : return TASK_BUILDER_STATUS_INTERNAL_ERROR;
408 : }
409 :
410 : uint64_t cur_ptr = ge::PtrToValue(tmp_buf.data()) + args_size;
411 : if (is_l2_buffer) {
412 : ret = DealKernelLaunchForL2Buffer(input_num, output_num, cur_ptr, x, y, g_tel2ctrl, args_size, l2_args_size,
413 : stub_func, core_dim, tmp_buf.data(), workspace_num, task_def);
414 : } else {
415 : ret = DealKernelLaunchForL2Fusion(input_num, output_num, cur_ptr, x, y, g_tel2ctrl, args_size, l2_args_size,
416 : stub_func, core_dim, tmp_buf.data(), workspace_num, task_def);
417 : }
418 : return ret;
419 : }
420 :
421 : if (PlatformUtils::Instance().IsEnableL2CacheRc()) {
422 : L2CacheKernelLaunch l2_cache_kernel_launch(input_num);
423 : return l2_cache_kernel_launch.DealKernelLaunch(node_, args, args_size, stub_func, core_dim, task_def);
424 : } else {
425 : TbeKernelLaunch tbe_kernel_launch(input_num);
426 : return tbe_kernel_launch.DealKernelLaunch(node_, args, args_size, stub_func, core_dim, task_def);
427 : }
428 : }
429 :
430 : Status TbeTaskBuilderAdapter::CheckTensorSize(const ge::GeTensorDesc &tensor_desc, uint32_t i, bool is_input,
431 : int32_t output_real_calc_flag) const {
432 : auto op_type = op_desc_->GetType();
433 : auto op_name = op_desc_->GetName();
434 : int64_t tensor_size = 0;
435 : if (OpTensorUtils::CalcTensorSize(tensor_desc, output_real_calc_flag, tensor_size) != SUCCESS) {
436 : REPORT_FE_ERROR("[GenTask][CheckTensorSize][Node %s type %s]:op output[%u] tensor size failed to calculate.",
437 : op_name.c_str(), op_type.c_str(), i);
438 : return FAILED;
439 : }
440 : int64_t size_output = 0;
441 : if (ge::TensorUtils::GetSize(tensor_desc, size_output) != ge::GRAPH_SUCCESS) {
442 : REPORT_FE_ERROR("[GenTask][CheckTensorSize][Node %s type %s]:Get size input[%u] tensor failed!", op_name.c_str(),
443 : op_type.c_str(), i);
444 : return fe::FAILED;
445 : }
446 : string input_or_output = is_input ? "Input" : "Output";
447 : // compare the two size
448 : if (size_output < tensor_size) {
449 : std::vector<int64_t> shape_dims = tensor_desc.GetShape().GetDims();
450 : REPORT_FE_ERROR("[GenTask][CheckTensorSize] Node[%s, %s]: %s shape is [%s], which size %ld is not equal to %ld",
451 : op_name.c_str(), op_type.c_str(), input_or_output.c_str(),
452 : StringUtils::IntegerVecToString(shape_dims).c_str(), tensor_size, size_output);
453 : return fe::FAILED;
454 : }
455 : return SUCCESS;
456 : }
457 :
458 : Status TbeTaskBuilderAdapter::CheckInputAndOutputSize() {
459 : // Get input size
460 : int32_t output_real_calc_flag = 0;
461 : for (size_t i = 0; i < op_desc_->GetAllInputsSize(); i++) {
462 : ge::GeTensorDescPtr tensorDescPtr = op_desc_->MutableInputDesc(i);
463 : if (tensorDescPtr == nullptr) {
464 : continue;
465 : }
466 : ge::GeTensorDesc tensor_desc = op_desc_->GetInputDesc(i);
467 : if (CheckTensorSize(tensor_desc, i, true, output_real_calc_flag) != SUCCESS) {
468 : return FAILED;
469 : }
470 : }
471 : bool ret = ge::AttrUtils::GetInt(op_desc_, ge::ATTR_NAME_GET_TENSOR_ACTUAL_SIZE, output_real_calc_flag);
472 : FE_LOGD("Output_real_calc_flag: [%d], ret: [%d].", output_real_calc_flag, ret);
473 : for (size_t i = 0; i < op_desc_->GetAllOutputsDescSize(); i++) {
474 : ge::GeTensorDescPtr tensorDescPtr = op_desc_->MutableOutputDesc(i);
475 : if (tensorDescPtr == nullptr) {
476 : continue;
477 : }
478 : ge::GeTensorDesc tensor_desc = op_desc_->GetOutputDesc(i);
479 : if (CheckTensorSize(tensor_desc, i, false, output_real_calc_flag) != SUCCESS) {
480 : return FAILED;
481 : }
482 : }
483 : return SUCCESS;
484 : }
485 :
486 : TbeTaskBuilderAdapter::TbeTaskBuilderAdapter(const ge::Node &node, TaskBuilderContext &context)
487 : : TaskBuilderAdapter(node, context), block_dim_(DEFAULT_KERNEL_BLOCK_DIM) {}
488 :
489 : TbeTaskBuilderAdapter::~TbeTaskBuilderAdapter() {}
490 :
491 : uint64_t TbeTaskBuilderAdapter::GetAtomicStubFuncId() const {
492 : static std::atomic<uint64_t> global_cmo_id(1);
493 : return global_cmo_id.fetch_add(1, std::memory_order_relaxed);
494 : }
495 :
496 : std::string TbeTaskBuilderAdapter::GetUniqueGraphIdForNode() const {
497 : string session_graph_id = "";
498 : string atomic_id = std::to_string(GetAtomicStubFuncId());
499 : if (ge::AttrUtils::GetStr(op_desc_, ge::ATTR_NAME_SESSION_GRAPH_ID, session_graph_id) && !session_graph_id.empty()) {
500 : return atomic_id + "_" + session_graph_id + "_" + op_desc_->GetName();
501 : } else {
502 : return atomic_id + "_" + op_desc_->GetName();
503 : }
504 : }
505 :
506 : Status TbeTaskBuilderAdapter::Init() {
507 : auto op_type = op_desc_->GetType();
508 : auto op_name = op_desc_->GetName();
509 : FE_LOGD("Init begin, node name:%s, node type:%s.", node_.GetName().c_str(), node_.GetType().c_str());
510 :
511 : // Common initialization
512 : Status status = TaskBuilderAdapter::Init();
513 : if (status != SUCCESS) {
514 : REPORT_FE_ERROR("[GenTask][Init][Node %s type %s]:TaskBuilderAdapter::Init failed.", op_name.c_str(),
515 : op_type.c_str());
516 : return status;
517 : }
518 :
519 : // Get block dim
520 : int32_t block_dim = -1;
521 : if (ge::AttrUtils::GetInt(op_desc_, ge::TVM_ATTR_NAME_BLOCKDIM, block_dim)) {
522 : block_dim_ = static_cast<uint32_t>(block_dim);
523 : }
524 : FE_LOGD("Tbe blockdim: %u.", block_dim_);
525 :
526 : // Get magic
527 : string bin_magic;
528 : (void)ge::AttrUtils::GetStr(op_desc_, ge::TVM_ATTR_NAME_MAGIC, bin_magic);
529 : if (DEV_BINARY_MAGIC_TYPE.find(bin_magic) == DEV_BINARY_MAGIC_TYPE.end()) {
530 : REPORT_FE_ERROR("[GenTask][Init] Node[%s, %s]: binary magic %s is unsupported. Only support %s.", op_name.c_str(),
531 : op_type.c_str(), bin_magic.c_str(), kStrValidBinaryMagic.c_str());
532 : return PARAM_INVALID;
533 : }
534 :
535 : // Check Size
536 : if (!UnknownShapeUtils::IsUnknownShapeOp(*op_desc_)) {
537 : Status ret = CheckInputAndOutputSize();
538 : if (ret != SUCCESS) {
539 : return fe::FAILED;
540 : }
541 : }
542 :
543 : return SUCCESS;
544 : }
545 :
546 : void TbeTaskBuilderAdapter::SetInputAddrFromDataBase(const size_t input_index, const int64_t &input_offset) {
547 : int64_t tensor_memtype = RT_MEMORY_HBM;
548 : if (ge::AttrUtils::GetInt(op_desc_->GetInputDescPtr(input_index), ge::ATTR_NAME_TENSOR_MEM_TYPE, tensor_memtype)) {
549 : FE_LOGD("The value of input_index is %zu, and the value of input_offset is %ld", input_index, input_offset);
550 : }
551 :
552 : auto it = context_.mem_type_to_data_mem_base.find(tensor_memtype);
553 : if (it != context_.mem_type_to_data_mem_base.end()) {
554 : input_addrs_.push_back(it->second + input_offset);
555 : } else {
556 : input_addrs_.push_back(context_.dataMemBase + input_offset);
557 : FE_LOGD("set input_addrs_ from the plus of context_.dataMemBase and input_offset");
558 : }
559 : }
560 :
561 : Status TbeTaskBuilderAdapter::HandleAnchorWeight(const size_t &anchor_index) {
562 : auto op_type = op_desc_->GetType();
563 : auto op_name = op_desc_->GetName();
564 : auto in_desc_ptr = op_desc_->MutableInputDesc(static_cast<uint32_t>(anchor_index));
565 :
566 : int64_t weight_offset = 0;
567 : if (in_desc_ptr == nullptr) {
568 : return FAILED;
569 : }
570 : if (ge::TensorUtils::GetDataOffset(*in_desc_ptr, weight_offset) != ge::GRAPH_SUCCESS) {
571 : REPORT_FE_ERROR("[GenTask][HandleAnchor][Node %s type %s]: Get weight offset failed.", op_name.c_str(),
572 : op_type.c_str());
573 : return FAILED;
574 : }
575 :
576 : // max value of weight offset : 30 * 1024 * 1024 * 1024L.
577 : if (weight_offset < kMaxWeightOffset) {
578 : input_addrs_.push_back(context_.weightMemBase + weight_offset);
579 : } else {
580 : FE_LOGI("Print Input Addr is offset of op name: %s, op type: %s, weight offset is too big.",
581 : op_desc_->GetName().c_str(), op_desc_->GetType().c_str());
582 : input_addrs_.push_back(reinterpret_cast<void *>(reinterpret_cast<intptr_t>(weight_offset)));
583 : }
584 :
585 : FE_LOGI("Name: %s, index: %zu, weightOffset: %lu.", op_desc_->GetName().c_str(), anchor_index, weight_offset);
586 : return SUCCESS;
587 : }
588 :
589 : Status TbeTaskBuilderAdapter::FeedInputAddrByAnchor(const ge::InDataAnchorPtr &anchor,
590 : InputIndexOffsetInfo &index_offset_info, bool is_gen_place_holder) {
591 : auto op_type = op_desc_->GetType();
592 : auto op_name = op_desc_->GetName();
593 : if (ge::AnchorUtils::GetStatus(anchor) == ge::ANCHOR_SUSPEND || anchor->GetPeerOutAnchor() == nullptr) {
594 : FE_LOGD("Node[type=%s,name=%s]:anchor %zu is suspend or peer anchor is null with gen flag:%d.", op_type.c_str(),
595 : op_name.c_str(), index_offset_info.anchor_index, is_gen_place_holder);
596 : if (is_gen_place_holder) {
597 : input_addrs_.push_back(reinterpret_cast<void *>(kTaskPlaceHolderAddr));
598 : }
599 : index_offset_info.anchor_index++;
600 : return SUCCESS;
601 : }
602 :
603 : if (ge::AnchorUtils::GetStatus(anchor) != ge::ANCHOR_DATA) {
604 : Status ret = HandleAnchorWeight(static_cast<size_t>(anchor->GetIdx()));
605 : if (ret != SUCCESS) {
606 : return ret;
607 : }
608 : index_offset_info.input_index++;
609 : index_offset_info.anchor_index++;
610 : return SUCCESS;
611 : }
612 :
613 : if (index_offset_info.input_index >= index_offset_info.input_offsets.size()) {
614 : REPORT_FE_ERROR(
615 : "[GenTask][InitInput] [Node %s type %s]: inputIndex must be less than size of offset, index[%zu], size[%zu].",
616 : op_name.c_str(), op_type.c_str(), index_offset_info.input_index, index_offset_info.input_offsets.size());
617 : return FAILED;
618 : }
619 :
620 : int64_t input_offset = index_offset_info.input_offsets[index_offset_info.input_index];
621 : FE_LOGD("Node[type=%s,name=%s]: input_index=%zu, input_offset=%ld.", op_type.c_str(), op_name.c_str(),
622 : index_offset_info.input_index, input_offset);
623 : bool is_addr_var = (index_offset_info.input_index < index_offset_info.input_is_addr_var.size()) &&
624 : (index_offset_info.input_is_addr_var[index_offset_info.input_index]);
625 : if (is_addr_var) {
626 : input_addrs_.push_back(reinterpret_cast<void *>(reinterpret_cast<intptr_t>(input_offset)));
627 : } else {
628 : SetInputAddrFromDataBase(index_offset_info.input_index, input_offset);
629 : }
630 :
631 : FE_LOGD("Node[type=%s,name=%s]: Init input.", op_type.c_str(), op_name.c_str());
632 : index_offset_info.input_index++;
633 : index_offset_info.anchor_index++;
634 : return SUCCESS;
635 : }
636 :
637 : Status TbeTaskBuilderAdapter::GetInputIndexOffsetInfos(InputIndexOffsetInfo &index_offset_info) {
638 : auto op_type = op_desc_->GetType();
639 : auto op_name = op_desc_->GetName();
640 : vector<bool> input_is_addr_var;
641 : (void)ge::AttrUtils::GetListBool(op_desc_, ATTR_NAME_INPUT_IS_VAR, input_is_addr_var);
642 :
643 : // if input_offsets is empty, set 0 to vector
644 : vector<int64_t> input_offsets = op_desc_->GetInputOffset();
645 : if (input_offsets.empty()) {
646 : vector<int64_t> input_offset_zero(op_desc_->GetInputsSize(), 0);
647 : input_offsets.swap(input_offset_zero);
648 : FE_LOGD("Node[type=%s,name=%s]: input_offset_size:%zu.", op_type.c_str(), op_name.c_str(), input_offsets.size());
649 : }
650 :
651 : index_offset_info.input_offsets = input_offsets;
652 : index_offset_info.input_is_addr_var = input_is_addr_var;
653 :
654 : vector<uint32_t> input_type_list;
655 : (void)ge::AttrUtils::GetListInt(op_desc_, kInputParaTypeList, input_type_list);
656 : if (input_type_list.empty()) {
657 : FE_LOGW("Node[type=%s,name=%s] get attr input param type list is null.", op_type.c_str(), op_name.c_str());
658 : return PARAM_INVALID;
659 : }
660 : index_offset_info.input_type_list = input_type_list;
661 : return SUCCESS;
662 : }
663 :
664 : Status TbeTaskBuilderAdapter::InitInputNoPlaceholder() {
665 : auto op_type = op_desc_->GetType();
666 : auto op_name = op_desc_->GetName();
667 :
668 : InputIndexOffsetInfo index_offset_info;
669 : if (GetInputIndexOffsetInfos(index_offset_info) == FAILED) {
670 : REPORT_FE_ERROR("[GenTask][InitInput]Node[type=%s,name=%s]: Init input failed.", op_type.c_str(), op_name.c_str());
671 : return FAILED;
672 : }
673 : size_t tmp_input_idx = 0;
674 : for (auto const &anchor : node_.GetAllInDataAnchors()) {
675 : tmp_input_idx = index_offset_info.input_index;
676 : if (FeedInputAddrByAnchor(anchor, index_offset_info) != SUCCESS) {
677 : return FAILED;
678 : }
679 : if (tmp_input_idx < index_offset_info.input_index) {
680 : FE_LOGD("Node[type=%s,name=%s]: Add argsInfo for req input.", op_type.c_str(), op_name.c_str());
681 : FeedArgsInfo(domi::ArgsInfo_ArgsType_INPUT, domi::ArgsInfo_ArgsFormat_DIRECT_ADDR, tmp_input_idx);
682 : }
683 : }
684 : FE_LOGD("Node[type=%s,name=%s]: Init input finished.", op_type.c_str(), op_name.c_str());
685 : return SUCCESS;
686 : }
687 :
688 : void TbeTaskBuilderAdapter::InsertMissOptAddr(size_t &arg_idx, std::vector<uint32_t> &insert_pos_vec) {
689 : for (auto insert_pos : insert_pos_vec) {
690 : if (arg_idx == insert_pos) {
691 : FE_LOGD("Insert optional input in pos %u.", insert_pos);
692 : input_addrs_.push_back(reinterpret_cast<void *>(kTaskPlaceHolderAddr));
693 : FeedArgsInfo(domi::ArgsInfo_ArgsType_INPUT, domi::ArgsInfo_ArgsFormat_DIRECT_ADDR, 0xFFFFFFFFU);
694 : arg_idx++;
695 : }
696 : }
697 : return;
698 : }
699 :
700 : Status TbeTaskBuilderAdapter::InitInputGenPlaceholder() {
701 : auto op_type = op_desc_->GetType();
702 : auto op_name = op_desc_->GetName();
703 :
704 : InputIndexOffsetInfo index_offset_info;
705 : if (GetInputIndexOffsetInfos(index_offset_info) == FAILED) {
706 : REPORT_FE_ERROR("[GenTask][InitInput]Node[type=%s,name=%s]: Init input failed.", op_type.c_str(), op_name.c_str());
707 : return FAILED;
708 : }
709 : std::vector<uint32_t> insert_pos_vec;
710 : (void)ge::AttrUtils::GetListInt(op_desc_, kInputInsertOptPosList, insert_pos_vec);
711 : size_t tmp_input_idx = 0;
712 : size_t tmp_anchor_idx = 0;
713 : size_t arg_idx = 0;
714 : for (auto const &anchor : node_.GetAllInDataAnchors()) {
715 : InsertMissOptAddr(arg_idx, insert_pos_vec);
716 : tmp_input_idx = index_offset_info.input_index;
717 : tmp_anchor_idx = index_offset_info.anchor_index;
718 : if (FeedInputAddrByAnchor(anchor, index_offset_info, true) != SUCCESS) {
719 : return FAILED;
720 : }
721 : if (tmp_input_idx < index_offset_info.input_index) {
722 : FE_LOGD("Node[type=%s,name=%s]: Add argsInfo for req input.", op_type.c_str(), op_name.c_str());
723 : FeedArgsInfo(domi::ArgsInfo_ArgsType_INPUT, domi::ArgsInfo_ArgsFormat_DIRECT_ADDR, tmp_input_idx);
724 : } else if (tmp_anchor_idx < index_offset_info.anchor_index && tmp_input_idx == index_offset_info.input_index) {
725 : FE_LOGD("Node[type=%s,name=%s]: Add argsInfo for null-opt-input.", op_type.c_str(), op_name.c_str());
726 : FeedArgsInfo(domi::ArgsInfo_ArgsType_INPUT, domi::ArgsInfo_ArgsFormat_DIRECT_ADDR, 0xFFFFFFFFU);
727 : }
728 : arg_idx++;
729 : }
730 : InsertMissOptAddr(arg_idx, insert_pos_vec);
731 : FE_LOGD("Node[type=%s,name=%s]: Init input with addrs size[%zu] finished.", op_type.c_str(), op_name.c_str(),
732 : input_addrs_.size());
733 : size_t ops_in_size = 0;
734 : (void)ge::AttrUtils::GetInt(op_desc_, kOpKernelAllInputSize, ops_in_size);
735 : size_t dyn_add_num = 0;
736 : (void)ge::AttrUtils::GetInt(op_desc_, kDyInputsAddNum, dyn_add_num);
737 : size_t all_kernel_size = ops_in_size + dyn_add_num;
738 : if (all_kernel_size != arg_idx) {
739 : REPORT_FE_ERROR("[GenTask][InitInput]Node[type=%s,name=%s]:Expect In addr size:%zu with real:%zu.", op_type.c_str(),
740 : op_name.c_str(), all_kernel_size, arg_idx);
741 : return FAILED;
742 : }
743 : return SUCCESS;
744 : }
745 :
746 : Status TbeTaskBuilderAdapter::InitInput() {
747 : const auto &op_type = op_desc_->GetType();
748 : const auto &op_name = op_desc_->GetName();
749 : std::string core_type;
750 : (void)ge::AttrUtils::GetStr(op_desc_, ATTR_NAME_CUBE_VECTOR_CORE_TYPE, core_type);
751 : bool need_sync =
752 : (core_type == kCoreTypeMixEnhance) && (PlatformUtils::Instance().GetFftsMode() == FFTS_MODE_FFTS_PLUS);
753 : need_sync |= op_desc_->HasAttr(ATTR_NAME_ALIAS_ENGINE_NAME);
754 : if (need_sync) {
755 : FE_LOGD("Node[type=%s,name=%s]:Mix add sync addr.", op_type.c_str(), op_name.c_str());
756 : input_addrs_.push_back(reinterpret_cast<void *>(0));
757 : FeedArgsInfo(domi::ArgsInfo_ArgsType_INPUT, domi::ArgsInfo_ArgsFormat_DIRECT_ADDR, 0xFFFFU);
758 : }
759 : std::string opt_mode;
760 : (void)ge::AttrUtils::GetStr(op_desc_, "optionalInputMode", opt_mode);
761 : Status ret;
762 : if (opt_mode == "gen_placeholder") {
763 : ret = InitInputGenPlaceholder();
764 : } else {
765 : ret = InitInputNoPlaceholder();
766 : }
767 :
768 : FE_LOGD("Node[type=%s,name=%s]: Init input finished.", op_type.c_str(), op_name.c_str());
769 : return ret;
770 : }
771 :
772 54 : bool TbeTaskBuilderAdapter::GetUnknownShapeFlag() const {
773 : bool is_support_unknown_shape = false;
774 : (void)ge::AttrUtils::GetBool(op_desc_, ATTR_NAME_SUPPORT_DYNAMIC_SHAPE, is_support_unknown_shape);
775 : bool is_unknown_shape = UnknownShapeUtils::IsUnknownShapeOp(*node_.GetOpDesc());
776 : FE_LOGD("Node[type=%s,name=%s]: is_unknown_shape flag is %ld.", op_desc_->GetTypePtr(), op_desc_->GetNamePtr(),
777 : is_unknown_shape);
778 : bool unknown_shape_flag = (is_support_unknown_shape && is_unknown_shape);
779 : return unknown_shape_flag;
780 : }
781 :
782 : void TbeTaskBuilderAdapter::AppendArgsTilingData(vector<void *> &device_addrs) {
783 : const auto &op_type = op_desc_->GetType();
784 : const auto &op_name = op_desc_->GetName();
785 :
786 : bool is_unknown_shape = GetUnknownShapeFlag();
787 : bool stc_dyn_soft_sync = false;
788 : (void)ge::AttrUtils::GetBool(op_desc_, kStaticToDynamicSoftSyncOp, stc_dyn_soft_sync);
789 : bool is_unknown_graph = FeGraphCommon::IsNodeOfUnknownRootGraph(node_);
790 : bool has_compile_info = op_desc_->HasAttr(COMPILE_INFO_JSON);
791 : bool is_static_reuse = !is_unknown_shape && !is_unknown_graph && has_compile_info;
792 : if (is_unknown_shape || stc_dyn_soft_sync || is_static_reuse) {
793 : FE_LOGD("Node[type=%s,name=%s] add args tiling data", op_type.c_str(), op_name.c_str());
794 : int64_t temp_op_para_size = 0;
795 : (void)ge::AttrUtils::GetInt(op_desc_, OP_PARA_SIZE, temp_op_para_size);
796 : if (temp_op_para_size > 0) {
797 : char tiling_data_ptr[RESERVED_TILING_DATA_SIZE] = {0x00};
798 : device_addrs.push_back(tiling_data_ptr);
799 : }
800 : }
801 : }
802 :
803 : void TbeTaskBuilderAdapter::AppendGlobalData(vector<void *> &device_addrs) {
804 : int64_t globalworkspace_size = 0;
805 : int64_t globalworkspace_type = 0;
806 : if (ge::AttrUtils::GetInt(op_desc_, kGlobalworkspaceSize, globalworkspace_size)) {
807 : (void)ge::AttrUtils::GetInt(op_desc_, kGlobalworkspaceType, globalworkspace_type);
808 : FE_LOGD("Get Globalworkspace size[%ld] and type[%ld] from node[%s, %s].", globalworkspace_size,
809 : globalworkspace_type, op_desc_->GetName().c_str(), op_desc_->GetType().c_str());
810 : if (globalworkspace_size > 0) {
811 : uint8_t *globalworkspace_data_ptr = nullptr;
812 : device_addrs.push_back(static_cast<void *>(globalworkspace_data_ptr));
813 : }
814 : }
815 : }
816 :
817 : Status TbeTaskBuilderAdapter::Run(domi::TaskDef &task_def) {
818 : const auto &op_type = op_desc_->GetType();
819 : const auto &op_name = op_desc_->GetName();
820 : FE_LOGD("Node[%s, %s]: start to run, input addrs size[%zu], output addrs size[%zu], input l1 addrs size[%zu].",
821 : op_type.c_str(), op_name.c_str(), input_addrs_.size(), output_addrs_.size(), input_l1_addrs_.size());
822 :
823 : vector<void *> device_addrs;
824 : device_addrs.insert(device_addrs.cend(), input_addrs_.cbegin(), input_addrs_.cend());
825 : device_addrs.insert(device_addrs.cend(), output_addrs_.cbegin(), output_addrs_.cend());
826 : device_addrs.insert(device_addrs.cend(), workspace_addrs_.cbegin(), workspace_addrs_.cend());
827 : device_addrs.insert(device_addrs.cend(), input_l1_addrs_.cbegin(), input_l1_addrs_.cend());
828 :
829 : AppendArgsTilingData(device_addrs);
830 : AppendGlobalData(device_addrs);
831 :
832 : size_t input_num = ge::OpDescUtils::GetNonConstInputsSize(node_) + ge::OpDescUtils::GetWeights(node_).size();
833 : size_t output_num = output_addrs_.size();
834 : FE_LOGD("Node[%s, %s]: input_num=%zu, output_num=%zu, workspace_addrs_size=%zu, device_addrs_size=%zu.",
835 : op_type.c_str(), op_name.c_str(), input_num, output_num, workspace_addrs_.size(), device_addrs.size());
836 :
837 : Status ret =
838 : TbeForward(block_dim_, device_addrs.data(), sizeof(void *) * device_addrs.size(), static_cast<int32_t>(input_num),
839 : const_cast<const void **>(input_addrs_.data()), static_cast<int32_t>(input_addrs_.size()),
840 : static_cast<int32_t>(output_num), const_cast<const void **>(output_addrs_.data()),
841 : static_cast<int32_t>(workspace_addrs_.size()), task_def);
842 : if (ret != SUCCESS) {
843 : REPORT_FE_ERROR("[GenTask][Run][Node %s type %s] TbeForward failed, ret[0x%X]", op_name.c_str(), op_type.c_str(),
844 : ret);
845 : return FAILED;
846 : }
847 : if (task_def.type() == ACL_RT_MODEL_TASK_KERNEL) {
848 : domi::KernelDef *kernel_def = task_def.mutable_kernel();
849 : FE_CHECK_NOTNULL(kernel_def);
850 : FE_LOGD("Node[%s, %s]:Task type[%u] append kernel.", op_type.c_str(), op_name.c_str(), task_def.type());
851 : for (auto &arg : kernel_args_info_) {
852 : domi::ArgsInfo *arg_info = kernel_def->add_args_info();
853 : *arg_info = arg;
854 : }
855 : } else {
856 : domi::KernelDefWithHandle *kernel_def_with_handle = task_def.mutable_kernel_with_handle();
857 : FE_CHECK_NOTNULL(kernel_def_with_handle);
858 : FE_LOGD("Node[%s, %s]:Task type[%u] append kernel with handle.", op_type.c_str(), op_name.c_str(), task_def.type());
859 : for (auto &arg : kernel_args_info_) {
860 : domi::ArgsInfo *arg_info = kernel_def_with_handle->add_args_info();
861 : *arg_info = arg;
862 : }
863 : }
864 : FE_LOGD("Node[%s, %s]: end to run.", op_type.c_str(), op_name.c_str());
865 : return SUCCESS;
866 : }
867 : } // namespace fe
|