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_mesh1d_mem2mem.h"
12 : #include "ccu_instruction_reduce_mesh1d_mem2mem.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 : using CurrentCtxArg = CcuCtxArgReduceMeshMem2Mem1D;
25 : using CurrentTaskArg = CcuTaskArgReduceMeshMem2Mem1D;
26 :
27 0 : CcuContextReduceMeshMem2Mem1D::CcuContextReduceMeshMem2Mem1D(
28 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
29 0 : : CcuContextAlgBase(arg, transports, group)
30 : {
31 0 : const CurrentCtxArg* ctxArg = dynamic_cast<const CurrentCtxArg*>(&arg);
32 0 : if (ctxArg == nullptr) {
33 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem1D::ctxArg ptr is null"));
34 : }
35 0 : rankId_ = ctxArg->rankId_;
36 0 : rankSize_ = ctxArg->dimSize_[0];
37 0 : dataType_ = ctxArg->op_.dataType;
38 0 : outputDataType_ = ctxArg->op_.outputDataType;
39 0 : if (outputDataType_ == DataType::INVALID) {
40 0 : outputDataType_ = dataType_;
41 0 : HCCL_INFO(
42 : "[CcuContextReduceMeshMem2Mem1D] outputDataType is [INVALID], set outputDataType to[%s]",
43 : outputDataType_.Describe().c_str());
44 : }
45 0 : if (ctxArg->dimSize_.size() > 0) {
46 0 : rankSize_ = ctxArg->dimSize_[0];
47 : }
48 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] CtxArg: rankId[%u] rankSize[%llu]", rankId_, rankSize_);
49 0 : reduceOp_ = ctxArg->op_.reduceOp;
50 0 : rootId_ = ctxArg->rootId_;
51 0 : HCCL_INFO(
52 : "[CcuContextReduceMeshMem2Mem1D] init end, ctxArg->dimSize size[%zu] rankSize[%llu]", ctxArg->dimSize_.size(),
53 : rankSize_);
54 0 : }
55 :
56 0 : void CcuContextReduceMeshMem2Mem1D::InitResource()
57 : {
58 0 : if (transports.size() == 0) {
59 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem1D transports is empty"));
60 : }
61 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D]transports.size: [%zu]", transports.size());
62 : // 初始化资源
63 0 : uint16_t transportIdx = 0;
64 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
65 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
66 0 : if (peerId == rankId_) {
67 0 : input_.push_back(CreateVariable());
68 0 : output_.push_back(CreateVariable());
69 0 : token_.push_back(CreateVariable());
70 : } else {
71 0 : HCCL_INFO(
72 : "[CcuContextReduceMeshMem2Mem1D] MyRank[%u], PeerId[%llu], TransportId[%hu]", rankId_, peerId,
73 : transportIdx);
74 : // 判断transport是否为空,为空直接报错
75 0 : CHK_PRT_RET(
76 : transports[transportIdx] == nullptr || transportIdx >= transports.size(),
77 : HCCL_ERROR("[CcuContextReduceMeshMem2Mem1D] Algorithm transport ptr is null or transportIdx is out of "
78 : "bounds"), );
79 0 : input_.push_back(
80 0 : CreateVariable((*transports[transportIdx]), CKE_IDX_0)); // 获取transport中id=1的Var来传递input
81 0 : output_.push_back(
82 0 : CreateVariable((*transports[transportIdx]), CKE_IDX_1)); // 获取transport中id=2的Var来传递output
83 0 : token_.push_back(CreateVariable((*transports[transportIdx]), CKE_IDX_2));
84 0 : transportIdx++;
85 : }
86 : }
87 0 : for (uint16_t roundId = 0; roundId < (rankSize_ - 1); roundId++) {
88 0 : chunkSize_.push_back(CreateVariable());
89 : }
90 0 : inputRepeatStride_ = CreateVariable();
91 0 : outputRepeatStride_ = CreateVariable();
92 0 : normalSliceSize_ = CreateVariable();
93 0 : lastSliceSize_ = CreateVariable();
94 0 : repeatNumVar_ = CreateVariable();
95 0 : flag_ = CreateVariable();
96 0 : isInputOutputEqual_ = CreateVariable();
97 0 : selfBit_ = 1 << rankId_;
98 0 : allBit_ = ((1 << rankSize_) - 1) & (~(1 << rankId_));
99 :
100 0 : srcMem_ = CreateMemory();
101 0 : dstMem_ = CreateMemory();
102 0 : locMask_ = CreateMaskSignal();
103 0 : localGoSize_ = CreateGroupOpSize();
104 0 : chunkOffset_ = CreateVariable();
105 0 : AllocGoResource(CCU_MS_LOCAL_COPY_LOOP_COUNT, LOCAL_COPY_MS_PER_LOOP);
106 : }
107 :
108 0 : void CcuContextReduceMeshMem2Mem1D::LoadArgs()
109 : {
110 0 : Load(input_[rankId_]);
111 0 : Load(output_[rankId_]);
112 0 : Load(token_[rankId_]);
113 0 : Load(isInputOutputEqual_);
114 0 : Load(inputRepeatStride_);
115 0 : Load(outputRepeatStride_);
116 0 : Load(normalSliceSize_);
117 0 : Load(lastSliceSize_);
118 0 : Load(repeatNumVar_);
119 0 : for (uint16_t i = 0; i < (rankSize_ - 1); i++) {
120 0 : Load(chunkSize_[i]);
121 : }
122 0 : Load(localGoSize_);
123 0 : }
124 :
125 0 : void CcuContextReduceMeshMem2Mem1D::PreSync()
126 : {
127 : // 互换内存信息
128 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D LocalPost begin");
129 0 : for (auto t : transports) {
130 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit_); // index = 1,传递input信息
131 0 : WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_2, selfBit_); // index = 0,传递output信息
132 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit_); // index = 2,传递token信息
133 : }
134 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit_);
135 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit_);
136 0 : GroupWait(*transportGroup, CKE_IDX_3, allBit_);
137 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D wait all end");
138 0 : }
139 :
140 0 : void CcuContextReduceMeshMem2Mem1D::PostSync()
141 : {
142 0 : for (auto t : transports) {
143 0 : RemotePost(*t, CKE_IDX_0, selfBit_);
144 : }
145 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit_);
146 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMesh1D Reduce groupwait end");
147 0 : }
148 :
149 0 : void CcuContextReduceMeshMem2Mem1D::DoRepeatReduce(
150 : const std::vector<CcuRep::Variable>& srcAddr, const CcuRep::Variable& dstAddr)
151 : {
152 : // 从远程设备读取数据并逐步归约到本地设备
153 0 : CHK_PRT_THROW(
154 : srcAddr.size() != transports.size() + 1,
155 : HCCL_ERROR(
156 : "[ReadReduceRmtToLoc] srcAddr.size[%zu] != transports size[%zu] +1", srcAddr.size(), transports.size()),
157 : InvalidParamsException, "Invalid srcAddr size");
158 :
159 0 : dstMem_.addr = dstAddr;
160 0 : dstMem_.token = token_[rankId_];
161 :
162 0 : srcMem_.addr = srcAddr[rankId_];
163 0 : srcMem_.token = token_[rankId_];
164 0 : CCU_IF(flag_ != 0)
165 : {
166 : // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
167 0 : dstMem_.addr += outputRepeatStride_;
168 0 : srcMem_.addr += inputRepeatStride_;
169 0 : }
170 0 : CCU_IF(isInputOutputEqual_ == 0) { GroupCopy(dstMem_, srcMem_, localGoSize_); }
171 0 : for (uint16_t i = 0; i < (rankSize_ - 1); i++) { // 外层循环控制step
172 : // 读不同rank的不同chunk
173 0 : for (uint16_t rmtId = 0; rmtId < rankSize_; ++rmtId) {
174 0 : if (rmtId == rootId_) {
175 0 : continue;
176 : }
177 0 : chunkOffset_ = 0;
178 0 : dstMem_.addr = dstAddr;
179 0 : srcMem_.addr = srcAddr[rmtId];
180 0 : srcMem_.token = token_[rmtId];
181 :
182 0 : CCU_IF(flag_ != 0)
183 : {
184 : // 非第一轮执行时,src 和 dst 已经初始化,需要添加偏移量
185 0 : dstMem_.addr += outputRepeatStride_;
186 0 : srcMem_.addr += inputRepeatStride_;
187 0 : }
188 0 : uint16_t chkId = 0;
189 0 : if (rmtId < rankId_) {
190 0 : chkId = (i + rmtId) % (rankSize_ - 1);
191 : } else {
192 0 : chkId = (i + rmtId - 1) % (rankSize_ - 1);
193 : }
194 0 : uint16_t transId = rmtId < rootId_ ? rmtId : rmtId - 1;
195 0 : HCCL_DEBUG(
196 : "[ReadReduceRmtToLoc] debug rankId[%llu], root[%llu] chkId[%llu], rmtId[%llu] transId[%llu]", rankId_,
197 : rootId_, chkId, rmtId, transId);
198 :
199 : // 计算一下offset 0~(chikd-1)
200 0 : for (uint16_t j = 0; j < chkId; ++j) {
201 0 : chunkOffset_ += chunkSize_[j];
202 : }
203 : // 更新对应的addr
204 0 : srcMem_.addr += chunkOffset_;
205 0 : dstMem_.addr += chunkOffset_;
206 :
207 0 : CCU_IF(chunkSize_[chkId] == 0) { LocalPost(locMask_, 1 << rmtId); }
208 :
209 0 : CCU_IF(chunkSize_[chkId] != 0)
210 : {
211 0 : ReadReduce(
212 0 : *transports[transId], dstMem_, srcMem_, chunkSize_[chkId], dataType_, reduceOp_, locMask_,
213 0 : 1 << rmtId);
214 0 : }
215 : }
216 0 : LocalWait(locMask_, allBit_);
217 : }
218 :
219 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D ReadReduce end");
220 0 : }
221 :
222 0 : void CcuContextReduceMeshMem2Mem1D::Algorithm()
223 : {
224 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D run");
225 0 : InitResource();
226 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D load input variables, id: [%u]", rankId_);
227 0 : LoadArgs();
228 0 : PreSync();
229 0 : CCU_IF(normalSliceSize_ != 0) // 所有rank
230 : {
231 0 : if (rankId_ == rootId_) {
232 0 : CcuRep::Variable repeatNumAdd = CreateVariable();
233 0 : repeatNumAdd = 1;
234 0 : flag_ = 0;
235 0 : CCU_WHILE(repeatNumVar_ != UINT64_MAX)
236 : { // 循环repeatNum_次
237 : // root要去读每个rank每个chunk的数据
238 0 : DoRepeatReduce(input_, output_[rankId_]);
239 0 : repeatNumVar_ += repeatNumAdd;
240 0 : flag_ = 1;
241 0 : }
242 0 : }
243 0 : }
244 0 : PostSync();
245 0 : HCCL_INFO("[CcuContextReduceMeshMem2Mem1D] ReduceMeshMem2Mem1D end");
246 0 : return;
247 : }
248 :
249 0 : std::vector<uint64_t> CcuContextReduceMeshMem2Mem1D::GeneArgs(const CcuTaskArg& arg)
250 : {
251 0 : const CcuTaskArgReduceMeshMem2Mem1D* taskArg = dynamic_cast<const CcuTaskArgReduceMeshMem2Mem1D*>(&arg);
252 0 : if (taskArg == nullptr) {
253 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceMeshMem2Mem1D::taskArg ptr is null"));
254 : }
255 0 : uint64_t inputAddr = taskArg->inputAddr_;
256 0 : uint64_t outputAddr = taskArg->outputAddr_;
257 0 : uint64_t tokenInfo = taskArg->token_;
258 :
259 0 : uint64_t bigDataSliceNum = taskArg->bigDataSliceNum_;
260 0 : uint64_t bigDataSliceSize = taskArg->bigDataSliceSize_;
261 0 : uint64_t smallDataSliceNum = taskArg->smallDataSliceNum_;
262 0 : uint64_t smallDataSliceSize = taskArg->smallDataSliceSize_;
263 0 : uint64_t inputRepeatStride = taskArg->inputRepeatStride_;
264 0 : uint64_t outputRepeatStride = taskArg->outputRepeatStride_;
265 0 : uint64_t normalSliceSize = taskArg->normalSliceSize_;
266 0 : uint64_t lastSliceSize = taskArg->lastSliceSize_;
267 0 : uint64_t repeatNumVar = taskArg->repeatNumVar_;
268 0 : uint64_t isInputOutputEqual = (inputAddr == outputAddr) ? 1 : 0;
269 : std::vector<uint64_t> taskArgs = {
270 : inputAddr, outputAddr, tokenInfo, isInputOutputEqual, inputRepeatStride,
271 : outputRepeatStride, normalSliceSize, lastSliceSize, repeatNumVar,
272 0 : };
273 0 : for (uint64_t i = 0; i < bigDataSliceNum; i++) {
274 0 : taskArgs.push_back(bigDataSliceSize);
275 : }
276 0 : for (uint64_t i = 0; i < smallDataSliceNum; i++) {
277 0 : taskArgs.push_back(smallDataSliceSize);
278 : }
279 :
280 0 : auto localGoSize = CalGoSize(normalSliceSize);
281 0 : taskArgs.push_back(localGoSize[0]);
282 0 : taskArgs.push_back(localGoSize[1]);
283 0 : taskArgs.push_back(localGoSize[2]);
284 0 : taskArgs.push_back(localGoSize[3]);
285 0 : HCCL_INFO(
286 : "[CcuContextReduceMeshMem2Mem1D] TaskArgs: inputAddr[%llu], outputAddr[%llu], inputRepeatStride[%llu], "
287 : "outputRepeatStride[%llu], normalSliceSize[%llu], lastSliceSize[%llu], repeatNumVar[%llu], "
288 : "bigDataSliceNum[%llu], bigDataSliceSize[%llu], smallDataSliceNum[%llu], smallDataSliceSize[%llu], ",
289 : inputAddr, outputAddr, inputRepeatStride, outputRepeatStride, normalSliceSize, lastSliceSize, repeatNumVar,
290 : bigDataSliceNum, bigDataSliceSize, smallDataSliceNum, smallDataSliceSize);
291 0 : return taskArgs;
292 0 : }
293 :
294 : } // namespace Hccl
|