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