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