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