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