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