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_mid_count_aiv_rdma_executor.h"
12 :
13 : namespace hccl {
14 : constexpr s32 INTRA_RS_STEP = 0;
15 : constexpr s32 INTRA_AG_STEP = 2;
16 :
17 0 : CollAllReduceMidCountAivRdmaExecutor::CollAllReduceMidCountAivRdmaExecutor(
18 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
19 0 : : CollAllReduceExecutor(dispatcher, topoMatcher)
20 : {
21 0 : DMAReduceFlag_ = false;
22 0 : desc_.isAivMode = true;
23 0 : desc_.aivTagNum = AIV_A2_ALL_REDUCE_RDMA_KERNEL_NUM;
24 0 : }
25 :
26 0 : HcclResult CollAllReduceMidCountAivRdmaExecutor::CalcStreamNum(u32& streamNum)
27 : {
28 0 : u32 totalStreamNum = topoAttr_.deviceNumPerAggregation > 1U ? topoAttr_.deviceNumPerAggregation - 1U : 1U;
29 0 : streamNum = totalStreamNum - 1U;
30 0 : HCCL_INFO("[CollAllReduceMidCountAivRdmaExecutor][CalcStreamNum] tag[%s] streamNum[%u]", tag_.c_str(), streamNum);
31 0 : return HCCL_SUCCESS;
32 : }
33 :
34 0 : HcclResult CollAllReduceMidCountAivRdmaExecutor::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 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
41 0 : return HCCL_SUCCESS;
42 : }
43 :
44 : HcclResult
45 0 : CollAllReduceMidCountAivRdmaExecutor::CalcTransportMemType(TransportMemType& inputType, TransportMemType& outputType)
46 : {
47 : // 中数据量:使用AIVIN,标记区在AIVIN末尾,单算子模式用CCLOUT,图模式用USEROUT
48 0 : inputType = TransportMemType::AIV_INPUT;
49 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
50 0 : outputType = TransportMemType::CCL_OUTPUT;
51 : } else {
52 0 : outputType = TransportMemType::PARAM_OUTPUT;
53 : }
54 0 : HCCL_INFO(
55 : "[CollAllReduceMidCountAivRdmaExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
56 : tag_.c_str(), inputType, outputType);
57 0 : return HCCL_SUCCESS;
58 : }
59 :
60 0 : HcclResult CollAllReduceMidCountAivRdmaExecutor::CalcLevel0CommInfo(
61 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
62 : {
63 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
64 0 : commParaLevel0.meshSinglePlane = true;
65 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
66 0 : return HCCL_SUCCESS;
67 0 : }
68 :
69 : HcclResult
70 0 : CollAllReduceMidCountAivRdmaExecutor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType)
71 : {
72 0 : numBlocks = rankSize; // 默认情况使用rankSize个AIV
73 0 : u32 bestNumBlocks = numBlocks;
74 :
75 0 : CHK_PRT_RET(
76 : numBlocks_ < numBlocks,
77 : HCCL_WARNING(
78 : "[CollAllReduceMidCountAivRdmaExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
79 : numBlocks_, numBlocks),
80 : HCCL_E_PARA);
81 :
82 0 : HCCL_INFO(
83 : "[CollAllReduceMidCountAivRdmaExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
84 : numBlocks, numBlocks_, bestNumBlocks);
85 0 : return HCCL_SUCCESS;
86 : }
87 :
88 0 : HcclResult CollAllReduceMidCountAivRdmaExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
89 : {
90 0 : HcclUs startut = TIME_NOW();
91 0 : tag_ = param.tag;
92 0 : algResResp_ = &algRes;
93 :
94 : // 中数据量:使用AIVIN,标记区在AIVIN末尾,单算子模式用CCLOUT,图模式用USEROUT
95 0 : ExecMem execMem;
96 0 : execMem.count = param.DataDes.count;
97 0 : execMem.inputPtr = param.inputPtr;
98 0 : execMem.inputMem = algRes.aivInputMem;
99 0 : execMem.outputPtr = param.outputPtr;
100 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
101 0 : execMem.outputMem = algRes.cclOutputMem;
102 : } else {
103 0 : execMem.outputMem = algRes.paramOutputMem;
104 : }
105 0 : HcclResult ret = KernelRun(param, execMem);
106 :
107 0 : CHK_PRT_RET(
108 : ret != HCCL_SUCCESS,
109 : HCCL_ERROR(
110 : "[CollAllReduceMidCountAivRdmaExecutor]errNo[0x%016llx] tag[%s] executor kernel run failed",
111 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
112 : ret);
113 :
114 0 : HCCL_INFO(
115 : "tag[%s], AllReduce executor orchestrate success, take time [%lld]us.", param.tag.c_str(),
116 : DURATION_US(TIME_NOW() - startut));
117 0 : return HCCL_SUCCESS;
118 0 : }
119 :
120 0 : HcclResult CollAllReduceMidCountAivRdmaExecutor::GetAdjInfo(AlgResourceResponse& algRes, AdjInfo& adjInfo)
121 : {
122 0 : return HCCL_SUCCESS;
123 : }
124 :
125 0 : HcclResult CollAllReduceMidCountAivRdmaExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
126 : {
127 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollAllReduceMidCountAivRdmaExecutor][KernelRun]AllReduce aiv enter");
128 0 : HcclWorkflowMode workflow = workflowMode_;
129 0 : bool isOpbase = (workflow == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
130 0 : CHK_RET(ActiveSlaveStreams(param.stream));
131 :
132 : // 获取通信域信息
133 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
134 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
135 0 : u32 commIndex = level0CommInfo.localRank;
136 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
137 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
138 :
139 : // 数据准备,按照server内rankSize切片
140 0 : u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
141 0 : u64 totalSize = param.DataDes.count * perDataSize;
142 0 : std::vector<Slice> dataSegsSlice; // 数据分成ranksize份,每份的起始偏移和大小
143 0 : u32 sliceNum = level0CommInfo.localRankSize;
144 0 : CHK_RET(PrepareSliceDataWithAlignSize(totalSize, sliceNum, 0, dataSegsSlice, HCCL_ALIGN_COUNT_32_B));
145 0 : CHK_PRT_RET(
146 : commIndex >= dataSegsSlice.size(),
147 : HCCL_ERROR(
148 : "[CollAllReduceMidCountAivRdmaExecutor][Run]commIndex[%u] >= dataSegsSlice size[%zu]", commIndex,
149 : dataSegsSlice.size()),
150 : HCCL_E_INTERNAL);
151 0 : std::vector<hccl::LINK> intraLinks = level0CommInfo.links;
152 0 : std::vector<hccl::LINK> interLinks = level1CommInfo.links;
153 0 : u32 intraRankSize = level0CommInfo.localRankSize;
154 0 : u32 intraRankId = level0CommInfo.localRank;
155 :
156 : // reduce scatter阶段,inputMem0-31m做数据区,32M开始后的1M做标记区
157 : void* dataBuffers[MAX_RANK_SIZE];
158 : void* flagBuffers[MAX_RANK_SIZE]; // 标记区的具体偏移在kernel中决定
159 0 : CHK_RET(PrepareAivBuffers(
160 : intraRankSize, intraRankId, 0, execMem.inputMem, execMem.inputMem, intraLinks, dataBuffers, flagBuffers,
161 : UserMemType::INPUT_MEM, UserMemType::INPUT_MEM, 0, HCCL_MID_COUNT_32_MB));
162 : // 先做本地拷贝到AIVIN再跨片拷贝;output统一为allreduceInput的位置,即buffer中原位
163 :
164 0 : AivOpArgs opArgs{HcclCMDType::HCCL_CMD_ALLREDUCE, execMem.inputPtr, nullptr, execMem.count,
165 0 : param.DataDes.dataType, param.reduceType, 0, isOpbase};
166 0 : AivTopoArgs topoArgs{intraRankId, intraRankSize};
167 : u32 numBlocks;
168 0 : CHK_PRT_RET(
169 : CalNumBlocks(numBlocks, intraRankSize) != HCCL_SUCCESS, HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
170 : HCCL_E_PARA);
171 0 : numBlocks_ = numBlocks;
172 0 : topoArgs.identify = algoAttr_.identifier;
173 0 : AivResourceArgs resourceArgs{param.tag, param.stream.ptr(), dataBuffers, flagBuffers, execMem.inputMem.size(),
174 0 : numBlocks_, param.aivTag};
175 0 : AivAlgArgs algArgs{INTRA_RS_STEP, false};
176 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
177 0 : algArgs.execTimeOutSet = true;
178 0 : struct AivProfilingInfo aivProfilingInfo;
179 0 : aivProfilingInfo.counter = opCounter_;
180 :
181 0 : CHK_RET(ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo));
182 :
183 : // allreduce 阶段
184 0 : std::unique_ptr<AlgTemplateBase> level1TempAlg;
185 0 : DeviceMem allreduceInput = execMem.inputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
186 0 : CHK_SMART_PTR_NULL(allreduceInput);
187 0 : DeviceMem allreduceOutput = execMem.outputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
188 0 : CHK_SMART_PTR_NULL(allreduceOutput);
189 :
190 0 : u64 reduceAttr = GetReduceAttr(execMem.inputMem, execMem.outputMem, param.DataDes.dataType, param.reduceType);
191 0 : auto autoSelectedAlgTypeLevel1 = static_cast<u32>(algType_.algoLevel1);
192 0 : auto opMeta = HcclOpMetaInfo::GetOneForAllReduce(
193 0 : autoSelectedAlgTypeLevel1, param.DataDes.dataType, ReduceType::INLINE_REDUCE, IsAllReduceSmallData(totalSize),
194 : 1, false, hccl::CopyPattern::BCOPY, 1, true);
195 0 : CHK_RET(InitTask(dispatcher_, const_cast<Stream&>(param.stream), opMeta.isEnableCache, opMeta.GetCacheKey()));
196 :
197 0 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
198 : level1TempAlg
199 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_RING, dispatcher_);
200 0 : HCCL_INFO("AllReduce mesh: using ring algo inter-server.");
201 0 : CHK_SMART_PTR_NULL(level1TempAlg);
202 0 : CHK_RET(level1TempAlg->Prepare(reduceAttr));
203 0 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR) {
204 0 : u64 curSize = execMem.count * perDataSize; // 单位 byte
205 0 : HCCL_DEBUG(
206 : "AllReduce mesh: curSize[%llu] deviceNumPerAggregation[%u] commLevel0Size[%u]", curSize,
207 : topoAttr_.deviceNumPerAggregation, level0CommInfo.localRankSize);
208 0 : if (curSize / topoAttr_.deviceNumPerAggregation <= NHR_ALLREDUCE_SMALL_SIZE) {
209 0 : level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
210 0 : TemplateType::TEMPLATE_ALL_REDUCE_NHR_ONESHOT, dispatcher_);
211 : } else {
212 : level1TempAlg
213 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NHR, dispatcher_);
214 : }
215 0 : HCCL_INFO("AllReduce mesh: using nhr algo inter-server.");
216 0 : CHK_SMART_PTR_NULL(level1TempAlg);
217 0 : CHK_RET(level1TempAlg->Prepare(reduceAttr));
218 0 : level1TempAlg->CloseBarrier();
219 0 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR_V1) {
220 : level1TempAlg
221 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NHR_V1, dispatcher_);
222 0 : HCCL_INFO("AllReduce mesh: using nhr_v1 algo inter-server.");
223 0 : CHK_SMART_PTR_NULL(level1TempAlg);
224 0 : CHK_RET(level1TempAlg->Prepare(reduceAttr));
225 0 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NB) {
226 : level1TempAlg
227 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NB, dispatcher_);
228 0 : HCCL_INFO("AllReduce mesh: using nb algo inter-server.");
229 0 : CHK_SMART_PTR_NULL(level1TempAlg);
230 0 : CHK_RET(level1TempAlg->Prepare(reduceAttr));
231 : } else {
232 0 : level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
233 0 : TemplateType::TEMPLATE_ALL_REDUCE_RECURSIVE_HALVING_DOUBLING, dispatcher_);
234 0 : HCCL_INFO("AllReduce mesh: using Recursive halving-doubling algo inter-server.");
235 0 : CHK_SMART_PTR_NULL(level1TempAlg);
236 0 : CHK_RET(level1TempAlg->Prepare(reduceAttr));
237 : }
238 0 : CHK_SMART_PTR_NULL(level1TempAlg);
239 :
240 0 : u32 rankSize = level1CommInfo.localRankSize;
241 0 : u64 hdCount = dataSegsSlice[commIndex].size / perDataSize;
242 0 : CHK_RET(level1TempAlg->Prepare(
243 : allreduceInput, allreduceOutput, allreduceOutput, hdCount, param.DataDes.dataType, param.stream,
244 : param.reduceType, LEVEL0_BRIDGE_RANK_ID, std::vector<Slice>(0), dataSegsSlice[commIndex].offset));
245 :
246 0 : CHK_RET(level1TempAlg->RegisterProfiler(
247 : (rankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level1CommInfo.localRank, PROF_STAGE_1, HCCL_EXEC_STEP_NOT_SET,
248 : param.stream));
249 0 : CHK_RET(RunTemplate(level1TempAlg, level1CommInfo));
250 0 : HCCL_INFO("[CollAllReduceMidCountAivRdmaExecutor] rdma stage run success.");
251 0 : CHK_RET(LaunchTask(dispatcher_, const_cast<Stream&>(param.stream)));
252 :
253 : // allgather阶段,outputMem做数据区,32M开始后的1M做标记区
254 0 : CHK_RET(PrepareAivBuffers(
255 : intraRankSize, intraRankId, 0, execMem.outputMem, execMem.inputMem, intraLinks, dataBuffers, flagBuffers,
256 : UserMemType::OUTPUT_MEM, UserMemType::INPUT_MEM, 0, HCCL_MID_COUNT_32_MB));
257 : // 输入统一为allreduceOutput的位置,各卡不同;单算子模式需要outputAddr,先做本地拷贝再跨片拷贝;图模式结果直接放在CCL
258 : // Out中
259 :
260 0 : opArgs.input = nullptr;
261 0 : opArgs.output = execMem.outputPtr;
262 0 : resourceArgs.buffersIn = dataBuffers;
263 0 : resourceArgs.buffersOut = flagBuffers;
264 0 : resourceArgs.aivTag = GetNextAivTag(resourceArgs.aivTag);
265 0 : algArgs.step = INTRA_AG_STEP;
266 :
267 0 : CHK_RET(ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo));
268 :
269 0 : HCCL_INFO("[CollAllReduceMidCountAivRdmaExecutor][KernelRun]AllReduce aiv run success");
270 0 : return HCCL_SUCCESS;
271 0 : }
272 :
273 : REGISTER_EXEC("AllReduceMidCountAivRdmaExecutor", AllReduceMidCountAivRdma, CollAllReduceMidCountAivRdmaExecutor);
274 :
275 : } // namespace hccl
|