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