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