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