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 "ccu_temp_all_reduce_mesh_2D_one_shot.h"
12 :
13 : #include <ios>
14 : #include <iostream>
15 :
16 : #include "log.h"
17 :
18 : #include "alg_data_trans_wrapper.h"
19 :
20 : #include "ccu_rank_group.h"
21 : #include "ccu_ctx_creator_registry.h"
22 : #include "ccu_ins_group.h"
23 :
24 : #include "ccu_instruction_all_reduce_mesh2d_one_shot.h"
25 : #include "ccu_context_all_reduce_mesh2d_one_shot.h"
26 :
27 :
28 : namespace Hccl {
29 : static CcuInstRegister<CcuContextAllReduceMesh2DOneShot> g_registrarCcuAllReduce2DOneShot(
30 : CcuInstType::CCU_ALL_REDUCE_MESH_2D_ONE_SHOT_DIRECT);
31 :
32 0 : CcuTempAllReduceMesh2DOneShot::CcuTempAllReduceMesh2DOneShot(const RankId virtualRank, const u32 tempRankSize,
33 : const std::vector<std::vector<RankId>> &tempVTopo,
34 0 : const std::map<RankId, u32> &tempVirtRankMap)
35 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
36 : {
37 0 : if (tempVTopo_.size() != 2 || tempVTopo_[0].size() <= 1 || tempVTopo_[1].size() <= 1) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
38 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempAllReduceMesh2DOneShot] Rank[%d], Invalid tempVTopo "
39 : "Size[%u] or Invalid tempVTopo[0] size %u or tempVTopo[1] size %u.",
40 0 : myRank_, tempVTopo_.size(), tempVTopo_[0].size(),
41 0 : tempVTopo_[1].size()));
42 : }
43 0 : dimSize_.emplace_back(tempVTopo[0].size());
44 0 : dimSize_.emplace_back(tempVTopo[1].size());
45 0 : }
46 :
47 0 : CcuTempAllReduceMesh2DOneShot::~CcuTempAllReduceMesh2DOneShot()
48 : {
49 0 : }
50 :
51 0 : void CcuTempAllReduceMesh2DOneShot::InitReduceInfo(const ReduceOp &reduceOp, const DataType &dataType)
52 : {
53 0 : reduceOp_ = reduceOp;
54 0 : dataType_ = dataType;
55 0 : }
56 :
57 0 : HcclResult CcuTempAllReduceMesh2DOneShot::CalcSliceInfo(const AllignInfo &allignInfo, const u64 dataSize,
58 : RankSliceInfo &sliceInfoVec)
59 : {
60 : // OneShot 算法不需要对数据进行切分,每个rank上的数据量即为整个数据量
61 : (void)allignInfo;
62 : SliceInfo basicSlice;
63 0 : basicSlice.offset = 0;
64 0 : basicSlice.size = dataSize;
65 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot] basicSlice.size[%u].", basicSlice.size);
66 0 : std::vector<SliceInfo> singleRankSliceInfoVector{basicSlice};
67 0 : sliceInfoVec.resize(tempRankSize_, singleRankSliceInfoVector);
68 0 : return HcclResult::HCCL_SUCCESS;
69 0 : }
70 :
71 0 : HcclResult CcuTempAllReduceMesh2DOneShot::CalcRes(AlgTempResReq &tempResReq)
72 : {
73 : // 按照IODienum来确定stream数量,支持2D和2D的template
74 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
75 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
76 0 : uint32_t dieNum = tempVTopo_.size();
77 0 : if (dieNum != 2) { // concurrmesh的topoMatch返回的vTopo大小应当为2,对应X轴和Y轴的大小
78 0 : HCCL_ERROR("[CcuTempAllReduceMesh2DOneShot] Rank[%d], Invalid IODieNum[%zu].", myRank_, tempVTopo_.size());
79 0 : return HcclResult::HCCL_E_PARA;
80 : }
81 0 : HCCL_INFO(
82 : "[CcuTempAllReduceMesh2DOneShot] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u] VtopoSize1[%u].",
83 : myRank_, tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size(), tempVTopo_[1].size());
84 :
85 : uint32_t myAlgRank;
86 0 : for (u32 dim = 0; dim < tempVTopo_.size(); dim++) {
87 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[dim], myAlgRank));
88 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[dim].size() - 1; queIdx++) {
89 : // find neighbors -> virtualRank
90 0 : u32 neighborAlgRank = (myAlgRank + 1 + queIdx) % (tempVTopo_[dim].size());
91 0 : RankId neighborRank = tempVTopo_[dim][neighborAlgRank];
92 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot] Rank[%d], Dim[%u], NeighborRank[%d].",
93 : myRank_, dim, neighborRank);
94 : // LinkNum
95 0 : tempResReq.links[neighborRank] = 1;
96 : }
97 : }
98 0 : return HcclResult::HCCL_SUCCESS;
99 : }
100 :
101 0 : HcclResult CcuTempAllReduceMesh2DOneShot::GetBufferAddr(const TempFuncs &tempFuncs, uint64_t &inputAddr,
102 : uint64_t &outputAddr, uint64_t &scratchAddr)
103 : {
104 : uint64_t inputBaseAddr;
105 : uint64_t outputBaseAddr;
106 : uint64_t scratchBaseAddr;
107 : uint64_t inputOffSet;
108 : uint64_t scratchOffSet;
109 : uint64_t outputOffSet;
110 :
111 0 : if (opMode_ == OpMode::OPBASE) {
112 0 : if (tempFuncs.isForepart) {
113 0 : inputBaseAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType());
114 0 : inputOffSet = tempFuncs.usrData.usrInSlices[0].GetOffset();
115 : } else {
116 0 : inputBaseAddr = BufferTypeToAddr(buffInfo_.inBuffType);
117 0 : inputOffSet = buffInfo_.inBuffBaseOff;
118 : }
119 0 : if (tempFuncs.isBottom) {
120 0 : outputBaseAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType());
121 0 : outputOffSet = tempFuncs.usrData.usrOutSlices[0].GetOffset();
122 : } else {
123 0 : outputBaseAddr = BufferTypeToAddr(buffInfo_.outBuffType);
124 0 : outputOffSet = buffInfo_.outBuffBaseOff;
125 : }
126 : } else {
127 0 : outputBaseAddr = BufferTypeToAddr(buffInfo_.outBuffType);
128 0 : outputOffSet = buffInfo_.outBuffBaseOff + tempFuncs.usrData.usrOutSlices[0].GetOffset();
129 0 : inputBaseAddr = BufferTypeToAddr(buffInfo_.inBuffType);
130 0 : inputOffSet = buffInfo_.inBuffBaseOff + tempFuncs.usrData.usrInSlices[0].GetOffset();
131 : }
132 :
133 0 : scratchBaseAddr = BufferTypeToAddr(buffInfo_.scratBuffType);
134 0 : scratchOffSet = buffInfo_.scratchBuffBaseOff;
135 :
136 0 : HCCL_INFO("[GetBufferAddr] inputBaseAddr[%llu], inputOffSet[%llu], outputBaseAddr[%llu], outputOffSet[%llu], "
137 : "sctrachBaseAddr[%llu], sctrachOffSet[%llu]",
138 : inputBaseAddr, inputOffSet, outputBaseAddr, outputOffSet, scratchBaseAddr, scratchOffSet);
139 :
140 0 : inputAddr = inputBaseAddr + inputOffSet;
141 0 : outputAddr = outputBaseAddr + outputOffSet;
142 0 : scratchAddr = scratchBaseAddr + scratchOffSet;
143 :
144 0 : HCCL_INFO("[GetBufferAddr] inputAddr[%llu], outputAddr[%llu], scratchAddr[%llu]",
145 : inputAddr, outputAddr, scratchAddr);
146 0 : return HcclResult::HCCL_SUCCESS;
147 : }
148 :
149 0 : HcclResult CcuTempAllReduceMesh2DOneShot::PrepareLinks(const ResLinks &tempLinks)
150 : {
151 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot] PrepareLinks Starts.");
152 : // 分别记录两个Die上的link,构造rankGroup
153 0 : for (auto pair : tempLinks) {
154 0 : if (pair.second.size() == 0 || pair.second[0].GetHop() != 1) { // ESL环境上暂只有直连链路
155 0 : HCCL_ERROR("[CcuTempAllReduceMesh2DOneShot] Rank[%d]--Peer[%d], InvalidHop[%u].", myRank_, pair.first,
156 : pair.second[0].GetHop());
157 0 : return HcclResult::HCCL_E_PARA;
158 : }
159 0 : if ((pair.first / dimSize_[0] == myRank_ / dimSize_[0]) && pair.second[0].GetHop() == 1) {
160 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot][Run] Rank[%d] insert link to Rank[%d] in linksX", myRank_,
161 : pair.first);
162 0 : linksX_.emplace_back(pair.second[0]);
163 0 : } else if ((pair.first % dimSize_[0] == myRank_ % dimSize_[0]) && pair.second[0].GetHop() == 1) {
164 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot][Run] Rank[%d] insert link to Rank[%d] in linksY", myRank_,
165 : pair.first);
166 0 : linksY_.emplace_back(pair.second[0]);
167 : } else {
168 0 : HCCL_ERROR("[CcuTempAllReduceMesh2DOneShot] Rank[%d], Unexpected peerRank[%d] in tempLinks.", myRank_,
169 : pair.first);
170 0 : return HcclResult::HCCL_E_PARA;
171 : }
172 0 : }
173 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot] PrepareLinks Eends. linksX Size[%u], linksY Size[%u]",
174 : linksX_.size(), linksY_.size());
175 0 : return HcclResult::HCCL_SUCCESS;
176 : }
177 :
178 0 : HcclResult CcuTempAllReduceMesh2DOneShot::PrepareRankGroups()
179 : {
180 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot] PrepareRankGroups Starts.");
181 0 : for (auto &peer : tempVTopo_[0]) {
182 0 : rankGroupX_.AddRank(peer);
183 : }
184 0 : for (auto &peer : tempVTopo_[1]) {
185 0 : rankGroupY_.AddRank(peer);
186 : }
187 0 : CHK_PRT_RET(rankGroupX_.GetRanks().size() <= 1 || rankGroupY_.GetRanks().size() <= 1,
188 : HCCL_ERROR("[CcuTempAllReduceMesh2DOneShot][PrepareRankGroups] Rank[%d] RankGroupX size[%u] or RankGroupY size[%u] is not greater than 1. ",
189 : myRank_, rankGroupX_.GetRanks().size(), rankGroupY_.GetRanks().size()),
190 : HcclResult::HCCL_E_PARA);
191 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot][PrepareRankGroups] RankGroupX size[%u], RankGroupY size[%u].",
192 : rankGroupX_.GetRanks().size(), rankGroupY_.GetRanks().size());
193 0 : return HcclResult::HCCL_SUCCESS;
194 : }
195 :
196 0 : HcclResult CcuTempAllReduceMesh2DOneShot::Run(const TempFuncs &tempFuncs, const RankSliceInfo &sliceInfoVec,
197 : const BuffInfo &buffInfo, const ResLinks &tempLinks,
198 : std::vector<InsQuePtr> &tempInsQues)
199 : {
200 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot][Run] Template Run Starts.");
201 0 : opMode_ = tempFuncs.opMode;
202 0 : buffInfo_ = buffInfo;
203 :
204 0 : u32 xDimSize = dimSize_[0];
205 0 : u32 yDimSize = dimSize_[1];
206 0 : HCCL_INFO("[Run] xDimSize[%u], yDimSize[%u]", xDimSize, yDimSize);
207 :
208 0 : CHK_RET(PrepareLinks(tempLinks));
209 0 : CHK_RET(PrepareRankGroups());
210 : // 计算 buffer 地址信息
211 : uint64_t inputAddr;
212 : uint64_t outputAddr;
213 : uint64_t scratchAddr;
214 0 : CHK_RET(GetBufferAddr(tempFuncs, inputAddr, outputAddr, scratchAddr));
215 0 : HCCL_INFO("[Run] inputAddr[%llu], outputAddr[%llu], scratchAddr[%llu]", inputAddr, outputAddr, scratchAddr);
216 :
217 0 : uint64_t totalSliceSize = sliceInfoVec[myRank_][0].size; // 获取本rank需要处理的数据总量
218 0 : uint64_t totalSliceCount = totalSliceSize / DataTypeSizeGet(dataType_);
219 0 : uint64_t xSliceSize = (totalSliceCount / (xDimSize + yDimSize)) * xDimSize * DataTypeSizeGet(dataType_);
220 0 : uint64_t ySliceSize = totalSliceSize - xSliceSize;
221 0 : uint64_t xSliceOffset = 0;
222 0 : uint64_t ySliceOffset = xSliceSize;
223 : uint64_t token;
224 0 : CHK_RET(GetToken(op_, token));
225 0 : HCCL_INFO("[Run] totalSliceSize[%llu], xSliceSize[%llu], ySliceSize[%llu], xSliceOffset[%llu], "
226 : "ySliceOffset[%llu]",
227 : totalSliceSize, xSliceSize, ySliceSize, xSliceOffset, ySliceOffset);
228 :
229 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
230 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2D算法,需要下发 2 条通信指令
231 0 : CcuInstructionAllReduceMesh2DOneShot ccuInstruction;
232 0 : ccuInstruction.Init(dimSize_, myRank_, inputAddr, outputAddr, scratchAddr, axisId, xSliceSize, ySliceSize,
233 0 : xSliceOffset, ySliceOffset, token, op_, tempVTopo_);
234 0 : ccuInstruction.SetLinks(axisId == 0 ? linksX_ : linksY_);
235 0 : ccuInstruction.SetRankGroup(axisId == 0 ? rankGroupX_ : rankGroupY_);
236 0 : ccuInstruction.SetCntCkeNum(5); // 每个transport用5个CKE
237 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionAllReduceMesh2DOneShot>(ccuInstruction)));
238 0 : }
239 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
240 0 : HCCL_INFO("[CcuTempAllReduceMesh2DOneShot][Run] Template Run Ends.");
241 0 : return HcclResult::HCCL_SUCCESS;
242 0 : }
243 : } // namespace Hccl
|