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 "alg_template_register.h"
12 : #include "all_reduce_graph_pipeline.h"
13 :
14 : constexpr u32 STEP_OFFSET_TWO = 2;
15 :
16 :
17 : namespace hccl {
18 0 : AllReduceGraphPipeline::AllReduceGraphPipeline(const HcclDispatcher dispatcher) : AlgTemplateBase(dispatcher)
19 0 : {}
20 :
21 0 : AllReduceGraphPipeline::~AllReduceGraphPipeline()
22 0 : {}
23 :
24 0 : HcclResult AllReduceGraphPipeline::Prepare(u64 reduceAttrBitMap, HcomCollOpInfo *opInfo)
25 : {
26 0 : reduceAttr_ = reduceAttrBitMap;
27 0 : return HCCL_SUCCESS;
28 : }
29 :
30 0 : HcclResult AllReduceGraphPipeline::MainWaitSub()
31 : {
32 0 : u32 subStreamNum = intraRankSize_ - 1;
33 0 : for (u32 signalIndex = 0; signalIndex < subStreamNum; signalIndex++) {
34 0 : CHK_RET(LocalNotify::Wait(stream_, dispatcher_, streamNotifyMain_[signalIndex], INVALID_VALUE_STAGE));
35 : }
36 0 : return HCCL_SUCCESS;
37 : }
38 :
39 0 : HcclResult AllReduceGraphPipeline::SubRecordMain()
40 : {
41 0 : u32 subStreamNum = intraRankSize_ - 1;
42 0 : for (u32 streamIndex = 0; streamIndex < subStreamNum; streamIndex++) {
43 0 : CHK_RET(LocalNotify::Post(subStreams_[streamIndex], dispatcher_, streamNotifyMain_[streamIndex], -1));
44 : }
45 0 : return HCCL_SUCCESS;
46 : }
47 :
48 0 : HcclResult AllReduceGraphPipeline::MainRecordSub()
49 : {
50 0 : u32 subStreamNum = intraRankSize_ - 1;
51 0 : for (u32 signalIndex = 0; signalIndex < subStreamNum; signalIndex++) {
52 0 : CHK_RET(LocalNotify::Post(stream_, dispatcher_, streamNotifySub_[signalIndex], -1));
53 : }
54 0 : return HCCL_SUCCESS;
55 : }
56 :
57 0 : HcclResult AllReduceGraphPipeline::SubWaitMain()
58 : {
59 0 : u32 subStreamNum = intraRankSize_ - 1;
60 0 : for (u32 streamIndex = 0; streamIndex < subStreamNum; streamIndex++) {
61 0 : CHK_RET(LocalNotify::Wait(subStreams_[streamIndex], dispatcher_, streamNotifySub_[streamIndex],
62 : INVALID_VALUE_STAGE));
63 : }
64 0 : return HCCL_SUCCESS;
65 : }
66 :
67 0 : HcclResult AllReduceGraphPipeline::RunReduceScatterIntraServer(u32 step)
68 : {
69 0 : for (u32 i = 1; i < intraRankSize_; i++) {
70 0 : u32 remIntraRankId = (intraRankId_ + i) % intraRankSize_;
71 0 : CHK_RET(intraLinks_[remIntraRankId]->TxAck(subStreams_[i - 1]));
72 0 : CHK_RET(intraLinks_[remIntraRankId]->RxAck(subStreams_[i - 1]));
73 0 : void* remoteMemPtr = nullptr;
74 0 : CHK_RET(intraLinks_[remIntraRankId]->GetRemoteMem(UserMemType::INPUT_MEM, &remoteMemPtr));
75 0 : u32 sliceId = ((interRankId_ + step + 1) % interRankSize_) * intraRankSize_ + remIntraRankId;
76 0 : u64 srcOffset = sliceId * memSliceSize_;
77 0 : u64 dataSize = memSliceSize_;
78 0 : u64 dataCount = sliceCount_;
79 0 : if (sliceId == (interRankSize_ * intraRankSize_ - 1)) {
80 0 : dataSize = lastSliceSize_;
81 0 : dataCount = lastSliceCount_;
82 : }
83 0 : DeviceMem src = DeviceMem::create(static_cast<u8 *>(usrInMem_) + srcOffset, dataSize);
84 0 : DeviceMem dst = DeviceMem::create(static_cast<u8 *>(remoteMemPtr) + srcOffset, dataSize);
85 :
86 0 : CHK_RET(HcclReduceAsync(dispatcher_, src.ptr(), dataCount, dataType_, reductionOp_,
87 : subStreams_[i - 1], dst.ptr(), intraLinks_[remIntraRankId]->GetRemoteRank(),
88 : intraLinks_[remIntraRankId]->GetLinkType(), INLINE_REDUCE_BIT));
89 :
90 0 : CHK_RET(intraLinks_[remIntraRankId]->TxDataSignal(subStreams_[i - 1]));
91 0 : CHK_RET(intraLinks_[remIntraRankId]->RxDataSignal(subStreams_[i - 1]));
92 0 : }
93 0 : return HCCL_SUCCESS;
94 : }
95 :
96 0 : HcclResult AllReduceGraphPipeline::RunAllGatherIntraServer(u32 step)
97 : {
98 0 : for (u32 i = 1; i < intraRankSize_; i++) {
99 0 : u32 remIntraRankId = (intraRankId_ + i) % intraRankSize_;
100 0 : CHK_RET(intraLinks_[remIntraRankId]->TxAck(subStreams_[i - 1]));
101 0 : CHK_RET(intraLinks_[remIntraRankId]->RxAck(subStreams_[i - 1]));
102 0 : void* remoteMemPtr = nullptr;
103 0 : CHK_RET(intraLinks_[remIntraRankId]->GetRemoteMem(UserMemType::OUTPUT_MEM, &remoteMemPtr));
104 0 : u32 sliceId = ((interRankId_ + step) % interRankSize_) * intraRankSize_ + remIntraRankId;
105 0 : u64 dstOffset = sliceId * memSliceSize_;
106 0 : u64 dataSize = memSliceSize_;
107 0 : if (sliceId == (interRankSize_ * intraRankSize_ - 1)) {
108 0 : dataSize = lastSliceSize_;
109 : }
110 0 : DeviceMem src = DeviceMem::create(static_cast<u8 *>(remoteMemPtr) + dstOffset, dataSize);
111 0 : DeviceMem dst = DeviceMem::create(static_cast<u8 *>(usrOutMem_) + dstOffset, dataSize);
112 :
113 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, subStreams_[i - 1],
114 : intraLinks_[remIntraRankId]->GetRemoteRank(), intraLinks_[remIntraRankId]->GetLinkType()));
115 :
116 0 : CHK_RET(intraLinks_[remIntraRankId]->TxDataSignal(subStreams_[i - 1]));
117 0 : CHK_RET(intraLinks_[remIntraRankId]->RxDataSignal(subStreams_[i - 1]));
118 0 : }
119 0 : return HCCL_SUCCESS;
120 : }
121 :
122 0 : HcclResult AllReduceGraphPipeline::RunReduceScatterInterServer(u32 step,
123 : const LINK &prevInterLink,
124 : const LINK &nextInterLink)
125 : {
126 0 : u32 txSliceId = ((interRankId_ + 1 + step) % interRankSize_) * intraRankSize_ + intraRankId_;
127 0 : u64 txSliceOffset = memSliceSize_ * txSliceId;
128 0 : u64 txDataSize = memSliceSize_;
129 0 : if (txSliceId == (interRankSize_ * intraRankSize_ - 1)) {
130 0 : txDataSize = lastSliceSize_;
131 : }
132 0 : DeviceMem srcMem = DeviceMem::create(static_cast<u8 *>(usrInMem_) + txSliceOffset, txDataSize);
133 0 : CHK_RET(senderInfo_->run(nextInterLink, txSliceOffset, srcMem, stream_, UserMemType::INPUT_MEM));
134 0 : HCCL_DEBUG("[AllReduceGraphPipeline][RunReduceScatterInterServer] local rank[%u], localOffset[%llu]," \
135 : "tx with slice[%llu]", rankId_, txSliceOffset, txDataSize);
136 :
137 0 : u32 rxSliceId = ((interRankId_ + 2 + step) % interRankSize_) * intraRankSize_ + intraRankId_;
138 0 : u64 rxSliceOffset = memSliceSize_ * rxSliceId;
139 0 : u64 rxDataSize = memSliceSize_;
140 0 : if (rxSliceId == (interRankSize_ * intraRankSize_ - 1)) {
141 0 : rxDataSize = lastSliceSize_;
142 : }
143 0 : DeviceMem rxLocalMem = DeviceMem::create(static_cast<u8 *>(usrInMem_) + rxSliceOffset, rxDataSize);
144 0 : CHK_RET(reducerInfo_->run(dispatcher_, prevInterLink, rxSliceOffset, rxLocalMem, rxLocalMem, rxLocalMem,
145 : stream_));
146 0 : return HCCL_SUCCESS;
147 0 : }
148 :
149 0 : HcclResult AllReduceGraphPipeline::RunAllGatherInterServer(u32 step,
150 : const LINK &prevInterLink,
151 : const LINK &nextInterLink)
152 : {
153 0 : u32 txSliceId = ((interRankId_ + step) % interRankSize_) * intraRankSize_ + intraRankId_;
154 0 : u64 txSliceOffset = memSliceSize_ * txSliceId;
155 0 : u64 txDataSize = memSliceSize_;
156 0 : if (txSliceId == (interRankSize_ * intraRankSize_ - 1)) {
157 0 : txDataSize = lastSliceSize_;
158 : }
159 0 : CHK_RET(nextInterLink->TxAsync(UserMemType::OUTPUT_MEM, txSliceOffset,
160 : static_cast<u8 *>(usrOutMem_) + txSliceOffset, txDataSize, stream_));
161 0 : HCCL_DEBUG("[AllReduceGraphPipeline][RunAllGatherInterServer] local rank[%u], localOffset[%llu]," \
162 : "tx with slice[%llu]", rankId_, txSliceOffset, txDataSize);
163 :
164 0 : u32 rxSliceId = ((interRankId_ + step + 1) % interRankSize_) * intraRankSize_ + intraRankId_;
165 0 : u64 rxSliceOffset = memSliceSize_ * rxSliceId;
166 0 : u64 rxDataSize = memSliceSize_;
167 0 : if (rxSliceId == (interRankSize_ * intraRankSize_ - 1)) {
168 0 : rxDataSize = lastSliceSize_;
169 : }
170 0 : CHK_RET(prevInterLink->RxAsync(UserMemType::OUTPUT_MEM, rxSliceOffset,
171 : static_cast<u8 *>(usrOutMem_) + rxSliceOffset, rxDataSize, stream_));
172 0 : return HCCL_SUCCESS;
173 : }
174 :
175 0 : HcclResult AllReduceGraphPipeline::RunAsync()
176 : {
177 : // inter ring algo
178 0 : u32 prevInterRankId = (interRankId_ + 1) % interRankSize_;
179 0 : u32 nextInterRankId = (interRankId_ - 1 + interRankSize_) % interRankSize_;
180 0 : LINK prevInterLink = interLinks_[prevInterRankId];
181 0 : LINK nextInterLink = interLinks_[nextInterRankId];
182 : // 在user in执行reducescatter pipeline
183 0 : for (u32 step = 0; step < interRankSize_; step ++) {
184 0 : if (step == 0) {
185 0 : CHK_RET(MainRecordSub());
186 0 : CHK_RET(SubWaitMain());
187 : }
188 : // server内做SDMA的reduce
189 0 : CHK_RET(RunReduceScatterIntraServer(step));
190 0 : CHK_RET(SubRecordMain());
191 0 : CHK_RET(MainWaitSub());
192 0 : if (step < interRankSize_ - 1) {
193 0 : CHK_RET(MainRecordSub());
194 0 : CHK_RET(SubWaitMain());
195 0 : CHK_RET(prevInterLink->TxAck(stream_));
196 0 : CHK_RET(nextInterLink->RxAck(stream_));
197 : // server间做RDMA的reduce,可与下一个step的SDMA并发执行
198 0 : CHK_RET(RunReduceScatterInterServer(step, prevInterLink, nextInterLink));
199 : // 确保step[n+2]的SDMA之前step[n]的RDMA已经完成,防止内存踩踏
200 0 : CHK_RET(prevInterLink->PostFinAck(stream_));
201 0 : CHK_RET(nextInterLink->WaitFinAck(stream_));
202 : }
203 : }
204 :
205 : // reducescatter通信结束,将数据从user in拷贝到user out
206 0 : u64 localOffsetByte = memSliceSize_ * rankId_;
207 0 : u64 dataSize = memSliceSize_;
208 0 : if (rankId_ == (interRankSize_ * intraRankSize_ - 1)) {
209 0 : dataSize = lastSliceSize_;
210 : }
211 0 : DeviceMem locSrc = DeviceMem::create(static_cast<u8 *>(usrInMem_) + localOffsetByte, dataSize);
212 0 : DeviceMem locDst = DeviceMem::create(static_cast<u8 *>(usrOutMem_) + localOffsetByte, dataSize);
213 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, locDst, locSrc, stream_));
214 :
215 : // 在user out执行allgather pipeline
216 0 : for (u32 step = 0; step < interRankSize_; step ++) {
217 0 : CHK_RET(MainRecordSub());
218 0 : CHK_RET(SubWaitMain());
219 0 : if (step < interRankSize_ - 1) {
220 0 : CHK_RET(prevInterLink->TxAck(stream_));
221 0 : CHK_RET(nextInterLink->RxAck(stream_));
222 0 : CHK_RET(RunAllGatherInterServer(step, prevInterLink, nextInterLink));
223 0 : CHK_RET(prevInterLink->PostFinAck(stream_));
224 0 : CHK_RET(nextInterLink->WaitFinAck(stream_));
225 : // inter的最后一步需要barrier确保数据发完
226 0 : if (step == interRankSize_ - STEP_OFFSET_TWO) {
227 0 : CHK_RET(ExecuteBarrier(prevInterLink, nextInterLink));
228 : }
229 : }
230 0 : HCCL_DEBUG("[AllReducePipeline][RunAsync]step %u runAllGatherInterServer success", step);
231 0 : CHK_RET(RunAllGatherIntraServer(step));
232 0 : CHK_RET(SubRecordMain());
233 0 : CHK_RET(MainWaitSub());
234 0 : HCCL_INFO("[AllReducePipeline][RunAsync]AllReducePipeline finished groupRankId[%u] ", rankId_);
235 : }
236 0 : return HCCL_SUCCESS;
237 0 : }
238 :
239 0 : HcclResult AllReduceGraphPipeline::Prepare(const HcomCollOpInfo *opInfo, DeviceMem &cclBufferA, DeviceMem &cclBufferB,
240 : const u64 count, const SubCommInfo &level1CommInfo, const SubCommInfo &level0CommInfo,
241 : Stream &mainStream, std::vector<Stream> &subStream,
242 : std::vector<std::shared_ptr<LocalNotify>> ¬ifyMain, std::vector<std::shared_ptr<LocalNotify>> ¬ifySub)
243 : {
244 0 : unitSize_ = SIZE_TABLE[opInfo->dataType];
245 0 : sliceCount_ = count / (level0CommInfo.localRankSize * level1CommInfo.localRankSize);
246 0 : memSliceSize_ = sliceCount_ * unitSize_;
247 0 : lastSliceCount_ = count - sliceCount_ * (level0CommInfo.localRankSize * level1CommInfo.localRankSize - 1);
248 0 : lastSliceSize_ = lastSliceCount_ * unitSize_;
249 0 : HCCL_DEBUG("[%s] PrepareSliceDataWithAlignSize for data_slice_prepare", __func__);
250 :
251 0 : usrInMem_ = opInfo->inputAddr;
252 0 : usrOutMem_ = opInfo->outputAddr;
253 0 : reductionOp_ = opInfo->reduceOp;
254 0 : dataType_ = opInfo->dataType;
255 :
256 : // needed resource
257 : // stream: 1 * mainStream + (n -1) * subStream
258 : // interNotify, streamNotify
259 :
260 : // stream
261 : // mainStream负责locMemCpy、inter执行以及subStream同步控制
262 0 : stream_ = mainStream;
263 : // subStream负责:
264 : // streamId[0:intraRankSize-1]: intraRankSize-1个intra执行
265 0 : subStreams_ = subStream;
266 :
267 0 : intraRankSize_ = level0CommInfo.localRankSize;
268 0 : interRankSize_ = level1CommInfo.localRankSize;
269 0 : intraRankId_ = level0CommInfo.localRank;
270 0 : interRankId_ = level1CommInfo.localRank;
271 0 : rankId_ = intraRankId_ + interRankId_ * intraRankSize_;
272 :
273 : // streamNotify, size: n
274 0 : streamNotifyMain_ = notifyMain;
275 0 : if (streamNotifyMain_.size() < intraRankSize_ - 1) {
276 0 : HCCL_ERROR("[AllReduceGraphPipeline][Prepare]rank[%u] streamNotifyMain_ size [%u] error, is smaller than," \
277 : "intraRankSize_[%u]", rankId_, streamNotifyMain_.size(), intraRankSize_);
278 0 : return HCCL_E_INTERNAL;
279 : }
280 0 : streamNotifySub_ = notifySub;
281 0 : if (streamNotifySub_.size() < intraRankSize_ - 1) {
282 0 : HCCL_ERROR("[AllReduceGraphPipeline][Prepare]rank[%u] streamNotifySub_ size [%u] error, is smaller than," \
283 : "intraRankSize_[%u]", rankId_, streamNotifySub_.size(), intraRankSize_);
284 0 : return HCCL_E_INTERNAL;
285 : }
286 :
287 0 : intraLinks_ = level0CommInfo.links;
288 0 : interLinks_ = level1CommInfo.links;
289 :
290 0 : HCCL_INFO("[AllReduceGraphPipeline][Prepare]streamNum[%u], streamNotifyMainNum[%u], streamNotifySubNum[%u]",
291 : subStreams_.size(), streamNotifyMain_.size(), streamNotifySub_.size());
292 0 : HCCL_INFO("[AllReduceGraphPipeline][Prepare]interLinksNum[%u], intraLinksNum[%u]",
293 : interLinks_.size(), intraLinks_.size());
294 0 : senderInfo_.reset(new (std::nothrow) Sender(dataType_, reductionOp_, reduceAttr_));
295 0 : CHK_SMART_PTR_NULL(senderInfo_);
296 0 : reducerInfo_.reset(new (std::nothrow) Reducer(dataType_, reductionOp_, reduceAttr_));
297 0 : CHK_SMART_PTR_NULL(reducerInfo_);
298 0 : return HCCL_SUCCESS;
299 : }
300 : REGISTER_TEMPLATE(TemplateType::TEMPLATE_ALLREDUCE_GRAPH_PIPELINE, AllReduceGraphPipeline);
301 : } // namespace hccl
|