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