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_reduce_scatter_mesh1d_detour.h"
18 : #include "ccu_rank_group.h"
19 : #include "ccu_ctx_creator_registry.h"
20 : #include "ccu_context_reduce_scatter_mesh1d_detour.h"
21 : #include "ccu_temp_reduce_scatter_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<CcuContextReduceScatterMeshDetour1D>
30 : g_registrarReduceScatter(CcuInstType::CCU_REDUCE_SCATTER_MESH_1D_DETOUR);
31 :
32 0 : CcuTempReduceScatterMeshDetour1D::CcuTempReduceScatterMeshDetour1D(
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 : CcuTempReduceScatterMeshDetour1D::~CcuTempReduceScatterMeshDetour1D() {}
39 :
40 0 : void CcuTempReduceScatterMeshDetour1D::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
41 : {
42 0 : reduceOp_ = reduceOp;
43 0 : dataType_ = dataType;
44 0 : }
45 :
46 0 : HcclResult CcuTempReduceScatterMeshDetour1D::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 CcuTempReduceScatterMeshDetour1D::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("[CcuTempReduceScatterMeshDetour1D] Invalid RankSize[%u].", tempRankSize_),
60 : HcclResult::HCCL_E_INTERNAL);
61 :
62 0 : tempResReq.queNum = 1; // 当前只有一个ccu mission,暂定1条流
63 0 : tempResReq.streamNum = tempResReq.queNum;
64 0 : HCCL_INFO("[CalcResDetour] tempResReq.queNum[%u]", tempResReq.queNum);
65 : u32 myAlgRank;
66 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[0], myAlgRank));
67 :
68 0 : for (u32 queIdx = 0; queIdx < tempVTopo_[0].size() - 1; queIdx++) {
69 : // find neighbors : virtualRank
70 0 : RankId neighborRank = tempVTopo_[0][(myAlgRank + 1 + queIdx) % tempRankSize_];
71 0 : uint32_t linkNum = GetPathsFromRankGraph(rankGraph, myRank_, neighborRank).size();
72 0 : tempResReq.links[neighborRank] = linkNum;
73 0 : HCCL_INFO(
74 : "[CcuTempReduceScatterMeshDetour1D][CalcResDetour] RankSize[%u], MyRank[%d]--Neighbor[%d], linkNum[%u]",
75 : tempRankSize_, myRank_, neighborRank, linkNum);
76 :
77 : // 2P支持2,3,4条link,4P支持2条link,注意绕路link分两条
78 0 : CHK_PRT_RET(
79 : (tempRankSize_ == DETOUR_RANK_SIZE_2P && (linkNum <= 1 || linkNum > 1 + 3 * DETOUR_SPLIT_LINK_NUM))
80 : || (tempRankSize_ == 4 && linkNum != 1 + 1 * 2), // 4P场景下,1条直连,绕路拆成2条
81 : HCCL_ERROR(
82 : "[CcuTempReduceScatterMeshDetour1D][CalcResDetour] Invalid linkNum[%u] for RankSize[%u].", linkNum,
83 : tempRankSize_),
84 : HcclResult::HCCL_E_INTERNAL);
85 0 : if (queIdx == 0) {
86 0 : detourPathNum_ = (tempRankSize_ == DETOUR_RANK_SIZE_2P) ? (linkNum - 1) / 2 :
87 : 1; // 2P时去掉直连有2N条绕路link,对应N个绕路路径
88 0 : pathNumPerPeer_ = (tempRankSize_ == DETOUR_RANK_SIZE_2P) ? (detourPathNum_ + 1) :
89 0 : detourPathNum_ + 2; // 4P直连有2条,固定3条
90 0 : HCCL_INFO(
91 : "[CcuTempReduceScatterMeshDetour1D][CalcResDetour] detourPathNum[%u], pathNum[%u]", detourPathNum_,
92 : pathNumPerPeer_);
93 : }
94 : }
95 :
96 0 : return HcclResult::HCCL_SUCCESS;
97 : }
98 :
99 0 : HcclResult CcuTempReduceScatterMeshDetour1D::CalcSliceInfo(
100 : const AllignInfo& allignInfo, const u64 dataSize, RankSliceInfo& sliceInfoVec)
101 : {
102 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
103 0 : sliceInfoVec.resize(tempRankSize_, tmp);
104 0 : CHK_RET(CalcRsAgSliceInfoMesh(myRank_, tempRankSize_, allignInfo, dataSize, sliceInfoVec));
105 0 : return HcclResult::HCCL_SUCCESS;
106 0 : }
107 :
108 0 : void CcuTempReduceScatterMeshDetour1D::ProcessLinks(std::vector<LinkData>& links, const ResLinks& tempLinks)
109 : {
110 : // 整理links,要区分sendOnly与recvOnly,根据读写操作选择不同的绕路link
111 : // 固定2P用2-4条链路,每个链路用一个ms;4P用2条链路,其中直连用2个ms,绕路用1个
112 0 : std::vector<LinkData> directLinks;
113 0 : std::vector<LinkData> sendLinks; // sendOnly
114 0 : std::vector<LinkData> recvLinks; // recvOnly
115 0 : for (auto& pair : tempLinks) {
116 0 : if (pair.second.empty()) {
117 0 : continue;
118 : }
119 0 : HCCL_INFO(
120 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] rankId[%d], linkSize[%zu]", pair.first,
121 : 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 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] Rank[%d]--Peer[%d]--link[%d], unexpected link "
133 : "type.",
134 0 : myRank_, pair.first, i));
135 : }
136 : }
137 : }
138 0 : singleTransportSize_ = 0;
139 0 : lengths_.clear();
140 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
141 0 : lengths_.emplace_back(MS_SIZE);
142 0 : singleTransportSize_ += MS_SIZE;
143 : }
144 :
145 : // 校验link
146 0 : if (sendLinks.size() != recvLinks.size() || directLinks.size() != tempRankSize_ - 1
147 0 : || sendLinks.size() % directLinks.size() != 0 || recvLinks.size() % directLinks.size() != 0) {
148 0 : THROW<InvalidParamsException>(StringFormat(
149 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] Unexpected "
150 : "directLinkSize[%u]--sendLinkSize[%u]--recvLinkSize[%u].",
151 : directLinks.size(), sendLinks.size(), recvLinks.size()));
152 : }
153 0 : for (uint32_t i = 0; i < directLinks.size(); i++) {
154 0 : HCCL_INFO(
155 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] directLinks[%u]: peer[%d], linkType[%s]", i,
156 : directLinks[i].GetRemoteRankId(), directLinks[i].GetDirection().Describe().c_str());
157 0 : links.emplace_back(directLinks[i]);
158 : }
159 0 : for (uint32_t i = 0; i < sendLinks.size(); i++) {
160 0 : HCCL_INFO(
161 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] sendLinks[%u]: peer[%d], linkType[%s]", i,
162 : sendLinks[i].GetRemoteRankId(), sendLinks[i].GetDirection().Describe().c_str());
163 0 : links.emplace_back(sendLinks[i]);
164 : }
165 0 : for (uint32_t i = 0; i < recvLinks.size(); i++) {
166 0 : HCCL_INFO(
167 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] recvLinks[%u]: peer[%d], linkType[%s]", i,
168 : recvLinks[i].GetRemoteRankId(), recvLinks[i].GetDirection().Describe().c_str());
169 0 : links.emplace_back(recvLinks[i]);
170 : }
171 :
172 0 : return;
173 0 : }
174 :
175 0 : HcclResult CcuTempReduceScatterMeshDetour1D::Run(
176 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
177 : std::vector<InsQuePtr>& tempInsQues)
178 : {
179 0 : CHK_PRT_RET(
180 : tempInsQues.empty(), HCCL_ERROR("[CcuTempReduceScatterMeshDetour1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
181 0 : CHK_PTR_NULL(tempInsQues[0]);
182 0 : opMode_ = tempFuncs.opMode;
183 0 : buffInfo_ = buffInfo;
184 0 : CcuInstructionReduceScatterMeshDetour1D ccuInsReduceScatterMeshDetour1D;
185 0 : std::vector<uint64_t> dimSize;
186 0 : dimSize.push_back(tempRankSize_);
187 :
188 : uint64_t inputAddr;
189 : uint64_t outputAddr;
190 : uint64_t offset;
191 0 : if (opMode_ == OpMode::OPBASE) {
192 0 : if (tempFuncs.isForepart) {
193 : // 从UserIn获取数据
194 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[myRank_].GetType());
195 : // 需要加上UserIn的偏移,包含了loop偏移和rank偏移
196 0 : offset = tempFuncs.usrData.usrInSlices[myRank_].GetOffset();
197 : } else {
198 : // 从inBuff获取数据,只需要加上rank偏移
199 0 : offset = sliceInfoVec[myRank_][0].offset;
200 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
201 : }
202 0 : if (tempFuncs.isBottom) {
203 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
204 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
205 : } else {
206 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
207 : }
208 : } else {
209 0 : offset = tempFuncs.usrData.usrInSlices[myRank_].GetOffset();
210 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
211 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff
212 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
213 : }
214 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size; // 获取本rank需要处理的数据量
215 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D]inputAddr[%llu], outputAddr[%llu]", inputAddr, outputAddr);
216 : uint64_t token;
217 0 : CHK_RET(GetToken(op_, token));
218 : // 计算搬运整块的iterNum
219 0 : uint64_t loopSize = pathNumPerPeer_ * MS_SIZE * CcuRep::CCU_MS_DEFAULT_LOOP_COUNT;
220 0 : uint64_t iterNum = sliceSize / loopSize;
221 : // 计算尾块数据量tailSize
222 0 : uint64_t tailSize = sliceSize % loopSize;
223 0 : uint64_t tailOffSet = sliceSize - tailSize;
224 :
225 0 : std::vector<LinkData> links;
226 0 : ProcessLinks(links, tempLinks);
227 :
228 0 : ccuInsReduceScatterMeshDetour1D.Init(
229 0 : static_cast<uint32_t>(myRank_), inputAddr, outputAddr, offset, token, op_, tempVTopo_, iterNum, tailOffSet,
230 0 : tailSize, singleTransportSize_, detourPathNum_, pathNumPerPeer_, lengths_);
231 0 : HCCL_INFO(
232 : "[CcuTempReduceScatterMeshDetour1D] Run Init: myRank_[%d], dimSize[%llu], inputAddr[%llu], outputAddr[%llu],"
233 : "sliceSize[%llu], offset[%llu], iterNum[%llu], tailOffSet[%llu], tailSize[%llu], singleTransportSize_[%u], "
234 : "detourPathNum_[%u], pathNumPerPeer_[%u]",
235 : myRank_, dimSize[0], inputAddr, outputAddr, sliceSize, offset, iterNum, tailOffSet, tailSize,
236 : singleTransportSize_, detourPathNum_, pathNumPerPeer_);
237 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D] links.size[%zu]", links.size());
238 0 : ccuInsReduceScatterMeshDetour1D.SetLinks(links);
239 0 : RankGroup rankGroup;
240 :
241 0 : for (auto& peer : tempVTopo_[0]) {
242 0 : rankGroup.AddRank(peer);
243 : }
244 0 : u32 cntCkeNum = 4;
245 0 : ccuInsReduceScatterMeshDetour1D.SetCntCkeNum(cntCkeNum);
246 0 : ccuInsReduceScatterMeshDetour1D.SetRankGroup(rankGroup);
247 0 : ccuInsReduceScatterMeshDetour1D.Describe();
248 0 : tempInsQues[0]->Append(
249 0 : std::move(std::make_unique<CcuInstructionReduceScatterMeshDetour1D>(ccuInsReduceScatterMeshDetour1D)));
250 :
251 0 : return HcclResult::HCCL_SUCCESS;
252 0 : }
253 : } // namespace Hccl
|