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