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 "coll_all_reduce_mesh_aiv_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollAllReduceMeshAivExecutor::CollAllReduceMeshAivExecutor(const HcclDispatcher dispatcher,
16 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
17 0 : : CollAllReduceExecutor(dispatcher, topoMatcher)
18 : {
19 0 : DMAReduceFlag_ = false;
20 0 : desc_.isAivMode = true;
21 0 : desc_.deterministic = 0;
22 0 : }
23 :
24 0 : HcclResult CollAllReduceMeshAivExecutor::CalcStreamNum(u32& streamNum)
25 : {
26 0 : streamNum = 0; // AIV通信不需要申请从流
27 0 : HCCL_INFO("[CollAllReduceMeshAivExecutor][CalcStreamNum] tag[%s] streamNum[%u].", tag_.c_str(), streamNum);
28 0 : return HCCL_SUCCESS;
29 : }
30 :
31 0 : HcclResult CollAllReduceMeshAivExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
32 : {
33 0 : TransportMemType inputType = TransportMemType::RESERVED;
34 0 : TransportMemType outputType = TransportMemType::RESERVED;
35 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
36 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
37 0 : return HCCL_SUCCESS;
38 : }
39 :
40 0 : HcclResult CollAllReduceMeshAivExecutor::CalcTransportMemType(TransportMemType &inputType, TransportMemType &outputType)
41 : {
42 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
43 0 : inputType = TransportMemType::CCL_INPUT;
44 0 : outputType = TransportMemType::AIV_OUTPUT;
45 : } else {
46 0 : inputType = TransportMemType::PARAM_INPUT;
47 0 : outputType = TransportMemType::AIV_OUTPUT;
48 : }
49 0 : HCCL_INFO("[CollAllReduceMeshAivExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d].",
50 : tag_.c_str(), inputType, outputType);
51 0 : return HCCL_SUCCESS;
52 : }
53 :
54 0 : HcclResult CollAllReduceMeshAivExecutor::CalcLevel0CommInfo(TransportMemType inputType,
55 : TransportMemType outputType,
56 : std::vector<LevelNSubCommTransport>& opTransport)
57 : {
58 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
59 0 : commParaLevel0.meshSinglePlane = true;
60 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
61 0 : return HCCL_SUCCESS;
62 0 : }
63 :
64 0 : HcclResult CollAllReduceMeshAivExecutor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType)
65 : {
66 0 : numBlocks = rankSize; // 默认情况使用rankSize个AIV
67 :
68 0 : bool isOpBase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
69 0 : if (isOpBase) {
70 0 : numBlocks = NUM_BLOCKS_FACTOR_TWO * rankSize; // 单机场景,单算子AllReduce大数据使用2倍 rankSize个aiv
71 : }
72 :
73 0 : u32 bestNumBlocks = numBlocks;
74 0 : CHK_PRT_RET(numBlocks_ < rankSize,
75 : HCCL_WARNING("[CollAllReduceMeshAivExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
76 : numBlocks_, rankSize), HCCL_E_PARA);
77 0 : CHK_PRT_RET(isOpBase && numBlocks_ < bestNumBlocks,
78 : HCCL_WARNING("[CollAllReduceMeshAivExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
79 : numBlocks_, bestNumBlocks), HCCL_E_PARA);
80 :
81 0 : if (numBlocks_ < numBlocks) {
82 0 : numBlocks = numBlocks_ / rankSize * rankSize;
83 : }
84 :
85 0 : HCCL_INFO("[CollAllReduceMeshAivExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
86 : numBlocks, numBlocks_, bestNumBlocks);
87 0 : return HCCL_SUCCESS;
88 : }
89 :
90 0 : HcclResult CollAllReduceMeshAivExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
91 : {
92 0 : HcclUs startut = TIME_NOW();
93 0 : tag_ = param.tag;
94 0 : algResResp_ = &algRes;
95 :
96 0 : HcclResult ret = HCCL_SUCCESS;
97 0 : ExecMem execMem;
98 0 : execMem.count = param.DataDes.count;
99 0 : execMem.inputPtr = param.inputPtr;
100 0 : execMem.outputPtr = param.outputPtr;
101 :
102 0 : if (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
103 0 : execMem.inputMem = algRes.paramInputMem;
104 0 : execMem.outputMem = algRes.aivOutputMem;
105 0 : ret = KernelRun(param, execMem);
106 : } else {
107 0 : execMem.inputMem = algRes.cclInputMem;
108 0 : execMem.outputMem = algRes.aivOutputMem;
109 0 : ret = KernelRun(param, execMem);
110 : }
111 :
112 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
113 : HCCL_ERROR("[CollAllReduceMeshAivExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel run failed",
114 : HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
115 :
116 0 : HCCL_INFO("tag[%s], AllReduce executor orchestrate success, take time [%lld]us",
117 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
118 0 : return HCCL_SUCCESS;
119 0 : }
120 :
121 0 : HcclResult CollAllReduceMeshAivExecutor::GetAdjInfo(AlgResourceResponse& algRes, AdjInfo& adjInfo)
122 : {
123 0 : return HCCL_SUCCESS;
124 : }
125 :
126 0 : HcclResult CollAllReduceMeshAivExecutor::GetAivExecParam(const OpParam& param, AlgResourceResponse& algRes, AivSuperKernelArgs &args)
127 : {
128 0 : HcclUs startut = TIME_NOW();
129 0 : tag_ = param.tag;
130 0 : algResResp_ = &algRes;
131 :
132 0 : HcclResult ret = HCCL_SUCCESS;
133 0 : ExecMem execMem;
134 0 : execMem.count = param.DataDes.count;
135 0 : execMem.inputPtr = param.inputPtr;
136 0 : execMem.outputPtr = param.outputPtr;
137 :
138 : // 单算子大数据量
139 0 : execMem.inputMem = algRes.paramInputMem;
140 0 : execMem.outputMem = algRes.aivOutputMem;
141 :
142 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
143 :
144 0 : u32 localRank = level0CommInfo.localRank;
145 0 : u32 localRankSize = level0CommInfo.localRankSize;
146 0 : HCCL_DEBUG("[CollAllReduceMeshAivExecutor][GetAivExecParam] userRank [%d] localRank [%d]",
147 : topoAttr_.userRank, localRank);
148 :
149 0 : for (u32 i = 0; i < localRankSize; i++) {
150 0 : if (i != localRank) {
151 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(args.buffersIn[i])));
152 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(args.buffersOut[i])));
153 : } else {
154 0 : args.buffersIn[i] = execMem.inputMem.ptr();
155 0 : args.buffersOut[i] = execMem.outputMem.ptr();
156 : }
157 : }
158 :
159 0 : HCCL_INFO("SPK, buffersIn [%p] [%p] [%p] [%p]"
160 : "buffersOut [%p] [%p] [%p] [%p]", args.buffersIn[0], args.buffersIn[1], args.buffersIn[2], args.buffersIn[3],
161 : args.buffersOut[0], args.buffersOut[1], args.buffersOut[2], args.buffersOut[3]);
162 0 : args.rank = localRank;
163 0 : args.rankSize = localRankSize;
164 0 : args.len = execMem.count;
165 0 : args.dataType = param.DataDes.dataType;
166 0 : args.unitSize = SIZE_TABLE[param.DataDes.dataType];
167 0 : args.reduceOp = param.reduceType;
168 0 : args.devType = static_cast<u32>(topoAttr_.deviceType);
169 0 : HCCL_INFO("SPK [CollAllReduceMeshAivExecutor][GetAivExecParam], rank[%llu], rankSize[%llu], len[%llu],datatype[%llu], op[%llu]", args.rank, args.rankSize, args.len, args.dataType, args.reduceOp);
170 :
171 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
172 : HCCL_ERROR("[CollAllReduceMeshAivExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
173 : "run failed", HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
174 :
175 0 : HCCL_INFO("tag[%s], AllReduce executor getalgexecparam success, take time [%lld]us.",
176 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
177 0 : return HCCL_SUCCESS;
178 0 : }
179 :
180 0 : HcclResult CollAllReduceMeshAivExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
181 : {
182 0 : HCCL_INFO("[%s] AllReduce aiv enter.", __func__);
183 :
184 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
185 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
186 :
187 : void *buffersIn[MAX_RANK_SIZE];
188 : void *buffersOut[MAX_RANK_SIZE];
189 :
190 0 : u32 localRank = level0CommInfo.localRank;
191 0 : u32 localRankSize = level0CommInfo.localRankSize;
192 0 : HCCL_DEBUG("[CollAllReduceMeshAivExecutor][KernelRun] userRank [%u] localRank [%u]", topoAttr_.userRank, localRank);
193 :
194 0 : for (u32 i = 0; i < localRankSize; i++) {
195 0 : if (i != localRank) {
196 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersIn[i])));
197 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersOut[i])));
198 : } else {
199 0 : buffersIn[i] = execMem.inputMem.ptr();
200 0 : buffersOut[i] = execMem.outputMem.ptr();
201 : }
202 : }
203 :
204 0 : bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
205 0 : AivOpArgs opArgs {
206 0 : HcclCMDType::HCCL_CMD_ALLREDUCE, execMem.inputPtr, execMem.outputPtr, execMem.count,
207 0 : param.DataDes.dataType, param.reduceType, 0, isOpbase
208 0 : };
209 0 : AivTopoArgs topoArgs { localRank, localRankSize, MAX_RANK_SIZE, 0, 1, topoAttr_.deviceType };
210 : u32 numBlocks;
211 0 : CHK_PRT_RET(CalNumBlocks(numBlocks, localRankSize) != HCCL_SUCCESS,
212 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
213 : HCCL_E_PARA);
214 0 : numBlocks_ = numBlocks;
215 0 : topoArgs.identify = algoAttr_.identifier;
216 0 : HCCL_DEBUG("[CollAllReduceMeshAivExecutor][KernelRun]numBlocks is %u", numBlocks_);
217 : AivResourceArgs resourceArgs {
218 0 : param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(), numBlocks_, param.aivTag
219 0 : };
220 0 : AivAlgArgs algArgs {};
221 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
222 0 : algArgs.execTimeOutSet = true;
223 0 : struct AivProfilingInfo aivProfilingInfo;
224 0 : aivProfilingInfo.counter = opCounter_;
225 :
226 0 : HcclResult ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
227 :
228 : //设置ExecuteKernelLaunch入参缓存,第二次调用的时候会调用到缓存上去
229 0 : ExtraArgs extraArgs;
230 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, false));
231 :
232 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
233 : HCCL_ERROR("[CollAllReduceMeshAivExecutor][KernelRun]AllReduce aiv failed, return[%d]", ret), ret);
234 :
235 0 : HCCL_INFO("[CollAllReduceMeshAivExecutor][KernelRun]AllReduce aiv run success.");
236 0 : return HCCL_SUCCESS;
237 0 : }
238 :
239 : REGISTER_EXEC("AllReduceMeshAivExecutor", AllReduceMeshAiv, CollAllReduceMeshAivExecutor);
240 :
241 : } // namespace hccl
|