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_mesh2d_mem2mem.h"
12 : #include "ccu_instruction_reduce_mesh2d_mem2mem.h"
13 :
14 : namespace Hccl {
15 :
16 0 : CcuContextReduceMeshMem2Mem2D::CcuContextReduceMeshMem2Mem2D(const CcuCtxArg &arg,
17 : const std::vector<CcuTransport *> &transports,
18 0 : const CcuTransportGroup &group)
19 0 : : CcuContextAlgBase(arg, transports, group)
20 : {
21 0 : const CcuCtxArgReduceMeshMem2Mem2D *ctxArg = dynamic_cast<const CcuCtxArgReduceMeshMem2Mem2D *>(&arg);
22 0 : if (ctxArg == nullptr) {
23 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem2D::ctxArg ptr is null"));
24 : }
25 0 : rankId_ = ctxArg->rankId_;
26 0 : dimSize_ = ctxArg->dimSize_;
27 0 : axisId_ = ctxArg->axisId_; // 要进行操作的是 行或列
28 :
29 0 : if (dimSize_.size() != 2 || axisId_ > 1 || dimSize_[0] == 0) { // 2D 拓扑校验
30 0 : THROW<NullPtrException>(
31 0 : StringFormat("[CcuContextReduceMeshMem2Mem2D] dimSize[%u] or axisId[%u] or dimSize[0] [%u] is invalid",
32 0 : dimSize_.size(), axisId_, dimSize_[0]));
33 : }
34 0 : dimId_.emplace_back(rankId_ % dimSize_[0]);
35 0 : dimId_.emplace_back(rankId_ / dimSize_[0]);
36 0 : localId_ = dimId_[axisId_]; // 本rank所在的行/列
37 0 : localSize_ = dimSize_[axisId_]; // 本rank所在的行/列的总数
38 :
39 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] RankId[%u], DimSize0[%llu], DimSize1[%llu], localId[%llu], lcoalSize[%llu]",
40 : rankId_, dimSize_[0], dimSize_[1], localId_, localSize_);
41 :
42 0 : dataType_ = ctxArg->op_.dataType;
43 0 : outputDataType_ = ctxArg->op_.outputDataType;
44 0 : if (outputDataType_ == DataType::INVALID) {
45 0 : outputDataType_ = dataType_;
46 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] outputDataType is [INVALID], set outputDataType to[%s]",
47 : outputDataType_.Describe().c_str());
48 : }
49 0 : reduceOp_ = ctxArg->op_.reduceOp;
50 0 : rootId_ = ctxArg->rootId_;
51 0 : rootDimId_.emplace_back(rootId_ % dimSize_[0]); // root的x
52 0 : rootDimId_.emplace_back(rootId_ / dimSize_[0]); // root的y
53 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] init end, ctxArg->dimSize size[%zu] localSize_[%u]",
54 : ctxArg->dimSize_.size(), localSize_);
55 :
56 0 : localAxisSignalName_ = "CcuContextReduceMeshMem2Mem2DAxisSync_" + std::to_string(axisId_);
57 0 : anotherAxisSignalName_ = "CcuContextReduceMeshMem2Mem2DAxisSync_" + std::to_string(1 - axisId_);
58 0 : }
59 :
60 0 : void CcuContextReduceMeshMem2Mem2D::InitResources()
61 : {
62 0 : localAxisSignal_ = CreateMaskSignal();
63 0 : anotherAxisSignal_ = CreateMaskSignal();
64 0 : ExportMaskSignal(localAxisSignal_, localAxisSignalName_);
65 0 : anotherAxisSignal_ = ImportMaskSignal(anotherAxisSignalName_);
66 :
67 0 : output_ = CreateVariable();
68 0 : if (transports.size() == 0) {
69 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem2D transports is empty"));
70 : }
71 0 : uint32_t transportIdx = 0;
72 0 : for (uint32_t peerId = 0; peerId < localSize_; peerId++) {
73 0 : if (peerId == localId_) {
74 0 : input_.push_back(CreateVariable());
75 0 : token_.push_back(CreateVariable());
76 : } else {
77 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] MyRank[%u], PeerId[%u], TransportId[%u]", localId_, peerId,
78 : transportIdx);
79 0 : CHK_PRT_RET(transports[transportIdx] == nullptr,
80 : HCCL_ERROR("[CcuContextReduceMeshMem2Mem2D] Algorithm transport ptr is null"), );
81 0 : input_.push_back(
82 0 : CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
83 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
84 0 : transportIdx++;
85 : }
86 : }
87 0 : locMask_ = CreateMaskSignal();
88 0 : xAxisGroupOpSize_ = CreateGroupOpSize();
89 0 : yAxisGroupOpSize_ = CreateGroupOpSize();
90 0 : xAxisSize_ = CreateVariable();
91 0 : yAxisSize_ = CreateVariable();
92 0 : yAxisOffset_ = CreateVariable();
93 0 : curGoSize_ = CreateGroupOpSize();
94 0 : for (uint16_t roundId = 0; roundId < (localSize_ - 1); roundId++) {
95 0 : xChunkSize_.push_back(CreateVariable());
96 0 : yChunkSize_.push_back(CreateVariable());
97 0 : chunkSize_.push_back(CreateVariable());
98 : }
99 0 : chunkOffset_ = CreateVariable();
100 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] InitResources finished");
101 : }
102 :
103 0 : void CcuContextReduceMeshMem2Mem2D::LoadArgs()
104 : {
105 0 : Load(input_[localId_]);
106 0 : Load(output_);
107 0 : Load(token_[localId_]);
108 0 : Load(xAxisSize_);
109 0 : Load(yAxisSize_);
110 0 : Load(yAxisOffset_);
111 0 : for (uint16_t i = 0; i < (localSize_ - 1); i++) {
112 0 : Load(xChunkSize_[i]);
113 : }
114 0 : for (uint16_t i = 0; i < (localSize_ - 1); i++) {
115 0 : Load(yChunkSize_[i]);
116 : }
117 0 : Load(xAxisGroupOpSize_);
118 0 : Load(yAxisGroupOpSize_);
119 : // 只有step2会用到localcopy
120 0 : curGoSize_ = (axisId_ == X_AXIS_ID) ? yAxisGroupOpSize_ : xAxisGroupOpSize_;
121 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] LoadArgs run finished");
122 0 : }
123 :
124 0 : void CcuContextReduceMeshMem2Mem2D::PreSync() // 前同步
125 : {
126 0 : uint16_t selfBit = 1 << localId_;
127 0 : uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
128 :
129 0 : for (auto t : transports) {
130 0 : WriteVariableWithSignal(*t, input_[localId_], INPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
131 0 : WriteVariableWithSignal(*t, token_[localId_], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
132 : }
133 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit); // index = 1,传递output信息
134 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit); // index = 2,传递token信息
135 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] PreSync run finished");
136 0 : }
137 :
138 0 : void CcuContextReduceMeshMem2Mem2D::PostSync(uint32_t signalIndex)
139 : {
140 0 : uint16_t selfBit = 1 << localId_;
141 0 : uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
142 :
143 0 : for (auto t : transports) {
144 0 : RemotePost(*t, signalIndex, selfBit);
145 : }
146 0 : GroupWait(*transportGroup, signalIndex, allBit);
147 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] PostSync run finished");
148 0 : }
149 :
150 0 : void CcuContextReduceMeshMem2Mem2D::AxisSync(uint32_t signalIndex) // 轴间同步
151 : {
152 0 : const uint32_t DIE_NUM = 2;
153 0 : LocalCtxPost(anotherAxisSignal_, 1 << (axisId_ + signalIndex * DIE_NUM));
154 0 : LocalWait(localAxisSignal_, 1 << (1 - axisId_ + signalIndex * DIE_NUM));
155 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] AxisSync run finished");
156 0 : return;
157 : }
158 :
159 0 : void CcuContextReduceMeshMem2Mem2D::ReduceStep1()
160 : {
161 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] RankId [%u], axisId [%u],Reduce Step1 starts", rankId_, axisId_);
162 0 : uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
163 0 : CcuRep::Memory dst = CreateMemory();
164 0 : CcuRep::Memory src = CreateMemory();
165 0 : dst.addr = input_[localId_]; // step1 reduce到input
166 0 : dst.token = token_[localId_];
167 0 : bool isYAxis = (axisId_ == Y_AXIS_ID);
168 0 : CcuRep::Memory tmpDst = CreateMemory();
169 0 : chunkSize_ = isYAxis ? yChunkSize_ : xChunkSize_;
170 0 : for (uint16_t i = 0; i < (localSize_ - 1); i++) { // 外层循环控制步数=chunk数量
171 : // 读不同rank的不同chunk
172 0 : for (uint16_t rmtId = 0; rmtId < localSize_; ++rmtId) {
173 0 : if (rmtId == localId_) {
174 0 : continue;
175 : }
176 0 : src.addr = input_[rmtId];
177 0 : src.token = token_[rmtId];
178 0 : tmpDst.addr = dst.addr;
179 0 : tmpDst.token = dst.token;
180 0 : if (isYAxis) { // 第一步yslicesize要在y轴方向reduce
181 0 : src.addr += yAxisOffset_;
182 0 : tmpDst.addr += yAxisOffset_;
183 : }
184 0 : chunkOffset_ = 0;
185 0 : uint16_t chkId = 0;
186 0 : if (rmtId < localId_) {
187 0 : chkId = (i + rmtId) % (localSize_ - 1);
188 : } else {
189 0 : chkId = (i + rmtId - 1) % (localSize_ - 1);
190 : }
191 : // 计算一下offset 0~(chikd-1)
192 0 : for (uint16_t j = 0; j < chkId; ++j) {
193 0 : chunkOffset_ += chunkSize_[j];
194 : }
195 : // 更新对应的addr
196 0 : src.addr += chunkOffset_;
197 0 : tmpDst.addr += chunkOffset_;
198 0 : CCU_IF(chunkSize_[chkId] == 0)
199 : {
200 0 : LocalPost(locMask_, 1 << rmtId);
201 0 : }
202 :
203 0 : CCU_IF(chunkSize_[chkId] != 0)
204 : {
205 0 : uint16_t transId = rmtId < localId_ ? rmtId : rmtId - 1;
206 0 : ReadReduce(*transports[transId], tmpDst, src, chunkSize_[chkId], dataType_, reduceOp_, locMask_,
207 0 : 1 << rmtId);
208 0 : }
209 : }
210 0 : LocalWait(locMask_, allBit);
211 : }
212 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] Reduce Step1 ends");
213 0 : }
214 :
215 0 : void CcuContextReduceMeshMem2Mem2D::ReduceStep2()
216 : {
217 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] RankId [%u] Reduce Step2 starts", rankId_);
218 0 : uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
219 0 : CcuRep::Memory dst = CreateMemory();
220 0 : CcuRep::Memory src = CreateMemory();
221 0 : dst.addr = output_; // 第二步reduce是从input reduce到root的output
222 0 : dst.token = token_[localId_];
223 :
224 0 : src.addr = input_[localId_];
225 0 : src.token = token_[localId_];
226 0 : bool isXAxis = (axisId_ == X_AXIS_ID);
227 0 : chunkSize_ = isXAxis ? yChunkSize_ : xChunkSize_;
228 0 : if (isXAxis) // 第二步yslicesize要在x轴方向reduce
229 : {
230 0 : src.addr += yAxisOffset_;
231 0 : dst.addr += yAxisOffset_;
232 : }
233 0 : GroupCopy(dst, src, curGoSize_);
234 0 : for (uint16_t i = 0; i < (localSize_ - 1); i++) {
235 0 : for (uint16_t rmtId = 0; rmtId < localSize_; ++rmtId) {
236 0 : if (rmtId == localId_) {
237 0 : continue;
238 : }
239 0 : dst.addr = output_;
240 0 : src.addr = input_[rmtId];
241 0 : src.token = token_[rmtId];
242 0 : if (isXAxis) {
243 0 : src.addr += yAxisOffset_;
244 0 : dst.addr += yAxisOffset_;
245 : }
246 0 : uint16_t chkId = 0;
247 0 : chunkOffset_ = 0;
248 0 : if (rmtId < localId_) {
249 0 : chkId = (i + rmtId) % (localSize_ - 1);
250 : } else {
251 0 : chkId = (i + rmtId - 1) % (localSize_ - 1);
252 : }
253 : // 计算一下offset 0~(chikd-1)
254 0 : for (uint16_t j = 0; j < chkId; ++j) {
255 0 : chunkOffset_ += chunkSize_[j];
256 : }
257 : // 更新对应的addr
258 0 : src.addr += chunkOffset_;
259 0 : dst.addr += chunkOffset_;
260 0 : CCU_IF(chunkSize_[chkId] == 0)
261 : {
262 0 : LocalPost(locMask_, 1 << rmtId);
263 0 : }
264 0 : CCU_IF(chunkSize_[chkId] != 0)
265 : {
266 0 : uint16_t transId = rmtId < localId_ ? rmtId : rmtId - 1;
267 0 : ReadReduce(*transports[transId], dst, src, chunkSize_[chkId], dataType_, reduceOp_, locMask_,
268 0 : 1 << rmtId);
269 0 : }
270 : }
271 0 : LocalWait(locMask_, allBit);
272 : }
273 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] Reduce Step2 ends");
274 0 : }
275 :
276 0 : void CcuContextReduceMeshMem2Mem2D::Algorithm()
277 : {
278 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] ReduceMeshMem2Mem2D run");
279 0 : InitResources();
280 0 : LoadArgs();
281 0 : PreSync(); // 前同步
282 0 : if (rankId_ == rootId_ || (dimId_[1] == rootDimId_[1] && axisId_ == Y_AXIS_ID)
283 0 : || (dimId_[0] == rootDimId_[0] && axisId_ == X_AXIS_ID)) {
284 : // 与root同行的元素要在Y方向规约 同列元素要在X方向规约
285 0 : ReduceStep1();
286 : }
287 0 : AxisSync(0);
288 0 : PostSync(CKE_IDX_3);
289 0 : AxisSync(1);
290 0 : if (rankId_ == rootId_) { // 第二步只有root进行readreduce
291 0 : ReduceStep2();
292 : }
293 0 : AxisSync(0);
294 0 : PostSync(CKE_IDX_0);
295 0 : AxisSync(1);
296 0 : }
297 :
298 0 : std::vector<uint64_t> CcuContextReduceMeshMem2Mem2D::CalMeshChunkSlice(uint64_t dataSize, uint64_t sliceNum)
299 : {
300 0 : uint64_t dataCount = dataSize / DataTypeSizeGet(dataType_);
301 0 : uint64_t bigDataSliceNum = dataCount % sliceNum;
302 0 : uint64_t bigDataSliceSize = (dataCount / sliceNum + 1) * DataTypeSizeGet(dataType_);
303 0 : uint64_t smallDataSliceNum = sliceNum - dataCount % sliceNum;
304 0 : uint64_t smallDataSliceSize = dataCount / sliceNum * DataTypeSizeGet(dataType_);
305 0 : return {bigDataSliceNum, bigDataSliceSize, smallDataSliceNum, smallDataSliceSize};
306 : }
307 :
308 0 : std::vector<uint64_t> CcuContextReduceMeshMem2Mem2D::GeneArgs(const CcuTaskArg &arg)
309 : {
310 0 : const CcuTaskArgReduceMeshMem2Mem2D *taskArg = dynamic_cast<const CcuTaskArgReduceMeshMem2Mem2D *>(&arg);
311 0 : if (taskArg == nullptr) {
312 0 : THROW<NullPtrException>(StringFormat("CcuTaskArgReduceMeshMem2Mem2D::taskArg ptr is null"));
313 : }
314 0 : uint64_t inputAddr = taskArg->inputAddr_;
315 0 : uint64_t outputAddr = taskArg->outputAddr_;
316 0 : uint64_t tokenInfo = taskArg->token_;
317 0 : uint64_t xAxisSize = taskArg->xAxisSize_;
318 0 : uint64_t yAxisSize = taskArg->yAxisSize_;
319 0 : uint64_t yAxisOffset = xAxisSize;
320 0 : auto xAxisGoSize = CalGoSize(xAxisSize);
321 0 : auto yAxisGoSize = CalGoSize(yAxisSize);
322 0 : std::vector<uint64_t> processReturn = {inputAddr, outputAddr, tokenInfo, xAxisSize, yAxisSize, yAxisOffset};
323 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem2D] ReduceMeshMem2Mem2D inputAddr [%llu] outputAddr [%llu] "
324 : "xAxisSize [%llu] yAxisSize [%llu],yAxisOffset[%llu],",
325 : inputAddr, outputAddr, xAxisSize, yAxisSize, yAxisOffset);
326 : // mesh chunk for xslicesize
327 0 : std::vector<uint64_t> xChunkVec = CalMeshChunkSlice(xAxisSize, localSize_ - 1);
328 0 : for (uint64_t i = 0; i < xChunkVec[0]; i++) {
329 0 : processReturn.push_back(xChunkVec[1]);
330 : }
331 0 : for (uint64_t i = 0; i < xChunkVec[2]; i++) {
332 0 : processReturn.push_back(xChunkVec[3]);
333 : }
334 : // mesh chunk for yslicesize
335 0 : std::vector<uint64_t> yChunkVec = CalMeshChunkSlice(yAxisSize, localSize_ - 1);
336 0 : for (uint64_t i = 0; i < yChunkVec[0]; i++) {
337 0 : processReturn.push_back(yChunkVec[1]);
338 : }
339 0 : for (uint64_t i = 0; i < yChunkVec[2]; i++) {
340 0 : processReturn.push_back(yChunkVec[3]);
341 : }
342 : // for gosize
343 0 : processReturn.insert(processReturn.end(), xAxisGoSize.begin(), xAxisGoSize.end());
344 0 : processReturn.insert(processReturn.end(), yAxisGoSize.begin(), yAxisGoSize.end());
345 0 : return processReturn;
346 0 : }
347 : } // namespace Hccl
|