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