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