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