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_ins_group.h"
19 : #include "ccu_context_broadcast_nhr1d_mem2mem.h"
20 : #include "ccu_temp_broadcast_nhr_1D_mem2mem.h"
21 :
22 : namespace Hccl {
23 :
24 : static CcuInstRegister<CcuContextBroadcastNHRMem2Mem1D> g_registrarReduceScatter(
25 : CcuInstType::CCU_BROADCAST_NHR_1D_MEM2MEM);
26 :
27 0 : CcuTempBroadcastNHRMem2Mem1D::CcuTempBroadcastNHRMem2Mem1D(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 : CcuTempBroadcastNHRMem2Mem1D::~CcuTempBroadcastNHRMem2Mem1D()
35 : {
36 0 : }
37 :
38 0 : u32 CcuTempBroadcastNHRMem2Mem1D::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType)
39 : {
40 : (void) inBuffType;
41 : (void) outBuffType;
42 0 : return 0;
43 : }
44 :
45 0 : HcclResult CcuTempBroadcastNHRMem2Mem1D::CalcRes(AlgTempResReq &tempResReq)
46 : {
47 0 : tempResReq.queNum = 1;
48 0 : tempResReq.streamNum = tempResReq.queNum;
49 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
50 0 : u32 linkNum = 1;
51 0 : linkNumBtwPeers_ = linkNum;
52 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
53 0 : return HcclResult::HCCL_SUCCESS;
54 : }
55 :
56 :
57 0 : uint64_t CcuTempBroadcastNHRMem2Mem1D::GetMaxSliceSize() const
58 : {
59 0 : return UB_MAX_DATA_SIZE;
60 : }
61 :
62 0 : uint32_t CcuTempBroadcastNHRMem2Mem1D::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 :
73 0 : HcclResult CcuTempBroadcastNHRMem2Mem1D::GenExtIns(const TempFuncs &tempFuncs, TemplateDataParams &tempAlgParams,
74 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
75 : {
76 0 : CHK_PRT_RET(tempInsQues.empty(),
77 : HCCL_ERROR("[CcuTempBroadcastNHRMem2Mem1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
78 0 : CHK_PTR_NULL(tempInsQues[0]);
79 0 : opMode_ = tempFuncs.opMode;
80 0 : std::vector<uint64_t> dimSize;
81 0 : dimSize.push_back(tempRankSize_);
82 :
83 0 : uint32_t axisSize = tempLinks.begin()->second.size();
84 0 : uint32_t myVirtRankId = tempVirtRankMap_[myRank_];
85 0 : uint64_t DataCount = (tempAlgParams.sliceSize / DataTypeSizeGet(dataType_));
86 0 : uint64_t die0Size = DataCount / axisSize * DataTypeSizeGet(dataType_);
87 0 : uint64_t die1Size = tempAlgParams.sliceSize - die0Size;
88 0 : uint64_t inputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.inBuffType) + tempAlgParams.buffInfo.inBuffBaseOff;
89 0 : uint64_t outputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.outBuffType) + tempAlgParams.buffInfo.outBuffBaseOff;
90 0 : uint64_t repeatNum = tempAlgParams.repeatNum;
91 0 : uint64_t die0SliceSize = die0Size / tempRankSize_;
92 0 : uint64_t die0LastSliceSize = die0Size % tempRankSize_ + die0SliceSize;
93 0 : uint64_t die1SliceSize = die1Size / tempRankSize_;
94 0 : uint64_t die1LastSliceSize = die1Size % tempRankSize_ + die1SliceSize;
95 : uint64_t token;
96 0 : CHK_RET(GetToken(op_, token));
97 :
98 0 : if (DataCount == 0) {
99 0 : HCCL_INFO("[CcuTempBroadcastNHRMem2Mem1D] DataCount == 0, Template Run Ends.");
100 0 : return HCCL_SUCCESS;
101 : }
102 0 : if (axisSize > 1 && die1Size == 0) {
103 0 : axisSize = 1;
104 : }
105 :
106 0 : HCCL_INFO("[CcuTempBroadcastNHRMem2Mem1D] dimSize[%llu], die0Size[%llu], die1Size[%llu], inputAddr[%llu],"\
107 : "outputAddr[%llu], repeatNum[%llu], die0SliceSize[%llu], die0LastSliceSize[%llu], die1SliceSize[%llu],"\
108 : "die1LastSliceSize[%llu]",
109 : dimSize[0], die0Size, die1Size, inputAddr, outputAddr, repeatNum,
110 : die0SliceSize, die0LastSliceSize,
111 : die1SliceSize, die1LastSliceSize);
112 :
113 0 : std::vector<LinkData> linksDie0;
114 0 : std::vector<LinkData> linksDie1;
115 0 : RankGroup broadcastRankGroup;
116 0 : std::map<u32, u32> indexMap;
117 0 : std::vector<NHRStepInfo> stepInfoVector;
118 0 : u32 nSteps = GetNHRStepNum(tempRankSize_) * 2; // 分为Scatter和AG两次NHR
119 :
120 0 : for (u32 step = 0; step < nSteps; step++) {
121 0 : NHRStepInfo stepInfo;
122 0 : CHK_RET(GetStepInfo(step, nSteps, stepInfo));
123 0 : stepInfoVector.push_back(stepInfo);
124 0 : if (indexMap.count(stepInfo.fromRank) == 0 && stepInfo.rxSliceIdxs.size() != 0) {
125 0 : u32 fromRankIdx = virtRankId2RankId(stepInfo.fromRank);
126 0 : indexMap[stepInfo.fromRank] = linksDie0.size();
127 0 : linksDie0.push_back(tempLinks.at(fromRankIdx)[0]);
128 0 : if (axisSize > 1) {
129 0 : linksDie1.push_back(tempLinks.at(fromRankIdx)[1]);
130 : }
131 0 : broadcastRankGroup.AddRank(fromRankIdx);
132 : }
133 0 : if (indexMap.count(stepInfo.toRank) == 0 && stepInfo.txSliceIdxs.size() != 0) {
134 0 : u32 toRankIdx = virtRankId2RankId(stepInfo.toRank);
135 0 : indexMap[stepInfo.toRank] = linksDie0.size();
136 0 : linksDie0.push_back(tempLinks.at(toRankIdx)[0]);
137 0 : if (axisSize > 1) {
138 0 : linksDie1.push_back(tempLinks.at(toRankIdx)[1]);
139 : }
140 0 : broadcastRankGroup.AddRank(toRankIdx);
141 : }
142 0 : }
143 0 : broadcastRankGroup.AddRank(myRank_);
144 :
145 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
146 0 : for (uint32_t axisId = 0; axisId < axisSize; axisId++) { // 2个die上各一个mission
147 0 : CcuInstructionBroadcastNHRMem2Mem1D ccuInstruction;
148 :
149 0 : ccuInstruction.Init(myVirtRankId, inputAddr, outputAddr, axisId, axisSize, die0Size, die1Size,
150 : die0SliceSize, die1SliceSize,
151 : die0LastSliceSize, die1LastSliceSize,
152 0 : stepInfoVector, indexMap, token, op_, tempVTopo_);
153 0 : ccuInstruction.SetLinks(axisId == 0 ? linksDie0 : linksDie1);
154 0 : ccuInstruction.SetRankGroup(broadcastRankGroup);
155 0 : ccuInstruction.SetCntCkeNum(5); // 每个transport用5个CKE
156 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionBroadcastNHRMem2Mem1D>(ccuInstruction)));
157 0 : }
158 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
159 0 : HCCL_INFO("[CcuTempBroadcastNHRMem2Mem1D] Template Run for all steps Ends.");
160 0 : return HcclResult::HCCL_SUCCESS;
161 0 : }
162 :
163 0 : HcclResult CcuTempBroadcastNHRMem2Mem1D::GetStepInfo(u32 step, u32 nSteps, NHRStepInfo &stepInfo)
164 : {
165 0 : u32 nStepsNHR = nSteps / 2;
166 0 : u32 realStep = step;
167 0 : if (realStep < nStepsNHR) {
168 0 : CHK_RET(GetScatterStepInfo(realStep, nStepsNHR, stepInfo));
169 : } else {
170 0 : realStep = step % nStepsNHR;
171 0 : CHK_RET(GetAllGatherStepInfo(realStep, nStepsNHR, stepInfo));
172 : }
173 0 : return HcclResult::HCCL_SUCCESS;
174 : }
175 :
176 0 : HcclResult CcuTempBroadcastNHRMem2Mem1D::GetScatterStepInfo(u32 step, u32 nSteps, NHRStepInfo &stepInfo)
177 : {
178 0 : u32 virtRankIdx = tempVirtRankMap_[myRank_];
179 0 : u32 rankSize = tempRankSize_;
180 0 : stepInfo.txSliceIdxs.clear();
181 0 : stepInfo.rxSliceIdxs.clear();
182 0 : stepInfo.nSlices = 0;
183 0 : stepInfo.toRank = rankSize;
184 0 : stepInfo.fromRank = rankSize;
185 0 : stepInfo.step = step;
186 0 : stepInfo.myRank = virtRankIdx;
187 :
188 0 : uint32_t rootId = tempVirtRankMap_[rootId_];
189 0 : u32 deltaRoot = (rootId + rankSize - virtRankIdx) % rankSize;
190 0 : u32 deltaRankPair = 1 << step;
191 :
192 : // 数据份数和数据编号增量
193 0 : u32 nSlices = (rankSize - 1 + (1 << step)) / (1 << (step + 1));
194 0 : u32 deltaSliceIndex = 1 << (step + 1);
195 :
196 : // 判断是否是2的幂
197 0 : u32 nRanks = 0; // 本步需要进行收/发的rank数
198 0 : bool isPerfect = (rankSize & (rankSize - 1)) == 0;
199 0 : if (!isPerfect && step == nSteps - 1) {
200 0 : nRanks = rankSize - deltaRankPair;
201 : } else {
202 0 : nRanks = deltaRankPair;
203 : }
204 :
205 0 : if (deltaRoot < nRanks) { // 需要发
206 0 : u32 sendTo = (virtRankIdx + rankSize - deltaRankPair) % rankSize;
207 0 : u32 txSliceIdx = sendTo;
208 0 : for (u32 i = 0; i < nSlices; i++) {
209 0 : u32 targetTxSliceIdx = txSliceIdx;
210 0 : stepInfo.txSliceIdxs.push_back(targetTxSliceIdx);
211 0 : txSliceIdx = (txSliceIdx + rankSize - deltaSliceIndex) % rankSize;
212 : }
213 :
214 0 : stepInfo.toRank = sendTo;
215 0 : stepInfo.nSlices = nSlices;
216 0 : } else if (deltaRoot >= deltaRankPair && deltaRoot < nRanks + deltaRankPair) { // 需要收
217 0 : u32 recvFrom = (virtRankIdx + deltaRankPair) % rankSize;
218 0 : u32 rxSliceIdx = virtRankIdx;
219 0 : for (u32 i = 0; i < nSlices; i++) {
220 0 : u32 targetRxSliceIdx = rxSliceIdx;
221 0 : stepInfo.rxSliceIdxs.push_back(targetRxSliceIdx);
222 0 : rxSliceIdx = (rxSliceIdx + rankSize - deltaSliceIndex) % rankSize;
223 : }
224 :
225 0 : stepInfo.fromRank = recvFrom;
226 0 : stepInfo.nSlices = nSlices;
227 : }
228 0 : return HcclResult::HCCL_SUCCESS;
229 : }
230 :
231 0 : HcclResult CcuTempBroadcastNHRMem2Mem1D::GetAllGatherStepInfo(u32 step, u32 nSteps, NHRStepInfo &stepInfo)
232 : {
233 0 : u32 virtRankIdx = tempVirtRankMap_[myRank_];
234 0 : stepInfo.txSliceIdxs.clear();
235 0 : stepInfo.rxSliceIdxs.clear();
236 0 : stepInfo.step = step;
237 0 : stepInfo.myRank = virtRankIdx;
238 :
239 : // BroadcastNHR计算通信对象
240 0 : u32 deltaRank = 1 << (nSteps - 1 - step);
241 0 : u32 recvFrom = (virtRankIdx + tempRankSize_ - deltaRank) % tempRankSize_;
242 0 : u32 sendTo = (virtRankIdx + deltaRank) % tempRankSize_;
243 :
244 : // BroadcastNHR数据份数和数据编号增量
245 0 : u32 nSlices = (tempRankSize_ - 1 + (1 << (nSteps - 1 - step))) / (1 << (nSteps - step));
246 0 : u32 deltaSliceIndex = 1 << (nSteps - step);
247 0 : u32 txSliceIdx = virtRankIdx;
248 0 : u32 rxSliceIdx = (virtRankIdx - (1 << (nSteps - 1 - step)) + tempRankSize_) % tempRankSize_;
249 :
250 0 : stepInfo.nSlices = nSlices;
251 0 : stepInfo.toRank = sendTo;
252 0 : stepInfo.fromRank = recvFrom;
253 :
254 0 : for (u32 i = 0; i < nSlices; i++) {
255 0 : stepInfo.txSliceIdxs.push_back(txSliceIdx);
256 0 : stepInfo.rxSliceIdxs.push_back(rxSliceIdx);
257 :
258 0 : HCCL_DEBUG("[BroadcastNHR][GetAllGatherStepInfo] i[%u] txSliceIdx[%u] rxSliceIdx[%u]", i, txSliceIdx, rxSliceIdx);
259 :
260 0 : txSliceIdx = (txSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
261 0 : rxSliceIdx = (rxSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
262 : }
263 0 : return HcclResult::HCCL_SUCCESS;
264 : }
265 :
266 :
267 : } // namespace Hccl
|