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 :
27 : static CcuInstRegister<CcuContextReduceScatterMeshDetour1D> g_registrarReduceScatter(
28 : CcuInstType::CCU_REDUCE_SCATTER_MESH_1D_DETOUR);
29 :
30 0 : CcuTempReduceScatterMeshDetour1D::CcuTempReduceScatterMeshDetour1D(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 : CcuTempReduceScatterMeshDetour1D::~CcuTempReduceScatterMeshDetour1D()
38 : {
39 0 : }
40 :
41 0 : void CcuTempReduceScatterMeshDetour1D::InitReduceInfo(const ReduceOp &reduceOp, const DataType &dataType) {
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(tempRankSize_ != 2 && tempRankSize_ != 4,
58 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D] 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("[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("[CcuTempReduceScatterMeshDetour1D][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("[CcuTempReduceScatterMeshDetour1D][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("[CcuTempReduceScatterMeshDetour1D][CalcResDetour] detourPathNum[%u], pathNum[%u]", detourPathNum_, pathNumPerPeer_);
83 : }
84 : }
85 :
86 0 : return HcclResult::HCCL_SUCCESS;
87 : }
88 :
89 :
90 0 : HcclResult CcuTempReduceScatterMeshDetour1D::CalcSliceInfo(const AllignInfo &allignInfo, const u64 dataSize,
91 : RankSliceInfo &sliceInfoVec)
92 : {
93 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
94 0 : sliceInfoVec.resize(tempRankSize_, tmp);
95 0 : CHK_RET(CalcRsAgSliceInfoMesh(myRank_, tempRankSize_, allignInfo, dataSize, sliceInfoVec));
96 0 : return HcclResult::HCCL_SUCCESS;
97 0 : }
98 :
99 0 : void CcuTempReduceScatterMeshDetour1D::ProcessLinks(std::vector<LinkData> &links, const ResLinks &tempLinks)
100 : {
101 : // 整理links,要区分sendOnly与recvOnly,根据读写操作选择不同的绕路link
102 : // 固定2P用2-4条链路,每个链路用一个ms;4P用2条链路,其中直连用2个ms,绕路用1个
103 0 : std::vector<LinkData> directLinks;
104 0 : std::vector<LinkData> sendLinks; // sendOnly
105 0 : std::vector<LinkData> recvLinks; // recvOnly
106 0 : for (auto &pair : tempLinks) {
107 0 : if (pair.second.empty()) {
108 0 : continue;
109 : }
110 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D][ProcessLinks] rankId[%d], linkSize[%zu]", pair.first, pair.second.size());
111 0 : for (uint32_t i = 0; i < pair.second.size(); i++) {
112 0 : LinkData curLink = pair.second[i];
113 0 : if (curLink.GetHop() == 1) {
114 0 : directLinks.emplace_back(curLink);
115 0 : } else if (curLink.GetDirection() == LinkDirection::SEND_ONLY) {
116 0 : sendLinks.emplace_back(curLink);
117 0 : } else if (curLink.GetDirection() == LinkDirection::RECV_ONLY) {
118 0 : recvLinks.emplace_back(curLink);
119 : } else {
120 0 : THROW<InvalidParamsException>(StringFormat(
121 0 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] Rank[%d]--Peer[%d]--link[%d], unexpected link type.", myRank_, pair.first, i));
122 : }
123 : }
124 : }
125 0 : singleTransportSize_ = 0;
126 0 : lengths_.clear();
127 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
128 0 : lengths_.emplace_back(MS_SIZE);
129 0 : singleTransportSize_ += MS_SIZE;
130 : }
131 :
132 : // 校验link
133 0 : if (sendLinks.size() != recvLinks.size() || directLinks.size() != tempRankSize_ - 1 ||
134 0 : sendLinks.size() % directLinks.size() != 0 || recvLinks.size() % directLinks.size() != 0) {
135 0 : THROW<InvalidParamsException>(StringFormat(
136 : "[CcuTempReduceScatterMeshDetour1D][ProcessLinks] Unexpected directLinkSize[%u]--sendLinkSize[%u]--recvLinkSize[%u].",
137 : directLinks.size(), sendLinks.size(), recvLinks.size()));
138 : }
139 0 : for (uint32_t i = 0; i < directLinks.size(); i++) {
140 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D][ProcessLinks] directLinks[%u]: peer[%d], linkType[%s]",
141 : i, directLinks[i].GetRemoteRankId(), directLinks[i].GetDirection().Describe().c_str());
142 0 : links.emplace_back(directLinks[i]);
143 : }
144 0 : for (uint32_t i = 0; i < sendLinks.size(); i++) {
145 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D][ProcessLinks] sendLinks[%u]: peer[%d], linkType[%s]",
146 : i, sendLinks[i].GetRemoteRankId(), sendLinks[i].GetDirection().Describe().c_str());
147 0 : links.emplace_back(sendLinks[i]);
148 : }
149 0 : for (uint32_t i = 0; i < recvLinks.size(); i++) {
150 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D][ProcessLinks] recvLinks[%u]: peer[%d], linkType[%s]",
151 : i, recvLinks[i].GetRemoteRankId(), recvLinks[i].GetDirection().Describe().c_str());
152 0 : links.emplace_back(recvLinks[i]);
153 : }
154 :
155 0 : return;
156 0 : }
157 :
158 0 : HcclResult CcuTempReduceScatterMeshDetour1D::Run(const TempFuncs &tempFuncs, const RankSliceInfo &sliceInfoVec,
159 : const BuffInfo &buffInfo, const ResLinks &tempLinks,
160 : std::vector<InsQuePtr> &tempInsQues)
161 : {
162 0 : CHK_PRT_RET(tempInsQues.empty(),
163 : HCCL_ERROR("[CcuTempReduceScatterMeshDetour1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
164 0 : CHK_PTR_NULL(tempInsQues[0]);
165 0 : opMode_ = tempFuncs.opMode;
166 0 : buffInfo_ = buffInfo;
167 0 : CcuInstructionReduceScatterMeshDetour1D ccuInsReduceScatterMeshDetour1D;
168 0 : std::vector<uint64_t> dimSize;
169 0 : dimSize.push_back(tempRankSize_);
170 :
171 : uint64_t inputAddr;
172 : uint64_t outputAddr;
173 : uint64_t offset;
174 0 : if (opMode_ == OpMode::OPBASE) {
175 0 : if (tempFuncs.isForepart) {
176 : // 从UserIn获取数据
177 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[myRank_].GetType());
178 : // 需要加上UserIn的偏移,包含了loop偏移和rank偏移
179 0 : offset = tempFuncs.usrData.usrInSlices[myRank_].GetOffset();
180 : } else {
181 : // 从inBuff获取数据,只需要加上rank偏移
182 0 : offset = sliceInfoVec[myRank_][0].offset;
183 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
184 : }
185 0 : if (tempFuncs.isBottom) {
186 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
187 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
188 : } else {
189 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
190 : }
191 : } else {
192 0 : offset = tempFuncs.usrData.usrInSlices[myRank_].GetOffset();
193 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
194 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff + tempFuncs.usrData.usrOutSlices[0].GetOffset();
195 : }
196 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size; // 获取本rank需要处理的数据量
197 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D]inputAddr[%llu], outputAddr[%llu]", inputAddr, outputAddr);
198 : uint64_t token;
199 0 : CHK_RET(GetToken(op_, token));
200 : // 计算搬运整块的iterNum
201 0 : uint64_t loopSize = pathNumPerPeer_ * MS_SIZE * CcuRep::CCU_MS_DEFAULT_LOOP_COUNT;
202 0 : uint64_t iterNum = sliceSize / loopSize;
203 : // 计算尾块数据量tailSize
204 0 : uint64_t tailSize = sliceSize % loopSize;
205 0 : uint64_t tailOffSet = sliceSize - tailSize;
206 :
207 0 : std::vector<LinkData> links;
208 0 : ProcessLinks(links, tempLinks);
209 :
210 0 : ccuInsReduceScatterMeshDetour1D.Init(static_cast<uint32_t>(myRank_), inputAddr, outputAddr, offset, token, op_, tempVTopo_, iterNum,
211 0 : tailOffSet, tailSize, singleTransportSize_, detourPathNum_, pathNumPerPeer_, lengths_);
212 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D] Run Init: myRank_[%d], dimSize[%llu], inputAddr[%llu], outputAddr[%llu],"\
213 : "sliceSize[%llu], offset[%llu], iterNum[%llu], tailOffSet[%llu], tailSize[%llu], singleTransportSize_[%u], detourPathNum_[%u], pathNumPerPeer_[%u]",
214 : myRank_, dimSize[0], inputAddr, outputAddr, sliceSize, offset, iterNum, tailOffSet, tailSize, singleTransportSize_, detourPathNum_, pathNumPerPeer_);
215 0 : HCCL_INFO("[CcuTempReduceScatterMeshDetour1D] links.size[%zu]", links.size());
216 0 : ccuInsReduceScatterMeshDetour1D.SetLinks(links);
217 0 : RankGroup rankGroup;
218 :
219 0 : for (auto &peer : tempVTopo_[0]) {
220 0 : rankGroup.AddRank(peer);
221 : }
222 0 : u32 cntCkeNum = 4;
223 0 : ccuInsReduceScatterMeshDetour1D.SetCntCkeNum(cntCkeNum);
224 0 : ccuInsReduceScatterMeshDetour1D.SetRankGroup(rankGroup);
225 0 : ccuInsReduceScatterMeshDetour1D.Describe();
226 0 : tempInsQues[0]->Append(std::move(std::make_unique<CcuInstructionReduceScatterMeshDetour1D>(ccuInsReduceScatterMeshDetour1D)));
227 :
228 0 : return HcclResult::HCCL_SUCCESS;
229 0 : }
230 : } // namespace Hccl
|