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