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