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_reduce_mesh1d_one_shot.h"
12 : #include "ccu_instruction_all_reduce_mesh1d_one_shot.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int INPUT_XN_ID = 0;
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 :
22 0 : CcuContextAllReduceMesh1DOneShot::CcuContextAllReduceMesh1DOneShot(
23 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
24 0 : : CcuContextAlgBase(arg, transports, group)
25 : {
26 0 : const CcuCtxArgAllReduceMesh1DOneShot* ctxArg = dynamic_cast<const CcuCtxArgAllReduceMesh1DOneShot*>(&arg);
27 0 : if (ctxArg == nullptr) {
28 0 : THROW<NullPtrException>(StringFormat("CcuCtxArgAllReduceMesh1DOneShot::ctxArg ptr is null"));
29 : }
30 0 : notifySignal_ = ctxArg->notifySignal_;
31 0 : rankId_ = ctxArg->rankId_;
32 0 : rankSize_ = ctxArg->dimSize_[0];
33 0 : dataType_ = ctxArg->op_.dataType;
34 0 : outputDataType_ = ctxArg->op_.outputDataType;
35 0 : reduceOp_ = ctxArg->op_.reduceOp;
36 0 : if (outputDataType_ == DataType::INVALID) {
37 0 : outputDataType_ = dataType_;
38 0 : HCCL_INFO(
39 : "[CcuContextAllReduceMesh1DOneShot] outputDataType is [INVALID], set outputDataType to[%s]",
40 : outputDataType_.Describe().c_str());
41 : }
42 0 : HCCL_INFO(
43 : "[CcuContextAllReduceMesh1DOneShot] Init, CtxArgs are notifySignal_[%s], rankId[%u], rankSize[%llu], "
44 : "dataType[%s], "
45 : "outputDataType[%s], reduceOp[%s]",
46 : notifySignal_.c_str(), rankId_, rankSize_, dataType_.Describe().c_str(), outputDataType_.Describe().c_str(),
47 : reduceOp_.Describe().c_str());
48 0 : }
49 :
50 0 : void CcuContextAllReduceMesh1DOneShot::Algorithm()
51 : {
52 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] AllReduceMesh1DOneShot start");
53 0 : InitResource();
54 0 : LoadArgs(); // 加载 taskArg 参数
55 0 : Presync(); // 跨卡前同步,交换参数信息
56 :
57 0 : DoGroupReduce();
58 :
59 0 : Postsync(); // 所有搬运任务结束后,跨卡后同步
60 :
61 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] AllReduceMesh1DOneShot end");
62 0 : return;
63 : }
64 :
65 0 : void CcuContextAllReduceMesh1DOneShot::InitResource()
66 : {
67 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] InitResource start");
68 : // 初始化资源
69 0 : output_ = CreateVariable();
70 0 : uint16_t transportIdx = 0;
71 0 : if (transports.size() == 0) {
72 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh1DOneShot transports is empty"));
73 : }
74 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
75 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
76 0 : if (peerId == rankId_) {
77 0 : input_.push_back(CreateVariable());
78 0 : token_.push_back(CreateVariable());
79 : } else {
80 0 : HCCL_INFO(
81 : "[CcuContextAllReduceMesh1DOneShot] MyRank[%u], PeerId[%llu], TransportId[%u]", rankId_, peerId,
82 : transportIdx);
83 0 : CHK_PRT_RET(
84 : transports[transportIdx] == nullptr,
85 : HCCL_ERROR("[CcuContextAllReduceMesh1DOneShot] Algorithm transport ptr is null"), );
86 0 : input_.push_back(CreateVariable((*transports[transportIdx]), INPUT_XN_ID));
87 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
88 0 : transportIdx++;
89 : }
90 : }
91 0 : groupOpSize_ = CreateGroupOpSize();
92 :
93 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] InitResource end");
94 : }
95 :
96 0 : void CcuContextAllReduceMesh1DOneShot::LoadArgs()
97 : {
98 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] LoadArgs start");
99 0 : Load(input_[rankId_]);
100 0 : Load(output_);
101 0 : Load(token_[rankId_]);
102 0 : Load(groupOpSize_);
103 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] LoadArgs end");
104 0 : }
105 :
106 0 : void CcuContextAllReduceMesh1DOneShot::Presync()
107 : {
108 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] Presync start");
109 0 : uint16_t selfBit = 1 << rankId_;
110 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
111 0 : for (auto t : transports) {
112 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit);
113 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit);
114 : }
115 :
116 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit);
117 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit);
118 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] Presync end");
119 0 : }
120 :
121 0 : void CcuContextAllReduceMesh1DOneShot::Postsync()
122 : {
123 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] Postsync start");
124 0 : uint16_t selfBit = 1 << rankId_;
125 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
126 0 : for (auto t : transports) {
127 0 : RemotePost(*t, CKE_IDX_0, selfBit);
128 : }
129 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
130 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] Postsync end");
131 0 : }
132 :
133 0 : void CcuContextAllReduceMesh1DOneShot::DoGroupReduce()
134 : {
135 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] DoGroupReduce start");
136 : // 初始化地址寄存器
137 0 : std::vector<CcuRep::Memory> reduceSrc;
138 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
139 0 : reduceSrc.push_back(CreateMemory());
140 : }
141 0 : CcuRep::Memory reduceDst = CreateMemory();
142 :
143 : // 填充地址
144 0 : uint32_t dstId = 0;
145 0 : uint32_t curId = 0;
146 : // SRC
147 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
148 0 : if (rankIdx != rankId_) {
149 0 : curId = dstId;
150 0 : dstId++;
151 : } else {
152 0 : curId = rankSize_ - 1;
153 : }
154 0 : reduceSrc[curId].addr = input_[rankIdx];
155 0 : reduceSrc[curId].token = token_[rankIdx];
156 : }
157 :
158 : // DST
159 0 : reduceDst.addr = output_;
160 0 : reduceDst.token = token_[rankId_];
161 :
162 : // 执行 reduce 操作
163 0 : GroupReduce(transports, reduceDst, reduceSrc, groupOpSize_, dataType_, outputDataType_, reduceOp_);
164 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] DoGroupReduce end");
165 0 : return;
166 0 : }
167 :
168 0 : std::vector<uint64_t> CcuContextAllReduceMesh1DOneShot::GeneArgs(const CcuTaskArg& arg)
169 : {
170 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] GeneArgs start");
171 0 : const CcuTaskArgAllReduceMesh1DOneShot* taskArg = dynamic_cast<const CcuTaskArgAllReduceMesh1DOneShot*>(&arg);
172 0 : if (taskArg == nullptr) {
173 0 : THROW<NullPtrException>(StringFormat("CcuContextAllReduceMesh1DOneShot::taskArg ptr is null"));
174 : }
175 0 : uint64_t inputAddr = taskArg->inputAddr_;
176 0 : uint64_t outputAddr = taskArg->outputAddr_;
177 0 : uint64_t tokenInfo = taskArg->token_;
178 0 : uint64_t sliceSize = taskArg->sliceSize_;
179 :
180 0 : auto mainBlockGoSize = CalGoSize(sliceSize);
181 :
182 0 : HCCL_INFO(
183 : "[CcuContextAllReduceMesh1DOneShot] GeneArgs, taskArg are inputAddr[%llu], outputAddr[%llu], "
184 : "sliceSize[%llu]",
185 : inputAddr, outputAddr, sliceSize);
186 :
187 0 : std::vector<uint64_t> taskArgList{inputAddr, outputAddr, tokenInfo};
188 0 : for (auto val : mainBlockGoSize) {
189 0 : taskArgList.push_back(val);
190 : }
191 :
192 0 : HCCL_INFO("[CcuContextAllReduceMesh1DOneShot] GeneArgs end");
193 0 : return taskArgList;
194 0 : }
195 :
196 : } // namespace Hccl
|