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 <ios>
12 : #include <iostream>
13 :
14 : #include "log.h"
15 : #include "executor_utils.h"
16 :
17 : #include "ccu_instruction_all_reduce_mesh1d_detour.h"
18 : #include "ccu_rank_group.h"
19 : #include "ccu_ctx_creator_registry.h"
20 : #include "ccu_context_all_reduce_mesh1d_detour.h"
21 : #include "ccu_temp_all_reduce_mesh_detour_1D.h"
22 :
23 : namespace Hccl {
24 :
25 : constexpr uint64_t MS_SIZE = 4096;
26 : constexpr u32 DETOUR_RANK_SIZE_2P = 2;
27 : constexpr u32 DETOUR_SPLIT_LINK_NUM = 2;
28 :
29 : static CcuInstRegister<CcuContextAllReduceMeshDetour1D>
30 : g_registrarAllReduce(CcuInstType::CCU_ALL_REDUCE_MESH_1D_DETOUR);
31 :
32 0 : CcuTempAllReduceMeshDetour1D::CcuTempAllReduceMeshDetour1D(
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 0 : {}
37 :
38 0 : CcuTempAllReduceMeshDetour1D::~CcuTempAllReduceMeshDetour1D() {}
39 :
40 0 : void CcuTempAllReduceMeshDetour1D::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
41 : {
42 0 : reduceOp_ = reduceOp;
43 0 : dataType_ = dataType;
44 0 : }
45 :
46 0 : HcclResult CcuTempAllReduceMeshDetour1D::CalcResDetour(ConnectedLinkMgr* linkMgr, AlgTempResReq& tempResReq)
47 : {
48 : (void)linkMgr;
49 : (void)tempResReq;
50 0 : HCCL_INFO("[InsCollAlgFactory] Unsupported interface of resource calculation!");
51 0 : return HcclResult::HCCL_E_INTERNAL;
52 : }
53 :
54 0 : HcclResult CcuTempAllReduceMeshDetour1D::CalcResDetour(const RankGraph* rankGraph, AlgTempResReq& tempResReq)
55 : {
56 : // 当前仅支持2P或4P
57 0 : CHK_PRT_RET(
58 : tempRankSize_ != DETOUR_RANK_SIZE_2P && tempRankSize_ != 4,
59 : HCCL_INFO("[CcuTempAllReduceMeshDetour1D] Invalid RankSize[%u].", tempRankSize_), HcclResult::HCCL_E_INTERNAL);
60 :
61 0 : tempResReq.queNum = 1; // 当前只有一个ccu mission,暂定1条流
62 0 : tempResReq.streamNum = tempResReq.queNum;
63 0 : HCCL_INFO("[CcuTempAllReduceMeshDetour1D][CalcResDetour] tempResReq.queNum[%u]", tempResReq.queNum);
64 : u32 myAlgRank;
65 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[0], myAlgRank));
66 :
67 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[0].size() - 1; queIdx++) {
68 : // find neighbors : virtualRank
69 0 : RankId neighborRank = tempVTopo_[0][(myAlgRank + 1 + queIdx) % tempRankSize_];
70 0 : uint32_t linkNum = GetPathsFromRankGraph(rankGraph, myRank_, neighborRank).size();
71 0 : tempResReq.links[neighborRank] = linkNum;
72 0 : HCCL_INFO(
73 : "[CcuTempAllReduceMeshDetour1D][CalcResDetour] RankSize[%u], MyRank[%d]--Neighbor[%d], linkNum[%u]",
74 : tempRankSize_, myRank_, neighborRank, linkNum);
75 :
76 : // 2P支持2,3,4条link,4P支持2条link,注意绕路link分两条
77 0 : CHK_PRT_RET(
78 : (tempRankSize_ == DETOUR_RANK_SIZE_2P && (linkNum <= 1 || linkNum > 1 + 3 * DETOUR_SPLIT_LINK_NUM))
79 : || (tempRankSize_ == 4 && linkNum != 1 + 1 * 2), // 4P场景下,1条直连,绕路拆成2条
80 : HCCL_ERROR(
81 : "[CcuTempAllReduceMeshDetour1D][CalcResDetour] Invalid linkNum[%u] for RankSize[%u].", linkNum,
82 : tempRankSize_),
83 : HcclResult::HCCL_E_INTERNAL);
84 0 : if (queIdx == 0) {
85 0 : detourPathNum_ = (tempRankSize_ == DETOUR_RANK_SIZE_2P) ? (linkNum - 1) / 2 :
86 : 1; // 2P时去掉直连有2N条绕路link,对应N个绕路路径
87 0 : pathNumPerPeer_ = (tempRankSize_ == DETOUR_RANK_SIZE_2P) ? (detourPathNum_ + 1) :
88 0 : detourPathNum_ + 2; // 4P直连有2条,固定3条
89 0 : HCCL_INFO(
90 : "[CcuTempAllReduceMeshDetour1D][CalcResDetour] detourPathNum[%u], pathNum[%u]", detourPathNum_,
91 : pathNumPerPeer_);
92 : }
93 : }
94 :
95 0 : return HcclResult::HCCL_SUCCESS;
96 : }
97 :
98 0 : HcclResult CcuTempAllReduceMeshDetour1D::CalcSliceInfo(
99 : const AllignInfo& allignInfo, const u64 dataSize, RankSliceInfo& sliceInfoVec)
100 : {
101 0 : CHK_RET(CalcSliceInfoAllReduce(allignInfo, tempRankSize_, dataSize, sliceInfoVec)); // ***
102 0 : return HcclResult::HCCL_SUCCESS;
103 : }
104 :
105 0 : void CcuTempAllReduceMeshDetour1D::CalcDetourOffset(
106 : uint64_t sliceSize, uint64_t& tailOffset, uint64_t& tailSize, uint64_t& iterNum)
107 : {
108 0 : uint64_t loopSize = pathNumPerPeer_ * MS_SIZE * CcuRep::CCU_MS_DEFAULT_LOOP_COUNT; // 整块迭代
109 0 : tailSize = sliceSize % loopSize;
110 0 : tailOffset = sliceSize - tailSize;
111 0 : iterNum = sliceSize / loopSize;
112 :
113 0 : singleTransportSize_ = 0;
114 0 : lengths_.clear(); // 多轮情况下每轮都需要清零
115 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
116 0 : lengths_.emplace_back(MS_SIZE);
117 0 : singleTransportSize_ += MS_SIZE;
118 : }
119 0 : return;
120 : }
121 :
122 0 : void CcuTempAllReduceMeshDetour1D::ProcessLinks(std::vector<LinkData>& links, const ResLinks& tempLinks) const
123 : {
124 : // 整理links,要区分sendOnly与recvOnly,根据读写操作选择不同的绕路link
125 : // 固定2P用2-4条链路,每个链路用一个ms;4P用2条链路,其中直连用2个ms,绕路用1个
126 0 : std::vector<LinkData> directLinks;
127 0 : std::vector<LinkData> sendLinks; // sendOnly
128 0 : std::vector<LinkData> recvLinks; // recvOnly
129 0 : for (auto& pair : tempLinks) {
130 0 : if (pair.second.empty()) {
131 0 : continue;
132 : }
133 0 : HCCL_INFO(
134 : "[CcuTempAllReduceMeshDetour1D][ProcessLinks] rankId[%d], linkSize[%zu]", pair.first, pair.second.size());
135 0 : for (uint32_t i = 0; i < pair.second.size(); i++) {
136 0 : LinkData curLink = pair.second[i];
137 0 : if (curLink.GetHop() == 1) {
138 0 : directLinks.emplace_back(curLink);
139 0 : } else if (curLink.GetDirection() == LinkDirection::SEND_ONLY) {
140 0 : sendLinks.emplace_back(curLink);
141 0 : } else if (curLink.GetDirection() == LinkDirection::RECV_ONLY) {
142 0 : recvLinks.emplace_back(curLink);
143 : } else {
144 0 : THROW<InvalidParamsException>(StringFormat(
145 : "[CcuTempAllReduceMeshDetour1D][ProcessLinks] Rank[%d]--Peer[%d]--link[%d], unexpected link type.",
146 0 : myRank_, pair.first, i));
147 : }
148 : }
149 : }
150 :
151 : // 校验link
152 0 : if (sendLinks.size() != recvLinks.size() || directLinks.size() != tempRankSize_ - 1
153 0 : || sendLinks.size() % directLinks.size() != 0 || recvLinks.size() % directLinks.size() != 0) {
154 0 : THROW<InvalidParamsException>(StringFormat(
155 : "[CcuTempAllReduceMeshDetour1D][ProcessLinks] Unexpected "
156 : "directLinkSize[%u]--sendLinkSize[%u]--recvLinkSize[%u].",
157 : directLinks.size(), sendLinks.size(), recvLinks.size()));
158 : }
159 0 : for (uint32_t i = 0; i < directLinks.size(); i++) {
160 0 : HCCL_INFO(
161 : "[CcuTempAllReduceMeshDetour1D][ProcessLinks] directLinks[%u]: peer[%d], linkType[%s]", i,
162 : directLinks[i].GetRemoteRankId(), directLinks[i].GetDirection().Describe().c_str());
163 0 : links.emplace_back(directLinks[i]);
164 : }
165 0 : for (uint32_t i = 0; i < sendLinks.size(); i++) {
166 0 : HCCL_INFO(
167 : "[CcuTempAllReduceMeshDetour1D][ProcessLinks] sendLinks[%u]: peer[%d], linkType[%s]", i,
168 : sendLinks[i].GetRemoteRankId(), sendLinks[i].GetDirection().Describe().c_str());
169 0 : links.emplace_back(sendLinks[i]);
170 : }
171 0 : for (uint32_t i = 0; i < recvLinks.size(); i++) {
172 0 : HCCL_INFO(
173 : "[CcuTempAllReduceMeshDetour1D][ProcessLinks] recvLinks[%u]: peer[%d], linkType[%s]", i,
174 : recvLinks[i].GetRemoteRankId(), recvLinks[i].GetDirection().Describe().c_str());
175 0 : links.emplace_back(recvLinks[i]);
176 : }
177 :
178 0 : return;
179 0 : }
180 :
181 0 : void CcuTempAllReduceMeshDetour1D::GetAddrInfo(const TempFuncs& tempFuncs, uint64_t& inputAddr, uint64_t& outputAddr)
182 : {
183 0 : if (opMode_ == OpMode::OPBASE) {
184 0 : if (tempFuncs.isBottom) {
185 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
186 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
187 : } else {
188 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
189 : }
190 0 : if (tempFuncs.isForepart) {
191 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType())
192 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
193 : } else {
194 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
195 : }
196 : } else {
197 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
198 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff
199 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
200 : }
201 0 : HCCL_INFO("inputAddr[%llu], outputAddr[%llu]", inputAddr, outputAddr);
202 0 : return;
203 : }
204 :
205 0 : HcclResult CcuTempAllReduceMeshDetour1D::Run(
206 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
207 : std::vector<InsQuePtr>& tempInsQues)
208 : {
209 0 : CHK_PRT_RET(
210 : tempInsQues.empty(), HCCL_ERROR("[CcuTempAllReduceMeshDetour1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
211 0 : CHK_PTR_NULL(tempInsQues[0]);
212 0 : opMode_ = tempFuncs.opMode;
213 0 : buffInfo_ = buffInfo;
214 0 : CcuInstructionAllReduceMeshDetour1D ccuInsAllReduceMeshDetour1D;
215 0 : std::vector<uint64_t> dimSize;
216 0 : dimSize.push_back(tempRankSize_);
217 : uint64_t inputAddr;
218 : uint64_t outputAddr;
219 0 : GetAddrInfo(tempFuncs, inputAddr, outputAddr);
220 :
221 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size; // 获取本rank需要处理的数据量
222 0 : uint64_t offset = sliceInfoVec[myRank_][0].offset; // 自己需要 reduce 的数据基于 inputAddr 的偏移
223 : uint64_t token;
224 0 : CHK_RET(GetToken(op_, token));
225 :
226 : uint64_t tailOffset;
227 : uint64_t tailSize;
228 : uint64_t iterNum;
229 0 : CalcDetourOffset(sliceSize, tailOffset, tailSize, iterNum);
230 0 : std::vector<LinkData> links;
231 0 : ProcessLinks(links, tempLinks);
232 :
233 0 : ccuInsAllReduceMeshDetour1D.Init(
234 0 : static_cast<uint32_t>(myRank_), inputAddr, outputAddr, offset, token, op_, tempVTopo_, iterNum, tailOffset,
235 0 : tailSize, singleTransportSize_, detourPathNum_, pathNumPerPeer_, lengths_);
236 0 : HCCL_INFO(
237 : "[CcuTempAllReduceMeshDetour1D] Run Init: myRank_[%d], dimSize[%llu], inputAddr[%llu], outputAddr[%llu],"
238 : "sliceSize[%llu], offset[%llu], iterNum[%llu], tailOffset[%llu], tailSize[%llu], singleTransportSize_[%u], "
239 : "detourPathNum_[%u], pathNumPerPeer_[%u]",
240 : myRank_, dimSize[0], inputAddr, outputAddr, sliceSize, offset, iterNum, tailOffset, tailSize,
241 : singleTransportSize_, detourPathNum_, pathNumPerPeer_);
242 :
243 0 : HCCL_INFO("[CcuTempAllReduceMeshDetour1D] links.size[%zu]", links.size());
244 0 : ccuInsAllReduceMeshDetour1D.SetLinks(links);
245 :
246 0 : RankGroup rankGroup;
247 :
248 0 : for (auto& peer : tempVTopo_[0]) {
249 0 : rankGroup.AddRank(peer);
250 : }
251 0 : u32 cntCkeNum = 4;
252 0 : ccuInsAllReduceMeshDetour1D.SetCntCkeNum(cntCkeNum);
253 0 : ccuInsAllReduceMeshDetour1D.SetRankGroup(rankGroup);
254 0 : HCCL_INFO("CCUInsAllReduceMeshDetour1D is [%s]", ccuInsAllReduceMeshDetour1D.Describe().c_str());
255 0 : ccuInsAllReduceMeshDetour1D.Describe();
256 0 : tempInsQues[0]->Append(
257 0 : std::move(std::make_unique<CcuInstructionAllReduceMeshDetour1D>(ccuInsAllReduceMeshDetour1D)));
258 0 : return HcclResult::HCCL_SUCCESS;
259 0 : }
260 :
261 : } // namespace Hccl
|