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_broadcast_mesh1d_mem2mem.h"
12 : #include "ccu_instruction_broadcast_mesh1d_mem2mem.h"
13 :
14 : namespace Hccl {
15 : constexpr int INPUT_XN_ID = 0;
16 : constexpr int OUTPUT_XN_ID = 1;
17 : constexpr int TOKEN_XN_ID = 2;
18 : constexpr int CKE_IDX_0 = 0;
19 : constexpr int CKE_IDX_1 = 1;
20 : constexpr int CKE_IDX_2 = 2;
21 : constexpr int CKE_IDX_3 = 3;
22 : constexpr int CKE_IDX_4 = 4;
23 :
24 : using CurrentCtxArg = CcuCtxArgBroadcastMesh1DMem2Mem;
25 : using CurrentTaskArg = CcuTaskArgBroadcastMesh1DMem2Mem;
26 :
27 0 : CcuContextBroadcastMesh1DMem2Mem::CcuContextBroadcastMesh1DMem2Mem(
28 0 : const CcuCtxArg &arg, const std::vector<CcuTransport *> &transports, const CcuTransportGroup &group)
29 0 : : CcuContextAlgBase(arg, transports, group)
30 : {
31 0 : HCCL_DEBUG("[CcuContextBroadcastMesh1DMem2Mem] Enter Constructor");
32 0 : const CurrentCtxArg *ctxArg = dynamic_cast<const CurrentCtxArg *>(&arg);
33 0 : if (ctxArg == nullptr) {
34 0 : THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem::ctxArg ptr is null"));
35 : }
36 0 : if (ctxArg->dimSize_.size() != 1) {
37 0 : THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem::dimSize is not 1"));
38 : }
39 0 : rankId_ = ctxArg->rankId_;
40 0 : rootId_ = ctxArg->rootId_;
41 0 : rankSize_ = ctxArg->dimSize_[0];
42 0 : dataType_ = ctxArg->op_.dataType;
43 0 : outputDataType_ = ctxArg->op_.outputDataType;
44 :
45 0 : if (outputDataType_ == DataType::INVALID) {
46 0 : outputDataType_ = dataType_;
47 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] outputDataType is [INVALID], set outputDataType to[%s]",
48 : outputDataType_.Describe().c_str());
49 : }
50 :
51 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] init end, ctxArg->dimSize size[%u] rankSize[%llu].",
52 : ctxArg->dimSize_.size(), rankSize_);
53 0 : }
54 :
55 0 : void CcuContextBroadcastMesh1DMem2Mem::InitResource()
56 : {
57 0 : if (transports.size() == 0) {
58 0 : THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem transports is empty"));
59 : }
60 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem]transports.size: [%u]", transports.size());
61 0 : uint16_t transportIdx = 0;
62 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
63 0 : for (uint16_t peerId = 0; peerId < rankSize_; peerId++) {
64 0 : if (peerId == rankId_) {
65 0 : input_.push_back(CreateVariable());
66 0 : output_.push_back(CreateVariable());
67 0 : token_.push_back(CreateVariable());
68 : } else {
69 0 : HCCL_DEBUG("[CcuContextBroadcastMesh1DMem2Mem] MyRank[%u], PeerId[%hu], TransportId[%hu]",
70 : rankId_, peerId, transportIdx);
71 : // 判断transport是否为空,为空直接报错
72 0 : CHK_PRT_THROW(
73 : transports[transportIdx] == nullptr || transportIdx >= transports.size(),
74 : HCCL_ERROR("[CcuContextBroadcastMesh1DMem2Mem] [InitResource] transports[%u] is nullptr or out of bounds",
75 : transportIdx),
76 : NullPtrException, "transport is null");
77 0 : input_.push_back(
78 0 : CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递input
79 0 : output_.push_back(
80 0 : CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID)); // 获取transport中id=2的Var来传递output
81 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
82 0 : transportIdx++; // transport对应远端通信数
83 : }
84 : }
85 0 : currentRankSliceInputOffset_ = CreateVariable();
86 0 : currentRankSliceOutputOffset_ = CreateVariable();
87 0 : inputRepeatStride_ = CreateVariable();
88 0 : outputRepeatStride_ = CreateVariable();
89 0 : normalSliceSize_ = CreateVariable();
90 0 : lastSliceSize_ = CreateVariable();
91 0 : allgatherOffset_ = CreateVariable();
92 0 : repeatNumVar_ = CreateVariable();
93 0 : flag_ = CreateVariable();
94 0 : SliceOffset_ = CreateVariable();
95 :
96 0 : selfBit_ = 1 << rankId_;
97 0 : allBit_ = ((1 << rankSize_) - 1) & (~(1 << rankId_));
98 :
99 0 : scattersrcMem_.reserve(rankSize_);
100 0 : scatterdstMem_.reserve(rankSize_);
101 0 : allgatherdstMem_.reserve(rankSize_);
102 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
103 0 : scattersrcMem_.push_back(CreateMemory());
104 0 : scatterdstMem_.push_back(CreateMemory());
105 0 : allgatherdstMem_.push_back(CreateMemory());
106 : }
107 0 : localSignal_ = CreateMaskSignal();
108 0 : return;
109 : }
110 :
111 0 : void CcuContextBroadcastMesh1DMem2Mem::LoadArgs()
112 : {
113 0 : Load(input_[rankId_]);
114 0 : Load(output_[rankId_]);
115 0 : Load(token_[rankId_]);
116 0 : Load(currentRankSliceInputOffset_);
117 0 : Load(currentRankSliceOutputOffset_);
118 0 : Load(inputRepeatStride_);
119 0 : Load(outputRepeatStride_);
120 0 : Load(normalSliceSize_);
121 0 : Load(lastSliceSize_);
122 0 : Load(allgatherOffset_);
123 0 : Load(repeatNumVar_);
124 0 : return;
125 : }
126 :
127 0 : void CcuContextBroadcastMesh1DMem2Mem::PreSync() // 前同步
128 : {
129 0 : for (auto t : transports) {
130 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D LocalPost begin");
131 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit_); // index = 1,传递input信息
132 0 : WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_2, selfBit_); // index = 0,传递output信息
133 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit_); // index = 2,传递token信息
134 : }
135 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit_);
136 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit_);
137 0 : GroupWait(*transportGroup, CKE_IDX_3, allBit_);
138 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D wait all end");
139 0 : return;
140 : }
141 :
142 0 : void CcuContextBroadcastMesh1DMem2Mem::PostSync(int CKE_id) // 后同步
143 : {
144 0 : for (auto t : transports) {
145 0 : RemotePost(*t, CKE_id, selfBit_);
146 : }
147 0 : GroupWait(*transportGroup, CKE_id, allBit_);
148 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D groupwait end");
149 0 : }
150 :
151 0 : void CcuContextBroadcastMesh1DMem2Mem::DoRepeaScatterMem2Mem()
152 : {
153 0 : if (rankId_ != rootId_) {
154 0 : return;
155 : }
156 0 : std::vector<CcuRep::Memory> &src = scattersrcMem_;
157 0 : std::vector<CcuRep::Memory> &dst = scatterdstMem_;
158 :
159 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
160 0 : if (rankIdx == 0) {
161 0 : SliceOffset_ = 0;
162 : } else {
163 0 : SliceOffset_ += normalSliceSize_;
164 : }
165 :
166 0 : src[rankIdx].addr = input_[rankId_];
167 0 : src[rankIdx].addr += currentRankSliceInputOffset_;
168 0 : src[rankIdx].addr += SliceOffset_;
169 0 : src[rankIdx].token = token_[rankIdx];
170 :
171 0 : dst[rankIdx].addr = output_[rankIdx];
172 0 : dst[rankIdx].addr += currentRankSliceOutputOffset_;
173 0 : dst[rankIdx].addr += SliceOffset_;
174 0 : dst[rankIdx].token = token_[rankIdx];
175 :
176 0 : CCU_IF(flag_ != 0)
177 : {
178 : // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
179 0 : for (uint32_t curId = 0; curId < rankSize_; curId++) {
180 0 : src[curId].addr += inputRepeatStride_;
181 0 : dst[curId].addr += outputRepeatStride_;
182 : }
183 0 : }
184 : }
185 0 : DoScatter(src, dst);
186 : }
187 0 : void CcuContextBroadcastMesh1DMem2Mem::DoRepeatAllGatherMem2Mem()
188 : {
189 0 : CcuRep::Memory &src = scatterdstMem_[rankId_];
190 0 : std::vector<CcuRep::Memory> &dst = allgatherdstMem_;
191 0 : src.addr = output_[rankId_];
192 0 : src.addr += currentRankSliceOutputOffset_;
193 0 : src.addr += allgatherOffset_;
194 0 : src.token = token_[rankId_];
195 :
196 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
197 0 : dst[rankIdx].addr = output_[rankIdx];
198 0 : dst[rankIdx].addr += currentRankSliceOutputOffset_;
199 0 : dst[rankIdx].addr += allgatherOffset_;
200 0 : dst[rankIdx].token = token_[rankIdx];
201 : }
202 0 : CCU_IF(flag_ != 0)
203 : {
204 : // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
205 0 : src.addr += inputRepeatStride_;
206 0 : for (auto &d : dst) {
207 0 : d.addr += outputRepeatStride_;
208 : }
209 0 : }
210 :
211 0 : DoAllGather(src, dst);
212 0 : }
213 :
214 0 : void CcuContextBroadcastMesh1DMem2Mem::DoScatter(const std::vector<CcuRep::Memory> &src,
215 : const std::vector<CcuRep::Memory> &dst)
216 : {
217 0 : uint64_t transportId = 0;
218 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
219 0 : auto &sliceSize = (rankIdx + 1 == rankSize_) ? lastSliceSize_ : normalSliceSize_;
220 0 : CCU_IF(sliceSize != 0)
221 : {
222 0 : if (rankIdx == rankId_) {
223 0 : LocalPost(localSignal_, 1 << rankIdx);
224 : } else {
225 0 : Write(*transports[transportId], dst[rankIdx], src[rankIdx], sliceSize, localSignal_, 1 << rankIdx);
226 0 : transportId++;
227 : }
228 0 : }
229 0 : CCU_IF(sliceSize == 0)
230 : {
231 0 : LocalPost(localSignal_, 1 << rankIdx);
232 0 : }
233 : }
234 :
235 0 : LocalWait(localSignal_, (1 << rankSize_) - 1);
236 0 : }
237 :
238 0 : void CcuContextBroadcastMesh1DMem2Mem::DoAllGather(const CcuRep::Memory &src,
239 : const std::vector<CcuRep::Memory> &dst)
240 : {
241 0 : uint64_t transportId = 0;
242 0 : auto &sliceSize = (rankId_ + 1 == rankSize_) ? lastSliceSize_ : normalSliceSize_;
243 0 : CCU_IF(sliceSize != 0)
244 : {
245 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
246 0 : if (rankIdx == rankId_) {
247 0 : LocalPost(localSignal_, (1 << rankIdx));
248 : } else {
249 0 : Write(*transports[transportId], dst[rankIdx], src, sliceSize, localSignal_, 1 << rankIdx);
250 0 : transportId++;
251 : }
252 : }
253 0 : LocalWait(localSignal_, (1 << rankSize_) - 1);
254 0 : }
255 0 : }
256 :
257 0 : void CcuContextBroadcastMesh1DMem2Mem::Algorithm()
258 : {
259 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D run");
260 0 : InitResource();
261 0 : LoadArgs();
262 0 : PreSync();
263 :
264 0 : CcuRep::Variable repeatNumAdd = CreateVariable();
265 0 : repeatNumAdd = 1;
266 :
267 0 : flag_ = 0;
268 0 : CCU_WHILE(repeatNumVar_ != UINT64_MAX)
269 : { // 循环repeatNum_次
270 0 : DoRepeaScatterMem2Mem();
271 0 : PostSync(CKE_IDX_4);
272 0 : DoRepeatAllGatherMem2Mem();
273 0 : PostSync(CKE_IDX_0);
274 0 : repeatNumVar_ += repeatNumAdd;
275 0 : flag_ = 1;
276 0 : }
277 :
278 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] BroadcastMesh1D end");
279 0 : return;
280 0 : }
281 :
282 0 : std::vector<uint64_t> CcuContextBroadcastMesh1DMem2Mem::GeneArgs(const CcuTaskArg &arg)
283 : {
284 0 : const CurrentTaskArg *taskArg = dynamic_cast<const CurrentTaskArg *>(&arg);
285 : // 空指针校验
286 0 : if (taskArg == nullptr) {
287 0 : THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1DMem2Mem::taskArg ptr is null"));
288 : }
289 0 : uint64_t inputAddr = taskArg->inputAddr_;
290 0 : uint64_t outputAddr = taskArg->outputAddr_;
291 0 : uint64_t tokenInfo = taskArg->token_;
292 :
293 0 : uint64_t currentRankSliceInputOffset = taskArg->inputSliceStride_ * rankId_;
294 0 : uint64_t currentRankSliceOutputOffset = taskArg->outputSliceStride_ * rankId_;
295 0 : uint64_t inputRepeatStride = taskArg->inputRepeatStride_;
296 0 : uint64_t outputRepeatStride = taskArg->outputRepeatStride_;
297 0 : uint64_t normalSliceSize = taskArg->normalSliceSize_;
298 0 : uint64_t lastSliceSize = taskArg->lastSliceSize_;
299 0 : uint64_t allgatherOffset = taskArg->normalSliceSize_ * rankId_;
300 0 : uint64_t repeatNumVar = taskArg->repeatNumVar_;
301 : std::vector<uint64_t> taskArgs = {
302 : inputAddr,
303 : outputAddr,
304 : tokenInfo,
305 : currentRankSliceInputOffset,
306 : currentRankSliceOutputOffset,
307 : inputRepeatStride,
308 : outputRepeatStride,
309 : normalSliceSize,
310 : lastSliceSize,
311 : allgatherOffset,
312 : repeatNumVar,
313 0 : };
314 :
315 0 : HCCL_INFO("[CcuContextBroadcastMesh1DMem2Mem] TaskArgs: inputAddr[%llx], outputAddr[%llx], "
316 : "currentRankSliceInputOffset[%llu], "
317 : "currentRankSliceOutputOffset[%llu], inputRepeatStride[%llu], outputRepeatStride[%llu], "
318 : "normalSliceSize[%llu], lastSliceSize[%llu], allgatherSliceSize[%llu], repeatNumVar[%llu] ",
319 : inputAddr, outputAddr, currentRankSliceInputOffset, currentRankSliceOutputOffset, inputRepeatStride,
320 : outputRepeatStride, normalSliceSize, lastSliceSize, allgatherOffset, repeatNumVar);
321 :
322 0 : return taskArgs;
323 0 : }
324 : } // namespace Hccl
|