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_reduce_scatter_nhr1d_mem2mem.h"
20 : #include "ccu_temp_reduce_scatter_nhr_1D_mem2mem.h"
21 :
22 : namespace Hccl {
23 :
24 : static CcuInstRegister<CcuContextReduceScatterNHR1DMem2Mem> g_registrarReduceScatter(
25 : CcuInstType::CCU_REDUCE_SCATTER_NHR_1D_MEM2MEM);
26 :
27 0 : CcuTempReduceScatterNHR1DMem2Mem::CcuTempReduceScatterNHR1DMem2Mem(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 : CcuTempReduceScatterNHR1DMem2Mem::~CcuTempReduceScatterNHR1DMem2Mem()
35 : {
36 0 : }
37 :
38 0 : u32 CcuTempReduceScatterNHR1DMem2Mem::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType)
39 : {
40 : (void) inBuffType;
41 : (void) outBuffType;
42 0 : return 0;
43 : }
44 :
45 0 : void CcuTempReduceScatterNHR1DMem2Mem::InitReduceInfo(const ReduceOp &reduceOp, const DataType &dataType)
46 : {
47 0 : reduceOp_ = reduceOp;
48 0 : dataType_ = dataType;
49 0 : }
50 :
51 0 : HcclResult CcuTempReduceScatterNHR1DMem2Mem::CalcRes(AlgTempResReq &tempResReq)
52 : {
53 0 : tempResReq.queNum = 1;
54 0 : tempResReq.streamNum = tempResReq.queNum;
55 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
56 : // 由于出框暂时只有一条Die,所以此处暂时实现单Die,改为1
57 0 : u32 linkNum = 1;
58 0 : linkNum_ = linkNum;
59 0 : u32 linkSize = 2;
60 0 : if (linkNum == linkSize) {
61 0 : isSipportTwoDie_ = true;
62 : }
63 0 : linkNumBtwPeers_ = linkNum;
64 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
65 0 : return HcclResult::HCCL_SUCCESS;
66 : }
67 :
68 0 : uint64_t CcuTempReduceScatterNHR1DMem2Mem::GetMaxSliceSize() const
69 : {
70 0 : return UB_MAX_DATA_SIZE;
71 : }
72 :
73 0 : HcclResult CcuTempReduceScatterNHR1DMem2Mem::GenExtIns(const TempFuncs &tempFuncs, TemplateDataParams &tempAlgParams,
74 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
75 : {
76 0 : HCCL_INFO("[CcuTempReduceScatterNHRMem2Mem1D] Template Run start.");
77 0 : CHK_PRT_RET(tempInsQues.empty(),
78 : HCCL_ERROR("[CcuTempReduceScatterNHRMem2Mem1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
79 0 : CHK_PTR_NULL(tempInsQues[0]);
80 0 : uint64_t isBottom = tempFuncs.isBottom;
81 0 : opMode_ = tempFuncs.opMode;
82 0 : std::vector<uint64_t> dimSize;
83 0 : dimSize.push_back(tempRankSize_);
84 0 : if (tempAlgParams.sliceSize == 0) {
85 0 : HCCL_INFO("[CcuTempReduceScatterNHRMem2Mem1D] sliceSize is 0, no need do, just success.");
86 0 : return HCCL_SUCCESS;
87 : }
88 0 : uint64_t die0Size = 0;
89 0 : uint64_t die1Size = 0;
90 0 : uint64_t dieNum = 2;
91 0 : if (isSipportTwoDie_) {
92 0 : die0Size = tempAlgParams.sliceSize / dieNum;
93 0 : die1Size = tempAlgParams.sliceSize - die0Size;
94 : } else {
95 0 : die0Size = tempAlgParams.sliceSize;
96 : }
97 0 : uint64_t inputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.inBuffType) + tempAlgParams.buffInfo.inBuffBaseOff;
98 0 : uint64_t outputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.outBuffType) + tempAlgParams.buffInfo.outBuffBaseOff;
99 0 : uint64_t repeatNum = tempAlgParams.repeatNum;
100 0 : uint64_t inputSliceStride = tempAlgParams.inputSliceStride;
101 0 : uint64_t outputSliceStride = tempAlgParams.outputSliceStride;
102 0 : uint64_t inputRepeatStride = tempAlgParams.inputRepeatStride;
103 0 : uint64_t outputRepeatStride = tempAlgParams.outputRepeatStride;
104 : uint64_t token;
105 0 : CHK_RET(GetToken(op_, token));
106 0 : uint64_t repeatNumVar = UINT64_MAX - repeatNum;
107 0 : HCCL_INFO("[CcuTempReduceScatterNHR1D] dimSize[%llu], die0Size[%llu], die1Size[%llu], inputAddr[%llu],"\
108 : "outputAddr[%llu], repeatNum[%llu], inputSliceStride[%llu], outputSliceStride[%llu],"\
109 : "inputRepeatStride[%llu], outputRepeatStride[%llu]",
110 : dimSize[0], die0Size, die1Size, inputAddr, outputAddr, repeatNum, inputSliceStride,
111 : outputSliceStride, inputRepeatStride, outputRepeatStride);
112 :
113 0 : std::vector<LinkData> linksDie0;
114 0 : std::vector<LinkData> linksDie1;
115 0 : RankGroup rankGroup;
116 0 : std::map<u32, u32> indexMap;
117 0 : std::vector<NHRStepInfo> stepInfoVector;
118 0 : u32 nSteps = GetNHRStepNum(tempRankSize_);
119 0 : for (u32 step = 0; step < nSteps; step++) {
120 0 : NHRStepInfo stepInfo;
121 0 : CHK_RET(GetStepInfo(step, stepInfo));
122 0 : stepInfoVector.push_back(stepInfo);
123 0 : if (indexMap.count(stepInfo.fromRank) == 0) {
124 0 : indexMap[stepInfo.fromRank] = linksDie0.size();
125 0 : linksDie0.push_back(tempLinks.at(GetRankFromMap(stepInfo.fromRank))[0]);
126 0 : if (isSipportTwoDie_) {
127 0 : linksDie1.push_back(tempLinks.at(GetRankFromMap(stepInfo.fromRank))[1]);
128 : }
129 0 : rankGroup.AddRank(GetRankFromMap(stepInfo.fromRank));
130 : }
131 0 : if (indexMap.count(stepInfo.toRank) == 0) {
132 0 : indexMap[stepInfo.toRank] = linksDie0.size();
133 0 : linksDie0.push_back(tempLinks.at(GetRankFromMap(stepInfo.toRank))[0]);
134 0 : if (isSipportTwoDie_) {
135 0 : linksDie1.push_back(tempLinks.at(GetRankFromMap(stepInfo.toRank))[1]);
136 : }
137 0 : rankGroup.AddRank(GetRankFromMap(stepInfo.toRank));
138 : }
139 0 : }
140 0 : rankGroup.AddRank(myRank_);
141 :
142 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
143 0 : for (uint32_t axisId = 0; axisId < linkNum_; axisId++) { // 2D算法,需要下发2条通信指令
144 0 : CcuInstructionReduceScatterNHR1D ccuInstruction;
145 0 : ccuInstruction.Init(tempVirtRankMap_[myRank_], inputAddr, outputAddr, axisId, die0Size, die1Size, repeatNumVar,
146 : inputSliceStride, outputSliceStride, inputRepeatStride, outputRepeatStride, stepInfoVector,
147 0 : indexMap, token, op_, tempVTopo_, linkNum_, isBottom);
148 0 : ccuInstruction.SetLinks(axisId == 0 ? linksDie0 : linksDie1);
149 0 : ccuInstruction.SetRankGroup(rankGroup);
150 0 : ccuInstruction.SetCntCkeNum(5); // 每个transport用5个CKE
151 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionReduceScatterNHR1D>(ccuInstruction)));
152 0 : }
153 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
154 0 : HCCL_INFO("[CcuTempReduceScatterNHRMem2Mem1D] Template Run for all steps Ends.");
155 0 : return HcclResult::HCCL_SUCCESS;
156 0 : }
157 :
158 0 : HcclResult CcuTempReduceScatterNHR1DMem2Mem::GetStepInfo(u32 step, NHRStepInfo &stepInfo)
159 : {
160 : // 将本rank号转换成算法使用的索引号
161 0 : u32 rankIdx = tempVirtRankMap_[myRank_];
162 0 : stepInfo.txSliceIdxs.clear();
163 0 : stepInfo.rxSliceIdxs.clear();
164 0 : stepInfo.step = step;
165 0 : stepInfo.myRank = rankIdx;
166 :
167 : // 计算通信对象
168 0 : u32 deltaRank = 1 << step;
169 0 : u32 sendTo = (rankIdx + tempRankSize_ - deltaRank) % tempRankSize_;
170 0 : u32 recvFrom = (rankIdx + deltaRank) % tempRankSize_;
171 :
172 : // 数据份数和数据编号增量
173 0 : u32 nSlices = (tempRankSize_ - 1 + (1 << step)) / (1 << (step + 1));
174 0 : u32 deltaSliceIndex = 1 << (step + 1);
175 0 : u32 txSliceIdx = sendTo;
176 0 : u32 rxSliceIdx = rankIdx;
177 :
178 0 : stepInfo.nSlices = nSlices;
179 0 : stepInfo.toRank = sendTo;
180 0 : stepInfo.fromRank = recvFrom;
181 :
182 : // 计算本rank在本轮收/发中的slice编号
183 0 : for (u32 i = 0; i < nSlices; i++) {
184 0 : stepInfo.txSliceIdxs.push_back(txSliceIdx);
185 0 : stepInfo.rxSliceIdxs.push_back(rxSliceIdx);
186 0 : HCCL_INFO("[ReduceScatterNHR1D][GetStepInfo] i[%u] txSliceIdx[%u] rxSliceIdx[%u]", i, txSliceIdx, rxSliceIdx);
187 0 : txSliceIdx = (txSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
188 0 : rxSliceIdx = (rxSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
189 : }
190 0 : return HcclResult::HCCL_SUCCESS;
191 : }
192 :
193 0 : RankId CcuTempReduceScatterNHR1DMem2Mem::GetRankFromMap(const u32 rankIdx)
194 : {
195 0 : RankId rank = -1;
196 0 : for (auto &pair : tempVirtRankMap_) {
197 0 : if (pair.second == rankIdx) {
198 0 : rank = pair.first;
199 0 : break;
200 : }
201 : }
202 0 : return rank;
203 : }
204 : } // namespace Hccl
|