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