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 :
13 : #include "alg_data_trans_wrapper.h"
14 : #include "executor_utils.h"
15 : #include "ins_temp_all_gather_mesh_2D.h"
16 :
17 : namespace Hccl {
18 0 : InsTempAllGatherMesh2D::InsTempAllGatherMesh2D(const RankId virtualRank, const u32 tempRankSize,
19 : const std::vector<std::vector<RankId>> &tempVTopo,
20 0 : const std::map<RankId, u32> &tempVirtRankMap)
21 0 : : InsAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
22 : {
23 0 : }
24 :
25 0 : InsTempAllGatherMesh2D::~InsTempAllGatherMesh2D()
26 : {
27 0 : }
28 :
29 0 : HcclResult InsTempAllGatherMesh2D::CalcRes(AlgTempResReq &tempResReq)
30 : {
31 0 : HCCL_DEBUG("Enter InsTempAllGatherMesh2D::CalcRes");
32 0 : const int TwoD = 2;
33 0 : CHK_PRT_RET(
34 : tempVTopo_.size() < TwoD,
35 : HCCL_ERROR("[InsTempAllGatherMesh2D] tempVTopo_ mismatch size:%zu", tempVTopo_.size()),
36 : HcclResult::HCCL_E_INTERNAL);
37 0 : CHK_PRT_RET(
38 : tempVTopo_[0].size() <= 1 || tempVTopo_[1].size() <= 1,
39 : HCCL_ERROR("[InsTempAllGatherMesh2D] tempVTopo_ size error, size:%zu %zu", tempVTopo_[0].size(), tempVTopo_[1].size()),
40 : HcclResult::HCCL_E_INTERNAL);
41 0 : tempResReq.queNum = tempVTopo_[0].size() - 1 + tempVTopo_[1].size() - 1;
42 :
43 0 : tempResReq.streamNum = tempResReq.queNum;
44 0 : tempResReq.queNotifys = CreateMasterSlaveQueNotifiesRequest(tempResReq.queNum);
45 0 : HCCL_DEBUG("InsTempAllGatherMesh2D::CalcRes queNotifys size[%u]", tempResReq.queNotifys.size());
46 :
47 0 : QId centerQ = 0;
48 0 : tempResReq.localWaitGroupCntNotify.emplace_back(centerQ, 0);
49 0 : tempResReq.localBcastPostCntNotify.emplace_back(centerQ, 0);
50 :
51 : uint32_t myAlgRank;
52 0 : for (u32 dim = 0; dim < tempVTopo_.size(); dim++) {
53 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[dim], myAlgRank));
54 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[dim].size() - 1; queIdx++) {
55 0 : u32 neighborAlgRank = (myAlgRank + 1 + queIdx) % (tempVTopo_[dim].size());
56 0 : RankId neighborRank = tempVTopo_[dim][neighborAlgRank];
57 0 : HCCL_INFO("InsTempAllGatherMesh2D::CalcRes Rank[%d], Dim[%u], NeighborRank[%d].", myRank_,
58 : dim, neighborRank);
59 : // LinkNum
60 0 : tempResReq.links[neighborRank] = 1;
61 : }
62 : }
63 0 : HCCL_INFO("InsTempAllGatherMesh2D::CalcRes done");
64 0 : return HcclResult::HCCL_SUCCESS;
65 : }
66 :
67 0 : HcclResult InsTempAllGatherMesh2D::GenExtIns(const TempFuncs &tempFuncs, const TemplateDataParams &tempAlgParams,
68 : const ResLinks &tempLinks,
69 : std::vector<InsQuePtr> &tempInsQues)
70 : {
71 0 : HCCL_INFO("[InsTempGatherMesh2D] Run start");
72 :
73 0 : opMode_ = tempFuncs.opMode;
74 0 : tempAlgParams_ = tempAlgParams;
75 0 : tempLinks_ = tempLinks;
76 0 : tempFuncs_ = tempFuncs;
77 0 : const int TwoD = 2;
78 0 : CHK_PRT_RET(
79 : tempVTopo_.size() < TwoD,
80 : HCCL_ERROR("[InsTempAllGatherMesh2D] tempVTopo_ mismatch size:%zu", tempVTopo_.size()),
81 : HcclResult::HCCL_E_INTERNAL);
82 0 : CHK_PRT_RET(
83 : tempVTopo_[0].size() <= 1 || tempVTopo_[1].size() <= 1,
84 : HCCL_ERROR("[InsTempAllGatherMesh2D] tempVTopo_ size error, size:%zu %zu", tempVTopo_[0].size(),
85 : tempVTopo_[1].size()),
86 : HcclResult::HCCL_E_INTERNAL);
87 0 : majorQueNum_ = tempVTopo_[0].size() - 1 + tempVTopo_[1].size() - 1;
88 0 : xQueNum_ = tempVTopo_[0].size() - 1;
89 0 : yQueNum_ = tempVTopo_[1].size() - 1;
90 :
91 : // queue arrangement
92 0 : std::vector<InsQuePtr> mainInsQues;
93 0 : std::vector<InsQuePtr> xInsQues;
94 0 : std::vector<InsQuePtr> yInsQues;
95 0 : for (u32 queIdx = 0; queIdx < majorQueNum_; queIdx++) {
96 0 : mainInsQues.push_back(tempInsQues[queIdx]);
97 0 : if (queIdx < xQueNum_) {
98 0 : xInsQues.push_back(tempInsQues[queIdx]);
99 : } else {
100 0 : yInsQues.push_back(tempInsQues[queIdx]);
101 : }
102 : }
103 :
104 : // Local Copy from Input to Output
105 0 : CHK_RET(LocalDataCopy(mainInsQues));
106 0 : if (tempRankSize_ == 1) {
107 0 : return HcclResult::HCCL_SUCCESS;
108 : }
109 : // semaphore sync
110 0 : CHK_RET(PreSyncInterQueues(mainInsQues));
111 :
112 : // // step1
113 0 : CHK_RET(Run2DStep1(xInsQues, yInsQues));
114 :
115 : // semaphore sync
116 0 : CHK_RET(PostSyncInterQueues(mainInsQues));
117 0 : CHK_RET(PreSyncInterQueues(mainInsQues));
118 :
119 : // step2 run Mesh
120 0 : CHK_RET(Run2DStep2(xInsQues, yInsQues));
121 0 : CHK_RET(PostSyncInterQueues(mainInsQues));
122 : // LocalCopy: from scratch to output for opbase
123 0 : if ((opMode_ == OpMode::OPBASE) && tempFuncs.isBottom) {
124 0 : CHK_RET(PostLocalCopy(mainInsQues));
125 : }
126 0 : return HcclResult::HCCL_SUCCESS;
127 0 : }
128 :
129 0 : HcclResult InsTempAllGatherMesh2D::Run2DStep1(std::vector<InsQuePtr> &xInsQues, std::vector<InsQuePtr> &yInsQues)
130 : {
131 : u32 myAlgRankX;
132 : u32 myAlgRankY;
133 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[0], myAlgRankX));
134 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[1], myAlgRankY));
135 :
136 0 : CHK_PRT_RET(
137 : RunMesh(myAlgRankX, myRank_, 0, tempVTopo_[0], xInsQues, 0, tempAlgParams_.sliceSize, DmaMode::PUT) != HcclResult::HCCL_SUCCESS,
138 : HCCL_ERROR("[InsCollAlgFactory] [InsTempAllGatherMesh2D] Rank [%d], unable to run the mesh x0 algorithm.", myRank_),
139 : HcclResult::HCCL_E_INTERNAL);
140 0 : CHK_PRT_RET(
141 : RunMesh(myAlgRankY, myRank_, 0, tempVTopo_[1], yInsQues, 0, tempAlgParams_.sliceSize, DmaMode::PUT) != HcclResult::HCCL_SUCCESS,
142 : HCCL_ERROR("[InsCollAlgFactory] [InsTempAllGatherMesh2D] Rank [%d], unable to run the mesh y0 algorithm.", myRank_),
143 : HcclResult::HCCL_E_INTERNAL);
144 0 : return HcclResult::HCCL_SUCCESS;
145 : }
146 :
147 0 : HcclResult InsTempAllGatherMesh2D::Run2DStep2(std::vector<InsQuePtr> &xInsQues, std::vector<InsQuePtr> &yInsQues)
148 : {
149 : u32 myAlgRankX;
150 : u32 myAlgRankY;
151 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[0], myAlgRankX));
152 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[1], myAlgRankY));
153 0 : u64 xSize = tempAlgParams_.sliceSize / 2;
154 0 : u64 ySize = tempAlgParams_.sliceSize - xSize;
155 0 : for (u32 rank = 0; rank < tempVTopo_[0].size(); rank++) {// 转发哪一个rank
156 0 : if (rank == myAlgRankX) {// 只处理转发,直连的在step1传输完成
157 0 : continue;
158 : }
159 0 : RankId globalRank = tempVTopo_[0][rank];
160 0 : int rankOffset = rank - myAlgRankX;
161 : // 上半部分数据通过Y轴传输
162 0 : CHK_PRT_RET(
163 : RunMesh(myAlgRankY, globalRank, rankOffset, tempVTopo_[1], yInsQues, 0, xSize, DmaMode::GET) != HcclResult::HCCL_SUCCESS,
164 : HCCL_ERROR("[InsCollAlgFactory] [InsTempAllGatherMesh2D] Rank [%d], unable to run the mesh y1 algorithm.", myRank_),
165 : HcclResult::HCCL_E_INTERNAL);
166 : }
167 0 : for (u32 rank = 0; rank < tempVTopo_[1].size(); rank++) {
168 0 : if (rank == myAlgRankY) {
169 0 : continue;
170 : }
171 0 : RankId globalRank = tempVTopo_[1][rank];
172 0 : int rankOffset = (rank - myAlgRankY) * tempVTopo_[0].size();
173 : // 下半部分数据通过X轴传输
174 0 : CHK_PRT_RET(
175 : RunMesh(myAlgRankX, globalRank, rankOffset, tempVTopo_[0], xInsQues, xSize, ySize, DmaMode::GET) != HcclResult::HCCL_SUCCESS,
176 : HCCL_ERROR("[InsCollAlgFactory] [InsTempAllGatherMesh2D] Rank [%d], unable to run the mesh x1 algorithm.", myRank_),
177 : HcclResult::HCCL_E_INTERNAL);
178 : }
179 0 : return HcclResult::HCCL_SUCCESS;
180 : }
181 :
182 0 : HcclResult InsTempAllGatherMesh2D::RunMesh(const u32 myAlgRank, RankId globalSrcRank, int rankOffset, const std::vector<RankId> &vTopo,
183 : std::vector<InsQuePtr> &tempInsQues, u64 xyOffset, u64 size, DmaMode dmaMode)
184 : {
185 0 : if (size == 0) {
186 0 : HCCL_INFO("[InsTempAllGatherMesh2D] 0 data skip sendrecv");
187 0 : return HcclResult::HCCL_SUCCESS;
188 : }
189 0 : const u64 scratchRepeatStride = tempAlgParams_.sliceSize * tempRankSize_;
190 0 : for (u32 rpt = 0; rpt < tempAlgParams_.repeatNum; ++rpt) {
191 0 : for (u32 queIdx = 0; queIdx < vTopo.size() - 1; queIdx++) {
192 : // find neighbors -> virtualRank
193 0 : RankId connectedRank = vTopo[(myAlgRank + 1 + queIdx) % vTopo.size()];
194 0 : RankId globalDstRank = connectedRank + rankOffset;
195 0 : HCCL_INFO("[InsTempAllGatherMesh2D] RunAllGather opbase find neighbors: ==============="
196 : "myRank=%d, connectedRank=%d, globalSrcRank=%d, globalDstRank=%d, myAlgRank=%u, queIdx=%u,",
197 : myRank_, connectedRank, globalSrcRank, globalDstRank, myAlgRank, queIdx);
198 :
199 0 : RankId srcAlgRank = globalSrcRank % tempRankSize_;
200 0 : RankId dstAlgRank = globalDstRank % tempRankSize_;
201 :
202 0 : CHK_PRT_RET(
203 : queIdx >= tempInsQues.size() or tempLinks_.at(connectedRank).size() <= 0,
204 : HCCL_ERROR("InsTempAllGatherMesh2D: tempInsQues.size()=%u, connectedRank=%d, tempLinks_.size()=%u, ",
205 : tempInsQues.size(), connectedRank, tempLinks_.size()), HcclResult::HCCL_E_INTERNAL);
206 0 : InsQuePtr currQue = tempInsQues[queIdx];
207 0 : LinkData& neighborLinkData = tempLinks_.at(connectedRank)[0];
208 :
209 0 : BufferType type = (opMode_ == OpMode::OPBASE || !tempFuncs_.isBottom) ? BufferType::SCRATCH : BufferType::OUTPUT;
210 0 : const u64 txOutOffset = tempAlgParams_.buffInfo.outBuffBaseOff + rpt * tempAlgParams_.outputRepeatStride +
211 0 : tempAlgParams_.outputSliceStride * srcAlgRank + xyOffset;
212 0 : const u64 txScratchOffset = tempAlgParams_.buffInfo.scratchBuffBaseOff + rpt * scratchRepeatStride +
213 0 : + tempAlgParams_.sliceSize * srcAlgRank + xyOffset;
214 0 : const u64 txDstOffset = (opMode_ == OpMode::OPBASE || !tempFuncs_.isBottom) ? txScratchOffset : txOutOffset;
215 0 : HCCL_DEBUG("[InsTempAllGatherMesh2D] RunAllGather opbase sendrecv: "
216 : "txOutOffset=%llu, txScratchOffset=%llu, txDstOffset=%llu "
217 : "(globalSrcRank=%d, globalDstRank=%d, opMode=%d)",
218 : txOutOffset, txScratchOffset, txDstOffset, globalSrcRank, globalDstRank, opMode_);
219 0 : const u64 rxOutOffset = tempAlgParams_.buffInfo.outBuffBaseOff + rpt * tempAlgParams_.outputRepeatStride +
220 0 : tempAlgParams_.outputSliceStride * dstAlgRank + xyOffset;
221 0 : const u64 rxScratchOffset = tempAlgParams_.buffInfo.scratchBuffBaseOff + rpt * scratchRepeatStride +
222 0 : tempAlgParams_.sliceSize * dstAlgRank + xyOffset;
223 0 : const u64 rxSrcOffset = (opMode_ == OpMode::OPBASE || !tempFuncs_.isBottom) ? rxScratchOffset : rxOutOffset;
224 0 : HCCL_DEBUG("[InsTempAllGatherMesh2D] RunAllGather opbase sendrecv: "
225 : "rxOutOffset=%llu, rxScratchOffset=%llu, rxSrcOffset=%llu "
226 : "(globalSrcRank=%d, globalDstRank=%d, opMode=%d)",
227 : rxOutOffset, rxScratchOffset, rxSrcOffset, globalSrcRank, globalDstRank, opMode_);
228 :
229 0 : BufferType txrxBufType = !tempFuncs_.isBottom ? BufferType::SCRATCH : BufferType::OUTPUT;
230 0 : vector<DataSlice> txSrcSlice = vector<DataSlice>{ DataSlice(txrxBufType, txOutOffset, size) }; // 发送源
231 0 : vector<DataSlice> txDstSlice = vector<DataSlice>{ DataSlice(type, txDstOffset, size) }; // 发送目标
232 0 : HCCL_INFO("[InsTempAllGatherMesh2D] RunAllGather opbase *****sendrecv*****, txSrcSlice: %s, txDstSlice: %s",
233 : txSrcSlice[0].Describe().c_str(), txDstSlice[0].Describe().c_str());
234 :
235 0 : vector<DataSlice> rxSrcSlice = vector<DataSlice>{ DataSlice(type, rxSrcOffset, size) }; // 接收源
236 0 : vector<DataSlice> rxDstSlice = vector<DataSlice>{ DataSlice(txrxBufType, rxOutOffset, size) }; // 接收目标
237 0 : HCCL_INFO("[InsTempAllGatherMesh2D] RunAllGather opbase *****sendrecv*****, rxSrcSlice: %s, rxDstSlice: %s",
238 : rxSrcSlice[0].Describe().c_str(), rxDstSlice[0].Describe().c_str());
239 :
240 0 : TxRxSlicesList sendRecvSlicesList({txSrcSlice, txDstSlice}, {rxSrcSlice, rxDstSlice});
241 0 : TxRxLinks sendRecvLinks(neighborLinkData, neighborLinkData);
242 0 : SendRecvInfo sendRecvInfo(sendRecvLinks, sendRecvSlicesList);
243 0 : CHK_PRT_RET(SendRecv(sendRecvInfo, currQue, 0, true, dmaMode),
244 : HCCL_ERROR("[InsTempAllGatherMesh2D] RunAllGather opbase sendrecv failed"),
245 : HcclResult::HCCL_E_INTERNAL);
246 0 : }
247 : }
248 0 : return HcclResult::HCCL_SUCCESS;
249 : }
250 :
251 0 : HcclResult InsTempAllGatherMesh2D::LocalDataCopy(std::vector<InsQuePtr> &tempInsQues)
252 : {
253 0 : if (tempAlgParams_.buffInfo.inBuffType == tempAlgParams_.buffInfo.outBuffType) {
254 0 : return HcclResult::HCCL_SUCCESS;
255 : }
256 0 : for (u32 rpt = 0; rpt < tempAlgParams_.repeatNum; ++rpt) {
257 0 : RankId algRank = myRank_ % tempRankSize_;
258 0 : const u64 inOffset = tempAlgParams_.buffInfo.inBuffBaseOff + rpt * tempAlgParams_.inputRepeatStride;
259 0 : DataSlice usrInSlice = DataSlice(BufferType::INPUT, inOffset, tempAlgParams_.sliceSize);
260 0 : const u64 outOffset = tempAlgParams_.buffInfo.outBuffBaseOff + rpt * tempAlgParams_.outputRepeatStride +
261 0 : tempAlgParams_.outputSliceStride * algRank;
262 0 : DataSlice usrOutSlice = DataSlice(BufferType::OUTPUT, outOffset, tempAlgParams_.sliceSize);
263 0 : HCCL_INFO("[InsTempAllGatherMesh2D] PreCopy usrInSlice: %s, usrOutSlice: %s", usrInSlice.Describe().c_str(),
264 : usrOutSlice.Describe().c_str());
265 0 : std::unique_ptr<Instruction> insLocalCopy = std::make_unique<InsLocalCopy>(usrInSlice, usrOutSlice);
266 0 : tempInsQues[0]->Append(std::move(insLocalCopy));
267 0 : }
268 0 : return HcclResult::HCCL_SUCCESS;
269 : }
270 :
271 0 : HcclResult InsTempAllGatherMesh2D::PostLocalCopy(std::vector<InsQuePtr> &tempInsQues)
272 : {
273 0 : CHK_PRT_RET(tempInsQues.empty(),
274 : HCCL_ERROR("[InsTempAllGatherMesh2D][PostLocalCopy] empty tempInsQues"), HcclResult::HCCL_E_INTERNAL);
275 0 : CHK_PTR_NULL(tempInsQues[0]);
276 0 : const u64 scratchRepeatStride = tempAlgParams_.sliceSize * tempRankSize_;
277 0 : for (u32 rpt = 0; rpt < tempAlgParams_.repeatNum; ++rpt) {
278 0 : for (u32 i =0; i < tempVTopo_.size(); i++) {
279 0 : for(auto rank : tempVTopo_[i]) {
280 0 : if (rank == myRank_) {
281 0 : continue;
282 : }
283 : // 只拷贝step1的对端
284 0 : RankId algRank = (rank % tempRankSize_);
285 0 : u64 scratchOffset = tempAlgParams_.buffInfo.scratchBuffBaseOff + rpt * scratchRepeatStride +
286 0 : tempAlgParams_.sliceSize * algRank;
287 0 : u64 outOffset = tempAlgParams_.buffInfo.outBuffBaseOff + rpt * tempAlgParams_.outputRepeatStride +
288 0 : tempAlgParams_.outputSliceStride * algRank;
289 0 : DataSlice usrInSlice = DataSlice(BufferType::SCRATCH, scratchOffset, tempAlgParams_.sliceSize);
290 0 : DataSlice usrOutSlice = DataSlice(BufferType::OUTPUT, outOffset, tempAlgParams_.sliceSize);
291 0 : HCCL_INFO("[InsTempAllGatherMesh2D] rank[%d] algRank[%d] PostCopy usrInSlice: %s, usrOutSlice: %s",
292 : myRank_, algRank, usrInSlice.Describe().c_str(), usrOutSlice.Describe().c_str());
293 0 : std::unique_ptr<Instruction> insLocalCopy = std::make_unique<InsLocalCopy>(usrInSlice, usrOutSlice);
294 0 : tempInsQues[0]->Append(std::move(insLocalCopy));
295 0 : }
296 : }
297 : }
298 0 : return HcclResult::HCCL_SUCCESS;
299 : }
300 :
301 : } // namespace Hccl
|