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 : isInputOutputEqual_ = CreateVariable();
75 0 : repeatTimeflag_ = CreateVariable();
76 0 : repeatTimeflag_ = 0;
77 0 : constVar1_ = CreateVariable();
78 0 : constVar1_ = 1;
79 :
80 0 : selfBit_ = 1 << rankId_;
81 0 : allBit_ = ((1 << rankSize_) - 1) & (~(1 << rankId_));
82 :
83 0 : localMem_ = CreateMemory();
84 0 : reomteMem_.reserve(rankSize_);
85 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
86 0 : reomteMem_.push_back(CreateMemory());
87 : }
88 :
89 0 : localSignal_ = CreateMaskSignal();
90 0 : return;
91 : }
92 :
93 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::LoadArgs()
94 : {
95 0 : Load(localInput_);
96 0 : Load(output_[rankId_]);
97 0 : Load(token_[rankId_]);
98 0 : Load(currentRankSliceInputOffset_);
99 0 : Load(currentRankSliceOutputOffset_);
100 0 : Load(tmpRepeatNum_);
101 0 : Load(inputRepeatStride_);
102 0 : Load(outputRepeatStride_);
103 0 : Load(normalSliceSize_);
104 0 : Load(lastSliceSize_);
105 0 : Load(isInputOutputEqual_);
106 0 : return;
107 : }
108 :
109 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::PreSync()
110 : {
111 0 : for (auto t : transports) {
112 0 : WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_1, selfBit_); // index = 1,传递output信息
113 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit_); // index = 2,传递token信息
114 : }
115 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit_); // index = 1,传递output信息
116 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit_); // index = 2,传递token信息
117 0 : return;
118 : }
119 :
120 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::PostSync()
121 : {
122 0 : for (auto t : transports) {
123 0 : RemotePost(*t, CKE_IDX_0, selfBit_);
124 : }
125 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit_);
126 0 : }
127 :
128 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::DoRepeatAllGather()
129 : {
130 0 : CcuRep::Memory &src = localMem_;
131 0 : std::vector<CcuRep::Memory> &dst = reomteMem_;
132 : // 初始化 src 和 dst
133 0 : src.addr = localInput_;
134 0 : src.addr += currentRankSliceInputOffset_;
135 0 : src.token = token_[rankId_];
136 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
137 0 : dst[rankIdx].addr = output_[rankIdx];
138 0 : dst[rankIdx].addr += currentRankSliceOutputOffset_;
139 0 : dst[rankIdx].token = token_[rankIdx];
140 : }
141 0 : CCU_WHILE(tmpRepeatNum_ != UINT64_MAX)
142 : {
143 0 : tmpRepeatNum_ += constVar1_;
144 0 : CCU_IF(repeatTimeflag_ != 0)
145 : {
146 0 : src.addr += inputRepeatStride_;
147 0 : for (auto &d : dst) {
148 0 : d.addr += outputRepeatStride_;
149 : }
150 0 : }
151 0 : CCU_IF(normalSliceSize_ != 0)
152 : {
153 0 : DoAllGather(src, dst, normalSliceSize_);
154 0 : }
155 0 : repeatTimeflag_ = 1;
156 0 : }
157 0 : }
158 :
159 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::DoAllGather(const CcuRep::Memory &src,
160 : const std::vector<CcuRep::Memory> &dst,
161 : const CcuRep::Variable &sliceSize)
162 : {
163 0 : uint32_t transportId = 0;
164 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
165 0 : if (rankIdx == rankId_) {
166 0 : CCU_IF(isInputOutputEqual_ != 0)
167 : {
168 0 : LocalPost(localSignal_, 1 << rankIdx);
169 0 : }
170 0 : CCU_IF(isInputOutputEqual_ == 0)
171 : {
172 0 : LocalCopy(dst[rankIdx], src, sliceSize, localSignal_, 1 << rankIdx);
173 0 : }
174 : } else {
175 0 : CCU_IF(normalSliceSize_ != 0)
176 : {
177 0 : Write(*transports[transportId], dst[rankIdx], src, sliceSize, localSignal_, 1 << rankIdx);
178 0 : }
179 0 : transportId++;
180 : }
181 : }
182 0 : LocalWait(localSignal_, (1 << rankSize_) - 1);
183 0 : }
184 :
185 0 : void CcuContextAllGatherMesh1DMem2MemWithStride::Algorithm()
186 : {
187 0 : HCCL_INFO("[CcuContextAllGatherMesh1DMem2MemWithStride] AllgatherMesh1D run.");
188 0 : InitResource();
189 0 : LoadArgs();
190 0 : PreSync();
191 0 : DoRepeatAllGather();
192 0 : PostSync();
193 0 : HCCL_INFO("[CcuContextAllGatherMesh1DMem2MemWithStride] AllgatherMesh1D end.");
194 0 : return;
195 : }
196 :
197 0 : std::vector<uint64_t> CcuContextAllGatherMesh1DMem2MemWithStride::GeneArgs(const CcuTaskArg &arg)
198 : {
199 0 : const CurrentTaskArg *taskArg = dynamic_cast<const CurrentTaskArg *>(&arg);
200 0 : uint64_t inputAddr = taskArg->inputAddr_;
201 0 : uint64_t outputAddr = taskArg->outputAddr_;
202 0 : uint64_t tokenInfo = taskArg->token_;
203 :
204 0 : uint64_t currentRankSliceInputOffset = taskArg->inputSliceStride_ * rankId_;
205 0 : uint64_t currentRankSliceOutputOffset = taskArg->outputSliceStride_ * rankId_;
206 0 : uint64_t tmpRepeatNum = UINT64_MAX - taskArg->repeatNum_;
207 0 : uint64_t inputRepeatStride = taskArg->inputRepeatStride_;
208 0 : uint64_t outputRepeatStride = taskArg->outputRepeatStride_;
209 0 : uint64_t normalSliceSize = taskArg->normalSliceSize_;
210 0 : uint64_t lastSliceSize = taskArg->lastSliceSize_;
211 0 : uint64_t isInputOutputEqual = taskArg->isInputOutputEqual_;
212 :
213 : std::vector<uint64_t> taskArgs = {inputAddr,
214 : outputAddr,
215 : tokenInfo,
216 : currentRankSliceInputOffset,
217 : currentRankSliceOutputOffset,
218 : tmpRepeatNum,
219 : inputRepeatStride,
220 : outputRepeatStride,
221 : normalSliceSize,
222 : lastSliceSize,
223 0 : isInputOutputEqual};
224 :
225 0 : HCCL_INFO(
226 : "[CcuContextAllGatherMesh1DMem2MemWithStride] TaskArgs: inputAddr[%llu], outputAddr[%llu], "
227 : "currentRankSliceInputOffset[%llu], currentRankSliceOutputOffset[%llu], "
228 : "repeatNum[%llu],inputRepeatStride[%llu], outputRepeatStride[%llu], normalSliceSize[%llu], lastSliceSize[%llu]",
229 : inputAddr, outputAddr, currentRankSliceInputOffset, currentRankSliceOutputOffset, tmpRepeatNum,
230 : inputRepeatStride, outputRepeatStride, normalSliceSize, lastSliceSize);
231 0 : return taskArgs;
232 0 : }
233 : } // namespace Hccl
|