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