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