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 "coll_reduce_scatter_mesh_aiv_for_910_93_executor.h"
12 : #include <algorithm>
13 :
14 : namespace hccl {
15 :
16 0 : CollReduceScatterMeshAivFor91093Executor::CollReduceScatterMeshAivFor91093Executor(const HcclDispatcher dispatcher,
17 0 : std::unique_ptr<TopoMatcher> &topoMatcher):
18 0 : CollReduceScatterExecutor(dispatcher, topoMatcher)
19 : {
20 0 : DMAReduceFlag_ = false;
21 0 : desc_.isAivMode = true;
22 0 : desc_.isAivCrossNode = true;
23 0 : desc_.deterministic = 1;
24 0 : }
25 :
26 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CalcStreamNum(u32& streamNum)
27 : {
28 0 : streamNum = 0; // AIV通信不需要申请从流
29 0 : HCCL_INFO("[CollReduceScatterMeshAivFor91093Executor][CalcStreamNum] tag[%s] streamNum[%u].",
30 : tag_.c_str(), streamNum);
31 0 : return HCCL_SUCCESS;
32 : }
33 :
34 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
35 : {
36 0 : TransportMemType inputType = TransportMemType::RESERVED;
37 0 : TransportMemType outputType = TransportMemType::RESERVED;
38 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
39 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
40 0 : return HCCL_SUCCESS;
41 : }
42 :
43 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CalcTransportMemType(TransportMemType &inputType,
44 : TransportMemType &outputType)
45 : {
46 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
47 0 : if (topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE) {
48 : // 图模式确定性
49 0 : inputType = TransportMemType::SCRATCH;
50 : } else {
51 0 : inputType = TransportMemType::PARAM_INPUT;
52 : }
53 : } else {
54 0 : inputType = TransportMemType::CCL_INPUT;
55 : }
56 :
57 0 : outputType = TransportMemType::AIV_OUTPUT;
58 0 : HCCL_INFO("[CollReduceScatterMeshAivFor91093Executor][CalcTransportMemType] tag[%s] inputType[%d],"
59 : " outputType[%d].", tag_.c_str(), inputType, outputType);
60 0 : return HCCL_SUCCESS;
61 : }
62 :
63 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CalcLevel0CommInfo(TransportMemType inputType,
64 : TransportMemType outputType,
65 : std::vector<LevelNSubCommTransport>& opTransport)
66 : {
67 0 : CommParaInfo commCombinePara(COMM_COMBINE_ORDER, CommType::COMM_TAG_MESH);
68 0 : commCombinePara.meshSinglePlane = true;
69 0 : CHK_RET(CalcCommPlaneInfo(tag_, commCombinePara, opTransport[COMM_COMBINE_ORDER], inputType, outputType));
70 :
71 0 : LevelNSubCommTransport &commTransportLevel0 = opTransport[COMM_COMBINE_ORDER];
72 0 : for (u32 subCommIndex = 0; subCommIndex < commTransportLevel0.size(); subCommIndex++) {
73 0 : for (auto &transportRequest : commTransportLevel0[subCommIndex].transportRequests) {
74 0 : transportRequest.isUsedRdma = false;
75 : }
76 : }
77 0 : return HCCL_SUCCESS;
78 0 : }
79 :
80 0 : void CollReduceScatterMeshAivFor91093Executor::ParseParam(const OpParam& param)
81 : {
82 0 : tag_ = param.tag;
83 :
84 0 : u64 inputSize = param.DataDes.count * SIZE_TABLE[param.DataDes.dataType];
85 :
86 0 : totalSize_ = BUFFER_DIVIDE * inputSize * topoAttr_.userRankSize;
87 0 : }
88 :
89 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CalcScratchMemSize(u64& scratchMemSize)
90 : {
91 0 : scratchMemSize = 0;
92 : // 确定性时图模式需要计算scratchMem
93 0 : if (topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
94 0 : scratchMemSize = totalSize_;
95 : }
96 0 : HCCL_INFO("[%s]tag[%s] scratchMemSize[%llu]", __func__, tag_.c_str(), scratchMemSize);
97 0 : return HCCL_SUCCESS;
98 : }
99 :
100 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CalNumBlocksDeter(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType) const
101 : {
102 : (void) dataSize;
103 : (void) cmdType;
104 : // Step1. Calculate the best block dimension
105 0 : u32 bestNumBlocks = (rankSize < MAX_NUM_BLOCKS ? rankSize : MAX_NUM_BLOCKS);
106 0 : u32 minNumBlocks = std::max((rankSize + MAX_TARGET_NUM - 1) / MAX_TARGET_NUM, NUM_BLOCKS_FACTOR_TWO);
107 :
108 : // Step2. Compare User Given numBlocks_ with bestNumBlocks
109 0 : numBlocks = bestNumBlocks;
110 0 : if (numBlocks_ < numBlocks) {
111 0 : numBlocks = numBlocks_ / NUM_BLOCKS_FACTOR_TWO * NUM_BLOCKS_FACTOR_TWO;
112 0 : minNumBlocks = (minNumBlocks + NUM_BLOCKS_FACTOR_TWO - 1) / NUM_BLOCKS_FACTOR_TWO * NUM_BLOCKS_FACTOR_TWO;
113 : }
114 :
115 0 : CHK_PRT_RET(numBlocks < minNumBlocks,
116 : HCCL_ERROR("[CollReduceScatterMeshAivFor91093Executor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
117 : numBlocks_, minNumBlocks),
118 : HCCL_E_PARA);
119 :
120 0 : HCCL_INFO("[CollReduceScatterMeshAivFor91093Executor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
121 : numBlocks, numBlocks_, bestNumBlocks);
122 0 : return HCCL_SUCCESS;
123 : }
124 :
125 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType)
126 : {
127 0 : if(topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE){
128 0 : return CalNumBlocksDeter(numBlocks, rankSize, dataSize, cmdType);
129 : }
130 :
131 0 : numBlocks = rankSize; // 默认情况使用rankSize个AIV
132 :
133 0 : u32 minNumBlocks = (rankSize + MAX_TARGET_NUM - 1) / MAX_TARGET_NUM;
134 :
135 : // 多核并行优化
136 0 : if (rankSize > HALF_MAX_NUM_BLOCKS || dataSize < AIV_A3_CROSSNODE_TINY_SIZE) {
137 0 : numBlocks = rankSize;
138 0 : } else if (rankSize > ONE_THIRD_MAX_NUM_BLOCKS || dataSize < AIV_A3_CROSSNODE_SMALL_SIZE) {
139 0 : numBlocks = rankSize * NUM_BLOCKS_FACTOR_TWO;
140 0 : } else if (rankSize > ONE_FOURTH_MAX_NUM_BLOCKS) {
141 0 : numBlocks = rankSize * NUM_BLOCKS_FACTOR_THREE;
142 : } else {
143 0 : numBlocks = rankSize * NUM_BLOCKS_FACTOR_FOUR;
144 : }
145 :
146 0 : u32 bestNumBlocks = numBlocks;
147 0 : numBlocks = numBlocks_ < rankSize ? numBlocks_ : (numBlocks_ < numBlocks ? numBlocks_ / rankSize * rankSize : numBlocks);
148 :
149 0 : CHK_PRT_RET(numBlocks < minNumBlocks,
150 : HCCL_ERROR("[CollReduceScatterMeshAivFor91093Executor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
151 : numBlocks_, minNumBlocks),
152 : HCCL_E_PARA);
153 :
154 0 : HCCL_INFO("[CollReduceScatterMeshAivFor91093Executor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
155 : numBlocks, numBlocks_, bestNumBlocks);
156 0 : return HCCL_SUCCESS;
157 : }
158 :
159 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::CopyAivCommInfoToDevice(const CommPlane levelIndex, const u32 subLevelIndex,
160 : AlgResourceResponse& algResource)
161 : {
162 0 : algResResp_ = &algResource;
163 0 : CHK_RET(CheckCommSize(levelIndex, subLevelIndex + 1));
164 0 : SubCommInfo commInfo = GetSubCommInfo(levelIndex, subLevelIndex);
165 0 : u32 localRank = commInfo.localRank;
166 0 : u32 localRankSize = commInfo.localRankSize;
167 :
168 0 : void* buffersInOut[MAX_RANK_SIZE_A3 * 2] = {};
169 0 : bool isOpbaseMode = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
170 :
171 0 : for (u32 i = 0; i < localRankSize; i++) {
172 0 : u32 idx = (i << 1);
173 0 : if (i != localRank) {
174 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersInOut[idx])));
175 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersInOut[idx + 1])));
176 : } else {
177 0 : if(isOpbaseMode){
178 0 : buffersInOut[idx] = algResource.cclInputMem.ptr();
179 : }else{
180 0 : if(topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE){
181 0 : buffersInOut[idx] = algResource.scratchMem.ptr();
182 : }else{
183 0 : buffersInOut[idx] = algResource.paramInputMem.ptr();
184 : }
185 : }
186 0 : buffersInOut[idx + 1] = algResource.aivOutputMem.ptr();
187 : }
188 : }
189 0 : const u32 bufferNum = 2;
190 0 : CHK_RET(hrtMemSyncCopy(algResource.aivCommInfoMem.ptr(), sizeof(u64) * localRankSize * bufferNum,
191 : buffersInOut, sizeof(u64) * localRankSize * bufferNum, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
192 0 : return HCCL_SUCCESS;
193 0 : }
194 :
195 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::PrepareCommInfoToDevice(AlgResourceResponse& algResource)
196 : {
197 0 : HCCL_INFO("[CollReduceScatterMeshAivFor91093Executor][PrepareCommInfoToDevice]"
198 : "ReduceScatter aiv copy comm info to device.");
199 0 : CHK_RET(CopyAivCommInfoToDevice(COMM_COMBINE_ORDER, COMM_INDEX_0, algResource));
200 0 : return HCCL_SUCCESS;
201 : }
202 :
203 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::GetAivExecParam(const OpParam& param, AlgResourceResponse& algRes, AivSuperKernelArgs &args)
204 : {
205 0 : HcclUs startut = TIME_NOW();
206 0 : tag_ = param.tag;
207 0 : algResResp_ = &algRes;
208 :
209 0 : HcclResult ret = HCCL_SUCCESS;
210 0 : ExecMem execMem;
211 0 : execMem.count = param.DataDes.count;
212 0 : execMem.inputPtr = param.inputPtr;
213 0 : execMem.outputPtr = param.outputPtr;
214 :
215 0 : execMem.inputMem = algRes.aivInputMem;
216 0 : if(topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE){
217 : // 图模式确定性
218 0 : execMem.inputMem = algRes.scratchMem;
219 : } else{
220 0 : execMem.inputMem = algRes.paramInputMem;
221 : }
222 0 : execMem.outputMem = algRes.aivOutputMem;
223 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
224 :
225 0 : u32 localRank = level0CommInfo.localRank;
226 0 : u32 localRankSize = level0CommInfo.localRankSize;
227 0 : HCCL_DEBUG("[CollReduceScatterMeshAivFor91093Executor][GetAivExecParam] userRank [%d] localRank [%d]",
228 : topoAttr_.userRank, localRank);
229 :
230 0 : args.buffersIn[0] = execMem.inputMem.ptr();
231 0 : args.buffersOut[0] = execMem.outputMem.ptr();
232 0 : constexpr u32 BUFFER_IDX_ONE = 1;
233 0 : args.buffersOut[BUFFER_IDX_ONE] = algRes.aivCommInfoMem.ptr(); // 通信域信息
234 :
235 0 : args.rank = localRank;
236 0 : args.rankSize = localRankSize;
237 0 : args.len = execMem.count;
238 0 : args.dataType = param.DataDes.dataType;
239 0 : args.unitSize = SIZE_TABLE[param.DataDes.dataType];
240 0 : args.reduceOp = param.reduceType;
241 0 : args.devType = static_cast<u32>(topoAttr_.deviceType);
242 0 : HCCL_INFO("SPK [CollReduceScatterMeshAivFor91093Executor][GetAivExecParam], rank[%llu], rankSize[%llu], len[%llu],datatype[%llu], op[%llu]", args.rank, args.rankSize, args.len, args.dataType, args.reduceOp);
243 :
244 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
245 : HCCL_ERROR("[CollReduceScatterMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
246 : "run failed", HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
247 :
248 0 : HCCL_INFO("tag[%s], ReduceScatter executor getalgexecparam success, take time [%lld]us.",
249 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
250 0 : return HCCL_SUCCESS;
251 0 : }
252 :
253 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
254 : {
255 0 : HcclUs startut = TIME_NOW();
256 0 : tag_ = param.tag;
257 0 : algResResp_ = &algRes;
258 :
259 0 : ExecMem execMem;
260 0 : execMem.count = param.DataDes.count;
261 0 : execMem.inputPtr = param.inputPtr;
262 0 : execMem.outputPtr = param.outputPtr;
263 :
264 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB){
265 0 : if(topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE){
266 : // 图模式确定性
267 0 : execMem.inputMem = algRes.scratchMem;
268 : }else{
269 0 : execMem.inputMem = algRes.paramInputMem;
270 : }
271 : }else{
272 0 : execMem.inputMem = algRes.cclInputMem;
273 : }
274 :
275 0 : execMem.outputMem = algRes.aivOutputMem;
276 0 : HcclResult ret = KernelRun(param, execMem);
277 :
278 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
279 : HCCL_ERROR("[CollReduceScatterMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
280 : "run failed", HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
281 :
282 0 : HCCL_INFO("tag[%s], ReduceScatter executor orchestrate success, take time [%lld]us.",
283 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
284 0 : return HCCL_SUCCESS;
285 0 : }
286 :
287 0 : HcclResult CollReduceScatterMeshAivFor91093Executor::KernelRun(const OpParam ¶m, ExecMem &execMem)
288 : {
289 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] ReduceScatter aiv enter.", __func__);
290 :
291 0 : CHK_RET(CheckCommSize(COMM_COMBINE_ORDER, COMM_INDEX_0 + 1));
292 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
293 :
294 : void *buffersIn[MAX_RANK_SIZE];
295 : void *buffersOut[MAX_RANK_SIZE];
296 :
297 0 : u32 localRank = level0CommInfo.localRank;
298 0 : u32 localRankSize = level0CommInfo.localRankSize;
299 0 : HCCL_DEBUG("[CollReduceScatterMeshAivFor91093Executor][KernelRun] userRank [%d] localRank [%d].",
300 : topoAttr_.userRank, localRank);
301 :
302 0 : buffersIn[0] = execMem.inputMem.ptr();
303 0 : buffersOut[0] = execMem.outputMem.ptr();
304 0 : constexpr u32 BUFFER_IDX_ONE = 1;
305 0 : buffersOut[BUFFER_IDX_ONE] = algResResp_->aivCommInfoMem.ptr(); // 通信域信息
306 :
307 0 : bool isOpbase = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
308 :
309 0 : AivOpArgs opArgs {
310 0 : HcclCMDType::HCCL_CMD_REDUCE_SCATTER, execMem.inputPtr, execMem.outputPtr, execMem.count,
311 0 : param.DataDes.dataType, param.reduceType, 0, isOpbase
312 0 : };
313 0 : AivTopoArgs topoArgs { localRank, localRankSize, MAX_RANK_SIZE, 0, topoAttr_.serverNum, topoAttr_.deviceType };
314 0 : topoArgs.identify = algoAttr_.identifier;
315 : u32 numBlocks;
316 0 : CHK_PRT_RET(CalNumBlocks(numBlocks, localRankSize, opArgs.count * SIZE_TABLE[opArgs.dataType]) != HCCL_SUCCESS,
317 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
318 : HCCL_E_PARA);
319 0 : numBlocks_ = numBlocks;
320 : AivResourceArgs resourceArgs {
321 0 : param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(), numBlocks_, param.aivTag
322 0 : };
323 0 : AivAlgArgs algArgs {};
324 0 : algArgs.argsType = KernelArgsType::ARGS_TYPE_SIMPLE;
325 0 : if(numBlocks_ >= localRankSize) {
326 0 : algArgs.step = localRankSize;
327 : } else {
328 0 : algArgs.step = numBlocks_;
329 : }
330 0 : if (topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE){
331 0 : algArgs.deterministic = 1;
332 : }
333 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
334 0 : algArgs.execTimeOutSet = true;
335 0 : struct AivProfilingInfo aivProfilingInfo;
336 0 : aivProfilingInfo.counter = opCounter_;
337 :
338 0 : HcclResult ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
339 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
340 : HCCL_ERROR("[CollReduceScatterMeshAivFor91093Executor][KernelRun]ReduceScatter aiv failed, return[%d]", ret),
341 : ret);
342 :
343 0 : ExtraArgs extraArgs;
344 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, true));
345 :
346 0 : HCCL_INFO("[CollReduceScatterMeshAivFor91093Executor][KernelRun]ReduceScatter aiv run success");
347 0 : return HCCL_SUCCESS;
348 0 : }
349 :
350 : REGISTER_EXEC("ReduceScatterMeshAivFor91093Executor", ReduceScatterMeshAivFor91093, CollReduceScatterMeshAivFor91093Executor);
351 :
352 : } // namespace hccl
|