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_all_to_all_mesh_aiv_for_910_93_executor.h"
12 : #include <algorithm>
13 :
14 : namespace hccl {
15 :
16 0 : CollAlltoAllMeshAivFor91093Executor::CollAlltoAllMeshAivFor91093Executor(
17 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
18 0 : : CollAlltoAllExecutor(dispatcher, topoMatcher)
19 : {
20 0 : desc_.isAivMode = true;
21 0 : desc_.isAivCrossNode = true;
22 0 : }
23 :
24 0 : HcclResult CollAlltoAllMeshAivFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
25 : {
26 0 : TransportMemType inputType = TransportMemType::RESERVED;
27 0 : TransportMemType outputType = TransportMemType::RESERVED;
28 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
29 : // level0 - level1 全连接通信域
30 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
31 0 : return HCCL_SUCCESS;
32 : }
33 :
34 : HcclResult
35 0 : CollAlltoAllMeshAivFor91093Executor::CalcTransportMemType(TransportMemType& inputType, TransportMemType& outputType)
36 : {
37 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
38 0 : inputType = TransportMemType::CCL_INPUT;
39 0 : outputType = TransportMemType::AIV_OUTPUT;
40 : } else {
41 0 : inputType = TransportMemType::PARAM_INPUT;
42 0 : outputType = TransportMemType::AIV_OUTPUT;
43 : }
44 0 : HCCL_INFO(
45 : "[CollAlltoAllMeshAivFor91093Executor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d].",
46 : tag_.c_str(), inputType, outputType);
47 0 : return HCCL_SUCCESS;
48 : }
49 :
50 : // level0-level1 打平fullmesh 超节点内建SDMA链路
51 0 : HcclResult CollAlltoAllMeshAivFor91093Executor::CalcLevel0CommInfo(
52 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
53 : {
54 0 : CommParaInfo commCombinePara(COMM_COMBINE_ORDER, CommType::COMM_TAG_MESH);
55 0 : commCombinePara.meshSinglePlane = true;
56 0 : CHK_RET(CalcCommPlaneInfo(tag_, commCombinePara, opTransport[COMM_COMBINE_ORDER], inputType, outputType));
57 :
58 0 : LevelNSubCommTransport& commTransportLevel0 = opTransport[COMM_COMBINE_ORDER];
59 0 : for (u32 subCommIndex = 0; subCommIndex < commTransportLevel0.size(); subCommIndex++) {
60 0 : for (auto& transportRequest : commTransportLevel0[subCommIndex].transportRequests) {
61 0 : transportRequest.isUsedRdma = false;
62 : }
63 : }
64 0 : return HCCL_SUCCESS;
65 0 : }
66 :
67 0 : HcclResult CollAlltoAllMeshAivFor91093Executor::CalNumBlocks(
68 : u32& numBlocks, u32 rankSize, [[maybe_unused]] u64 dataSize, [[maybe_unused]] HcclCMDType cmdType)
69 : {
70 : // A3超节点内多机场景,numBlocks_需要为偶数
71 0 : numBlocks = (rankSize < MAX_NUM_BLOCKS ? rankSize + rankSize % NUM_BLOCKS_FACTOR_TWO : MAX_NUM_BLOCKS);
72 0 : u32 bestNumBlocks = numBlocks;
73 0 : u32 minNumBlocks = std::max((rankSize + MAX_TARGET_NUM - 1) / MAX_TARGET_NUM, NUM_BLOCKS_FACTOR_TWO);
74 :
75 0 : if (numBlocks_ < numBlocks) {
76 0 : numBlocks = numBlocks_ / NUM_BLOCKS_FACTOR_TWO * NUM_BLOCKS_FACTOR_TWO;
77 0 : minNumBlocks = (minNumBlocks + NUM_BLOCKS_FACTOR_TWO - 1) / NUM_BLOCKS_FACTOR_TWO * NUM_BLOCKS_FACTOR_TWO;
78 : }
79 :
80 0 : CHK_PRT_RET(
81 : numBlocks < minNumBlocks,
82 : HCCL_ERROR(
83 : "[CollAlltoAllMeshAivFor91093Executor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
84 : numBlocks_, minNumBlocks),
85 : HCCL_E_PARA);
86 :
87 0 : HCCL_INFO(
88 : "[CollAlltoAllMeshAivFor91093Executor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
89 : numBlocks, numBlocks_, bestNumBlocks);
90 0 : return HCCL_SUCCESS;
91 : }
92 :
93 0 : HcclResult CollAlltoAllMeshAivFor91093Executor::GetAivExecParam(
94 : const OpParam& param, AlgResourceResponse& algRes, AivSuperKernelArgs& args)
95 : {
96 0 : HcclUs startut = TIME_NOW();
97 0 : tag_ = param.tag;
98 0 : algResResp_ = &algRes;
99 :
100 0 : HcclResult ret = HCCL_SUCCESS;
101 0 : ExecMem execMem;
102 0 : execMem.count = param.DataDes.count;
103 0 : execMem.inputPtr = param.inputPtr;
104 0 : execMem.outputPtr = param.outputPtr;
105 0 : execMem.inputMem = algRes.paramInputMem;
106 0 : execMem.outputMem = algRes.aivOutputMem;
107 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
108 :
109 0 : u32 localRank = level0CommInfo.localRank;
110 0 : u32 localRankSize = level0CommInfo.localRankSize;
111 0 : HCCL_DEBUG(
112 : "[CollAlltoAllMeshAivFor91093Executor][GetAivExecParam] userRank [%u] localRank [%u]", topoAttr_.userRank,
113 : localRank);
114 :
115 0 : args.buffersIn[0] = execMem.inputMem.ptr();
116 0 : args.buffersOut[0] = execMem.outputMem.ptr();
117 0 : constexpr u32 BUFFER_IDX_ONE = 1;
118 0 : args.buffersOut[BUFFER_IDX_ONE] = algRes.aivCommInfoMem.ptr(); // 通信域信息
119 :
120 0 : args.rank = localRank;
121 0 : args.rankSize = localRankSize;
122 0 : args.len = param.All2AllDataDes.sendCount;
123 0 : args.dataType = param.All2AllDataDes.sendType;
124 0 : args.unitSize = SIZE_TABLE[param.All2AllDataDes.sendType];
125 0 : args.reduceOp = param.reduceType;
126 0 : args.devType = static_cast<u32>(topoAttr_.deviceType);
127 0 : HCCL_INFO(
128 : "SPK [CollAlltoAllMeshAivFor91093Executor][GetAivExecParam], rank[%llu], rankSize[%llu], "
129 : "len[%llu],datatype[%llu], op[%llu]",
130 : args.rank, args.rankSize, args.len, args.dataType, args.reduceOp);
131 :
132 0 : CHK_PRT_RET(
133 : ret != HCCL_SUCCESS,
134 : HCCL_ERROR(
135 : "[CollAlltoAllMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
136 : "run failed",
137 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
138 : ret);
139 :
140 0 : HCCL_INFO(
141 : "tag[%s], AlltoAll executor getalgexecparam success, take time [%lld]us.", param.tag.c_str(),
142 : DURATION_US(TIME_NOW() - startut));
143 0 : return HCCL_SUCCESS;
144 0 : }
145 :
146 0 : HcclResult CollAlltoAllMeshAivFor91093Executor::PrepareCommInfoToDevice(AlgResourceResponse& algResource)
147 : {
148 0 : HCCL_INFO("[CollAlltoAllMeshAivFor91093Executor][PrepareCommInfoToDevice]AllToAll aiv copy comm info to device.");
149 0 : CHK_RET(CopyAivCommInfoToDevice(COMM_COMBINE_ORDER, COMM_INDEX_0, algResource));
150 0 : return HCCL_SUCCESS;
151 : }
152 :
153 0 : HcclResult CollAlltoAllMeshAivFor91093Executor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
154 : {
155 0 : HcclUs startut = TIME_NOW();
156 0 : tag_ = param.tag;
157 0 : algResResp_ = &algRes;
158 :
159 0 : ExecMem execMem;
160 0 : execMem.count = param.DataDes.count;
161 0 : execMem.inputPtr = param.inputPtr;
162 0 : execMem.outputPtr = param.outputPtr;
163 :
164 : execMem.inputMem
165 0 : = (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ? algRes.paramInputMem : algRes.cclInputMem);
166 0 : execMem.outputMem = algRes.aivOutputMem;
167 0 : HcclResult ret = KernelRun(param, execMem);
168 :
169 0 : CHK_PRT_RET(
170 : ret != HCCL_SUCCESS,
171 : HCCL_ERROR(
172 : "[CollAlltoAllMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] "
173 : "executor kernel run failed",
174 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
175 : ret);
176 :
177 0 : HCCL_INFO(
178 : "tag[%s], AlltoAll executor orchestrate success, take time [%lld]us", param.tag.c_str(),
179 : DURATION_US(TIME_NOW() - startut));
180 0 : return HCCL_SUCCESS;
181 0 : }
182 :
183 0 : HcclResult CollAlltoAllMeshAivFor91093Executor::KernelRun(const OpParam& param, ExecMem& execMem)
184 : {
185 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollAlltoAllMeshAivFor91093Executor][KernelRun]AllToAll aiv enter.");
186 :
187 0 : CHK_RET(CheckCommSize(COMM_COMBINE_ORDER, COMM_INDEX_0 + 1));
188 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
189 :
190 0 : u32 localRank = level0CommInfo.localRank;
191 0 : u32 localRankSize = level0CommInfo.localRankSize;
192 0 : HCCL_DEBUG(
193 : "[CollAlltoAllMeshAivFor91093Executor][KernelRun] userRank [%u] localRank [%u] localRankSize[%u]",
194 : topoAttr_.userRank, localRank, localRankSize);
195 :
196 : HcclResult ret;
197 0 : bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
198 0 : void* buffersIn[MAX_RANK_SIZE] = {};
199 0 : void* buffersOut[MAX_RANK_SIZE] = {};
200 0 : buffersIn[0] = execMem.inputMem.ptr();
201 0 : buffersOut[0] = execMem.outputMem.ptr();
202 0 : constexpr u32 BUFFER_IDX_ONE = 1;
203 0 : buffersOut[BUFFER_IDX_ONE] = algResResp_->aivCommInfoMem.ptr(); // 通信域信息
204 :
205 0 : AivTopoArgs topoArgs{localRank, localRankSize, MAX_RANK_SIZE, 0, topoAttr_.serverNum, topoAttr_.deviceType};
206 : u32 numBlocks;
207 0 : CHK_PRT_RET(
208 : CalNumBlocks(numBlocks, localRankSize) != HCCL_SUCCESS, HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
209 : HCCL_E_PARA);
210 0 : numBlocks_ = numBlocks;
211 0 : topoArgs.identify = algoAttr_.identifier;
212 0 : AivResourceArgs resourceArgs{param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(),
213 0 : numBlocks_, param.aivTag};
214 0 : AivAlgArgs algArgs{};
215 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
216 0 : algArgs.execTimeOutSet = true;
217 0 : AivProfilingInfo aivProfilingInfo;
218 0 : aivProfilingInfo.counter = opCounter_;
219 0 : AivOpArgs opArgs{
220 : HcclCMDType::HCCL_CMD_ALLTOALL,
221 0 : execMem.inputPtr,
222 0 : execMem.outputPtr,
223 0 : param.All2AllDataDes.sendCount,
224 0 : param.All2AllDataDes.sendType,
225 : HCCL_REDUCE_RESERVED,
226 : 0,
227 0 : isOpbase};
228 :
229 0 : if (param.opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
230 : // 兜底算法,单机也可能走这里
231 0 : constexpr u32 TWO_SERVER_NUM = 2;
232 0 : if (topoArgs.serverNum == 1) {
233 0 : topoArgs.serverNum = TWO_SERVER_NUM;
234 : }
235 0 : ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
236 : } else {
237 0 : algArgs.argsType = KernelArgsType::ARGS_TYPE_SUPERPOD;
238 0 : ExtraArgsV2 extraArgs;
239 0 : if (param.opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
240 0 : for (u32 i = 0; i < localRankSize; i++) {
241 : extraArgs.sendCounts[i]
242 0 : = *(static_cast<const u64*>(param.All2AllDataDes.sendCountMatrix) + localRank * localRankSize + i);
243 : extraArgs.recvCounts[i]
244 0 : = *(static_cast<const u64*>(param.All2AllDataDes.sendCountMatrix) + i * localRankSize + localRank);
245 0 : if (i == 0) {
246 0 : extraArgs.sendDispls[i] = 0;
247 0 : extraArgs.recvDispls[i] = 0;
248 : } else {
249 0 : extraArgs.sendDispls[i] = extraArgs.sendDispls[i - 1] + extraArgs.sendCounts[i - 1];
250 0 : extraArgs.recvDispls[i] = extraArgs.recvDispls[i - 1] + extraArgs.recvCounts[i - 1];
251 : }
252 : }
253 : } else {
254 0 : for (u32 i = 0; i < localRankSize; i++) {
255 0 : extraArgs.sendCounts[i] = *(static_cast<const u64*>(param.All2AllDataDes.sendCounts) + i);
256 0 : extraArgs.recvCounts[i] = *(static_cast<const u64*>(param.All2AllDataDes.recvCounts) + i);
257 0 : extraArgs.sendDispls[i] = *(static_cast<const u64*>(param.All2AllDataDes.sdispls) + i);
258 0 : extraArgs.recvDispls[i] = *(static_cast<const u64*>(param.All2AllDataDes.rdispls) + i);
259 : }
260 : }
261 0 : opArgs.cmdType = HcclCMDType::HCCL_CMD_ALLTOALLV;
262 0 : opArgs.dataType = param.All2AllDataDes.sendType;
263 0 : ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo);
264 : }
265 0 : CHK_PRT_RET(
266 : ret != HCCL_SUCCESS,
267 : HCCL_ERROR("[CollAlltoAllMeshAivFor91093Executor][KernelRun]AllToAll aiv failed, return[%d]", ret), ret);
268 :
269 0 : if (param.opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
270 0 : ExtraArgs extraArgs;
271 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, true));
272 : }
273 :
274 0 : HCCL_INFO("[CollAlltoAllMeshAivFor91093Executor][KernelRun]AllToAll aiv run success.");
275 0 : return HCCL_SUCCESS;
276 0 : }
277 :
278 : REGISTER_EXEC("AlltoAllMeshAivFor91093Executor", AlltoAllMeshAivFor91093, CollAlltoAllMeshAivFor91093Executor);
279 :
280 : } // namespace hccl
|