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_mesh2d.h"
12 : #include "ccu_instruction_reduce_mesh2d.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int INPUT_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 : constexpr int CKE_IDX_4 = 4;
23 :
24 0 : CcuContextReduceMesh2D::CcuContextReduceMesh2D(
25 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
26 0 : : CcuContextAlgBase(arg, transports, group)
27 : {
28 0 : const CcuCtxArgReduceMesh2D* ctxArg = dynamic_cast<const CcuCtxArgReduceMesh2D*>(&arg);
29 0 : if (ctxArg == nullptr) {
30 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceMesh2D::ctxArg ptr is null"));
31 : }
32 0 : rankId_ = ctxArg->rankId_;
33 0 : dimSize_ = ctxArg->dimSize_;
34 0 : axisId_ = ctxArg->axisId_; // 要进行操作的是 行或列
35 :
36 0 : if (dimSize_.size() != 2 || axisId_ > 1 || dimSize_[0] == 0) { // 2D 拓扑校验
37 0 : THROW<NullPtrException>(StringFormat(
38 : "[CcuContextReduceMesh2D] dimSize[%u] or axisId[%u] or dimSize[0] [%u] is invalid", dimSize_.size(),
39 0 : axisId_, dimSize_[0]));
40 : }
41 0 : dimId_.emplace_back(rankId_ % dimSize_[0]);
42 0 : dimId_.emplace_back(rankId_ / dimSize_[0]);
43 0 : localId_ = dimId_[axisId_]; // 本rank所在的行/列
44 0 : localSize_ = dimSize_[axisId_]; // 本rank所在的行/列的总数
45 :
46 0 : HCCL_INFO(
47 : "[CcuContextReduceMesh2D] RankId[%u], DimSize0[%llu], DimSize1[%llu], localId[%llu], lcoalSize[%llu]", rankId_,
48 : dimSize_[0], dimSize_[1], localId_, localSize_);
49 :
50 0 : dataType_ = ctxArg->op_.dataType;
51 0 : outputDataType_ = ctxArg->op_.outputDataType;
52 0 : if (outputDataType_ == DataType::INVALID) {
53 0 : outputDataType_ = dataType_;
54 0 : HCCL_INFO(
55 : "[CcuContextReduceMesh2D] outputDataType is [INVALID], set outputDataType to[%s]",
56 : outputDataType_.Describe().c_str());
57 : }
58 0 : reduceOp_ = ctxArg->op_.reduceOp;
59 0 : rootId_ = ctxArg->rootId_;
60 0 : rootDimId_.emplace_back(rootId_ % dimSize_[0]); // root的x
61 0 : rootDimId_.emplace_back(rootId_ / dimSize_[0]); // root的y
62 0 : rootLocalId_ = rootDimId_[axisId_]; // 未用
63 0 : HCCL_INFO(
64 : "[CcuContextReduceMesh2D] init end, ctxArg->dimSize size[%zu] localSize_[%u]", ctxArg->dimSize_.size(),
65 : localSize_);
66 :
67 0 : localAxisSignalName_ = "CcuContextReduceMesh2DAxisSync_" + std::to_string(axisId_);
68 0 : anotherAxisSignalName_ = "CcuContextReduceMesh2DAxisSync_" + std::to_string(1 - axisId_);
69 0 : }
70 :
71 0 : void CcuContextReduceMesh2D::InitResources()
72 : {
73 0 : localAxisSignal_ = CreateMaskSignal();
74 0 : anotherAxisSignal_ = CreateMaskSignal();
75 0 : ExportMaskSignal(localAxisSignal_, localAxisSignalName_);
76 0 : anotherAxisSignal_ = ImportMaskSignal(anotherAxisSignalName_);
77 0 : offset_ = CreateVariable();
78 :
79 0 : output_.push_back(CreateVariable());
80 0 : if (transports.size() == 0) {
81 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceMesh2D transports is empty"));
82 : }
83 0 : uint32_t transportIdx = 0;
84 0 : for (uint32_t peerId = 0; peerId < localSize_; peerId++) {
85 0 : if (peerId == localId_) {
86 0 : input_.push_back(CreateVariable());
87 0 : token_.push_back(CreateVariable());
88 : } else {
89 0 : HCCL_INFO(
90 : "[CcuContextReduceMesh2D] MyRank[%u], PeerId[%u], TransportId[%u]", localId_, peerId, transportIdx);
91 0 : CHK_PRT_RET(
92 : transports[transportIdx] == nullptr,
93 : HCCL_ERROR("[CcuContextReduceMesh2D] Algorithm transport ptr is null"), );
94 0 : input_.push_back(
95 0 : CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
96 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
97 0 : transportIdx++;
98 : }
99 : }
100 :
101 0 : xAxisGroupOpSize_ = CreateGroupOpSize();
102 0 : yAxisGroupOpSize_ = CreateGroupOpSize();
103 0 : HCCL_INFO("[CcuContextReduceMesh2D] InitResources finished");
104 : }
105 :
106 0 : void CcuContextReduceMesh2D::PreSync() // 前同步
107 : {
108 0 : uint16_t selfBit = 1 << localId_;
109 0 : uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
110 :
111 0 : for (auto t : transports) {
112 0 : WriteVariableWithSignal(*t, input_[localId_], INPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
113 0 : WriteVariableWithSignal(*t, token_[localId_], 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 : HCCL_INFO("[CcuContextReduceMesh2D] PreSync run finished");
118 0 : }
119 :
120 0 : void CcuContextReduceMesh2D::PostSync(uint32_t signalIndex)
121 : {
122 0 : uint16_t selfBit = 1 << localId_;
123 0 : uint16_t allBit = ((1 << localSize_) - 1) & (~(1 << localId_));
124 :
125 0 : for (auto t : transports) {
126 0 : RemotePost(*t, signalIndex, selfBit);
127 : }
128 0 : GroupWait(*transportGroup, signalIndex, allBit);
129 0 : HCCL_INFO("[CcuContextReduceMesh2D] PostSync run finished");
130 0 : }
131 :
132 0 : void CcuContextReduceMesh2D::AxisSync(uint32_t signalIndex) // 轴间同步
133 : {
134 0 : const uint32_t DIE_NUM = 2;
135 0 : LocalCtxPost(anotherAxisSignal_, 1 << (axisId_ + signalIndex * DIE_NUM));
136 0 : LocalWait(localAxisSignal_, 1 << (1 - axisId_ + signalIndex * DIE_NUM));
137 0 : HCCL_INFO("[CcuContextReduceMesh2D] AxisSync run finished");
138 0 : return;
139 : }
140 :
141 0 : void CcuContextReduceMesh2D::LoadArgs()
142 : {
143 0 : Load(input_[localId_]);
144 0 : Load(output_[0]);
145 0 : Load(token_[localId_]);
146 0 : Load(offset_);
147 0 : Load(xAxisGroupOpSize_);
148 0 : Load(yAxisGroupOpSize_);
149 0 : HCCL_INFO("[CcuContextReduceMesh2D] LoadArgs run finished");
150 0 : }
151 :
152 0 : void CcuContextReduceMesh2D::Step1Reduce()
153 : {
154 : // 只有与 root 同列的 rank 的 die0 进行第一步 reduce
155 0 : if (dimId_[0] != rootDimId_[0] || axisId_ != 0) {
156 0 : HCCL_INFO("[CcuContextReduceMesh2D] RankId [%u], axisId [%u], skip Step1Reduce", rankId_, axisId_);
157 0 : return;
158 : }
159 0 : HCCL_INFO("[CcuContextReduceMesh2D] RankId [%u], axisId [%u], run Step1Reduce", rankId_, axisId_);
160 :
161 0 : CcuRep::Memory dst = CreateMemory();
162 0 : dst.addr = input_[localId_]; // 第一步reduce都是从input reduce到input
163 0 : dst.token = token_[localId_];
164 :
165 0 : std::vector<CcuRep::Memory> src;
166 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
167 0 : src.push_back(CreateMemory());
168 : }
169 0 : uint32_t curId = 0;
170 0 : uint32_t dstId = 0;
171 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
172 0 : if (rankIdx != localId_) {
173 0 : curId = dstId;
174 0 : dstId++;
175 : } else {
176 0 : curId = localSize_ - 1;
177 : }
178 0 : src[curId].addr = input_[rankIdx];
179 0 : src[curId].token = token_[rankIdx];
180 : }
181 :
182 0 : GroupReduce(transports, dst, src, xAxisGroupOpSize_, dataType_, outputDataType_, reduceOp_);
183 0 : }
184 :
185 0 : void CcuContextReduceMesh2D::Step2ReduceForRoot()
186 : {
187 : // 只有与 root 的 die0 进行第一步 reduce
188 0 : if (rankId_ != rootId_ || axisId_ != 1) {
189 0 : HCCL_INFO("[CcuContextReduceMesh2D] RankId [%u], axisId [%u], skip Step2Reduce", rankId_, axisId_);
190 0 : return;
191 : }
192 0 : HCCL_INFO("[CcuContextReduceMesh2D] RankId [%u], axisId [%u], run Step2Reduce", rankId_, axisId_);
193 :
194 0 : std::vector<CcuRep::Memory> src;
195 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
196 0 : src.push_back(CreateMemory());
197 : }
198 0 : CcuRep::Memory dst = CreateMemory();
199 0 : dst.addr = output_[0]; // 第二步reduce是从input reduce到root的output
200 0 : dst.token = token_[localId_];
201 0 : uint32_t dstId = 0;
202 0 : uint32_t curId = 0;
203 0 : for (uint32_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
204 0 : if (rankIdx != localId_) {
205 0 : curId = dstId;
206 0 : dstId++;
207 : } else {
208 0 : curId = localSize_ - 1; // 最后一个位置放root的input
209 : }
210 0 : src[curId].addr = input_[rankIdx];
211 0 : src[curId].token = token_[rankIdx];
212 : }
213 0 : GroupReduce(transports, dst, src, xAxisGroupOpSize_, dataType_, outputDataType_, reduceOp_);
214 0 : }
215 :
216 0 : void CcuContextReduceMesh2D::Algorithm()
217 : {
218 0 : HCCL_INFO("[CcuContextReduceMesh2D] ReduceMesh2D run");
219 0 : InitResources();
220 0 : LoadArgs();
221 0 : HCCL_INFO("[CcuContextReduceMesh2D] Algorithm first step begins.");
222 0 : PreSync(); // 前同步
223 0 : Step1Reduce();
224 0 : AxisSync(0);
225 0 : PostSync(CKE_IDX_3);
226 0 : AxisSync(1);
227 0 : Step2ReduceForRoot();
228 0 : AxisSync(0);
229 0 : PostSync(CKE_IDX_0);
230 0 : AxisSync(1);
231 0 : }
232 :
233 0 : std::vector<uint64_t> CcuContextReduceMesh2D::GeneArgs(const CcuTaskArg& arg)
234 : {
235 0 : const CcuTaskArgReduceMesh2D* taskArg = dynamic_cast<const CcuTaskArgReduceMesh2D*>(&arg);
236 0 : if (taskArg == nullptr) {
237 0 : THROW<NullPtrException>(StringFormat("CcuTaskArgReduceMesh2D::taskArg ptr is null"));
238 : }
239 0 : uint64_t inputAddr = taskArg->inputAddr_;
240 0 : uint64_t outputAddr = taskArg->outputAddr_;
241 0 : uint64_t tokenInfo = taskArg->token_;
242 0 : uint64_t offset = taskArg->offset_;
243 0 : uint64_t xAxisSize = taskArg->xAxisSize_;
244 0 : uint64_t yAxisSize = taskArg->yAxisSize_;
245 0 : auto xAxisGoSize = CalGoSize(xAxisSize);
246 0 : auto yAxisGoSize = CalGoSize(yAxisSize);
247 :
248 0 : HCCL_INFO(
249 : "[CcuContextReduceMesh2D] ReduceMesh2D inputAddr [%llu] outputAddr [%llu] offset [%llu]"
250 : "xAxisSize [%llu] yAxisSize [%llu]",
251 : inputAddr, outputAddr, offset, xAxisSize, yAxisSize);
252 :
253 0 : return {inputAddr, outputAddr, tokenInfo, offset, xAxisGoSize[0], xAxisGoSize[1],
254 0 : xAxisGoSize[2], xAxisGoSize[3], yAxisGoSize[0], yAxisGoSize[1], yAxisGoSize[2], yAxisGoSize[3]};
255 0 : }
256 : } // namespace Hccl
|