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