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_temp_reduce_scatter_mesh_1D_meshchunk.h"
12 : #include "log.h"
13 : #include "alg_data_trans_wrapper.h"
14 :
15 : namespace Hccl {
16 0 : InsTempReduceScatterMesh1DMeshChunk::InsTempReduceScatterMesh1DMeshChunk(const RankId virtualRank, const u32 tempRankSize,
17 : const std::vector<std::vector<RankId>> &tempVTopo,
18 0 : const std::map<RankId, u32> &tempVirtRankMap)
19 0 : : InsAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
20 : {
21 0 : }
22 :
23 0 : InsTempReduceScatterMesh1DMeshChunk::~InsTempReduceScatterMesh1DMeshChunk()
24 : {
25 0 : }
26 :
27 0 : HcclResult InsTempReduceScatterMesh1DMeshChunk::CalcRes(AlgTempResReq &tempResReq)
28 : {
29 : // Mesh 需要的 que Num 为 tempVTopo_[0].size()-1
30 0 : tempResReq.queNum = (tempVTopo_[0].size() > 1) ? tempVTopo_[0].size() - 1 : 1;
31 0 : tempResReq.streamNum = tempResReq.queNum;
32 0 : tempResReq.queNotifys = CreateMasterSlaveQueNotifiesRequest(tempResReq.queNum);
33 0 : QId centerQ = 0;
34 0 : tempResReq.localWaitGroupCntNotify.emplace_back(centerQ, 0);
35 0 : tempResReq.localBcastPostCntNotify.emplace_back(centerQ, 0);
36 : // linkNumBtwPeers_这个在没有绕路的情况下,是设置成1
37 0 : CHK_PRT_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq) != HcclResult::HCCL_SUCCESS,
38 : HCCL_ERROR("[CollAlgFactory] [InsTempReduceScatterMesh1DMeshChunk] Rank [%d], resLinks calculation error!", myRank_),
39 : HcclResult::HCCL_E_INTERNAL);
40 :
41 0 : return HcclResult::HCCL_SUCCESS;
42 : }
43 :
44 0 : u64 InsTempReduceScatterMesh1DMeshChunk::CalcScratchMultiple(const BufferType &inBuffType, const BufferType &outBuffType) const
45 : {
46 : (void)inBuffType;
47 : (void)outBuffType;
48 0 : u64 scratchMultiple = tempRankSize_ - 1;
49 0 : return scratchMultiple;
50 : }
51 :
52 0 : HcclResult InsTempReduceScatterMesh1DMeshChunk::CalcSliceInfoVec(const u64 &dataSize, RankSliceInfo &sliceInfoVec)
53 : {
54 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
55 0 : sliceInfoVec.resize(tempRankSize_, tmp);
56 0 : u64 accumOff = 0;
57 0 : for (u32 rankIdx = 0; rankIdx < sliceInfoVec.size(); rankIdx++) {
58 0 : SliceInfo slice = {accumOff, dataSize};
59 0 : sliceInfoVec[rankIdx][0] = slice;
60 0 : accumOff += dataSize;
61 : }
62 0 : CHK_PRT_RET(
63 : (sliceInfoVec[tempRankSize_ - 1][0].offset + sliceInfoVec[tempRankSize_ - 1][0].size != dataSize * tempRankSize_),
64 : HCCL_ERROR("[CollAlgFactory] Rank [%d], SliceInfo calculation error!", myRank_), HcclResult::HCCL_E_INTERNAL);
65 :
66 0 : return HcclResult::HCCL_SUCCESS;
67 0 : }
68 :
69 0 : HcclResult InsTempReduceScatterMesh1DMeshChunk::GenExtIns(const TempFuncs &tempFuncs, const TemplateDataParams &tempAlgParams,
70 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
71 : {
72 0 : opMode_ = tempFuncs.opMode;
73 0 : enableCounterNotify_ = tempFuncs.enableCounterNotify;
74 0 : queNum_ = tempVTopo_[0].size() - 1;
75 0 : processSize_ = tempAlgParams.sliceSize;
76 0 : rankIdx_ = tempVirtRankMap_[myRank_];
77 0 : RankSliceInfo sliceInfoVec;
78 0 : CHK_RET(CalcSliceInfoVec(tempAlgParams.sliceSize, sliceInfoVec));
79 0 : HCCL_INFO("[InsTempReduceScatterMesh1DMeshChunk] Run Start");
80 : // 这里不支持绕路的时候,应该就用原始的tempInsQues就行
81 0 : CHK_PRT_RET(queNum_ != tempInsQues.size(),
82 : HCCL_ERROR("[CollAlgFactory] [InsTempReduceScatterMesh1DMeshChunk] Rank [%d], requiredQue Error.", myRank_),
83 : HcclResult::HCCL_E_INTERNAL);
84 0 : PreCopy(tempAlgParams, tempInsQues);
85 0 : if (queNum_ > 1) {
86 0 : CHK_RET(PreSyncInterQueues(tempInsQues));
87 : }
88 :
89 0 : CHK_RET(RunReduceScatter(tempLinks, tempInsQues, tempAlgParams, sliceInfoVec));
90 :
91 0 : if (queNum_ > 1) {
92 0 : CHK_RET(PostSyncInterQueues(tempInsQues));
93 : }
94 0 : PostCopy(tempAlgParams, tempInsQues);
95 0 : return HcclResult::HCCL_SUCCESS;
96 0 : }
97 :
98 0 : HcclResult InsTempReduceScatterMesh1DMeshChunk::PreCopy(const TemplateDataParams &tempAlgParams, std::vector<InsQuePtr> &tempInsQues) const
99 : {
100 0 : HCCL_INFO("[InsTempReduceScatterMesh1DMeshChunk][PreCopy], copy from userIn to scratch");
101 0 : for (u32 repeatIdx = 0; repeatIdx < tempAlgParams.repeatNum; repeatIdx++) {
102 0 : DataSlice srcSlice = DataSlice(tempAlgParams.buffInfo.inBuffType, tempAlgParams.buffInfo.inBuffBaseOff +
103 0 : repeatIdx * tempAlgParams.inputRepeatStride + rankIdx_ * tempAlgParams.inputSliceStride, processSize_);
104 0 : DataSlice dstSlice = DataSlice(tempAlgParams.buffInfo.scratBuffType, tempAlgParams.buffInfo.scratchBuffBaseOff, processSize_);
105 0 : CHK_RET(LocalCopy(tempInsQues[0], srcSlice, dstSlice));
106 : }
107 0 : return HcclResult::HCCL_SUCCESS;
108 : }
109 :
110 0 : HcclResult InsTempReduceScatterMesh1DMeshChunk::RunReduceScatter(const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues,
111 : const TemplateDataParams &tempAlgParams, RankSliceInfo &sliceInfoVec)
112 : {
113 0 : HCCL_INFO("[InsTempReduceScatterMesh1DMeshChunk][RunReduceScatter] myRank[%d]", myRank_);
114 : u32 myAlgRank;
115 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[0], myAlgRank));
116 :
117 0 : uint64_t sliceNum = tempRankSize_ - 1;
118 0 : uint64_t mySliceSize = sliceInfoVec[myAlgRank][0].size; // 获取本rank需要处理的数据量
119 0 : uint64_t mySliceCount = mySliceSize / DataTypeSizeGet(op_.dataType);
120 : // 数据切分为sliceNum块,当数据量不能均匀切分时,后面smallDataSliceNum个数据块比前面bigDataSliceNum个数据块每块少1个数据
121 0 : uint64_t bigDataSliceNum = mySliceCount % sliceNum;
122 0 : uint64_t bigDataSliceSize = (mySliceCount / sliceNum + 1) * DataTypeSizeGet(op_.dataType);
123 0 : uint64_t smallDataSliceNum = sliceNum - mySliceCount % sliceNum;
124 0 : uint64_t smallDataSliceSize = mySliceCount / sliceNum * DataTypeSizeGet(op_.dataType);
125 :
126 0 : std::vector<uint64_t> sliceSize;
127 0 : for (uint64_t i = 0; i < bigDataSliceNum; i++) {
128 0 : sliceSize.push_back(bigDataSliceSize);
129 : }
130 0 : for (uint64_t i = 0; i < smallDataSliceNum; i++) {
131 0 : sliceSize.push_back(smallDataSliceSize);
132 : }
133 0 : uint64_t sliceRecvBaseOffset = 0;
134 0 : uint16_t rankNum = 2;
135 0 : for (uint16_t i = 0; i < (tempRankSize_ - rankNum); i++) {
136 0 : sliceRecvBaseOffset += sliceSize[i];
137 : }
138 0 : for (u32 repeatIdx = 0; repeatIdx < tempAlgParams.repeatNum; repeatIdx++) {
139 : uint64_t sliceSendOffset_;
140 : uint64_t sliceRecvOffset_;
141 0 : DoMeshChunk(tempLinks, tempInsQues, tempAlgParams, sliceSize, repeatIdx, myAlgRank, sliceSendOffset_, sliceRecvOffset_,
142 : sliceRecvBaseOffset);
143 : }
144 0 : return HcclResult::HCCL_SUCCESS;
145 0 : }
146 :
147 0 : HcclResult InsTempReduceScatterMesh1DMeshChunk::DoMeshChunk(const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues,
148 : const TemplateDataParams &tempAlgParams, const std::vector<uint64_t> &sliceSize, const u32 &repeatIdx,
149 : const u32 &myAlgRank, uint64_t &sliceSendOffset_, uint64_t &sliceRecvOffset_, const uint64_t &sliceRecvBaseOffset)
150 : {
151 0 : for (uint16_t stepIdx = 0; stepIdx < (tempRankSize_ - 1); stepIdx++) {
152 0 : sliceSendOffset_ = 0;
153 0 : sliceRecvOffset_ = sliceRecvBaseOffset;
154 0 : uint16_t rankNum = 2;
155 0 : uint16_t tempNum = 3;
156 0 : for (uint16_t i = 0; i < (tempRankSize_ - 1); i++) {
157 0 : uint16_t nextNum = stepIdx + i + 1;
158 0 : if (nextNum >= tempRankSize_) {
159 0 : nextNum += 1;
160 : }
161 0 : uint16_t nextRank = (myAlgRank + nextNum) % tempRankSize_;
162 0 : uint16_t frontNum = 2 * myAlgRank - nextRank + tempRankSize_;
163 0 : uint16_t frontRank = frontNum % tempRankSize_;
164 0 : RankId toRank = tempVTopo_[0][frontRank];
165 : uint16_t queIdx;
166 0 : if (frontRank < myAlgRank) {
167 0 : queIdx = frontRank;
168 : } else {
169 0 : queIdx = frontRank - 1;
170 : }
171 0 : DataSlice rxSrcSlice = DataSlice(tempAlgParams.buffInfo.inBuffType, tempAlgParams.buffInfo.inBuffBaseOff +
172 0 : repeatIdx * tempAlgParams.inputRepeatStride + myAlgRank * tempAlgParams.inputSliceStride + sliceRecvOffset_, sliceSize[i]); // 接收源
173 0 : DataSlice rxDstSlice = DataSlice(tempAlgParams.buffInfo.scratBuffType, tempAlgParams.buffInfo.scratchBuffBaseOff +
174 0 : sliceRecvOffset_, sliceSize[i]); // 接收目标
175 0 : DataSlice txSrcSlice = DataSlice(tempAlgParams.buffInfo.inBuffType, tempAlgParams.buffInfo.inBuffBaseOff +
176 0 : repeatIdx * tempAlgParams.inputRepeatStride + frontRank * tempAlgParams.inputSliceStride + sliceSendOffset_, sliceSize[i]); // 发送源
177 0 : DataSlice txDstSlice = DataSlice(tempAlgParams.buffInfo.scratBuffType, tempAlgParams.buffInfo.scratchBuffBaseOff +
178 0 : sliceSendOffset_, sliceSize[i]); // 发送目标
179 :
180 0 : u32 rankFromRank = GetRankFromMap(toRank);
181 0 : auto it = tempLinks.find(rankFromRank);
182 0 : if (it == tempLinks.end()) {
183 0 : HCCL_ERROR("rankFromRank [%u] not in tempLinks.", rankFromRank);
184 0 : return HcclResult::HCCL_E_PARA;
185 : }
186 0 : const std::vector<LinkData> &linkRecv = tempLinks.at(GetRankFromMap(toRank));
187 0 : const std::vector<LinkData> &linkSend = tempLinks.at(GetRankFromMap(toRank));
188 0 : std::vector<DataSlice> txSrcSlices;
189 0 : std::vector<DataSlice> txDstSlices;
190 0 : std::vector<DataSlice> rxSrcSlices;
191 0 : std::vector<DataSlice> rxDstSlices;
192 0 : rxSrcSlices.push_back(rxSrcSlice);
193 0 : rxDstSlices.push_back(rxDstSlice);
194 0 : txSrcSlices.push_back(txSrcSlice);
195 0 : txDstSlices.push_back(txDstSlice);
196 :
197 : SendRecvReduceInfo sendRecvReduceInfo{
198 0 : {linkSend[0],linkRecv[0]},
199 : {{txSrcSlices, txDstSlices},{rxSrcSlices, rxDstSlices}}, dataType_, redOp_
200 0 : };
201 :
202 0 : CHK_PRT_RET(SendRecvReduce(sendRecvReduceInfo, tempInsQues[queIdx], 0, true, DmaMode::PUT),
203 : HCCL_ERROR("[InsTempReduceScatterMesh1DMeshChunk] RunReduceScatter SendRecvReduce failed"),
204 : HcclResult::HCCL_E_INTERNAL);
205 :
206 0 : sliceSendOffset_ += sliceSize[i];
207 0 : if (tempRankSize_ > rankNum && i < (tempRankSize_ - rankNum)) {
208 0 : sliceRecvOffset_ -= sliceSize[tempRankSize_ - tempNum - i];
209 : }
210 0 : }
211 0 : if (queNum_ > 1 && stepIdx < (tempRankSize_ - rankNum)) {
212 0 : CHK_RET(PostSyncInterQueues(tempInsQues));
213 0 : CHK_RET(PreSyncInterQueues(tempInsQues));
214 : }
215 : }
216 0 : return HcclResult::HCCL_SUCCESS;
217 : }
218 :
219 0 : HcclResult InsTempReduceScatterMesh1DMeshChunk::PostCopy(const TemplateDataParams &tempAlgParams, std::vector<InsQuePtr> &tempInsQues)
220 : {
221 : // 如果是单算子模式, 并且是最后一步算子,需要将数据从 scratch 拷贝到 userOut
222 0 : HCCL_INFO("[InsTempReduceScatterMesh1DMeshChunk][PostCopy], copy from scratch to userOut");
223 : u32 myAlgRank;
224 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[0], myAlgRank));
225 : // 先把本卡的数据从input搬运到output
226 0 : for (u32 repeatIdx = 0; repeatIdx < tempAlgParams.repeatNum; repeatIdx++) {
227 : DataSlice myRankSlice = DataSlice(tempAlgParams.buffInfo.scratBuffType,
228 0 : tempAlgParams.buffInfo.scratchBuffBaseOff, processSize_);
229 : DataSlice outputSlice = DataSlice(tempAlgParams.buffInfo.outBuffType,
230 0 : tempAlgParams.buffInfo.outBuffBaseOff, processSize_);
231 0 : CHK_RET(LocalCopy(tempInsQues[0], myRankSlice, outputSlice));
232 : }
233 0 : return HcclResult::HCCL_SUCCESS;
234 : }
235 :
236 0 : RankId InsTempReduceScatterMesh1DMeshChunk::GetRankFromMap(const u32 rankIdx)
237 : {
238 0 : RankId rank = -1;
239 0 : for (auto &pair : tempVirtRankMap_) {
240 0 : if (pair.second == rankIdx) {
241 0 : rank = pair.first;
242 0 : break;
243 : }
244 : }
245 0 : return rank;
246 : }
247 : } // namespace Hccl
|