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