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