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 "log.h"
12 : #include "ins_coll_alg_registry.h"
13 : #include "topo_match_concurr_mesh.h"
14 : #include "topo_match_mesh.h"
15 : #include "topo_match_nhr.h"
16 :
17 : #include "ins_temp_all_reduce_mesh_1D_two_shot.h"
18 : #include "ins_temp_all_reduce_mesh_2D_two_shot.h"
19 : #include "ins_temp_all_reduce_mesh_1D_one_shot.h"
20 : #include "ins_temp_all_reduce_mesh_1D_two_shot_mesh_chunk.h"
21 : #include "ins_temp_all_reduce_nhr.h"
22 : #include "ins_v2_all_reduce_sole_executor.h"
23 : #ifndef CCL_KERNEL_AICPU
24 : #include "aiv_temp_all_reduce_mesh_1D_oneshot.h"
25 : #include "aiv_temp_all_reduce_mesh_1D_twoshot.h"
26 : #include "ccu_temp_all_reduce_nhr_1D_mem2mem.h"
27 : #include "ccu_temp_all_reduce_mesh_1D_mem2mem.h"
28 : #endif
29 : #include "ins_temp_all_reduce_aicpu_reduce.h"
30 : #include "ins_temp_all_reduce_aicpu_reduce_mesh_2D.h"
31 :
32 : namespace Hccl {
33 : constexpr u64 MAX_OFFLOAD_SCRATCH_SIZE = 200 * 1024 * 1024; // 200M
34 :
35 : template <typename AlgTopoMatch, typename InsAlgTemplate>
36 0 : InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InsV2AllReduceSoleExecutor() : InsCollAlgBase()
37 0 : {}
38 :
39 : template <typename AlgTopoMatch, typename InsAlgTemplate>
40 0 : InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::~InsV2AllReduceSoleExecutor()
41 0 : {}
42 :
43 : template <typename AlgTopoMatch, typename InsAlgTemplate>
44 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const RankGraph *rankGraph)
45 : {
46 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
47 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
48 0 : return HcclResult::HCCL_SUCCESS;
49 0 : }
50 :
51 : template <typename AlgTopoMatch, typename InsAlgTemplate>
52 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const AlgTopoInfo &topoInfo)
53 : {
54 0 : CHK_PRT_RET(topoInfo.vTopo.size() == 0 || topoInfo.virtRankMap.size() == 0 || topoInfo.virtRanks.size() == 0,
55 : HCCL_ERROR("[InsV2AllReduceSoleExecutor][InitCommInfo] topoInfo vector member size is 0 !"),
56 : HCCL_E_INTERNAL);
57 0 : vTopo_ = topoInfo.vTopo[0]; // 本通信域内的通信平面
58 0 : virtRankMap_ = topoInfo.virtRankMap[0]; // 本通信域内的 rank 映射表
59 0 : virtRanks_ = topoInfo.virtRanks[0]; // 本通信域内的 rank 集合
60 0 : return HcclResult::HCCL_SUCCESS;
61 : }
62 :
63 : template <typename AlgTopoMatch, typename InsAlgTemplate>
64 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CreateTemplates(
65 : std::shared_ptr<InsAlgTemplate> &algTemplatePtr)
66 : {
67 0 : algTemplatePtr = std::make_shared<InsAlgTemplate>(myRank_, rankSize_, vTopo_, virtRankMap_);
68 0 : CHK_PTR_NULL(algTemplatePtr); // 检查是否成功分配内存
69 0 : algTemplatePtr->SetDmaMode(dmaMode_);
70 0 : algTemplatePtr->InitReduceInfo(redOp_, dataType_);
71 0 : algTemplatePtr->SetCollOp(op_);
72 0 : return HcclResult::HCCL_SUCCESS;
73 : }
74 :
75 : template <typename AlgTopoMatch, typename InsAlgTemplate>
76 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::GetTemplateResRequest(
77 : const RankGraph *rankGraph, std::shared_ptr<InsAlgTemplate> &algTemplate, AlgTempResReq &tempResReq) const
78 : {
79 0 : if (enableDetour_) {
80 0 : HCCL_DEBUG("[%s] Rank[%d]. CalcRes with detouring enabled.", __func__, myRank_);
81 0 : CHK_RET(algTemplate->CalcResDetour(rankGraph, tempResReq));
82 : } else {
83 0 : HCCL_DEBUG("[%s] Rank[%d]. CalcRes with detouring disabled.", __func__, myRank_);
84 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
85 : }
86 0 : return HcclResult::HCCL_SUCCESS;
87 : }
88 :
89 : template <typename AlgTopoMatch, typename InsAlgTemplate>
90 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::GetTemplateResRequest(
91 : ConnectedLinkMgr *linkMgr, std::shared_ptr<InsAlgTemplate> &algTemplate, AlgTempResReq &tempResReq) const
92 : {
93 0 : if (enableDetour_) {
94 0 : HCCL_DEBUG("[InsV2AllReduceSoleExecutor] Rank[%d], CalcRes with detouring enabled.", myRank_);
95 0 : CHK_RET(algTemplate->CalcResDetour(linkMgr, tempResReq));
96 : } else {
97 0 : HCCL_DEBUG("[InsV2AllReduceSoleExecutor] Rank[%d], CalcRes with detouring disabled.", myRank_);
98 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
99 : }
100 0 : return HcclResult::HCCL_SUCCESS;
101 : }
102 :
103 : template <typename AlgTopoMatch, typename InsAlgTemplate>
104 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcRes(
105 : const RankGraph *rankGraph, CollAlgResReq &algResReq)
106 : {
107 : // Topo Match
108 0 : CHK_RET(InitCommInfo(rankGraph));
109 :
110 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
111 0 : CHK_RET(CreateTemplates(algTemplate));
112 :
113 : // 通过判断哪层通信域能有到所有remoteRank的path,判断当前算法跑在哪一层
114 0 : std::map<u32, u32>rank2PathNumMap;
115 0 : HCCL_INFO("[InsV2AllReduceSoleExecutor] CalcRes SetPathNumMap");
116 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
117 0 : algTemplate->setPathNumMap(rank2PathNumMap);
118 0 : AlgTempResReq tempResReq;
119 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
120 :
121 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, tempResReq.links, algResReq.levelRankPairs));
122 0 : algResReq.topoInfo.UpdateSingleLevelTopo(virtRanks_, virtRankMap_, vTopo_);
123 0 : algResReq.primQueueNum = tempResReq.streamNum;
124 0 : algResReq.localWaitGroupCntNotify = tempResReq.localWaitGroupCntNotify;
125 0 : algResReq.localBcastPostCntNotify = tempResReq.localBcastPostCntNotify;
126 0 : algResReq.queueNotifys = tempResReq.queNotifys;
127 0 : HCCL_DEBUG("[%s] Rank[%d], requiredQueNum [%u].", __func__, myRank_, algResReq.primQueueNum);
128 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, algResReq.links));
129 :
130 0 : return HcclResult::HCCL_SUCCESS;
131 0 : }
132 :
133 : template <typename AlgTopoMatch, typename InsAlgTemplate>
134 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcResOffload(
135 : const RankGraph *rankGraph, const u64 &dataSize, CollOffloadOpResReq &resReq)
136 : {
137 : (void)dataSize;
138 :
139 : // Topo Match
140 0 : CHK_RET(InitCommInfo(rankGraph));
141 :
142 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
143 0 : CHK_RET(CreateTemplates(algTemplate));
144 :
145 : // 通过判断哪层通信域能有到所有remoteRank的path,判断当前算法跑在哪一层
146 0 : std::map<u32, u32>rank2PathNumMap;
147 0 : HCCL_INFO("[InsV2AllReduceSoleExecutor] CalcResOffload SetPathNumMap");
148 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
149 0 : algTemplate->setPathNumMap(rank2PathNumMap);
150 0 : AlgTempResReq tempResReq;
151 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
152 0 : u64 transportBoundDataSize = UB_MAX_DATA_SIZE;
153 0 : resReq.requiredScratchMemSize = transportBoundDataSize;
154 0 : resReq.requiredSubQueNum = tempResReq.streamNum - 1;
155 :
156 0 : return HcclResult::HCCL_SUCCESS;
157 0 : }
158 :
159 : template <typename AlgTopoMatch, typename InsAlgTemplate>
160 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalNumBlocks(u32& numBlocks, u64 dataSize, u32 numBlocksLimit){
161 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
162 0 : CHK_RET(CreateTemplates(algTemplate));
163 0 : algTemplate->CalNumBlocks(numBlocks, dataSize, numBlocksLimit);
164 0 : return HcclResult::HCCL_SUCCESS;
165 0 : }
166 : // HOST 侧算法入口
167 : template <typename AlgTopoMatch, typename InsAlgTemplate>
168 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(
169 : const RankGraph *rankGraph, const CollAlgOperator &op, const CollAlgParams ¶ms, InsQuePtr insQue)
170 : {
171 0 : HCCL_INFO("[InsV2AllReduceSoleExecutor][Orchestrate] Orchestrate HOST Start.");
172 0 : CHK_RET(Init(op, params, insQue));
173 0 : CHK_RET(InitCommInfo(rankGraph));
174 :
175 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
176 0 : CHK_RET(CreateTemplates(algTemplate));
177 :
178 0 : AlgTempResReq tempResReq;
179 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
180 :
181 0 : HCCL_DEBUG("[InsV2AllReduceSoleExecutor][Orchestrate] Rank[%d], template [%s], requiredQue Num [%u].",
182 : myRank_,
183 : algTemplate->Describe().c_str(),
184 : tempResReq.queNum);
185 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
186 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, tempResLinks_));
187 0 : CHK_RET(OrchestrateLoop(algTemplate));
188 0 : return HcclResult::HCCL_SUCCESS;
189 0 : }
190 :
191 : // AICPU 侧算法入口
192 : template <typename AlgTopoMatch, typename InsAlgTemplate>
193 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(const AlgTopoInfo &topoInfo,
194 : const CollAlgOperator &op, const CollAlgParams ¶ms, ConnectedLinkMgr *linkMgr, InsQuePtr insQue)
195 : {
196 0 : HCCL_INFO("[InsV2AllReduceSoleExecutor][Orchestrate] Orchestrate AICPU Start.");
197 0 : CHK_RET(Init(op, params, insQue));
198 0 : CHK_RET(InitCommInfo(topoInfo));
199 :
200 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
201 0 : CHK_RET(CreateTemplates(algTemplate));
202 :
203 0 : std::map<u32, u32>rank2PathNumMap;
204 0 : CHK_RET(SetPathNumMapByLinkMgrMultiLevel(linkMgr, virtRanks_, myRank_, rank2PathNumMap));
205 0 : algTemplate->setPathNumMap(rank2PathNumMap);
206 :
207 0 : AlgTempResReq tempResReq;
208 0 : CHK_RET(GetTemplateResRequest(linkMgr, algTemplate, tempResReq));
209 :
210 0 : HCCL_DEBUG("[InsV2AllReduceSoleExecutor][Orchestrate] Rank[%d], template [%s], requiredQue Num [%u].",
211 : myRank_, algTemplate->Describe().c_str(),
212 : tempResReq.queNum);
213 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
214 0 : CHK_RET(PrepResLinks(myRank_, tempResReq.links, linkMgr, tempResLinks_));
215 0 : CHK_RET(OrchestrateLoop(algTemplate));
216 0 : return HcclResult::HCCL_SUCCESS;
217 0 : }
218 :
219 : // 切分数据并调用 template
220 : template <typename AlgTopoMatch, typename InsAlgTemplate>
221 0 : HcclResult InsV2AllReduceSoleExecutor<AlgTopoMatch, InsAlgTemplate>::OrchestrateLoop(
222 : std::shared_ptr<InsAlgTemplate> algTemplate)
223 : {
224 0 : HCCL_INFO("[InsV2AllReduceSoleExecutor][OrchestrateOpbase] Start, template[%s]", algTemplate->Describe().c_str());
225 0 : u32 dataSizePerVolume = DataTypeSizeGet(dataType_);
226 0 : dataSize_ = dataCount_ * dataSizePerVolume;
227 :
228 0 : TemplateDataParams tempAlgParams;
229 0 : tempAlgParams.repeatNum = 1; // 不需要重复
230 0 : tempAlgParams.buffInfo.inBuffType = BufferType::INPUT;
231 0 : tempAlgParams.buffInfo.outBuffType = BufferType::OUTPUT;
232 0 : tempAlgParams.buffInfo.scratBuffType = BufferType::SCRATCH;
233 0 : tempAlgParams.inputRepeatStride = 0;
234 0 : tempAlgParams.outputRepeatStride = 0;
235 :
236 0 : TempFuncs tempFuncs;
237 0 : tempFuncs.opMode = opMode_;
238 : //template 中2D未适配
239 0 : tempFuncs.enableCounterNotify = false;
240 0 : tempFuncs.isForepart = true;
241 0 : tempFuncs.isBottom = true;
242 :
243 0 : u64 maxDataSizePerLoop = 0;
244 0 : u64 transportBoundDataSize = UB_MAX_DATA_SIZE; // algTemplate->CalcLoopMaxCount();
245 0 : u32 templateScratchMultiplier = algTemplate->CalcScratchMultiple(BufferType::INPUT, BufferType::OUTPUT);
246 0 : if (templateScratchMultiplier != 0) {
247 : //maxTmpMemSize_大小的buffer当前template仍然可以用,只是传入的待处理数据只有scratchBoundDataSize大小
248 0 : u64 scratchBoundDataSize = maxTmpMemSize_ / templateScratchMultiplier;
249 0 : maxDataSizePerLoop = std::min(transportBoundDataSize, scratchBoundDataSize);
250 : } else {
251 0 : maxDataSizePerLoop = transportBoundDataSize;
252 : }
253 :
254 : //如果有template融合可以多种取整策略融合: maxDataSizePerLoop / (MN2*dataSizePerVolume)*MN2;
255 0 : u64 maxDataCountPerLoop = maxDataSizePerLoop / dataSizePerVolume;
256 0 : HCCL_INFO("[InsV2AllReduceSoleExecutor][OrchestrateOpbase] maxDataCountPerLoop[%llu], maxDataSizePerLoop[%llu], "
257 : "transportBoundDataSize[%llu], templateScratchMultiplier[%llu]", maxDataCountPerLoop,
258 : maxDataSizePerLoop,
259 : transportBoundDataSize,
260 : templateScratchMultiplier);
261 0 : CHK_PRT_RET(maxDataCountPerLoop == 0,
262 : HCCL_ERROR("[InsV2AllReduceSoleExecutor][OrchestrateOpbase] maxDataCountPerLoop is 0, scratch buffer "
263 : "size:%u,maxDataSizePerLoop:%llu, dataSize:%llu", maxTmpMemSize_, maxDataSizePerLoop, dataSize_), HCCL_E_INTERNAL);
264 :
265 0 : u64 processedDataCount = 0;
266 0 : u64 loopTimes = dataCount_ / maxDataCountPerLoop + static_cast<u32>(dataCount_ % maxDataCountPerLoop != 0);
267 0 : for (u64 loop = 0; loop < loopTimes; loop++) {
268 0 : u64 currDataCount = (loop == loopTimes - 1) ? dataCount_ - processedDataCount : maxDataCountPerLoop;
269 :
270 0 : tempAlgParams.buffInfo.inBuffBaseOff = processedDataCount * dataSizePerVolume;
271 0 : tempAlgParams.buffInfo.outBuffBaseOff = processedDataCount * dataSizePerVolume;
272 0 : tempAlgParams.buffInfo.scratchBuffBaseOff = 0;
273 0 : tempAlgParams.buffInfo.scratchBuffSize = maxTmpMemSize_;
274 0 : tempAlgParams.sliceSize = currDataCount * dataSizePerVolume;
275 0 : tempAlgParams.tailSize = tempAlgParams.sliceSize;
276 0 : tempAlgParams.inputSliceStride = 0; // 输入数据仅有 1 个 slice, 不需要 stride
277 0 : tempAlgParams.outputSliceStride = 0; // 每张卡的数据间隔为算子输入大小
278 :
279 0 : CHK_RET(algTemplate->GenExtIns(tempFuncs, tempAlgParams, tempResLinks_, tempInsQue_));
280 0 : processedDataCount += currDataCount;
281 : }
282 :
283 0 : return HcclResult::HCCL_SUCCESS;
284 0 : }
285 :
286 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, InsAllReduceMesh1DOneShot, InsV2AllReduceSoleExecutor, TopoMatchMesh,
287 : InsTempAllReduceMesh1DOneShot);
288 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, InsAllReduceMesh2DTwoShot, InsV2AllReduceSoleExecutor, TopoMatchConcurrMesh,
289 : InsTempAllReduceMesh2DTwoShot);
290 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, InsAllReduceMesh1DTwoShot, InsV2AllReduceSoleExecutor, TopoMatchMesh,
291 : InsTempAllReduceMesh1DTwoShot);
292 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, InsAllReduceMesh1DTwoShotMeshChunk, InsV2AllReduceSoleExecutor, TopoMatchMesh,
293 : InsTempAllReduceMesh1DTwoShotMeshChunk);
294 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, InsAllReduceNHR, InsV2AllReduceSoleExecutor, TopoMatchNHR,
295 : InsTempAllReduceNHR);
296 : #ifndef CCL_KERNEL_AICPU
297 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, AivAllReduceMesh1DOneShot, InsV2AllReduceSoleExecutor, TopoMatchMesh,
298 : AivTempAllReduceMesh1DOneShot);
299 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, AivAllReduceMesh1DTwoShot, InsV2AllReduceSoleExecutor, TopoMatchMesh,
300 : AivTempAllReduceMesh1DTwoShot);
301 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, CcuAllReduceNHR1D, InsV2AllReduceSoleExecutor, TopoMatchMesh,
302 : CcuTempAllReduceNHRMem2Mem1D);
303 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, CcuAllReduceMeshMem2Mem1D, InsV2AllReduceSoleExecutor,
304 : TopoMatchMesh, CcuTempAllReduceMeshMem2Mem1D);
305 : #endif
306 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, InsAllReduceAicpuReduce, InsV2AllReduceSoleExecutor, TopoMatchMesh,
307 : InsTempAllReduceAicpuReduce);
308 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLREDUCE, InsAllReduceAicpuReduceMesh2D, InsV2AllReduceSoleExecutor, TopoMatchConcurrMesh,
309 : InsTempAllReduceAicpuReduceMesh2D);
310 :
311 : } // namespace Hccl
|