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_reduce_mesh_aiv_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollAllReduceMeshAivExecutor::CollAllReduceMeshAivExecutor(
16 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
17 0 : : CollAllReduceExecutor(dispatcher, topoMatcher)
18 : {
19 0 : DMAReduceFlag_ = false;
20 0 : desc_.isAivMode = true;
21 0 : desc_.deterministic = 0;
22 0 : }
23 :
24 0 : HcclResult CollAllReduceMeshAivExecutor::CalcStreamNum(u32& streamNum)
25 : {
26 0 : streamNum = 0; // AIV通信不需要申请从流
27 0 : HCCL_INFO("[CollAllReduceMeshAivExecutor][CalcStreamNum] tag[%s] streamNum[%u].", tag_.c_str(), streamNum);
28 0 : return HCCL_SUCCESS;
29 : }
30 :
31 0 : HcclResult CollAllReduceMeshAivExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
32 : {
33 0 : TransportMemType inputType = TransportMemType::RESERVED;
34 0 : TransportMemType outputType = TransportMemType::RESERVED;
35 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
36 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
37 0 : return HCCL_SUCCESS;
38 : }
39 :
40 0 : HcclResult CollAllReduceMeshAivExecutor::CalcTransportMemType(TransportMemType& inputType, TransportMemType& outputType)
41 : {
42 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
43 0 : inputType = TransportMemType::CCL_INPUT;
44 0 : outputType = TransportMemType::AIV_OUTPUT;
45 : } else {
46 0 : inputType = TransportMemType::PARAM_INPUT;
47 0 : outputType = TransportMemType::AIV_OUTPUT;
48 : }
49 0 : HCCL_INFO(
50 : "[CollAllReduceMeshAivExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d].", tag_.c_str(),
51 : inputType, outputType);
52 0 : return HCCL_SUCCESS;
53 : }
54 :
55 0 : HcclResult CollAllReduceMeshAivExecutor::CalcLevel0CommInfo(
56 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
57 : {
58 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
59 0 : commParaLevel0.meshSinglePlane = true;
60 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
61 0 : return HCCL_SUCCESS;
62 0 : }
63 :
64 0 : HcclResult CollAllReduceMeshAivExecutor::CalNumBlocks(
65 : u32& numBlocks, u32 rankSize, [[maybe_unused]] u64 dataSize, [[maybe_unused]] HcclCMDType cmdType)
66 : {
67 0 : numBlocks = rankSize; // 默认情况使用rankSize个AIV
68 :
69 0 : bool isOpBase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
70 0 : if (isOpBase) {
71 0 : numBlocks = NUM_BLOCKS_FACTOR_TWO * rankSize; // 单机场景,单算子AllReduce大数据使用2倍 rankSize个aiv
72 : }
73 :
74 0 : u32 bestNumBlocks = numBlocks;
75 0 : CHK_PRT_RET(
76 : numBlocks_ < rankSize,
77 : HCCL_WARNING(
78 : "[CollAllReduceMeshAivExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].", numBlocks_,
79 : rankSize),
80 : HCCL_E_PARA);
81 0 : CHK_PRT_RET(
82 : isOpBase && numBlocks_ < bestNumBlocks,
83 : HCCL_WARNING(
84 : "[CollAllReduceMeshAivExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].", numBlocks_,
85 : bestNumBlocks),
86 : HCCL_E_PARA);
87 :
88 0 : if (numBlocks_ < numBlocks) {
89 0 : numBlocks = numBlocks_ / rankSize * rankSize;
90 : }
91 :
92 0 : HCCL_INFO(
93 : "[CollAllReduceMeshAivExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]", numBlocks,
94 : numBlocks_, bestNumBlocks);
95 0 : return HCCL_SUCCESS;
96 : }
97 :
98 0 : HcclResult CollAllReduceMeshAivExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
99 : {
100 0 : HcclUs startut = TIME_NOW();
101 0 : tag_ = param.tag;
102 0 : algResResp_ = &algRes;
103 :
104 0 : HcclResult ret = HCCL_SUCCESS;
105 0 : ExecMem execMem;
106 0 : execMem.count = param.DataDes.count;
107 0 : execMem.inputPtr = param.inputPtr;
108 0 : execMem.outputPtr = param.outputPtr;
109 :
110 0 : if (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
111 0 : execMem.inputMem = algRes.paramInputMem;
112 0 : execMem.outputMem = algRes.aivOutputMem;
113 0 : ret = KernelRun(param, execMem);
114 : } else {
115 0 : execMem.inputMem = algRes.cclInputMem;
116 0 : execMem.outputMem = algRes.aivOutputMem;
117 0 : ret = KernelRun(param, execMem);
118 : }
119 :
120 0 : CHK_PRT_RET(
121 : ret != HCCL_SUCCESS,
122 : HCCL_ERROR(
123 : "[CollAllReduceMeshAivExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel run failed",
124 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
125 : ret);
126 :
127 0 : HCCL_INFO(
128 : "tag[%s], AllReduce executor orchestrate success, take time [%lld]us", param.tag.c_str(),
129 : DURATION_US(TIME_NOW() - startut));
130 0 : return HCCL_SUCCESS;
131 0 : }
132 :
133 0 : HcclResult CollAllReduceMeshAivExecutor::GetAdjInfo(
134 : [[maybe_unused]] AlgResourceResponse& algRes, [[maybe_unused]] AdjInfo& adjInfo)
135 : {
136 0 : return HCCL_SUCCESS;
137 : }
138 :
139 0 : HcclResult CollAllReduceMeshAivExecutor::GetAivExecParam(
140 : const OpParam& param, AlgResourceResponse& algRes, AivSuperKernelArgs& args)
141 : {
142 0 : HcclUs startut = TIME_NOW();
143 0 : tag_ = param.tag;
144 0 : algResResp_ = &algRes;
145 :
146 0 : HcclResult ret = HCCL_SUCCESS;
147 0 : ExecMem execMem;
148 0 : execMem.count = param.DataDes.count;
149 0 : execMem.inputPtr = param.inputPtr;
150 0 : execMem.outputPtr = param.outputPtr;
151 :
152 : // 单算子大数据量
153 0 : execMem.inputMem = algRes.paramInputMem;
154 0 : execMem.outputMem = algRes.aivOutputMem;
155 :
156 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
157 :
158 0 : u32 localRank = level0CommInfo.localRank;
159 0 : u32 localRankSize = level0CommInfo.localRankSize;
160 0 : HCCL_DEBUG(
161 : "[CollAllReduceMeshAivExecutor][GetAivExecParam] userRank [%d] localRank [%d]", topoAttr_.userRank, localRank);
162 :
163 0 : for (u32 i = 0; i < localRankSize; i++) {
164 0 : if (i != localRank) {
165 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(args.buffersIn[i])));
166 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(args.buffersOut[i])));
167 : } else {
168 0 : args.buffersIn[i] = execMem.inputMem.ptr();
169 0 : args.buffersOut[i] = execMem.outputMem.ptr();
170 : }
171 : }
172 :
173 0 : HCCL_INFO(
174 : "SPK, buffersIn [%p] [%p] [%p] [%p]"
175 : "buffersOut [%p] [%p] [%p] [%p]",
176 : args.buffersIn[0], args.buffersIn[1], args.buffersIn[2], args.buffersIn[3], args.buffersOut[0],
177 : args.buffersOut[1], args.buffersOut[2], args.buffersOut[3]);
178 0 : args.rank = localRank;
179 0 : args.rankSize = localRankSize;
180 0 : args.len = execMem.count;
181 0 : args.dataType = param.DataDes.dataType;
182 0 : args.unitSize = SIZE_TABLE[param.DataDes.dataType];
183 0 : args.reduceOp = param.reduceType;
184 0 : args.devType = static_cast<u32>(topoAttr_.deviceType);
185 0 : HCCL_INFO(
186 : "SPK [CollAllReduceMeshAivExecutor][GetAivExecParam], rank[%llu], rankSize[%llu], len[%llu],datatype[%llu], "
187 : "op[%llu]",
188 : args.rank, args.rankSize, args.len, args.dataType, args.reduceOp);
189 :
190 0 : CHK_PRT_RET(
191 : ret != HCCL_SUCCESS,
192 : HCCL_ERROR(
193 : "[CollAllReduceMeshAivExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
194 : "run failed",
195 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
196 : ret);
197 :
198 0 : HCCL_INFO(
199 : "tag[%s], AllReduce executor getalgexecparam success, take time [%lld]us.", param.tag.c_str(),
200 : DURATION_US(TIME_NOW() - startut));
201 0 : return HCCL_SUCCESS;
202 0 : }
203 :
204 0 : HcclResult CollAllReduceMeshAivExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
205 : {
206 0 : HCCL_INFO("[%s] AllReduce aiv enter.", __func__);
207 :
208 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
209 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
210 :
211 : void* buffersIn[MAX_RANK_SIZE];
212 : void* buffersOut[MAX_RANK_SIZE];
213 :
214 0 : u32 localRank = level0CommInfo.localRank;
215 0 : u32 localRankSize = level0CommInfo.localRankSize;
216 0 : HCCL_DEBUG("[CollAllReduceMeshAivExecutor][KernelRun] userRank [%u] localRank [%u]", topoAttr_.userRank, localRank);
217 :
218 0 : for (u32 i = 0; i < localRankSize; i++) {
219 0 : if (i != localRank) {
220 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersIn[i])));
221 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersOut[i])));
222 : } else {
223 0 : buffersIn[i] = execMem.inputMem.ptr();
224 0 : buffersOut[i] = execMem.outputMem.ptr();
225 : }
226 : }
227 :
228 0 : bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
229 0 : AivOpArgs opArgs{
230 : HcclCMDType::HCCL_CMD_ALLREDUCE,
231 0 : execMem.inputPtr,
232 0 : execMem.outputPtr,
233 0 : execMem.count,
234 0 : param.DataDes.dataType,
235 0 : param.reduceType,
236 : 0,
237 0 : isOpbase};
238 0 : AivTopoArgs topoArgs{localRank, localRankSize, MAX_RANK_SIZE, 0, 1, topoAttr_.deviceType};
239 : u32 numBlocks;
240 0 : CHK_PRT_RET(
241 : CalNumBlocks(numBlocks, localRankSize) != HCCL_SUCCESS, HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
242 : HCCL_E_PARA);
243 0 : numBlocks_ = numBlocks;
244 0 : topoArgs.identify = algoAttr_.identifier;
245 0 : HCCL_DEBUG("[CollAllReduceMeshAivExecutor][KernelRun]numBlocks is %u", numBlocks_);
246 0 : AivResourceArgs resourceArgs{param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(),
247 0 : numBlocks_, param.aivTag};
248 0 : AivAlgArgs algArgs{};
249 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
250 0 : algArgs.execTimeOutSet = true;
251 0 : struct AivProfilingInfo aivProfilingInfo;
252 0 : aivProfilingInfo.counter = opCounter_;
253 :
254 0 : HcclResult ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
255 :
256 : // 设置ExecuteKernelLaunch入参缓存,第二次调用的时候会调用到缓存上去
257 0 : ExtraArgs extraArgs;
258 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, false));
259 :
260 0 : CHK_PRT_RET(
261 : ret != HCCL_SUCCESS,
262 : HCCL_ERROR("[CollAllReduceMeshAivExecutor][KernelRun]AllReduce aiv failed, return[%d]", ret), ret);
263 :
264 0 : HCCL_INFO("[CollAllReduceMeshAivExecutor][KernelRun]AllReduce aiv run success.");
265 0 : return HCCL_SUCCESS;
266 0 : }
267 :
268 : REGISTER_EXEC("AllReduceMeshAivExecutor", AllReduceMeshAiv, CollAllReduceMeshAivExecutor);
269 :
270 : } // namespace hccl
|