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_mesh1d_mem2mem_with_stride.h"
12 : #include "ccu_instruction_all_gather_mesh1d_mem2mem_with_stride.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;
19 : constexpr int CKE_IDX_1 = 1;
20 : constexpr int CKE_IDX_2 = 2;
21 : constexpr int CKE_IDX_3 = 3;
22 :
23 : using CurrentCtxArg = CcuCtxArgAllGatherMesh1DMem2MemWithStride;
24 : using CurrentTaskArg = CcuTaskArgAllGatherMesh1DMem2MemWithStride;
25 :
26 0 : CcuContextAllGatherMesh1DMem2MemWithStride::CcuContextAllGatherMesh1DMem2MemWithStride(
27 0 : const CcuCtxArg &arg, const std::vector<CcuTransport *> &transports, const CcuTransportGroup &group)
28 0 : : CcuContextAlgBase(arg, transports, group)
29 : {
30 0 : HCCL_DEBUG("[CcuContextAllGatherMesh1DMem2MemWithStride] Enter Constructor.");
31 0 : const CurrentCtxArg *ctxArg = dynamic_cast<const CurrentCtxArg *>(&arg);
32 0 : if (ctxArg == nullptr) {
33 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMesh1DMem2MemWithStride::ctxArg ptr is null"));
34 : }
35 0 : rankId_ = ctxArg->rankId_;
36 0 : if (ctxArg->dimSize_.size() > 0) {
37 0 : rankSize_ = ctxArg->dimSize_[0];
38 : }
39 0 : HCCL_INFO("[CcuContextAllGatherMesh1DMem2MemWithStride] CtxArg: rankId[%u] rankSize[%u].", rankId_,
40 : rankSize_);
41 0 : }
42 :
43 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::InitResource()
44 : {
45 0 : localInput_ = CreateVariable();
46 0 : uint16_t transportIdx = 0;
47 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
48 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
49 0 : if (peerId == rankId_) {
50 0 : output_.push_back(CreateVariable());
51 0 : token_.push_back(CreateVariable());
52 : } else {
53 0 : HCCL_DEBUG("[CcuContextAllGatherMesh1DMem2MemWithStride] MyRank[%u], PeerId[%u], TransportId[%u]", rankId_,
54 : peerId, transportIdx);
55 0 : CHK_PRT_THROW(
56 : transports.at(transportIdx) == nullptr,
57 : HCCL_ERROR("[CcuContextAllGatherMesh1DMem2MemWithStride][InitResource] transports[%u] is nullptr",
58 : transportIdx),
59 : NullPtrException, "transport is null");
60 0 : output_.push_back(
61 0 : CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID)); // 获取transport中id=1的Var来传递output
62 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
63 0 : transportIdx++;
64 : }
65 : }
66 :
67 0 : currentRankSliceInputOffset_ = CreateVariable();
68 0 : currentRankSliceOutputOffset_ = CreateVariable();
69 0 : inputRepeatStride_ = CreateVariable();
70 0 : outputRepeatStride_ = CreateVariable();
71 0 : tmpRepeatNum_ = CreateVariable();
72 0 : normalSliceSize_ = CreateVariable();
73 0 : lastSliceSize_ = CreateVariable();
74 0 : repeatTimeflag_ = CreateVariable();
75 0 : repeatTimeflag_ = 0;
76 0 : constVar1_ = CreateVariable();
77 0 : constVar1_ = 1;
78 :
79 0 : selfBit_ = 1 << rankId_;
80 0 : allBit_ = ((1 << rankSize_) - 1) & (~(1 << rankId_));
81 :
82 0 : localMem_ = CreateMemory();
83 0 : reomteMem_.reserve(rankSize_);
84 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
85 0 : reomteMem_.push_back(CreateMemory());
86 : }
87 :
88 0 : localSignal_ = CreateMaskSignal();
89 0 : return;
90 : }
91 :
92 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::LoadArgs()
93 : {
94 0 : Load(localInput_);
95 0 : Load(output_[rankId_]);
96 0 : Load(token_[rankId_]);
97 0 : Load(currentRankSliceInputOffset_);
98 0 : Load(currentRankSliceOutputOffset_);
99 0 : Load(tmpRepeatNum_);
100 0 : Load(inputRepeatStride_);
101 0 : Load(outputRepeatStride_);
102 0 : Load(normalSliceSize_);
103 0 : Load(lastSliceSize_);
104 0 : Load(isInputOutputEqual_);
105 0 : return;
106 : }
107 :
108 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::PreSync()
109 : {
110 0 : for (auto t : transports) {
111 0 : WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_1, selfBit_); // index = 1,传递output信息
112 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit_); // index = 2,传递token信息
113 : }
114 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit_); // index = 1,传递output信息
115 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit_); // index = 2,传递token信息
116 0 : return;
117 : }
118 :
119 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::PostSync()
120 : {
121 0 : for (auto t : transports) {
122 0 : RemotePost(*t, CKE_IDX_0, selfBit_);
123 : }
124 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit_);
125 0 : }
126 :
127 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::DoRepeatAllGather()
128 : {
129 0 : CcuRep::Memory &src = localMem_;
130 0 : std::vector<CcuRep::Memory> &dst = reomteMem_;
131 : // 初始化 src 和 dst
132 0 : src.addr = localInput_;
133 0 : src.addr += currentRankSliceInputOffset_;
134 0 : src.token = token_[rankId_];
135 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
136 0 : dst[rankIdx].addr = output_[rankIdx];
137 0 : dst[rankIdx].addr += currentRankSliceOutputOffset_;
138 0 : dst[rankIdx].token = token_[rankIdx];
139 : }
140 0 : CCU_WHILE(tmpRepeatNum_ != UINT64_MAX)
141 : {
142 0 : tmpRepeatNum_ += constVar1_;
143 0 : CCU_IF(repeatTimeflag_ != 0)
144 : {
145 0 : src.addr += inputRepeatStride_;
146 0 : for (auto &d : dst) {
147 0 : d.addr += outputRepeatStride_;
148 : }
149 0 : }
150 0 : CCU_IF(normalSliceSize_ != 0)
151 : {
152 0 : DoAllGather(src, dst, normalSliceSize_);
153 0 : }
154 0 : repeatTimeflag_ = 1;
155 0 : }
156 0 : }
157 :
158 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::DoAllGather(const CcuRep::Memory &src,
159 : const std::vector<CcuRep::Memory> &dst,
160 : const CcuRep::Variable &sliceSize)
161 : {
162 0 : uint32_t transportId = 0;
163 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
164 0 : if (rankIdx == rankId_) {
165 0 : CCU_IF(isInputOutputEqual_ != 0)
166 : {
167 0 : LocalPost(localSignal_, 1 << rankIdx);
168 0 : }
169 0 : CCU_IF(isInputOutputEqual_ == 0)
170 : {
171 0 : LocalCopy(dst[rankIdx], src, sliceSize, localSignal_, 1 << rankIdx);
172 0 : }
173 : } else {
174 0 : CCU_IF(normalSliceSize_ != 0)
175 : {
176 0 : Write(*transports[transportId], dst[rankIdx], src, sliceSize, localSignal_, 1 << rankIdx);
177 0 : }
178 0 : transportId++;
179 : }
180 : }
181 0 : LocalWait(localSignal_, (1 << rankSize_) - 1);
182 0 : }
183 :
184 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::Algorithm()
185 : {
186 0 : HCCL_INFO("[CcuContextAllGatherMesh1DMem2MemWithStride] AllgatherMesh1D run.");
187 0 : InitResource();
188 0 : LoadArgs();
189 0 : PreSync();
190 0 : DoRepeatAllGather();
191 0 : PostSync();
192 0 : HCCL_INFO("[CcuContextAllGatherMesh1DMem2MemWithStride] AllgatherMesh1D end.");
193 0 : return;
194 : }
195 :
196 0 : std::vector<uint64_t> CcuContextAllGatherMesh1DMem2MemWithStride::GeneArgs(const CcuTaskArg &arg)
197 : {
198 0 : const CurrentTaskArg *taskArg = dynamic_cast<const CurrentTaskArg *>(&arg);
199 0 : uint64_t inputAddr = taskArg->inputAddr_;
200 0 : uint64_t outputAddr = taskArg->outputAddr_;
201 0 : uint64_t tokenInfo = taskArg->token_;
202 :
203 0 : uint64_t currentRankSliceInputOffset = taskArg->inputSliceStride_ * rankId_;
204 0 : uint64_t currentRankSliceOutputOffset = taskArg->outputSliceStride_ * rankId_;
205 0 : uint64_t tmpRepeatNum = UINT64_MAX - taskArg->repeatNum_;
206 0 : uint64_t inputRepeatStride = taskArg->inputRepeatStride_;
207 0 : uint64_t outputRepeatStride = taskArg->outputRepeatStride_;
208 0 : uint64_t normalSliceSize = taskArg->normalSliceSize_;
209 0 : uint64_t lastSliceSize = taskArg->lastSliceSize_;
210 0 : uint64_t isInputOutputEqual = taskArg->isInputOutputEqual_;
211 :
212 : std::vector<uint64_t> taskArgs = {inputAddr,
213 : outputAddr,
214 : tokenInfo,
215 : currentRankSliceInputOffset,
216 : currentRankSliceOutputOffset,
217 : tmpRepeatNum,
218 : inputRepeatStride,
219 : outputRepeatStride,
220 : normalSliceSize,
221 : lastSliceSize,
222 0 : isInputOutputEqual};
223 :
224 0 : HCCL_INFO(
225 : "[CcuContextAllGatherMesh1DMem2MemWithStride] TaskArgs: inputAddr[%llu], outputAddr[%llu], "
226 : "currentRankSliceInputOffset[%llu], currentRankSliceOutputOffset[%llu], "
227 : "repeatNum[%llu],inputRepeatStride[%llu], outputRepeatStride[%llu], normalSliceSize[%llu], lastSliceSize[%llu]",
228 : inputAddr, outputAddr, currentRankSliceInputOffset, currentRankSliceOutputOffset, tmpRepeatNum,
229 : inputRepeatStride, outputRepeatStride, normalSliceSize, lastSliceSize);
230 0 : return taskArgs;
231 0 : }
232 : } // namespace Hccl
|