Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "ccu_launch.h"
12 :
13 : #include <algorithm>
14 : #include <iterator>
15 : #include <memory>
16 : #include <utility>
17 : #include <vector>
18 :
19 : #include "adapter_rts_common.h"
20 : #include "ccu_res.h"
21 :
22 : #include "ccu_log.h"
23 :
24 : #include "hcom_common.h"
25 :
26 : #include "ccu_kernel_mgr.h"
27 : #include "ccu_instance_mgr.h"
28 : #include "ccu_var_event_res_mgr.h"
29 :
30 : #include "thread.h"
31 :
32 : #include "env_config/env_config_v2.h" // 暂时引用orion的环境变量处理模块
33 :
34 : #include "hcomm_adapter_rts.h"
35 :
36 : #include "task_param.h"
37 :
38 : #include "ccu_assist_v1.h"
39 :
40 : #include "unified_platform/pub_inc/config_plf_log.h"
41 : using Hccl::PLF_TASK;
42 :
43 55 : CcuResult HcommCcuKernelRegisterStart(CcuInsHandle insHandle)
44 : {
45 55 : const uint32_t devLogicId = HcclGetThreadDeviceId();
46 55 : auto* ccuIns = hcomm::CcuInstanceMgr::GetInstance(devLogicId).Get(insHandle);
47 55 : CCU_CHK_PTR_NULL(ccuIns);
48 :
49 55 : CCU_CHK_RET(ccuIns->BeginRegister());
50 :
51 55 : CcuResult ret = ccuIns->Reset();
52 55 : if (ret != CcuResult::CCU_SUCCESS) {
53 0 : (void)ccuIns->EndRegister();
54 0 : HCCL_ERROR("[%s] failed, Reset failed[%d], rollback register state.", __func__, ret);
55 0 : return ret;
56 : }
57 55 : return CcuResult::CCU_SUCCESS;
58 : }
59 :
60 55 : static CcuResult CcuKernelTryRegister(
61 : hcomm::CcuInstance* ccuIns, hcomm::CcuResPack* resPack, uint32_t devLogicId, uint32_t dieId,
62 : const char* kernelFuncName, const void* kernelFunc, const void** kernelArgs, uint32_t argNum,
63 : CcuKernelHandle& newHandle)
64 : {
65 : CCU_EXCEPTION_HANDLE_BEGIN
66 55 : auto& kernelMgr = hcomm::CcuKernelMgr::GetInstance(devLogicId);
67 55 : CCU_CHK_RET(kernelMgr.Register(*resPack, dieId, kernelFuncName, kernelFunc, kernelArgs, argNum, newHandle));
68 44 : CCU_CHK_RET(ccuIns->SaveKernel(newHandle));
69 8 : CCU_EXCEPTION_HANDLE_END
70 44 : return CcuResult::CCU_SUCCESS;
71 : }
72 :
73 55 : CcuResult HcommCcuKernelRegister(
74 : CcuInsHandle insHandle, uint32_t dieId, const char* kernelFuncName, const void* kernelFunc, const void** kernelArgs,
75 : uint32_t argNum, CcuKernelHandle* kernelHandle)
76 : {
77 55 : HCCL_RUN_INFO("Entry-%s", __func__);
78 55 : HcclUs startut = TIME_NOW();
79 :
80 55 : CCU_CHK_PTR_NULL(kernelFunc);
81 55 : CCU_CHK_PTR_NULL(kernelHandle);
82 :
83 55 : if (argNum != 0) {
84 55 : CCU_CHK_PTR_NULL(kernelArgs);
85 : }
86 :
87 55 : const uint32_t devLogicId = HcclGetThreadDeviceId();
88 55 : auto* ccuIns = hcomm::CcuInstanceMgr::GetInstance(devLogicId).Get(insHandle);
89 55 : CCU_CHK_PTR_NULL(ccuIns);
90 :
91 55 : CCU_CHK_RET(ccuIns->CheckRegistering());
92 :
93 55 : auto* resPack = ccuIns->GetResPack();
94 55 : CCU_CHK_PTR_NULL(resPack);
95 :
96 55 : CcuKernelHandle newHandle{0};
97 55 : CcuResult ret = CcuKernelTryRegister(
98 : ccuIns, resPack, devLogicId, dieId, kernelFuncName, kernelFunc, kernelArgs, argNum, newHandle);
99 55 : if (ret != CcuResult::CCU_SUCCESS) {
100 11 : ccuIns->AbortRegister();
101 11 : if (CCU_CHK_RES_UNAVAIL(ret)) {
102 0 : HCCL_WARNING(
103 : "[%s] register kernel resource unavailable[%d], current register round aborted.", __func__, ret);
104 0 : return CcuResult::CCU_E_UNAVAIL;
105 : } else {
106 11 : HCCL_ERROR("[%s] failed, register kernel failed[%d], current register round aborted.", __func__, ret);
107 11 : return ret;
108 : }
109 : }
110 :
111 44 : *kernelHandle = newHandle;
112 44 : HCCL_INFO("[%s] success, take time [%lld]us.", __func__, DURATION_US(TIME_NOW() - startut).count());
113 44 : return CcuResult::CCU_SUCCESS;
114 : }
115 :
116 45 : CcuResult HcommCcuKernelRegisterEnd(CcuInsHandle insHandle)
117 : {
118 45 : const uint32_t devLogicId = HcclGetThreadDeviceId();
119 45 : auto* ccuIns = hcomm::CcuInstanceMgr::GetInstance(devLogicId).Get(insHandle);
120 45 : CCU_CHK_PTR_NULL(ccuIns);
121 :
122 45 : CCU_CHK_RET(ccuIns->EndRegister());
123 45 : const auto& newKernels = ccuIns->GetUntranslatedKernels();
124 :
125 45 : auto& kernelMgr = hcomm::CcuKernelMgr::GetInstance(devLogicId);
126 : // 当前翻译内部流程可能抛异常
127 : CCU_EXCEPTION_HANDLE_BEGIN
128 45 : CCU_CHK_RET(kernelMgr.Translate(newKernels));
129 0 : CCU_EXCEPTION_HANDLE_END
130 :
131 44 : return CcuResult::CCU_SUCCESS;
132 : }
133 :
134 5 : CcuResult HcommCcuGetTaskArgsNum(CcuKernelHandle kernelHandle, uint32_t* taskArgsNum)
135 : {
136 5 : HCCL_INFO("Entry-%s", __func__);
137 5 : CCU_CHK_PTR_NULL(taskArgsNum);
138 :
139 4 : const uint32_t devLogicId = HcclGetThreadDeviceId();
140 4 : auto& kernelMgr = hcomm::CcuKernelMgr::GetInstance(devLogicId);
141 :
142 4 : hcomm::CcuKernelInfo info{};
143 4 : CCU_CHK_RET(kernelMgr.GetCcuKernelInfo(kernelHandle, info));
144 :
145 3 : *taskArgsNum = info.maxTaskArgsNum;
146 3 : HCCL_INFO("[%s] success, kernelHandle[%llx], taskArgsNum[%u].", __func__, kernelHandle, *taskArgsNum);
147 3 : return CcuResult::CCU_SUCCESS;
148 : }
149 :
150 : static std::shared_ptr<std::vector<Hccl::CcuProfilingInfo>>
151 5 : ConstructCcuDetailInfo(const std::vector<hcomm::CcuProfilingInfo>& allCcuProfilingInfo, bool isSaveProfilingData)
152 : {
153 5 : if (allCcuProfilingInfo.empty() || !isSaveProfilingData) {
154 5 : return nullptr;
155 : }
156 :
157 0 : std::vector<Hccl::CcuProfilingInfo> converted(allCcuProfilingInfo.size());
158 0 : for (u32 idx = 0; idx < allCcuProfilingInfo.size(); ++idx) {
159 0 : auto& src = allCcuProfilingInfo[idx];
160 0 : auto& dst = converted[idx];
161 0 : dst.name = src.name;
162 0 : dst.type = src.type;
163 0 : dst.dieId = src.dieId;
164 0 : dst.missionId = src.missionId;
165 0 : dst.instrId = src.instrId;
166 0 : dst.reduceOpType = src.reduceOpType;
167 0 : dst.inputDataType = src.inputDataType;
168 0 : dst.outputDataType = src.outputDataType;
169 0 : dst.dataSize = src.dataSize;
170 0 : dst.ckeId = src.ckeId;
171 0 : dst.mask = src.mask;
172 0 : (void)memcpy_s(dst.channelId, sizeof(dst.channelId), src.channelId, sizeof(src.channelId));
173 0 : (void)memcpy_s(dst.channelHandle, sizeof(dst.channelHandle), src.channelHandle, sizeof(src.channelHandle));
174 : }
175 0 : return std::make_shared<std::vector<Hccl::CcuProfilingInfo>>(std::move(converted));
176 0 : }
177 :
178 8 : static Hccl::TaskParam ConstructCcuTaskParam(
179 : const hcomm::CcuTaskParam& ccuParam, const CcuKernelHandle kernelHandle,
180 : const std::shared_ptr<std::vector<Hccl::CcuProfilingInfo>>& ccuDetailInfo, u64 beginTime, u64 endTime,
181 : bool isMaster)
182 : {
183 8 : Hccl::TaskParam taskParam{};
184 8 : taskParam.beginTime = beginTime;
185 8 : taskParam.endTime = endTime;
186 8 : taskParam.taskType = Hccl::TaskParamType::TASK_CCU;
187 8 : taskParam.taskPara.Ccu.dieId = ccuParam.dieId;
188 8 : taskParam.taskPara.Ccu.missionId = ccuParam.missionId;
189 8 : taskParam.taskPara.Ccu.execMissionId = ccuParam.missionId;
190 8 : taskParam.taskPara.Ccu.instrId = ccuParam.instStartId;
191 8 : taskParam.taskPara.Ccu.executeId = kernelHandle;
192 8 : taskParam.taskPara.Ccu.ccuKernelHandle = kernelHandle;
193 8 : taskParam.isMaster = isMaster;
194 8 : taskParam.ccuDetailInfo = ccuDetailInfo;
195 8 : return taskParam;
196 : }
197 :
198 5 : static void LogCcuTaskInfo(const std::vector<hcomm::CcuTaskParam>& ccuParams, const CcuKernelHandle kernelHandle)
199 : {
200 5 : if (!HcclCheckLogLevel(HCCL_LOG_INFO)) {
201 0 : return;
202 : }
203 5 : const uint32_t execTimeOutSec = Hccl::EnvConfig::GetInstance().GetRtsConfig().GetExecTimeOut();
204 13 : for (u32 idx = 0; idx < ccuParams.size(); idx++) {
205 8 : const auto& param = ccuParams[idx];
206 8 : PLF_CONFIG_INFO(
207 : PLF_TASK,
208 : "[%s] start ccu task, dieId[%u], missionId[%u], execMissionId[%u], instStartId[%u], instCnt[%u], "
209 : "argSize[%u], timeout[%u]s, executeId[0x%llx], ccuKernelHandle[0x%llx]",
210 : __func__, param.dieId, param.missionId, param.missionId, param.instStartId, param.instCnt, param.argSize,
211 : execTimeOutSec, kernelHandle, kernelHandle);
212 : }
213 : }
214 :
215 0 : static void ConstructProfilingInfoLog(const std::vector<hcomm::CcuProfilingInfo>& allCcuProfilingInfo)
216 : {
217 0 : if (!HcclCheckLogLevel(HCCL_LOG_INFO)) {
218 0 : return;
219 : }
220 0 : for (const hcomm::CcuProfilingInfo& profInfo : allCcuProfilingInfo) {
221 0 : for (int idx = 0; idx < hcomm::CCU_MAX_CHANNEL_NUM; idx++) {
222 0 : if (profInfo.channelId[idx] == hcomm::INVALID_VALUE_CHANNELID) {
223 0 : break;
224 : }
225 0 : HCCL_INFO(
226 : "[%s]idx[%d]: channelId[%u], channelHandle[0x%llx]", __func__, idx, profInfo.channelId[idx],
227 : profInfo.channelHandle[idx]);
228 : }
229 : }
230 : }
231 :
232 5 : static CcuResult ConstructProfilingInfo(
233 : hcomm::CcuKernel* kernel, const uint64_t* taskArgs, uint32_t argNum,
234 : std::vector<hcomm::CcuProfilingInfo>& allCcuProfilingInfo, bool isSaveProfilingData)
235 : {
236 5 : if (!isSaveProfilingData) {
237 5 : return CcuResult::CCU_SUCCESS;
238 : }
239 :
240 0 : CCU_CHK_RET(kernel->GetCcuProfilingInfo(taskArgs, argNum, allCcuProfilingInfo));
241 0 : if (allCcuProfilingInfo.empty()) {
242 0 : return CcuResult::CCU_SUCCESS;
243 : }
244 0 : ConstructProfilingInfoLog(allCcuProfilingInfo);
245 0 : return CcuResult::CCU_SUCCESS;
246 : }
247 :
248 8 : static CcuResult ReportCcuTaskDfx(const ThreadHandle threadHandle, const Hccl::TaskParam& taskParam)
249 : {
250 8 : auto* rtsThread = reinterpret_cast<hccl::Thread*>(threadHandle);
251 8 : CCU_CHK_PTR_NULL(rtsThread);
252 :
253 8 : auto callback = rtsThread->GetCallback();
254 8 : if (!callback) {
255 8 : HCCL_WARNING("[%s] task info callback is not registered on thread, skip ccu profiling report.", __func__);
256 8 : return CcuResult::CCU_SUCCESS;
257 : }
258 0 : u32 streamId = INVALID_UINT;
259 0 : u32 taskId = INVALID_UINT;
260 0 : CCU_CHK_RET(hrtGetTaskIdAndStreamID(taskId, streamId));
261 0 : CCU_CHK_RET(callback(streamId, taskId, taskParam, DFX_INVALID_U64));
262 0 : return CcuResult::CCU_SUCCESS;
263 8 : }
264 :
265 8 : static HcclResult LaunchCcuTasks(const hcomm::CcuTaskParam& param, const aclrtStream stream)
266 : {
267 8 : const uint32_t execTimeOutSec = Hccl::EnvConfig::GetInstance().GetRtsConfig().GetExecTimeOut();
268 8 : rtCcuTaskInfo_t taskInfo{};
269 8 : taskInfo.dieId = param.dieId;
270 8 : taskInfo.missionId = param.missionId;
271 8 : taskInfo.instStartId = param.instStartId;
272 8 : taskInfo.instCnt = param.instCnt;
273 8 : taskInfo.key = param.key;
274 8 : taskInfo.argSize = param.argSize;
275 8 : taskInfo.timeout = execTimeOutSec;
276 24 : std::copy(std::begin(param.args), std::end(param.args), std::begin(taskInfo.args));
277 :
278 8 : auto ret = rtCCULaunch(&taskInfo, stream);
279 8 : if (ret != RT_ERROR_NONE) {
280 0 : HCCL_ERROR("[%s] failed to launch ccu, ret[%d]", __func__, ret);
281 0 : return HcclResult::HCCL_E_RUNTIME;
282 : }
283 :
284 8 : return HcclResult::HCCL_SUCCESS;
285 : }
286 :
287 : CcuResult
288 5 : HcommCcuKernelLaunch(ThreadHandle threadHandle, CcuKernelHandle kernelHandle, const void* taskArgs, uint32_t argNum)
289 : {
290 5 : const auto& startus = TIME_NOW();
291 :
292 5 : CHK_PRT_RET(threadHandle == 0, HCCL_ERROR("[%s] failed, thread handle is empty.", __func__), CcuResult::CCU_E_PARA);
293 5 : CHK_PRT_RET(kernelHandle == 0, HCCL_ERROR("[%s] failed, kernel handle is empty.", __func__), CcuResult::CCU_E_PARA);
294 5 : CHK_PRT_RET(
295 : argNum > 0 && taskArgs == nullptr,
296 : HCCL_ERROR("[%s] failed, taskArgs is nullptr while argNum[%u] > 0.", __func__, argNum), CcuResult::CCU_E_PTR);
297 :
298 5 : PLF_CONFIG_INFO(
299 : PLF_TASK, "[HcommCcuKernelLaunch] threadHandle[0x%llx] kernelHandle[0x%llx].", threadHandle, kernelHandle);
300 :
301 5 : const auto* rtsThread = reinterpret_cast<hccl::Thread*>(threadHandle);
302 5 : const auto* threadStream = rtsThread->GetStream();
303 5 : CCU_CHK_PTR_NULL(threadStream);
304 5 : auto* streamPtr = threadStream->ptr();
305 5 : CCU_CHK_PTR_NULL(streamPtr);
306 :
307 5 : const uint32_t devLogicId = HcclGetThreadDeviceId();
308 5 : auto& kernelMgr = hcomm::CcuKernelMgr::GetInstance(devLogicId);
309 5 : auto* kernel = kernelMgr.GetKernel(kernelHandle);
310 5 : CCU_CHK_PTR_NULL(kernel);
311 :
312 : CCU_EXCEPTION_HANDLE_BEGIN
313 5 : std::vector<hcomm::CcuTaskParam> taskParams{};
314 5 : auto ret = kernel->GeneTaskParams(static_cast<const uint64_t*>(taskArgs), argNum, taskParams);
315 5 : CHK_PRT_RET(
316 : ret != CcuResult::CCU_SUCCESS,
317 : HCCL_ERROR("[%s] failed, threadHandle[0x%llx] kernelHandle[0x%llx].", __func__, threadHandle, kernelHandle),
318 : ret);
319 :
320 5 : if (taskParams.empty()) {
321 0 : HCCL_INFO("[%s] passed, ccu params are empty.", __func__);
322 0 : return CcuResult::CCU_SUCCESS;
323 : }
324 5 : bool isProfilingEnabledL1 = Hccl::ProfilingHandler::GetInstance().GetHcclL1State();
325 5 : bool isProfilingEnabledL0 = Hccl::ProfilingHandler::GetInstance().GetHcclL0State();
326 5 : bool isCached = Hccl::ProfilingHandler::GetInstance().GetCachedFlag();
327 5 : bool isSaveProfilingData = isProfilingEnabledL1 || isProfilingEnabledL0 || isCached;
328 :
329 5 : std::vector<hcomm::CcuProfilingInfo> allCcuProfilingInfo;
330 5 : CCU_CHK_RET(ConstructProfilingInfo(
331 : kernel, static_cast<const uint64_t*>(taskArgs), argNum, allCcuProfilingInfo, isSaveProfilingData));
332 5 : LogCcuTaskInfo(taskParams, kernelHandle);
333 5 : auto ccuDetailInfo = ConstructCcuDetailInfo(allCcuProfilingInfo, isSaveProfilingData);
334 13 : for (u32 idx = 0; idx < taskParams.size(); idx++) {
335 8 : u64 beginTime = Hccl::DlProfFunction::GetInstance().dlMsprofSysCycleTime();
336 8 : CCU_CHK_RET(LaunchCcuTasks(taskParams[idx], streamPtr));
337 8 : u64 endTime = Hccl::DlProfFunction::GetInstance().dlMsprofSysCycleTime();
338 : Hccl::TaskParam taskParam = ConstructCcuTaskParam(
339 8 : taskParams[idx], kernelHandle, ccuDetailInfo, beginTime, endTime, rtsThread->GetMaster());
340 8 : CCU_CHK_RET(ReportCcuTaskDfx(threadHandle, taskParam));
341 8 : }
342 5 : CCU_EXCEPTION_HANDLE_END
343 5 : HCCL_INFO("[%s] success, take time [%lld]us.", __func__, DURATION_US(TIME_NOW() - startus).count());
344 5 : return CcuResult::CCU_SUCCESS;
345 : }
346 :
347 : // 定位实例资源仓后转发给 CcuVarEventResMgr;预约、地址映射与失败回滚均由该类内部完成
348 14 : static CcuResult CcuAllocVarEventRes(
349 : CcuInsHandle insHandle, hcomm::CcuVarEventType type, uint8_t dieId, uint32_t num, uint64_t& outHandle)
350 : {
351 14 : const uint32_t devLogicId = HcclGetThreadDeviceId();
352 14 : auto* ccuIns = hcomm::CcuInstanceMgr::GetInstance(devLogicId).Get(insHandle);
353 14 : CCU_CHK_PTR_NULL(ccuIns);
354 :
355 14 : auto* resPack = ccuIns->GetResPack();
356 14 : CCU_CHK_PTR_NULL(resPack);
357 :
358 14 : CCU_CHK_RET(hcomm::CcuVarEventResMgr::GetInstance(devLogicId)
359 : .Acquire(insHandle, resPack->GetCcuResRepo(), type, dieId, num, outHandle));
360 7 : return CcuResult::CCU_SUCCESS;
361 : }
362 :
363 9 : CcuResult HcommCcuVariableAlloc(CcuInsHandle insHandle, uint8_t dieId, uint32_t num, CcuVariableHandle* varHandle)
364 : {
365 9 : CCU_CHK_PTR_NULL(varHandle);
366 8 : *varHandle = 0;
367 8 : CCU_CHK_RET(CcuAllocVarEventRes(insHandle, hcomm::CcuVarEventType::VARIABLE, dieId, num, *varHandle));
368 4 : return CcuResult::CCU_SUCCESS;
369 : }
370 :
371 7 : CcuResult HcommCcuEventAlloc(CcuInsHandle insHandle, uint8_t dieId, uint32_t num, CcuEventHandle* eventHandle)
372 : {
373 7 : CCU_CHK_PTR_NULL(eventHandle);
374 6 : *eventHandle = 0;
375 6 : CCU_CHK_RET(CcuAllocVarEventRes(insHandle, hcomm::CcuVarEventType::EVENT, dieId, num, *eventHandle));
376 3 : return CcuResult::CCU_SUCCESS;
377 : }
378 :
379 21 : CcuResult HcommCcuVariableGetAddr(CcuVariableHandle varHandle, uint32_t index, uint64_t* va)
380 : {
381 21 : CCU_CHK_PTR_NULL(va);
382 :
383 20 : const uint32_t devLogicId = HcclGetThreadDeviceId();
384 20 : CCU_CHK_RET(hcomm::CcuVarEventResMgr::GetInstance(devLogicId)
385 : .GetSavedAddr(hcomm::CcuVarEventType::VARIABLE, varHandle, index, *va));
386 :
387 18 : return CcuResult::CCU_SUCCESS;
388 : }
389 :
390 11 : CcuResult HcommCcuEventGetAddr(CcuEventHandle eventHandle, uint32_t index, uint64_t* va)
391 : {
392 11 : CCU_CHK_PTR_NULL(va);
393 :
394 11 : const uint32_t devLogicId = HcclGetThreadDeviceId();
395 11 : CCU_CHK_RET(hcomm::CcuVarEventResMgr::GetInstance(devLogicId)
396 : .GetSavedAddr(hcomm::CcuVarEventType::EVENT, eventHandle, index, *va));
397 :
398 8 : return CcuResult::CCU_SUCCESS;
399 : }
400 :
401 0 : CcuResult HcommCcuGetMemToken(uint64_t srcVa, uint64_t size, uint64_t* tokenInfo)
402 : {
403 0 : CCU_CHK_PTR_NULL(tokenInfo);
404 :
405 0 : if (srcVa == 0 || size == 0) {
406 0 : HCCL_ERROR(
407 : "[%s] failed, srcVa[0x%llx] size[%llu] should not be 0.", __func__, static_cast<unsigned long long>(srcVa),
408 : static_cast<unsigned long long>(size));
409 0 : return CcuResult::CCU_E_PARA;
410 : }
411 : // 注意token信息属于安全信息,均不允许打印
412 0 : hcomm::rtMemUbTokenInfo info{};
413 0 : info.va = srcVa;
414 0 : info.size = size;
415 0 : CCU_CHK_RET(hcomm::RtsUbDevQueryInfo(QUERY_PROCESS_TOKEN, info));
416 0 : *tokenInfo = hcomm::CcuRep::CcuCombineTokenInfo(info.tokenId, info.tokenValue, 1);
417 :
418 0 : return CcuResult::CCU_SUCCESS;
419 : }
|