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 "orion_adapter_rts.h"
12 : #include "ccu_context_all_reduce_mesh1d.h"
13 : #include "ccu_instruction_all_reduce_mesh1d.h"
14 :
15 : namespace Hccl {
16 :
17 : constexpr int INPUT_XN_ID = 0;
18 : constexpr int OUTPUT_XN_ID = 1;
19 : constexpr int TOKEN_XN_ID = 2;
20 : constexpr int CKE_IDX_0 = 0;
21 : constexpr int CKE_IDX_1 = 1;
22 : constexpr int CKE_IDX_2 = 2;
23 : constexpr int CKE_IDX_3 = 3;
24 :
25 0 : CcuContextAllReduceMesh1D::CcuContextAllReduceMesh1D(
26 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
27 0 : : CcuContextAlgBase(arg, transports, group)
28 : {
29 0 : const CcuCtxArgAllReduceMesh1D* ctxArg = dynamic_cast<const CcuCtxArgAllReduceMesh1D*>(&arg);
30 0 : if (ctxArg == nullptr) {
31 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh1D::ctxArg ptr is null"));
32 : }
33 0 : rankId_ = ctxArg->rankId_;
34 0 : rankSize_ = ctxArg->dimSize_[0];
35 0 : dataType_ = ctxArg->op_.dataType;
36 0 : outputDataType_ = ctxArg->op_.outputDataType;
37 0 : if (outputDataType_ == DataType::INVALID) {
38 0 : outputDataType_ = dataType_;
39 0 : HCCL_INFO(
40 : "[CcuContextAllReduceMesh1D] outputDataType is [INVALID], set outputDataType to[%s]",
41 : outputDataType_.Describe().c_str());
42 : }
43 :
44 0 : reduceOp_ = ctxArg->op_.reduceOp;
45 0 : HCCL_DEBUG(
46 : "[CcuContextAllReduceMesh1D] Init, CtxArgs are rankId[%u], rankSize[%u], dataType[%s], "
47 : "outputDataType[%s], reduceOp[%s]",
48 : rankId_, rankSize_, dataType_.Describe().c_str(), outputDataType_.Describe().c_str(),
49 : reduceOp_.Describe().c_str());
50 :
51 : // 判断device类型
52 0 : int32_t devLogicId = HrtGetDevice();
53 0 : if (CcuDeviceManager::GetCcuVersion(devLogicId, ccuVersion_) != HcclResult::HCCL_SUCCESS) {
54 0 : THROW<CcuApiException>("Cannot get ccu version: %s", __func__);
55 : }
56 0 : }
57 :
58 0 : void CcuContextAllReduceMesh1D::RunBroadcast(std::vector<CcuRep::Memory>& dst, CcuRep::Memory& src)
59 : {
60 0 : if (ccuVersion_ == CcuVersion::CCU_V1) {
61 0 : GroupBroadcast(transports, dst, src, groupOpSize_);
62 : } else {
63 0 : THROW<NotSupportException>(StringFormat("CCU version not support, version[%u]", ccuVersion_));
64 : }
65 0 : }
66 :
67 0 : void CcuContextAllReduceMesh1D::RunReduce(CcuRep::Memory& dst, std::vector<CcuRep::Memory>& src)
68 : {
69 0 : if (ccuVersion_ == CcuVersion::CCU_V1) {
70 0 : GroupReduce(transports, dst, src, groupOpSize_, dataType_, outputDataType_, reduceOp_);
71 : } else {
72 0 : THROW<NotSupportException>(StringFormat("CCU version not support, version[%u]", ccuVersion_));
73 : }
74 0 : }
75 :
76 0 : void CcuContextAllReduceMesh1D::Algorithm()
77 : {
78 0 : HCCL_INFO("[CcuContextAllReduceMesh1D] AllReduceMesh1D run");
79 0 : uint16_t selfBit = 1 << rankId_;
80 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
81 :
82 : // 初始化资源
83 0 : uint16_t transportIdx = 0;
84 0 : if (transports.size() == 0) {
85 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh1D transports is empty"));
86 : }
87 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
88 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
89 0 : if (peerId == rankId_) {
90 0 : input_.push_back(CreateVariable());
91 0 : output_.push_back(CreateVariable());
92 0 : token_.push_back(CreateVariable());
93 : } else {
94 0 : HCCL_INFO(
95 : "[CcuContextAllReduceMesh1D] MyRank[%u], PeerId[%llu], TransportId[%u]", rankId_, peerId, transportIdx);
96 0 : CHK_PRT_RET(
97 : transports[transportIdx] == nullptr,
98 : HCCL_ERROR("[CcuContextAllReduceMesh1D] Algorithm transport ptr is null"), );
99 0 : input_.push_back(CreateVariable((*transports[transportIdx]), INPUT_XN_ID));
100 0 : output_.push_back(CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID));
101 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
102 0 : transportIdx++;
103 : }
104 : }
105 0 : offset_ = CreateVariable();
106 0 : groupOpSize_ = CreateGroupOpSize();
107 :
108 0 : Load(input_[rankId_]);
109 0 : Load(output_[rankId_]);
110 0 : Load(token_[rankId_]);
111 0 : Load(offset_);
112 :
113 0 : if (ccuVersion_ == CcuVersion::CCU_V1) {
114 0 : Load(groupOpSize_);
115 : } else {
116 0 : THROW<NotSupportException>(StringFormat("CCU version not support, version[%u]", ccuVersion_));
117 : }
118 :
119 0 : for (auto t : transports) {
120 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit);
121 0 : WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_2, selfBit);
122 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit);
123 : }
124 :
125 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit);
126 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit);
127 0 : GroupWait(*transportGroup, CKE_IDX_3, allBit);
128 :
129 0 : std::vector<CcuRep::Memory> reduceScatterSrc;
130 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
131 0 : reduceScatterSrc.push_back(CreateMemory());
132 : }
133 0 : CcuRep::Memory reduceScatterDst = CreateMemory();
134 : // DST
135 0 : reduceScatterDst.addr = output_[rankId_];
136 0 : reduceScatterDst.addr += offset_;
137 0 : reduceScatterDst.token = token_[rankId_];
138 :
139 0 : uint32_t dstId = 0;
140 0 : uint32_t curId = 0;
141 : // SRC
142 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
143 0 : if (rankIdx != rankId_) {
144 0 : curId = dstId;
145 0 : dstId++;
146 : } else {
147 0 : curId = rankSize_ - 1;
148 : }
149 0 : reduceScatterSrc[curId].addr = input_[rankIdx];
150 0 : reduceScatterSrc[curId].addr += offset_;
151 0 : reduceScatterSrc[curId].token = token_[rankIdx];
152 : }
153 0 : RunReduce(reduceScatterDst, reduceScatterSrc);
154 :
155 0 : CcuRep::Memory allGatherSrc = CreateMemory();
156 0 : std::vector<CcuRep::Memory> allGatherDst;
157 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
158 0 : allGatherDst.push_back(CreateMemory());
159 : }
160 : // allGather 的输入就是 reduceScatter 的输出
161 0 : allGatherSrc.addr = output_[rankId_];
162 0 : allGatherSrc.addr += offset_;
163 0 : allGatherSrc.token = token_[rankId_];
164 :
165 0 : dstId = 0;
166 0 : curId = 0;
167 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
168 0 : if (rankIdx != rankId_) {
169 0 : curId = dstId;
170 0 : dstId++;
171 : } else {
172 0 : curId = rankSize_ - 1;
173 : }
174 0 : allGatherDst[curId].addr = output_[rankIdx];
175 0 : allGatherDst[curId].addr += offset_;
176 0 : allGatherDst[curId].token = token_[rankIdx];
177 : }
178 0 : RunBroadcast(allGatherDst, allGatherSrc);
179 :
180 0 : for (auto t : transports) {
181 0 : RemotePost(*t, CKE_IDX_0, selfBit);
182 : }
183 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
184 0 : HCCL_INFO("[CcuContextAllReduceMesh1D] AllReduceMesh1D end");
185 0 : return;
186 0 : }
187 :
188 0 : std::vector<uint64_t> CcuContextAllReduceMesh1D::GeneArgs(const CcuTaskArg& arg)
189 : {
190 0 : const CcuTaskArgAllReduceMesh1D* taskArg = dynamic_cast<const CcuTaskArgAllReduceMesh1D*>(&arg);
191 0 : if (taskArg == nullptr) {
192 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh1D::taskArg ptr is null"));
193 : }
194 0 : uint64_t inputAddr = taskArg->inputAddr_;
195 0 : uint64_t outputAddr = taskArg->outputAddr_;
196 0 : uint64_t tokenInfo = taskArg->token_;
197 0 : uint64_t sliceSize = taskArg->sliceSize_;
198 0 : uint64_t offset = taskArg->offset_;
199 :
200 0 : if (ccuVersion_ == CcuVersion::CCU_V1) {
201 0 : auto goSize = CalGoSize(sliceSize);
202 :
203 0 : HCCL_INFO(
204 : "[CcuContextAllReduceMesh1D] GeneArgs, taskArg are inputAddr[%llu], outputAddr[%llu], "
205 : "offset[%llu], sliceSize[%llu]",
206 : inputAddr, outputAddr, offset, sliceSize);
207 0 : return {inputAddr, outputAddr, tokenInfo, offset, goSize[0], goSize[1], goSize[2], goSize[3]};
208 0 : } else {
209 0 : THROW<NotSupportException>(StringFormat("CCU version not support, version[%u]", ccuVersion_));
210 : }
211 :
212 : return {};
213 : }
214 : } // namespace Hccl
|