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_mesh.h"
14 : #include "ins_v2_reduce_scatter_v_sole_executor.h"
15 :
16 : #ifndef CCL_KERNEL_AICPU
17 : #include "ccu_temp_reduce_scatter_v_mesh_1D.h"
18 : #include "ccu_temp_reduce_scatter_v_mesh_1D_mem2mem.h"
19 : #endif
20 :
21 : namespace Hccl {
22 : template <typename AlgTopoMatch, typename InsAlgTemplate>
23 0 : InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InsV2ReduceScatterVSoleExecutor() : InsCollAlgBase()
24 0 : {}
25 :
26 : template <typename AlgTopoMatch, typename InsAlgTemplate>
27 0 : InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::~InsV2ReduceScatterVSoleExecutor()
28 0 : {}
29 :
30 : template <typename AlgTopoMatch, typename InsAlgTemplate>
31 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(
32 : const RankGraph *rankGraph)
33 : {
34 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
35 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
36 0 : return HcclResult::HCCL_SUCCESS;
37 0 : }
38 :
39 : template <typename AlgTopoMatch, typename InsAlgTemplate>
40 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const AlgTopoInfo &topoInfo)
41 : {
42 0 : CHK_PRT_RET(topoInfo.vTopo.empty(),
43 : HCCL_ERROR("[InsV2ReduceSoleExecutor][InitCommInfo] vTopo size is invalid"),
44 : HCCL_E_PARA);
45 0 : CHK_PRT_RET(topoInfo.virtRankMap.empty(),
46 : HCCL_ERROR("[InsV2ReduceSoleExecutor][InitCommInfo] virtRankMap size is invalid"),
47 : HCCL_E_PARA);
48 0 : CHK_PRT_RET(topoInfo.virtRanks.empty(),
49 : HCCL_ERROR("[InsV2ReduceSoleExecutor][InitCommInfo] virtRanks size is invalid"),
50 : HCCL_E_PARA);
51 0 : vTopo_ = topoInfo.vTopo[0]; // 本通信域内的通信平面
52 0 : virtRankMap_ = topoInfo.virtRankMap[0]; // 本通信域内的 rank 映射表
53 0 : virtRanks_ = topoInfo.virtRanks[0]; // 本通信域内的 rank 集合
54 0 : return HcclResult::HCCL_SUCCESS;
55 : }
56 :
57 : template <typename AlgTopoMatch, typename InsAlgTemplate>
58 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CreateTemplates(
59 : std::shared_ptr<InsAlgTemplate> &algTemplatePtr)
60 : {
61 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][CreateTemplates]");
62 0 : algTemplatePtr = std::make_shared<InsAlgTemplate>(myRank_, rankSize_, vTopo_, virtRankMap_);
63 0 : CHK_PTR_NULL(algTemplatePtr);
64 0 : algTemplatePtr->SetDmaMode(dmaMode_);
65 0 : algTemplatePtr->SetDataType(dataType_);
66 0 : algTemplatePtr->SetCollOp(op_);
67 0 : algTemplatePtr->InitReduceInfo(redOp_, dataType_);
68 0 : return HcclResult::HCCL_SUCCESS;
69 : }
70 :
71 : // HOST 侧算法入口,将对应的 instruction 添加到指令队列中
72 : template <typename AlgTopoMatch, typename InsAlgTemplate>
73 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(
74 : const RankGraph *rankGraph, const CollAlgOperator &op, const CollAlgParams ¶ms, InsQuePtr insQue)
75 : {
76 0 : HCCL_INFO("[InsV2ReduceScatterVSoleExecutor][Orchestrate] Orchestrate host Start");
77 :
78 0 : CHK_RET(Init(op, params, insQue));
79 0 : CHK_RET(InitCommInfo(rankGraph));
80 0 : u32 dataSizePerVolume = DataTypeSizeGet(dataType_);
81 0 : dataSize_ = dataCount_ * dataSizePerVolume;
82 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
83 0 : CHK_RET(CreateTemplates(algTemplate));
84 :
85 0 : AlgTempResReq tempResReq;
86 0 : if (enableDetour_) {
87 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][Orchestrate] [%s] Rank[%d], CalcRes with detouring enabled.", __func__, myRank_);
88 0 : CHK_RET(algTemplate->CalcResDetour(rankGraph, tempResReq));
89 : } else {
90 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][Orchestrate] [%s] Rank[%d], CalcRes with detouring disabled.", __func__, myRank_);
91 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
92 : }
93 :
94 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][Orchestrate] Rank[%d], template [%s], requiredQue Num [%u].",
95 : myRank_,
96 : algTemplate->Describe().c_str(),
97 : tempResReq.queNum);
98 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
99 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, tempResLinks_));
100 0 : CHK_RET(OrchestrateLoop(algTemplate));
101 0 : return HcclResult::HCCL_SUCCESS;
102 0 : }
103 :
104 : template <typename AlgTopoMatch, typename InsAlgTemplate>
105 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(const AlgTopoInfo &topoInfo,
106 : const CollAlgOperator &op, const CollAlgParams ¶ms, ConnectedLinkMgr *linkMgr, InsQuePtr insQue)
107 : {
108 0 : HCCL_INFO("[InsV2ReduceScatterVSoleExecutor][Orchestrate] Orchestrate AICPU Start");
109 0 : CHK_RET(Init(op, params, insQue));
110 0 : CHK_RET(InitCommInfo(topoInfo));
111 0 : u32 dataSizePerVolume = DataTypeSizeGet(dataType_);
112 0 : dataSize_ = dataCount_ * dataSizePerVolume;
113 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
114 0 : CHK_RET(CreateTemplates(algTemplate));
115 0 : AlgTempResReq tempResReq;
116 0 : if (enableDetour_) {
117 0 : CHK_RET(algTemplate->CalcResDetour(linkMgr, tempResReq));
118 : } else {
119 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
120 : }
121 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][Orchestrate] Rank[%d], template [%s], requiredQue Num [%u].",
122 : myRank_,
123 : algTemplate->Describe().c_str(),
124 : tempResReq.queNum);
125 0 : CHK_RET(InitQueue(tempResReq.queNum, tempInsQue_));
126 0 : CHK_RET(PrepResLinks(myRank_, tempResReq.links, linkMgr, tempResLinks_));
127 0 : CHK_RET(OrchestrateLoop(algTemplate));
128 0 : return HcclResult::HCCL_SUCCESS;
129 0 : }
130 :
131 : // 单算子模式资源计算接口
132 : template <typename AlgTopoMatch, typename InsAlgTemplate>
133 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::OrchestrateLoop(
134 : std::shared_ptr<InsAlgTemplate> algTemplate)
135 : {
136 0 : HCCL_INFO(
137 : "[InsV2ReduceScatterVSoleExecutor][OrchestrateOpbase] Start, template[%s]", algTemplate->Describe().c_str());
138 :
139 0 : TemplateDataParams tempAlgParams;
140 0 : tempAlgParams.buffInfo.inBuffType = BufferType::INPUT;
141 0 : tempAlgParams.buffInfo.outBuffType = BufferType::OUTPUT;
142 0 : tempAlgParams.buffInfo.scratBuffType = BufferType::SCRATCH;
143 0 : tempAlgParams.repeatNum = 1; // 不需要重复
144 0 : tempAlgParams.inputRepeatStride = 0;
145 0 : tempAlgParams.outputRepeatStride = 0;
146 :
147 0 : TempFuncs tempFuncs;
148 0 : tempFuncs.opMode = opMode_;
149 0 : tempFuncs.isForepart = true;
150 0 : tempFuncs.isBottom = true;
151 0 : tempFuncs.enableCounterNotify = IsEnableCounterNotify();
152 :
153 0 : u64 maxDataSizePerLoop = 0;
154 : u64 transportBoundDataSize;
155 0 : CHK_RET(algTemplate->GetMaxTransPortDataSize(transportBoundDataSize));
156 0 : u64 templateScratchMultiplier =
157 0 : algTemplate->CalcScratchMultiple(tempAlgParams.buffInfo.inBuffType, tempAlgParams.buffInfo.outBuffType);
158 0 : if (templateScratchMultiplier != 0) {
159 0 : Buffer *scratchBuffer = op_.GetBuffer(BufferType::SCRATCH);
160 0 : CHK_PRT_RET(scratchBuffer == nullptr || scratchBuffer->GetAddr() == 0 || scratchBuffer->GetSize() == 0,
161 : HCCL_ERROR("Scratch buffer is null while ScratchMultiplier is not 0"),
162 : HCCL_E_MEMORY);
163 0 : u64 scratchBoundDataSize = scratchBuffer->GetSize() / templateScratchMultiplier;
164 0 : maxDataSizePerLoop = std::min(transportBoundDataSize, scratchBoundDataSize);
165 : } else {
166 0 : maxDataSizePerLoop = transportBoundDataSize;
167 : }
168 0 : u64 maxDataCountPerLoop = maxDataSizePerLoop / dataTypeSize_;
169 0 : HCCL_INFO("[InsAllReduceCombExecutor][OrchestrateOpbase] maxDataCountPerLoop[%llu], maxDataSizePerLoop[%llu], "
170 : "transportBoundDataSize[%llu], templateScratchMultiplier[%llu]",
171 : maxDataCountPerLoop,
172 : maxDataSizePerLoop,
173 : transportBoundDataSize,
174 : templateScratchMultiplier);
175 0 : CHK_PRT_RET(maxDataCountPerLoop == 0,
176 : HCCL_ERROR("[InsAllReduceCombExecutor][OrchestrateOpbase] maxDataCountPerLoop is 0"),
177 : HCCL_E_INTERNAL);
178 :
179 0 : CHK_PRT_RET(op_.vDataDes.counts == nullptr || op_.vDataDes.displs == nullptr,
180 : HCCL_ERROR("[InsAllReduceCombExecutor][OrchestrateOpbase] counts or displs is nullptr"),
181 : HCCL_E_PTR);
182 0 : u64 myRankRecvCount = static_cast<u64 *>(op_.vDataDes.counts)[myRank_];
183 0 : u64 myDisplacementCount = static_cast<u64 *>(op_.vDataDes.displs)[myRank_];
184 0 : u64 maxRecvDataCount = 0;
185 0 : for (u64 i = 0; i < rankSize_; i++) {
186 0 : maxRecvDataCount = max(maxRecvDataCount, static_cast<u64 *>(op_.vDataDes.counts)[i]);
187 : }
188 :
189 0 : u64 loopTimes = 1 + ((maxRecvDataCount - 1) / maxDataCountPerLoop); // 向上取整
190 0 : u64 processedDataCount = 0;
191 0 : for (u64 loop = 0; loop < loopTimes; loop++) {
192 0 : u64 currDataCount =
193 0 : processedDataCount < myRankRecvCount ? min(maxDataCountPerLoop, myRankRecvCount - processedDataCount) : 0;
194 0 : tempAlgParams.buffInfo.inBuffBaseOff = processedDataCount * dataTypeSize_;
195 0 : tempAlgParams.buffInfo.outBuffBaseOff = processedDataCount * dataTypeSize_;
196 0 : tempAlgParams.buffInfo.scratchBuffBaseOff = 0;
197 0 : tempAlgParams.sliceSize = currDataCount * dataTypeSize_;
198 0 : tempAlgParams.tailSize = 0; // 变长算子不涉及
199 0 : tempAlgParams.inputSliceStride = myDisplacementCount * dataTypeSize_; // 变长算子, 表示自己的这片输入数据的起始位置
200 0 : tempAlgParams.outputSliceStride = 0; // 变长算子, 表示自己的这片输出数据的起始位置
201 :
202 0 : CHK_RET(algTemplate->GenExtIns(tempFuncs, tempAlgParams, tempResLinks_, tempInsQue_));
203 0 : processedDataCount += maxDataCountPerLoop;
204 : }
205 0 : return HcclResult::HCCL_SUCCESS;
206 0 : }
207 :
208 : template <typename AlgTopoMatch, typename InsAlgTemplate>
209 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcRes(
210 : const RankGraph *rankGraph, CollAlgResReq &algResReq)
211 : {
212 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
213 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
214 0 : algResReq.topoInfo.UpdateSingleLevelTopo(virtRanks_, virtRankMap_, vTopo_);
215 :
216 0 : InsAlgTemplate tempAlg(myRank_, rankSize_, vTopo_, virtRankMap_);
217 :
218 0 : AlgTempResReq tempResReq;
219 0 : if (enableDetour_) {
220 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][CalcRes] [%s] Rank[%d], CalcRes with detouring enabled.", __func__, myRank_);
221 0 : CHK_RET(tempAlg.CalcResDetour(rankGraph, tempResReq));
222 : } else {
223 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][CalcRes] [%s] Rank[%d], CalcRes with detouring disabled.", __func__, myRank_);
224 0 : CHK_RET(tempAlg.CalcRes(tempResReq));
225 : }
226 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, tempResReq.links, algResReq.levelRankPairs));
227 0 : algResReq.primQueueNum = tempResReq.streamNum;
228 0 : algResReq.queueNotifys = tempResReq.queNotifys;
229 0 : algResReq.localWaitGroupCntNotify = tempResReq.localWaitGroupCntNotify;
230 0 : algResReq.localBcastPostCntNotify = tempResReq.localBcastPostCntNotify;
231 0 : HCCL_INFO("[%s] Rank[%d], requiredQueNum [%u].", __func__, myRank_, algResReq.primQueueNum);
232 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, algResReq.links));
233 :
234 0 : return HcclResult::HCCL_SUCCESS;
235 0 : }
236 :
237 : template <typename AlgTopoMatch, typename InsAlgTemplate>
238 0 : HcclResult InsV2ReduceScatterVSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcResOffload(const RankGraph *rankGraph,
239 : const u64 &dataSize, CollOffloadOpResReq &resReq)
240 : {
241 : (void)dataSize;
242 : // Topo Match
243 0 : CHK_RET(InitCommInfo(rankGraph));
244 :
245 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
246 0 : CHK_RET(CreateTemplates(algTemplate));
247 :
248 0 : AlgTempResReq tempResReq;
249 0 : if (enableDetour_) {
250 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][CalcResOffload] [%s] Rank[%d], CalcRes with detouring enabled.", __func__, myRank_);
251 0 : CHK_RET(algTemplate->CalcResDetour(rankGraph, tempResReq));
252 : } else {
253 0 : HCCL_DEBUG("[InsV2ReduceScatterVSoleExecutor][CalcResOffload] [%s] Rank[%d], CalcRes with detouring disabled.", __func__, myRank_);
254 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
255 : }
256 0 : resReq.requiredScratchMemSize = UB_MAX_DATA_SIZE;
257 0 : resReq.requiredSubQueNum = tempResReq.streamNum - 1;
258 :
259 0 : return HcclResult::HCCL_SUCCESS;
260 0 : }
261 :
262 : #ifndef CCL_KERNEL_AICPU
263 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTERV, CcuReduceScatterVMeshMem2Mem1D, InsV2ReduceScatterVSoleExecutor,
264 : TopoMatchMesh, CcuTempReduceScatterVMeshMem2Mem1D);
265 : INS_REGISTER_IMPL_BY_TEMP(OpType::REDUCESCATTERV, CcuReduceScatterVMesh1D, InsV2ReduceScatterVSoleExecutor,
266 : TopoMatchMesh, CcuTempReduceScatterVMesh1D);
267 : #endif
268 :
269 : } // namespace Hccl
|