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_nhr.h"
14 : #include "topo_match_mesh.h"
15 : #include "topo_match_concurr_mesh.h"
16 : #include "ins_temp_reduce_scatter_mesh_1D.h"
17 : #include "ins_temp_reduce_scatter_mesh_1D_meshchunk.h"
18 : #include "ins_temp_reduce_scatter_mesh_2D.h"
19 : #include "ins_temp_reduce_scatter_nhr.h"
20 : #ifndef CCL_KERNEL_AICPU
21 : #include "aiv_temp_reduce_scatter_mesh_1D.h"
22 : #include "ccu_temp_reduce_scatter_nhr_1D_mem2mem.h"
23 : #include "ccu_temp_reduce_scatter_mesh_1D_2die.h"
24 : #endif
25 : #include "ins_v2_reduce_scatter_sole_executor.h"
26 : #include "ins_temp_reduce_scatter_aicpu_reduce.h"
27 : #include "ins_temp_reduce_scatter_aicpu_reduce_mesh_2D.h"
28 : #include "ccu_temp_reduce_scatter_mesh_1D_mem2mem.h"
29 :
30 : namespace Hccl {
31 : constexpr u64 MAX_OFFLOAD_SCRATCH_SIZE = 200 * 1024 * 1024; // 200M
32 : template <typename AlgTopoMatch, typename InsAlgTemplate>
33 0 : InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InsV2ReduceScatterSoleExecutor() : InsCollAlgBase()
34 0 : {}
35 :
36 : template <typename AlgTopoMatch, typename InsAlgTemplate>
37 0 : InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::~InsV2ReduceScatterSoleExecutor()
38 0 : {}
39 :
40 : template <typename AlgTopoMatch, typename InsAlgTemplate>
41 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const RankGraph *rankGraph)
42 : {
43 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
44 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
45 0 : return HcclResult::HCCL_SUCCESS;
46 0 : }
47 :
48 : template <typename AlgTopoMatch, typename InsAlgTemplate>
49 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const AlgTopoInfo &topoInfo)
50 : {
51 0 : CHK_PRT_RET(topoInfo.vTopo.empty(),
52 : HCCL_ERROR("[InsV2ReduceScatterSoleExecutor][InitCommInfo] vTopo size is invalid"), HCCL_E_PARA);
53 0 : CHK_PRT_RET(topoInfo.virtRankMap.empty(),
54 : HCCL_ERROR("[InsV2ReduceScatterSoleExecutor][InitCommInfo] virtRankMap size is invalid"), HCCL_E_PARA);
55 0 : CHK_PRT_RET(topoInfo.virtRanks.empty(),
56 : HCCL_ERROR("[InsV2ReduceScatterSoleExecutor][InitCommInfo] virtRanks size is invalid"), HCCL_E_PARA);
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 InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CreateTemplates(
65 : std::shared_ptr<InsAlgTemplate> &algTemplatePtr)
66 : {
67 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][CreateTemplates]");
68 0 : algTemplatePtr = std::make_shared<InsAlgTemplate>(myRank_, rankSize_, vTopo_, virtRankMap_);
69 0 : CHK_PTR_NULL(algTemplatePtr);
70 0 : algTemplatePtr->SetDmaMode(dmaMode_);
71 0 : algTemplatePtr->SetDataType(dataType_);
72 0 : algTemplatePtr->SetCollOp(op_);
73 0 : algTemplatePtr->InitReduceInfo(redOp_, dataType_);
74 0 : return HcclResult::HCCL_SUCCESS;
75 : }
76 :
77 : template <typename AlgTopoMatch, typename InsAlgTemplate>
78 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalNumBlocks(u32& numBlocks, u64 dataSize, u32 numBlocksLimit)
79 : {
80 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
81 0 : CHK_RET(CreateTemplates(algTemplate));
82 0 : CHK_RET(algTemplate->CalNumBlocks(numBlocks, dataSize, numBlocksLimit));
83 0 : return HcclResult::HCCL_SUCCESS;
84 0 : }
85 :
86 : // HOST 侧算法入口,将对应的instruction添加到指令队列中
87 : template <typename AlgTopoMatch, typename InsAlgTemplate>
88 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(const RankGraph *rankGraph,
89 : const CollAlgOperator &op, const CollAlgParams ¶ms, InsQuePtr insQue)
90 : {
91 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor][Orchestrate] Orchestrate host Start");
92 0 : CHK_RET(Init(op, params, insQue));
93 0 : CHK_RET(InitCommInfo(rankGraph));
94 0 : u32 dataSizePerVolume = DataTypeSizeGet(dataType_);
95 0 : dataSize_ = dataCount_ * dataSizePerVolume;
96 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
97 0 : CHK_RET(CreateTemplates(algTemplate));
98 :
99 0 : AlgTempResReq tempResReq;
100 0 : if (enableDetour_) {
101 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][Orchestrate] [%s] Rank[%d], CalcRes with detouring enabled.", __func__, myRank_);
102 0 : CHK_RET(algTemplate->CalcResDetour(rankGraph, tempResReq));
103 : } else {
104 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][Orchestrate] [%s] Rank[%d], CalcRes with detouring disabled.", __func__, myRank_);
105 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
106 : }
107 :
108 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][Orchestrate] Rank[%d], template [%s], requiredQue Num [%u].", myRank_,
109 : algTemplate->Describe().c_str(), tempResReq.queNum);
110 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
111 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, tempResLinks_));
112 0 : CHK_RET(OrchestrateLoop(algTemplate));
113 0 : return HcclResult::HCCL_SUCCESS;
114 0 : }
115 :
116 : template <typename AlgTopoMatch, typename InsAlgTemplate>
117 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(const AlgTopoInfo &topoInfo,
118 : const CollAlgOperator &op, const CollAlgParams ¶ms, ConnectedLinkMgr *linkMgr, InsQuePtr insQue)
119 : {
120 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor][Orchestrate] Orchestrate AICPU Start");
121 0 : CHK_RET(Init(op, params, insQue));
122 0 : CHK_RET(InitCommInfo(topoInfo));
123 0 : vTopo_ = topoInfo.vTopo[0]; // 本通信域内的通信平面
124 0 : virtRankMap_ = topoInfo.virtRankMap[0]; // 本通信域内的 rank 映射表
125 0 : virtRanks_ = topoInfo.virtRanks[0]; // 本通信域内的 rank 集合
126 0 : u32 dataSizePerVolume = DataTypeSizeGet(dataType_);
127 0 : dataSize_ = dataCount_ * dataSizePerVolume;
128 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
129 0 : CHK_RET(CreateTemplates(algTemplate));
130 :
131 0 : std::map<u32, u32>rank2PathNumMap;
132 0 : CHK_RET(SetPathNumMapByLinkMgrMultiLevel(linkMgr, virtRanks_, myRank_, rank2PathNumMap));
133 0 : algTemplate->setPathNumMap(rank2PathNumMap);
134 :
135 0 : AlgTempResReq tempResReq;
136 0 : if (enableDetour_) {
137 0 : CHK_RET(algTemplate->CalcResDetour(linkMgr, tempResReq));
138 : } else {
139 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
140 : }
141 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
142 0 : CHK_RET(PrepResLinks(myRank_, tempResReq.links, linkMgr, tempResLinks_));
143 0 : CHK_RET(OrchestrateLoop(algTemplate));
144 0 : return HcclResult::HCCL_SUCCESS;
145 0 : }
146 :
147 : // 单算子模式资源计算接口
148 : template <typename AlgTopoMatch, typename InsAlgTemplate>
149 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::OrchestrateLoop(
150 : std::shared_ptr<InsAlgTemplate> algTemplate)
151 : {
152 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor][OrchestrateOpbase] Start, template[%s]", algTemplate->Describe().c_str());
153 :
154 0 : TemplateDataParams tempAlgParams;
155 0 : tempAlgParams.buffInfo.inBuffType = BufferType::INPUT;
156 0 : tempAlgParams.buffInfo.outBuffType = BufferType::OUTPUT;
157 0 : tempAlgParams.buffInfo.scratBuffType = BufferType::SCRATCH;
158 :
159 0 : u64 maxDataSizePerLoop = 0;
160 0 : u64 transportBoundDataSize = UB_MAX_DATA_SIZE;
161 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor]maxTmpMemSize_ [%u]", maxTmpMemSize_);
162 0 : u32 templateScratchMultiplier =
163 0 : algTemplate->CalcScratchMultiple(tempAlgParams.buffInfo.inBuffType, tempAlgParams.buffInfo.outBuffType);
164 0 : if (templateScratchMultiplier != 0) {
165 : // reduce 时 保障scratch上偏移按照dataTypeSize_对齐
166 0 : u64 scratchBoundDataSize = (maxTmpMemSize_ / templateScratchMultiplier) / dataTypeSize_ * dataTypeSize_;
167 0 : maxDataSizePerLoop = min(transportBoundDataSize, scratchBoundDataSize);
168 : } else {
169 0 : maxDataSizePerLoop = transportBoundDataSize;
170 : }
171 0 : u64 maxDataCountPerLoop = maxDataSizePerLoop / dataTypeSize_; // 单次循环处理的数据量大小,同时会处理两片数据
172 0 : HCCL_INFO(
173 : "[InsV2ReduceScatterSoleExecutor][OrchestrateOpbase] maxDataCountPerLoop[%llu], maxDataSizePerLoop[%llu], "
174 : "transportBoundDataSize[%llu], templateScratchMultiplier[%llu]",
175 : maxDataCountPerLoop, maxDataSizePerLoop, transportBoundDataSize, templateScratchMultiplier);
176 0 : CHK_PRT_RET(maxDataCountPerLoop == 0,
177 : HCCL_ERROR("[InsV2ReduceScatterSoleExecutor][OrchestrateOpbase] maxDataCountPerLoop is 0"), HCCL_E_INTERNAL);
178 : // 这里处理的数据量,是单次循环所处理的总数据量,包括两个数据片,每一半stream处理一个数据片
179 0 : TempFuncs tempFuncs;
180 0 : tempFuncs.isForepart = true;
181 0 : tempFuncs.opMode = opMode_;
182 0 : tempFuncs.enableCounterNotify = IsEnableCounterNotify();
183 0 : tempFuncs.isBottom = true;
184 : // maxDataCountPerLoop是一次循环所处理的一片数据量大小
185 0 : u64 processedDataCount = 0;
186 0 : u64 allDataCountPerLoop = maxDataCountPerLoop;
187 : #ifdef CCL_KERNEL_AICPU
188 : if (vTopo_.size() > 1) { // aicpu mesh 2d
189 : allDataCountPerLoop = maxDataCountPerLoop * 2;
190 : }
191 : #endif
192 0 : u64 loopTimes = dataCount_ / allDataCountPerLoop + static_cast<u64>(dataCount_ % allDataCountPerLoop != 0);
193 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor]allDataCountPerLoop [%u],dataCount_ [%u],loopTimes [%u]", allDataCountPerLoop, dataCount_, loopTimes);
194 0 : for (u64 loop = 0; loop < loopTimes; loop++) {
195 0 : u64 currDataCount = (loop == loopTimes - 1) ? dataCount_ - processedDataCount : allDataCountPerLoop;
196 0 : tempAlgParams.buffInfo.inBuffBaseOff = processedDataCount * dataTypeSize_;
197 0 : tempAlgParams.buffInfo.outBuffBaseOff = processedDataCount * dataTypeSize_;
198 0 : tempAlgParams.buffInfo.scratchBuffBaseOff = 0;
199 :
200 0 : tempAlgParams.sliceSize = currDataCount * dataTypeSize_; // 这里是单次循环处理的两片数据的大小
201 0 : tempAlgParams.tailSize = tempAlgParams.sliceSize;
202 : // 这里的stride当成传统意义上的sreide间隔
203 0 : tempAlgParams.inputSliceStride = dataSize_; // 如果是输入,偏移是算子的output datasize
204 0 : tempAlgParams.outputSliceStride = maxDataSizePerLoop; // 如果是scratchbuffer,偏移是单次循环所处理的最大数据量
205 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor] loop [%u] tempAlgParams.inputSliceStride [%u],tempAlgParams.outputSliceStride [%u] tempAlgParams.sliceSize [%u]",
206 : loop, tempAlgParams.inputSliceStride, tempAlgParams.outputSliceStride, tempAlgParams.sliceSize);
207 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor] loop [%u] tempAlgParams.buffInfo.inBuffBaseOff [%u],tempAlgParams.buffInfo.outBuffBaseOff [%u]",
208 : loop, tempAlgParams.buffInfo.inBuffBaseOff, tempAlgParams.buffInfo.outBuffBaseOff);
209 : // 不需要重复
210 0 : tempAlgParams.repeatNum = 1;
211 0 : tempAlgParams.inputRepeatStride = 0;
212 0 : tempAlgParams.outputRepeatStride = 0;
213 :
214 0 : CHK_RET(algTemplate->GenExtIns(tempFuncs, tempAlgParams, tempResLinks_, tempInsQue_));
215 0 : processedDataCount += currDataCount;
216 : }
217 :
218 0 : return HcclResult::HCCL_SUCCESS;
219 0 : }
220 :
221 : template <typename AlgTopoMatch, typename InsAlgTemplate>
222 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcRes(const RankGraph *rankGraph,
223 : CollAlgResReq &algResReq)
224 : {
225 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
226 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
227 0 : algResReq.topoInfo.UpdateSingleLevelTopo(virtRanks_, virtRankMap_, vTopo_);
228 :
229 0 : InsAlgTemplate tempAlg(myRank_, rankSize_, vTopo_, virtRankMap_);
230 :
231 : // 通过判断哪层通信域能有到所有remoteRank的path,判断当前算法跑在哪一层
232 0 : std::map<u32, u32>rank2PathNumMap;
233 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor] CalcRes SetPathNumMap");
234 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
235 0 : tempAlg.setPathNumMap(rank2PathNumMap);
236 :
237 0 : AlgTempResReq tempResReq;
238 0 : if (enableDetour_) {
239 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][CalcRes] [%s] Rank[%d], CalcRes with detouring enabled.", __func__, myRank_);
240 0 : CHK_RET(tempAlg.CalcResDetour(rankGraph, tempResReq));
241 : } else {
242 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][CalcRes] [%s] Rank[%d], CalcRes with detouring disabled.", __func__, myRank_);
243 0 : CHK_RET(tempAlg.CalcRes(tempResReq));
244 : }
245 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, tempResReq.links, algResReq.levelRankPairs));
246 0 : algResReq.primQueueNum = tempResReq.streamNum;
247 0 : algResReq.queueNotifys = tempResReq.queNotifys;
248 0 : algResReq.localWaitGroupCntNotify = tempResReq.localWaitGroupCntNotify;
249 0 : algResReq.localBcastPostCntNotify = tempResReq.localBcastPostCntNotify;
250 0 : HCCL_DEBUG("[%s] Rank[%d], requiredQueNum [%u].", __func__, myRank_, algResReq.primQueueNum);
251 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, algResReq.links));
252 :
253 0 : return HcclResult::HCCL_SUCCESS;
254 0 : }
255 :
256 : template <typename AlgTopoMatch, typename InsAlgTemplate>
257 0 : HcclResult InsV2ReduceScatterSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcResOffload(const RankGraph *rankGraph,
258 : const u64 &dataSize, CollOffloadOpResReq &resReq)
259 : {
260 : (void)dataSize;
261 :
262 : // Topo Match
263 0 : CHK_RET(InitCommInfo(rankGraph));
264 :
265 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
266 0 : CHK_RET(CreateTemplates(algTemplate));
267 :
268 : // 通过判断哪层通信域能有到所有remoteRank的path,判断当前算法跑在哪一层
269 0 : std::map<u32, u32>rank2PathNumMap;
270 0 : HCCL_INFO("[InsV2ReduceScatterSoleExecutor] CalcResOffload SetPathNumMap");
271 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
272 0 : algTemplate->setPathNumMap(rank2PathNumMap);
273 :
274 0 : AlgTempResReq tempResReq;
275 0 : if (enableDetour_) {
276 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][CalcResOffload] [%s] Rank[%d], CalcRes with detouring enabled.", __func__, myRank_);
277 0 : CHK_RET(algTemplate->CalcResDetour(rankGraph, tempResReq));
278 : } else {
279 0 : HCCL_DEBUG("[InsV2ReduceScatterSoleExecutor][CalcResOffload] [%s] Rank[%d], CalcRes with detouring disabled.", __func__, myRank_);
280 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
281 : }
282 0 : resReq.requiredScratchMemSize = UB_MAX_DATA_SIZE;
283 0 : resReq.requiredSubQueNum = tempResReq.streamNum - 1;
284 :
285 0 : return HcclResult::HCCL_SUCCESS;
286 0 : }
287 :
288 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, InsReduceScatterMesh1D, InsV2ReduceScatterSoleExecutor, TopoMatchMesh,
289 : InsTempReduceScatterMesh1D);
290 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, InsReduceScatterMesh1DMeshChunk, InsV2ReduceScatterSoleExecutor, TopoMatchMesh,
291 : InsTempReduceScatterMesh1DMeshChunk);
292 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, InsReduceScatterNHR, InsV2ReduceScatterSoleExecutor, TopoMatchNHR,
293 : InsTempReduceScatterNHR);
294 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, InsReduceScatterMesh2D, InsV2ReduceScatterSoleExecutor, TopoMatchConcurrMesh,
295 : InsTempReduceScatterMesh2D);
296 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, InsReduceScatterAicpuReduce, InsV2ReduceScatterSoleExecutor, TopoMatchMesh,
297 : InsTempReduceScatterAicpuReduce);
298 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, InsReduceScatterAicpuReduceMesh2D, InsV2ReduceScatterSoleExecutor, TopoMatchConcurrMesh,
299 : InsTempReduceScatterAicpuReduceMesh2D);
300 : #ifndef CCL_KERNEL_AICPU
301 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, AivReduceScatterMesh1D, InsV2ReduceScatterSoleExecutor, TopoMatchMesh,
302 : AivTempReduceScatterMesh1D);
303 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, CcuReduceScatterMeshMem2Mem1D, InsV2ReduceScatterSoleExecutor, TopoMatchMesh,
304 : CcuTempReduceScatterMeshMem2Mem1D);
305 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, CcuReduceScatterNHR1DMem2Mem, InsV2ReduceScatterSoleExecutor,
306 : TopoMatchMesh, CcuTempReduceScatterNHR1DMem2Mem);
307 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTER, CcuReduceScatterMesh1D2Die, InsV2ReduceScatterSoleExecutor,
308 : TopoMatchMesh, CcuTempReduceScatterMesh1D2Die);
309 : #endif
310 : } // namespace Hccl
|