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