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_all_gather_mesh2d_mem2mem.h"
12 : #include "ccu_instruction_all_gather_mesh2d_mem2mem.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int OUTPUT_XN_ID = 1;
17 : constexpr int TOKEN_XN_ID = 2;
18 : constexpr int CKE_IDX_0 = 0; // 约定:尾同步均使用第0个CKE
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 : constexpr int FST_AXIS_ID = 0;
24 : constexpr int SEC_AXIS_ID = 1;
25 :
26 0 : CcuContextAllGatherMeshMem2Mem2D::CcuContextAllGatherMeshMem2Mem2D(
27 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
28 0 : : CcuContextAlgBase(arg, transports, group)
29 : {
30 0 : xAxisSize_ = CreateVariable();
31 0 : yAxisSize_ = CreateVariable();
32 0 : offset_ = CreateVariable();
33 0 : sliceSize_ = CreateVariable();
34 0 : firstOutOffset_ = CreateVariable();
35 0 : firstInOffset_ = CreateVariable();
36 0 : secondInOutBaseOffset_ = CreateVariable();
37 0 : secondInOutStepOffset_ = CreateVariable();
38 0 : goASize_ = CreateGroupOpSize();
39 0 : goBSize_ = CreateGroupOpSize();
40 0 : localAxisSignal_ = CreateMaskSignal();
41 :
42 0 : const CcuCtxArgAllGatherMeshMem2Mem2D* ctxArg = dynamic_cast<const CcuCtxArgAllGatherMeshMem2Mem2D*>(&arg);
43 0 : if (ctxArg == nullptr) {
44 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMeshMem2Mem2D::ctxArg ptr is null"));
45 : }
46 0 : rankId_ = ctxArg->rankId_;
47 0 : dimSize_ = ctxArg->dimSize_;
48 0 : axisId_ = ctxArg->axisId_;
49 : // DIM_SIZE由头文件传入,=2
50 0 : if (dimSize_.size() != DIM_SIZE or axisId_ > 1) {
51 0 : THROW<NullPtrException>(StringFormat(
52 : "CcuContextAllGatherMeshMem2Mem2D::dimSize[%u] or axisId[%u] is invalid", dimSize_.size(), axisId_));
53 : }
54 0 : CHK_PRT_THROW(
55 : dimSize_[0] == 0 || dimSize_[1] == 0,
56 : HCCL_ERROR(
57 : "[CcuContextAllGatherMeshMem2Mem2D] dimSize0[%llu] or dimSize1[%llu] is zero", dimSize_[0], dimSize_[1]),
58 : InvalidParamsException, "dimSize[0] or dimSize[1] is invalid");
59 0 : dimId_.emplace_back(rankId_ % dimSize_[0]);
60 0 : dimId_.emplace_back(rankId_ / dimSize_[0]);
61 0 : localId_ = dimId_[axisId_];
62 0 : localSize_ = dimSize_[axisId_];
63 0 : localAxisSignalName_ = "CcuContextAllGatherMeshMem2Mem2DAxisSync_" + std::to_string(axisId_);
64 0 : anotherAxisSignalName_ = "CcuContextAllGatherMeshMem2Mem2DAxisSync_" + std::to_string(1 - axisId_);
65 0 : HCCL_INFO(
66 : "[CcuContextAllGatherMeshMem2Mem2D] RankId[%u], DimSize: D0[%u]--D1[%u], localId[%u], lcoalSize[%u]", rankId_,
67 : dimSize_[0], dimSize_[1], localId_, localSize_);
68 0 : }
69 :
70 0 : void CcuContextAllGatherMeshMem2Mem2D::InitResources()
71 : {
72 0 : input_.push_back(CreateVariable());
73 :
74 0 : uint32_t transportIdx = 0;
75 0 : for (uint32_t peerId = 0; peerId < localSize_; peerId++) {
76 0 : if (peerId == localId_) {
77 0 : output_.push_back(CreateVariable());
78 0 : token_.push_back(CreateVariable());
79 : } else {
80 0 : HCCL_INFO(
81 : "[CcuContextAllGatherMeshMem2Mem2D] MyRank[%u], peerId[%u], transportIdx[%u]", rankId_, peerId,
82 : transportIdx);
83 0 : CHK_PRT_RET(
84 : transports[transportIdx] == nullptr,
85 : HCCL_ERROR("[CcuContextAllGatherMeshMem2Mem2D] Algorithm transport ptr is null"), );
86 0 : output_.push_back(CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID));
87 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
88 0 : transportIdx++;
89 : }
90 : }
91 :
92 0 : ExportMaskSignal(localAxisSignal_, localAxisSignalName_);
93 0 : anotherAxisSignal_ = ImportMaskSignal(anotherAxisSignalName_);
94 0 : return;
95 : }
96 :
97 : // 需要与GeneArgs返回时的顺序相同
98 0 : void CcuContextAllGatherMeshMem2Mem2D::LoadArgs()
99 : {
100 0 : Load(input_[0]);
101 0 : Load(output_[localId_]);
102 0 : Load(token_[localId_]);
103 0 : Load(xAxisSize_);
104 0 : Load(yAxisSize_);
105 0 : Load(offset_);
106 0 : Load(sliceSize_);
107 0 : Load(firstInOffset_);
108 0 : Load(firstOutOffset_);
109 0 : Load(secondInOutBaseOffset_);
110 0 : Load(secondInOutStepOffset_);
111 0 : Load(goASize_);
112 0 : Load(goBSize_);
113 :
114 0 : return;
115 : }
116 :
117 0 : void CcuContextAllGatherMeshMem2Mem2D::ExchangeInfoAndSync()
118 : {
119 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] ExchangeInfoAndSync run begins.");
120 0 : uint16_t selfBit = 1 << localId_;
121 0 : uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
122 :
123 0 : for (auto t : transports) {
124 0 : if (t == nullptr) {
125 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMeshMem2Mem2D::Algorithm transport ptr is null"));
126 : }
127 0 : WriteVariableWithSignal(*t, output_[localId_], OUTPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
128 0 : WriteVariableWithSignal(*t, token_[localId_], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
129 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] change addr success");
130 : }
131 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit);
132 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit);
133 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] ExchangeInfoAndSync run finished.");
134 0 : return;
135 : }
136 :
137 0 : void CcuContextAllGatherMeshMem2Mem2D::RankSync(uint32_t signalIndex)
138 : {
139 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] RankSync run begins.");
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 : if (t == nullptr) {
145 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMeshMem2Mem2D::Algorithm transport ptr is null"));
146 : }
147 0 : RemotePost(*t, signalIndex, selfBit);
148 : }
149 0 : GroupWait(*transportGroup, signalIndex, allBit);
150 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] RankSync run ends.");
151 0 : return;
152 : }
153 :
154 0 : void CcuContextAllGatherMeshMem2Mem2D::AxisSync(uint32_t signalIndex)
155 : {
156 0 : constexpr uint32_t DIE_NUM = 2;
157 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] AxisSync run begins.");
158 0 : LocalCtxPost(anotherAxisSignal_, 1 << (axisId_ + (signalIndex * DIE_NUM)));
159 0 : LocalWait(localAxisSignal_, 1 << ((1 - axisId_) + (signalIndex * DIE_NUM)));
160 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] AxisSync run ends.");
161 0 : return;
162 : }
163 :
164 0 : void CcuContextAllGatherMeshMem2Mem2D::FirstStep()
165 : {
166 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] firstStep run begins.");
167 0 : CcuRep::Memory src = CreateMemory();
168 0 : src.addr = input_[0];
169 0 : src.addr += firstInOffset_;
170 0 : src.token = token_[localId_];
171 :
172 0 : std::vector<CcuRep::Memory> dst;
173 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
174 0 : dst.push_back(CreateMemory());
175 : }
176 0 : CcuRep::Variable tempSize = CreateVariable();
177 0 : tempSize = (axisId_ == 0) ? xAxisSize_ : yAxisSize_;
178 0 : GroupOpSize tempGoSize = CreateGroupOpSize();
179 0 : tempGoSize = (axisId_ == 0) ? goASize_ : goBSize_;
180 0 : CCU_IF(tempSize != 0)
181 : {
182 0 : u32 transportId = 0;
183 0 : CcuRep::MaskSignal locMask = CreateMaskSignal();
184 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
185 0 : dst[rankIdx].addr = output_[rankIdx];
186 0 : dst[rankIdx].addr += firstOutOffset_;
187 0 : dst[rankIdx].token = token_[rankIdx];
188 0 : if (rankIdx == localId_) {
189 0 : LocalPost(locMask, (1 << rankIdx));
190 : } else {
191 0 : Write(*transports[transportId], dst[rankIdx], src, tempSize, locMask, 1 << rankIdx);
192 0 : transportId++;
193 : }
194 : }
195 0 : GroupCopy(dst[localId_], src, tempGoSize);
196 0 : LocalWait(locMask, (1 << localSize_) - 1);
197 0 : }
198 :
199 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] firstStep run ends.");
200 0 : }
201 :
202 0 : void CcuContextAllGatherMeshMem2Mem2D::SecondStep()
203 : {
204 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] secodeStep run begins.");
205 0 : uint64_t anotherSize = dimSize_[1 - axisId_];
206 :
207 0 : CcuRep::Memory src = CreateMemory();
208 0 : src.addr = output_[localId_];
209 0 : src.token = token_[localId_];
210 :
211 0 : std::vector<CcuRep::Memory> dst;
212 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
213 0 : dst.push_back(CreateMemory());
214 : }
215 :
216 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
217 0 : dst[rankIdx].addr = output_[rankIdx];
218 0 : dst[rankIdx].token = token_[rankIdx];
219 : }
220 :
221 0 : for (uint64_t localIdx = 0; localIdx < anotherSize; localIdx++) { // 分片数等于另外一个轴的大小
222 0 : if (localIdx == 0) {
223 0 : src.addr += secondInOutBaseOffset_;
224 : } else {
225 0 : src.addr += secondInOutStepOffset_;
226 : }
227 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
228 0 : if (localIdx == 0) {
229 0 : dst[rankIdx].addr += secondInOutBaseOffset_;
230 : } else {
231 0 : dst[rankIdx].addr += secondInOutStepOffset_;
232 : }
233 : }
234 0 : auto tempSize = (axisId_ == 0) ? yAxisSize_ : xAxisSize_;
235 0 : CCU_IF(tempSize != 0)
236 : {
237 0 : u32 transportId = 0;
238 0 : CcuRep::MaskSignal locMask = CreateMaskSignal();
239 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
240 0 : if (rankIdx == localId_) {
241 0 : LocalPost(locMask, (1 << rankIdx));
242 : } else {
243 0 : Write(*transports[transportId], dst[rankIdx], src, tempSize, locMask, 1 << rankIdx);
244 0 : transportId++;
245 : }
246 : }
247 0 : LocalWait(locMask, (1 << localSize_) - 1);
248 0 : }
249 0 : }
250 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] secondStep run ends.");
251 0 : }
252 :
253 0 : void CcuContextAllGatherMeshMem2Mem2D::Algorithm()
254 : {
255 : // 初始化寄存器资源 & 加载外部输入参数
256 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] AllgatherMesh2D Algorithm Init Begins.");
257 0 : InitResources();
258 0 : LoadArgs();
259 :
260 : // 第一轮
261 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] Algorithm first step begins.");
262 0 : ExchangeInfoAndSync();
263 0 : FirstStep();
264 0 : RankSync(CKE_IDX_3);
265 0 : AxisSync(FST_AXIS_ID);
266 :
267 : // 第二轮
268 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] Algorithm second step begins.");
269 0 : RankSync(CKE_IDX_4);
270 0 : SecondStep();
271 0 : RankSync(CKE_IDX_0);
272 0 : AxisSync(SEC_AXIS_ID);
273 :
274 0 : HCCL_INFO("[CcuContextAllGatherMeshMem2Mem2D] Algorithm Ends.");
275 0 : return;
276 : }
277 :
278 0 : std::vector<uint64_t> CcuContextAllGatherMeshMem2Mem2D::GeneArgs(const CcuTaskArg& arg)
279 : {
280 0 : const CcuTaskArgAllGatherMeshMem2Mem2D* taskArg = dynamic_cast<const CcuTaskArgAllGatherMeshMem2Mem2D*>(&arg);
281 0 : if (taskArg == nullptr) {
282 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMeshMem2Mem2D::taskArg ptr is null"));
283 : }
284 :
285 : // input&output&buffer地址
286 0 : uint64_t inputAddr = taskArg->inputAddr_;
287 0 : uint64_t outputAddr = taskArg->outputAddr_;
288 0 : uint64_t xAxisSize = taskArg->xAxisSize_;
289 0 : uint64_t yAxisSize = taskArg->yAxisSize_;
290 0 : uint64_t offset = taskArg->offset_;
291 0 : uint64_t sliceSize = xAxisSize + yAxisSize;
292 0 : uint64_t tokenValue = taskArg->token_;
293 :
294 0 : auto goSizeAxis = CalGoSize(xAxisSize);
295 0 : auto goSizeBxis = CalGoSize(yAxisSize);
296 :
297 : uint64_t firstInOffset;
298 0 : uint64_t firstOutOffset = 0;
299 0 : uint64_t secondInOutBaseOffset = 0;
300 0 : uint64_t secondInOutStepOffset = 0;
301 :
302 0 : for (uint32_t i = 0; i < rankId_; i++) {
303 0 : firstOutOffset += offset;
304 : }
305 :
306 0 : if (axisId_ == 0) {
307 0 : firstInOffset = 0;
308 0 : for (uint32_t i = 0; i < dimId_[0]; i++) {
309 0 : secondInOutBaseOffset += offset;
310 : }
311 0 : secondInOutBaseOffset += xAxisSize;
312 0 : for (uint64_t i = 0; i < dimSize_[0]; i++) {
313 0 : secondInOutStepOffset += offset;
314 : }
315 : } else {
316 0 : firstInOffset = xAxisSize;
317 0 : firstOutOffset += xAxisSize;
318 0 : for (uint32_t i = 0; i < dimId_[1]; i++) {
319 0 : for (uint64_t j = 0; j < dimSize_[0]; j++) {
320 0 : secondInOutBaseOffset += offset;
321 : }
322 : }
323 0 : secondInOutStepOffset = offset;
324 : }
325 :
326 0 : HCCL_INFO(
327 : "[CcuContextAllGatherMeshMem2Mem2D][GeneArgs] RankId[%u]--AxisId[%u], inputAddr[%llu], outputAddr[%llu], "
328 : "aSize[%llu], bSize[%llu], offset[%llu], sliceSize[%llu], firstInOffset[%llu], firstOutOffset[%llu], "
329 : "secondInOutBaseOffset[%llu], secondInOutStepOffset[%llu]",
330 : rankId_, axisId_, inputAddr, outputAddr, xAxisSize, yAxisSize, offset, sliceSize, firstInOffset, firstOutOffset,
331 : secondInOutBaseOffset, secondInOutStepOffset);
332 :
333 : return {
334 : inputAddr,
335 : outputAddr,
336 : tokenValue,
337 : xAxisSize,
338 : yAxisSize,
339 : offset,
340 : sliceSize,
341 : firstInOffset,
342 : firstOutOffset,
343 : secondInOutBaseOffset,
344 : secondInOutStepOffset,
345 0 : goSizeAxis[0],
346 0 : goSizeAxis[1],
347 0 : goSizeAxis[2],
348 0 : goSizeAxis[3],
349 0 : goSizeBxis[0],
350 0 : goSizeBxis[1],
351 0 : goSizeBxis[2],
352 0 : goSizeBxis[3]};
353 0 : }
354 :
355 : } // namespace Hccl
|