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 <ios>
12 : #include <iostream>
13 :
14 : #include "log.h"
15 :
16 : #include "ccu_rank_group.h"
17 : #include "ccu_ctx_creator_registry.h"
18 : #include "ccu_context_all_reduce_nhr1d_mem2mem.h"
19 : #include "ccu_temp_all_reduce_nhr_1D_mem2mem.h"
20 : #include "ccu_ins_group.h"
21 :
22 : namespace Hccl {
23 :
24 : static CcuInstRegister<CcuContextAllReduceNHR1D> g_registrarAllReduce(CcuInstType::CCU_ALLREDUCE_NHR_1D_MEM2MEM);
25 :
26 0 : CcuTempAllReduceNHRMem2Mem1D::CcuTempAllReduceNHRMem2Mem1D(
27 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
28 0 : const std::map<RankId, u32>& tempVirtRankMap)
29 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
30 0 : {}
31 :
32 0 : CcuTempAllReduceNHRMem2Mem1D::~CcuTempAllReduceNHRMem2Mem1D() {}
33 :
34 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::CalcRes(AlgTempResReq& tempResReq)
35 : {
36 0 : tempResReq.queNum = 1;
37 0 : tempResReq.streamNum = tempResReq.queNum;
38 0 : HCCL_DEBUG("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
39 0 : u32 linkNum = 1;
40 0 : linkNumBtwPeers_ = linkNum;
41 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
42 0 : return HcclResult::HCCL_SUCCESS;
43 : }
44 :
45 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::CalcSlice(const u64 dataSize, RankSliceInfo& sliceInfoVec)
46 : {
47 0 : AllignInfo allignInfo;
48 0 : allignInfo.enableAllign = false;
49 0 : allignInfo.dataType = dataType_;
50 0 : CHK_RET(CalcSliceInfoAllReduce(allignInfo, tempRankSize_, dataSize, sliceInfoVec));
51 0 : return HcclResult::HCCL_SUCCESS;
52 : }
53 :
54 0 : void CcuTempAllReduceNHRMem2Mem1D::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
55 : {
56 0 : reduceOp_ = reduceOp;
57 0 : dataType_ = dataType;
58 0 : }
59 :
60 0 : uint64_t CcuTempAllReduceNHRMem2Mem1D::GetMaxSliceSize() const { return UB_MAX_DATA_SIZE; }
61 :
62 0 : uint32_t CcuTempAllReduceNHRMem2Mem1D::virtRankId2RankId(const uint32_t virtRankId)
63 : {
64 0 : for (auto iter = tempVirtRankMap_.begin(); iter != tempVirtRankMap_.end(); iter++) {
65 0 : if (iter->second == virtRankId) {
66 0 : return iter->first;
67 : }
68 : }
69 0 : return 0;
70 : }
71 :
72 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::SplitDataFor2Dies(
73 : uint64_t dataCount, const ResLinks& tempLinks, uint64_t& die0Size, uint64_t& die1Size) const
74 : {
75 0 : constexpr uint64_t MULTIPLIER = 4;
76 :
77 0 : if (dataCount <= tempRankSize_ * MULTIPLIER) { // 数据量极小,不划分die
78 0 : die0Size = 0;
79 0 : die1Size = dataCount * DataTypeSizeGet(dataType_);
80 0 : return HcclResult::HCCL_SUCCESS;
81 : }
82 0 : u8 die0PortGroupSize = 1;
83 0 : u8 die1PortGroupSize = 1;
84 0 : for (LinkData linkData : tempLinks.begin()->second) {
85 0 : if (linkData.GetPortGroupSize() == 0)
86 0 : continue;
87 0 : if (linkData.GetLocalDieId() == 0) {
88 0 : die0PortGroupSize = linkData.GetPortGroupSize();
89 : } else {
90 0 : die1PortGroupSize = linkData.GetPortGroupSize();
91 : }
92 : }
93 :
94 0 : die0Size = (dataCount * die0PortGroupSize / (die0PortGroupSize + die1PortGroupSize)) * DataTypeSizeGet(dataType_);
95 0 : die1Size = dataCount * DataTypeSizeGet(dataType_) - die0Size;
96 0 : return HcclResult::HCCL_SUCCESS;
97 : }
98 :
99 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::ProcessNHRStepInfo(
100 : std::vector<NHRStepInfo>& stepInfoVector, RankGroup& rankGroup, std::map<u32, u32>& indexMap,
101 : std::vector<LinkData>& linksDie0, std::vector<LinkData>& linksDie1, const ResLinks& tempLinks, uint32_t axisSize)
102 : {
103 0 : u32 nSteps = GetNHRStepNum(tempRankSize_) * 2; // 分为RS和AG两次NHR
104 0 : for (u32 step = 0; step < nSteps; step++) {
105 0 : NHRStepInfo stepInfo;
106 0 : CHK_RET(GetStepInfo(step, nSteps, stepInfo));
107 0 : stepInfoVector.push_back(stepInfo);
108 0 : if (indexMap.count(stepInfo.fromRank) == 0) {
109 0 : u32 fromRankIdx = virtRankId2RankId(stepInfo.fromRank);
110 0 : indexMap[stepInfo.fromRank] = linksDie0.size();
111 0 : linksDie0.push_back(tempLinks.at(fromRankIdx)[0]);
112 0 : if (axisSize > 1) {
113 0 : linksDie1.push_back(tempLinks.at(fromRankIdx)[1]);
114 : }
115 0 : rankGroup.AddRank(fromRankIdx);
116 : }
117 0 : if (indexMap.count(stepInfo.toRank) == 0) {
118 0 : u32 toRankIdx = virtRankId2RankId(stepInfo.toRank);
119 0 : indexMap[stepInfo.toRank] = linksDie0.size();
120 0 : linksDie0.push_back(tempLinks.at(toRankIdx)[0]);
121 0 : if (axisSize > 1) {
122 0 : linksDie1.push_back(tempLinks.at(toRankIdx)[1]);
123 : }
124 0 : rankGroup.AddRank(toRankIdx);
125 : }
126 0 : }
127 0 : rankGroup.AddRank(myRank_);
128 :
129 0 : return HcclResult::HCCL_SUCCESS;
130 : }
131 :
132 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::GenExtIns(
133 : const TempFuncs& tempFuncs, TemplateDataParams& tempAlgParams, const ResLinks& tempLinks,
134 : std::vector<InsQuePtr>& tempInsQues)
135 : {
136 0 : CHK_PRT_RET(
137 : tempInsQues.empty(), HCCL_ERROR("[CcuTempAllReduceNHRMem2Mem1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
138 0 : CHK_PTR_NULL(tempInsQues[0]);
139 0 : opMode_ = tempFuncs.opMode;
140 0 : CcuInstructionAllReduceNHR1D ccuInsAllReduceNHR1D;
141 0 : std::vector<uint64_t> dimSize;
142 0 : dimSize.push_back(tempRankSize_);
143 :
144 0 : uint32_t myVirtRankId = tempVirtRankMap_[myRank_];
145 0 : uint64_t dataCount = (tempAlgParams.sliceSize / DataTypeSizeGet(dataType_));
146 0 : if (dataCount == 0) {
147 0 : HCCL_INFO("[CcuTempAllReduceNHRMem2Mem1D] dataCount == 0, Template Run Ends.");
148 0 : return HCCL_SUCCESS;
149 : }
150 0 : uint32_t axisSize = tempLinks.begin()->second.size();
151 :
152 0 : uint64_t die0Size = 0;
153 0 : uint64_t die1Size = 0;
154 0 : if (axisSize == 1) {
155 0 : die0Size = tempAlgParams.sliceSize;
156 : } else {
157 0 : SplitDataFor2Dies(dataCount, tempLinks, die0Size, die1Size);
158 : }
159 0 : RankSliceInfo die0SliceInfoVec;
160 0 : RankSliceInfo die1SliceInfoVec;
161 0 : CHK_RET(CalcSlice(die0Size, die0SliceInfoVec));
162 0 : CHK_RET(CalcSlice(die1Size, die1SliceInfoVec));
163 0 : uint64_t inputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.inBuffType) + tempAlgParams.buffInfo.inBuffBaseOff;
164 0 : uint64_t outputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.outBuffType) + tempAlgParams.buffInfo.outBuffBaseOff;
165 0 : uint64_t repeatNum = tempAlgParams.repeatNum;
166 : uint64_t token;
167 0 : CHK_RET(GetToken(op_, token));
168 :
169 0 : HCCL_INFO(
170 : "[CcuTempAllReduceNHRMem2Mem1D] dimSize[%llu], die0Size[%llu], die1Size[%llu], inputAddr[%llu],"
171 : "outputAddr[%llu], repeatNum[%llu], die0Slicesize[%llu], die1Slicesize[%llu], die0LastSlicesize[%llu],"
172 : "die1LastSlicesize[%llu]",
173 : dimSize[0], die0Size, die1Size, inputAddr, outputAddr, repeatNum, die0SliceInfoVec[0][0].size,
174 : die1SliceInfoVec[0][0].size, die0SliceInfoVec[tempRankSize_ - 1][0].size,
175 : die1SliceInfoVec[tempRankSize_ - 1][0].size);
176 :
177 0 : std::vector<LinkData> linksDie0;
178 0 : std::vector<LinkData> linksDie1;
179 0 : RankGroup rankGroup;
180 0 : std::map<u32, u32> indexMap;
181 0 : std::vector<NHRStepInfo> stepInfoVector;
182 :
183 0 : CHK_RET(ProcessNHRStepInfo(stepInfoVector, rankGroup, indexMap, linksDie0, linksDie1, tempLinks, axisSize));
184 :
185 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
186 :
187 0 : for (uint32_t axisId = 0; axisId < axisSize; axisId++) { // 2个die上各一个mission
188 0 : if ((axisId == 0 && die0Size == 0) || (axisId == 1 && die1Size == 0)) {
189 0 : continue;
190 : }
191 :
192 0 : CcuInstructionAllReduceNHR1D ccuInstruction;
193 0 : uint64_t isInputOutputEqual = (inputAddr == outputAddr) ? 1 : 0;
194 :
195 0 : ccuInstruction.Init(
196 0 : myVirtRankId, inputAddr, outputAddr, axisId, axisSize, die0Size, die1Size, die0SliceInfoVec[0][0].size,
197 0 : die1SliceInfoVec[0][0].size, die0SliceInfoVec[tempRankSize_ - 1][0].size,
198 0 : die1SliceInfoVec[tempRankSize_ - 1][0].size, stepInfoVector, indexMap, token, isInputOutputEqual, op_,
199 0 : tempVTopo_);
200 0 : ccuInstruction.SetLinks(axisId == 0 ? linksDie0 : linksDie1);
201 0 : ccuInstruction.SetRankGroup(rankGroup);
202 0 : ccuInstruction.SetCntCkeNum(5); // 每个transport用5个CKE
203 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionAllReduceNHR1D>(ccuInstruction)));
204 0 : }
205 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
206 0 : HCCL_INFO("[CcuTempAllReduceNHRMem2Mem1D] Template Run for all steps Ends.");
207 0 : return HcclResult::HCCL_SUCCESS;
208 0 : }
209 :
210 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::GetStepInfo(u32 step, u32 nSteps, NHRStepInfo& stepInfo)
211 : {
212 0 : u32 nStepsNHR = nSteps / 2;
213 0 : u32 realStep = step;
214 0 : if (realStep < nStepsNHR) {
215 0 : CHK_RET(GetReduceScatterStepInfo(realStep, stepInfo));
216 : } else {
217 0 : realStep = step % nStepsNHR;
218 0 : CHK_RET(GetAllGatherStepInfo(realStep, nStepsNHR, stepInfo));
219 : }
220 0 : return HcclResult::HCCL_SUCCESS;
221 : }
222 :
223 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::GetReduceScatterStepInfo(u32 step, NHRStepInfo& stepInfo)
224 : {
225 0 : u32 virtRankIdx = tempVirtRankMap_[myRank_];
226 0 : stepInfo.txSliceIdxs.clear();
227 0 : stepInfo.rxSliceIdxs.clear();
228 0 : stepInfo.step = step;
229 0 : stepInfo.myRank = virtRankIdx;
230 :
231 : // AllReduceNHR计算通信对象
232 0 : u32 deltaRank = 1 << step;
233 0 : u32 sendTo = (virtRankIdx + tempRankSize_ - deltaRank) % tempRankSize_;
234 0 : u32 recvFrom = (virtRankIdx + deltaRank) % tempRankSize_;
235 :
236 : // AllReduceNHR数据份数和数据编号增量
237 0 : u32 nSlices = (tempRankSize_ - 1 + (1 << step)) / (1 << (step + 1));
238 0 : u32 deltaSliceIndex = 1 << (step + 1);
239 0 : u32 rxSliceIdx = virtRankIdx;
240 0 : u32 txSliceIdx = (virtRankIdx - (1 << step) + tempRankSize_) % tempRankSize_;
241 :
242 0 : stepInfo.nSlices = nSlices;
243 0 : stepInfo.toRank = sendTo;
244 0 : stepInfo.fromRank = recvFrom;
245 :
246 0 : for (u32 i = 0; i < nSlices; i++) {
247 0 : stepInfo.txSliceIdxs.push_back(txSliceIdx);
248 0 : stepInfo.rxSliceIdxs.push_back(rxSliceIdx);
249 :
250 0 : HCCL_DEBUG(
251 : "[AllReduceNHR][GetReduceScatterStepInfo] i[%u] txSliceIdx[%u] rxSliceIdx[%u]", i, txSliceIdx, rxSliceIdx);
252 :
253 0 : txSliceIdx = (txSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
254 0 : rxSliceIdx = (rxSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
255 : }
256 0 : return HcclResult::HCCL_SUCCESS;
257 : }
258 :
259 0 : HcclResult CcuTempAllReduceNHRMem2Mem1D::GetAllGatherStepInfo(u32 step, u32 nSteps, NHRStepInfo& stepInfo)
260 : {
261 0 : u32 virtRankIdx = tempVirtRankMap_[myRank_];
262 0 : stepInfo.txSliceIdxs.clear();
263 0 : stepInfo.rxSliceIdxs.clear();
264 0 : stepInfo.step = step;
265 0 : stepInfo.myRank = virtRankIdx;
266 :
267 : // AllReduceNHR计算通信对象
268 0 : u32 deltaRank = 1 << (nSteps - 1 - step);
269 0 : u32 recvFrom = (virtRankIdx + tempRankSize_ - deltaRank) % tempRankSize_;
270 0 : u32 sendTo = (virtRankIdx + deltaRank) % tempRankSize_;
271 :
272 : // AllReduceNHR数据份数和数据编号增量
273 0 : u32 nSlices = (tempRankSize_ - 1 + (1 << (nSteps - 1 - step))) / (1 << (nSteps - step));
274 0 : u32 deltaSliceIndex = 1 << (nSteps - step);
275 0 : u32 txSliceIdx = virtRankIdx;
276 0 : u32 rxSliceIdx = (virtRankIdx - (1 << (nSteps - 1 - step)) + tempRankSize_) % tempRankSize_;
277 :
278 0 : stepInfo.nSlices = nSlices;
279 0 : stepInfo.toRank = sendTo;
280 0 : stepInfo.fromRank = recvFrom;
281 :
282 0 : for (u32 i = 0; i < nSlices; i++) {
283 0 : stepInfo.txSliceIdxs.push_back(txSliceIdx);
284 0 : stepInfo.rxSliceIdxs.push_back(rxSliceIdx);
285 :
286 0 : HCCL_DEBUG(
287 : "[AllReduceNHR][GetAllGatherStepInfo] i[%u] txSliceIdx[%u] rxSliceIdx[%u]", i, txSliceIdx, rxSliceIdx);
288 :
289 0 : txSliceIdx = (txSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
290 0 : rxSliceIdx = (rxSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
291 : }
292 0 : return HcclResult::HCCL_SUCCESS;
293 : }
294 :
295 : } // namespace Hccl
|