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