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