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