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 "ccu_context_reduce_scatter_mesh1d_detour.h"
12 : #include "ccu_instruction_reduce_scatter_mesh1d_detour.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int INPUT_XN_ID = 0;
17 : constexpr int OUTPUT_XN_ID = 1;
18 : constexpr int TOKEN_XN_ID = 2;
19 : constexpr int CKE_IDX_0 = 0;
20 : constexpr int CKE_IDX_1 = 1;
21 : constexpr int CKE_IDX_2 = 2;
22 : constexpr int CKE_IDX_3 = 3;
23 :
24 0 : CcuContextReduceScatterMeshDetour1D::CcuContextReduceScatterMeshDetour1D(const CcuCtxArg &arg,
25 : const std::vector<CcuTransport *> &transports,
26 0 : const CcuTransportGroup &group)
27 0 : : CcuContextAlgBase(arg, transports, group)
28 : {
29 0 : const CcuCtxArgReduceScatterMeshDetour1D *ctxArg = dynamic_cast<const CcuCtxArgReduceScatterMeshDetour1D *>(&arg);
30 0 : if (ctxArg == nullptr) {
31 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMeshDetour1D::ctxArg ptr is null"));
32 : }
33 0 : rankId_ = ctxArg->rankId_;
34 0 : rankSize_ = ctxArg->dimSize_[0];
35 0 : dataType_ = ctxArg->op_.dataType;
36 0 : outputDataType_ = ctxArg->op_.outputDataType;
37 0 : if (outputDataType_ == DataType::INVALID) {
38 0 : outputDataType_ = dataType_;
39 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] outputDataType is [INVALID], set outputDataType to[%s]",
40 : outputDataType_.Describe().c_str());
41 : }
42 0 : reduceOp_ = ctxArg->op_.reduceOp;
43 0 : singleTransportSize_ = ctxArg->singleTransportSize_;
44 0 : detourPathNum_ = ctxArg->detourPathNum_;
45 0 : pathNumPerPeer_ = ctxArg->pathNumPerPeer_;
46 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] Init, CtxArgs are rankId[%u], rankSize[%u], dataType[%s], "
47 : "outputDataType[%s], reduceOp[%s]", rankId_, rankSize_, dataType_.Describe().c_str(),
48 : outputDataType_.Describe().c_str(), reduceOp_.Describe().c_str());
49 0 : if (transports.size() == 0 || transports.size() < rankSize_ - 1) {
50 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMeshDetour1D transports is empty or size is less"));
51 : }
52 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] transport.size[%zu]", transports.size());
53 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
54 : // 到每个对端有pathNum个transport,故detourTransport中共有pathNum组
55 0 : detourTransports_.emplace_back(std::vector<CcuTransport*>());
56 : }
57 0 : uint64_t directPathNum = pathNumPerPeer_ - detourPathNum_;
58 0 : for (uint64_t i = 0; i < directPathNum; i++) {
59 : // 有pathNum-detourPathNum组的直连链路,每组重复
60 0 : for (uint32_t j = 0; j < rankSize_ - 1; j++) {
61 0 : detourTransports_[i].emplace_back(transports[j]);
62 : }
63 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] Add directTransports[%llu], size[%zu]", i, detourTransports_[i].size());
64 : }
65 0 : for (uint32_t i = 0; i < detourPathNum_; i++) {
66 0 : for (uint32_t j = 0; j < rankSize_ - 1; j++) {
67 0 : detourTransports_[i + directPathNum].emplace_back(transports[(i + 1) * (rankSize_ - 1) + j]);
68 0 : detourTransports_[i + directPathNum].emplace_back(transports[(i + 1) * (rankSize_ - 1) + j + detourPathNum_ * (rankSize_ - 1)]);
69 0 : HCCL_INFO("detourTransports_ emplace_back sendLink[%u], recvLink[%u]",
70 : (i + 1) * (rankSize_ - 1) + j, (i + 1) * (rankSize_ - 1) + j + detourPathNum_ * (rankSize_ - 1));
71 : }
72 : }
73 0 : }
74 :
75 0 : void CcuContextReduceScatterMeshDetour1D::CreateMultiOpReduceDetour(DataType &dataType, DataType &outputDataType, ReduceOp &opType)
76 : {
77 0 : moConfig.loopCount = CcuRep::CCU_MS_DEFAULT_LOOP_COUNT;
78 0 : moConfig.msInterleave = pathNumPerPeer_ * rankSize_;
79 0 : if (moRes.executor.size() == 0) {
80 0 : moRes.executor = CreateBlockExecutor(moConfig.loopCount);
81 0 : moRes.maskSignal = CreateBlockMaskSignal(moConfig.loopCount);
82 0 : moRes.ccuBuffer = CreateBlockCcuBuffer(moConfig.loopCount * moConfig.msInterleave);
83 : }
84 0 : std::string loopType = "reduceDetour";
85 0 : if (registeredLoop.find(loopType) != registeredLoop.end()) {
86 0 : return;
87 : }
88 0 : CcuRep::LoopBlock lb(this, loopType + "_loop");
89 : {
90 : // loopblock的形参
91 0 : std::vector<CcuRep::Memory> src;
92 0 : std::vector<CcuRep::Memory> dst;
93 0 : std::vector<CcuRep::Variable> lengths;
94 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
95 0 : lengths.emplace_back(CreateVariable());
96 0 : dst.emplace_back(CreateMemory());
97 0 : for (uint32_t j = 0; j < rankSize_; j++) {
98 0 : src.emplace_back(CreateMemory());
99 : }
100 : }
101 :
102 0 : lb(src, dst, lengths);
103 0 : std::vector<std::vector<CcuRep::CcuBuffer>> bufs;
104 0 : bufs.resize(pathNumPerPeer_);
105 0 : std::vector<CcuRep::MaskSignal> sems;
106 :
107 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
108 0 : for (uint32_t j = 0; j < rankSize_; j++) {
109 0 : bufs[i].emplace_back(moRes.ccuBuffer[i * rankSize_ + j]);
110 : }
111 0 : sems.emplace_back(moRes.maskSignal[i]);
112 : }
113 :
114 : // 先读远端直连的到本地MS
115 0 : uint64_t directPathNum = pathNumPerPeer_ - detourPathNum_;
116 0 : for (uint32_t i = 0; i < directPathNum; i++) {
117 0 : for (uint32_t j = 0; j < detourTransports_[i].size(); j++) {
118 0 : if (detourTransports_[i][j] == nullptr) {
119 0 : THROW<CcuApiException>("transport is nullptr");
120 : }
121 0 : Read(*detourTransports_[i][j], bufs[i][j], src[i * rankSize_ + j], lengths[i], sems[i], 1 << j);
122 : }
123 : }
124 : // 再读远端绕路的到本地MS
125 0 : for (uint32_t i = directPathNum; i < pathNumPerPeer_; i++) {
126 0 : for (uint32_t j = 0; j < rankSize_ - 1; j++) {
127 0 : if (detourTransports_[i][j * 2 + 1] == nullptr) { // j * 2 + 1是recvOnly Link
128 0 : THROW<CcuApiException>("transport is nullptr");
129 : }
130 0 : Read(*detourTransports_[i][j * 2 + 1], bufs[i][j], src[i * rankSize_ + j], lengths[i], sems[i], 1 << j);
131 : }
132 : }
133 :
134 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
135 0 : LocalCopy(bufs[i][rankSize_ - 1], src[i * rankSize_ + rankSize_ - 1], lengths[i], sems[i], 1 << (rankSize_ - 1));
136 : }
137 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
138 0 : LocalWait(sems[i], (1 << rankSize_) - 1);
139 : }
140 0 : if (rankSize_ > 1) {
141 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
142 0 : LocalReduce(bufs[i], rankSize_, dataType, outputDataType, opType, sems[i], lengths[i]);
143 0 : LocalWait(sems[i]);
144 : }
145 : }
146 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
147 0 : LocalCopy(dst[i], bufs[i][0], lengths[i], sems[i]);
148 0 : LocalWait(sems[i]);
149 : }
150 0 : }
151 0 : registeredLoop.insert(loopType);
152 0 : return;
153 0 : }
154 :
155 0 : void CcuContextReduceScatterMeshDetour1D::GroupReduceDetour(std::vector<CcuRep::Memory> &src,
156 : std::vector<CcuRep::Memory> &dst, DataType &dataType, DataType &outputDataType, ReduceOp &opType)
157 : {
158 0 : CreateMultiOpReduceDetour(dataType, outputDataType, opType);
159 0 : uint32_t interLeave = 8;
160 :
161 0 : CCU_IF(iterNum_ != 0) {
162 0 : CcuRep::Variable loopParam = CreateVariable();
163 0 : CcuRep::Variable paraCfg = CreateVariable();
164 0 : CcuRep::Variable offsetCfg = CreateVariable();
165 :
166 0 : loopParam = CcuRep::GetLoopParam(0, singleTransportSize_ * moConfig.loopCount, 0); // 下次迭代的偏移是单次总搬运量*loopNum
167 0 : loopParam += iterNum_; // 加上loop的迭代次数构成完整loop参数
168 0 : paraCfg = CcuRep::GetParallelParam(moConfig.loopCount - 1, 0, 1); // loop固定展开到128个
169 0 : offsetCfg = CcuRep::GetOffsetParam(singleTransportSize_, interLeave, pathNumPerPeer_); // 下一个loop偏移量
170 0 : auto lc = Loop("reduceDetour_loop")(src, dst, lengths_);
171 0 : LoopGroup({lc}, {loopParam}, paraCfg, offsetCfg);
172 0 : }
173 0 : return;
174 0 : }
175 :
176 :
177 0 : void CcuContextReduceScatterMeshDetour1D::Algorithm()
178 : {
179 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] ReduceScatterMeshDetour1D run");
180 0 : uint16_t selfBit = 1 << rankId_;
181 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
182 0 : output_.push_back(CreateVariable());
183 : // 初始化资源
184 0 : uint16_t transportIdx = 0;
185 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
186 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
187 0 : if (peerId == rankId_) {
188 0 : input_.push_back(CreateVariable());
189 0 : token_.push_back(CreateVariable());
190 : } else {
191 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] MyRank[%u], PeerId[%llu], TransportId[%u]",
192 : rankId_, peerId, transportIdx);
193 0 : CHK_PRT_RET(detourTransports_[0][transportIdx] == nullptr,
194 : HCCL_ERROR("[CcuContextReduceScatterMeshDetour1D] Algorithm transport ptr is null"),);
195 0 : input_.push_back(CreateVariable((*detourTransports_[0][transportIdx]), INPUT_XN_ID));
196 0 : token_.push_back(CreateVariable((*detourTransports_[0][transportIdx]), TOKEN_XN_ID));
197 0 : transportIdx++;
198 : }
199 : }
200 0 : offset_ = CreateVariable();
201 0 : iterNum_ = CreateVariable();
202 0 : tailOffset_ = CreateVariable();
203 0 : tailSize_ = CreateVariable();
204 0 : groupOpSize_ = CreateGroupOpSize();
205 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
206 0 : lengths_.emplace_back(CreateVariable());
207 : }
208 :
209 0 : Load(input_[rankId_]);
210 0 : Load(output_[0]);
211 0 : Load(token_[rankId_]);
212 0 : Load(offset_);
213 0 : Load(iterNum_);
214 0 : Load(tailOffset_);
215 0 : Load(tailSize_);
216 0 : Load(groupOpSize_);
217 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
218 0 : Load(lengths_[i]);
219 : }
220 :
221 0 : for (auto &t : detourTransports_[0]) {
222 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit);
223 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit);
224 : }
225 :
226 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit);
227 0 : GroupWait(*transportGroup, CKE_IDX_3, allBit);
228 : // 如果是4p*2场景,template里可以都传4k进来,transport和length通过<直连4k>, <直连4k>, <绕路4k>这样构造达成数据量2:1的效果
229 :
230 0 : std::vector<CcuRep::Memory> reduceSrc;
231 0 : std::vector<CcuRep::Memory> reduceDst;
232 :
233 : // 为每个直连或绕路transport分别准备reduceSrc与reduceDst
234 0 : for (uint32_t i = 0; i < pathNumPerPeer_; i++) {
235 0 : reduceDst.emplace_back(CreateMemory());
236 0 : for (uint32_t j = 0; j < rankSize_; j++) {
237 0 : reduceSrc.emplace_back(CreateMemory());
238 : }
239 : }
240 :
241 : // reduceDst填充
242 0 : reduceDst[0].addr = output_[0];
243 : // reduceDst[0].addr += offset_;
244 0 : reduceDst[0].token = token_[rankId_];
245 0 : for (uint32_t i = 1; i < pathNumPerPeer_; i++) {
246 0 : reduceDst[i].addr = reduceDst[i - 1].addr + lengths_[i - 1];
247 0 : reduceDst[i].token = token_[rankId_];
248 : }
249 : // 直连transport的reduceSrc填充
250 0 : uint32_t srcId = 0;
251 0 : uint32_t curId = 0;
252 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
253 0 : if (rankIdx != rankId_) {
254 0 : curId = srcId;
255 0 : srcId++;
256 : } else {
257 0 : curId = rankSize_ - 1;
258 : }
259 0 : reduceSrc[curId].addr = input_[rankIdx];
260 0 : reduceSrc[curId].token = token_[rankIdx];
261 0 : reduceSrc[curId].addr += offset_;
262 : }
263 : // 绕路transport的reduceSrc相比直连src再做偏移
264 0 : for (uint32_t i = 1; i < pathNumPerPeer_; i++) {
265 0 : for (uint32_t j = 0; j < rankSize_; j++) {
266 0 : reduceSrc[i * rankSize_ + j].addr = reduceSrc[(i - 1) * rankSize_ + j].addr + lengths_[i - 1];
267 0 : reduceSrc[i * rankSize_ + j].token = reduceSrc[(i - 1) * rankSize_ + j].token;
268 : }
269 : }
270 :
271 0 : GroupReduceDetour(reduceSrc, reduceDst, dataType_, outputDataType_, reduceOp_);
272 :
273 : // 余下的尾块用直连Reduce
274 0 : std::vector<CcuRep::Memory> tailSrc;
275 0 : CcuRep::Memory tailDst = CreateMemory();
276 0 : for (uint32_t i = 0; i < rankSize_; i++) {
277 0 : tailSrc.emplace_back(CreateMemory());
278 : }
279 0 : tailDst.addr = output_[0];
280 : // tailDst.addr += offset_;
281 0 : tailDst.addr += tailOffset_;
282 0 : tailDst.token = token_[rankId_];
283 0 : srcId = 0;
284 0 : curId = 0;
285 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
286 0 : if (rankIdx != rankId_) {
287 0 : curId = srcId;
288 0 : srcId++;
289 : } else {
290 0 : curId = rankSize_ - 1;
291 : }
292 0 : tailSrc[curId].addr = input_[rankIdx];
293 0 : tailSrc[curId].addr += offset_;
294 0 : tailSrc[curId].addr += tailOffset_;
295 0 : tailSrc[curId].token = token_[rankIdx];
296 : }
297 :
298 0 : GroupReduce(detourTransports_[0], tailDst, tailSrc, groupOpSize_, dataType_, outputDataType_, reduceOp_);
299 :
300 0 : for (auto t : detourTransports_[0]) {
301 0 : RemotePost(*t, CKE_IDX_0, selfBit);
302 : }
303 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
304 :
305 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] ReduceScatterMeshDetour1D end");
306 0 : return;
307 0 : }
308 :
309 0 : std::vector<uint64_t> CcuContextReduceScatterMeshDetour1D::GeneArgs(const CcuTaskArg &arg)
310 : {
311 0 : const CcuTaskArgReduceScatterMeshDetour1D *taskArg = dynamic_cast<const CcuTaskArgReduceScatterMeshDetour1D *>(&arg);
312 0 : if (taskArg == nullptr) {
313 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMeshDetour1D::taskArg ptr is null"));
314 : }
315 0 : uint64_t inputAddr = taskArg->inputAddr_;
316 0 : uint64_t outputAddr = taskArg->outputAddr_;
317 0 : uint64_t tokenInfo = taskArg->token_;
318 0 : uint64_t offset = taskArg->offset_;
319 0 : uint64_t iterNum = taskArg->iterNum_;
320 0 : uint64_t tailOffset = taskArg->tailOffset_;
321 0 : uint64_t tailSize = taskArg->tailSize_;
322 0 : auto goSize = CalGoSize(tailSize); // ***
323 :
324 0 : HCCL_INFO("[CcuContextReduceScatterMeshDetour1D] GeneArgs, taskArg are inputAddr[%llu], outputAddr[%llu], "
325 : "offset[%llu], iterNum[%llu], tailOffset[%llu], tailSize[%llu]",
326 : inputAddr, outputAddr, offset, iterNum, tailOffset, tailSize);
327 : std::vector<uint64_t> sqeArgs = {inputAddr, outputAddr, tokenInfo, offset, iterNum, tailOffset, tailSize,
328 0 : goSize[0], goSize[1], goSize[2], goSize[3]};
329 0 : for (auto len : taskArg->lengths_) {
330 0 : HCCL_INFO("get lengths");
331 0 : sqeArgs.emplace_back(len);
332 : }
333 0 : return sqeArgs;
334 0 : }
335 :
336 : }
|