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_all_reduce_mesh1d_detour.h"
12 : #include "ccu_instruction_all_reduce_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 : CcuContextAllReduceMeshDetour1D::CcuContextAllReduceMeshDetour1D(
25 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
26 0 : : CcuContextAlgBase(arg, transports, group)
27 : {
28 0 : const CcuCtxArgAllReduceMeshDetour1D* ctxArg = dynamic_cast<const CcuCtxArgAllReduceMeshDetour1D*>(&arg);
29 0 : if (ctxArg == nullptr) {
30 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMeshDetour1D::ctxArg ptr is null"));
31 : }
32 0 : rankId_ = ctxArg->rankId_;
33 0 : rankSize_ = ctxArg->dimSize_[0];
34 0 : dataType_ = ctxArg->op_.dataType;
35 0 : outputDataType_ = ctxArg->op_.outputDataType;
36 0 : if (outputDataType_ == DataType::INVALID) {
37 0 : outputDataType_ = dataType_;
38 0 : HCCL_INFO(
39 : "[CcuContextAllReduceMeshDetour1D] 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(
47 : "[CcuContextAllReduceMeshDetour1D] Init, CtxArgs are rankId_[%u], rankSize_[%u], dataType[%s], "
48 : "outputDataType[%s], reduceOp[%s]",
49 : rankId_, rankSize_, dataType_.Describe().c_str(), outputDataType_.Describe().c_str(),
50 : reduceOp_.Describe().c_str());
51 :
52 0 : HCCL_INFO("[CcuContextAllReduceMeshDetour1D] transport.size[%zu]", transports.size());
53 0 : if (transports.size() < rankSize_ - 1) {
54 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMeshDetour1D transports size is less"));
55 : }
56 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
57 : // 到每个对端有pathNum个transport,故detourTransport中共有pathNum组
58 0 : detourTransports_.emplace_back(std::vector<CcuTransport*>());
59 : }
60 0 : uint64_t directPathNum = pathNumPerPeer - detourPathNum;
61 0 : for (uint64_t i = 0; i < directPathNum; i++) {
62 : // 有pathNum-detourPathNum组的直连链路,每组重复
63 0 : for (uint64_t j = 0; j < rankSize_ - 1; j++) {
64 0 : detourTransports_[i].emplace_back(transports[j]);
65 : }
66 0 : HCCL_INFO(
67 : "[CcuContextAllReduceMeshDetour1D] Add directTransports[%llu], size[%zu]", i, detourTransports_[i].size());
68 : }
69 0 : for (uint64_t i = 0; i < detourPathNum; i++) {
70 0 : for (uint64_t j = 0; j < rankSize_ - 1; j++) {
71 0 : detourTransports_[i + directPathNum].emplace_back(transports[(i + 1) * (rankSize_ - 1) + j]);
72 0 : detourTransports_[i + directPathNum].emplace_back(
73 0 : transports[(i + 1) * (rankSize_ - 1) + j + detourPathNum * (rankSize_ - 1)]);
74 0 : HCCL_INFO(
75 : "detourTransports_ emplace_back sendLink[%u], recvLink[%u]", (i + 1) * (rankSize_ - 1) + j,
76 : (i + 1) * (rankSize_ - 1) + j + detourPathNum * (rankSize_ - 1));
77 : }
78 : }
79 0 : }
80 :
81 0 : void CcuContextAllReduceMeshDetour1D::CreateMultiOpReduceDetour(
82 : DataType& dataType, DataType& outputDataType, ReduceOp& opType)
83 : {
84 0 : moConfig.loopCount = CcuRep::CCU_MS_DEFAULT_LOOP_COUNT;
85 0 : moConfig.msInterleave = pathNumPerPeer * rankSize_;
86 0 : if (moRes.executor.size() == 0) {
87 0 : moRes.maskSignal = CreateBlockMaskSignal(moConfig.loopCount);
88 0 : moRes.executor = CreateBlockExecutor(moConfig.loopCount);
89 0 : moRes.ccuBuffer = CreateBlockCcuBuffer(moConfig.loopCount * moConfig.msInterleave);
90 : }
91 0 : std::string loopType = "reduceDetour";
92 0 : if (registeredLoop.find(loopType) != registeredLoop.end()) {
93 0 : return;
94 : }
95 0 : CcuRep::LoopBlock lb(this, loopType + "_loop");
96 : {
97 : // loopblock的形参
98 0 : std::vector<CcuRep::Memory> dst;
99 0 : std::vector<CcuRep::Memory> src;
100 0 : std::vector<CcuRep::Variable> lengths;
101 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
102 0 : lengths.emplace_back(CreateVariable());
103 0 : dst.emplace_back(CreateMemory());
104 0 : for (uint64_t j = 0; j < rankSize_; j++) {
105 0 : src.emplace_back(CreateMemory());
106 : }
107 : }
108 :
109 0 : lb(src, dst, lengths);
110 0 : std::vector<std::vector<CcuRep::CcuBuffer>> bufs;
111 0 : bufs.resize(pathNumPerPeer);
112 0 : std::vector<CcuRep::MaskSignal> sems;
113 :
114 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
115 0 : for (uint64_t j = 0; j < rankSize_; j++) {
116 0 : bufs[i].emplace_back(moRes.ccuBuffer[i * rankSize_ + j]);
117 : }
118 0 : sems.emplace_back(moRes.maskSignal[i]);
119 : }
120 :
121 : // 先读远端直连的到本地MS
122 0 : uint64_t directPathNum = pathNumPerPeer - detourPathNum;
123 0 : for (uint64_t i = 0; i < directPathNum; i++) {
124 0 : for (uint32_t j = 0; j < detourTransports_[i].size(); j++) {
125 0 : if (detourTransports_[i][j] == nullptr) {
126 0 : THROW<CcuApiException>("transport is nullptr");
127 : }
128 0 : Read(*detourTransports_[i][j], bufs[i][j], src[i * rankSize_ + j], lengths[i], sems[i], 1 << j);
129 : }
130 : }
131 : // 再读远端绕路的到本地MS
132 0 : for (uint64_t i = directPathNum; i < pathNumPerPeer; i++) {
133 0 : for (uint64_t j = 0; j < rankSize_ - 1; j++) {
134 0 : if (detourTransports_[i][j * 2 + 1] == nullptr) { // j * 2 + 1是recvOnly Link
135 0 : THROW<CcuApiException>("transport is nullptr");
136 : }
137 0 : Read(*detourTransports_[i][j * 2 + 1], bufs[i][j], src[i * rankSize_ + j], lengths[i], sems[i], 1 << j);
138 : }
139 : }
140 :
141 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
142 0 : LocalCopy(
143 0 : bufs[i][rankSize_ - 1], src[i * rankSize_ + rankSize_ - 1], lengths[i], sems[i], 1 << (rankSize_ - 1));
144 : }
145 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
146 0 : LocalWait(sems[i], (1 << rankSize_) - 1);
147 : }
148 0 : if (rankSize_ > 1) {
149 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
150 0 : LocalReduce(bufs[i], rankSize_, dataType, outputDataType, opType, sems[i], lengths[i]);
151 0 : LocalWait(sems[i]);
152 : }
153 : }
154 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
155 0 : LocalCopy(dst[i], bufs[i][0], lengths[i], sems[i]);
156 0 : LocalWait(sems[i]);
157 : }
158 0 : }
159 0 : registeredLoop.insert(loopType);
160 0 : return;
161 0 : }
162 :
163 0 : void CcuContextAllReduceMeshDetour1D::GroupReduceDetour(
164 : std::vector<CcuRep::Memory>& src, std::vector<CcuRep::Memory>& dst, DataType& dataType, DataType& outputDataType,
165 : ReduceOp& opType)
166 : {
167 0 : CreateMultiOpReduceDetour(dataType, outputDataType, opType);
168 0 : uint32_t interLeave = 8;
169 :
170 0 : CCU_IF(iterNum_ != 0)
171 : {
172 0 : CcuRep::Variable loopParam = CreateVariable();
173 0 : CcuRep::Variable paraCfg = CreateVariable();
174 0 : CcuRep::Variable offsetCfg = CreateVariable();
175 :
176 0 : loopParam = CcuRep::GetLoopParam(
177 0 : 0, singleTransportSize * moConfig.loopCount, 0); // 下次迭代的偏移是单次总搬运量*loopNum
178 0 : loopParam += iterNum_; // 加上loop的迭代次数构成完整loop参数
179 0 : paraCfg = CcuRep::GetParallelParam(moConfig.loopCount - 1, 0, 1); // loop固定展开到128个
180 0 : offsetCfg = CcuRep::GetOffsetParam(singleTransportSize, interLeave, pathNumPerPeer); // 下一个loop偏移量
181 0 : auto lc = Loop("reduceDetour_loop")(src, dst, lengths_);
182 0 : LoopGroup({lc}, {loopParam}, paraCfg, offsetCfg);
183 0 : }
184 0 : return;
185 0 : }
186 :
187 0 : void CcuContextAllReduceMeshDetour1D::CreateMultiOpBroadcastDetour()
188 : {
189 0 : moConfig.loopCount = CcuRep::CCU_MS_DEFAULT_LOOP_COUNT;
190 0 : moConfig.msInterleave = pathNumPerPeer * 1; // Bcast为msNum*1,Reduce为msNum*rankSize_
191 0 : if (moRes.executor.size() == 0) {
192 0 : moRes.executor = CreateBlockExecutor(moConfig.loopCount);
193 0 : moRes.maskSignal = CreateBlockMaskSignal(moConfig.loopCount);
194 0 : moRes.ccuBuffer = CreateBlockCcuBuffer(moConfig.loopCount * moConfig.msInterleave);
195 : }
196 :
197 0 : std::string loopType = "broadcastDetour";
198 0 : if (registeredLoop.find(loopType) != registeredLoop.end()) {
199 0 : return;
200 : }
201 :
202 0 : CcuRep::LoopBlock lb(this, loopType + "_loop");
203 : {
204 : // loopblock的形参
205 0 : std::vector<CcuRep::Memory> src;
206 0 : std::vector<CcuRep::Memory> dst;
207 0 : std::vector<CcuRep::Variable> lengths;
208 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
209 0 : lengths.emplace_back(CreateVariable());
210 0 : src.emplace_back(CreateMemory());
211 0 : for (uint64_t j = 0; j < rankSize_; j++) {
212 0 : dst.emplace_back(CreateMemory());
213 : }
214 : }
215 :
216 0 : lb(src, dst, lengths);
217 0 : std::vector<CcuRep::CcuBuffer> bufs;
218 0 : std::vector<CcuRep::MaskSignal> sems;
219 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
220 0 : bufs.emplace_back(moRes.ccuBuffer[i]);
221 0 : sems.emplace_back(moRes.maskSignal[i]);
222 : }
223 :
224 : // 从本地搬运多片数据到多个MS
225 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
226 0 : LocalCopy(bufs[i], src[i], lengths[i], sems[i]);
227 : }
228 : // 等待数据搬到MS
229 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
230 0 : LocalWait(sems[i]);
231 : }
232 : // 给每个peer搬运多个MS上的数据
233 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
234 0 : for (uint64_t j = 0; j < rankSize_ - 1; j++) {
235 0 : if (detourTransports_[i][j * 2] == nullptr) { // j * 2是sendOnly Link
236 0 : THROW<CcuApiException>("transport is nullptr");
237 : }
238 0 : Write(*detourTransports_[i][j * 2], dst[i * rankSize_ + j], bufs[i], lengths[i], sems[i], 1 << j);
239 : }
240 0 : LocalCopy(dst[i * rankSize_ + rankSize_ - 1], bufs[i], lengths[i], sems[i], 1 << (rankSize_ - 1));
241 : }
242 : // 等待给所有远端写完数据
243 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
244 0 : LocalWait(sems[i], (1 << rankSize_) - 1);
245 : }
246 0 : }
247 :
248 0 : registeredLoop.insert(loopType);
249 0 : return;
250 0 : }
251 :
252 0 : void CcuContextAllReduceMeshDetour1D::GroupBroadcastDetour(
253 : std::vector<CcuRep::Variable>& lengths, std::vector<CcuRep::Memory>& src, std::vector<CcuRep::Memory>& dst)
254 : {
255 0 : CreateMultiOpBroadcastDetour();
256 0 : uint32_t interLeave = 8;
257 :
258 0 : CCU_IF(iterNum_ != 0)
259 : {
260 0 : CcuRep::Variable loopParam = CreateVariable();
261 0 : CcuRep::Variable paraCfg = CreateVariable();
262 0 : CcuRep::Variable offsetCfg = CreateVariable();
263 :
264 0 : loopParam = CcuRep::GetLoopParam(0, singleTransportSize * moConfig.loopCount, 0); // 偏移是单次总搬运量*loopNum
265 0 : loopParam += iterNum_; // 加上loop的迭代次数构成完整loop参数
266 0 : paraCfg = CcuRep::GetParallelParam(moConfig.loopCount - 1, 0, 1); // loop固定展开到128个
267 0 : offsetCfg = CcuRep::GetOffsetParam(singleTransportSize, interLeave, pathNumPerPeer); // 下一个loop偏移量
268 0 : auto lc = Loop("broadcastDetour_loop")(src, dst, lengths);
269 0 : LoopGroup({lc}, {loopParam}, paraCfg, offsetCfg);
270 0 : }
271 0 : return;
272 0 : }
273 :
274 0 : void CcuContextAllReduceMeshDetour1D::ReduceScatterFirstStep()
275 : {
276 0 : std::vector<CcuRep::Memory> reduceSrc;
277 0 : std::vector<CcuRep::Memory> reduceDst;
278 :
279 : // 为每个直连或绕路transport分别准备reduceSrc与reduceDst
280 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
281 0 : reduceDst.emplace_back(CreateMemory());
282 0 : for (uint64_t j = 0; j < rankSize_; j++) {
283 0 : reduceSrc.emplace_back(CreateMemory());
284 : }
285 : }
286 :
287 : // reduceDst填充
288 0 : reduceDst[0].addr = output_[rankId_];
289 0 : reduceDst[0].addr += offset_;
290 0 : reduceDst[0].token = token_[rankId_];
291 0 : for (uint64_t i = 1; i < pathNumPerPeer; i++) {
292 0 : reduceDst[i].addr = reduceDst[i - 1].addr + lengths_[i - 1];
293 0 : reduceDst[i].token = token_[rankId_];
294 : }
295 : // 直连transport的reduceSrc填充
296 0 : uint32_t srcId = 0;
297 0 : uint32_t curId = 0;
298 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
299 0 : if (rankIdx != rankId_) {
300 0 : curId = srcId;
301 0 : srcId++;
302 : } else {
303 0 : curId = rankSize_ - 1;
304 : }
305 0 : reduceSrc[curId].addr = input_[rankIdx];
306 0 : reduceSrc[curId].addr += offset_;
307 0 : reduceSrc[curId].token = token_[rankIdx];
308 : }
309 : // 绕路transport的reduceSrc相比直连src再做偏移
310 0 : for (uint64_t i = 1; i < pathNumPerPeer; i++) {
311 0 : for (uint64_t j = 0; j < rankSize_; j++) {
312 0 : reduceSrc[i * rankSize_ + j].addr = reduceSrc[(i - 1) * rankSize_ + j].addr + lengths_[i - 1];
313 0 : reduceSrc[i * rankSize_ + j].token = reduceSrc[(i - 1) * rankSize_ + j].token;
314 : }
315 : }
316 :
317 : // 整块数据用绕路Reduce
318 0 : GroupReduceDetour(reduceSrc, reduceDst, dataType_, outputDataType_, reduceOp_);
319 0 : return;
320 0 : }
321 :
322 0 : void CcuContextAllReduceMeshDetour1D::ReduceScatterSecondStep()
323 : {
324 : // 余下的尾块用直连Reduce
325 0 : std::vector<CcuRep::Memory> tailSrc;
326 0 : CcuRep::Memory tailDst = CreateMemory();
327 0 : for (uint64_t i = 0; i < rankSize_; i++) {
328 0 : tailSrc.emplace_back(CreateMemory());
329 : }
330 0 : tailDst.addr = output_[rankId_];
331 0 : tailDst.addr += offset_;
332 0 : tailDst.addr += tailOffset_;
333 0 : tailDst.token = token_[rankId_];
334 0 : uint32_t srcId = 0;
335 0 : uint32_t curId = 0;
336 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
337 0 : if (rankIdx != rankId_) {
338 0 : curId = srcId;
339 0 : srcId++;
340 : } else {
341 0 : curId = rankSize_ - 1;
342 : }
343 0 : tailSrc[curId].addr = input_[rankIdx];
344 0 : tailSrc[curId].addr += tailOffset_;
345 0 : tailSrc[curId].addr += offset_;
346 0 : tailSrc[curId].token = token_[rankIdx];
347 : }
348 :
349 0 : GroupReduce(detourTransports_[0], tailDst, tailSrc, groupOpSize_, dataType_, outputDataType_, reduceOp_);
350 0 : return;
351 0 : }
352 :
353 0 : void CcuContextAllReduceMeshDetour1D::AllGatherFirstStep()
354 : {
355 : // 开始AllGather
356 0 : std::vector<CcuRep::Memory> allGatherSrc;
357 0 : std::vector<CcuRep::Memory> allGatherDst;
358 :
359 : // 为每个直连或绕路transport分别准备src与dst
360 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
361 0 : allGatherSrc.emplace_back(CreateMemory());
362 0 : for (uint64_t j = 0; j < rankSize_; j++) {
363 0 : allGatherDst.emplace_back(CreateMemory());
364 : }
365 : }
366 : // allGather 的输入就是 reduceScatter 的输出
367 0 : allGatherSrc[0].addr = output_[rankId_]; // 直连源地址
368 0 : allGatherSrc[0].addr += offset_;
369 0 : allGatherSrc[0].token = token_[rankId_];
370 0 : for (uint64_t i = 1; i < pathNumPerPeer; i++) {
371 0 : allGatherSrc[i].addr = allGatherSrc[i - 1].addr + lengths_[i - 1];
372 0 : allGatherSrc[i].token = token_[rankId_];
373 : }
374 :
375 : // 直连的allGatherDst填充
376 0 : uint32_t curId = 0;
377 0 : uint32_t dstId = 0;
378 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
379 0 : if (rankIdx != rankId_) {
380 0 : curId = dstId;
381 0 : dstId++;
382 : } else {
383 0 : curId = rankSize_ - 1;
384 : }
385 0 : allGatherDst[curId].addr = output_[rankIdx];
386 0 : allGatherDst[curId].addr += offset_;
387 0 : allGatherDst[curId].token = token_[rankIdx];
388 : }
389 :
390 : // 绕路的allGatherDst填充,相比直连做偏移
391 0 : for (uint64_t i = 1; i < pathNumPerPeer; i++) {
392 0 : for (uint64_t j = 0; j < rankSize_; j++) {
393 0 : allGatherDst[i * rankSize_ + j].addr = allGatherDst[(i - 1) * rankSize_ + j].addr + lengths_[i - 1];
394 0 : allGatherDst[i * rankSize_ + j].token = allGatherDst[(i - 1) * rankSize_ + j].token;
395 : }
396 : }
397 0 : GroupBroadcastDetour(lengths_, allGatherSrc, allGatherDst);
398 0 : return;
399 0 : }
400 :
401 0 : void CcuContextAllReduceMeshDetour1D::AllGatherSecondStep()
402 : {
403 : // 余下的尾块用直连transport发送
404 0 : CcuRep::Memory bcastTailSrc = CreateMemory();
405 0 : std::vector<CcuRep::Memory> bcastTailDst;
406 0 : for (uint64_t i = 0; i < rankSize_; i++) {
407 0 : bcastTailDst.emplace_back(CreateMemory());
408 : }
409 0 : bcastTailSrc.addr = output_[rankId_];
410 0 : bcastTailSrc.addr += offset_;
411 0 : bcastTailSrc.addr += tailOffset_;
412 0 : bcastTailSrc.token = token_[rankId_];
413 0 : uint32_t dstId = 0;
414 0 : uint32_t curId = 0;
415 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
416 0 : if (rankIdx != rankId_) {
417 0 : curId = dstId;
418 0 : dstId++;
419 : } else {
420 0 : curId = rankSize_ - 1;
421 : }
422 0 : bcastTailDst[curId].addr = output_[rankIdx];
423 0 : bcastTailDst[curId].addr += offset_;
424 0 : bcastTailDst[curId].addr += tailOffset_;
425 0 : bcastTailDst[curId].token = token_[rankIdx];
426 : }
427 0 : GroupBroadcast(detourTransports_[0], bcastTailDst, bcastTailSrc, groupOpSize_);
428 0 : return;
429 0 : }
430 :
431 0 : void CcuContextAllReduceMeshDetour1D::Algorithm()
432 : {
433 0 : HCCL_INFO("[CcuContextAllReduceMeshDetour1D] AllReduceMeshDetour1D run.");
434 0 : uint16_t selfBit = 1 << rankId_;
435 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
436 :
437 : // 初始化资源
438 0 : uint16_t transportIdx = 0;
439 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
440 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
441 0 : if (peerId == rankId_) {
442 0 : input_.push_back(CreateVariable());
443 0 : output_.push_back(CreateVariable());
444 0 : token_.push_back(CreateVariable());
445 : } else {
446 0 : HCCL_INFO(
447 : "[CcuContextAllReduceMeshDetour1D] MyRank[%u], PeerId[%llu], TransportId[%u]", rankId_, peerId,
448 : transportIdx);
449 0 : CHK_PRT_RET(
450 : detourTransports_[0][transportIdx] == nullptr,
451 : HCCL_ERROR("[CcuContextAllReduceMeshDetour1D] Algorithm transport ptr is null"), );
452 0 : input_.push_back(CreateVariable((*detourTransports_[0][transportIdx]), INPUT_XN_ID));
453 0 : output_.push_back(CreateVariable((*detourTransports_[0][transportIdx]), OUTPUT_XN_ID));
454 0 : token_.push_back(CreateVariable((*detourTransports_[0][transportIdx]), TOKEN_XN_ID));
455 0 : transportIdx++;
456 : }
457 : }
458 0 : offset_ = CreateVariable();
459 0 : iterNum_ = CreateVariable();
460 0 : tailOffset_ = CreateVariable();
461 0 : tailSize_ = CreateVariable();
462 0 : groupOpSize_ = CreateGroupOpSize();
463 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
464 0 : lengths_.emplace_back(CreateVariable());
465 : }
466 :
467 0 : Load(input_[rankId_]);
468 0 : Load(output_[rankId_]);
469 0 : Load(token_[rankId_]);
470 0 : Load(offset_);
471 0 : Load(iterNum_);
472 0 : Load(tailOffset_);
473 0 : Load(tailSize_);
474 0 : Load(groupOpSize_);
475 0 : for (uint64_t i = 0; i < pathNumPerPeer; i++) {
476 0 : Load(lengths_[i]);
477 : }
478 :
479 0 : for (auto& t : detourTransports_[0]) {
480 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit);
481 0 : WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_2, selfBit);
482 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit);
483 : }
484 :
485 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit);
486 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit);
487 0 : GroupWait(*transportGroup, CKE_IDX_3, allBit);
488 :
489 0 : ReduceScatterFirstStep();
490 0 : ReduceScatterSecondStep();
491 :
492 0 : AllGatherFirstStep();
493 0 : AllGatherSecondStep();
494 :
495 0 : for (auto t : detourTransports_[0]) {
496 0 : RemotePost(*t, CKE_IDX_0, selfBit);
497 : }
498 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
499 :
500 0 : HCCL_INFO("[CcuContextAllReduceMeshDetour1D] AllReduceMeshDetour1D end.");
501 0 : return;
502 : }
503 :
504 0 : std::vector<uint64_t> CcuContextAllReduceMeshDetour1D::GeneArgs(const CcuTaskArg& arg)
505 : {
506 0 : const CcuTaskArgAllReduceMeshDetour1D* taskArg = dynamic_cast<const CcuTaskArgAllReduceMeshDetour1D*>(&arg);
507 0 : if (taskArg == nullptr) {
508 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMeshDetour1D::taskArg ptr is null"));
509 : }
510 0 : uint64_t inputAddr = taskArg->inputAddr_;
511 0 : uint64_t outputAddr = taskArg->outputAddr_;
512 0 : uint64_t tokenInfo = taskArg->token_;
513 0 : uint64_t offset = taskArg->offset_;
514 0 : uint64_t iterNum = taskArg->iterNum_;
515 0 : uint64_t tailOffset = taskArg->tailOffset_;
516 0 : uint64_t tailSize = taskArg->tailSize_;
517 0 : auto goSize = CalGoSize(tailSize);
518 :
519 0 : HCCL_INFO(
520 : "[CcuContextAllReduceMeshDetour1D] GeneArgs, taskArg are inputAddr[%llu], outputAddr[%llu], "
521 : "offset[%llu], iterNum[%llu], tailOffset[%llu], tailSize[%llu]",
522 : inputAddr, outputAddr, offset, iterNum, tailOffset, tailSize);
523 : std::vector<uint64_t> sqeArgs = {inputAddr, outputAddr, tokenInfo, offset, iterNum, tailOffset,
524 0 : tailSize, goSize[0], goSize[1], goSize[2], goSize[3]};
525 0 : for (auto len : taskArg->lengths_) {
526 0 : sqeArgs.emplace_back(len);
527 : }
528 0 : return sqeArgs;
529 0 : }
530 :
531 : } // namespace Hccl
|