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