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