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 "reduce_scatter_graph_pipeline.h"
12 : #include "alg_template_register.h"
13 :
14 : constexpr u32 STEP_OFFSET_TWO = 2;
15 :
16 : namespace hccl {
17 0 : ReduceScatterGraphPipeline::ReduceScatterGraphPipeline(const HcclDispatcher dispatcher) : AlgTemplateBase(dispatcher) {}
18 :
19 0 : ReduceScatterGraphPipeline::~ReduceScatterGraphPipeline() {}
20 :
21 0 : HcclResult ReduceScatterGraphPipeline::MainWaitSub(u32 begin)
22 : {
23 0 : u32 subStreamNum = intraRankSize_;
24 0 : for (u32 signalIndex = begin; signalIndex < subStreamNum; signalIndex++) {
25 0 : CHK_RET(LocalNotify::Wait(stream_, dispatcher_, streamNotifyMain_[signalIndex], INVALID_VALUE_STAGE));
26 : }
27 0 : return HCCL_SUCCESS;
28 : }
29 :
30 0 : HcclResult ReduceScatterGraphPipeline::SubRecordMain(u32 begin)
31 : {
32 0 : u32 subStreamNum = intraRankSize_;
33 0 : for (u32 streamIndex = begin; streamIndex < subStreamNum; streamIndex++) {
34 0 : CHK_RET(LocalNotify::Post(subStream_[streamIndex], dispatcher_, streamNotifyMain_[streamIndex], -1));
35 : }
36 0 : return HCCL_SUCCESS;
37 : }
38 :
39 0 : HcclResult ReduceScatterGraphPipeline::MainRecordSub(u32 begin)
40 : {
41 0 : u32 subStreamNum = intraRankSize_;
42 0 : for (u32 signalIndex = begin; signalIndex < subStreamNum; signalIndex++) {
43 0 : CHK_RET(LocalNotify::Post(stream_, dispatcher_, streamNotifySub_[signalIndex], -1));
44 : }
45 0 : return HCCL_SUCCESS;
46 : }
47 :
48 0 : HcclResult ReduceScatterGraphPipeline::SubWaitMain(u32 begin)
49 : {
50 0 : u32 subStreamNum = intraRankSize_;
51 0 : for (u32 streamIndex = begin; streamIndex < subStreamNum; streamIndex++) {
52 0 : CHK_RET(LocalNotify::Wait(
53 : subStream_[streamIndex], dispatcher_, streamNotifySub_[streamIndex], INVALID_VALUE_STAGE));
54 : }
55 0 : return HCCL_SUCCESS;
56 : }
57 :
58 0 : HcclResult ReduceScatterGraphPipeline::RunIntraServer(u64 blockIdx)
59 : {
60 0 : u64 blockOff = blockIdx * intraRankSize_;
61 0 : u64 memOffset = (blockOff + intraRankId_) * memSliceSize_;
62 0 : for (u32 i = 1; i < intraRankSize_; i++) {
63 0 : u32 remIntraRankId = (intraRankId_ + i) % intraRankSize_;
64 0 : CHK_RET(intraLinks_[remIntraRankId]->TxAck(subStream_[i]));
65 0 : CHK_RET(intraLinks_[remIntraRankId]->RxAck(subStream_[i]));
66 0 : void* remoteMemPtr = nullptr;
67 0 : CHK_RET(intraLinks_[remIntraRankId]->GetRemoteMem(UserMemType::INPUT_MEM, &remoteMemPtr));
68 0 : DeviceMem dst = DeviceMem::create(static_cast<u8*>(usrInMem_) + memOffset, memSliceSize_);
69 0 : DeviceMem src = DeviceMem::create(static_cast<u8*>(remoteMemPtr) + memOffset, memSliceSize_);
70 :
71 0 : CHK_RET(HcclReduceAsync(
72 : dispatcher_, src.ptr(), count_, dataType_, reductionOp_, subStream_[i], dst.ptr(),
73 : intraLinks_[remIntraRankId]->GetRemoteRank(), intraLinks_[remIntraRankId]->GetLinkType(),
74 : INLINE_REDUCE_BIT));
75 :
76 0 : CHK_RET(intraLinks_[remIntraRankId]->TxDataSignal(subStream_[i]));
77 0 : CHK_RET(intraLinks_[remIntraRankId]->RxDataSignal(subStream_[i]));
78 0 : }
79 0 : return HCCL_SUCCESS;
80 : }
81 :
82 : HcclResult
83 0 : ReduceScatterGraphPipeline::RunInterServer(u64 blockIdx, const LINK& prevInterLink, const LINK& nextInterLink)
84 : {
85 0 : u64 blockOff = blockIdx * intraRankSize_;
86 0 : u64 memOffset = (blockOff + intraRankId_) * memSliceSize_;
87 0 : u64 preBlockOff = ((blockIdx + 1) % interRankSize_) * intraRankSize_;
88 0 : u64 preMemOffset = (preBlockOff + intraRankId_) * memSliceSize_;
89 :
90 0 : DeviceMem srcMem = DeviceMem::create(static_cast<u8*>(usrInMem_) + memOffset, memSliceSize_);
91 0 : CHK_RET(senderInfo_->run(nextInterLink, memOffset, srcMem, subStream_[0], UserMemType::INPUT_MEM));
92 0 : HCCL_DEBUG(
93 : "[ReduceScatterGraphPipeline][RunInterServer] local rank[%u] localOffset[%llu]tx with slice[%llu]", rankId_,
94 : memOffset, memSliceSize_);
95 :
96 0 : DeviceMem rxLocalMem = DeviceMem::create(static_cast<u8*>(usrInMem_) + preMemOffset, memSliceSize_);
97 0 : CHK_RET(
98 : reducerInfo_->run(dispatcher_, prevInterLink, preMemOffset, rxLocalMem, rxLocalMem, rxLocalMem, subStream_[0]));
99 0 : return HCCL_SUCCESS;
100 0 : }
101 :
102 0 : HcclResult ReduceScatterGraphPipeline::RunAsync()
103 : {
104 : // inter ring algo
105 0 : u32 prevInterRankId = (interRankId_ + 1) % interRankSize_;
106 0 : u32 nextInterRankId = (interRankId_ - 1 + interRankSize_) % interRankSize_;
107 0 : LINK prevInterLink = interLinks_[prevInterRankId];
108 0 : LINK nextInterLink = interLinks_[nextInterRankId];
109 :
110 0 : for (u32 step = 0; step < interRankSize_; step++) {
111 0 : u32 begin = 0;
112 0 : if (step == 0) {
113 0 : begin = 1;
114 0 : CHK_RET(MainRecordSub(begin));
115 0 : CHK_RET(SubWaitMain(begin));
116 : }
117 : // server内做SDMA的reduce
118 0 : u64 blockIdx = ((interRankId_ + step + 1) % interRankSize_);
119 0 : CHK_RET(RunIntraServer(blockIdx));
120 0 : CHK_RET(SubRecordMain(begin));
121 0 : CHK_RET(MainWaitSub(begin));
122 0 : if (step < interRankSize_ - 1) {
123 : // 全部流同步,确保SDMA执行完成
124 0 : CHK_RET(MainRecordSub(0));
125 0 : CHK_RET(SubWaitMain(0));
126 0 : CHK_RET(prevInterLink->TxAck(subStream_[0]));
127 0 : CHK_RET(nextInterLink->RxAck(subStream_[0]));
128 : // server间做RDMA的reduce,可与下一个step的SDMA并发执行
129 0 : CHK_RET(RunInterServer(blockIdx, prevInterLink, nextInterLink));
130 0 : CHK_RET(prevInterLink->PostFinAck(subStream_[0]));
131 0 : CHK_RET(nextInterLink->WaitFinAck(subStream_[0]));
132 : // inter的最后一步需要barrier确保数据发完
133 0 : if (step == interRankSize_ - STEP_OFFSET_TWO) {
134 0 : CHK_RET(ExecuteBarrier(prevInterLink, nextInterLink, subStream_[0]));
135 : }
136 : }
137 : }
138 : // 把对应的切片从usrIn拷贝到userOut
139 0 : DeviceMem locSrc = DeviceMem::create(static_cast<u8*>(usrInMem_) + rankId_ * memSliceSize_, memSliceSize_);
140 0 : DeviceMem locDst = DeviceMem::create(static_cast<u8*>(usrOutMem_), memSliceSize_);
141 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, locDst, locSrc, stream_));
142 0 : HCCL_INFO("[ReduceScatterGraphPipeline][RunAsync]ReduceScatterGraphPipeline finished groupRankId[%u] ", rankId_);
143 0 : return HCCL_SUCCESS;
144 0 : }
145 :
146 : // 适配新CollExecutor接口
147 0 : HcclResult ReduceScatterGraphPipeline::Prepare(
148 : HcomCollOpInfo* opInfo, [[maybe_unused]] DeviceMem& cclBuffer, [[maybe_unused]] const u64 count,
149 : [[maybe_unused]] const u64 bufferSize, const u64 offset, const SubCommInfo& level0CommInfo,
150 : const SubCommInfo& level1CommInfo, Stream& mainStream, std::vector<Stream>& subStream,
151 : std::vector<std::shared_ptr<LocalNotify>>& notifyMain, std::vector<std::shared_ptr<LocalNotify>>& notifySub,
152 : u64 reduceAttrBitMap)
153 : {
154 0 : reduceAttr_ = reduceAttrBitMap;
155 0 : opInfo_ = opInfo;
156 :
157 0 : unitSize_ = SIZE_TABLE[opInfo_->dataType];
158 0 : count_ = opInfo_->count;
159 0 : memSliceSize_ = opInfo_->count * unitSize_;
160 0 : usrInMem_ = opInfo_->inputAddr;
161 0 : usrOutMem_ = opInfo_->outputAddr;
162 0 : reductionOp_ = opInfo_->reduceOp;
163 0 : dataType_ = opInfo_->dataType;
164 0 : offset_ = offset;
165 :
166 : // needed resource
167 : // stream: 1 * mainStream + n * subStream
168 : // mem: usrInMem_, usrOutMem
169 : // interNotify, streamNotify
170 :
171 : // stream
172 : // mainStream负责locMemCPY以及subStream同步控制
173 0 : stream_ = mainStream;
174 : // subStream负责:
175 : // streamId[0]: inter执行
176 : // streamId[1:intraRankSize]: intraRankSize-1个intra执行
177 0 : subStream_ = subStream;
178 :
179 : // DMAMem + interNotify from Link
180 0 : intraRankSize_ = level0CommInfo.localRankSize;
181 0 : interRankSize_ = level1CommInfo.localRankSize;
182 0 : intraRankId_ = level0CommInfo.localRank;
183 0 : interRankId_ = level1CommInfo.localRank;
184 0 : rankId_ = intraRankId_ + interRankId_ * intraRankSize_;
185 :
186 : // streamNotify, size: n
187 0 : streamNotifyMain_ = notifyMain;
188 0 : if (streamNotifyMain_.size() < intraRankSize_) {
189 0 : HCCL_ERROR(
190 : "[ReduceScatterGraphPipeline][Prepare]rank[%u] streamNotifyMain_ size [%u] error, is smaller than,"
191 : "intraRankSize_[%u]",
192 : rankId_, streamNotifyMain_.size(), intraRankSize_);
193 0 : return HCCL_E_INTERNAL;
194 : }
195 0 : streamNotifySub_ = notifySub;
196 0 : if (streamNotifySub_.size() < intraRankSize_) {
197 0 : HCCL_ERROR(
198 : "[ReduceScatterGraphPipeline][Prepare]rank[%u] streamNotifySub_ size [%u] error, is smaller than,"
199 : "intraRankSize_[%u]",
200 : rankId_, streamNotifySub_.size(), intraRankSize_);
201 0 : return HCCL_E_INTERNAL;
202 : }
203 :
204 0 : intraLinks_ = level0CommInfo.links;
205 0 : interLinks_ = level1CommInfo.links;
206 :
207 0 : HCCL_INFO(
208 : "[ReduceScatterGraphPipeline][Prepare]streamNum[%u], streamNotifyMainNum[%u], streamNotifySubNum[%u]",
209 : subStream_.size(), streamNotifyMain_.size(), streamNotifySub_.size());
210 0 : HCCL_INFO(
211 : "[ReduceScatterGraphPipeline][Prepare]interLinksNum[%u], intraLinksNum[%u]", interLinks_.size(),
212 : intraLinks_.size());
213 0 : senderInfo_.reset(new (std::nothrow) Sender(dataType_, reductionOp_, reduceAttr_));
214 0 : CHK_SMART_PTR_NULL(senderInfo_);
215 0 : reducerInfo_.reset(new (std::nothrow) Reducer(dataType_, reductionOp_, reduceAttr_));
216 0 : CHK_SMART_PTR_NULL(reducerInfo_);
217 0 : return HCCL_SUCCESS;
218 : }
219 :
220 : REGISTER_TEMPLATE(TemplateType::TEMPLATE_REDUCESCATTER_GRAPH_PIPELINE, ReduceScatterGraphPipeline);
221 : } // namespace hccl
|