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