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