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 "ins_v2_all_to_all_sole_executor.h"
12 : #include "log.h"
13 : #include "ins_coll_alg_registry.h"
14 : #include "topo_match_mesh.h"
15 : #include "topo_match_concurr_mesh.h"
16 : #include "ins_temp_all_to_all_mesh_2D.h"
17 : #ifndef CCL_KERNEL_AICPU
18 : #include "aiv_temp_all_to_all_mesh_1D.h"
19 : #include "ccu_temp_all_to_all_mesh_1D_2Die.h"
20 : #endif
21 :
22 : namespace Hccl {
23 : constexpr u64 MAX_OFFLOAD_SCRATCH_SIZE = 200 * 1024 * 1024; // 200M
24 :
25 : template <typename AlgTopoMatch, typename InsAlgTemplate>
26 0 : InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InsV2AlltoAllSoleExecutor() : InsCollAlgBase()
27 0 : {}
28 :
29 : template <typename AlgTopoMatch, typename InsAlgTemplate>
30 0 : InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::~InsV2AlltoAllSoleExecutor()
31 0 : {}
32 :
33 : template <typename AlgTopoMatch, typename InsAlgTemplate>
34 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitParams(const CollAlgOperator &op,
35 : const CollAlgParams ¶ms)
36 : {
37 0 : op_ = op;
38 0 : opMode_ = params.opMode;
39 0 : maxTmpMemSize_ = params.maxTmpMemSize;
40 0 : CHK_PRT_RET((maxTmpMemSize_ == 0),
41 : HCCL_ERROR("[InitParams] maxTmpMemSize equals to zero."),
42 : HcclResult::HCCL_E_PARA);
43 :
44 0 : CHK_PRT_RET((op.opType != OpType::ALLTOALL),
45 : HCCL_ERROR("[InitParams] opType is invalid."),
46 : HcclResult::HCCL_E_PARA);
47 :
48 0 : dataType_ = op.all2AllDataDes.sendType;
49 0 : dataCount_ = op.all2AllDataDes.sendCount; // 本卡数据量/rankSize
50 0 : outputDataType_ = op.all2AllDataDes.sendType;
51 0 : dataTypeSize_ = DataTypeSizeGet(dataType_);
52 0 : dataSize_ = dataCount_ * dataTypeSize_;
53 :
54 0 : HCCL_DEBUG("dataType_ is [%u], dataCount_ is [%u]", dataType_, dataCount_);
55 :
56 0 : CHK_PRT_RET(InitOpInfo(op, opType_, redOp_, root_),
57 : HCCL_ERROR("[InitParams] unable to init OpInfo."),
58 : HcclResult::HCCL_E_PARA);
59 0 : return HcclResult::HCCL_SUCCESS;
60 : }
61 :
62 : template <typename AlgTopoMatch, typename InsAlgTemplate>
63 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const RankGraph *rankGraph)
64 : {
65 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
66 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
67 0 : return HcclResult::HCCL_SUCCESS;
68 0 : }
69 :
70 : template <typename AlgTopoMatch, typename InsAlgTemplate>
71 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const AlgTopoInfo &topoInfo)
72 : {
73 0 : CHK_PRT_RET((topoInfo.vTopo.empty()),
74 : HCCL_ERROR("[InsV2AlltoAllSoleExecutor][InitCommInfo], topoInfo.vTopo is empty"), HcclResult::HCCL_E_PARA);
75 0 : CHK_PRT_RET((topoInfo.virtRankMap.empty()),
76 : HCCL_ERROR("[InsV2AlltoAllSoleExecutor][InitCommInfo], topoInfo.virtRankMap is empty"), HcclResult::HCCL_E_PARA);
77 0 : CHK_PRT_RET((topoInfo.virtRanks.empty()),
78 : HCCL_ERROR("[InsV2AlltoAllSoleExecutor][InitCommInfo], topoInfo.virtRanks is empty"), HcclResult::HCCL_E_PARA);
79 :
80 0 : vTopo_ = topoInfo.vTopo[0]; // 本通信域内的通信平面
81 0 : virtRankMap_ = topoInfo.virtRankMap[0]; // 本通信域内的 rank 映射表
82 0 : virtRanks_ = topoInfo.virtRanks[0]; // 本通信域内的 rank 集合
83 0 : return HcclResult::HCCL_SUCCESS;
84 : }
85 :
86 : template <typename AlgTopoMatch, typename InsAlgTemplate>
87 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CreateTemplates(
88 : std::shared_ptr<InsAlgTemplate> &algTemplatePtr)
89 : {
90 0 : HCCL_DEBUG("[InsV2AlltoAllSoleExecutor][CreateTemplates]");
91 0 : algTemplatePtr = std::make_shared<InsAlgTemplate>(myRank_, rankSize_, vTopo_, virtRankMap_);
92 0 : CHK_PTR_NULL(algTemplatePtr); // 检查是否成功分配内存
93 0 : algTemplatePtr->SetDmaMode(dmaMode_);
94 0 : algTemplatePtr->SetDataType(dataType_);
95 0 : algTemplatePtr->SetCollOp(op_);
96 0 : return HcclResult::HCCL_SUCCESS;
97 : }
98 :
99 : template <typename AlgTopoMatch, typename InsAlgTemplate>
100 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::GetTemplateResRequest(
101 : const RankGraph *rankGraph, std::shared_ptr<InsAlgTemplate> &algTemplate, AlgTempResReq &tempResReq) const
102 : {
103 0 : if (enableDetour_) {
104 0 : CHK_RET(algTemplate->CalcResDetour(rankGraph, tempResReq));
105 : } else {
106 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
107 : }
108 :
109 0 : return HcclResult::HCCL_SUCCESS;
110 : }
111 :
112 : template <typename AlgTopoMatch, typename InsAlgTemplate>
113 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::GetTemplateResRequest(
114 : ConnectedLinkMgr *linkMgr, std::shared_ptr<InsAlgTemplate> &algTemplate, AlgTempResReq &tempResReq) const
115 : {
116 0 : if (enableDetour_) {
117 0 : CHK_RET(algTemplate->CalcResDetour(linkMgr, tempResReq));
118 : } else {
119 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
120 : }
121 0 : return HcclResult::HCCL_SUCCESS;
122 : }
123 :
124 : template <typename AlgTopoMatch, typename InsAlgTemplate>
125 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalNumBlocks(
126 : u32& numBlocks, u64 dataSize, u32 numBlocksLimit)
127 : {
128 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
129 0 : CHK_RET(CreateTemplates(algTemplate));
130 0 : CHK_RET(algTemplate->CalNumBlocks(numBlocks, dataSize, numBlocksLimit));
131 0 : return HcclResult::HCCL_SUCCESS;
132 0 : }
133 :
134 : // HOST 侧算法入口
135 : template <typename AlgTopoMatch, typename InsAlgTemplate>
136 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(
137 : const RankGraph *rankGraph, const CollAlgOperator &op, const CollAlgParams ¶ms, InsQuePtr insQue)
138 : {
139 0 : HCCL_DEBUG("[InsV2AlltoAllSoleExecutor][Orchestrate] Orchestrate HOST Start");
140 0 : CHK_RET(Init(op, params, insQue));
141 0 : CHK_RET(InitCommInfo(rankGraph));
142 :
143 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
144 0 : CHK_RET(CreateTemplates(algTemplate));
145 :
146 0 : AlgTempResReq tempResReq;
147 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
148 :
149 0 : HCCL_DEBUG("[InsV2AlltoAllSoleExecutor][Orchestrate] Rank[%d], template [%s], requiredQue Num [%u].",
150 : myRank_,
151 : algTemplate->Describe().c_str(),
152 : tempResReq.queNum);
153 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
154 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, tempResLinks_));
155 :
156 0 : CHK_RET(OrchestrateLoop(algTemplate));
157 0 : HCCL_DEBUG("[InsV2AlltoAllSoleExecutor][Orchestrate] Orchestrate HOST End");
158 0 : return HcclResult::HCCL_SUCCESS;
159 0 : }
160 :
161 : // AICPU 侧算法入口
162 : template <typename AlgTopoMatch, typename InsAlgTemplate>
163 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(const AlgTopoInfo &topoInfo,
164 : const CollAlgOperator &op, const CollAlgParams ¶ms, ConnectedLinkMgr *linkMgr, InsQuePtr insQue)
165 : {
166 0 : CHK_RET(Init(op, params, insQue));
167 0 : CHK_RET(InitCommInfo(topoInfo));
168 :
169 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
170 0 : CHK_RET(CreateTemplates(algTemplate));
171 :
172 0 : AlgTempResReq tempResReq;
173 0 : CHK_RET(GetTemplateResRequest(linkMgr, algTemplate, tempResReq));
174 :
175 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
176 0 : CHK_RET(PrepResLinks(myRank_, tempResReq.links, linkMgr, tempResLinks_));
177 :
178 0 : CHK_RET(OrchestrateLoop(algTemplate));
179 0 : return HcclResult::HCCL_SUCCESS;
180 0 : }
181 :
182 : // 切分数据并调用 template
183 : template <typename AlgTopoMatch, typename InsAlgTemplate>
184 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::OrchestrateLoop(
185 : std::shared_ptr<InsAlgTemplate> algTemplate)
186 : {
187 0 : u32 dataSizePerVolume = DataTypeSizeGet(dataType_);
188 0 : HCCL_INFO("[InsV2AlltoAllSoleExecutor][Orchestrate] Start, template[%s]", algTemplate->Describe().c_str());
189 0 : dataSize_ = dataCount_ * dataSizePerVolume;
190 :
191 0 : TemplateDataParams tempAlgParams;
192 0 : tempAlgParams.repeatNum = 1; // 不需要重复
193 0 : tempAlgParams.inputRepeatStride = 0;
194 0 : tempAlgParams.outputRepeatStride = 0;
195 0 : tempAlgParams.buffInfo.outBuffType = BufferType::OUTPUT;
196 0 : tempAlgParams.buffInfo.scratBuffType = BufferType::SCRATCH;
197 0 : tempAlgParams.buffInfo.inBuffType = BufferType::INPUT;
198 :
199 0 : TempFuncs tempFuncs;
200 0 : tempFuncs.opMode = opMode_;
201 0 : tempFuncs.enableCounterNotify = IsEnableCounterNotify();
202 0 : tempFuncs.isBottom = true;
203 0 : tempFuncs.isForepart = true;
204 :
205 0 : u64 maxDataSizePerLoop = 0;
206 0 : u64 transportBoundDataSize_ = UB_MAX_DATA_SIZE;
207 0 : u32 templateScratchMultiplier = algTemplate->CalcScratchMultiple(BufferType::INPUT, BufferType::OUTPUT);
208 0 : if (templateScratchMultiplier != 0) {
209 0 : u64 scratchBoundDataSize = maxTmpMemSize_ / templateScratchMultiplier;
210 0 : maxDataSizePerLoop = std::min(transportBoundDataSize_, scratchBoundDataSize);
211 : } else {
212 0 : maxDataSizePerLoop = transportBoundDataSize_;
213 : }
214 :
215 : // 先将cclBuffer切块,看每一块大小
216 0 : u64 scratchDataCountPerLoopPerRank = maxDataSizePerLoop / dataTypeSize_ / rankSize_;
217 0 : HCCL_DEBUG("[InsV2AlltoAllSoleExecutor][OrchestrateLoop] maxTmpMemSize_[%llu], templateScratchMultiplier[%llu], maxDataSizePerLoop[%llu], "
218 : "transportBoundDataSize_[%llu], scratchDataCountPerLoopPerRank[%llu]",
219 : maxTmpMemSize_,
220 : templateScratchMultiplier,
221 : maxDataSizePerLoop,
222 : transportBoundDataSize_,
223 : scratchDataCountPerLoopPerRank);
224 0 : CHK_PRT_RET(scratchDataCountPerLoopPerRank == 0,
225 : HCCL_ERROR("[InsV2AlltoAllSoleExecutor][OrchestrateLoop] scratchDataCountPerLoopPerRank is 0"),
226 : HCCL_E_INTERNAL);
227 :
228 : // 将usrIn数据切块,看每一块大小,这里要保障是能整除的
229 0 : u64 dataCountPerRank = dataCount_;
230 0 : u64 allToAllProcessedDataCount = 0;
231 0 : u64 loopTimes = dataCountPerRank / scratchDataCountPerLoopPerRank +
232 0 : static_cast<u64>(dataCountPerRank % scratchDataCountPerLoopPerRank != 0);
233 0 : for (u64 loop = 0; loop < loopTimes; loop++) {
234 0 : u64 currDataCount = (loop == loopTimes - 1) ? dataCountPerRank - allToAllProcessedDataCount : scratchDataCountPerLoopPerRank;
235 :
236 0 : tempAlgParams.buffInfo.inBuffBaseOff = allToAllProcessedDataCount * dataTypeSize_;
237 0 : tempAlgParams.buffInfo.outBuffBaseOff = allToAllProcessedDataCount * dataTypeSize_;
238 0 : tempAlgParams.buffInfo.scratchBuffBaseOff = 0;
239 0 : tempAlgParams.sliceSize = currDataCount * dataTypeSize_; // 这里就是 cclbuf 一块数据的大小
240 0 : tempAlgParams.inputSliceStride = dataCountPerRank * dataTypeSize_;
241 0 : tempAlgParams.outputSliceStride = dataCountPerRank * dataTypeSize_;
242 :
243 0 : CHK_RET(algTemplate->GenExtIns(tempFuncs, tempAlgParams, tempResLinks_, tempInsQue_));
244 0 : allToAllProcessedDataCount += currDataCount;
245 : }
246 :
247 0 : HCCL_INFO("[InsV2AlltoAllSoleExecutor][Orchestrate] End, template[%s]", algTemplate->Describe().c_str());
248 :
249 0 : return HcclResult::HCCL_SUCCESS;
250 0 : }
251 :
252 : template <typename AlgTopoMatch, typename InsAlgTemplate>
253 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcRes(
254 : const RankGraph *rankGraph, CollAlgResReq &algResReq)
255 : {
256 : // Topo Match
257 0 : CHK_RET(InitCommInfo(rankGraph));
258 :
259 : // instantiate a template
260 0 : InsAlgTemplate tempAlg(myRank_, rankSize_, vTopo_, virtRankMap_);
261 :
262 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
263 0 : CHK_RET(CreateTemplates(algTemplate));
264 :
265 0 : AlgTempResReq tempResReq;
266 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
267 :
268 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, tempResReq.links, algResReq.levelRankPairs));
269 0 : algResReq.primQueueNum = tempResReq.streamNum;
270 0 : algResReq.queueNotifys = tempResReq.queNotifys;
271 0 : algResReq.topoInfo.UpdateSingleLevelTopo(virtRanks_, virtRankMap_, vTopo_);
272 0 : algResReq.localBcastPostCntNotify = tempResReq.localBcastPostCntNotify;
273 0 : algResReq.localWaitGroupCntNotify = tempResReq.localWaitGroupCntNotify;
274 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, algResReq.links));
275 :
276 0 : return HcclResult::HCCL_SUCCESS;
277 0 : }
278 :
279 : template <typename AlgTopoMatch, typename InsAlgTemplate>
280 0 : HcclResult InsV2AlltoAllSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcResOffload(
281 : const RankGraph *rankGraph, const u64 &dataSize, CollOffloadOpResReq &resReq)
282 : {
283 : (void)dataSize;
284 : // Topo Match
285 0 : CHK_RET(InitCommInfo(rankGraph));
286 :
287 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
288 0 : CHK_RET(CreateTemplates(algTemplate));
289 :
290 0 : AlgTempResReq tempResReq;
291 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
292 0 : resReq.requiredScratchMemSize = MAX_OFFLOAD_SCRATCH_SIZE; // 最大 200M
293 0 : resReq.requiredSubQueNum = tempResReq.streamNum - 1;
294 :
295 0 : return HcclResult::HCCL_SUCCESS;
296 0 : }
297 :
298 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLTOALL, InsAlltoAllMesh2D, InsV2AlltoAllSoleExecutor, TopoMatchConcurrMesh,
299 : InsTempAlltoAllMesh2D);
300 : #ifndef CCL_KERNEL_AICPU
301 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLTOALL, AivAlltoAllMesh1D, InsV2AlltoAllSoleExecutor, TopoMatchMesh,
302 : AivTempAlltoAllMesh1D);
303 : INS_REGISTER_IMPL_BY_TEMP(OpType::ALLTOALL, CcuAlltoAllMesh1D2Die, InsV2AlltoAllSoleExecutor, TopoMatchMesh,
304 : CcuTempAllToAllMesh1D2Die);
305 : #endif
306 : } // namespace Hccl
|